Kea  1.9.9-git
newhook.h File Reference

Enable the use of special operator new that throws for testing. More...

+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.


 Defines the logger used by the top-level component of kea-dhcp-ddns.

Detailed Description

Enable the use of special operator new that throws for testing.

This small utility allows a test case to force the global operator new to throw for a given size to test a case where memory allocation fails (which normally doesn't happen). To enable the feature, everything must be built with defining ENABLE_CUSTOM_OPERATOR_NEW beforehand, and set force_throw_on_new to true and throw_size_on_new to the size of data that should trigger the exception, immediately before starting the specific test that needs the exception.


TEST(SomeTest, newException) {
try {
// this will do 'new Foo()' internally and should throw
ASSERT_FALSE(true) << "Expected throw on new";
} catch (const std::bad_alloc&) {
// do some integrity check, etc, if necessary

Replacing the global operator new (and delete) is a dangerous technique, and triggering an exception solely based on the allocation size is not reliable, so this feature is disabled by default two-fold: The ENABLE_CUSTOM_OPERATOR_NEW build time variable, and run-time force_throw_on_new.

Definition in file newhook.h.