test_utils.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "freertos/FreeRTOS.h"
  20. #include "freertos/task.h"
  21. #include "unity.h"
  22. #include "soc/soc_caps.h"
  23. /* include performance pass standards header file */
  24. #include "idf_performance.h"
  25. #include "idf_performance_target.h"
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /* For performance check with unity test on IDF */
  30. /* These macros should only be used with ESP-IDF.
  31. * To use performance check, we need to first define pass standard in idf_performance.h.
  32. */
  33. //macros call this to expand an argument instead of directly converting into str
  34. #define PERFORMANCE_STR(s) #s
  35. //macros call this to contact strings after expanding them
  36. #define PERFORMANCE_CON(a, b) _PERFORMANCE_CON(a, b)
  37. #define _PERFORMANCE_CON(a, b) a##b
  38. #if !CONFIG_UNITY_IGNORE_PERFORMANCE_TESTS
  39. #define _TEST_PERFORMANCE_ASSERT TEST_ASSERT
  40. #else
  41. #define _TEST_PERFORMANCE_ASSERT(ARG) printf("Ignoring performance test [%s]\n", PERFORMANCE_STR(ARG))
  42. #endif
  43. #define TEST_PERFORMANCE_LESS_THAN(name, value_fmt, value, ...) do { \
  44. IDF_LOG_PERFORMANCE(#name, value_fmt, value, ##__VA_ARGS__); \
  45. _TEST_PERFORMANCE_ASSERT(value < PERFORMANCE_CON(IDF_PERFORMANCE_MAX_, name)); \
  46. } while(0)
  47. #define TEST_PERFORMANCE_GREATER_THAN(name, value_fmt, value, ...) do { \
  48. IDF_LOG_PERFORMANCE(#name, value_fmt, value, ##__VA_ARGS__); \
  49. _TEST_PERFORMANCE_ASSERT(value > PERFORMANCE_CON(IDF_PERFORMANCE_MIN_, name)); \
  50. } while(0)
  51. /* Macros to be used when performance is calculated using the cache compensated timer
  52. will not assert if ccomp not supported */
  53. #if SOC_CCOMP_TIMER_SUPPORTED
  54. #define TEST_PERFORMANCE_CCOMP_GREATER_THAN(name, value_fmt, value, ...) \
  55. TEST_PERFORMANCE_GREATER_THAN(name, value_fmt, value, ##__VA_ARGS__)
  56. #define TEST_PERFORMANCE_CCOMP_LESS_THAN(name, value_fmt, value, ...) \
  57. TEST_PERFORMANCE_LESS_THAN(name, value_fmt, value, ##__VA_ARGS__)
  58. #else
  59. #define TEST_PERFORMANCE_CCOMP_GREATER_THAN(name, value_fmt, value, ...) \
  60. IDF_LOG_PERFORMANCE(#name, value_fmt, value, ##__VA_ARGS__)
  61. #define TEST_PERFORMANCE_CCOMP_LESS_THAN(name, value_fmt, value, ...) \
  62. IDF_LOG_PERFORMANCE(#name, value_fmt, value, ##__VA_ARGS__)
  63. #endif //SOC_CCOMP_TIMER_SUPPORTED
  64. /* @brief macro to print IDF performance
  65. * @param item : performance item name. a string pointer.
  66. * @param value_fmt: print format and unit of the value, for example: "%02fms", "%dKB"
  67. * @param value : the performance value.
  68. */
  69. #define IDF_LOG_PERFORMANCE(item, value_fmt, value, ...) \
  70. printf("[Performance][%s]: " value_fmt "\n", item, value, ##__VA_ARGS__)
  71. /* Some definitions applicable to Unity running in FreeRTOS */
  72. #define UNITY_FREERTOS_PRIORITY CONFIG_UNITY_FREERTOS_PRIORITY
  73. #define UNITY_FREERTOS_CPU CONFIG_UNITY_FREERTOS_CPU
  74. #define UNITY_FREERTOS_STACK_SIZE CONFIG_UNITY_FREERTOS_STACK_SIZE
  75. /* Return the 'flash_test' custom data partition (type 0x55)
  76. defined in the custom partition table.
  77. */
  78. const esp_partition_t *get_test_data_partition(void);
  79. /**
  80. * @brief Initialize reference clock
  81. *
  82. * Reference clock provides timestamps at constant 1 MHz frequency, even when
  83. * the APB frequency is changing.
  84. */
  85. void ref_clock_init(void);
  86. /**
  87. * @brief Deinitialize reference clock
  88. */
  89. void ref_clock_deinit(void);
  90. /**
  91. * @brief Get reference clock timestamp
  92. * @return number of microseconds since the reference clock was initialized
  93. */
  94. uint64_t ref_clock_get(void);
  95. /**
  96. * @brief Entry point of the test application
  97. *
  98. * Starts Unity test runner in a separate task and returns.
  99. */
  100. void test_main(void);
  101. /**
  102. * @brief Reset automatic leak checking which happens in unit tests.
  103. *
  104. * Updates recorded "before" free memory values to the free memory values
  105. * at time of calling. Resets leak checker if tracing is enabled in
  106. * config.
  107. *
  108. * This can be called if a test case does something which allocates
  109. * memory on first use, for example.
  110. *
  111. * @note Use with care as this can mask real memory leak problems.
  112. */
  113. void unity_reset_leak_checks(void);
  114. /**
  115. * @brief Call this function from a test case which requires TCP/IP or
  116. * LWIP functionality.
  117. *
  118. * @note This should be the first function the test case calls, as it will
  119. * allocate memory on first use (and also reset the test case leak checker).
  120. */
  121. void test_case_uses_tcpip(void);
  122. /**
  123. * @brief wait for signals with parameters.
  124. *
  125. * for multiple devices test cases, DUT might need to wait for other DUTs before continue testing.
  126. * As all DUTs are independent, need user (or test script) interaction to make test synchronized.
  127. *
  128. * Here we provide signal functions for this.
  129. * For example, we're testing GPIO, DUT1 has one pin connect to with DUT2.
  130. * DUT2 will output high level and then DUT1 will read input.
  131. * DUT1 should call `unity_wait_for_signal("output high level");` before it reads input.
  132. * DUT2 should call `unity_send_signal("output high level");` after it finished setting output high level.
  133. * According to the console logs:
  134. *
  135. * DUT1 console:
  136. *
  137. * ```
  138. * Waiting for signal: [output high level]!
  139. * Please press "Enter" key to once any board send this signal.
  140. * ```
  141. *
  142. * DUT2 console:
  143. *
  144. * ```
  145. * Send signal: [output high level]!
  146. * ```
  147. *
  148. * Then we press Enter key on DUT1's console, DUT1 starts to read input and then test success.
  149. *
  150. * Another example, we have 2 DUTs in multiple devices test, and DUT1 need to get DUT2's mac address to perform BT connection.
  151. * DUT1 should call `unity_wait_for_signal_param("dut2 mac address", mac, 19);` to wait for DUT2's mac address.
  152. * DUT2 should call `unity_send_signal_param("dut2 mac address", "10:20:30:40:50:60");` to send to DUT1 its mac address.
  153. * According to the console logs:
  154. *
  155. * DUT1 console:
  156. *
  157. * ```
  158. * Waiting for signal: [dut2 mac address]!
  159. * Please input parameter value from any board send this signal and press "Enter" key.
  160. * ```
  161. *
  162. * DUT2 console:
  163. *
  164. * ```
  165. * Send signal: [dut2 mac address][10:20:30:40:50:60]!
  166. * ```
  167. *
  168. * @param signal_name signal name which DUT expected to wait before proceed testing
  169. * @param parameter_buf buffer to receive parameter
  170. * @param buf_len length of parameter_buf.
  171. * Currently we have a limitation that it will write 1 extra byte at the end of string.
  172. * We need to use a buffer with 2 bytes longer than actual string length.
  173. */
  174. void unity_wait_for_signal_param(const char* signal_name, char *parameter_buf, uint8_t buf_len);
  175. /**
  176. * @brief wait for signals.
  177. *
  178. * @param signal_name signal name which DUT expected to wait before proceed testing
  179. */
  180. static inline void unity_wait_for_signal(const char* signal_name)
  181. {
  182. unity_wait_for_signal_param(signal_name, NULL, 0);
  183. }
  184. /**
  185. * @brief DUT send signal and pass parameter to other devices.
  186. *
  187. * @param signal_name signal name which DUT send once it finished preparing.
  188. * @param parameter a string to let remote device to receive.
  189. */
  190. void unity_send_signal_param(const char* signal_name, const char *parameter);
  191. /**
  192. * @brief DUT send signal with parameter.
  193. *
  194. * @param signal_name signal name which DUT send once it finished preparing.
  195. */
  196. static inline void unity_send_signal(const char* signal_name)
  197. {
  198. unity_send_signal_param(signal_name, NULL);
  199. }
  200. /**
  201. * @brief convert mac string to mac address
  202. *
  203. * @param mac_str MAC address string with format "xx:xx:xx:xx:xx:xx"
  204. * @param[out] mac_addr store converted MAC address
  205. */
  206. bool unity_util_convert_mac_from_string(const char* mac_str, uint8_t *mac_addr);
  207. /**
  208. * @brief Leak for components
  209. */
  210. typedef enum {
  211. COMP_LEAK_GENERAL = 0, /**< Leak by default */
  212. COMP_LEAK_LWIP, /**< Leak for LWIP */
  213. COMP_LEAK_NVS, /**< Leak for NVS */
  214. COMP_LEAK_ALL, /**< Use for getting the summary leak level */
  215. } esp_comp_leak_t;
  216. /**
  217. * @brief Type of leak
  218. */
  219. typedef enum {
  220. TYPE_LEAK_WARNING = 0, /**< Warning level of leak */
  221. TYPE_LEAK_CRITICAL, /**< Critical level of leak */
  222. TYPE_LEAK_MAX, /**< Max number of leak levels */
  223. } esp_type_leak_t;
  224. /**
  225. * @brief Set a leak level for the required type and component.
  226. *
  227. * @param[in] leak_level Level of leak
  228. * @param[in] type Type of leak
  229. * @param[in] component Name of component
  230. *
  231. * return ESP_OK: Successful.
  232. * ESP_ERR_INVALID_ARG: Invalid argument.
  233. */
  234. esp_err_t test_utils_set_leak_level(size_t leak_level, esp_type_leak_t type, esp_comp_leak_t component);
  235. /**
  236. * @brief Get a leak level for the required type and component.
  237. *
  238. * @param[in] type Type of leak.
  239. * @param[in] component Name of component. If COMP_LEAK_ALL, then the level will be summarized for all components.
  240. * return Leak level
  241. */
  242. size_t test_utils_get_leak_level(esp_type_leak_t type, esp_comp_leak_t component);
  243. typedef struct test_utils_exhaust_memory_record_s *test_utils_exhaust_memory_rec;
  244. /**
  245. * Limit the largest free block of memory with a particular capability set to
  246. * 'limit' bytes (meaning an allocation of 'limit' should succeed at least once,
  247. * but any allocation of more bytes will fail.)
  248. *
  249. * Returns a record pointer which needs to be passed back in to test_utils_free_exhausted_memory
  250. * before the test completes, to avoid a major memory leak.
  251. *
  252. * @param caps Capabilities of memory to exhause
  253. * @param limit The size to limit largest free block to
  254. * @return Record pointer to pass to test_utils_free_exhausted_memory() once done
  255. */
  256. test_utils_exhaust_memory_rec test_utils_exhaust_memory(uint32_t caps, size_t limit);
  257. /**
  258. * Call to free memory which was taken up by test_utils_exhaust_memory() call
  259. *
  260. * @param rec Result previously returned from test_utils_exhaust_memory()
  261. */
  262. void test_utils_free_exhausted_memory(test_utils_exhaust_memory_rec rec);
  263. /**
  264. * @brief Delete task ensuring dynamic memory (for stack, tcb etc.) gets freed up immediately
  265. *
  266. * @param[in] thandle Handle of task to be deleted (should not be NULL or self handle)
  267. */
  268. void test_utils_task_delete(TaskHandle_t thandle);
  269. #ifdef __cplusplus
  270. }
  271. #endif