diff --git a/Tests/LibC/TestAbort.cpp b/Tests/LibC/TestAbort.cpp index 2572fc3c673..5c5f2c3a10b 100644 --- a/Tests/LibC/TestAbort.cpp +++ b/Tests/LibC/TestAbort.cpp @@ -7,6 +7,7 @@ #include #include // FIXME: Remove when `_abort` is moved to +#include #include TEST_CASE(_abort) @@ -15,6 +16,10 @@ TEST_CASE(_abort) _abort(); return Test::Crash::Failure::DidNotCrash; }); + EXPECT_CRASH_WITH_SIGNAL("This should _abort with SIGILL signal", SIGILL, [] { + _abort(); + return Test::Crash::Failure::DidNotCrash; + }); } TEST_CASE(abort) @@ -23,4 +28,8 @@ TEST_CASE(abort) abort(); return Test::Crash::Failure::DidNotCrash; }); + EXPECT_CRASH_WITH_SIGNAL("This should abort with SIGABRT signal", SIGABRT, [] { + abort(); + return Test::Crash::Failure::DidNotCrash; + }); } diff --git a/Tests/LibC/TestAssert.cpp b/Tests/LibC/TestAssert.cpp index 296485d3b91..08eae12c236 100644 --- a/Tests/LibC/TestAssert.cpp +++ b/Tests/LibC/TestAssert.cpp @@ -7,6 +7,7 @@ #include #include +#include TEST_CASE(assert) { @@ -14,4 +15,8 @@ TEST_CASE(assert) assert(!"This should assert"); return Test::Crash::Failure::DidNotCrash; }); + EXPECT_CRASH_WITH_SIGNAL("This should assert with SIGABRT signal", SIGABRT, [] { + assert(!"This should assert"); + return Test::Crash::Failure::DidNotCrash; + }); } diff --git a/Tests/LibC/TestRaise.cpp b/Tests/LibC/TestRaise.cpp index 7957ab45629..0be57627d5e 100644 --- a/Tests/LibC/TestRaise.cpp +++ b/Tests/LibC/TestRaise.cpp @@ -10,7 +10,7 @@ TEST_CASE(raise) { - EXPECT_CRASH("This should raise a SIGUSR1 signal", [] { + EXPECT_CRASH_WITH_SIGNAL("This should raise a SIGUSR1 signal", SIGUSR1, [] { raise(SIGUSR1); return Test::Crash::Failure::DidNotCrash; }); diff --git a/Userland/Libraries/LibTest/CrashTest.cpp b/Userland/Libraries/LibTest/CrashTest.cpp index c002bec1486..ac8433d9bec 100644 --- a/Userland/Libraries/LibTest/CrashTest.cpp +++ b/Userland/Libraries/LibTest/CrashTest.cpp @@ -6,6 +6,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -17,9 +18,10 @@ namespace Test { -Crash::Crash(String test_type, Function crash_function) +Crash::Crash(String test_type, Function crash_function, int crash_signal) : m_type(move(test_type)) , m_crash_function(move(crash_function)) + , m_crash_signal(crash_signal) { } @@ -49,7 +51,9 @@ bool Crash::run(RunType run_type) return do_report(Failure(WEXITSTATUS(status))); } if (WIFSIGNALED(status)) { - return do_report(WTERMSIG(status)); + int signal = WTERMSIG(status); + VERIFY(signal > 0); + return do_report(signal); } VERIFY_NOT_REACHED(); } @@ -57,7 +61,14 @@ bool Crash::run(RunType run_type) bool Crash::do_report(Report report) { - const bool pass = report.has(); + bool pass = false; + if (m_crash_signal == ANY_SIGNAL) { + pass = report.has(); + } else if (m_crash_signal > 0) { + pass = report.has() && report.get() == m_crash_signal; + } else { + VERIFY_NOT_REACHED(); + } if (pass) out("\x1B[32mPASS\x1B[0m: "); @@ -68,19 +79,28 @@ bool Crash::do_report(Report report) [&](const Failure& failure) { switch (failure) { case Failure::DidNotCrash: - outln("Did not crash!"); + out("Did not crash"); break; case Failure::UnexpectedError: - outln("Unexpected error!"); + out("Unexpected error"); break; default: VERIFY_NOT_REACHED(); } }, [&](const int& signal) { - outln("Terminated with signal {}", signal); + out("Terminated with signal {}", signal); }); + if (!pass) { + if (m_crash_signal == ANY_SIGNAL) { + out(" while expecting any signal"); + } else if (m_crash_signal > 0) { + out(" while expecting signal {}", m_crash_signal); + } + } + outln(); + return pass; } diff --git a/Userland/Libraries/LibTest/CrashTest.h b/Userland/Libraries/LibTest/CrashTest.h index 994b4060e43..eda8cc03b8a 100644 --- a/Userland/Libraries/LibTest/CrashTest.h +++ b/Userland/Libraries/LibTest/CrashTest.h @@ -26,7 +26,9 @@ public: UnexpectedError, }; - Crash(String test_type, Function crash_function); + static constexpr int ANY_SIGNAL = -1; + + Crash(String test_type, Function crash_function, int crash_signal = ANY_SIGNAL); bool run(RunType run_type = RunType::UsingChildProcess); @@ -36,6 +38,7 @@ private: String m_type; Function m_crash_function; + int m_crash_signal; }; } diff --git a/Userland/Libraries/LibTest/Macros.h b/Userland/Libraries/LibTest/Macros.h index d883da655f4..d0dca516aae 100644 --- a/Userland/Libraries/LibTest/Macros.h +++ b/Userland/Libraries/LibTest/Macros.h @@ -127,3 +127,10 @@ void current_test_case_did_fail(); if (!crash.run()) \ ::Test::current_test_case_did_fail(); \ } while (false) + +#define EXPECT_CRASH_WITH_SIGNAL(test_message, signal, test_func) \ + do { \ + Test::Crash crash(test_message, test_func, (signal)); \ + if (!crash.run()) \ + ::Test::current_test_case_did_fail(); \ + } while (false)