1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <gtest/gtest.h>
#include <exception>
#include "lix/libmain/crash-handler.hh"

namespace nix {

class OopsException : public std::exception
{
    const char * msg;

public:
    OopsException(const char * msg) : msg(msg) {}
    const char * what() const noexcept override
    {
        return msg;
    }
};

void causeCrashForTesting(std::function<void()> fixture)
{
    registerCrashHandler();
    std::cerr << "time to crash\n";
    try {
        fixture();
    } catch (...) {
        std::terminate();
    }
}

TEST(CrashHandler, exceptionName)
{
    ASSERT_DEATH(
        causeCrashForTesting([]() {
            throw OopsException{"lol oops"}; // NOLINT(lix-foreign-exceptions)
        }),
        "time to crash\nLix crashed.*OopsException: lol oops"
    );
}

TEST(CrashHandler, unknownTerminate)
{
    ASSERT_DEATH(
        causeCrashForTesting([]() { std::terminate(); }),
        "time to crash\nLix crashed.*std::terminate\\(\\) called without exception"
    );
}

TEST(CrashHandler, nonStdException)
{
    ASSERT_DEATH(
        causeCrashForTesting([]() {
            // NOLINTNEXTLINE(hicpp-exception-baseclass, lix-foreign-exceptions): intentional
            throw 4;
        }),
        "time to crash\nLix crashed.*Unknown exception! Spooky\\."
    );
}

}