| 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/MemoryLeakDetector.h" |
| 30 | #include "CppUTest/TestMemoryAllocator.h" |
| 31 | #include "CppUTest/PlatformSpecificFunctions.h" |
| 32 | |
| 33 | class MemoryLeakFailureForTest: public MemoryLeakFailure |
| 34 | { |
| 35 | public: |
| 36 | virtual ~MemoryLeakFailureForTest() _destructor_override |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | virtual void fail(char* fail_string) _override |
| 41 | { |
| 42 | *message = fail_string; |
| 43 | } |
| 44 | |
| 45 | SimpleString *message; |
| 46 | }; |
| 47 | |
| 48 | class NewAllocatorForMemoryLeakDetectionTest: public TestMemoryAllocator |
| 49 | { |
| 50 | public: |
| 51 | NewAllocatorForMemoryLeakDetectionTest() : |
| 52 | TestMemoryAllocator("Standard New Allocator", "new", "delete"), |
| 53 | alloc_called(0), free_called(0) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | int alloc_called; |
| 58 | int free_called; |
| 59 | char* alloc_memory(size_t size, const char*, size_t) |
| 60 | { |
| 61 | alloc_called++; |
| 62 | return TestMemoryAllocator::alloc_memory(size, "file", 1); |
| 63 | } |
| 64 | void free_memory(char* memory, size_t size, const char* file, size_t line) |
| 65 | { |
| 66 | free_called++; |
| 67 | TestMemoryAllocator::free_memory(memory, size, file, line); |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | class AllocatorForMemoryLeakDetectionTest: public TestMemoryAllocator |
| 72 | { |
| 73 | public: |
| 74 | AllocatorForMemoryLeakDetectionTest() : |
| 75 | alloc_called(0), free_called(0), allocMemoryLeakNodeCalled(0), freeMemoryLeakNodeCalled(0) |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | int alloc_called; |
| 80 | int free_called; |
| 81 | int allocMemoryLeakNodeCalled; |
| 82 | int freeMemoryLeakNodeCalled; |
| 83 | |
| 84 | char* alloc_memory(size_t size, const char* file, size_t line) |
| 85 | { |
| 86 | alloc_called++; |
| 87 | return TestMemoryAllocator::alloc_memory(size, file, line); |
| 88 | } |
| 89 | void free_memory(char* memory, size_t size, const char* file, size_t line) |
| 90 | { |
| 91 | free_called++; |
| 92 | TestMemoryAllocator::free_memory(memory, size, file, line); |
| 93 | } |
| 94 | |
| 95 | char* allocMemoryLeakNode(size_t size) |
| 96 | { |
| 97 | allocMemoryLeakNodeCalled++; |
| 98 | return TestMemoryAllocator::alloc_memory(size, __FILE__, __LINE__); |
| 99 | } |
| 100 | |
| 101 | void freeMemoryLeakNode(char* memory) |
| 102 | { |
| 103 | freeMemoryLeakNodeCalled++; |
| 104 | TestMemoryAllocator::free_memory(memory, 0, __FILE__, __LINE__); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | TEST_GROUP(MemoryLeakDetectorTest) |
| 109 | { |
| 110 | MemoryLeakDetector* detector; |
| 111 | MemoryLeakFailureForTest *reporter; |
| 112 | AllocatorForMemoryLeakDetectionTest* testAllocator; |
| 113 | |
| 114 | void setup() |
| 115 | { |
| 116 | reporter = new MemoryLeakFailureForTest; |
| 117 | detector = new MemoryLeakDetector(reporter); |
| 118 | testAllocator = new AllocatorForMemoryLeakDetectionTest; |
| 119 | detector->enable(); |
| 120 | detector->startChecking(); |
| 121 | reporter->message = new SimpleString(); |
| 122 | } |
| 123 | void teardown() |
| 124 | { |
| 125 | delete reporter->message; |
| 126 | delete detector; |
| 127 | delete reporter; |
| 128 | delete testAllocator; |
| 129 | } |
| 130 | }; |
| 131 | |
| 132 | TEST(MemoryLeakDetectorTest, OneLeak) |
| 133 | { |
| 134 | char* mem = detector->allocMemory(testAllocator, 3); |
| 135 | detector->stopChecking(); |
| 136 | SimpleString output = detector->report(mem_leak_period_checking); |
| 137 | STRCMP_CONTAINS("Memory leak(s) found", output.asCharString()); |
| 138 | STRCMP_CONTAINS("size: 3", output.asCharString()); |
| 139 | STRCMP_CONTAINS("alloc", output.asCharString()); |
| 140 | STRCMP_CONTAINS(StringFromFormat("%p", (void*) mem).asCharString(), output.asCharString()); |
| 141 | STRCMP_CONTAINS("Total number of leaks", output.asCharString()); |
| 142 | PlatformSpecificFree(mem); |
| 143 | LONGS_EQUAL(1, testAllocator->alloc_called); |
| 144 | LONGS_EQUAL(0, testAllocator->free_called); |
| 145 | } |
| 146 | |
| 147 | TEST(MemoryLeakDetectorTest, sequenceNumbersOfMemoryLeaks) |
| 148 | { |
| 149 | char* mem = detector->allocMemory(defaultNewAllocator(), 1); |
| 150 | char* mem2 = detector->allocMemory(defaultNewAllocator(), 2); |
| 151 | char* mem3 = detector->allocMemory(defaultNewAllocator(), 3); |
| 152 | SimpleString output = detector->report(mem_leak_period_checking); |
| 153 | |
| 154 | STRCMP_CONTAINS("Alloc num (1)", output.asCharString()); |
| 155 | STRCMP_CONTAINS("Alloc num (2)", output.asCharString()); |
| 156 | STRCMP_CONTAINS("Alloc num (3)", output.asCharString()); |
| 157 | |
| 158 | PlatformSpecificFree(mem); |
| 159 | PlatformSpecificFree(mem2); |
| 160 | PlatformSpecificFree(mem3); |
| 161 | } |
| 162 | |
| 163 | TEST(MemoryLeakDetectorTest, memoryDumpOutput) |
| 164 | { |
| 165 | char* mem = detector->allocMemory(defaultNewAllocator(), 6); |
| 166 | SimpleString::StrNCpy(mem, "test1", 6); |
| 167 | SimpleString output = detector->report(mem_leak_period_checking); |
| 168 | |
| 169 | STRCMP_CONTAINS("Alloc num (1)", output.asCharString()); |
| 170 | STRCMP_CONTAINS("Leak size: 6 Allocated at", output.asCharString()); |
| 171 | STRCMP_CONTAINS("Content:", output.asCharString()); |
| 172 | STRCMP_CONTAINS("0000: 74 65 73 74 31 00 |test1.|", output.asCharString()); |
| 173 | |
| 174 | PlatformSpecificFree(mem); |
| 175 | } |
| 176 | |
| 177 | TEST(MemoryLeakDetectorTest, OneHundredLeaks) |
| 178 | { |
| 179 | const int amount_alloc = 100; |
| 180 | char *mem[amount_alloc]; |
| 181 | for (int i = 0; i < amount_alloc; i++) |
| 182 | mem[i] = detector->allocMemory(defaultMallocAllocator(), 3); |
| 183 | detector->stopChecking(); |
| 184 | |
| 185 | SimpleString output = detector->report(mem_leak_period_checking); |
| 186 | |
| 187 | STRCMP_CONTAINS("Memory leak(s) found", output.asCharString()); |
| 188 | STRCMP_CONTAINS("Total number of leaks", output.asCharString()); |
| 189 | STRCMP_CONTAINS("Memory leak reports about malloc and free", output.asCharString()); |
| 190 | |
| 191 | //don't reuse i for vc6 compatibility |
| 192 | for (int j = 0; j < amount_alloc; j++) |
| 193 | PlatformSpecificFree(mem[j]); |
| 194 | } |
| 195 | |
| 196 | TEST(MemoryLeakDetectorTest, OneLeakOutsideCheckingPeriod) |
| 197 | { |
| 198 | detector->stopChecking(); |
| 199 | char* mem = detector->allocMemory(defaultNewAllocator(), 4); |
| 200 | SimpleString output = detector->report(mem_leak_period_all); |
| 201 | CHECK(output.contains("Memory leak(s) found")); |
| 202 | CHECK(output.contains("size: 4")); |
| 203 | CHECK(output.contains("new")); |
| 204 | CHECK(output.contains("Total number of leaks")); |
| 205 | PlatformSpecificFree(mem); |
| 206 | } |
| 207 | |
| 208 | TEST(MemoryLeakDetectorTest, NoLeaksWhatsoever) |
| 209 | { |
| 210 | detector->stopChecking(); |
| 211 | STRCMP_CONTAINS("No memory leaks", detector->report(mem_leak_period_checking)); |
| 212 | STRCMP_CONTAINS("No memory leaks", detector->report(mem_leak_period_all)); |
| 213 | } |
| 214 | |
| 215 | TEST(MemoryLeakDetectorTest, TwoLeaksUsingOperatorNew) |
| 216 | { |
| 217 | char* mem = detector->allocMemory(defaultNewAllocator(), 4); |
| 218 | char* mem2 = detector->allocMemory(defaultNewAllocator(), 8); |
| 219 | detector->stopChecking(); |
| 220 | SimpleString output = detector->report(mem_leak_period_checking); |
| 221 | LONGS_EQUAL(2, detector->totalMemoryLeaks(mem_leak_period_checking)); |
| 222 | CHECK(output.contains("size: 8")); |
| 223 | CHECK(output.contains("size: 4")); |
| 224 | PlatformSpecificFree(mem); |
| 225 | PlatformSpecificFree(mem2); |
| 226 | } |
| 227 | |
| 228 | TEST(MemoryLeakDetectorTest, OneAllocButNoLeak) |
| 229 | { |
| 230 | char* mem = detector->allocMemory(testAllocator, 4); |
| 231 | detector->deallocMemory(testAllocator, mem); |
| 232 | detector->stopChecking(); |
| 233 | STRCMP_CONTAINS("No memory leaks", detector->report(mem_leak_period_checking)); |
| 234 | LONGS_EQUAL(1, testAllocator->alloc_called); |
| 235 | LONGS_EQUAL(1, testAllocator->free_called); |
| 236 | } |
| 237 | |
| 238 | TEST(MemoryLeakDetectorTest, TwoAllocOneFreeOneLeak) |
| 239 | { |
| 240 | char* mem = detector->allocMemory(testAllocator, 4); |
| 241 | char* mem2 = detector->allocMemory(testAllocator, 12); |
| 242 | detector->deallocMemory(testAllocator, mem); |
| 243 | detector->stopChecking(); |
| 244 | SimpleString output = detector->report(mem_leak_period_checking); |
| 245 | LONGS_EQUAL(1, detector->totalMemoryLeaks(mem_leak_period_checking)); |
| 246 | CHECK(output.contains("Leak size: 12")); |
| 247 | CHECK(!output.contains("Leak size: 4")); |
| 248 | PlatformSpecificFree(mem2); |
| 249 | LONGS_EQUAL(2, testAllocator->alloc_called); |
| 250 | LONGS_EQUAL(1, testAllocator->free_called); |
| 251 | } |
| 252 | |
| 253 | TEST(MemoryLeakDetectorTest, TwoAllocOneFreeOneLeakReverseOrder) |
| 254 | { |
| 255 | char* mem = detector->allocMemory(defaultNewAllocator(), 4); |
| 256 | char* mem2 = detector->allocMemory(defaultNewAllocator(), 12); |
| 257 | detector->deallocMemory(defaultNewAllocator(), mem2); |
| 258 | detector->stopChecking(); |
| 259 | SimpleString output = detector->report(mem_leak_period_checking); |
| 260 | LONGS_EQUAL(1, detector->totalMemoryLeaks(mem_leak_period_checking)); |
| 261 | CHECK(!output.contains("size: 12")); |
| 262 | CHECK(output.contains("size: 4")); |
| 263 | PlatformSpecificFree(mem); |
| 264 | } |
| 265 | |
| 266 | TEST(MemoryLeakDetectorTest, DeleteNonAlocatedMemory) |
| 267 | { |
| 268 | char a; |
| 269 | char* pa = &a; |
| 270 | detector->deallocMemory(defaultMallocAllocator(), pa, "FREE.c", 100); |
| 271 | detector->stopChecking(); |
| 272 | CHECK(reporter->message->contains("Deallocating non-allocated memory")); |
| 273 | CHECK(reporter->message->contains(" allocated at file: <unknown> line: 0 size: 0 type: unknown")); |
| 274 | CHECK(reporter->message->contains(" deallocated at file: FREE.c line: 100 type: free")); |
| 275 | } |
| 276 | |
| 277 | TEST(MemoryLeakDetectorTest, IgnoreMemoryAllocatedOutsideCheckingPeriod) |
| 278 | { |
| 279 | detector->stopChecking(); |
| 280 | char* mem = detector->allocMemory(defaultNewAllocator(), 4); |
| 281 | LONGS_EQUAL(0, detector->totalMemoryLeaks(mem_leak_period_checking)); |
| 282 | LONGS_EQUAL(1, detector->totalMemoryLeaks(mem_leak_period_all)); |
| 283 | detector->deallocMemory(defaultNewAllocator(), mem); |
| 284 | } |
| 285 | |
| 286 | TEST(MemoryLeakDetectorTest, IgnoreMemoryAllocatedOutsideCheckingPeriodComplicatedCase) |
| 287 | { |
| 288 | char* mem = detector->allocMemory(defaultNewAllocator(), 4); |
| 289 | detector->stopChecking(); |
| 290 | |
| 291 | char* mem2 = detector->allocMemory(defaultNewAllocator(), 8); |
| 292 | LONGS_EQUAL(1, detector->totalMemoryLeaks(mem_leak_period_checking)); |
| 293 | detector->clearAllAccounting(mem_leak_period_checking); |
| 294 | PlatformSpecificFree(mem); |
| 295 | LONGS_EQUAL(0, detector->totalMemoryLeaks(mem_leak_period_checking)); |
| 296 | LONGS_EQUAL(1, detector->totalMemoryLeaks(mem_leak_period_all)); |
| 297 | |
| 298 | detector->startChecking(); |
| 299 | char* mem3 = detector->allocMemory(defaultNewAllocator(), 4); |
| 300 | detector->stopChecking(); |
| 301 | LONGS_EQUAL(1, detector->totalMemoryLeaks(mem_leak_period_checking)); |
| 302 | LONGS_EQUAL(2, detector->totalMemoryLeaks(mem_leak_period_all)); |
| 303 | |
| 304 | detector->clearAllAccounting(mem_leak_period_checking); |
| 305 | LONGS_EQUAL(0, detector->totalMemoryLeaks(mem_leak_period_checking)); |
| 306 | LONGS_EQUAL(1, detector->totalMemoryLeaks(mem_leak_period_all)); |
| 307 | |
| 308 | detector->clearAllAccounting(mem_leak_period_all); |
| 309 | LONGS_EQUAL(0, detector->totalMemoryLeaks(mem_leak_period_all)); |
| 310 | PlatformSpecificFree(mem2); |
| 311 | PlatformSpecificFree(mem3); |
| 312 | } |
| 313 | |
| 314 | TEST(MemoryLeakDetectorTest, OneLeakUsingOperatorNewWithFileLine) |
| 315 | { |
| 316 | char* mem = detector->allocMemory(defaultNewAllocator(), 4, "file.cpp", 1234); |
| 317 | detector->stopChecking(); |
| 318 | SimpleString output = detector->report(mem_leak_period_checking); |
| 319 | CHECK(output.contains("file.cpp")); |
| 320 | CHECK(output.contains("1234")); |
| 321 | PlatformSpecificFree(mem); |
| 322 | } |
| 323 | |
| 324 | TEST(MemoryLeakDetectorTest, OneAllocAndFreeUsingArrayNew) |
| 325 | { |
| 326 | char* mem = detector->allocMemory(defaultNewArrayAllocator(), 10, "file.cpp", 1234); |
| 327 | char* mem2 = detector->allocMemory(defaultNewArrayAllocator(), 12); |
| 328 | LONGS_EQUAL(2, detector->totalMemoryLeaks(mem_leak_period_all)); |
| 329 | SimpleString output = detector->report(mem_leak_period_checking); |
| 330 | CHECK(output.contains("new []")); |
| 331 | detector->deallocMemory(defaultNewArrayAllocator(), mem); |
| 332 | detector->deallocMemory(defaultNewArrayAllocator(), mem2); |
| 333 | LONGS_EQUAL(0, detector->totalMemoryLeaks(mem_leak_period_all)); |
| 334 | detector->stopChecking(); |
| 335 | } |
| 336 | |
| 337 | TEST(MemoryLeakDetectorTest, OneAllocAndFree) |
| 338 | { |
| 339 | char* mem = detector->allocMemory(defaultMallocAllocator(), 10, "file.cpp", 1234); |
| 340 | char* mem2 = detector->allocMemory(defaultMallocAllocator(), 12); |
| 341 | LONGS_EQUAL(2, detector->totalMemoryLeaks(mem_leak_period_checking)); |
| 342 | SimpleString output = detector->report(mem_leak_period_checking); |
| 343 | CHECK(output.contains("malloc")); |
| 344 | detector->deallocMemory(defaultMallocAllocator(), mem); |
| 345 | detector->deallocMemory(defaultMallocAllocator(), mem2, "file.c", 5678); |
| 346 | LONGS_EQUAL(0, detector->totalMemoryLeaks(mem_leak_period_all)); |
| 347 | detector->stopChecking(); |
| 348 | } |
| 349 | |
| 350 | TEST(MemoryLeakDetectorTest, OneRealloc) |
| 351 | { |
| 352 | char* mem1 = detector->allocMemory(testAllocator, 10, "file.cpp", 1234, true); |
| 353 | |
| 354 | char* mem2 = detector->reallocMemory(testAllocator, mem1, 1000, "other.cpp", 5678, true); |
| 355 | |
| 356 | LONGS_EQUAL(1, detector->totalMemoryLeaks(mem_leak_period_checking)); |
| 357 | SimpleString output = detector->report(mem_leak_period_checking); |
| 358 | CHECK(output.contains("other.cpp")); |
| 359 | |
| 360 | detector->deallocMemory(testAllocator, mem2, true); |
| 361 | LONGS_EQUAL(0, detector->totalMemoryLeaks(mem_leak_period_all)); |
| 362 | detector->stopChecking(); |
| 363 | LONGS_EQUAL(1, testAllocator->alloc_called); |
| 364 | LONGS_EQUAL(1, testAllocator->free_called); |
| 365 | LONGS_EQUAL(2, testAllocator->allocMemoryLeakNodeCalled); |
| 366 | LONGS_EQUAL(2, testAllocator->freeMemoryLeakNodeCalled); |
| 367 | } |
| 368 | |
| 369 | TEST(MemoryLeakDetectorTest, ReallocNonAllocatedMemory) |
| 370 | { |
| 371 | char mem1; |
| 372 | char* mem2 = detector->reallocMemory(testAllocator, &mem1, 5, "other.cpp", 13, true); |
| 373 | detector->deallocMemory(testAllocator, mem2, true); |
| 374 | detector->stopChecking(); |
| 375 | CHECK(reporter->message->contains("Deallocating non-allocated memory\n")); |
| 376 | CHECK(reporter->message->contains(" deallocated at file: other.cpp line: 13")); |
| 377 | } |
| 378 | |
| 379 | TEST(MemoryLeakDetectorTest, AllocOneTypeFreeAnotherType) |
| 380 | { |
| 381 | char* mem = detector->allocMemory(defaultNewArrayAllocator(), 100, "ALLOC.c", 10); |
| 382 | detector->deallocMemory(defaultMallocAllocator(), mem, "FREE.c", 100); |
| 383 | detector->stopChecking(); |
| 384 | CHECK(reporter->message->contains("Allocation/deallocation type mismatch")); |
| 385 | CHECK(reporter->message->contains(" allocated at file: ALLOC.c line: 10 size: 100 type: new []")); |
| 386 | CHECK(reporter->message->contains(" deallocated at file: FREE.c line: 100 type: free")); |
| 387 | } |
| 388 | |
| 389 | TEST(MemoryLeakDetectorTest, AllocOneTypeFreeAnotherTypeWithCheckingDisabled) |
| 390 | { |
| 391 | detector->disableAllocationTypeChecking(); |
| 392 | char* mem = detector->allocMemory(defaultNewArrayAllocator(), 100, "ALLOC.c", 10); |
| 393 | detector->deallocMemory(defaultNewAllocator(), mem, "FREE.c", 100); |
| 394 | detector->stopChecking(); |
| 395 | STRCMP_EQUAL("", reporter->message->asCharString()); |
| 396 | detector->enableAllocationTypeChecking(); |
| 397 | } |
| 398 | |
| 399 | TEST(MemoryLeakDetectorTest, mallocLeakGivesAdditionalWarning) |
| 400 | { |
| 401 | char* mem = detector->allocMemory(defaultMallocAllocator(), 100, "ALLOC.c", 10); |
| 402 | detector->stopChecking(); |
| 403 | SimpleString output = detector->report(mem_leak_period_checking); |
| 404 | STRCMP_CONTAINS("Memory leak reports about malloc and free can be caused by allocating using the cpputest version of malloc", output.asCharString()); |
| 405 | PlatformSpecificFree(mem); |
| 406 | } |
| 407 | |
| 408 | TEST(MemoryLeakDetectorTest, newLeakDoesNotGiveAdditionalWarning) |
| 409 | { |
| 410 | char* mem = detector->allocMemory(defaultNewAllocator(), 100, "ALLOC.c", 10); |
| 411 | detector->stopChecking(); |
| 412 | SimpleString output = detector->report(mem_leak_period_checking); |
| 413 | CHECK(! output.contains("Memory leak reports about malloc and free")); |
| 414 | PlatformSpecificFree(mem); |
| 415 | } |
| 416 | |
| 417 | TEST(MemoryLeakDetectorTest, MarkCheckingPeriodLeaksAsNonCheckingPeriod) |
| 418 | { |
| 419 | char* mem = detector->allocMemory(defaultNewArrayAllocator(), 100); |
| 420 | char* mem2 = detector->allocMemory(defaultNewArrayAllocator(), 100); |
| 421 | detector->stopChecking(); |
| 422 | LONGS_EQUAL(2, detector->totalMemoryLeaks(mem_leak_period_checking)); |
| 423 | LONGS_EQUAL(2, detector->totalMemoryLeaks(mem_leak_period_all)); |
| 424 | detector->markCheckingPeriodLeaksAsNonCheckingPeriod(); |
| 425 | LONGS_EQUAL(0, detector->totalMemoryLeaks(mem_leak_period_checking)); |
| 426 | LONGS_EQUAL(2, detector->totalMemoryLeaks(mem_leak_period_all)); |
| 427 | PlatformSpecificFree(mem); |
| 428 | PlatformSpecificFree(mem2); |
| 429 | } |
| 430 | |
| 431 | TEST(MemoryLeakDetectorTest, memoryCorruption) |
| 432 | { |
| 433 | char* mem = detector->allocMemory(defaultMallocAllocator(), 10, "ALLOC.c", 10); |
| 434 | mem[10] = 'O'; |
| 435 | mem[11] = 'H'; |
| 436 | detector->deallocMemory(defaultMallocAllocator(), mem, "FREE.c", 100); |
| 437 | detector->stopChecking(); |
| 438 | CHECK(reporter->message->contains("Memory corruption")); |
| 439 | CHECK(reporter->message->contains(" allocated at file: ALLOC.c line: 10 size: 10 type: malloc")); |
| 440 | CHECK(reporter->message->contains(" deallocated at file: FREE.c line: 100 type: free")); |
| 441 | } |
| 442 | |
| 443 | TEST(MemoryLeakDetectorTest, safelyDeleteNULL) |
| 444 | { |
| 445 | detector->deallocMemory(defaultNewAllocator(), NULLPTR); |
| 446 | STRCMP_EQUAL("", reporter->message->asCharString()); |
| 447 | } |
| 448 | |
| 449 | TEST(MemoryLeakDetectorTest, periodDisabled) |
| 450 | { |
| 451 | detector->disable(); |
| 452 | char* mem = detector->allocMemory(defaultMallocAllocator(), 2); |
| 453 | LONGS_EQUAL(1, detector->totalMemoryLeaks(mem_leak_period_all)); |
| 454 | LONGS_EQUAL(1, detector->totalMemoryLeaks(mem_leak_period_disabled)); |
| 455 | LONGS_EQUAL(0, detector->totalMemoryLeaks(mem_leak_period_enabled)); |
| 456 | LONGS_EQUAL(0, detector->totalMemoryLeaks(mem_leak_period_checking)); |
| 457 | detector->deallocMemory(defaultMallocAllocator(), mem); |
| 458 | } |
| 459 | |
| 460 | TEST(MemoryLeakDetectorTest, periodEnabled) |
| 461 | { |
| 462 | detector->enable(); |
| 463 | char* mem = detector->allocMemory(defaultMallocAllocator(), 2); |
| 464 | LONGS_EQUAL(1, detector->totalMemoryLeaks(mem_leak_period_all)); |
| 465 | LONGS_EQUAL(0, detector->totalMemoryLeaks(mem_leak_period_disabled)); |
| 466 | LONGS_EQUAL(1, detector->totalMemoryLeaks(mem_leak_period_enabled)); |
| 467 | LONGS_EQUAL(0, detector->totalMemoryLeaks(mem_leak_period_checking)); |
| 468 | detector->deallocMemory(defaultMallocAllocator(), mem); |
| 469 | } |
| 470 | |
| 471 | TEST(MemoryLeakDetectorTest, periodChecking) |
| 472 | { |
| 473 | char* mem = detector->allocMemory(defaultMallocAllocator(), 2); |
| 474 | LONGS_EQUAL(1, detector->totalMemoryLeaks(mem_leak_period_all)); |
| 475 | LONGS_EQUAL(0, detector->totalMemoryLeaks(mem_leak_period_disabled)); |
| 476 | LONGS_EQUAL(1, detector->totalMemoryLeaks(mem_leak_period_enabled)); |
| 477 | LONGS_EQUAL(1, detector->totalMemoryLeaks(mem_leak_period_checking)); |
| 478 | detector->deallocMemory(defaultMallocAllocator(), mem); |
| 479 | } |
| 480 | |
| 481 | TEST(MemoryLeakDetectorTest, defaultAllocationStageIsZero) |
| 482 | { |
| 483 | LONGS_EQUAL(0, detector->getCurrentAllocationStage()); |
| 484 | } |
| 485 | |
| 486 | TEST(MemoryLeakDetectorTest, canFreeNoAllocations) |
| 487 | { |
| 488 | detector->deallocAllMemoryInCurrentAllocationStage(); |
| 489 | LONGS_EQUAL(0, detector->getCurrentAllocationStage()); |
| 490 | } |
| 491 | |
| 492 | TEST(MemoryLeakDetectorTest, increaseAllocationStage) |
| 493 | { |
| 494 | detector->increaseAllocationStage(); |
| 495 | LONGS_EQUAL(1, detector->getCurrentAllocationStage()); |
| 496 | } |
| 497 | |
| 498 | TEST(MemoryLeakDetectorTest, decreaseAllocationStage) |
| 499 | { |
| 500 | detector->increaseAllocationStage(); |
| 501 | detector->decreaseAllocationStage(); |
| 502 | LONGS_EQUAL(0, detector->getCurrentAllocationStage()); |
| 503 | } |
| 504 | |
| 505 | TEST(MemoryLeakDetectorTest, freeAllMemoryInCurrentAllocationStage) |
| 506 | { |
| 507 | detector->increaseAllocationStage(); |
| 508 | detector->allocMemory(defaultMallocAllocator(), 2); |
| 509 | detector->allocMemory(defaultMallocAllocator(), 2); |
| 510 | detector->deallocAllMemoryInCurrentAllocationStage(); |
| 511 | LONGS_EQUAL(0, detector->totalMemoryLeaks(mem_leak_period_all)); |
| 512 | } |
| 513 | |
| 514 | TEST(MemoryLeakDetectorTest, freeOnlyTheMemoryInTheAllocationStage) |
| 515 | { |
| 516 | char* mem = detector->allocMemory(defaultMallocAllocator(), 2); |
| 517 | detector->increaseAllocationStage(); |
| 518 | detector->allocMemory(defaultMallocAllocator(), 2); |
| 519 | detector->deallocAllMemoryInCurrentAllocationStage(); |
| 520 | LONGS_EQUAL(1, detector->totalMemoryLeaks(mem_leak_period_all)); |
| 521 | detector->deallocMemory(defaultMallocAllocator(), mem); |
| 522 | } |
| 523 | |
| 524 | TEST(MemoryLeakDetectorTest, allocateWithANullAllocatorCausesNoProblems) |
| 525 | { |
| 526 | char* mem = detector->allocMemory(NullUnknownAllocator::defaultAllocator(), 2); |
| 527 | detector->deallocMemory(NullUnknownAllocator::defaultAllocator(), mem); |
| 528 | } |
| 529 | |
| 530 | TEST(MemoryLeakDetectorTest, invalidateMemory) |
| 531 | { |
| 532 | unsigned char* mem = (unsigned char*)detector->allocMemory(defaultMallocAllocator(), 2); |
| 533 | |
| 534 | detector->invalidateMemory((char*)mem); |
| 535 | CHECK(mem[0] == 0xCD); |
| 536 | CHECK(mem[1] == 0xCD); |
| 537 | detector->deallocMemory(defaultMallocAllocator(), mem); |
| 538 | } |
| 539 | |
| 540 | TEST(MemoryLeakDetectorTest, invalidateMemoryNULLShouldWork) |
| 541 | { |
| 542 | detector->invalidateMemory(NULLPTR); |
| 543 | } |
| 544 | |
| 545 | TEST_GROUP(MemoryLeakDetectorListTest) |
| 546 | { |
| 547 | }; |
| 548 | |
| 549 | TEST(MemoryLeakDetectorListTest, clearAllAccountingIsWorkingProperly) |
| 550 | { |
| 551 | MemoryLeakDetectorList listForTesting; |
| 552 | MemoryLeakDetectorNode node1, node2, node3; |
| 553 | node3.period_ = mem_leak_period_disabled; |
| 554 | listForTesting.addNewNode(&node1); |
| 555 | listForTesting.addNewNode(&node2); |
| 556 | listForTesting.addNewNode(&node3); |
| 557 | |
| 558 | listForTesting.clearAllAccounting(mem_leak_period_enabled); |
| 559 | |
| 560 | POINTERS_EQUAL(NULLPTR, listForTesting.getFirstLeak(mem_leak_period_enabled)); |
| 561 | CHECK(&node3 == listForTesting.getFirstLeak(mem_leak_period_disabled)); |
| 562 | } |
| 563 | |
| 564 | TEST_GROUP(SimpleStringBuffer) |
| 565 | { |
| 566 | }; |
| 567 | |
| 568 | TEST(SimpleStringBuffer, initialStringIsEmpty) |
| 569 | { |
| 570 | SimpleStringBuffer buffer; |
| 571 | STRCMP_EQUAL("", buffer.toString()); |
| 572 | } |
| 573 | |
| 574 | TEST(SimpleStringBuffer, simpleTest) |
| 575 | { |
| 576 | SimpleStringBuffer buffer; |
| 577 | buffer.add("Hello"); |
| 578 | buffer.add(" World"); |
| 579 | STRCMP_EQUAL("Hello World", buffer.toString()); |
| 580 | } |
| 581 | |
| 582 | TEST(SimpleStringBuffer, writePastLimit) |
| 583 | { |
| 584 | SimpleStringBuffer buffer; |
| 585 | for (int i = 0; i < SimpleStringBuffer::SIMPLE_STRING_BUFFER_LEN * 2; i++) |
| 586 | buffer.add("h"); |
| 587 | SimpleString str("h", SimpleStringBuffer::SIMPLE_STRING_BUFFER_LEN-1); |
| 588 | STRCMP_EQUAL(str.asCharString(), buffer.toString()); |
| 589 | } |
| 590 | |
| 591 | TEST(SimpleStringBuffer, setWriteLimit) |
| 592 | { |
| 593 | SimpleStringBuffer buffer; |
| 594 | buffer.setWriteLimit(10); |
| 595 | for (int i = 0; i < SimpleStringBuffer::SIMPLE_STRING_BUFFER_LEN ; i++) |
| 596 | buffer.add("h"); |
| 597 | SimpleString str("h", 10); |
| 598 | STRCMP_EQUAL(str.asCharString(), buffer.toString()); |
| 599 | } |
| 600 | |
| 601 | TEST(SimpleStringBuffer, setWriteLimitTooHighIsIgnored) |
| 602 | { |
| 603 | SimpleStringBuffer buffer; |
| 604 | buffer.setWriteLimit(SimpleStringBuffer::SIMPLE_STRING_BUFFER_LEN+10); |
| 605 | for (int i = 0; i < SimpleStringBuffer::SIMPLE_STRING_BUFFER_LEN+10; i++) |
| 606 | buffer.add("h"); |
| 607 | SimpleString str("h", SimpleStringBuffer::SIMPLE_STRING_BUFFER_LEN-1); |
| 608 | STRCMP_EQUAL(str.asCharString(), buffer.toString()); |
| 609 | } |
| 610 | |
| 611 | TEST(SimpleStringBuffer, resetWriteLimit) |
| 612 | { |
| 613 | SimpleStringBuffer buffer; |
| 614 | buffer.setWriteLimit(10); |
| 615 | for (int i = 0; i < SimpleStringBuffer::SIMPLE_STRING_BUFFER_LEN ; i++) |
| 616 | buffer.add("h"); |
| 617 | buffer.resetWriteLimit(); |
| 618 | buffer.add("%s", SimpleString("h", 10).asCharString()); |
| 619 | |
| 620 | SimpleString str("h", 20); |
| 621 | STRCMP_EQUAL(str.asCharString(), buffer.toString()); |
| 622 | } |
| 623 | |
| 624 | TEST(SimpleStringBuffer, addMemoryDumpOneLinePlusOnePartial) |
| 625 | { |
| 626 | SimpleStringBuffer buffer; |
| 627 | buffer.addMemoryDump("deadbeefdeadbeefhopsxx", 22); |
| 628 | STRCMP_EQUAL(" 0000: 64 65 61 64 62 65 65 66 64 65 61 64 62 65 65 66 |deadbeefdeadbeef|\n" |
| 629 | " 0010: 68 6f 70 73 78 78 |hopsxx|\n", |
| 630 | buffer.toString()); |
| 631 | } |
| 632 | |
| 633 | TEST(SimpleStringBuffer, addMemoryDumpNonPrintable) |
| 634 | { |
| 635 | SimpleStringBuffer buffer; |
| 636 | // Ensure we test edge cases - NUL, 0x1F, 0x7F, 0xFF |
| 637 | buffer.addMemoryDump("\x15\x7f\xff\x00\x1ftdd", 8); |
| 638 | STRCMP_EQUAL(" 0000: 15 7f ff 00 1f 74 64 64 |.....tdd|\n", |
| 639 | buffer.toString()); |
| 640 | } |
| 641 | |
| 642 | TEST(SimpleStringBuffer, addMemoryDumpOneLine) |
| 643 | { |
| 644 | SimpleStringBuffer buffer; |
| 645 | buffer.addMemoryDump("deadbeefdeadbeef", 16); |
| 646 | STRCMP_EQUAL(" 0000: 64 65 61 64 62 65 65 66 64 65 61 64 62 65 65 66 |deadbeefdeadbeef|\n", |
| 647 | buffer.toString()); |
| 648 | } |
| 649 | |
| 650 | TEST(SimpleStringBuffer, addMemoryDumpOneHalfLine) |
| 651 | { |
| 652 | SimpleStringBuffer buffer; |
| 653 | buffer.addMemoryDump("deadbeef", 8); |
| 654 | STRCMP_EQUAL(" 0000: 64 65 61 64 62 65 65 66 |deadbeef|\n", |
| 655 | buffer.toString()); |
| 656 | } |
| 657 | |
| 658 | TEST(SimpleStringBuffer, addMemoryDumpOneByte) |
| 659 | { |
| 660 | SimpleStringBuffer buffer; |
| 661 | buffer.addMemoryDump("Z", 1); |
| 662 | STRCMP_EQUAL(" 0000: 5a |Z|\n", |
| 663 | buffer.toString()); |
| 664 | } |
| 665 | |
| 666 | TEST(SimpleStringBuffer, addMemoryDumpZeroBytes) |
| 667 | { |
| 668 | SimpleStringBuffer buffer; |
| 669 | buffer.addMemoryDump("", 0); |
| 670 | STRCMP_EQUAL("", buffer.toString()); |
| 671 | } |
| 672 | |
| 673 | TEST_GROUP(ReallocBugReported) |
| 674 | { |
| 675 | MemoryLeakFailureForTest reporter; |
| 676 | }; |
| 677 | |
| 678 | TEST(ReallocBugReported, CanSafelyDoAReallocWithANewAllocator) |
| 679 | { |
| 680 | MemoryLeakDetector detector(&reporter); |
| 681 | char* mem = detector.allocMemory(defaultNewAllocator(), 5, "file", 1); |
| 682 | mem = detector.reallocMemory(defaultNewAllocator(), mem, 19, "file", 1); |
| 683 | detector.deallocMemory(defaultNewAllocator(), mem); |
| 684 | } |
| 685 | |
| 686 | TEST(ReallocBugReported, CanSafelyDoAReallocWithAMallocAllocator) |
| 687 | { |
| 688 | MemoryLeakDetector detector(&reporter); |
| 689 | char* mem = detector.allocMemory(defaultMallocAllocator(), 5, "file", 1, true); |
| 690 | mem = detector.reallocMemory(defaultMallocAllocator(), mem, 19, "file", 1, true); |
| 691 | detector.deallocMemory(defaultMallocAllocator(), mem, true); |
| 692 | } |