test_integration.cpp 17 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.cpp
  10. * @brief integration test implementation for some general functions
  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. using Catch::Matchers::Equals;
  19. static uint32_t write_callCnt = 0u;
  20. static char write_data[1024];
  21. static uint32_t write_length;
  22. static const char *doSomethingArguments;
  23. static uint32_t doSomethingLength;
  24. static char *doSomethingStdin;
  25. static uint32_t doSomethingStdinLength;
  26. static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  27. {
  28. write_callCnt ++;
  29. while((length > 0) && (write_length < sizeof(write_data)))
  30. {
  31. write_data[write_length] = *data;
  32. data ++;
  33. length --;
  34. write_length ++;
  35. }
  36. return SHELLMATTA_OK;
  37. }
  38. static shellmatta_retCode_t doSomething(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  39. {
  40. doSomethingArguments = arguments;
  41. doSomethingLength = length;
  42. shellmatta_read(handle, &doSomethingStdin, &doSomethingStdinLength);
  43. shellmatta_printf(handle, "%s - length: %u", arguments, length);
  44. return SHELLMATTA_OK;
  45. }
  46. shellmatta_cmd_t doSomethingCmd = {(char*)"doSomething", (char*)"do", (char*)"Function does something", (char*)"use me, please", doSomething, NULL};
  47. static shellmatta_retCode_t empty(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  48. {
  49. shellmatta_printf(handle, "empty - %s - length: %u", arguments, length);
  50. return SHELLMATTA_OK;
  51. }
  52. shellmatta_cmd_t emptyCmd = {(char*)"empty", NULL, NULL, NULL, empty, NULL};
  53. TEST_CASE( "shellmatta empty function" ) {
  54. shellmatta_instance_t inst;
  55. shellmatta_handle_t handle;
  56. char buffer[1024];
  57. char historyBuffer[1024];
  58. shellmatta_doInit( &inst,
  59. &handle,
  60. buffer,
  61. sizeof(buffer),
  62. historyBuffer,
  63. sizeof(historyBuffer),
  64. "shellmatta->",
  65. NULL,
  66. writeFct);
  67. write_callCnt = 0u;
  68. memset(write_data, 0, sizeof(write_data));
  69. write_length = 0u;
  70. shellmatta_processData(handle, (char*)"\r", 1);
  71. CHECK( write_length == 14u);
  72. REQUIRE_THAT( write_data, Equals("\r\nshellmatta->") );
  73. }
  74. TEST_CASE( "shellmatta heredoc test" ) {
  75. shellmatta_instance_t inst;
  76. shellmatta_handle_t handle;
  77. char buffer[1024];
  78. char historyBuffer[1024];
  79. shellmatta_doInit( &inst,
  80. &handle,
  81. buffer,
  82. sizeof(buffer),
  83. historyBuffer,
  84. sizeof(historyBuffer),
  85. "shellmatta->",
  86. NULL,
  87. writeFct);
  88. shellmatta_addCmd(handle, &doSomethingCmd);
  89. doSomethingArguments = NULL;
  90. doSomethingLength = 0u;
  91. shellmatta_processData(handle, (char*)"do this << EOF\r\n"
  92. "asdf\r\n"
  93. "1234\r\n"
  94. "EOF\r\n"
  95. , 33);
  96. CHECK( doSomethingStdinLength == 10u);
  97. CHECK_THAT( doSomethingStdin, Equals("asdf\r\n1234") );
  98. CHECK( doSomethingLength == 8u);
  99. REQUIRE_THAT( doSomethingArguments, Equals("do this ") );
  100. }
  101. TEST_CASE( "shellmatta heredoc test empty" ) {
  102. shellmatta_instance_t inst;
  103. shellmatta_handle_t handle;
  104. char buffer[1024];
  105. char historyBuffer[1024];
  106. shellmatta_doInit( &inst,
  107. &handle,
  108. buffer,
  109. sizeof(buffer),
  110. historyBuffer,
  111. sizeof(historyBuffer),
  112. "shellmatta->",
  113. NULL,
  114. writeFct);
  115. shellmatta_addCmd(handle, &doSomethingCmd);
  116. doSomethingArguments = NULL;
  117. doSomethingLength = 0u;
  118. shellmatta_processData(handle, (char*)"do this << EOF\r\n"
  119. "EOF\r\n"
  120. , 21);
  121. CHECK( doSomethingStdinLength == 0u);
  122. CHECK( NULL == doSomethingStdin );
  123. CHECK( doSomethingLength == 8u);
  124. REQUIRE_THAT( doSomethingArguments, Equals("do this ") );
  125. }
  126. TEST_CASE( "shellmatta remove function" ) {
  127. shellmatta_instance_t inst;
  128. shellmatta_handle_t handle;
  129. char buffer[1024];
  130. char historyBuffer[1024];
  131. char *dummyData = (char*)"?\r\n"
  132. "doSomething do Function does something\r\n"
  133. "help ? help [command] - print help or usage information\r\n"
  134. "\r\nshellmatta->";
  135. shellmatta_doInit( &inst,
  136. &handle,
  137. buffer,
  138. sizeof(buffer),
  139. historyBuffer,
  140. sizeof(historyBuffer),
  141. "shellmatta->",
  142. NULL,
  143. writeFct);
  144. shellmatta_addCmd(handle, &doSomethingCmd);
  145. write_callCnt = 0u;
  146. memset(write_data, 0, sizeof(write_data));
  147. write_length = 0u;
  148. shellmatta_processData(handle, (char*)"?\r", 2);
  149. CHECK( write_length == strlen(dummyData));
  150. CHECK_THAT( write_data, Equals(dummyData) );
  151. write_callCnt = 0u;
  152. memset(write_data, 0, sizeof(write_data));
  153. write_length = 0u;
  154. dummyData = (char*)"? 564 321 56 465 46\r\n"
  155. "doSomething do Function does something\r\n"
  156. "help ? help [command] - print help or usage information\r\n"
  157. "\r\nshellmatta->";
  158. shellmatta_processData(handle, (char*)"? 564 321 56 465 46\r", 20);
  159. CHECK(write_length == strlen(dummyData));
  160. CHECK_THAT(write_data, Catch::Matchers::Equals(dummyData));
  161. write_callCnt = 0u;
  162. memset(write_data, 0, sizeof(write_data));
  163. write_length = 0u;
  164. shellmatta_removeCmd(handle, &doSomethingCmd);
  165. shellmatta_processData(handle, (char*)"? 564 321 56 465 46\r", 20);
  166. dummyData = (char*)"? 564 321 56 465 46\r\n"
  167. "help ? help [command] - print help or usage information\r\n"
  168. "\r\nshellmatta->";
  169. CHECK(write_length == strlen(dummyData));
  170. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  171. }
  172. TEST_CASE( "shellmatta reset no prompt" ) {
  173. shellmatta_instance_t inst;
  174. shellmatta_handle_t handle;
  175. char buffer[1024];
  176. char historyBuffer[1024];
  177. char *dummyData = (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh glkd?\r\n"
  178. "doSomething do Function does something\r\n"
  179. "help ? help [command] - print help or usage information\r\n"
  180. "\r\nshellmatta->";
  181. shellmatta_doInit( &inst,
  182. &handle,
  183. buffer,
  184. sizeof(buffer),
  185. historyBuffer,
  186. sizeof(historyBuffer),
  187. "shellmatta->",
  188. NULL,
  189. writeFct);
  190. shellmatta_addCmd(handle, &doSomethingCmd);
  191. write_callCnt = 0u;
  192. memset(write_data, 0, sizeof(write_data));
  193. write_length = 0u;
  194. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh glkd", 35);
  195. shellmatta_resetShell(handle, false);
  196. shellmatta_processData(handle, (char*)"?\r", 2);
  197. CHECK(write_length == strlen(dummyData));
  198. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  199. }
  200. TEST_CASE( "shellmatta reset with prompt" ) {
  201. shellmatta_instance_t inst;
  202. shellmatta_handle_t handle;
  203. char buffer[1024];
  204. char historyBuffer[1024];
  205. char *dummyData = (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh glkd"
  206. "\r\nshellmatta->?\r\n"
  207. "doSomething do Function does something\r\n"
  208. "help ? help [command] - print help or usage information\r\n"
  209. "\r\nshellmatta->";
  210. shellmatta_doInit( &inst,
  211. &handle,
  212. buffer,
  213. sizeof(buffer),
  214. historyBuffer,
  215. sizeof(historyBuffer),
  216. "shellmatta->",
  217. NULL,
  218. writeFct);
  219. shellmatta_addCmd(handle, &doSomethingCmd);
  220. write_callCnt = 0u;
  221. memset(write_data, 0, sizeof(write_data));
  222. write_length = 0u;
  223. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh glkd", 35);
  224. shellmatta_resetShell(handle, true);
  225. shellmatta_processData(handle, (char*)"?\r", 2);
  226. CHECK(write_length == strlen(dummyData));
  227. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  228. }
  229. TEST_CASE( "shellmatta reset no prompt history buffer" ) {
  230. shellmatta_instance_t inst;
  231. shellmatta_handle_t handle;
  232. char buffer[1024];
  233. char historyBuffer[1024];
  234. char *dummyData = (char*)"?\r\n"
  235. "doSomething do Function does something\r\n"
  236. "help ? help [command] - print help or usage information\r\n"
  237. "\r\nshellmatta->";
  238. shellmatta_doInit( &inst,
  239. &handle,
  240. buffer,
  241. sizeof(buffer),
  242. historyBuffer,
  243. sizeof(historyBuffer),
  244. "shellmatta->",
  245. NULL,
  246. writeFct);
  247. shellmatta_addCmd(handle, &doSomethingCmd);
  248. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh gl\r\n", 35u);
  249. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh gl\r\n", 35u);
  250. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh gl\r\n", 35u);
  251. write_callCnt = 0u;
  252. memset(write_data, 0, sizeof(write_data));
  253. write_length = 0u;
  254. shellmatta_resetShell(handle, false);
  255. /* process arrow key up - if reset worked this should deliver nothing */
  256. shellmatta_processData(handle, (char*)"\033[A", 3u);
  257. shellmatta_processData(handle, (char*)"?\r", 2);
  258. CHECK(write_length == strlen(dummyData));
  259. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  260. }
  261. TEST_CASE( "shellmatta reset no prompt heredoc" ) {
  262. shellmatta_instance_t inst;
  263. shellmatta_handle_t handle;
  264. char buffer[1024];
  265. char historyBuffer[1024];
  266. char *dummyData = (char*)"?\r\n"
  267. "doSomething do Function does something\r\n"
  268. "help ? help [command] - print help or usage information\r\n"
  269. "\r\nshellmatta->";
  270. shellmatta_doInit( &inst,
  271. &handle,
  272. buffer,
  273. sizeof(buffer),
  274. historyBuffer,
  275. sizeof(historyBuffer),
  276. "shellmatta->",
  277. NULL,
  278. writeFct);
  279. shellmatta_addCmd(handle, &doSomethingCmd);
  280. shellmatta_processData(handle, (char*)"dummy cmd << EOF\r\n", 18u);
  281. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh gl\r\n", 35u);
  282. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh gl\r\n", 35u);
  283. write_callCnt = 0u;
  284. memset(write_data, 0, sizeof(write_data));
  285. write_length = 0u;
  286. /* end the heredoc session by resetting the shell */
  287. shellmatta_resetShell(handle, false);
  288. /* now the new command should be processed */
  289. shellmatta_processData(handle, (char*)"?\r", 2);
  290. CHECK(write_length == strlen(dummyData));
  291. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  292. }
  293. TEST_CASE( "shellmatta configure disable echo" ) {
  294. shellmatta_instance_t inst;
  295. shellmatta_handle_t handle;
  296. shellmatta_retCode_t ret;
  297. char buffer[1024];
  298. char historyBuffer[1024];
  299. char *dummyData = (char*)"help this is some dummy Text\r\n"
  300. "doSomething do Function does something\r\n"
  301. "help ? help [command] - print help or usage information\r\n"
  302. "\r\nshellmatta->";
  303. char *dummyData2 = (char*)"doSomething do Function does something\r\n"
  304. "help ? help [command] - print help or usage information\r\n"
  305. "\r\nshellmatta->";
  306. shellmatta_doInit( &inst,
  307. &handle,
  308. buffer,
  309. sizeof(buffer),
  310. historyBuffer,
  311. sizeof(historyBuffer),
  312. "shellmatta->",
  313. NULL,
  314. writeFct);
  315. shellmatta_addCmd(handle, &doSomethingCmd);
  316. write_callCnt = 0u;
  317. memset(write_data, 0, sizeof(write_data));
  318. write_length = 0u;
  319. /* check with echo enabled */
  320. shellmatta_processData(handle, (char*)"help this is some dummy Text\r\n", 30u);
  321. CHECK(write_length == strlen(dummyData));
  322. CHECK_THAT(write_data, Catch::Matchers::Equals(dummyData));
  323. write_callCnt = 0u;
  324. memset(write_data, 0, sizeof(write_data));
  325. write_length = 0u;
  326. /* turn off echo now */
  327. ret = shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
  328. CHECK( ret == SHELLMATTA_OK );
  329. /* check with echo disabled */
  330. shellmatta_processData(handle, (char*)"help this is some dummy Text\r\n", 30u);
  331. CHECK(write_length == strlen(dummyData2));
  332. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData2));
  333. }
  334. TEST_CASE( "shellmatta configure mode" ) {
  335. shellmatta_instance_t inst;
  336. shellmatta_handle_t handle;
  337. shellmatta_retCode_t ret;
  338. char buffer[1024];
  339. char historyBuffer[1024];
  340. char *dummyData = (char*)"\r\nCommand: meow this is some dum123456my Text not found\r\nshellmatta->";
  341. char *dummyData2 = (char*)"\r\nCommand: meow this is some dum123456t not found\r\nshellmatta->";
  342. shellmatta_doInit( &inst,
  343. &handle,
  344. buffer,
  345. sizeof(buffer),
  346. historyBuffer,
  347. sizeof(historyBuffer),
  348. "shellmatta->",
  349. NULL,
  350. writeFct);
  351. shellmatta_addCmd(handle, &doSomethingCmd);
  352. ret = shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
  353. write_callCnt = 0u;
  354. memset(write_data, 0, sizeof(write_data));
  355. write_length = 0u;
  356. /* check with insert mode */
  357. shellmatta_processData(handle, (char*)"meow this is some dummy Text\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D123456\r\n", 57u);
  358. CHECK(write_length == strlen(dummyData));
  359. CHECK_THAT(write_data, Catch::Matchers::Equals(dummyData));
  360. write_callCnt = 0u;
  361. memset(write_data, 0, sizeof(write_data));
  362. write_length = 0u;
  363. /* check with overwrite mode */
  364. ret = shellmatta_configure(handle, SHELLMATTA_MODE_OVERWRITE, false, '\r');
  365. CHECK( ret == SHELLMATTA_OK );
  366. /* check with echo disabled */
  367. shellmatta_processData(handle, (char*)"meow this is some dummy Text\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D123456\r\n", 57u);
  368. CHECK(write_length == strlen(dummyData2));
  369. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData2));
  370. }
  371. TEST_CASE( "shellmatta configure delimiter" ) {
  372. shellmatta_instance_t inst;
  373. shellmatta_handle_t handle;
  374. shellmatta_retCode_t ret;
  375. char buffer[1024];
  376. char historyBuffer[1024];
  377. char *dummyData = (char*)"doSomething argument - length: 20\r\nshellmatta->";
  378. shellmatta_doInit( &inst,
  379. &handle,
  380. buffer,
  381. sizeof(buffer),
  382. historyBuffer,
  383. sizeof(historyBuffer),
  384. "shellmatta->",
  385. NULL,
  386. writeFct);
  387. shellmatta_addCmd(handle, &doSomethingCmd);
  388. ret = shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
  389. write_callCnt = 0u;
  390. memset(write_data, 0, sizeof(write_data));
  391. write_length = 0u;
  392. /* check with insert mode */
  393. shellmatta_processData(handle, (char*)"doSomething argument\n", 21u);
  394. CHECK( write_length == 0u);
  395. shellmatta_resetShell(handle, false);
  396. write_callCnt = 0u;
  397. memset(write_data, 0, sizeof(write_data));
  398. write_length = 0u;
  399. /* check with changed delimiter mode */
  400. ret = shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\n');
  401. CHECK( ret == SHELLMATTA_OK );
  402. /* check with echo disabled */
  403. shellmatta_processData(handle, (char*)"doSomething argument\n", 21u);
  404. CHECK(write_length == strlen(dummyData));
  405. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  406. }