test_integration_transport.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * Copyright (c) 2021 - 2024 Stefan Strobel <stefan.strobel@shimatta.net>
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. /**
  9. * @file test_integration_transport.cpp
  10. * @brief integration test implementation for the transport layer
  11. * @author Stefan Strobel <stefan.strobel@shimatta.net>
  12. */
  13. #include "test/framework/catch.hpp"
  14. extern "C" {
  15. #include "shellmatta.h"
  16. }
  17. #include <string.h>
  18. static uint32_t write_callCnt = 0u;
  19. static char write_data[1024];
  20. static uint32_t write_length;
  21. static const char *doSomethingArguments;
  22. static uint32_t doSomethingLength;
  23. static char *doSomethingStdin;
  24. static uint32_t doSomethingStdinLength;
  25. static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  26. {
  27. write_callCnt ++;
  28. while((length > 0) && (write_length < sizeof(write_data)))
  29. {
  30. write_data[write_length] = *data;
  31. data ++;
  32. length --;
  33. write_length ++;
  34. }
  35. return SHELLMATTA_OK;
  36. }
  37. static shellmatta_retCode_t doSomething(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  38. {
  39. doSomethingArguments = arguments;
  40. doSomethingLength = length;
  41. shellmatta_read(handle, &doSomethingStdin, &doSomethingStdinLength);
  42. shellmatta_printf(handle, "%s - length: %u", arguments, length);
  43. return SHELLMATTA_OK;
  44. }
  45. shellmatta_cmd_t doSomethingCmd =
  46. {
  47. (char*)"doSomething",
  48. (char*)"do",
  49. (char*)"Function does something",
  50. (char*)"use me, please",
  51. doSomething,
  52. NULL
  53. };
  54. static shellmatta_retCode_t largePayload(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  55. {
  56. uint32_t i;
  57. for (i = 0u; i < 8; i ++)
  58. {
  59. shellmatta_printf(handle, "This is my very very very very large payload\r\n");
  60. }
  61. (void)arguments;
  62. (void)length;
  63. return SHELLMATTA_OK;
  64. }
  65. shellmatta_cmd_t largePayloadCmd =
  66. {
  67. (char*)"largePayload",
  68. (char*)"lp",
  69. (char*)"Function returns large payload",
  70. (char*)"i have much to say",
  71. largePayload,
  72. NULL
  73. };
  74. static shellmatta_retCode_t largePayloadAtOnce(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  75. {
  76. shellmatta_write(handle,
  77. (char*)"This is my very very very very large payload\r\n"
  78. "This is my very very very very large payload\r\n"
  79. "This is my very very very very large payload\r\n"
  80. "This is my very very very very large payload\r\n"
  81. "This is my very very very very large payload\r\n"
  82. "This is my very very very very large payload\r\n"
  83. "This is my very very very very large payload\r\n"
  84. "This is my very very very very large payload\r\n",
  85. 46u*8u);
  86. (void)arguments;
  87. (void)length;
  88. return SHELLMATTA_OK;
  89. }
  90. shellmatta_cmd_t largePayloadAtOnceCmd =
  91. {
  92. (char*)"largePayload",
  93. (char*)"lp",
  94. (char*)"Function returns large payload",
  95. (char*)"i have much to say",
  96. largePayloadAtOnce,
  97. NULL
  98. };
  99. SCENARIO("Integration test of Transport layer", "[integration, transport]")
  100. {
  101. GIVEN("Shellmatta up and running with one command")
  102. {
  103. shellmatta_instance_t inst;
  104. shellmatta_handle_t handle;
  105. char buffer[1024];
  106. char historyBuffer[1024];
  107. shellmatta_doInit( &inst,
  108. &handle,
  109. buffer,
  110. sizeof(buffer),
  111. historyBuffer,
  112. sizeof(historyBuffer),
  113. "shellmatta->",
  114. NULL,
  115. writeFct);
  116. shellmatta_addCmd(handle, &doSomethingCmd);
  117. write_callCnt = 0u;
  118. memset(write_data, 0, sizeof(write_data));
  119. write_length = 0u;
  120. WHEN("Invalid CRC is passed")
  121. {
  122. /* check with invalid payload */
  123. shellmatta_processData(handle, (char*)"\x01\x01\x00\x16\x00\x00\x00\x00"
  124. "doSomething argument\r\n"
  125. "\x00\x00\x00\x00", 34u);
  126. THEN("Shellmatta responds with CRC error")
  127. {
  128. char *dummyData = (char*)"crc error\r\n\r\nshellmatta->";
  129. CHECK(write_length == strlen(dummyData));
  130. REQUIRE(strcmp(dummyData, write_data) == 0);
  131. }
  132. }
  133. WHEN("Valid CRC is passed")
  134. {
  135. /* check with valid payload - disable echo to reduce cluttering */
  136. shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
  137. shellmatta_processData(handle, (char*)"\x01\x01\x00\x16\x00\x00\x00\x00"
  138. "doSomething argument\r\n"
  139. "\x7b\x49\xfa\x72", 34u);
  140. THEN("The shellmatta responds to the command")
  141. {
  142. char *dummyData = (char*)"\x01\x01\x00\x2f\x00\x01\x01\x01"
  143. "doSomething argument - length: 20"
  144. "\r\n"
  145. "shellmatta->"
  146. "\x4c\x9f\xd9\xa7";
  147. CHECK(write_length == 59u);
  148. REQUIRE(memcmp(write_data, dummyData, 59) == 0);
  149. }
  150. }
  151. WHEN("Large payload is written by shellmatta in steps")
  152. {
  153. /* add large payload command */
  154. shellmatta_addCmd(handle, &largePayloadCmd);
  155. /* check with valid payload - disable echo to reduce cluttering */
  156. shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
  157. shellmatta_processData(handle, (char*)"\x01\x01\x00\x0e\x00\x00\x00\x00"
  158. "largePayload\r\n"
  159. "\xad\x33\x31\xcc", 26u);
  160. THEN("The shellmatta responds multiple telegrams")
  161. {
  162. char *dummyData = (char*)"\x01\x01\x00\xff\x00\x01\x01\x01"
  163. "This is my very very very very large payload\r\n"
  164. "This is my very very very very large payload\r\n"
  165. "This is my very very very very large payload\r\n"
  166. "This is my very very very very large payload\r\n"
  167. "This is my very very very very large payload\r\n"
  168. "This is my very very very"
  169. "\x00\x9c\x41\xe1"
  170. "\x01\x01\x00\x7f\x00\x01\x01\x02"
  171. " very large payload\r\n"
  172. "This is my very very very very large payload\r\n"
  173. "This is my very very very very large payload\r\n"
  174. "\r\n"
  175. "shellmatta->"
  176. "\x39\x75\x0d\x6b";
  177. CHECK(write_length == 406u);
  178. REQUIRE(memcmp(write_data, dummyData, 406u) == 0);
  179. }
  180. }
  181. WHEN("Large payload is written by shellmatta at once")
  182. {
  183. /* add large payload command */
  184. shellmatta_addCmd(handle, &largePayloadAtOnceCmd);
  185. /* check with valid payload - disable echo to reduce cluttering */
  186. shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
  187. shellmatta_processData(handle, (char*)"\x01\x01\x00\x0e\x00\x00\x00\x00"
  188. "largePayload\r\n"
  189. "\xad\x33\x31\xcc", 26u);
  190. THEN("The shellmatta responds multiple telegrams")
  191. {
  192. char *dummyData = (char*)"\x01\x01\x00\xff\x00\x01\x01\x01"
  193. "This is my very very very very large payload\r\n"
  194. "This is my very very very very large payload\r\n"
  195. "This is my very very very very large payload\r\n"
  196. "This is my very very very very large payload\r\n"
  197. "This is my very very very very large payload\r\n"
  198. "This is my very very very"
  199. "\x00\x9c\x41\xe1"
  200. "\x01\x01\x00\x7f\x00\x01\x01\x02"
  201. " very large payload\r\n"
  202. "This is my very very very very large payload\r\n"
  203. "This is my very very very very large payload\r\n"
  204. "\r\n"
  205. "shellmatta->"
  206. "\x39\x75\x0d\x6b";
  207. CHECK(write_length == 406u);
  208. REQUIRE(memcmp(write_data, dummyData, 406u) == 0);
  209. }
  210. }
  211. WHEN("manual mode is used after transport layer mode")
  212. {
  213. /* check with valid payload - disable echo to reduce cluttering */
  214. shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
  215. shellmatta_processData(handle, (char*)"\x01\x01\x00\x16\x00\x00\x00\x00"
  216. "doSomething argument\r\n"
  217. "\x7b\x49\xfa\x72", 34u);
  218. shellmatta_processData(handle, (char*)"doSomething argument\r\n", 22u);
  219. THEN("The shellmatta responds to the command")
  220. {
  221. char *dummyData = (char*)"\x01\x01\x00\x2f\x00\x01\x01\x01"
  222. "doSomething argument - length: 20"
  223. "\r\n"
  224. "shellmatta->"
  225. "\x4c\x9f\xd9\xa7"
  226. "doSomething argument - length: 20\r\n"
  227. "shellmatta->";
  228. CHECK(write_length == 106);
  229. REQUIRE(memcmp(write_data, dummyData, 106) == 0);
  230. }
  231. }
  232. WHEN("disabling auto flush")
  233. {
  234. /* check with valid payload - disable echo to reduce cluttering */
  235. shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
  236. shellmatta_transport_configure(handle, false, true, NULL, NULL);
  237. shellmatta_processData(handle, (char*)"\x01\x01\x00\x16\x00\x00\x00\x00"
  238. "doSomething argument\r\n"
  239. "\x7b\x49\xfa\x72", 34u);
  240. THEN("The Shellmatta does not respond")
  241. {
  242. CHECK(write_length == 0u);
  243. AND_WHEN("The flush method is called")
  244. {
  245. shellmatta_transport_flush(handle);
  246. THEN("The shellmatta returns the data")
  247. {
  248. char *dummyData = (char*)"\x01\x01\x00\x2f\x00\x01\x01\x01"
  249. "doSomething argument - length: 20"
  250. "\r\n"
  251. "shellmatta->"
  252. "\x4c\x9f\xd9\xa7";
  253. CHECK(write_length == 59u);
  254. REQUIRE(memcmp(write_data, dummyData, 59) == 0);
  255. }
  256. }
  257. }
  258. }
  259. WHEN("Sequence counter is requested")
  260. {
  261. /* request sequence counter */
  262. shellmatta_processData(handle, (char*)"\x01\x01\x01\x00\x00\x00\x00\x00"
  263. "\xc4\xa3\x07\xe6", 12u);
  264. THEN("The valid Sequence counter is returned")
  265. {
  266. char *dummyData = (char*)"\x01\x01\x81\x00\x00\x01\x01\x01"
  267. "\xb5\xcd\x78\xde";
  268. CHECK(write_length == 12);
  269. REQUIRE(memcmp(write_data, dummyData, 12) == 0);
  270. }
  271. AND_WHEN("Sequence counter is requested again")
  272. {
  273. /* request sequence counter again */
  274. write_callCnt = 0u;
  275. memset(write_data, 0, sizeof(write_data));
  276. write_length = 0u;
  277. shellmatta_processData(handle, (char*)"\x01\x01\x01\x00\x00\x00\x00\x00"
  278. "\xc4\xa3\x07\xe6", 12u);
  279. THEN("The next Sequence counter is returned")
  280. {
  281. char *dummyData = (char*)"\x01\x01\x81\x00\x00\x01\x02\x02"
  282. "\x07\xe9\x7a\xa7";
  283. CHECK(write_length == 12);
  284. REQUIRE(memcmp(write_data, dummyData, 12) == 0);
  285. }
  286. }
  287. }
  288. }
  289. }
  290. SCENARIO("Integration test of Transport layer with mandatory transport layer", "[integration, transport]")
  291. {
  292. GIVEN("Shellmatta up and running with one command")
  293. {
  294. shellmatta_instance_t inst;
  295. shellmatta_handle_t handle;
  296. shellmatta_retCode_t ret;
  297. char buffer[1024];
  298. char historyBuffer[1024];
  299. shellmatta_doInit( &inst,
  300. &handle,
  301. buffer,
  302. sizeof(buffer),
  303. historyBuffer,
  304. sizeof(historyBuffer),
  305. "shellmatta->",
  306. NULL,
  307. writeFct);
  308. shellmatta_addCmd(handle, &doSomethingCmd);
  309. write_callCnt = 0u;
  310. memset(write_data, 0, sizeof(write_data));
  311. write_length = 0u;
  312. WHEN("The tansport layer is set to mandatory") {
  313. ret = shellmatta_transport_configure(handle, true, false, NULL, NULL);
  314. CHECK(ret == SHELLMATTA_OK);
  315. AND_WHEN("manual mode is used after transport layer mode")
  316. {
  317. /* check with valid payload - disable echo to reduce cluttering */
  318. shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
  319. shellmatta_processData(handle, (char*)"\x01\x01\x00\x16\x00\x00\x00\x00"
  320. "doSomething argument\r\n"
  321. "\x7b\x49\xfa\x72", 34u);
  322. shellmatta_processData(handle, (char*)"doSomething argument\r\n", 22u);
  323. THEN("The shellmatta responds only to the first command - and waits for a new telegram to start")
  324. {
  325. char *dummyData = (char*)"\x01\x01\x00\x2f\x00\x01\x01\x01"
  326. "doSomething argument - length: 20"
  327. "\r\n"
  328. "shellmatta->"
  329. "\x4c\x9f\xd9\xa7";
  330. CHECK(write_length == 59);
  331. CHECK(memcmp(write_data, dummyData, write_length) == 0);
  332. REQUIRE(inst.transportLayer.state == SHELLMATTA_TRANSPORT_STATE_WAIT);
  333. }
  334. }
  335. }
  336. }
  337. }
  338. SCENARIO("Integration test of Transport layer - reset transport layer", "[integration, transport]")
  339. {
  340. GIVEN("Shellmatta up and running with one command")
  341. {
  342. shellmatta_instance_t inst;
  343. shellmatta_handle_t handle;
  344. shellmatta_retCode_t ret;
  345. char buffer[1024];
  346. char historyBuffer[1024];
  347. shellmatta_doInit( &inst,
  348. &handle,
  349. buffer,
  350. sizeof(buffer),
  351. historyBuffer,
  352. sizeof(historyBuffer),
  353. "shellmatta->",
  354. NULL,
  355. writeFct);
  356. shellmatta_addCmd(handle, &doSomethingCmd);
  357. write_callCnt = 0u;
  358. memset(write_data, 0, sizeof(write_data));
  359. write_length = 0u;
  360. WHEN("The tansport layer is set to mandatory") {
  361. ret = shellmatta_transport_configure(handle, true, false, NULL, NULL);
  362. CHECK(ret == SHELLMATTA_OK);
  363. AND_WHEN("A partial telegram is passed")
  364. {
  365. /* check with valid payload - disable echo to reduce cluttering */
  366. shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
  367. shellmatta_processData(handle, (char*)"\x01\x01\x00\x16\x00\x00\x00\x00"
  368. "doSomething argument", 28u);
  369. THEN("The transport layer is in state SHELLMATTA_TRANSPORT_STATE_GET_PAYLOAD")
  370. {
  371. CHECK(write_length == 0);
  372. CHECK_THAT(inst.transportLayer.inPacket.payload, Catch::Matchers::Equals("doSomething argument"));
  373. REQUIRE(inst.transportLayer.state == SHELLMATTA_TRANSPORT_STATE_GET_PAYLOAD);
  374. AND_WHEN("The transport layer is resetted")
  375. {
  376. ret = shellmatta_transport_reset(handle);
  377. CHECK(ret == SHELLMATTA_OK);
  378. THEN("The transport layer is in state wait again and no payload is set") {
  379. CHECK(write_length == 0);
  380. CHECK(inst.transportLayer.inPacket.payload[0] == '\0');
  381. REQUIRE(inst.transportLayer.state == SHELLMATTA_TRANSPORT_STATE_WAIT);
  382. }
  383. }
  384. }
  385. }
  386. }
  387. }
  388. }
  389. SCENARIO("Integration test of Transport layer - addressing", "[integration, transport]")
  390. {
  391. GIVEN("Shellmatta up and running with one command - transport layer with UUID")
  392. {
  393. shellmatta_instance_t inst;
  394. shellmatta_handle_t handle;
  395. shellmatta_retCode_t ret;
  396. char buffer[1024];
  397. char historyBuffer[1024];
  398. shellmatta_doInit( &inst,
  399. &handle,
  400. buffer,
  401. sizeof(buffer),
  402. historyBuffer,
  403. sizeof(historyBuffer),
  404. "shellmatta->",
  405. NULL,
  406. writeFct);
  407. shellmatta_addCmd(handle, &doSomethingCmd);
  408. ret = shellmatta_transport_configure(handle, true, false, (uint8_t *)"123456789012345", NULL);
  409. CHECK(ret == SHELLMATTA_OK);
  410. write_callCnt = 0u;
  411. memset(write_data, 0, sizeof(write_data));
  412. write_length = 0u;
  413. WHEN("The shellmatta is searched using an invalid range") {
  414. ret = shellmatta_transport_configure(handle, true, false, NULL, NULL);
  415. /* check with valid payload - disable echo to reduce cluttering */
  416. shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
  417. shellmatta_processData(handle, (char*)"\x01\x01\x03\x20\x00\x00\x00\x00"
  418. "234567890123456\0"
  419. "345678901234567\0"
  420. "\x93\x0c\xc3\x04", 44u);
  421. THEN("The shellmatta does not respond")
  422. {
  423. CHECK(write_length == 0);
  424. CHECK(inst.transportLayer.sequenceH2S == 1);
  425. REQUIRE(inst.transportLayer.state == SHELLMATTA_TRANSPORT_STATE_WAIT);
  426. }
  427. }
  428. WHEN("The shellmatta is searched using a valid range") {
  429. ret = shellmatta_transport_configure(handle, true, false, NULL, NULL);
  430. /* check with valid payload - disable echo to reduce cluttering */
  431. shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
  432. shellmatta_processData(handle, (char*)"\x01\x01\x03\x20\x00\x00\x00\x00"
  433. "123456789012344\0"
  434. "345678901234567\0"
  435. "\x9b\x69\x59\x5f", 44u);
  436. THEN("The shellmatta does not respond")
  437. {
  438. CHECK(write_length == 28);
  439. CHECK(inst.transportLayer.sequenceH2S == 1);
  440. REQUIRE(inst.transportLayer.state == SHELLMATTA_TRANSPORT_STATE_WAIT);
  441. }
  442. }
  443. }
  444. }