2018-05-17 07:31:43 +02:00
|
|
|
#include <gmock/gmock.h>
|
2018-05-19 07:29:41 +02:00
|
|
|
#include <csignal>
|
2018-05-17 07:31:43 +02:00
|
|
|
#include "cpp-utils/assert/backtrace.h"
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using testing::HasSubstr;
|
|
|
|
|
|
|
|
TEST(BacktraceTest, ContainsExecutableName) {
|
|
|
|
string backtrace = cpputils::backtrace();
|
|
|
|
EXPECT_THAT(backtrace, HasSubstr("cpp-utils-test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BacktraceTest, ContainsTopLevelLine) {
|
|
|
|
string backtrace = cpputils::backtrace();
|
|
|
|
EXPECT_THAT(backtrace, HasSubstr("BacktraceTest"));
|
|
|
|
EXPECT_THAT(backtrace, HasSubstr("ContainsTopLevelLine"));
|
|
|
|
}
|
2018-05-17 07:42:03 +02:00
|
|
|
|
|
|
|
namespace {
|
2018-05-19 07:29:41 +02:00
|
|
|
void nullptr_access() {
|
|
|
|
cpputils::showBacktraceOnCrash();
|
2018-05-17 07:42:03 +02:00
|
|
|
int* ptr = nullptr;
|
2018-05-19 07:29:41 +02:00
|
|
|
*ptr = 5;
|
|
|
|
}
|
|
|
|
void raise_signal(int signal) {
|
|
|
|
cpputils::showBacktraceOnCrash();
|
|
|
|
::raise(signal);
|
2018-05-17 07:42:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-19 07:29:41 +02:00
|
|
|
TEST(BacktraceTest, ShowBacktraceOnNullptrAccess) {
|
2018-05-17 07:42:03 +02:00
|
|
|
EXPECT_DEATH(
|
2018-05-19 07:29:41 +02:00
|
|
|
nullptr_access(),
|
2018-05-17 07:42:03 +02:00
|
|
|
"cpputils::backtrace"
|
|
|
|
);
|
|
|
|
}
|
2018-05-19 07:29:41 +02:00
|
|
|
|
|
|
|
TEST(BacktraceTest, ShowBacktraceOnSigSegv) {
|
|
|
|
EXPECT_DEATH(
|
|
|
|
raise_signal(SIGSEGV),
|
|
|
|
"cpputils::backtrace"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BacktraceTest, ShowBacktraceOnSigAbrt) {
|
|
|
|
EXPECT_DEATH(
|
|
|
|
raise_signal(SIGABRT),
|
|
|
|
"cpputils::backtrace"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BacktraceTest, ShowBacktraceOnSigIll) {
|
|
|
|
EXPECT_DEATH(
|
|
|
|
raise_signal(SIGILL),
|
|
|
|
"cpputils::backtrace"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BacktraceTest, ShowBacktraceOnSigSegv_ShowsCorrectSignalName) {
|
|
|
|
EXPECT_DEATH(
|
|
|
|
raise_signal(SIGSEGV),
|
|
|
|
"SIGSEGV"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BacktraceTest, ShowBacktraceOnSigAbrt_ShowsCorrectSignalName) {
|
|
|
|
EXPECT_DEATH(
|
|
|
|
raise_signal(SIGABRT),
|
|
|
|
"SIGABRT"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(BacktraceTest, ShowBacktraceOnSigIll_ShowsCorrectSignalName) {
|
|
|
|
EXPECT_DEATH(
|
|
|
|
raise_signal(SIGILL),
|
|
|
|
"SIGILL"
|
|
|
|
);
|
|
|
|
}
|