test_runner.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2016-2018 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdio.h>
  15. #include "string.h"
  16. #include "esp_heap_caps.h"
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/task.h"
  19. #include "unity.h"
  20. #include "unity_test_runner.h"
  21. #include "test_utils.h"
  22. #include "esp_newlib.h"
  23. #ifdef CONFIG_HEAP_TRACING
  24. #include "esp_heap_trace.h"
  25. #endif
  26. static size_t before_free_8bit;
  27. static size_t before_free_32bit;
  28. static size_t warn_leak_threshold;
  29. static size_t critical_leak_threshold;
  30. static void unity_task(void *pvParameters)
  31. {
  32. vTaskDelay(2); /* Delay a bit to let the main task be deleted */
  33. unity_run_menu(); /* Doesn't return */
  34. }
  35. void test_main(void)
  36. {
  37. // Note: if unpinning this task, change the way run times are calculated in
  38. // unity_port_esp32.c
  39. xTaskCreatePinnedToCore(unity_task, "unityTask", UNITY_FREERTOS_STACK_SIZE, NULL,
  40. UNITY_FREERTOS_PRIORITY, NULL, UNITY_FREERTOS_CPU);
  41. }
  42. void unity_reset_leak_checks(void)
  43. {
  44. before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  45. before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
  46. #ifdef CONFIG_HEAP_TRACING
  47. heap_trace_start(HEAP_TRACE_LEAKS);
  48. #endif
  49. }
  50. /* setUp runs before every test */
  51. void setUp(void)
  52. {
  53. // If heap tracing is enabled in kconfig, leak trace the test
  54. #ifdef CONFIG_HEAP_TRACING
  55. const size_t num_heap_records = 80;
  56. static heap_trace_record_t *record_buffer;
  57. if (!record_buffer) {
  58. record_buffer = malloc(sizeof(heap_trace_record_t) * num_heap_records);
  59. assert(record_buffer);
  60. heap_trace_init_standalone(record_buffer, num_heap_records);
  61. }
  62. #endif
  63. printf("%s", ""); /* sneakily lazy-allocate the reent structure for this test task */
  64. #ifdef CONFIG_APP_BUILD_USE_FLASH_SECTIONS
  65. /* TODO: add sufficient startup code in case of building an ELF file, so that
  66. * flash cache is initialized and can work in such mode.
  67. * For now this is disabled to allow running unit tests which don't require
  68. * flash cache related operations.
  69. */
  70. get_test_data_partition(); /* allocate persistent partition table structures */
  71. #endif // CONFIG_APP_BUILD_USE_FLASH_SECTIONS
  72. unity_reset_leak_checks();
  73. test_utils_set_leak_level(CONFIG_UNITY_CRITICAL_LEAK_LEVEL_GENERAL, TYPE_LEAK_CRITICAL, COMP_LEAK_GENERAL);
  74. test_utils_set_leak_level(CONFIG_UNITY_WARN_LEAK_LEVEL_GENERAL, TYPE_LEAK_WARNING, COMP_LEAK_GENERAL);
  75. }
  76. static void check_leak(size_t before_free, size_t after_free, const char *type)
  77. {
  78. int free_delta = (int)after_free - (int)before_free;
  79. printf("MALLOC_CAP_%s usage: Free memory delta: %d Leak threshold: -%u \n",
  80. type,
  81. free_delta,
  82. critical_leak_threshold);
  83. if (free_delta > 0) {
  84. return; // free memory went up somehow
  85. }
  86. size_t leaked = (size_t)(free_delta * -1);
  87. if (leaked <= warn_leak_threshold) {
  88. return;
  89. }
  90. printf("MALLOC_CAP_%s %s leak: Before %u bytes free, After %u bytes free (delta %u)\n",
  91. type,
  92. leaked <= critical_leak_threshold ? "potential" : "critical",
  93. before_free, after_free, leaked);
  94. fflush(stdout);
  95. TEST_ASSERT_MESSAGE(leaked <= critical_leak_threshold, "The test leaked too much memory");
  96. }
  97. static bool leak_check_required(void)
  98. {
  99. warn_leak_threshold = test_utils_get_leak_level(TYPE_LEAK_WARNING, COMP_LEAK_ALL);
  100. critical_leak_threshold = test_utils_get_leak_level(TYPE_LEAK_CRITICAL, COMP_LEAK_ALL);
  101. if (Unity.CurrentDetail1 != NULL) {
  102. const char *leaks = "[leaks";
  103. const int len_leaks = strlen(leaks);
  104. const char *sub_leaks = strstr(Unity.CurrentDetail1, leaks);
  105. if (sub_leaks != NULL) {
  106. if (sub_leaks[len_leaks] == ']') {
  107. return false;
  108. } else if (sub_leaks[len_leaks] == '=') {
  109. critical_leak_threshold = strtol(&sub_leaks[len_leaks + 1], NULL, 10);
  110. warn_leak_threshold = critical_leak_threshold;
  111. return true;
  112. }
  113. }
  114. }
  115. return true;
  116. }
  117. /* tearDown runs after every test */
  118. void tearDown(void)
  119. {
  120. /* some FreeRTOS stuff is cleaned up by idle task */
  121. vTaskDelay(5);
  122. /* clean up some of the newlib's lazy allocations */
  123. esp_reent_cleanup();
  124. /* We want the teardown to have this file in the printout if TEST_ASSERT fails */
  125. const char *real_testfile = Unity.TestFile;
  126. Unity.TestFile = __FILE__;
  127. /* check if unit test has caused heap corruption in any heap */
  128. TEST_ASSERT_MESSAGE( heap_caps_check_integrity(MALLOC_CAP_INVALID, true), "The test has corrupted the heap");
  129. /* check for leaks */
  130. #ifdef CONFIG_HEAP_TRACING
  131. heap_trace_stop();
  132. heap_trace_dump();
  133. #endif
  134. if (leak_check_required()) {
  135. size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  136. size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
  137. check_leak(before_free_8bit, after_free_8bit, "8BIT");
  138. check_leak(before_free_32bit, after_free_32bit, "32BIT");
  139. }
  140. Unity.TestFile = real_testfile; // go back to the real filename
  141. }