test_utils.c 8.3 KB

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