test_utils.c 4.8 KB

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