LibTest: Add EXPECT_CRASH_WITH_SIGNAL

This commit is contained in:
Michel Hermier 2021-12-17 18:12:47 +01:00 committed by Brian Gianforcaro
parent b1b94692e6
commit 4c6e826c05
Notes: sideshowbarker 2024-07-17 22:34:51 +09:00
6 changed files with 52 additions and 8 deletions

View file

@ -7,6 +7,7 @@
#include <LibTest/TestCase.h>
#include <assert.h> // FIXME: Remove when `_abort` is moved to <stdlib.h>
#include <signal.h>
#include <stdlib.h>
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;
});
}

View file

@ -7,6 +7,7 @@
#include <LibTest/TestCase.h>
#include <assert.h>
#include <signal.h>
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;
});
}

View file

@ -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;
});

View file

@ -6,6 +6,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Assertions.h>
#include <AK/Platform.h>
#include <LibTest/CrashTest.h>
#include <sys/wait.h>
@ -17,9 +18,10 @@
namespace Test {
Crash::Crash(String test_type, Function<Crash::Failure()> crash_function)
Crash::Crash(String test_type, Function<Crash::Failure()> 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<int>();
bool pass = false;
if (m_crash_signal == ANY_SIGNAL) {
pass = report.has<int>();
} else if (m_crash_signal > 0) {
pass = report.has<int>() && report.get<int>() == 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;
}

View file

@ -26,7 +26,9 @@ public:
UnexpectedError,
};
Crash(String test_type, Function<Crash::Failure()> crash_function);
static constexpr int ANY_SIGNAL = -1;
Crash(String test_type, Function<Crash::Failure()> crash_function, int crash_signal = ANY_SIGNAL);
bool run(RunType run_type = RunType::UsingChildProcess);
@ -36,6 +38,7 @@ private:
String m_type;
Function<Crash::Failure()> m_crash_function;
int m_crash_signal;
};
}

View file

@ -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)