utils_for_testing.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*********************************************************************
  2. * _ _ _
  3. * _ __ | |_ _ | | __ _ | |__ ___
  4. * | '__|| __|(_)| | / _` || '_ \ / __|
  5. * | | | |_ _ | || (_| || |_) |\__ \
  6. * |_| \__|(_)|_| \__,_||_.__/ |___/
  7. *
  8. * www.rt-labs.com
  9. * Copyright 2018 rt-labs AB, Sweden.
  10. *
  11. * This software is dual-licensed under GPLv3 and a commercial
  12. * license. See the file LICENSE.md distributed with this software for
  13. * full license information.
  14. ********************************************************************/
  15. #ifndef UTILS_FOR_TESTING_H
  16. #define UTILS_FOR_TESTING_H
  17. #include <gtest/gtest.h>
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #include "pf_includes.h"
  22. #include "mocks.h"
  23. #if defined(TEST_DEBUG)
  24. #define TEST_TRACE(...) printf (__VA_ARGS__)
  25. #else
  26. #define TEST_TRACE(...)
  27. #endif
  28. #define TEST_UDP_DELAY (500 * 1000) /* us */
  29. #define TEST_TICK_INTERVAL_US 1000 /* us */
  30. #define TEST_DATA_DELAY (2 * TEST_TICK_INTERVAL_US) /* us */
  31. #define TEST_TIMEOUT_DELAY (3 * 1000 * 1000) /* us */
  32. #define TEST_SCHEDULER_CALLBACK_DELAY (4 * TEST_TICK_INTERVAL_US) /* us */
  33. #define TEST_SCHEDULER_RUNTIME (10 * TEST_TICK_INTERVAL_US) /* us */
  34. #define TEST_API_IDENT 0
  35. #define TEST_API_NONEXIST_IDENT 2
  36. #define TEST_SLOT_IDENT 1
  37. #define TEST_SLOT_NONEXIST_IDENT 2
  38. #define TEST_SUBSLOT_IDENT 1
  39. #define TEST_SUBSLOT_NONEXIST_IDENT 2
  40. #define TEST_CHANNEL_IDENT 0
  41. #define TEST_CHANNEL_NONEXIST_IDENT 1
  42. #define TEST_CHANNEL_ILLEGAL 0x8001
  43. #define TEST_CHANNEL_DIRECTION PNET_DIAG_CH_PROP_DIR_OUTPUT
  44. #define TEST_CHANNEL_NUMBER_OF_BITS PNET_DIAG_CH_PROP_TYPE_8_BIT
  45. #define TEST_CHANNEL_ERRORTYPE 0x0001
  46. #define TEST_CHANNEL_ERRORTYPE_B 0x0002
  47. #define TEST_CHANNEL_ERRORTYPE_C 0x0003
  48. #define TEST_CHANNEL_ERRORTYPE_D 0x0004
  49. #define TEST_CHANNEL_ERRORTYPE_NONEXIST 0x0007
  50. #define TEST_DIAG_USI_CUSTOM 0x1234
  51. #define TEST_DIAG_USI_NONEXIST 0x1235
  52. #define TEST_DIAG_USI_INVALID 0x8888
  53. #define TEST_DIAG_EXT_ERRTYPE 0x0002
  54. #define TEST_DIAG_EXT_ERRTYPE_NONEXIST 0x0009
  55. #define TEST_DIAG_EXT_ADDVALUE 0x00030004
  56. #define TEST_DIAG_EXT_ADDVALUE_B 0x00030005
  57. #define TEST_DIAG_QUALIFIER 0x00010000
  58. #define TEST_DIAG_QUALIFIER_NOTSET 0x00000000
  59. #define TEST_INTERFACE_NAME "en1"
  60. #define TEST_MAX_NUMBER_AVAILABLE_MODULE_TYPES 20
  61. #define TEST_MAX_NUMBER_AVAILABLE_SUBMODULE_TYPES 20
  62. /*
  63. * I/O Modules. These modules and their sub-modules must be plugged by the
  64. * application after the call to pnet_init.
  65. *
  66. * Assume that all modules only have a single submodule, with same number.
  67. */
  68. #define TEST_MOD_8_0_IDENT 0x00000030 /* 8 bit input */
  69. #define TEST_MOD_0_8_IDENT 0x00000031 /* 8 bit output */
  70. #define TEST_MOD_8_8_IDENT 0x00000032 /* 8 bit input, 8 bit output */
  71. #define TEST_SUBMOD_CUSTOM_IDENT 0x00000001
  72. #define TEST_DATASIZE_INPUT 1 /* bytes, for digital inputs data */
  73. #define TEST_DATASIZE_OUTPUT 1 /* bytes, for digital outputs data */
  74. typedef struct cfg_submodules
  75. {
  76. uint32_t module_ident_number;
  77. uint32_t submodule_ident_number;
  78. pnet_submodule_dir_t direction;
  79. uint16_t input_length;
  80. uint16_t output_length;
  81. } cfg_submodules_t;
  82. typedef struct call_counters_obj
  83. {
  84. uint16_t state_calls;
  85. uint16_t connect_calls;
  86. uint16_t release_calls;
  87. uint16_t dcontrol_calls;
  88. uint16_t ccontrol_calls;
  89. uint16_t read_calls;
  90. uint16_t write_calls;
  91. uint16_t led_on_calls;
  92. uint16_t led_off_calls;
  93. uint16_t scheduler_callback_a_calls;
  94. uint16_t scheduler_callback_b_calls;
  95. } call_counters_t;
  96. typedef struct app_data_for_testing_obj
  97. {
  98. uint32_t tick_ctr;
  99. os_timer_t * periodic_timer;
  100. pnet_event_values_t cmdev_state;
  101. uint16_t data_cycle_ctr;
  102. uint32_t counter_data;
  103. os_event_t * main_events;
  104. uint32_t main_arep;
  105. bool alarm_allowed;
  106. uint32_t app_param_1;
  107. uint32_t app_param_2;
  108. uint8_t inputdata[TEST_DATASIZE_INPUT];
  109. uint8_t custom_input_slots[PNET_MAX_SLOTS];
  110. uint8_t custom_output_slots[PNET_MAX_SLOTS];
  111. uint32_t available_module_types[TEST_MAX_NUMBER_AVAILABLE_MODULE_TYPES];
  112. cfg_submodules_t
  113. available_submodule_types[TEST_MAX_NUMBER_AVAILABLE_SUBMODULE_TYPES];
  114. bool init_done;
  115. uint16_t read_fails;
  116. call_counters_t call_counters;
  117. pf_scheduler_handle_t scheduler_handle_a;
  118. pf_scheduler_handle_t scheduler_handle_b;
  119. } app_data_for_testing_t;
  120. /******************** Callbacks defined by p-net *****************************/
  121. int my_connect_ind (
  122. pnet_t * net,
  123. void * arg,
  124. uint32_t arep,
  125. pnet_result_t * p_result);
  126. int my_release_ind (
  127. pnet_t * net,
  128. void * arg,
  129. uint32_t arep,
  130. pnet_result_t * p_result);
  131. int my_dcontrol_ind (
  132. pnet_t * net,
  133. void * arg,
  134. uint32_t arep,
  135. pnet_control_command_t control_command,
  136. pnet_result_t * p_result);
  137. int my_ccontrol_cnf (
  138. pnet_t * net,
  139. void * arg,
  140. uint32_t arep,
  141. pnet_result_t * p_result);
  142. int my_read_ind (
  143. pnet_t * net,
  144. void * arg,
  145. uint32_t arep,
  146. uint16_t api,
  147. uint16_t slot,
  148. uint16_t subslot,
  149. uint16_t idx,
  150. uint16_t sequence_number,
  151. uint8_t ** pp_read_data, /* Out: A pointer to the data */
  152. uint16_t * p_read_length, /* Out: Size of data */
  153. pnet_result_t * p_result); /* Error status if returning != 0 */
  154. int my_write_ind (
  155. pnet_t * net,
  156. void * arg,
  157. uint32_t arep,
  158. uint16_t api,
  159. uint16_t slot,
  160. uint16_t subslot,
  161. uint16_t idx,
  162. uint16_t sequence_number,
  163. uint16_t write_length,
  164. uint8_t * p_write_data,
  165. pnet_result_t * p_result);
  166. int my_new_data_status_ind (
  167. pnet_t * net,
  168. void * arg,
  169. uint32_t arep,
  170. uint32_t crep,
  171. uint8_t changes,
  172. uint8_t data_status);
  173. int my_alarm_ind (
  174. pnet_t * net,
  175. void * arg,
  176. uint32_t arep,
  177. const pnet_alarm_argument_t * p_alarm_arg,
  178. uint16_t data_len,
  179. uint16_t data_usi,
  180. uint8_t * p_data);
  181. int my_alarm_cnf (
  182. pnet_t * net,
  183. void * arg,
  184. uint32_t arep,
  185. pnet_pnio_status_t * p_pnio_status);
  186. int my_state_ind (
  187. pnet_t * net,
  188. void * arg,
  189. uint32_t arep,
  190. pnet_event_values_t state);
  191. int my_exp_module_ind (
  192. pnet_t * net,
  193. void * arg,
  194. uint16_t api,
  195. uint16_t slot,
  196. uint32_t module_ident);
  197. int my_exp_submodule_ind (
  198. pnet_t * net,
  199. void * arg,
  200. uint16_t api,
  201. uint16_t slot,
  202. uint16_t subslot,
  203. uint32_t module_ident,
  204. uint32_t submodule_ident);
  205. #ifdef __cplusplus
  206. }
  207. #endif
  208. /*********************** Base classes for tests *****************************/
  209. class PnetUnitTest : public ::testing::Test
  210. {
  211. protected:
  212. virtual void SetUp(){};
  213. };
  214. class PnetIntegrationTestBase : public ::testing::Test
  215. {
  216. protected:
  217. pnet_cfg_t pnet_default_cfg;
  218. app_data_for_testing_t appdata;
  219. pnet_t the_net;
  220. pnet_t * net = &the_net;
  221. /** Initialize appdata, including clearing available modules etc. */
  222. virtual void appdata_init();
  223. virtual void callcounter_reset();
  224. virtual void available_modules_and_submodules_init();
  225. virtual void cfg_init();
  226. /** Simulate sleep
  227. *
  228. * This function updates the app state and calls the periodic
  229. * maintenance functions while simulating sleeping.
  230. *
  231. * @param us In: time to sleep (in us)
  232. */
  233. virtual void run_stack (int us);
  234. /** Send raw Ethernet test data
  235. *
  236. * @param data_packet In: Data packet
  237. * @param len In: Length of data packet
  238. */
  239. virtual void send_data (uint8_t * data_packet, uint16_t len);
  240. };
  241. class PnetIntegrationTest : public PnetIntegrationTestBase
  242. {
  243. protected:
  244. virtual void SetUp() override
  245. {
  246. mock_init();
  247. cfg_init();
  248. appdata_init();
  249. available_modules_and_submodules_init();
  250. callcounter_reset();
  251. pnet_init_only (net, &pnet_default_cfg);
  252. pf_pdport_update_eth_status (net);
  253. mock_clear(); /* lldp sends a frame at init */
  254. };
  255. };
  256. /*************************** Assertion helpers ******************************/
  257. template <typename T, size_t size>
  258. ::testing::AssertionResult ArraysMatch (
  259. const T (&expected)[size],
  260. const T (&actual)[size])
  261. {
  262. for (size_t i (0); i < size; ++i)
  263. {
  264. if (expected[i] != actual[i])
  265. {
  266. return ::testing::AssertionFailure()
  267. << std::hex << std::showbase << "array[" << i << "] ("
  268. << static_cast<int> (actual[i]) << ") != expected[" << i
  269. << "] (" << static_cast<int> (expected[i]) << ")";
  270. }
  271. }
  272. return ::testing::AssertionSuccess();
  273. }
  274. #endif /* UTILS_FOR_TESTING_H */