mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 14:57:35 +00:00
Tests: Convert LibC mktemp tests to be LibTest based.
This commit is contained in:
parent
2cef015528
commit
8d8535f297
2 changed files with 21 additions and 34 deletions
|
@ -2,6 +2,7 @@ set(TEST_SOURCES
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/snprintf-correctness.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/snprintf-correctness.cpp
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/strlcpy-correctness.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/strlcpy-correctness.cpp
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/TestLibCTime.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/TestLibCTime.cpp
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/TestLibCMkTemp.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
file(GLOB CMD_SOURCES CONFIGURE_DEPENDS "*.cpp")
|
file(GLOB CMD_SOURCES CONFIGURE_DEPENDS "*.cpp")
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2021, the SerenityOS developers.
|
* Copyright (c) 2021, the SerenityOS developers.
|
||||||
|
* Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <assert.h>
|
#include <LibTest/TestCase.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <mman.h>
|
#include <mman.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -15,15 +16,15 @@
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static void test_mktemp_unique_filename()
|
TEST_CASE(test_mktemp_unique_filename)
|
||||||
{
|
{
|
||||||
u8* ptr = (u8*)mmap(nullptr, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
u8* ptr = (u8*)mmap(nullptr, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
||||||
assert(ptr != MAP_FAILED);
|
EXPECT(ptr != MAP_FAILED);
|
||||||
|
|
||||||
if (fork() == 0) {
|
if (fork() == 0) {
|
||||||
char path[] = "/tmp/test.mktemp.XXXXXX";
|
char path[] = "/tmp/test.mktemp.XXXXXX";
|
||||||
auto temp_path = String::formatted("{}", mktemp(path));
|
auto temp_path = String::formatted("{}", mktemp(path));
|
||||||
assert(temp_path.characters());
|
EXPECT(temp_path.characters());
|
||||||
unlink(path);
|
unlink(path);
|
||||||
|
|
||||||
memcpy(&ptr[0], temp_path.characters(), temp_path.length());
|
memcpy(&ptr[0], temp_path.characters(), temp_path.length());
|
||||||
|
@ -36,24 +37,24 @@ static void test_mktemp_unique_filename()
|
||||||
|
|
||||||
char path[] = "/tmp/test.mktemp.XXXXXX";
|
char path[] = "/tmp/test.mktemp.XXXXXX";
|
||||||
auto path2 = String::formatted("{}", mktemp(path));
|
auto path2 = String::formatted("{}", mktemp(path));
|
||||||
assert(path2.characters());
|
EXPECT(path2.characters());
|
||||||
unlink(path);
|
unlink(path);
|
||||||
|
|
||||||
assert(path1 != path2);
|
EXPECT_NE(path1, path2);
|
||||||
}
|
}
|
||||||
|
|
||||||
munmap(ptr, sizeof(*ptr));
|
munmap(ptr, sizeof(*ptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_mkdtemp_unique_filename()
|
TEST_CASE(test_mkdtemp_unique_filename)
|
||||||
{
|
{
|
||||||
u8* ptr = (u8*)mmap(nullptr, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
u8* ptr = (u8*)mmap(nullptr, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
||||||
assert(ptr != MAP_FAILED);
|
EXPECT_NE(ptr, MAP_FAILED);
|
||||||
|
|
||||||
if (fork() == 0) {
|
if (fork() == 0) {
|
||||||
char path[] = "/tmp/test.mkdtemp.XXXXXX";
|
char path[] = "/tmp/test.mkdtemp.XXXXXX";
|
||||||
auto temp_path = String::formatted("{}", mkdtemp(path));
|
auto temp_path = String::formatted("{}", mkdtemp(path));
|
||||||
assert(temp_path.characters());
|
EXPECT(temp_path.characters());
|
||||||
rmdir(path);
|
rmdir(path);
|
||||||
|
|
||||||
memcpy(&ptr[0], temp_path.characters(), temp_path.length());
|
memcpy(&ptr[0], temp_path.characters(), temp_path.length());
|
||||||
|
@ -66,26 +67,27 @@ static void test_mkdtemp_unique_filename()
|
||||||
|
|
||||||
char path[] = "/tmp/test.mkdtemp.XXXXXX";
|
char path[] = "/tmp/test.mkdtemp.XXXXXX";
|
||||||
auto path2 = String::formatted("{}", mkdtemp(path));
|
auto path2 = String::formatted("{}", mkdtemp(path));
|
||||||
assert(path2.characters());
|
EXPECT(path2.characters());
|
||||||
rmdir(path);
|
rmdir(path);
|
||||||
|
|
||||||
assert(path1 != path2);
|
EXPECT_NE(path1, path2);
|
||||||
}
|
}
|
||||||
|
|
||||||
munmap(ptr, sizeof(*ptr));
|
munmap(ptr, sizeof(*ptr));
|
||||||
}
|
}
|
||||||
static void test_mkstemp_unique_filename()
|
|
||||||
|
TEST_CASE(test_mkstemp_unique_filename)
|
||||||
{
|
{
|
||||||
u8* ptr = (u8*)mmap(nullptr, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
u8* ptr = (u8*)mmap(nullptr, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
||||||
assert(ptr != MAP_FAILED);
|
EXPECT_NE(ptr, MAP_FAILED);
|
||||||
|
|
||||||
if (fork() == 0) {
|
if (fork() == 0) {
|
||||||
char path[] = "/tmp/test.mkstemp.XXXXXX";
|
char path[] = "/tmp/test.mkstemp.XXXXXX";
|
||||||
auto fd = mkstemp(path);
|
auto fd = mkstemp(path);
|
||||||
assert(fd != -1);
|
EXPECT_NE(fd, -1);
|
||||||
|
|
||||||
auto temp_path = Core::File::read_link(String::formatted("/proc/{}/fd/{}", getpid(), fd));
|
auto temp_path = Core::File::read_link(String::formatted("/proc/{}/fd/{}", getpid(), fd));
|
||||||
assert(temp_path.characters());
|
EXPECT(temp_path.characters());
|
||||||
|
|
||||||
close(fd);
|
close(fd);
|
||||||
unlink(path);
|
unlink(path);
|
||||||
|
@ -100,32 +102,16 @@ static void test_mkstemp_unique_filename()
|
||||||
|
|
||||||
char path[] = "/tmp/test.mkstemp.XXXXXX";
|
char path[] = "/tmp/test.mkstemp.XXXXXX";
|
||||||
auto fd = mkstemp(path);
|
auto fd = mkstemp(path);
|
||||||
assert(fd != -1);
|
EXPECT(fd != -1);
|
||||||
|
|
||||||
auto path2 = Core::File::read_link(String::formatted("/proc/{}/fd/{}", getpid(), fd));
|
auto path2 = Core::File::read_link(String::formatted("/proc/{}/fd/{}", getpid(), fd));
|
||||||
assert(path2.characters());
|
EXPECT(path2.characters());
|
||||||
|
|
||||||
close(fd);
|
close(fd);
|
||||||
unlink(path);
|
unlink(path);
|
||||||
|
|
||||||
assert(path1 != path2);
|
EXPECT_NE(path1, path2);
|
||||||
}
|
}
|
||||||
|
|
||||||
munmap(ptr, sizeof(*ptr));
|
munmap(ptr, sizeof(*ptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
#define RUNTEST(x) \
|
|
||||||
{ \
|
|
||||||
printf("Running " #x " ...\n"); \
|
|
||||||
x(); \
|
|
||||||
printf("Success!\n"); \
|
|
||||||
}
|
|
||||||
RUNTEST(test_mktemp_unique_filename);
|
|
||||||
RUNTEST(test_mkstemp_unique_filename);
|
|
||||||
RUNTEST(test_mkdtemp_unique_filename);
|
|
||||||
printf("PASS\n");
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue