Debian Codegraph @

tests/CppUTest/MemoryLeakWarningTest.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the <organization> nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "CppUTest/TestHarness.h"
29 #include "CppUTest/TestRegistry.h"
30 #include "CppUTest/TestOutput.h"
31 #include "CppUTest/MemoryLeakWarningPlugin.h"
32 #include "CppUTest/MemoryLeakDetector.h"
33 #include "CppUTest/TestMemoryAllocator.h"
34 #include "CppUTest/TestTestingFixture.h"
35 #include "CppUTest/TestHarness_c.h"
36 #include "CppUTest/SimpleMutex.h"
37 #include "DummyMemoryLeakDetector.h"
38
39 TEST_GROUP(MemoryLeakWarningLocalDetectorTest)
40 {
41 DummyMemoryLeakFailure dummy;
42 };
43
44 TEST(MemoryLeakWarningLocalDetectorTest, localDetectorReturnsNewGlobalWhenNoneWasSet)
45 {
46 MemoryLeakWarningPlugin memoryLeakWarningPlugin("TestMemoryLeakWarningPlugin", NULLPTR);
47 CHECK(NULLPTR != memoryLeakWarningPlugin.getMemoryLeakDetector());
48 }
49
50 TEST(MemoryLeakWarningLocalDetectorTest, localDetectorIsTheOneSpecifiedInConstructor)
51 {
52 MemoryLeakDetector localDetector(&dummy);
53 MemoryLeakWarningPlugin memoryLeakWarningPlugin("TestMemoryLeakWarningPlugin", &localDetector);
54 POINTERS_EQUAL(&localDetector, memoryLeakWarningPlugin.getMemoryLeakDetector());
55 }
56
57 TEST(MemoryLeakWarningLocalDetectorTest, localDetectorIsGlobalDetector)
58 {
59 MemoryLeakDetector* globalDetector = MemoryLeakWarningPlugin::getGlobalDetector();
60 MemoryLeakWarningPlugin memoryLeakWarningPlugin("TestMemoryLeakWarningPlugin", NULLPTR);
61 MemoryLeakDetector* localDetector = memoryLeakWarningPlugin.getMemoryLeakDetector();
62 POINTERS_EQUAL(globalDetector, localDetector);
63 }
64
65 static char* leak1;
66 static long* leak2;
67
68 static MemoryLeakDetector* detector;
69 static MemoryLeakWarningPlugin* memPlugin;
70 static TestMemoryAllocator* allocator;
71
72 TEST_GROUP(MemoryLeakWarningTest)
73 {
74 DummyMemoryLeakFailure dummy;
75 TestTestingFixture* fixture;
76
77 void setup()
78 {
79 fixture = new TestTestingFixture();
80 detector = new MemoryLeakDetector(&dummy);
81 allocator = new TestMemoryAllocator;
82 memPlugin = new MemoryLeakWarningPlugin("TestMemoryLeakWarningPlugin", detector);
83 fixture->installPlugin(memPlugin);
84 memPlugin->enable();
85
86 leak1 = NULLPTR;
87 leak2 = NULLPTR;
88 }
89
90 void teardown()
91 {
92 detector->deallocMemory(allocator, leak1);
93 detector->deallocMemory(allocator, leak2);
94
95 delete fixture;
96 delete memPlugin;
97 delete detector;
98 delete allocator;
99 }
100 };
101
102 static void _testTwoLeaks()
103 {
104 leak1 = detector->allocMemory(allocator, 10);
105 leak2 = (long*) (void*) detector->allocMemory(allocator, 4);
106 }
107
108 #if CPPUTEST_USE_MEM_LEAK_DETECTION
109
110 TEST(MemoryLeakWarningTest, TwoLeaks)
111 {
112 fixture->setTestFunction(_testTwoLeaks);
113 fixture->runAllTests();
114
115 LONGS_EQUAL(1, fixture->getFailureCount());
116 }
117
118 #else
119
120 TEST(MemoryLeakWarningTest, TwoLeaks)
121 {
122 fixture->setTestFunction(_testTwoLeaks);
123 fixture->runAllTests();
124
125 LONGS_EQUAL(0, fixture->getFailureCount());
126 }
127
128 #endif
129
130
131 static void _testLeakWarningWithPluginDisabled()
132 {
133 memPlugin->expectLeaksInTest(1);
134 leak1 = (char*) cpputest_malloc_location_with_leak_detection(10, __FILE__, __LINE__);
135 }
136
137 TEST(MemoryLeakWarningTest, LeakWarningWithPluginDisabled)
138 {
139 fixture->setTestFunction(_testLeakWarningWithPluginDisabled);
140
141 MemoryLeakWarningPlugin::saveAndDisableNewDeleteOverloads();
142
143 fixture->runAllTests();
144
145 LONGS_EQUAL(0, fixture->getFailureCount());
146 fixture->assertPrintContains("Warning: Expected 1 leak(s), but leak detection was disabled");
147
148 cpputest_free_location_with_leak_detection(leak1, __FILE__, __LINE__);
149 leak1 = NULLPTR;
150
151 MemoryLeakWarningPlugin::restoreNewDeleteOverloads();
152 }
153
154 static void _testIgnore2()
155 {
156 memPlugin->expectLeaksInTest(2);
157 leak1 = detector->allocMemory(allocator, 10);
158 leak2 = (long*) (void*) detector->allocMemory(allocator, 4);
159 }
160
161 TEST(MemoryLeakWarningTest, Ignore2)
162 {
163 fixture->setTestFunction(_testIgnore2);
164 fixture->runAllTests();
165 LONGS_EQUAL(0, fixture->getFailureCount());
166 }
167
168 static void _failAndLeakMemory()
169 {
170 leak1 = detector->allocMemory(allocator, 10);
171 FAIL("");
172 }
173
174 TEST(MemoryLeakWarningTest, FailingTestDoesNotReportMemoryLeaks)
175 {
176 fixture->setTestFunction(_failAndLeakMemory);
177 fixture->runAllTests();
178 LONGS_EQUAL(1, fixture->getFailureCount());
179 }
180
181 static bool cpputestHasCrashed;
182
183 TEST_GROUP(MemoryLeakWarningGlobalDetectorTest)
184 {
185 MemoryLeakDetector* detector;
186 MemoryLeakFailure* failureReporter;
187
188 DummyMemoryLeakDetector * dummyDetector;
189 MemoryLeakFailure* dummyReporter;
190
191 GlobalMemoryAllocatorStash memoryAllocatorStash;
192
193 static void crashMethod()
194 {
195 cpputestHasCrashed = true;
196 }
197
198 void setup()
199 {
200 memoryAllocatorStash.save();
201 detector = MemoryLeakWarningPlugin::getGlobalDetector();
202 failureReporter = MemoryLeakWarningPlugin::getGlobalFailureReporter();
203
204 MemoryLeakWarningPlugin::saveAndDisableNewDeleteOverloads();
205
206 dummyReporter = new DummyMemoryLeakFailure;
207 dummyDetector = new DummyMemoryLeakDetector(dummyReporter);
208
209 UtestShell::setCrashMethod(crashMethod);
210 cpputestHasCrashed = false;
211 }
212
213 void teardown()
214 {
215 MemoryLeakWarningPlugin::restoreNewDeleteOverloads();
216
217 MemoryLeakWarningPlugin::saveAndDisableNewDeleteOverloads();
218
219 if (!DummyMemoryLeakDetector::wasDeleted()) delete dummyDetector;
220 if (!DummyMemoryLeakFailure::wasDeleted()) delete dummyReporter;
221 MemoryLeakWarningPlugin::setGlobalDetector(detector, failureReporter);
222
223 MemoryLeakWarningPlugin::restoreNewDeleteOverloads();
224
225 UtestShell::resetCrashMethod();
226
227 memoryAllocatorStash.restore();
228 }
229 };
230
231 TEST(MemoryLeakWarningGlobalDetectorTest, turnOffNewOverloadsCausesNoAdditionalLeaks)
232 {
233 size_t storedAmountOfLeaks = detector->totalMemoryLeaks(mem_leak_period_all);
234
235 char* arrayMemory = new char[100];
236 char* nonArrayMemory = new char;
237 char* mallocMemory = (char*) cpputest_malloc_location_with_leak_detection(10, "file", 10);
238 char* reallocMemory = (char*) cpputest_realloc_location_with_leak_detection(NULLPTR, 10, "file", 10);
239
240 LONGS_EQUAL(storedAmountOfLeaks, detector->totalMemoryLeaks(mem_leak_period_all));
241
242 cpputest_free_location_with_leak_detection(mallocMemory, "file", 10);
243 cpputest_free_location_with_leak_detection(reallocMemory, "file", 10);
244 delete [] arrayMemory;
245 delete nonArrayMemory;
246 }
247
248 TEST(MemoryLeakWarningGlobalDetectorTest, destroyGlobalDetector)
249 {
250 MemoryLeakWarningPlugin::setGlobalDetector(dummyDetector, dummyReporter);
251
252 MemoryLeakWarningPlugin::destroyGlobalDetector();
253
254 CHECK(DummyMemoryLeakDetector::wasDeleted());
255 CHECK(DummyMemoryLeakFailure::wasDeleted());
256 }
257
258 TEST(MemoryLeakWarningGlobalDetectorTest, MemoryWarningPluginCanBeSetToDestroyTheGlobalDetector)
259 {
260 MemoryLeakWarningPlugin* plugin = new MemoryLeakWarningPlugin("dummy");
261 plugin->destroyGlobalDetectorAndTurnOffMemoryLeakDetectionInDestructor(true);
262 MemoryLeakWarningPlugin::setGlobalDetector(dummyDetector, dummyReporter);
263
264 delete plugin;
265
266 CHECK(DummyMemoryLeakDetector::wasDeleted());
267 }
268
269 #if CPPUTEST_USE_MEM_LEAK_DETECTION
270
271 TEST(MemoryLeakWarningGlobalDetectorTest, crashOnLeakWithOperatorNew)
272 {
273 MemoryLeakWarningPlugin::setGlobalDetector(dummyDetector, dummyReporter);
274
275 MemoryLeakWarningPlugin::restoreNewDeleteOverloads();
276
277 crash_on_allocation_number(1);
278 char* memory = new char[100];
279 CHECK(cpputestHasCrashed);
280 delete [] memory;
281 MemoryLeakWarningPlugin::saveAndDisableNewDeleteOverloads();
282 }
283
284 TEST(MemoryLeakWarningGlobalDetectorTest, crashOnLeakWithOperatorNewArray)
285 {
286 MemoryLeakWarningPlugin::setGlobalDetector(dummyDetector, dummyReporter);
287
288 MemoryLeakWarningPlugin::restoreNewDeleteOverloads();
289
290 crash_on_allocation_number(1);
291 char* memory = new char;
292 CHECK(cpputestHasCrashed);
293 delete memory;
294
295 MemoryLeakWarningPlugin::saveAndDisableNewDeleteOverloads();
296 }
297
298 TEST(MemoryLeakWarningGlobalDetectorTest, crashOnLeakWithOperatorMalloc)
299 {
300 MemoryLeakWarningPlugin::setGlobalDetector(dummyDetector, dummyReporter);
301
302 MemoryLeakWarningPlugin::restoreNewDeleteOverloads();
303
304 crash_on_allocation_number(1);
305 char* memory = (char*) cpputest_malloc(10);
306 CHECK(cpputestHasCrashed);
307 cpputest_free(memory);
308
309 MemoryLeakWarningPlugin::saveAndDisableNewDeleteOverloads();
310 }
311
312 TEST(MemoryLeakWarningGlobalDetectorTest, gettingTheGlobalDetectorDoesNotRestoreTheMemoryLeakOverloadsWhenTheyWereAlreadyOff)
313 {
314 MemoryLeakWarningPlugin::setGlobalDetector(NULLPTR, NULLPTR);
315 MemoryLeakDetector* temporaryDetector = MemoryLeakWarningPlugin::getGlobalDetector();
316 MemoryLeakFailure* temporaryReporter = MemoryLeakWarningPlugin::getGlobalFailureReporter();
317
318 MemoryLeakWarningPlugin::saveAndDisableNewDeleteOverloads();
319
320 bool areNewDeleteOverloaded = MemoryLeakWarningPlugin::areNewDeleteOverloaded();
321 MemoryLeakWarningPlugin::restoreNewDeleteOverloads();
322
323 CHECK(!areNewDeleteOverloaded);
324
325 delete temporaryReporter;
326 delete temporaryDetector;
327 MemoryLeakWarningPlugin::setGlobalDetector(NULLPTR, NULLPTR);
328 }
329
330 TEST(MemoryLeakWarningGlobalDetectorTest, checkIfTheMemoryLeakOverloadsAreOn)
331 {
332 MemoryLeakWarningPlugin::turnOnDefaultNotThreadSafeNewDeleteOverloads();
333 CHECK(MemoryLeakWarningPlugin::areNewDeleteOverloaded());
334
335 MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
336 }
337
338 TEST(MemoryLeakWarningGlobalDetectorTest, checkIfTheMemoryLeakOverloadsAreOff)
339 {
340 MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
341 bool areNewDeleteOverloaded = MemoryLeakWarningPlugin::areNewDeleteOverloaded();
342
343 MemoryLeakWarningPlugin::turnOnDefaultNotThreadSafeNewDeleteOverloads();
344 CHECK(!areNewDeleteOverloaded);
345 }
346
347 TEST(MemoryLeakWarningGlobalDetectorTest, checkIfTheMemoryLeakOverloadsAreOnWithRestore)
348 {
349 MemoryLeakWarningPlugin::restoreNewDeleteOverloads();
350 CHECK(MemoryLeakWarningPlugin::areNewDeleteOverloaded());
351 MemoryLeakWarningPlugin::saveAndDisableNewDeleteOverloads();
352 }
353
354 TEST(MemoryLeakWarningGlobalDetectorTest, checkIfTheMemoryLeakOverloadsAreOffWithSaveDisable)
355 {
356 MemoryLeakWarningPlugin::saveAndDisableNewDeleteOverloads();
357 CHECK(!MemoryLeakWarningPlugin::areNewDeleteOverloaded());
358 MemoryLeakWarningPlugin::restoreNewDeleteOverloads();
359 }
360
361 TEST(MemoryLeakWarningGlobalDetectorTest, threadSafeMemoryLeakDetectorOverloadsAreAlsoOverloaded)
362 {
363 MemoryLeakWarningPlugin::restoreNewDeleteOverloads();
364 MemoryLeakWarningPlugin::turnOnThreadSafeNewDeleteOverloads();
365 CHECK(MemoryLeakWarningPlugin::areNewDeleteOverloaded());
366 MemoryLeakWarningPlugin::turnOnDefaultNotThreadSafeNewDeleteOverloads();
367 MemoryLeakWarningPlugin::saveAndDisableNewDeleteOverloads();
368 }
369
370 #endif
371
372 #if CPPUTEST_USE_STD_CPP_LIB
373
374 TEST(MemoryLeakWarningGlobalDetectorTest, turnOffNewOverloadsNoThrowCausesNoAdditionalLeaks)
375 {
376 #undef new
377 size_t storedAmountOfLeaks = detector->totalMemoryLeaks(mem_leak_period_all);
378
379 char* nonMemoryNoThrow = new (std::nothrow) char;
380 char* nonArrayMemoryNoThrow = new (std::nothrow) char[10];
381 char* nonArrayMemoryThrow = new char[10];
382
383 LONGS_EQUAL(storedAmountOfLeaks, detector->totalMemoryLeaks(mem_leak_period_all));
384
385 ::operator delete(nonMemoryNoThrow, std::nothrow);
386 ::operator delete[](nonArrayMemoryNoThrow, std::nothrow);
387 ::operator delete[](nonArrayMemoryThrow, std::nothrow);
388 #ifdef CPPUTEST_USE_NEW_MACROS
389 #include "CppUTest/MemoryLeakDetectorNewMacros.h"
390 #endif
391 }
392
393 #if CPPUTEST_USE_MEM_LEAK_DETECTION
394
395 static int mutexLockCount = 0;
396 static int mutexUnlockCount = 0;
397
398 static void StubMutexLock(PlatformSpecificMutex)
399 {
400 mutexLockCount++;
401 }
402
403 static void StubMutexUnlock(PlatformSpecificMutex)
404 {
405 mutexUnlockCount++;
406 }
407
408 TEST_GROUP(MemoryLeakWarningThreadSafe)
409 {
410 void setup()
411 {
412 UT_PTR_SET(PlatformSpecificMutexLock, StubMutexLock);
413 UT_PTR_SET(PlatformSpecificMutexUnlock, StubMutexUnlock);
414
415 mutexLockCount = 0;
416 mutexUnlockCount = 0;
417 }
418
419 void teardown()
420 {
421 }
422 };
423
424 TEST(MemoryLeakWarningThreadSafe, turnOnThreadSafeMallocFreeReallocOverloadsDebug)
425 {
426 size_t storedAmountOfLeaks = MemoryLeakWarningPlugin::getGlobalDetector()->totalMemoryLeaks(mem_leak_period_all);
427
428 MemoryLeakWarningPlugin::turnOnThreadSafeNewDeleteOverloads();
429
430 int *n = (int*) cpputest_malloc(sizeof(int));
431
432 LONGS_EQUAL(storedAmountOfLeaks + 1, MemoryLeakWarningPlugin::getGlobalDetector()->totalMemoryLeaks(mem_leak_period_all));
433 CHECK_EQUAL(1, mutexLockCount);
434 CHECK_EQUAL(1, mutexUnlockCount);
435
436 n = (int*) cpputest_realloc(n, sizeof(int)*3);
437
438 LONGS_EQUAL(storedAmountOfLeaks + 1, MemoryLeakWarningPlugin::getGlobalDetector()->totalMemoryLeaks(mem_leak_period_all));
439 CHECK_EQUAL(2, mutexLockCount);
440 CHECK_EQUAL(2, mutexUnlockCount);
441
442 cpputest_free(n);
443
444 LONGS_EQUAL(storedAmountOfLeaks, MemoryLeakWarningPlugin::getGlobalDetector()->totalMemoryLeaks(mem_leak_period_all));
445 CHECK_EQUAL(3, mutexLockCount);
446 CHECK_EQUAL(3, mutexUnlockCount);
447
448 MemoryLeakWarningPlugin::turnOnDefaultNotThreadSafeNewDeleteOverloads();
449 }
450
451 TEST(MemoryLeakWarningThreadSafe, turnOnThreadSafeNewDeleteOverloadsDebug)
452 {
453 size_t storedAmountOfLeaks = MemoryLeakWarningPlugin::getGlobalDetector()->totalMemoryLeaks(mem_leak_period_all);
454
455 MemoryLeakWarningPlugin::turnOnThreadSafeNewDeleteOverloads();
456
457 int *n = new int;
458 char *str = new char[20];
459
460 LONGS_EQUAL(storedAmountOfLeaks + 2, MemoryLeakWarningPlugin::getGlobalDetector()->totalMemoryLeaks(mem_leak_period_all));
461 CHECK_EQUAL(2, mutexLockCount);
462 CHECK_EQUAL(2, mutexUnlockCount);
463
464 delete [] str;
465 delete n;
466
467 LONGS_EQUAL(storedAmountOfLeaks, MemoryLeakWarningPlugin::getGlobalDetector()->totalMemoryLeaks(mem_leak_period_all));
468 CHECK_EQUAL(4, mutexLockCount);
469 CHECK_EQUAL(4, mutexUnlockCount);
470
471 MemoryLeakWarningPlugin::turnOnDefaultNotThreadSafeNewDeleteOverloads();
472 }
473
474 #ifdef __clang__
475
476 IGNORE_TEST(MemoryLeakWarningThreadSafe, turnOnThreadSafeNewDeleteOverloads)
477 {
478 /* Clang misbehaves with -O2 - it will not overload operator new or
479 * operator new[] no matter what. Therefore, this test is must be ignored.
480 */
481 }
482
483 #else
484
485 TEST(MemoryLeakWarningThreadSafe, turnOnThreadSafeNewDeleteOverloads)
486 {
487 #undef new
488
489 size_t storedAmountOfLeaks = MemoryLeakWarningPlugin::getGlobalDetector()->totalMemoryLeaks(mem_leak_period_all);
490 MemoryLeakWarningPlugin::turnOnThreadSafeNewDeleteOverloads();
491
492 int *n = new int;
493 int *n_nothrow = new (std::nothrow) int;
494 char *str = new char[20];
495 char *str_nothrow = new (std::nothrow) char[20];
496
497 LONGS_EQUAL(storedAmountOfLeaks + 4, MemoryLeakWarningPlugin::getGlobalDetector()->totalMemoryLeaks(mem_leak_period_all));
498 CHECK_EQUAL(4, mutexLockCount);
499 CHECK_EQUAL(4, mutexUnlockCount);
500
501 delete [] str_nothrow;
502 delete [] str;
503 delete n;
504 delete n_nothrow;
505
506 LONGS_EQUAL(storedAmountOfLeaks, MemoryLeakWarningPlugin::getGlobalDetector()->totalMemoryLeaks(mem_leak_period_all));
507 CHECK_EQUAL(8, mutexLockCount);
508 CHECK_EQUAL(8, mutexUnlockCount);
509
510 MemoryLeakWarningPlugin::turnOnDefaultNotThreadSafeNewDeleteOverloads();
511 #ifdef CPPUTEST_USE_NEW_MACROS
512 #include "CppUTest/MemoryLeakDetectorNewMacros.h"
513 #endif
514 }
515
516 #endif
517
518 #endif
519
520 #endif