test_utils.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright 2015-2018 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. #pragma once
  15. // Utilities for esp-idf unit tests
  16. #include <stdint.h>
  17. #include <esp_partition.h>
  18. #include "sdkconfig.h"
  19. /* include performance pass standards header file */
  20. #include "idf_performance.h"
  21. /* For performance check with unity test on IDF */
  22. /* These macros should only be used with ESP-IDF.
  23. * To use performance check, we need to first define pass standard in idf_performance.h.
  24. */
  25. #define TEST_PERFORMANCE_LESS_THAN(name, value_fmt, value) do { \
  26. printf("[Performance]["#name"]: "value_fmt"\n", value); \
  27. TEST_ASSERT(value < IDF_PERFORMANCE_MAX_##name); \
  28. } while(0)
  29. #define TEST_PERFORMANCE_GREATER_THAN(name, value_fmt, value) do { \
  30. printf("[Performance]["#name"]: "value_fmt"\n", value); \
  31. TEST_ASSERT(value > IDF_PERFORMANCE_MIN_##name); \
  32. } while(0)
  33. /* @brief macro to print IDF performance
  34. * @param mode : performance item name. a string pointer.
  35. * @param value_fmt: print format and unit of the value, for example: "%02fms", "%dKB"
  36. * @param value : the performance value.
  37. */
  38. #define IDF_LOG_PERFORMANCE(item, value_fmt, value) \
  39. printf("[Performance][%s]: "value_fmt"\n", item, value)
  40. /* Some definitions applicable to Unity running in FreeRTOS */
  41. #define UNITY_FREERTOS_PRIORITY CONFIG_UNITY_FREERTOS_PRIORITY
  42. #define UNITY_FREERTOS_CPU CONFIG_UNITY_FREERTOS_CPU
  43. #define UNITY_FREERTOS_STACK_SIZE CONFIG_UNITY_FREERTOS_STACK_SIZE
  44. /* Return the 'flash_test' custom data partition (type 0x55)
  45. defined in the custom partition table.
  46. */
  47. const esp_partition_t *get_test_data_partition();
  48. /**
  49. * @brief Initialize reference clock
  50. *
  51. * Reference clock provides timestamps at constant 1 MHz frequency, even when
  52. * the APB frequency is changing.
  53. */
  54. void ref_clock_init();
  55. /**
  56. * @brief Deinitialize reference clock
  57. */
  58. void ref_clock_deinit();
  59. /**
  60. * @brief Get reference clock timestamp
  61. * @return number of microseconds since the reference clock was initialized
  62. */
  63. uint64_t ref_clock_get();
  64. /**
  65. * @brief Entry point of the test application
  66. *
  67. * Starts Unity test runner in a separate task and returns.
  68. */
  69. void test_main();
  70. /**
  71. * @brief Reset automatic leak checking which happens in unit tests.
  72. *
  73. * Updates recorded "before" free memory values to the free memory values
  74. * at time of calling. Resets leak checker if tracing is enabled in
  75. * config.
  76. *
  77. * This can be called if a test case does something which allocates
  78. * memory on first use, for example.
  79. *
  80. * @note Use with care as this can mask real memory leak problems.
  81. */
  82. void unity_reset_leak_checks(void);
  83. /**
  84. * @brief Call this function from a test case which requires TCP/IP or
  85. * LWIP functionality.
  86. *
  87. * @note This should be the first function the test case calls, as it will
  88. * allocate memory on first use (and also reset the test case leak checker).
  89. */
  90. void test_case_uses_tcpip(void);
  91. /**
  92. * @brief wait for signals with parameters.
  93. *
  94. * for multiple devices test cases, DUT might need to wait for other DUTs before continue testing.
  95. * As all DUTs are independent, need user (or test script) interaction to make test synchronized.
  96. *
  97. * Here we provide signal functions for this.
  98. * For example, we're testing GPIO, DUT1 has one pin connect to with DUT2.
  99. * DUT2 will output high level and then DUT1 will read input.
  100. * DUT1 should call `unity_wait_for_signal("output high level");` before it reads input.
  101. * DUT2 should call `unity_send_signal("output high level");` after it finished setting output high level.
  102. * According to the console logs:
  103. *
  104. * DUT1 console:
  105. *
  106. * ```
  107. * Waiting for signal: [output high level]!
  108. * Please press "Enter" key to once any board send this signal.
  109. * ```
  110. *
  111. * DUT2 console:
  112. *
  113. * ```
  114. * Send signal: [output high level]!
  115. * ```
  116. *
  117. * Then we press Enter key on DUT1's console, DUT1 starts to read input and then test success.
  118. *
  119. * Another example, we have 2 DUTs in multiple devices test, and DUT1 need to get DUT2's mac address to perform BT connection.
  120. * DUT1 should call `unity_wait_for_signal_param("dut2 mac address", mac, 19);` to wait for DUT2's mac address.
  121. * DUT2 should call `unity_send_signal_param("dut2 mac address", "10:20:30:40:50:60");` to send to DUT1 its mac address.
  122. * According to the console logs:
  123. *
  124. * DUT1 console:
  125. *
  126. * ```
  127. * Waiting for signal: [dut2 mac address]!
  128. * Please input parameter value from any board send this signal and press "Enter" key.
  129. * ```
  130. *
  131. * DUT2 console:
  132. *
  133. * ```
  134. * Send signal: [dut2 mac address][10:20:30:40:50:60]!
  135. * ```
  136. *
  137. * @param signal_name signal name which DUT expected to wait before proceed testing
  138. * @param parameter_buf buffer to receive parameter
  139. * @param buf_len length of parameter_buf.
  140. * Currently we have a limitation that it will write 1 extra byte at the end of string.
  141. * We need to use a buffer with 2 bytes longer than actual string length.
  142. */
  143. void unity_wait_for_signal_param(const char* signal_name, char *parameter_buf, uint8_t buf_len);
  144. /**
  145. * @brief wait for signals.
  146. *
  147. * @param signal_name signal name which DUT expected to wait before proceed testing
  148. */
  149. static inline void unity_wait_for_signal(const char* signal_name)
  150. {
  151. unity_wait_for_signal_param(signal_name, NULL, 0);
  152. }
  153. /**
  154. * @brief DUT send signal and pass parameter to other devices.
  155. *
  156. * @param signal_name signal name which DUT send once it finished preparing.
  157. * @param parameter a string to let remote device to receive.
  158. */
  159. void unity_send_signal_param(const char* signal_name, const char *parameter);
  160. /**
  161. * @brief DUT send signal with parameter.
  162. *
  163. * @param signal_name signal name which DUT send once it finished preparing.
  164. */
  165. static inline void unity_send_signal(const char* signal_name)
  166. {
  167. unity_send_signal_param(signal_name, NULL);
  168. }
  169. /**
  170. * @brief convert mac string to mac address
  171. *
  172. * @param mac_str MAC address string with format "xx:xx:xx:xx:xx:xx"
  173. * @param[out] mac_addr store converted MAC address
  174. */
  175. bool unity_util_convert_mac_from_string(const char* mac_str, uint8_t *mac_addr);