test_utils.c 4.9 KB

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