Skip to content

Latest commit

 

History

History
120 lines (91 loc) · 3.43 KB

death.md

File metadata and controls

120 lines (91 loc) · 3.43 KB

samples/death

DrMock 0.4 introduces death tests. A death test checks if a certain statement will cause the process to raise a certain signal. This may be used to assert that in certain unrecoverable situations, the program exits before causing further damage. Death tests are currently not available on Windows.

This sample demonstrates the death testing capabilities of DrMock.

Table of contents

Project structure

samples/death
│   CMakeLists.txt
│   Makefile
│   deathTest.cpp

Requirements

This project requires an installation of DrMock in build/install/ or a path container in the CMAKE_PREFIX_PATH variable. If your installation of DrMock is located elsewhere, you must change the value of CMAKE_PREFIX_PATH.

Source code

Open deathTest.cpp! The DRTEST_ASSERT_DEATH(statement, expected) macro checks if executing statement will cause the signal expected to be raised. As with DRTEST_ASSERT_THROW, statement may contain multiple lines of code, if they are seperated by semicolons (see below).

In the test catch_segfault, we test the classic segmentation fault scenario, dereferencing a nullptr:

DRTEST_TEST(catch_segfault)
{
  DRTEST_ASSERT_DEATH(
      int* foo = nullptr;
      *foo = 0,
      SIGSEGV
    );
}

We expect this to raise the SIGSEGV signal. The test will verify this.

Running the tests

Do make. This should yield the following:

    Start 1: deathTest
1/1 Test #1: deathTest ........................   Passed    0.00 sec

100% tests passed, 0 tests failed out of 1

Total Test time (real) =   0.01 sec

Details

Supported signals

The following POSIX signals may be caught using DRTEST_ASSERT_DEATH:

SIGABRT, SIGALRM,   SIGBUS,  SIGCHLD,
SIGCONT, SIGFPE,    SIGHUP,  SIGILL,
SIGINT,  SIGPIPE,   SIGPROF, SIGQUIT,
SIGSEGV, SIGTSTP,   SIGSYS,  SIGTERM,
SIGTRAP, SIGTTIN,   SIGTTOU, SIGURG,
SIGUSR2, SIGVTALRM, SIGXCPU, SIGXFSZ

Note that SIGKILL, SIGSTOP and SIGUSR1 are not supported.

clone, fork, signal, multi-threading

Using clone(), fork(), signal() or multi-threading are not allowed when using DRTEST_ASSERT_DEATH.

Log messages

Although DRTEST_ASSERT_DEATH may be used to catch SIGABRT raised by a failed assertion, it will not catch log messages sent by the assert macro. Depending on the implementation, even on a successful DRTEST_ASSERT_DEATH, you may receive a log message of the following kind:

Assertion failed: (false), function death_success, file /path/to/test, line 213.