mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-06 02:55:49 +03:00
Tests: Convert LibC mktemp tests to be LibTest based.
This commit is contained in:
parent
2cef015528
commit
8d8535f297
Notes:
sideshowbarker
2024-07-18 18:55:21 +09:00
Author: https://github.com/bgianfo Commit: https://github.com/SerenityOS/serenity/commit/8d8535f2976 Pull-request: https://github.com/SerenityOS/serenity/pull/6727 Reviewed-by: https://github.com/linusg
@ -2,6 +2,7 @@ set(TEST_SOURCES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/snprintf-correctness.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/strlcpy-correctness.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/TestLibCTime.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/TestLibCMkTemp.cpp
|
||||
)
|
||||
|
||||
file(GLOB CMD_SOURCES CONFIGURE_DEPENDS "*.cpp")
|
||||
|
@ -1,12 +1,13 @@
|
||||
/*
|
||||
* 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 <assert.h>
|
||||
#include <LibTest/TestCase.h>
|
||||
#include <fcntl.h>
|
||||
#include <mman.h>
|
||||
#include <stdio.h>
|
||||
@ -15,15 +16,15 @@
|
||||
#include <sys/wait.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);
|
||||
assert(ptr != MAP_FAILED);
|
||||
EXPECT(ptr != MAP_FAILED);
|
||||
|
||||
if (fork() == 0) {
|
||||
char path[] = "/tmp/test.mktemp.XXXXXX";
|
||||
auto temp_path = String::formatted("{}", mktemp(path));
|
||||
assert(temp_path.characters());
|
||||
EXPECT(temp_path.characters());
|
||||
unlink(path);
|
||||
|
||||
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";
|
||||
auto path2 = String::formatted("{}", mktemp(path));
|
||||
assert(path2.characters());
|
||||
EXPECT(path2.characters());
|
||||
unlink(path);
|
||||
|
||||
assert(path1 != path2);
|
||||
EXPECT_NE(path1, path2);
|
||||
}
|
||||
|
||||
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);
|
||||
assert(ptr != MAP_FAILED);
|
||||
EXPECT_NE(ptr, MAP_FAILED);
|
||||
|
||||
if (fork() == 0) {
|
||||
char path[] = "/tmp/test.mkdtemp.XXXXXX";
|
||||
auto temp_path = String::formatted("{}", mkdtemp(path));
|
||||
assert(temp_path.characters());
|
||||
EXPECT(temp_path.characters());
|
||||
rmdir(path);
|
||||
|
||||
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";
|
||||
auto path2 = String::formatted("{}", mkdtemp(path));
|
||||
assert(path2.characters());
|
||||
EXPECT(path2.characters());
|
||||
rmdir(path);
|
||||
|
||||
assert(path1 != path2);
|
||||
EXPECT_NE(path1, path2);
|
||||
}
|
||||
|
||||
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);
|
||||
assert(ptr != MAP_FAILED);
|
||||
EXPECT_NE(ptr, MAP_FAILED);
|
||||
|
||||
if (fork() == 0) {
|
||||
char path[] = "/tmp/test.mkstemp.XXXXXX";
|
||||
auto fd = mkstemp(path);
|
||||
assert(fd != -1);
|
||||
EXPECT_NE(fd, -1);
|
||||
|
||||
auto temp_path = Core::File::read_link(String::formatted("/proc/{}/fd/{}", getpid(), fd));
|
||||
assert(temp_path.characters());
|
||||
EXPECT(temp_path.characters());
|
||||
|
||||
close(fd);
|
||||
unlink(path);
|
||||
@ -100,32 +102,16 @@ static void test_mkstemp_unique_filename()
|
||||
|
||||
char path[] = "/tmp/test.mkstemp.XXXXXX";
|
||||
auto fd = mkstemp(path);
|
||||
assert(fd != -1);
|
||||
EXPECT(fd != -1);
|
||||
|
||||
auto path2 = Core::File::read_link(String::formatted("/proc/{}/fd/{}", getpid(), fd));
|
||||
assert(path2.characters());
|
||||
EXPECT(path2.characters());
|
||||
|
||||
close(fd);
|
||||
unlink(path);
|
||||
|
||||
assert(path1 != path2);
|
||||
EXPECT_NE(path1, path2);
|
||||
}
|
||||
|
||||
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…
Reference in New Issue
Block a user