From 8d8535f29763362de8a59dc0bcdd4ca63996a9a4 Mon Sep 17 00:00:00 2001 From: Brian Gianforcaro Date: Wed, 28 Apr 2021 19:59:19 -0700 Subject: [PATCH] Tests: Convert LibC mktemp tests to be LibTest based. --- Userland/Tests/LibC/CMakeLists.txt | 1 + ...unique-filename.cpp => TestLibCMkTemp.cpp} | 54 +++++++------------ 2 files changed, 21 insertions(+), 34 deletions(-) rename Userland/Tests/LibC/{stdlib-generate-unique-filename.cpp => TestLibCMkTemp.cpp} (70%) diff --git a/Userland/Tests/LibC/CMakeLists.txt b/Userland/Tests/LibC/CMakeLists.txt index 45cbf3f5d8..070badc55c 100644 --- a/Userland/Tests/LibC/CMakeLists.txt +++ b/Userland/Tests/LibC/CMakeLists.txt @@ -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") diff --git a/Userland/Tests/LibC/stdlib-generate-unique-filename.cpp b/Userland/Tests/LibC/TestLibCMkTemp.cpp similarity index 70% rename from Userland/Tests/LibC/stdlib-generate-unique-filename.cpp rename to Userland/Tests/LibC/TestLibCMkTemp.cpp index 06f785c28a..85bdb03420 100644 --- a/Userland/Tests/LibC/stdlib-generate-unique-filename.cpp +++ b/Userland/Tests/LibC/TestLibCMkTemp.cpp @@ -1,12 +1,13 @@ /* * Copyright (c) 2021, the SerenityOS developers. + * Copyright (c) 2021, Brian Gianforcaro * * SPDX-License-Identifier: BSD-2-Clause */ #include #include -#include +#include #include #include #include @@ -15,15 +16,15 @@ #include #include -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; -}