Kea 2.7.5
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.

Namespaces

namespace  isc
 Defines the logger used by the top-level component of kea-lfc.
 
namespace  isc::util
 
namespace  isc::util::unittests
 

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.

Example:

...
TEST(SomeTest, newException) {
try {
// this will do 'new Foo()' internally and should throw
createFoo();
ASSERT_FALSE(true) << "Expected throw on new";
} catch (const std::bad_alloc&) {
// do some integrity check, etc, if necessary
}
}
size_t throw_size_on_new
The allocation size that triggers an exception in the special operator new.
Definition newhook.cc:42
bool force_throw_on_new
Switch to enable the use of special operator new.
Definition newhook.cc:41
Enable the use of special operator new that throws for testing.

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.