unity_fixture_Test.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /* Copyright (c) 2010 James Grenning and Contributed to Unity Project
  2. * ==========================================
  3. * Unity Project - A Test Framework for C
  4. * Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
  5. * [Released under MIT License. Please refer to license.txt for details]
  6. * ========================================== */
  7. #include "unity_fixture.h"
  8. #include "unity_output_Spy.h"
  9. #include <stdlib.h>
  10. #include <string.h>
  11. TEST_GROUP(UnityFixture);
  12. TEST_SETUP(UnityFixture)
  13. {
  14. }
  15. TEST_TEAR_DOWN(UnityFixture)
  16. {
  17. }
  18. static int* pointer1 = 0;
  19. static int* pointer2 = (int*)2;
  20. static int* pointer3 = (int*)3;
  21. static int int1;
  22. static int int2;
  23. static int int3;
  24. static int int4;
  25. TEST(UnityFixture, PointerSetting)
  26. {
  27. TEST_ASSERT_POINTERS_EQUAL(pointer1, 0);
  28. UT_PTR_SET(pointer1, &int1);
  29. UT_PTR_SET(pointer2, &int2);
  30. UT_PTR_SET(pointer3, &int3);
  31. TEST_ASSERT_POINTERS_EQUAL(pointer1, &int1);
  32. TEST_ASSERT_POINTERS_EQUAL(pointer2, &int2);
  33. TEST_ASSERT_POINTERS_EQUAL(pointer3, &int3);
  34. UT_PTR_SET(pointer1, &int4);
  35. UnityPointer_UndoAllSets();
  36. TEST_ASSERT_POINTERS_EQUAL(pointer1, 0);
  37. TEST_ASSERT_POINTERS_EQUAL(pointer2, (int*)2);
  38. TEST_ASSERT_POINTERS_EQUAL(pointer3, (int*)3);
  39. }
  40. TEST(UnityFixture, ForceMallocFail)
  41. {
  42. void* m;
  43. void* mfails;
  44. UnityMalloc_MakeMallocFailAfterCount(1);
  45. m = malloc(10);
  46. CHECK(m);
  47. mfails = malloc(10);
  48. TEST_ASSERT_POINTERS_EQUAL(0, mfails);
  49. free(m);
  50. }
  51. TEST(UnityFixture, ReallocSmallerIsUnchanged)
  52. {
  53. void* m1 = malloc(10);
  54. void* m2 = realloc(m1, 5);
  55. TEST_ASSERT_POINTERS_EQUAL(m1, m2);
  56. free(m2);
  57. }
  58. TEST(UnityFixture, ReallocSameIsUnchanged)
  59. {
  60. void* m1 = malloc(10);
  61. void* m2 = realloc(m1, 10);
  62. TEST_ASSERT_POINTERS_EQUAL(m1, m2);
  63. free(m2);
  64. }
  65. TEST(UnityFixture, ReallocLargerNeeded)
  66. {
  67. void* m1 = malloc(10);
  68. void* m2;
  69. CHECK(m1);
  70. strcpy((char*)m1, "123456789");
  71. m2 = realloc(m1, 15);
  72. /* CHECK(m1 != m2); //Depends on implementation */
  73. STRCMP_EQUAL("123456789", m2);
  74. free(m2);
  75. }
  76. TEST(UnityFixture, ReallocNullPointerIsLikeMalloc)
  77. {
  78. void* m = realloc(0, 15);
  79. CHECK(m != 0);
  80. free(m);
  81. }
  82. TEST(UnityFixture, ReallocSizeZeroFreesMemAndReturnsNullPointer)
  83. {
  84. void* m1 = malloc(10);
  85. void* m2 = realloc(m1, 0);
  86. TEST_ASSERT_POINTERS_EQUAL(0, m2);
  87. }
  88. TEST(UnityFixture, CallocFillsWithZero)
  89. {
  90. void* m = calloc(3, sizeof(char));
  91. char* s = (char*)m;
  92. CHECK(m);
  93. TEST_ASSERT_BYTES_EQUAL(0, s[0]);
  94. TEST_ASSERT_BYTES_EQUAL(0, s[1]);
  95. TEST_ASSERT_BYTES_EQUAL(0, s[2]);
  96. free(m);
  97. }
  98. static char *p1;
  99. static char *p2;
  100. TEST(UnityFixture, PointerSet)
  101. {
  102. char c1;
  103. char c2;
  104. char newC1;
  105. char newC2;
  106. p1 = &c1;
  107. p2 = &c2;
  108. UnityPointer_Init();
  109. UT_PTR_SET(p1, &newC1);
  110. UT_PTR_SET(p2, &newC2);
  111. TEST_ASSERT_POINTERS_EQUAL(&newC1, p1);
  112. TEST_ASSERT_POINTERS_EQUAL(&newC2, p2);
  113. UnityPointer_UndoAllSets();
  114. TEST_ASSERT_POINTERS_EQUAL(&c1, p1);
  115. TEST_ASSERT_POINTERS_EQUAL(&c2, p2);
  116. }
  117. TEST(UnityFixture, FreeNULLSafety)
  118. {
  119. free(NULL);
  120. }
  121. TEST(UnityFixture, ConcludeTestIncrementsFailCount)
  122. {
  123. UNITY_UINT savedFails = Unity.TestFailures;
  124. UNITY_UINT savedIgnores = Unity.TestIgnores;
  125. UnityOutputCharSpy_Enable(1);
  126. Unity.CurrentTestFailed = 1;
  127. UnityConcludeFixtureTest(); /* Resets TestFailed for this test to pass */
  128. Unity.CurrentTestIgnored = 1;
  129. UnityConcludeFixtureTest(); /* Resets TestIgnored */
  130. UnityOutputCharSpy_Enable(0);
  131. TEST_ASSERT_EQUAL(savedFails + 1, Unity.TestFailures);
  132. TEST_ASSERT_EQUAL(savedIgnores + 1, Unity.TestIgnores);
  133. Unity.TestFailures = savedFails;
  134. Unity.TestIgnores = savedIgnores;
  135. }
  136. /*------------------------------------------------------------ */
  137. TEST_GROUP(UnityCommandOptions);
  138. static int savedVerbose;
  139. static unsigned int savedRepeat;
  140. static const char* savedName;
  141. static const char* savedGroup;
  142. TEST_SETUP(UnityCommandOptions)
  143. {
  144. savedVerbose = UnityFixture.Verbose;
  145. savedRepeat = UnityFixture.RepeatCount;
  146. savedName = UnityFixture.NameFilter;
  147. savedGroup = UnityFixture.GroupFilter;
  148. }
  149. TEST_TEAR_DOWN(UnityCommandOptions)
  150. {
  151. UnityFixture.Verbose = savedVerbose;
  152. UnityFixture.RepeatCount= savedRepeat;
  153. UnityFixture.NameFilter = savedName;
  154. UnityFixture.GroupFilter = savedGroup;
  155. }
  156. static const char* noOptions[] = {
  157. "testrunner.exe"
  158. };
  159. TEST(UnityCommandOptions, DefaultOptions)
  160. {
  161. UnityGetCommandLineOptions(1, noOptions);
  162. TEST_ASSERT_EQUAL(0, UnityFixture.Verbose);
  163. TEST_ASSERT_POINTERS_EQUAL(0, UnityFixture.GroupFilter);
  164. TEST_ASSERT_POINTERS_EQUAL(0, UnityFixture.NameFilter);
  165. TEST_ASSERT_EQUAL(1, UnityFixture.RepeatCount);
  166. }
  167. static const char* verbose[] = {
  168. "testrunner.exe",
  169. "-v"
  170. };
  171. TEST(UnityCommandOptions, OptionVerbose)
  172. {
  173. TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(2, verbose));
  174. TEST_ASSERT_EQUAL(1, UnityFixture.Verbose);
  175. }
  176. static const char* group[] = {
  177. "testrunner.exe",
  178. "-g", "groupname"
  179. };
  180. TEST(UnityCommandOptions, OptionSelectTestByGroup)
  181. {
  182. TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, group));
  183. STRCMP_EQUAL("groupname", UnityFixture.GroupFilter);
  184. }
  185. static const char* name[] = {
  186. "testrunner.exe",
  187. "-n", "testname"
  188. };
  189. TEST(UnityCommandOptions, OptionSelectTestByName)
  190. {
  191. TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, name));
  192. STRCMP_EQUAL("testname", UnityFixture.NameFilter);
  193. }
  194. static const char* repeat[] = {
  195. "testrunner.exe",
  196. "-r", "99"
  197. };
  198. TEST(UnityCommandOptions, OptionSelectRepeatTestsDefaultCount)
  199. {
  200. TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(2, repeat));
  201. TEST_ASSERT_EQUAL(2, UnityFixture.RepeatCount);
  202. }
  203. TEST(UnityCommandOptions, OptionSelectRepeatTestsSpecificCount)
  204. {
  205. TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(3, repeat));
  206. TEST_ASSERT_EQUAL(99, UnityFixture.RepeatCount);
  207. }
  208. static const char* multiple[] = {
  209. "testrunner.exe",
  210. "-v",
  211. "-g", "groupname",
  212. "-n", "testname",
  213. "-r", "98"
  214. };
  215. TEST(UnityCommandOptions, MultipleOptions)
  216. {
  217. TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(8, multiple));
  218. TEST_ASSERT_EQUAL(1, UnityFixture.Verbose);
  219. STRCMP_EQUAL("groupname", UnityFixture.GroupFilter);
  220. STRCMP_EQUAL("testname", UnityFixture.NameFilter);
  221. TEST_ASSERT_EQUAL(98, UnityFixture.RepeatCount);
  222. }
  223. static const char* dashRNotLast[] = {
  224. "testrunner.exe",
  225. "-v",
  226. "-g", "gggg",
  227. "-r",
  228. "-n", "tttt",
  229. };
  230. TEST(UnityCommandOptions, MultipleOptionsDashRNotLastAndNoValueSpecified)
  231. {
  232. TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(7, dashRNotLast));
  233. TEST_ASSERT_EQUAL(1, UnityFixture.Verbose);
  234. STRCMP_EQUAL("gggg", UnityFixture.GroupFilter);
  235. STRCMP_EQUAL("tttt", UnityFixture.NameFilter);
  236. TEST_ASSERT_EQUAL(2, UnityFixture.RepeatCount);
  237. }
  238. static const char* unknownCommand[] = {
  239. "testrunner.exe",
  240. "-v",
  241. "-g", "groupname",
  242. "-n", "testname",
  243. "-r", "98",
  244. "-z"
  245. };
  246. TEST(UnityCommandOptions, UnknownCommandIsIgnored)
  247. {
  248. TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(9, unknownCommand));
  249. TEST_ASSERT_EQUAL(1, UnityFixture.Verbose);
  250. STRCMP_EQUAL("groupname", UnityFixture.GroupFilter);
  251. STRCMP_EQUAL("testname", UnityFixture.NameFilter);
  252. TEST_ASSERT_EQUAL(98, UnityFixture.RepeatCount);
  253. }
  254. TEST(UnityCommandOptions, GroupOrNameFilterWithoutStringFails)
  255. {
  256. TEST_ASSERT_EQUAL(1, UnityGetCommandLineOptions(3, unknownCommand));
  257. TEST_ASSERT_EQUAL(1, UnityGetCommandLineOptions(5, unknownCommand));
  258. TEST_ASSERT_EQUAL(1, UnityMain(3, unknownCommand, NULL));
  259. }
  260. TEST(UnityCommandOptions, GroupFilterReallyFilters)
  261. {
  262. UNITY_UINT saved = Unity.NumberOfTests;
  263. TEST_ASSERT_EQUAL(0, UnityGetCommandLineOptions(4, unknownCommand));
  264. UnityIgnoreTest(NULL, "non-matching", NULL);
  265. TEST_ASSERT_EQUAL(saved, Unity.NumberOfTests);
  266. }
  267. IGNORE_TEST(UnityCommandOptions, TestShouldBeIgnored)
  268. {
  269. TEST_FAIL_MESSAGE("This test should not run!");
  270. }
  271. /*------------------------------------------------------------ */
  272. TEST_GROUP(LeakDetection);
  273. TEST_SETUP(LeakDetection)
  274. {
  275. #ifdef UNITY_EXCLUDE_STDLIB_MALLOC
  276. UnityOutputCharSpy_Create(200);
  277. #else
  278. UnityOutputCharSpy_Create(1000);
  279. #endif
  280. }
  281. TEST_TEAR_DOWN(LeakDetection)
  282. {
  283. UnityOutputCharSpy_Destroy();
  284. }
  285. #define EXPECT_ABORT_BEGIN \
  286. { \
  287. jmp_buf TestAbortFrame; \
  288. memcpy(TestAbortFrame, Unity.AbortFrame, sizeof(jmp_buf)); \
  289. if (TEST_PROTECT()) \
  290. {
  291. #define EXPECT_ABORT_END \
  292. } \
  293. memcpy(Unity.AbortFrame, TestAbortFrame, sizeof(jmp_buf)); \
  294. }
  295. /* This tricky set of defines lets us see if we are using the Spy, returns 1 if true */
  296. #ifdef __STDC_VERSION__
  297. #if __STDC_VERSION__ >= 199901L
  298. #define USING_SPY_AS(a) EXPAND_AND_USE_2ND(ASSIGN_VALUE(a), 0)
  299. #define ASSIGN_VALUE(a) VAL_##a
  300. #define VAL_UnityOutputCharSpy_OutputChar 0, 1
  301. #define EXPAND_AND_USE_2ND(a, b) SECOND_PARAM(a, b, throwaway)
  302. #define SECOND_PARAM(a, b, ...) b
  303. #if USING_SPY_AS(UNITY_OUTPUT_CHAR)
  304. #define USING_OUTPUT_SPY /* UNITY_OUTPUT_CHAR = UnityOutputCharSpy_OutputChar */
  305. #endif
  306. #endif /* >= 199901 */
  307. #else /* __STDC_VERSION__ else */
  308. #define UnityOutputCharSpy_OutputChar 42
  309. #if UNITY_OUTPUT_CHAR == UnityOutputCharSpy_OutputChar /* Works if no -Wundef -Werror */
  310. #define USING_OUTPUT_SPY
  311. #endif
  312. #undef UnityOutputCharSpy_OutputChar
  313. #endif /* __STDC_VERSION__ */
  314. TEST(LeakDetection, DetectsLeak)
  315. {
  316. #ifndef USING_OUTPUT_SPY
  317. TEST_IGNORE_MESSAGE("Build with '-D UNITY_OUTPUT_CHAR=UnityOutputCharSpy_OutputChar' to enable tests");
  318. #else
  319. void* m = malloc(10);
  320. TEST_ASSERT_NOT_NULL(m);
  321. UnityOutputCharSpy_Enable(1);
  322. EXPECT_ABORT_BEGIN
  323. UnityMalloc_EndTest();
  324. EXPECT_ABORT_END
  325. UnityOutputCharSpy_Enable(0);
  326. Unity.CurrentTestFailed = 0;
  327. CHECK(strstr(UnityOutputCharSpy_Get(), "This test leaks!"));
  328. free(m);
  329. #endif
  330. }
  331. TEST(LeakDetection, BufferOverrunFoundDuringFree)
  332. {
  333. #ifndef USING_OUTPUT_SPY
  334. TEST_IGNORE();
  335. #else
  336. void* m = malloc(10);
  337. char* s = (char*)m;
  338. TEST_ASSERT_NOT_NULL(m);
  339. s[10] = (char)0xFF;
  340. UnityOutputCharSpy_Enable(1);
  341. EXPECT_ABORT_BEGIN
  342. free(m);
  343. EXPECT_ABORT_END
  344. UnityOutputCharSpy_Enable(0);
  345. Unity.CurrentTestFailed = 0;
  346. CHECK(strstr(UnityOutputCharSpy_Get(), "Buffer overrun detected during free()"));
  347. #endif
  348. }
  349. TEST(LeakDetection, BufferOverrunFoundDuringRealloc)
  350. {
  351. #ifndef USING_OUTPUT_SPY
  352. TEST_IGNORE();
  353. #else
  354. void* m = malloc(10);
  355. char* s = (char*)m;
  356. TEST_ASSERT_NOT_NULL(m);
  357. s[10] = (char)0xFF;
  358. UnityOutputCharSpy_Enable(1);
  359. EXPECT_ABORT_BEGIN
  360. m = realloc(m, 100);
  361. EXPECT_ABORT_END
  362. UnityOutputCharSpy_Enable(0);
  363. Unity.CurrentTestFailed = 0;
  364. CHECK(strstr(UnityOutputCharSpy_Get(), "Buffer overrun detected during realloc()"));
  365. #endif
  366. }
  367. TEST(LeakDetection, BufferGuardWriteFoundDuringFree)
  368. {
  369. #ifndef USING_OUTPUT_SPY
  370. TEST_IGNORE();
  371. #else
  372. void* m = malloc(10);
  373. char* s = (char*)m;
  374. TEST_ASSERT_NOT_NULL(m);
  375. s[-1] = (char)0x00; /* Will not detect 0 */
  376. s[-2] = (char)0x01;
  377. UnityOutputCharSpy_Enable(1);
  378. EXPECT_ABORT_BEGIN
  379. free(m);
  380. EXPECT_ABORT_END
  381. UnityOutputCharSpy_Enable(0);
  382. Unity.CurrentTestFailed = 0;
  383. CHECK(strstr(UnityOutputCharSpy_Get(), "Buffer overrun detected during free()"));
  384. #endif
  385. }
  386. TEST(LeakDetection, BufferGuardWriteFoundDuringRealloc)
  387. {
  388. #ifndef USING_OUTPUT_SPY
  389. TEST_IGNORE();
  390. #else
  391. void* m = malloc(10);
  392. char* s = (char*)m;
  393. TEST_ASSERT_NOT_NULL(m);
  394. s[-1] = (char)0x0A;
  395. UnityOutputCharSpy_Enable(1);
  396. EXPECT_ABORT_BEGIN
  397. m = realloc(m, 100);
  398. EXPECT_ABORT_END
  399. UnityOutputCharSpy_Enable(0);
  400. Unity.CurrentTestFailed = 0;
  401. CHECK(strstr(UnityOutputCharSpy_Get(), "Buffer overrun detected during realloc()"));
  402. #endif
  403. }
  404. TEST(LeakDetection, PointerSettingMax)
  405. {
  406. #ifndef USING_OUTPUT_SPY
  407. TEST_IGNORE();
  408. #else
  409. int i;
  410. for (i = 0; i < UNITY_MAX_POINTERS; i++) UT_PTR_SET(pointer1, &int1);
  411. UnityOutputCharSpy_Enable(1);
  412. EXPECT_ABORT_BEGIN
  413. UT_PTR_SET(pointer1, &int1);
  414. EXPECT_ABORT_END
  415. UnityOutputCharSpy_Enable(0);
  416. Unity.CurrentTestFailed = 0;
  417. CHECK(strstr(UnityOutputCharSpy_Get(), "Too many pointers set"));
  418. #endif
  419. }
  420. /*------------------------------------------------------------ */
  421. TEST_GROUP(InternalMalloc);
  422. #define TEST_ASSERT_MEMORY_ALL_FREE_LIFO_ORDER(first_mem_ptr, ptr) \
  423. ptr = malloc(10); free(ptr); \
  424. TEST_ASSERT_EQUAL_PTR_MESSAGE(first_mem_ptr, ptr, "Memory was stranded, free in LIFO order");
  425. TEST_SETUP(InternalMalloc) { }
  426. TEST_TEAR_DOWN(InternalMalloc) { }
  427. TEST(InternalMalloc, MallocPastBufferFails)
  428. {
  429. #ifdef UNITY_EXCLUDE_STDLIB_MALLOC
  430. void* m = malloc(UNITY_INTERNAL_HEAP_SIZE_BYTES/2 + 1);
  431. void* n = malloc(UNITY_INTERNAL_HEAP_SIZE_BYTES/2);
  432. free(m);
  433. TEST_ASSERT_NOT_NULL(m);
  434. TEST_ASSERT_NULL(n);
  435. TEST_ASSERT_MEMORY_ALL_FREE_LIFO_ORDER(m, n);
  436. #endif
  437. }
  438. TEST(InternalMalloc, CallocPastBufferFails)
  439. {
  440. #ifdef UNITY_EXCLUDE_STDLIB_MALLOC
  441. void* m = calloc(1, UNITY_INTERNAL_HEAP_SIZE_BYTES/2 + 1);
  442. void* n = calloc(1, UNITY_INTERNAL_HEAP_SIZE_BYTES/2);
  443. free(m);
  444. TEST_ASSERT_NOT_NULL(m);
  445. TEST_ASSERT_NULL(n);
  446. TEST_ASSERT_MEMORY_ALL_FREE_LIFO_ORDER(m, n);
  447. #endif
  448. }
  449. TEST(InternalMalloc, MallocThenReallocGrowsMemoryInPlace)
  450. {
  451. #ifdef UNITY_EXCLUDE_STDLIB_MALLOC
  452. void* m = malloc(UNITY_INTERNAL_HEAP_SIZE_BYTES/2 + 1);
  453. void* n = realloc(m, UNITY_INTERNAL_HEAP_SIZE_BYTES/2 + 9);
  454. free(n);
  455. TEST_ASSERT_NOT_NULL(m);
  456. TEST_ASSERT_EQUAL(m, n);
  457. TEST_ASSERT_MEMORY_ALL_FREE_LIFO_ORDER(m, n);
  458. #endif
  459. }
  460. TEST(InternalMalloc, ReallocFailDoesNotFreeMem)
  461. {
  462. #ifdef UNITY_EXCLUDE_STDLIB_MALLOC
  463. void* m = malloc(UNITY_INTERNAL_HEAP_SIZE_BYTES/2);
  464. void* n1 = malloc(10);
  465. void* out_of_mem = realloc(n1, UNITY_INTERNAL_HEAP_SIZE_BYTES/2 + 1);
  466. void* n2 = malloc(10);
  467. free(n2);
  468. if (out_of_mem == NULL) free(n1);
  469. free(m);
  470. TEST_ASSERT_NOT_NULL(m); /* Got a real memory location */
  471. TEST_ASSERT_NULL(out_of_mem); /* The realloc should have failed */
  472. TEST_ASSERT_NOT_EQUAL(n2, n1); /* If n1 != n2 then realloc did not free n1 */
  473. TEST_ASSERT_MEMORY_ALL_FREE_LIFO_ORDER(m, n2);
  474. #endif
  475. }