test_integration_transport.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright (c) 2021 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\x00\x01\x01"
  143. "doSomething argument - length: 20"
  144. "\r\n"
  145. "shellmatta->"
  146. "\xc8\xae\xc0\x56";
  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\x00\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. "\x0f\x90\xc0\xfd"
  170. "\x01\x01\x00\x7f\x00\x00\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. "\x00\xc9\x5d\x37";
  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\x00\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. "\x0f\x90\xc0\xfd"
  200. "\x01\x01\x00\x7f\x00\x00\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. "\x00\xc9\x5d\x37";
  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\x00\x01\x01"
  222. "doSomething argument - length: 20"
  223. "\r\n"
  224. "shellmatta->"
  225. "\xc8\xae\xc0\x56"
  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);
  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\x00\x01\x01"
  249. "doSomething argument - length: 20"
  250. "\r\n"
  251. "shellmatta->"
  252. "\xc8\xae\xc0\x56";
  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\x00\x01\x01"
  267. "\xb4\x0f\x12\xe9";
  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\x00\x02\x02"
  282. "\x06\x2b\x10\x90";
  283. CHECK(write_length == 12);
  284. REQUIRE(memcmp(write_data, dummyData, 12) == 0);
  285. }
  286. }
  287. }
  288. }
  289. }