mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 01:47:34 +00:00
Tests: Establish root Tests directory, move Userland/Tests there
With the goal of centralizing all tests in the system, this is a first step to establish a Tests sub-tree. It will contain all of the unit tests and test harnesses for the various components in the system.
This commit is contained in:
parent
6e641fadfa
commit
fd0dbd1ebf
49 changed files with 1 additions and 1 deletions
117
Tests/LibC/TestLibCMkTemp.cpp
Normal file
117
Tests/LibC/TestLibCMkTemp.cpp
Normal file
|
@ -0,0 +1,117 @@
|
|||
/*
|
||||
* Copyright (c) 2021, the SerenityOS developers.
|
||||
* Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibTest/TestCase.h>
|
||||
#include <fcntl.h>
|
||||
#include <mman.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
TEST_CASE(test_mktemp_unique_filename)
|
||||
{
|
||||
u8* ptr = (u8*)mmap(nullptr, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
||||
EXPECT(ptr != MAP_FAILED);
|
||||
|
||||
if (fork() == 0) {
|
||||
char path[] = "/tmp/test.mktemp.XXXXXX";
|
||||
auto temp_path = String::formatted("{}", mktemp(path));
|
||||
EXPECT(temp_path.characters());
|
||||
unlink(path);
|
||||
|
||||
memcpy(&ptr[0], temp_path.characters(), temp_path.length());
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
} else {
|
||||
wait(NULL);
|
||||
|
||||
auto path1 = String::formatted("{}", reinterpret_cast<const char*>(ptr));
|
||||
|
||||
char path[] = "/tmp/test.mktemp.XXXXXX";
|
||||
auto path2 = String::formatted("{}", mktemp(path));
|
||||
EXPECT(path2.characters());
|
||||
unlink(path);
|
||||
|
||||
EXPECT_NE(path1, path2);
|
||||
}
|
||||
|
||||
munmap(ptr, sizeof(*ptr));
|
||||
}
|
||||
|
||||
TEST_CASE(test_mkdtemp_unique_filename)
|
||||
{
|
||||
u8* ptr = (u8*)mmap(nullptr, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
||||
EXPECT_NE(ptr, MAP_FAILED);
|
||||
|
||||
if (fork() == 0) {
|
||||
char path[] = "/tmp/test.mkdtemp.XXXXXX";
|
||||
auto temp_path = String::formatted("{}", mkdtemp(path));
|
||||
EXPECT(temp_path.characters());
|
||||
rmdir(path);
|
||||
|
||||
memcpy(&ptr[0], temp_path.characters(), temp_path.length());
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
} else {
|
||||
wait(NULL);
|
||||
|
||||
auto path1 = String::formatted("{}", reinterpret_cast<const char*>(ptr));
|
||||
|
||||
char path[] = "/tmp/test.mkdtemp.XXXXXX";
|
||||
auto path2 = String::formatted("{}", mkdtemp(path));
|
||||
EXPECT(path2.characters());
|
||||
rmdir(path);
|
||||
|
||||
EXPECT_NE(path1, path2);
|
||||
}
|
||||
|
||||
munmap(ptr, sizeof(*ptr));
|
||||
}
|
||||
|
||||
TEST_CASE(test_mkstemp_unique_filename)
|
||||
{
|
||||
u8* ptr = (u8*)mmap(nullptr, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
||||
EXPECT_NE(ptr, MAP_FAILED);
|
||||
|
||||
if (fork() == 0) {
|
||||
char path[] = "/tmp/test.mkstemp.XXXXXX";
|
||||
auto fd = mkstemp(path);
|
||||
EXPECT_NE(fd, -1);
|
||||
|
||||
auto temp_path = Core::File::read_link(String::formatted("/proc/{}/fd/{}", getpid(), fd));
|
||||
EXPECT(temp_path.characters());
|
||||
|
||||
close(fd);
|
||||
unlink(path);
|
||||
|
||||
memcpy(&ptr[0], temp_path.characters(), temp_path.length());
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
} else {
|
||||
wait(NULL);
|
||||
|
||||
auto path1 = String::formatted("{}", reinterpret_cast<const char*>(ptr));
|
||||
|
||||
char path[] = "/tmp/test.mkstemp.XXXXXX";
|
||||
auto fd = mkstemp(path);
|
||||
EXPECT(fd != -1);
|
||||
|
||||
auto path2 = Core::File::read_link(String::formatted("/proc/{}/fd/{}", getpid(), fd));
|
||||
EXPECT(path2.characters());
|
||||
|
||||
close(fd);
|
||||
unlink(path);
|
||||
|
||||
EXPECT_NE(path1, path2);
|
||||
}
|
||||
|
||||
munmap(ptr, sizeof(*ptr));
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue