mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 20:07:36 +00:00
LibTest: Add EXPECT_CRASH_WITH_SIGNAL
This commit is contained in:
parent
b1b94692e6
commit
4c6e826c05
6 changed files with 52 additions and 8 deletions
|
@ -7,6 +7,7 @@
|
||||||
#include <LibTest/TestCase.h>
|
#include <LibTest/TestCase.h>
|
||||||
|
|
||||||
#include <assert.h> // FIXME: Remove when `_abort` is moved to <stdlib.h>
|
#include <assert.h> // FIXME: Remove when `_abort` is moved to <stdlib.h>
|
||||||
|
#include <signal.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
TEST_CASE(_abort)
|
TEST_CASE(_abort)
|
||||||
|
@ -15,6 +16,10 @@ TEST_CASE(_abort)
|
||||||
_abort();
|
_abort();
|
||||||
return Test::Crash::Failure::DidNotCrash;
|
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)
|
TEST_CASE(abort)
|
||||||
|
@ -23,4 +28,8 @@ TEST_CASE(abort)
|
||||||
abort();
|
abort();
|
||||||
return Test::Crash::Failure::DidNotCrash;
|
return Test::Crash::Failure::DidNotCrash;
|
||||||
});
|
});
|
||||||
|
EXPECT_CRASH_WITH_SIGNAL("This should abort with SIGABRT signal", SIGABRT, [] {
|
||||||
|
abort();
|
||||||
|
return Test::Crash::Failure::DidNotCrash;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#include <LibTest/TestCase.h>
|
#include <LibTest/TestCase.h>
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
TEST_CASE(assert)
|
TEST_CASE(assert)
|
||||||
{
|
{
|
||||||
|
@ -14,4 +15,8 @@ TEST_CASE(assert)
|
||||||
assert(!"This should assert");
|
assert(!"This should assert");
|
||||||
return Test::Crash::Failure::DidNotCrash;
|
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;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
|
|
||||||
TEST_CASE(raise)
|
TEST_CASE(raise)
|
||||||
{
|
{
|
||||||
EXPECT_CRASH("This should raise a SIGUSR1 signal", [] {
|
EXPECT_CRASH_WITH_SIGNAL("This should raise a SIGUSR1 signal", SIGUSR1, [] {
|
||||||
raise(SIGUSR1);
|
raise(SIGUSR1);
|
||||||
return Test::Crash::Failure::DidNotCrash;
|
return Test::Crash::Failure::DidNotCrash;
|
||||||
});
|
});
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/Assertions.h>
|
||||||
#include <AK/Platform.h>
|
#include <AK/Platform.h>
|
||||||
#include <LibTest/CrashTest.h>
|
#include <LibTest/CrashTest.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
|
@ -17,9 +18,10 @@
|
||||||
|
|
||||||
namespace Test {
|
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_type(move(test_type))
|
||||||
, m_crash_function(move(crash_function))
|
, 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)));
|
return do_report(Failure(WEXITSTATUS(status)));
|
||||||
}
|
}
|
||||||
if (WIFSIGNALED(status)) {
|
if (WIFSIGNALED(status)) {
|
||||||
return do_report(WTERMSIG(status));
|
int signal = WTERMSIG(status);
|
||||||
|
VERIFY(signal > 0);
|
||||||
|
return do_report(signal);
|
||||||
}
|
}
|
||||||
VERIFY_NOT_REACHED();
|
VERIFY_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
@ -57,7 +61,14 @@ bool Crash::run(RunType run_type)
|
||||||
|
|
||||||
bool Crash::do_report(Report report)
|
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)
|
if (pass)
|
||||||
out("\x1B[32mPASS\x1B[0m: ");
|
out("\x1B[32mPASS\x1B[0m: ");
|
||||||
|
@ -68,19 +79,28 @@ bool Crash::do_report(Report report)
|
||||||
[&](const Failure& failure) {
|
[&](const Failure& failure) {
|
||||||
switch (failure) {
|
switch (failure) {
|
||||||
case Failure::DidNotCrash:
|
case Failure::DidNotCrash:
|
||||||
outln("Did not crash!");
|
out("Did not crash");
|
||||||
break;
|
break;
|
||||||
case Failure::UnexpectedError:
|
case Failure::UnexpectedError:
|
||||||
outln("Unexpected error!");
|
out("Unexpected error");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
VERIFY_NOT_REACHED();
|
VERIFY_NOT_REACHED();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[&](const int& signal) {
|
[&](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;
|
return pass;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,9 @@ public:
|
||||||
UnexpectedError,
|
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);
|
bool run(RunType run_type = RunType::UsingChildProcess);
|
||||||
|
|
||||||
|
@ -36,6 +38,7 @@ private:
|
||||||
|
|
||||||
String m_type;
|
String m_type;
|
||||||
Function<Crash::Failure()> m_crash_function;
|
Function<Crash::Failure()> m_crash_function;
|
||||||
|
int m_crash_signal;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -127,3 +127,10 @@ void current_test_case_did_fail();
|
||||||
if (!crash.run()) \
|
if (!crash.run()) \
|
||||||
::Test::current_test_case_did_fail(); \
|
::Test::current_test_case_did_fail(); \
|
||||||
} while (false)
|
} 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)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue