test_runner.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include "string.h"
  8. #include "esp_heap_caps.h"
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "unity.h"
  12. #include "unity_test_runner.h"
  13. #include "test_utils.h"
  14. #include "esp_newlib.h"
  15. #include "memory_checks.h"
  16. #ifdef CONFIG_HEAP_TRACING
  17. #include "esp_heap_trace.h"
  18. #endif
  19. static void unity_task(void *pvParameters)
  20. {
  21. vTaskDelay(2); /* Delay a bit to let the main task be deleted */
  22. unity_run_menu(); /* Doesn't return */
  23. }
  24. void test_main(void)
  25. {
  26. // Note: if unpinning this task, change the way run times are calculated in
  27. // unity_port_esp32.c
  28. xTaskCreatePinnedToCore(unity_task, "unityTask", UNITY_FREERTOS_STACK_SIZE, NULL,
  29. UNITY_FREERTOS_PRIORITY, NULL, UNITY_FREERTOS_CPU);
  30. }
  31. /* setUp runs before every test */
  32. void setUp(void)
  33. {
  34. // If heap tracing is enabled in kconfig, leak trace the test
  35. #ifdef CONFIG_HEAP_TRACING
  36. setup_heap_record();
  37. #endif
  38. printf("%s", ""); /* sneakily lazy-allocate the reent structure for this test task */
  39. #ifdef CONFIG_APP_BUILD_USE_FLASH_SECTIONS
  40. /* TODO: add sufficient startup code in case of building an ELF file, so that
  41. * flash cache is initialized and can work in such mode.
  42. * For now this is disabled to allow running unit tests which don't require
  43. * flash cache related operations.
  44. */
  45. get_test_data_partition(); /* allocate persistent partition table structures */
  46. #endif // CONFIG_APP_BUILD_USE_FLASH_SECTIONS
  47. #ifdef CONFIG_HEAP_TRACING
  48. heap_trace_start(HEAP_TRACE_LEAKS);
  49. #endif
  50. test_utils_record_free_mem();
  51. test_utils_set_leak_level(CONFIG_UNITY_CRITICAL_LEAK_LEVEL_GENERAL, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL);
  52. test_utils_set_leak_level(CONFIG_UNITY_WARN_LEAK_LEVEL_GENERAL, ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_GENERAL);
  53. }
  54. typedef enum {
  55. NO_LEAK_CHECK,
  56. DEFAULT_LEAK_CHECK,
  57. SPECIAL_LEAK_CHECK
  58. } leak_check_type_t;
  59. /**
  60. * It is possible to specify the maximum allowed memory leak level directly in the test case
  61. * or disable leak checking for a test case.
  62. * This function checks if this is the case and return the appropriate return value.
  63. * If a custom leak level has been specified, that custom threshold is written to the value pointed by threshold.
  64. */
  65. static leak_check_type_t leak_check_required(size_t *threshold)
  66. {
  67. if (Unity.CurrentDetail1 != NULL) {
  68. const char *leaks = "[leaks";
  69. const int len_leaks = strlen(leaks);
  70. const char *sub_leaks = strstr(Unity.CurrentDetail1, leaks);
  71. if (sub_leaks != NULL) {
  72. if (sub_leaks[len_leaks] == ']') {
  73. return NO_LEAK_CHECK;
  74. } else if (sub_leaks[len_leaks] == '=') {
  75. *threshold = strtol(&sub_leaks[len_leaks + 1], NULL, 10);
  76. return SPECIAL_LEAK_CHECK;
  77. }
  78. }
  79. }
  80. return DEFAULT_LEAK_CHECK;
  81. }
  82. /* tearDown runs after every test */
  83. void tearDown(void)
  84. {
  85. /* some FreeRTOS stuff is cleaned up by idle task */
  86. vTaskDelay(5);
  87. /* clean up some of the newlib's lazy allocations */
  88. esp_reent_cleanup();
  89. /* We want the teardown to have this file in the printout if TEST_ASSERT fails */
  90. const char *real_testfile = Unity.TestFile;
  91. Unity.TestFile = __FILE__;
  92. /* check if unit test has caused heap corruption in any heap */
  93. TEST_ASSERT_MESSAGE( heap_caps_check_integrity(MALLOC_CAP_INVALID, true), "The test has corrupted the heap");
  94. /* check for leaks */
  95. #ifdef CONFIG_HEAP_TRACING
  96. heap_trace_stop();
  97. heap_trace_dump();
  98. #endif
  99. size_t leak_threshold_critical = 0;
  100. size_t leak_threshold_warning = 0;
  101. leak_check_type_t check_type = leak_check_required(&leak_threshold_critical);
  102. // In the "special case", only one level can be passed directly from the test case.
  103. // Hence, we set both warning and critical leak levels to that same value here
  104. leak_threshold_warning = leak_threshold_critical;
  105. if (check_type == NO_LEAK_CHECK) {
  106. // do not check
  107. } else if (check_type == SPECIAL_LEAK_CHECK) {
  108. test_utils_finish_and_evaluate_leaks(leak_threshold_warning, leak_threshold_critical);
  109. } else if (check_type == DEFAULT_LEAK_CHECK) {
  110. test_utils_finish_and_evaluate_leaks(test_utils_get_leak_level(ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_ALL),
  111. test_utils_get_leak_level(ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_ALL));
  112. } else {
  113. assert(false); // coding error
  114. }
  115. Unity.TestFile = real_testfile; // go back to the real filename
  116. }