test_integration.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  12. {
  13. write_callCnt ++;
  14. while((length > 0) && (write_length < sizeof(write_data)))
  15. {
  16. write_data[write_length] = *data;
  17. data ++;
  18. length --;
  19. write_length ++;
  20. }
  21. return SHELLMATTA_OK;
  22. }
  23. static shellmatta_retCode_t doSomething(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  24. {
  25. doSomethingArguments = arguments;
  26. doSomethingLength = length;
  27. shellmatta_printf(handle, "%s - length: %u", arguments, length);
  28. return SHELLMATTA_OK;
  29. }
  30. shellmatta_cmd_t doSomethingCmd = {(char*)"doSomething", (char*)"do", (char*)"Function does something", (char*)"use me, please", doSomething, NULL};
  31. static shellmatta_retCode_t empty(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  32. {
  33. shellmatta_printf(handle, "empty - %s - length: %u", arguments, length);
  34. return SHELLMATTA_OK;
  35. }
  36. shellmatta_cmd_t emptyCmd = {(char*)"empty", NULL, NULL, NULL, empty, NULL};
  37. TEST_CASE( "shellmatta empty function" ) {
  38. shellmatta_instance_t inst;
  39. shellmatta_handle_t handle;
  40. char buffer[1024];
  41. char historyBuffer[1024];
  42. char *dummyData = (char*)"\r\nshellmatta->";
  43. shellmatta_doInit( &inst,
  44. &handle,
  45. buffer,
  46. sizeof(buffer),
  47. historyBuffer,
  48. sizeof(historyBuffer),
  49. "shellmatta->",
  50. NULL,
  51. writeFct);
  52. write_callCnt = 0u;
  53. memset(write_data, 0, sizeof(write_data));
  54. write_length = 0u;
  55. shellmatta_processData(handle, (char*)"\r", 1);
  56. CHECK( write_length == 14u);
  57. REQUIRE( strcmp(dummyData, write_data) == 0);
  58. }
  59. TEST_CASE( "shellmatta help function" ) {
  60. shellmatta_instance_t inst;
  61. shellmatta_handle_t handle;
  62. char buffer[1024];
  63. char historyBuffer[1024];
  64. char *dummyData = (char*)"?\r\n"
  65. "doSomething do Function does something use me, please\r\n"
  66. "empty \r\n"
  67. "help ? Print this help text help\r\n"
  68. "\r\nshellmatta->";
  69. shellmatta_doInit( &inst,
  70. &handle,
  71. buffer,
  72. sizeof(buffer),
  73. historyBuffer,
  74. sizeof(historyBuffer),
  75. "shellmatta->",
  76. NULL,
  77. writeFct);
  78. shellmatta_addCmd(handle, &emptyCmd);
  79. shellmatta_addCmd(handle, &doSomethingCmd);
  80. write_callCnt = 0u;
  81. memset(write_data, 0, sizeof(write_data));
  82. write_length = 0u;
  83. shellmatta_processData(handle, (char*)"?\r", 2);
  84. CHECK( write_length == strlen(dummyData));
  85. CHECK( strcmp(dummyData, write_data) == 0);
  86. write_callCnt = 0u;
  87. memset(write_data, 0, sizeof(write_data));
  88. write_length = 0u;
  89. dummyData = (char*)"? 564 321 56 465 46\r\n"
  90. "doSomething do Function does something use me, please\r\n"
  91. "empty \r\n"
  92. "help ? Print this help text help\r\n"
  93. "\r\nshellmatta->";
  94. shellmatta_processData(handle, (char*)"? 564 321 56 465 46\r", 20);
  95. CHECK( write_length == strlen(dummyData));
  96. CHECK( strcmp(dummyData, write_data) == 0);
  97. write_callCnt = 0u;
  98. memset(write_data, 0, sizeof(write_data));
  99. write_length = 0u;
  100. dummyData = (char*)"?r\r\n"
  101. "Command: ?r not found"
  102. "\r\nshellmatta->";
  103. shellmatta_processData(handle, (char*)"?r\r", 3);
  104. CHECK( write_length == 39u);
  105. REQUIRE( strcmp(dummyData, write_data) == 0);
  106. }
  107. TEST_CASE( "shellmatta heredoc test" ) {
  108. shellmatta_instance_t inst;
  109. shellmatta_handle_t handle;
  110. char buffer[1024];
  111. char historyBuffer[1024];
  112. char *dummyData = (char*)"do this \r\n"
  113. "asdf\r\n"
  114. "1234";
  115. shellmatta_doInit( &inst,
  116. &handle,
  117. buffer,
  118. sizeof(buffer),
  119. historyBuffer,
  120. sizeof(historyBuffer),
  121. "shellmatta->",
  122. NULL,
  123. writeFct);
  124. shellmatta_addCmd(handle, &doSomethingCmd);
  125. doSomethingArguments = NULL;
  126. doSomethingLength = 0u;
  127. shellmatta_processData(handle, (char*)"do this << EOF\r\n"
  128. "asdf\r\n"
  129. "1234\r\n"
  130. "EOF\r\n"
  131. , 34);
  132. CHECK( doSomethingLength == 20u);
  133. REQUIRE( strcmp(dummyData, doSomethingArguments) == 0);
  134. }
  135. TEST_CASE( "shellmatta remove function" ) {
  136. shellmatta_instance_t inst;
  137. shellmatta_handle_t handle;
  138. char buffer[1024];
  139. char historyBuffer[1024];
  140. char *dummyData = (char*)"?\r\n"
  141. "doSomething do Function does something use me, please\r\n"
  142. "help ? Print this help text help\r\n"
  143. "\r\nshellmatta->";
  144. shellmatta_doInit( &inst,
  145. &handle,
  146. buffer,
  147. sizeof(buffer),
  148. historyBuffer,
  149. sizeof(historyBuffer),
  150. "shellmatta->",
  151. NULL,
  152. writeFct);
  153. shellmatta_addCmd(handle, &doSomethingCmd);
  154. write_callCnt = 0u;
  155. memset(write_data, 0, sizeof(write_data));
  156. write_length = 0u;
  157. shellmatta_processData(handle, (char*)"?\r", 2);
  158. CHECK( write_length == 123u);
  159. CHECK( strcmp(dummyData, write_data) == 0);
  160. write_callCnt = 0u;
  161. memset(write_data, 0, sizeof(write_data));
  162. write_length = 0u;
  163. dummyData = (char*)"? 564 321 56 465 46\r\n"
  164. "doSomething do Function does something use me, please\r\n"
  165. "help ? Print this help text help\r\n"
  166. "\r\nshellmatta->";
  167. shellmatta_processData(handle, (char*)"? 564 321 56 465 46\r", 20);
  168. CHECK( write_length == 141u);
  169. CHECK( strcmp(dummyData, write_data) == 0);
  170. write_callCnt = 0u;
  171. memset(write_data, 0, sizeof(write_data));
  172. write_length = 0u;
  173. shellmatta_removeCmd(handle, &doSomethingCmd);
  174. shellmatta_processData(handle, (char*)"? 564 321 56 465 46\r", 20);
  175. dummyData = (char*)"? 564 321 56 465 46\r\n"
  176. "help ? Print this help text help\r\n"
  177. "\r\nshellmatta->";
  178. CHECK( write_length == 72u);
  179. REQUIRE( strcmp(dummyData, write_data) == 0);
  180. }
  181. TEST_CASE( "shellmatta reset no prompt" ) {
  182. shellmatta_instance_t inst;
  183. shellmatta_handle_t handle;
  184. char buffer[1024];
  185. char historyBuffer[1024];
  186. char *dummyData = (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh glkd?\r\n"
  187. "doSomething do Function does something use me, please\r\n"
  188. "help ? Print this help text help\r\n"
  189. "\r\nshellmatta->";
  190. shellmatta_doInit( &inst,
  191. &handle,
  192. buffer,
  193. sizeof(buffer),
  194. historyBuffer,
  195. sizeof(historyBuffer),
  196. "shellmatta->",
  197. NULL,
  198. writeFct);
  199. shellmatta_addCmd(handle, &doSomethingCmd);
  200. write_callCnt = 0u;
  201. memset(write_data, 0, sizeof(write_data));
  202. write_length = 0u;
  203. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh glkd", 35);
  204. shellmatta_resetShell(handle, false);
  205. shellmatta_processData(handle, (char*)"?\r", 2);
  206. CHECK( write_length == strlen(dummyData));
  207. REQUIRE( strcmp(dummyData, write_data) == 0);
  208. }
  209. TEST_CASE( "shellmatta reset with prompt" ) {
  210. shellmatta_instance_t inst;
  211. shellmatta_handle_t handle;
  212. char buffer[1024];
  213. char historyBuffer[1024];
  214. char *dummyData = (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh glkd"
  215. "\r\nshellmatta->?\r\n"
  216. "doSomething do Function does something use me, please\r\n"
  217. "help ? Print this help text help\r\n"
  218. "\r\nshellmatta->";
  219. shellmatta_doInit( &inst,
  220. &handle,
  221. buffer,
  222. sizeof(buffer),
  223. historyBuffer,
  224. sizeof(historyBuffer),
  225. "shellmatta->",
  226. NULL,
  227. writeFct);
  228. shellmatta_addCmd(handle, &doSomethingCmd);
  229. write_callCnt = 0u;
  230. memset(write_data, 0, sizeof(write_data));
  231. write_length = 0u;
  232. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh glkd", 35);
  233. shellmatta_resetShell(handle, true);
  234. shellmatta_processData(handle, (char*)"?\r", 2);
  235. CHECK( write_length == strlen(dummyData));
  236. REQUIRE( strcmp(dummyData, write_data) == 0);
  237. }
  238. TEST_CASE( "shellmatta reset no prompt history buffer" ) {
  239. shellmatta_instance_t inst;
  240. shellmatta_handle_t handle;
  241. char buffer[1024];
  242. char historyBuffer[1024];
  243. char *dummyData = (char*)"?\r\n"
  244. "doSomething do Function does something use me, please\r\n"
  245. "help ? Print this help text help\r\n"
  246. "\r\nshellmatta->";
  247. shellmatta_doInit( &inst,
  248. &handle,
  249. buffer,
  250. sizeof(buffer),
  251. historyBuffer,
  252. sizeof(historyBuffer),
  253. "shellmatta->",
  254. NULL,
  255. writeFct);
  256. shellmatta_addCmd(handle, &doSomethingCmd);
  257. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh gl\r\n", 35u);
  258. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh gl\r\n", 35u);
  259. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh gl\r\n", 35u);
  260. write_callCnt = 0u;
  261. memset(write_data, 0, sizeof(write_data));
  262. write_length = 0u;
  263. shellmatta_resetShell(handle, false);
  264. /* process arrow key up - if reset worked this should deliver nothing */
  265. shellmatta_processData(handle, (char*)"\033[A", 3u);
  266. shellmatta_processData(handle, (char*)"?\r", 2);
  267. CHECK( write_length == strlen(dummyData));
  268. REQUIRE( strcmp(dummyData, write_data) == 0);
  269. }
  270. TEST_CASE( "shellmatta reset no prompt heredoc" ) {
  271. shellmatta_instance_t inst;
  272. shellmatta_handle_t handle;
  273. char buffer[1024];
  274. char historyBuffer[1024];
  275. char *dummyData = (char*)"?\r\n"
  276. "doSomething do Function does something use me, please\r\n"
  277. "help ? Print this help text help\r\n"
  278. "\r\nshellmatta->";
  279. shellmatta_doInit( &inst,
  280. &handle,
  281. buffer,
  282. sizeof(buffer),
  283. historyBuffer,
  284. sizeof(historyBuffer),
  285. "shellmatta->",
  286. NULL,
  287. writeFct);
  288. shellmatta_addCmd(handle, &doSomethingCmd);
  289. shellmatta_processData(handle, (char*)"dummy cmd << EOF\r\n", 18u);
  290. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh gl\r\n", 35u);
  291. shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh gl\r\n", 35u);
  292. write_callCnt = 0u;
  293. memset(write_data, 0, sizeof(write_data));
  294. write_length = 0u;
  295. /* end the heredoc session by resetting the shell */
  296. shellmatta_resetShell(handle, false);
  297. /* now the new command should be processed */
  298. shellmatta_processData(handle, (char*)"?\r", 2);
  299. printf("%s", write_data);
  300. CHECK( write_length == strlen(dummyData));
  301. REQUIRE( strcmp(dummyData, write_data) == 0);
  302. }