test_utils.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include "unity.h"
  8. #include "test_utils.h"
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "esp_netif.h"
  12. #include "lwip/sockets.h"
  13. #include "sdkconfig.h"
  14. #include "memory_checks.h"
  15. #if !CONFIG_FREERTOS_UNICORE
  16. #include "esp_ipc.h"
  17. #include "esp_freertos_hooks.h"
  18. #endif
  19. const esp_partition_t *get_test_data_partition(void)
  20. {
  21. /* This finds "flash_test" partition defined in partition_table_unit_test_app.csv */
  22. const esp_partition_t *result = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
  23. ESP_PARTITION_SUBTYPE_ANY, "flash_test");
  24. TEST_ASSERT_NOT_NULL(result); /* means partition table set wrong */
  25. return result;
  26. }
  27. void test_case_uses_tcpip(void)
  28. {
  29. // Can be called more than once, does nothing on subsequent calls
  30. esp_netif_init();
  31. // Allocate all sockets then free them
  32. // (First time each socket is allocated some one-time allocations happen.)
  33. int sockets[CONFIG_LWIP_MAX_SOCKETS];
  34. for (int i = 0; i < CONFIG_LWIP_MAX_SOCKETS; i++) {
  35. int type = (i % 2 == 0) ? SOCK_DGRAM : SOCK_STREAM;
  36. int family = (i % 3 == 0) ? PF_INET6 : PF_INET;
  37. sockets[i] = socket(family, type, IPPROTO_IP);
  38. }
  39. for (int i = 0; i < CONFIG_LWIP_MAX_SOCKETS; i++) {
  40. close(sockets[i]);
  41. }
  42. // Allow LWIP tasks to finish initialising themselves
  43. vTaskDelay(25 / portTICK_RATE_MS);
  44. printf("Note: esp_netif_init() has been called. Until next reset, TCP/IP task will periodicially allocate memory and consume CPU time.\n");
  45. // Reset the leak checker as LWIP allocates a lot of memory on first run
  46. test_utils_record_free_mem();
  47. test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL);
  48. test_utils_set_leak_level(CONFIG_UNITY_CRITICAL_LEAK_LEVEL_LWIP, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_LWIP);
  49. }
  50. // wait user to send "Enter" key or input parameter
  51. static void wait_user_control(char* parameter_buf, uint8_t buf_len)
  52. {
  53. char *buffer = parameter_buf;
  54. char sign[5];
  55. uint8_t buffer_len = buf_len - 1;
  56. if (parameter_buf == NULL) {
  57. buffer = sign;
  58. buffer_len = sizeof(sign) - 1;
  59. }
  60. // workaround that unity_gets (esp_rom_uart_rx_string) will not set '\0' correctly
  61. bzero(buffer, buffer_len);
  62. unity_gets(buffer, buffer_len);
  63. }
  64. // signal functions, used for sync between unity DUTs for multiple devices cases
  65. void unity_wait_for_signal_param(const char* signal_name, char* parameter_buf, uint8_t buf_len)
  66. {
  67. printf("Waiting for signal: [%s]!\n", signal_name);
  68. if (parameter_buf == NULL) {
  69. printf("Please press \"Enter\" key once any board send this signal.\n");
  70. } else {
  71. printf("Please input parameter value from any board send this signal and press \"Enter\" key.\n");
  72. }
  73. wait_user_control(parameter_buf, buf_len);
  74. }
  75. void unity_send_signal_param(const char* signal_name, const char *parameter)
  76. {
  77. if (parameter == NULL) {
  78. printf("Send signal: [%s]!\n", signal_name);
  79. } else {
  80. printf("Send signal: [%s][%s]!\n", signal_name, parameter);
  81. }
  82. }
  83. bool unity_util_convert_mac_from_string(const char* mac_str, uint8_t *mac_addr)
  84. {
  85. uint8_t loop = 0;
  86. uint8_t tmp = 0;
  87. const char *start;
  88. char *stop;
  89. for (loop = 0; loop < 6; loop++) {
  90. start = mac_str + loop * 3;
  91. tmp = strtol(start, &stop, 16);
  92. if (stop - start == 2 && (*stop == ':' || (*stop == 0 && loop == 5))) {
  93. mac_addr[loop] = tmp;
  94. } else {
  95. return false;
  96. }
  97. }
  98. return true;
  99. }
  100. #define EXHAUST_MEMORY_ENTRIES 100
  101. struct test_utils_exhaust_memory_record_s {
  102. int *entries[EXHAUST_MEMORY_ENTRIES];
  103. };
  104. test_utils_exhaust_memory_rec test_utils_exhaust_memory(uint32_t caps, size_t limit)
  105. {
  106. int idx = 0;
  107. test_utils_exhaust_memory_rec rec = calloc(1, sizeof(struct test_utils_exhaust_memory_record_s));
  108. TEST_ASSERT_NOT_NULL_MESSAGE(rec, "test_utils_exhaust_memory: not enough free memory to allocate record structure!");
  109. while (idx < EXHAUST_MEMORY_ENTRIES) {
  110. size_t free_caps = heap_caps_get_largest_free_block(caps);
  111. if (free_caps <= limit) {
  112. return rec; // done!
  113. }
  114. rec->entries[idx] = heap_caps_malloc(free_caps - limit, caps);
  115. TEST_ASSERT_NOT_NULL_MESSAGE(rec->entries[idx],
  116. "test_utils_exhaust_memory: something went wrong while freeing up memory, is another task using heap?");
  117. heap_caps_check_integrity_all(true);
  118. idx++;
  119. }
  120. TEST_FAIL_MESSAGE("test_utils_exhaust_memory: The heap with the requested caps is too fragmented, increase EXHAUST_MEMORY_ENTRIES or defrag the heap!");
  121. abort();
  122. }
  123. void test_utils_free_exhausted_memory(test_utils_exhaust_memory_rec rec)
  124. {
  125. for (int i = 0; i < EXHAUST_MEMORY_ENTRIES; i++) {
  126. free(rec->entries[i]);
  127. }
  128. free(rec);
  129. }
  130. #if !CONFIG_FREERTOS_UNICORE
  131. static SemaphoreHandle_t test_sem;
  132. static bool test_idle_hook_func(void)
  133. {
  134. if (test_sem) {
  135. xSemaphoreGive(test_sem);
  136. }
  137. return true;
  138. }
  139. static void test_task_delete_func(void *arg)
  140. {
  141. vTaskDelete(arg);
  142. }
  143. #endif // !CONFIG_FREERTOS_UNICORE
  144. void test_utils_task_delete(TaskHandle_t thandle)
  145. {
  146. /* Self deletion can not free up associated task dynamic memory immediately,
  147. * hence not recommended for test scenarios */
  148. TEST_ASSERT_NOT_NULL_MESSAGE(thandle, "test_utils_task_delete: handle is NULL");
  149. TEST_ASSERT_NOT_EQUAL_MESSAGE(thandle, xTaskGetCurrentTaskHandle(), "test_utils_task_delete: handle is of currently executing task");
  150. #if CONFIG_FREERTOS_UNICORE
  151. vTaskDelete(thandle);
  152. #else // CONFIG_FREERTOS_UNICORE
  153. const BaseType_t tsk_affinity = xTaskGetAffinity(thandle);
  154. const BaseType_t core_id = xPortGetCoreID();
  155. printf("Task_affinity: 0x%x, current_core: %d\n", tsk_affinity, core_id);
  156. if (tsk_affinity == tskNO_AFFINITY) {
  157. /* For no affinity case, we wait for idle hook to trigger on different core */
  158. esp_err_t ret = esp_register_freertos_idle_hook_for_cpu(test_idle_hook_func, !core_id);
  159. TEST_ASSERT_EQUAL_MESSAGE(ret, ESP_OK, "test_utils_task_delete: failed to register idle hook");
  160. vTaskDelete(thandle);
  161. test_sem = xSemaphoreCreateBinary();
  162. TEST_ASSERT_NOT_NULL_MESSAGE(test_sem, "test_utils_task_delete: failed to create semaphore");
  163. xSemaphoreTake(test_sem, portMAX_DELAY);
  164. esp_deregister_freertos_idle_hook_for_cpu(test_idle_hook_func, !core_id);
  165. vSemaphoreDelete(test_sem);
  166. test_sem = NULL;
  167. } else if (tsk_affinity != core_id) {
  168. /* Task affinity and current core are differnt, schedule IPC call (to delete task)
  169. * on core where task is pinned to */
  170. esp_ipc_call_blocking(tsk_affinity, test_task_delete_func, thandle);
  171. } else {
  172. /* Task affinity and current core are same, so we can safely proceed for deletion */
  173. vTaskDelete(thandle);
  174. }
  175. #endif // !CONFIG_FREERTOS_UNICORE
  176. }