diff options
author | Brian Gianforcaro <bgianf@serenityos.org> | 2021-04-28 19:59:19 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-04-29 10:37:26 +0200 |
commit | 8d8535f29763362de8a59dc0bcdd4ca63996a9a4 (patch) | |
tree | d30781b3131def12a261f232043b79202d1ac54d /Userland/Tests | |
parent | 2cef015528db81a2ea1280d689d23a8c7a857487 (diff) | |
download | serenity-8d8535f29763362de8a59dc0bcdd4ca63996a9a4.zip |
Tests: Convert LibC mktemp tests to be LibTest based.
Diffstat (limited to 'Userland/Tests')
-rw-r--r-- | Userland/Tests/LibC/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Userland/Tests/LibC/TestLibCMkTemp.cpp (renamed from Userland/Tests/LibC/stdlib-generate-unique-filename.cpp) | 54 |
2 files changed, 21 insertions, 34 deletions
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 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 <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; -} |