test_runner.c 4.7 KB

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