| 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/TestOutput.h" |
| 30 | #include "CppUTestExt/MemoryReporterPlugin.h" |
| 31 | #include "CppUTestExt/MemoryReportFormatter.h" |
| 32 | #include "CppUTestExt/MockSupport.h" |
| 33 | #include "CppUTestExt/MockNamedValue.h" |
| 34 | |
| 35 | static TestMemoryAllocator* previousNewAllocator; |
| 36 | |
| 37 | class TemporaryDefaultNewAllocator |
| 38 | { |
| 39 | TestMemoryAllocator* newAllocator; |
| 40 | public: |
| 41 | TemporaryDefaultNewAllocator(TestMemoryAllocator* oldAllocator) |
| 42 | { |
| 43 | newAllocator = getCurrentNewAllocator(); |
| 44 | setCurrentNewAllocator(oldAllocator); |
| 45 | } |
| 46 | ~TemporaryDefaultNewAllocator() |
| 47 | { |
| 48 | setCurrentNewAllocator(newAllocator); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | class MockMemoryReportFormatter : public MemoryReportFormatter |
| 53 | { |
| 54 | public: |
| 55 | virtual void report_testgroup_start(TestResult* result, UtestShell& test) |
| 56 | { |
| 57 | TemporaryDefaultNewAllocator tempAlloc(previousNewAllocator); |
| 58 | mock("formatter").actualCall("report_testgroup_start").withParameter("result", result).withParameter("test", &test); |
| 59 | } |
| 60 | |
| 61 | virtual void report_testgroup_end(TestResult* result, UtestShell& test) |
| 62 | { |
| 63 | TemporaryDefaultNewAllocator tempAlloc(previousNewAllocator); |
| 64 | mock("formatter").actualCall("report_testgroup_end").withParameter("result", result).withParameter("test", &test); |
| 65 | } |
| 66 | |
| 67 | virtual void report_test_start(TestResult* result, UtestShell& test) |
| 68 | { |
| 69 | TemporaryDefaultNewAllocator tempAlloc(previousNewAllocator); |
| 70 | mock("formatter").actualCall("report_test_start").withParameter("result", result).withParameter("test", &test); |
| 71 | } |
| 72 | |
| 73 | virtual void report_test_end(TestResult* result, UtestShell& test) |
| 74 | { |
| 75 | TemporaryDefaultNewAllocator tempAlloc(previousNewAllocator); |
| 76 | mock("formatter").actualCall("report_test_end").withParameter("result", result).withParameter("test", &test); |
| 77 | } |
| 78 | |
| 79 | virtual void report_alloc_memory(TestResult* result, TestMemoryAllocator* allocator, size_t, char* , const char* , size_t ) |
| 80 | { |
| 81 | TemporaryDefaultNewAllocator tempAlloc(previousNewAllocator); |
| 82 | mock("formatter").actualCall("report_alloc_memory").withParameter("result", result).withParameterOfType("TestMemoryAllocator", "allocator", allocator); |
| 83 | } |
| 84 | |
| 85 | virtual void report_free_memory(TestResult* result, TestMemoryAllocator* allocator, char* , const char* , size_t ) |
| 86 | { |
| 87 | TemporaryDefaultNewAllocator tempAlloc(previousNewAllocator); |
| 88 | mock("formatter").actualCall("report_free_memory").withParameter("result", result).withParameterOfType("TestMemoryAllocator", "allocator", allocator); |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | static MockMemoryReportFormatter formatterForPluginTest; |
| 93 | |
| 94 | class MemoryReporterPluginUnderTest : public MemoryReporterPlugin |
| 95 | { |
| 96 | public: |
| 97 | MemoryReportFormatter* createMemoryFormatter(const SimpleString& type) |
| 98 | { |
| 99 | mock("reporter").actualCall("createMemoryFormatter").onObject(this).withParameter("type", type.asCharString()); |
| 100 | return new MockMemoryReportFormatter; |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | class TestMemoryAllocatorComparator : public MockNamedValueComparator |
| 105 | { |
| 106 | public: |
| 107 | bool isEqual(const void* object1, const void* object2) |
| 108 | { |
| 109 | return ((const TestMemoryAllocator*)object1)->name() == ((const TestMemoryAllocator*)object2)->name(); |
| 110 | } |
| 111 | SimpleString valueToString(const void* object) |
| 112 | { |
| 113 | return ((const TestMemoryAllocator*)object)->name(); |
| 114 | } |
| 115 | |
| 116 | }; |
| 117 | |
| 118 | TEST_GROUP(MemoryReporterPlugin) |
| 119 | { |
| 120 | MemoryReporterPluginUnderTest* reporter; |
| 121 | StringBufferTestOutput output; |
| 122 | TestMemoryAllocatorComparator memLeakAllocatorComparator; |
| 123 | |
| 124 | TestResult* result; |
| 125 | UtestShell* test; |
| 126 | |
| 127 | void setup() |
| 128 | { |
| 129 | previousNewAllocator = getCurrentNewAllocator(); |
| 130 | result = new TestResult(output); |
| 131 | test = new UtestShell("groupname", "testname", "filename", 1); |
| 132 | reporter = new MemoryReporterPluginUnderTest; |
| 133 | |
| 134 | mock("formatter").installComparator("TestMemoryAllocator", memLeakAllocatorComparator); |
| 135 | |
| 136 | mock("reporter").disable(); |
| 137 | const char *cmd_line[] = {"-pmemoryreport=normal"}; |
| 138 | reporter->parseArguments(1, cmd_line, 0); |
| 139 | mock("reporter").enable(); |
| 140 | } |
| 141 | void teardown() |
| 142 | { |
| 143 | setCurrentNewAllocator(previousNewAllocator); |
| 144 | mock().clear(); |
| 145 | delete reporter; |
| 146 | delete test; |
| 147 | delete result; |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | TEST(MemoryReporterPlugin, offReportsNothing) |
| 152 | { |
| 153 | MemoryReporterPluginUnderTest freshReporter; |
| 154 | freshReporter.preTestAction(*test, *result); |
| 155 | char* memory = new char; |
| 156 | delete memory; |
| 157 | freshReporter.postTestAction(*test, *result); |
| 158 | } |
| 159 | |
| 160 | TEST(MemoryReporterPlugin, meaninglessArgumentsAreIgnored) |
| 161 | { |
| 162 | const char *cmd_line[] = {"-nothing", "-pnotmemoryreport=normal", "alsomeaningless", "-pmemoryreportnonsensebutnotus"}; |
| 163 | CHECK(reporter->parseArguments(3, cmd_line, 1) == false); |
| 164 | } |
| 165 | |
| 166 | TEST(MemoryReporterPlugin, commandLineParameterTurnsOnNormalLogging) |
| 167 | { |
| 168 | mock("reporter").expectOneCall("createMemoryFormatter").onObject(reporter).withParameter("type", "normal"); |
| 169 | |
| 170 | const char *cmd_line[] = {"-nothing", "-pmemoryreport=normal", "alsomeaningless" }; |
| 171 | CHECK(reporter->parseArguments(3, cmd_line, 1)); |
| 172 | } |
| 173 | |
| 174 | TEST(MemoryReporterPlugin, preTestActionReportsTest) |
| 175 | { |
| 176 | mock("formatter").expectOneCall("report_testgroup_start").withParameter("result", result).withParameter("test", test); |
| 177 | mock("formatter").expectOneCall("report_test_start").withParameter("result", result).withParameter("test", test); |
| 178 | reporter->preTestAction(*test, *result); |
| 179 | } |
| 180 | |
| 181 | TEST(MemoryReporterPlugin, postTestActionReportsTest) |
| 182 | { |
| 183 | mock("formatter").expectOneCall("report_test_end").withParameter("result", result).withParameter("test", test); |
| 184 | mock("formatter").expectOneCall("report_testgroup_end").withParameter("result", result).withParameter("test", test); |
| 185 | |
| 186 | reporter->postTestAction(*test, *result); |
| 187 | } |
| 188 | |
| 189 | TEST(MemoryReporterPlugin, newAllocationsAreReportedTest) |
| 190 | { |
| 191 | mock("formatter").expectOneCall("report_alloc_memory").withParameter("result", result).withParameterOfType("TestMemoryAllocator", "allocator", reporter->getNewAllocator()); |
| 192 | mock("formatter").expectOneCall("report_free_memory").withParameter("result", result).withParameterOfType("TestMemoryAllocator", "allocator", reporter->getNewAllocator()); |
| 193 | mock("formatter").ignoreOtherCalls(); |
| 194 | |
| 195 | reporter->preTestAction(*test, *result); |
| 196 | char *memory = getCurrentNewAllocator()->allocMemoryLeakNode(100); |
| 197 | getCurrentNewAllocator()->free_memory(memory, 100, "unknown", 1); |
| 198 | } |
| 199 | |
| 200 | TEST(MemoryReporterPlugin, whenUsingOnlyMallocAllocatorNoOtherOfTheAllocatorsAreUsed) |
| 201 | { |
| 202 | mock("formatter").expectOneCall("report_test_start").withParameter("result", result).withParameter("test", test); |
| 203 | mock("formatter").expectOneCall("report_alloc_memory").withParameter("result", result).withParameterOfType("TestMemoryAllocator", "allocator", reporter->getMallocAllocator()); |
| 204 | mock("formatter").expectOneCall("report_free_memory").withParameter("result", result).withParameterOfType("TestMemoryAllocator", "allocator", reporter->getMallocAllocator()); |
| 205 | mock("formatter").ignoreOtherCalls(); |
| 206 | |
| 207 | reporter->preTestAction(*test, *result); |
| 208 | char *memory = getCurrentMallocAllocator()->allocMemoryLeakNode(100); |
| 209 | getCurrentMallocAllocator()->free_memory(memory, 100, "unknown", 1); |
| 210 | } |
| 211 | |
| 212 | TEST(MemoryReporterPlugin, newArrayAllocationsAreReportedTest) |
| 213 | { |
| 214 | mock("formatter").expectOneCall("report_alloc_memory").withParameter("result", result).withParameterOfType("TestMemoryAllocator", "allocator", reporter->getNewArrayAllocator()); |
| 215 | mock("formatter").expectOneCall("report_free_memory").withParameter("result", result).withParameterOfType("TestMemoryAllocator", "allocator", reporter->getNewArrayAllocator()); |
| 216 | mock("formatter").ignoreOtherCalls(); |
| 217 | |
| 218 | reporter->preTestAction(*test, *result); |
| 219 | char *memory = getCurrentNewArrayAllocator()->allocMemoryLeakNode(100); |
| 220 | getCurrentNewArrayAllocator()->free_memory(memory, 100, "unknown", 1); |
| 221 | } |
| 222 | |
| 223 | TEST(MemoryReporterPlugin, mallocAllocationsAreReportedTest) |
| 224 | { |
| 225 | mock("formatter").expectOneCall("report_alloc_memory").withParameter("result", result).withParameterOfType("TestMemoryAllocator", "allocator", reporter->getMallocAllocator()); |
| 226 | mock("formatter").expectOneCall("report_free_memory").withParameter("result", result).withParameterOfType("TestMemoryAllocator", "allocator", reporter->getMallocAllocator()); |
| 227 | mock("formatter").ignoreOtherCalls(); |
| 228 | |
| 229 | reporter->preTestAction(*test, *result); |
| 230 | char *memory = getCurrentMallocAllocator()->allocMemoryLeakNode(100); |
| 231 | getCurrentMallocAllocator()->free_memory(memory, 100, "unknown", 1); |
| 232 | } |
| 233 | |
| 234 | TEST(MemoryReporterPlugin, startOfANewTestWillReportTheTestGroupStart) |
| 235 | { |
| 236 | mock("formatter").expectOneCall("report_testgroup_start").withParameter("result", result).withParameter("test", test); |
| 237 | mock("formatter").expectOneCall("report_test_start").withParameter("result", result).withParameter("test", test); |
| 238 | mock("formatter").expectOneCall("report_test_end").withParameter("result", result).withParameter("test", test); |
| 239 | mock("formatter").expectOneCall("report_test_start").withParameter("result", result).withParameter("test", test); |
| 240 | mock("formatter").expectOneCall("report_test_end").withParameter("result", result).withParameter("test", test); |
| 241 | mock("formatter").ignoreOtherCalls(); |
| 242 | |
| 243 | reporter->preTestAction(*test, *result); |
| 244 | reporter->postTestAction(*test, *result); |
| 245 | reporter->preTestAction(*test, *result); |
| 246 | reporter->postTestAction(*test, *result); |
| 247 | } |
| 248 | |
| 249 | class UtestForMemoryReportingPlugingTest : public UtestShell |
| 250 | { |
| 251 | public: |
| 252 | UtestForMemoryReportingPlugingTest(const char* groupname, UtestShell* test) : UtestShell(groupname, "testname", "filename", 1, test) |
| 253 | { |
| 254 | |
| 255 | } |
| 256 | }; |
| 257 | |
| 258 | TEST(MemoryReporterPlugin, endOfaTestGroupWillReportSo) |
| 259 | { |
| 260 | UtestForMemoryReportingPlugingTest fourthTest("differentGroupName", NULLPTR); |
| 261 | UtestForMemoryReportingPlugingTest thirdTest("differentGroupName", &fourthTest); |
| 262 | UtestForMemoryReportingPlugingTest secondTest("groupname", &thirdTest); |
| 263 | UtestForMemoryReportingPlugingTest firstTest("groupname", &secondTest); |
| 264 | |
| 265 | mock("formatter").expectOneCall("report_testgroup_end").withParameter("result", result).withParameter("test", &secondTest); |
| 266 | mock("formatter").ignoreOtherCalls(); |
| 267 | |
| 268 | reporter->preTestAction(firstTest, *result); |
| 269 | reporter->postTestAction(firstTest, *result); |
| 270 | reporter->preTestAction(secondTest, *result); |
| 271 | reporter->postTestAction(secondTest, *result); |
| 272 | reporter->preTestAction(thirdTest, *result); |
| 273 | reporter->postTestAction(thirdTest, *result); |
| 274 | } |
| 275 | |
| 276 | TEST(MemoryReporterPlugin, preActionReplacesAllocators) |
| 277 | { |
| 278 | mock("formatter").ignoreOtherCalls(); |
| 279 | |
| 280 | TestMemoryAllocator* allocator = getCurrentMallocAllocator(); |
| 281 | reporter->preTestAction(*test, *result); |
| 282 | CHECK(allocator != getCurrentMallocAllocator()); |
| 283 | } |
| 284 | |
| 285 | TEST(MemoryReporterPlugin, postActionRestoresAllocators) |
| 286 | { |
| 287 | mock("formatter").ignoreOtherCalls(); |
| 288 | |
| 289 | TestMemoryAllocator* allocator = getCurrentMallocAllocator(); |
| 290 | reporter->preTestAction(*test, *result); |
| 291 | reporter->postTestAction(*test, *result); |
| 292 | CHECK(allocator == getCurrentMallocAllocator()); |
| 293 | } |
| 294 | |
| 295 | TEST(MemoryReporterPlugin, shouldCreateNormalMemoryReportFormatterWithoutMock) |
| 296 | { |
| 297 | MemoryReporterPlugin realReporter; |
| 298 | const char *cmd_line[] = {"-pmemoryreport=normal"}; |
| 299 | CHECK(realReporter.parseArguments(1, cmd_line, 0)); |
| 300 | } |
| 301 | |
| 302 | TEST(MemoryReporterPlugin, shouldCreateCodeMemoryReportFormatterWithoutMock) |
| 303 | { |
| 304 | MemoryReporterPlugin realReporter; |
| 305 | const char *cmd_line[] = {"-pmemoryreport=code"}; |
| 306 | CHECK(realReporter.parseArguments(1, cmd_line, 0)); |
| 307 | } |
| 308 | |
| 309 | TEST(MemoryReporterPlugin, shouldntCrashCreateInvalidMemoryReportFormatterWithoutMock) |
| 310 | { |
| 311 | MemoryReporterPlugin realReporter; |
| 312 | const char *cmd_line[] = {"-pmemoryreport=foo"}; |
| 313 | CHECK(realReporter.parseArguments(1, cmd_line, 0)); |
| 314 | realReporter.preTestAction(*test, *result); |
| 315 | realReporter.postTestAction(*test, *result); |
| 316 | } |
| 317 |