test_utils.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "esp32/rom/ets_sys.h"
  18. #include "esp32/rom/uart.h"
  19. #include "freertos/FreeRTOS.h"
  20. #include "freertos/task.h"
  21. #include "tcpip_adapter.h"
  22. #include "lwip/sockets.h"
  23. const esp_partition_t *get_test_data_partition()
  24. {
  25. /* This finds "flash_test" partition defined in partition_table_unit_test_app.csv */
  26. const esp_partition_t *result = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
  27. ESP_PARTITION_SUBTYPE_ANY, "flash_test");
  28. TEST_ASSERT_NOT_NULL(result); /* means partition table set wrong */
  29. return result;
  30. }
  31. void test_case_uses_tcpip()
  32. {
  33. // Can be called more than once, does nothing on subsequent calls
  34. tcpip_adapter_init();
  35. // Allocate all sockets then free them
  36. // (First time each socket is allocated some one-time allocations happen.)
  37. int sockets[CONFIG_LWIP_MAX_SOCKETS];
  38. for (int i = 0; i < CONFIG_LWIP_MAX_SOCKETS; i++) {
  39. int type = (i % 2 == 0) ? SOCK_DGRAM : SOCK_STREAM;
  40. int family = (i % 3 == 0) ? PF_INET6 : PF_INET;
  41. sockets[i] = socket(family, type, IPPROTO_IP);
  42. }
  43. for (int i = 0; i < CONFIG_LWIP_MAX_SOCKETS; i++) {
  44. close(sockets[i]);
  45. }
  46. // Allow LWIP tasks to finish initialising themselves
  47. vTaskDelay(25 / portTICK_RATE_MS);
  48. printf("Note: tcpip_adapter_init() has been called. Until next reset, TCP/IP task will periodicially allocate memory and consume CPU time.\n");
  49. // Reset the leak checker as LWIP allocates a lot of memory on first run
  50. unity_reset_leak_checks();
  51. test_utils_set_leak_level(0, TYPE_LEAK_CRITICAL, COMP_LEAK_GENERAL);
  52. test_utils_set_leak_level(CONFIG_UNITY_CRITICAL_LEAK_LEVEL_LWIP, TYPE_LEAK_CRITICAL, COMP_LEAK_LWIP);
  53. }
  54. // wait user to send "Enter" key or input parameter
  55. static void wait_user_control(char* parameter_buf, uint8_t buf_len)
  56. {
  57. char *buffer = parameter_buf;
  58. char sign[5];
  59. uint8_t buffer_len = buf_len - 1;
  60. if (parameter_buf == NULL) {
  61. buffer = sign;
  62. buffer_len = sizeof(sign) - 1;
  63. }
  64. // workaround that unity_gets (UartRxString) will not set '\0' correctly
  65. bzero(buffer, buffer_len);
  66. unity_gets(buffer, buffer_len);
  67. }
  68. // signal functions, used for sync between unity DUTs for multiple devices cases
  69. void unity_wait_for_signal_param(const char* signal_name, char* parameter_buf, uint8_t buf_len)
  70. {
  71. printf("Waiting for signal: [%s]!\n", signal_name);
  72. if (parameter_buf == NULL) {
  73. printf("Please press \"Enter\" key once any board send this signal.\n");
  74. } else {
  75. printf("Please input parameter value from any board send this signal and press \"Enter\" key.\n");
  76. }
  77. wait_user_control(parameter_buf, buf_len);
  78. }
  79. void unity_send_signal_param(const char* signal_name, const char *parameter)
  80. {
  81. if (parameter == NULL) {
  82. printf("Send signal: [%s]!\n", signal_name);
  83. } else {
  84. printf("Send signal: [%s][%s]!\n", signal_name, parameter);
  85. }
  86. }
  87. bool unity_util_convert_mac_from_string(const char* mac_str, uint8_t *mac_addr)
  88. {
  89. uint8_t loop = 0;
  90. uint8_t tmp = 0;
  91. const char *start;
  92. char *stop;
  93. for (loop = 0; loop < 6; loop++) {
  94. start = mac_str + loop * 3;
  95. tmp = strtol(start, &stop, 16);
  96. if (stop - start == 2 && (*stop == ':' || (*stop == 0 && loop == 5))) {
  97. mac_addr[loop] = tmp;
  98. } else {
  99. return false;
  100. }
  101. }
  102. return true;
  103. }
  104. static size_t test_unity_leak_level[TYPE_LEAK_MAX][COMP_LEAK_ALL] = { 0 };
  105. esp_err_t test_utils_set_leak_level(size_t leak_level, esp_type_leak_t type_of_leak, esp_comp_leak_t component)
  106. {
  107. if (type_of_leak >= TYPE_LEAK_MAX || component >= COMP_LEAK_ALL) {
  108. return ESP_ERR_INVALID_ARG;
  109. }
  110. test_unity_leak_level[type_of_leak][component] = leak_level;
  111. return ESP_OK;
  112. }
  113. size_t test_utils_get_leak_level(esp_type_leak_t type_of_leak, esp_comp_leak_t component)
  114. {
  115. size_t leak_level = 0;
  116. if (type_of_leak >= TYPE_LEAK_MAX || component > COMP_LEAK_ALL) {
  117. leak_level = 0;
  118. } else {
  119. if (component == COMP_LEAK_ALL) {
  120. for (int comp = 0; comp < COMP_LEAK_ALL; ++comp) {
  121. leak_level += test_unity_leak_level[type_of_leak][comp];
  122. }
  123. } else {
  124. leak_level = test_unity_leak_level[type_of_leak][component];
  125. }
  126. }
  127. return leak_level;
  128. }
  129. #define EXHAUST_MEMORY_ENTRIES 100
  130. struct test_utils_exhaust_memory_record_s {
  131. int *entries[EXHAUST_MEMORY_ENTRIES];
  132. };
  133. test_utils_exhaust_memory_rec test_utils_exhaust_memory(uint32_t caps, size_t limit)
  134. {
  135. int idx = 0;
  136. test_utils_exhaust_memory_rec rec = calloc(1, sizeof(struct test_utils_exhaust_memory_record_s));
  137. TEST_ASSERT_NOT_NULL_MESSAGE(rec, "test_utils_exhaust_memory: not enough free memory to allocate record structure!");
  138. while (idx < EXHAUST_MEMORY_ENTRIES) {
  139. size_t free_caps = heap_caps_get_largest_free_block(caps);
  140. if (free_caps <= limit) {
  141. return rec; // done!
  142. }
  143. rec->entries[idx] = heap_caps_malloc(free_caps - limit, caps);
  144. TEST_ASSERT_NOT_NULL_MESSAGE(rec->entries[idx],
  145. "test_utils_exhaust_memory: something went wrong while freeing up memory, is another task using heap?");
  146. heap_caps_check_integrity_all(true);
  147. idx++;
  148. }
  149. TEST_FAIL_MESSAGE("test_utils_exhaust_memory: The heap with the requested caps is too fragmented, increase EXHAUST_MEMORY_ENTRIES or defrag the heap!");
  150. abort();
  151. }
  152. void test_utils_free_exhausted_memory(test_utils_exhaust_memory_rec rec)
  153. {
  154. for (int i = 0; i < EXHAUST_MEMORY_ENTRIES; i++) {
  155. free(rec->entries[i]);
  156. }
  157. free(rec);
  158. }