Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing for crash with google test

In Google Test I would like to be able to do something like this:

void ImNotNiceToPointers( void* p )
{
  ((int*)p) [5] = 1;
}

TEST( Bla, BlaBla )
{
  EXPECT_NO_CRASH( ImNotNiceToPointers(NULL) );
}

And I would like the output to show error that the statement actually made the process die abnormally.

Is there any support for this in Google Test? I'm pretty sure how I would implement it myself, so I'm almost certain it's possible.

like image 665
sharkin Avatar asked Oct 28 '25 13:10

sharkin


1 Answers

You can use a death test to isolate a crash:

EXPECT_EXIT(ImNotNiceToPointers(p); exit(0), ExitedWithCode(0), '');

However, I recommend against using death tests. Using a death test incurs the overhead of launching a subprocess whether there is a crash or not. But if you simply leave your code as is and your test crashes, you will know, and will be able to fix it. Tracing the origin of the crash is easy with the help of tools like Valgrind or Dr. Memory.

like image 59
VladLosev Avatar answered Oct 31 '25 12:10

VladLosev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!