test_integration.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 remove function" ) {
  141. shellmatta_instance_t inst;
  142. shellmatta_handle_t handle;
  143. char buffer[1024];
  144. char historyBuffer[1024];
  145. char *dummyData = (char*)"?\r\n"
  146. "doSomething do Function does something use me, please\r\n"
  147. "help ? Print this help text help\r\n"
  148. "\r\nshellmatta->";
  149. shellmatta_doInit( &inst,
  150. &handle,
  151. buffer,
  152. sizeof(buffer),
  153. historyBuffer,
  154. sizeof(historyBuffer),
  155. "shellmatta->",
  156. NULL,
  157. writeFct);
  158. shellmatta_addCmd(handle, &doSomethingCmd);
  159. write_callCnt = 0u;
  160. memset(write_data, 0, sizeof(write_data));
  161. write_length = 0u;
  162. shellmatta_processData(handle, (char*)"?\r", 2);
  163. CHECK( write_length == 123u);
  164. CHECK( strcmp(dummyData, write_data) == 0);
  165. write_callCnt = 0u;
  166. memset(write_data, 0, sizeof(write_data));
  167. write_length = 0u;
  168. dummyData = (char*)"? 564 321 56 465 46\r\n"
  169. "doSomething do Function does something use me, please\r\n"
  170. "help ? Print this help text help\r\n"
  171. "\r\nshellmatta->";
  172. shellmatta_processData(handle, (char*)"? 564 321 56 465 46\r", 20);
  173. CHECK( write_length == 141u);
  174. CHECK( strcmp(dummyData, write_data) == 0);
  175. write_callCnt = 0u;
  176. memset(write_data, 0, sizeof(write_data));
  177. write_length = 0u;
  178. shellmatta_removeCmd(handle, &doSomethingCmd);
  179. shellmatta_processData(handle, (char*)"? 564 321 56 465 46\r", 20);
  180. dummyData = (char*)"? 564 321 56 465 46\r\n"
  181. "help ? Print this help text help\r\n"
  182. "\r\nshellmatta->";
  183. CHECK( write_length == 72u);
  184. REQUIRE( strcmp(dummyData, write_data) == 0);
  185. }
  186. TEST_CASE( "shellmatta reset no prompt" ) {
  187. shellmatta_instance_t inst;
  188. shellmatta_handle_t handle;
  189. char buffer[1024];
  190. char historyBuffer[1024];
  191. char *dummyData = (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh glkd?\r\n"
  192. "doSomething do Function does something use me, please\r\n"
  193. "help ? Print this help text help\r\n"
  194. "\r\nshellmatta->";
  195. shellmatta_doInit( &inst,
  196. &handle,
  197. buffer,
  198. sizeof(buffer),
  199. historyBuffer,
  200. sizeof(historyBuffer),
  201. "shellmatta->",
  202. NULL,
  203. writeFct);
  204. shellmatta_addCmd(handle, &doSomethingCmd);
  205. write_callCnt = 0u;
  206. memset(write_data, 0, sizeof(write_data));
  207. write_length = 0u;
  208. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh glkd", 35);
  209. shellmatta_resetShell(handle, false);
  210. shellmatta_processData(handle, (char*)"?\r", 2);
  211. CHECK( write_length == strlen(dummyData));
  212. REQUIRE( strcmp(dummyData, write_data) == 0);
  213. }
  214. TEST_CASE( "shellmatta reset with prompt" ) {
  215. shellmatta_instance_t inst;
  216. shellmatta_handle_t handle;
  217. char buffer[1024];
  218. char historyBuffer[1024];
  219. char *dummyData = (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh glkd"
  220. "\r\nshellmatta->?\r\n"
  221. "doSomething do Function does something use me, please\r\n"
  222. "help ? Print this help text help\r\n"
  223. "\r\nshellmatta->";
  224. shellmatta_doInit( &inst,
  225. &handle,
  226. buffer,
  227. sizeof(buffer),
  228. historyBuffer,
  229. sizeof(historyBuffer),
  230. "shellmatta->",
  231. NULL,
  232. writeFct);
  233. shellmatta_addCmd(handle, &doSomethingCmd);
  234. write_callCnt = 0u;
  235. memset(write_data, 0, sizeof(write_data));
  236. write_length = 0u;
  237. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh glkd", 35);
  238. shellmatta_resetShell(handle, true);
  239. shellmatta_processData(handle, (char*)"?\r", 2);
  240. CHECK( write_length == strlen(dummyData));
  241. REQUIRE( strcmp(dummyData, write_data) == 0);
  242. }
  243. TEST_CASE( "shellmatta reset no prompt history buffer" ) {
  244. shellmatta_instance_t inst;
  245. shellmatta_handle_t handle;
  246. char buffer[1024];
  247. char historyBuffer[1024];
  248. char *dummyData = (char*)"?\r\n"
  249. "doSomething do Function does something use me, please\r\n"
  250. "help ? Print this help text help\r\n"
  251. "\r\nshellmatta->";
  252. shellmatta_doInit( &inst,
  253. &handle,
  254. buffer,
  255. sizeof(buffer),
  256. historyBuffer,
  257. sizeof(historyBuffer),
  258. "shellmatta->",
  259. NULL,
  260. writeFct);
  261. shellmatta_addCmd(handle, &doSomethingCmd);
  262. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh gl\r\n", 35u);
  263. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh gl\r\n", 35u);
  264. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh gl\r\n", 35u);
  265. write_callCnt = 0u;
  266. memset(write_data, 0, sizeof(write_data));
  267. write_length = 0u;
  268. shellmatta_resetShell(handle, false);
  269. /* process arrow key up - if reset worked this should deliver nothing */
  270. shellmatta_processData(handle, (char*)"\033[A", 3u);
  271. shellmatta_processData(handle, (char*)"?\r", 2);
  272. CHECK( write_length == strlen(dummyData));
  273. REQUIRE( strcmp(dummyData, write_data) == 0);
  274. }
  275. TEST_CASE( "shellmatta reset no prompt heredoc" ) {
  276. shellmatta_instance_t inst;
  277. shellmatta_handle_t handle;
  278. char buffer[1024];
  279. char historyBuffer[1024];
  280. char *dummyData = (char*)"?\r\n"
  281. "doSomething do Function does something use me, please\r\n"
  282. "help ? Print this help text help\r\n"
  283. "\r\nshellmatta->";
  284. shellmatta_doInit( &inst,
  285. &handle,
  286. buffer,
  287. sizeof(buffer),
  288. historyBuffer,
  289. sizeof(historyBuffer),
  290. "shellmatta->",
  291. NULL,
  292. writeFct);
  293. shellmatta_addCmd(handle, &doSomethingCmd);
  294. shellmatta_processData(handle, (char*)"dummy cmd << EOF\r\n", 18u);
  295. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh gl\r\n", 35u);
  296. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh gl\r\n", 35u);
  297. write_callCnt = 0u;
  298. memset(write_data, 0, sizeof(write_data));
  299. write_length = 0u;
  300. /* end the heredoc session by resetting the shell */
  301. shellmatta_resetShell(handle, false);
  302. /* now the new command should be processed */
  303. shellmatta_processData(handle, (char*)"?\r", 2);
  304. CHECK( write_length == strlen(dummyData));
  305. REQUIRE( strcmp(dummyData, write_data) == 0);
  306. }
  307. TEST_CASE( "shellmatta configure disable echo" ) {
  308. shellmatta_instance_t inst;
  309. shellmatta_handle_t handle;
  310. shellmatta_retCode_t ret;
  311. char buffer[1024];
  312. char historyBuffer[1024];
  313. char *dummyData = (char*)"help this is some dummy Text\r\n"
  314. "doSomething do Function does something use me, please\r\n"
  315. "help ? Print this help text help\r\n"
  316. "\r\nshellmatta->";
  317. char *dummyData2 = (char*)"doSomething do Function does something use me, please\r\n"
  318. "help ? Print this help text help\r\n"
  319. "\r\nshellmatta->";
  320. shellmatta_doInit( &inst,
  321. &handle,
  322. buffer,
  323. sizeof(buffer),
  324. historyBuffer,
  325. sizeof(historyBuffer),
  326. "shellmatta->",
  327. NULL,
  328. writeFct);
  329. shellmatta_addCmd(handle, &doSomethingCmd);
  330. write_callCnt = 0u;
  331. memset(write_data, 0, sizeof(write_data));
  332. write_length = 0u;
  333. /* check with echo enabled */
  334. shellmatta_processData(handle, (char*)"help this is some dummy Text\r\n", 30u);
  335. CHECK( write_length == strlen(dummyData));
  336. CHECK( strcmp(dummyData, write_data) == 0);
  337. write_callCnt = 0u;
  338. memset(write_data, 0, sizeof(write_data));
  339. write_length = 0u;
  340. /* turn off echo now */
  341. ret = shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false);
  342. CHECK( ret == SHELLMATTA_OK );
  343. /* check with echo disabled */
  344. shellmatta_processData(handle, (char*)"help this is some dummy Text\r\n", 30u);
  345. CHECK( write_length == strlen(dummyData2));
  346. REQUIRE( strcmp(dummyData2, write_data) == 0);
  347. }
  348. TEST_CASE( "shellmatta configure mode" ) {
  349. shellmatta_instance_t inst;
  350. shellmatta_handle_t handle;
  351. shellmatta_retCode_t ret;
  352. char buffer[1024];
  353. char historyBuffer[1024];
  354. char *dummyData = (char*)"\r\nCommand: meow this is some dum123456my Text not found\r\nshellmatta->";
  355. char *dummyData2 = (char*)"\r\nCommand: meow this is some dum123456t not found\r\nshellmatta->";
  356. shellmatta_doInit( &inst,
  357. &handle,
  358. buffer,
  359. sizeof(buffer),
  360. historyBuffer,
  361. sizeof(historyBuffer),
  362. "shellmatta->",
  363. NULL,
  364. writeFct);
  365. shellmatta_addCmd(handle, &doSomethingCmd);
  366. ret = shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false);
  367. write_callCnt = 0u;
  368. memset(write_data, 0, sizeof(write_data));
  369. write_length = 0u;
  370. /* check with insert mode */
  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(dummyData));
  373. CHECK( strcmp(dummyData, write_data) == 0);
  374. write_callCnt = 0u;
  375. memset(write_data, 0, sizeof(write_data));
  376. write_length = 0u;
  377. /* check with overwrite mode */
  378. ret = shellmatta_configure(handle, SHELLMATTA_MODE_OVERWRITE, false);
  379. CHECK( ret == SHELLMATTA_OK );
  380. /* check with echo disabled */
  381. 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);
  382. CHECK( write_length == strlen(dummyData2));
  383. REQUIRE( strcmp(dummyData2, write_data) == 0);
  384. }