test_integration.cpp 18 KB

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