test_integration.cpp 19 KB

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