test_integration.cpp 17 KB

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