test_integration_continue.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright (c) 2019 - 2024 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_busy.cpp
  10. * @brief integration test implementation for the cmd busy function
  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 uint32_t contCallCnt;
  22. static uint32_t busyCallCnt;
  23. static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  24. {
  25. write_callCnt ++;
  26. while((length > 0) && (write_length < sizeof(write_data)))
  27. {
  28. write_data[write_length] = *data;
  29. data ++;
  30. length --;
  31. write_length ++;
  32. }
  33. return SHELLMATTA_OK;
  34. }
  35. static shellmatta_retCode_t continueCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  36. {
  37. (void) handle;
  38. (void) arguments;
  39. (void) length;
  40. shellmatta_retCode_t ret = SHELLMATTA_CONTINUE;
  41. char *stdinData;
  42. uint32_t stdinLength = 0u;
  43. shellmatta_read(handle, &stdinData, &stdinLength);
  44. if(NULL == stdinData)
  45. {
  46. stdinData = (char *)"";
  47. }
  48. shellmatta_printf(handle, "arguments: %s length: %u\r\n", stdinData, stdinLength);
  49. /** change to busy mode when k is pressed */
  50. if('k' == stdinData[0])
  51. {
  52. ret = SHELLMATTA_BUSY;
  53. busyCallCnt ++;
  54. }
  55. if(busyCallCnt > 1u)
  56. {
  57. ret = SHELLMATTA_CONTINUE;
  58. }
  59. if(contCallCnt >= 9u)
  60. {
  61. ret = SHELLMATTA_OK;
  62. }
  63. /* -# the arguments shall stay the same on every call - data is transferred per stdin */
  64. CHECK(length == 28u);
  65. CHECK_THAT(arguments, Catch::Matchers::Equals("continue some arguments meow"));
  66. contCallCnt ++;
  67. return ret;
  68. }
  69. shellmatta_cmd_t continueCmd = {(char*)"continue", (char*)"c", NULL, NULL, continueCmdFct, NULL};
  70. TEST_CASE( "shellmatta continue 1" ) {
  71. shellmatta_retCode_t ret;
  72. shellmatta_instance_t inst;
  73. shellmatta_handle_t handle;
  74. char buffer[1024];
  75. char historyBuffer[1024];
  76. char *dummyData = (char*) "continue some arguments meow\r\n"
  77. "arguments: length: 0\r\n"
  78. "arguments: length: 0\r\n"
  79. "arguments: a length: 1\r\n"
  80. "arguments: b length: 1\r\n"
  81. "arguments: c length: 1\r\n"
  82. "arguments: 8 length: 1\r\n"
  83. "arguments: 7 length: 1\r\n"
  84. "arguments: 6 length: 1\r\n"
  85. "arguments: 5 length: 1\r\n"
  86. "arguments: length: 0\r\n"
  87. "\r\n"
  88. "shellmatta->\r\n"
  89. "shellmatta->";
  90. shellmatta_doInit( &inst,
  91. &handle,
  92. buffer,
  93. sizeof(buffer),
  94. historyBuffer,
  95. sizeof(historyBuffer),
  96. "shellmatta->",
  97. NULL,
  98. writeFct);
  99. contCallCnt = 0u;
  100. busyCallCnt = 0u;
  101. write_callCnt = 0u;
  102. memset(write_data, 0, sizeof(write_data));
  103. write_length = 0u;
  104. shellmatta_addCmd(handle, &continueCmd);
  105. ret = shellmatta_processData(handle, (char*)"continue some arguments meow\r", 29);
  106. CHECK(SHELLMATTA_CONTINUE == ret);
  107. /** -# call without any argument */
  108. ret = shellmatta_processData(handle, NULL, 0);
  109. CHECK(SHELLMATTA_CONTINUE == ret);
  110. /** -# pass some argument */
  111. ret = shellmatta_processData(handle, (char*)"abc", 3);
  112. CHECK(SHELLMATTA_CONTINUE == ret);
  113. ret = shellmatta_processData(handle, (char*)"8765", 4);
  114. CHECK(SHELLMATTA_CONTINUE == ret);
  115. /** -# call without any argument */
  116. ret = shellmatta_processData(handle, NULL, 0);
  117. CHECK(SHELLMATTA_OK == ret);
  118. /** -# continue session should be over */
  119. ret = shellmatta_processData(handle, (char*)"\r", 1);
  120. CHECK(SHELLMATTA_OK == ret);
  121. CHECK( 0u == busyCallCnt);
  122. CHECK( 10u == contCallCnt);
  123. CHECK( write_length == strlen(dummyData));
  124. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  125. }
  126. TEST_CASE( "shellmatta continue cancel" ) {
  127. shellmatta_retCode_t ret;
  128. shellmatta_instance_t inst;
  129. shellmatta_handle_t handle;
  130. char buffer[1024];
  131. char historyBuffer[1024];
  132. char *dummyData = (char*) "continue some arguments meow\r\n"
  133. "arguments: length: 0\r\n"
  134. "arguments: length: 0\r\n"
  135. "arguments: a length: 1\r\n"
  136. "arguments: b length: 1\r\n"
  137. "arguments: \x03 length: 1\r\n"
  138. "\r\n"
  139. "shellmatta->\r\n"
  140. "shellmatta->";
  141. shellmatta_doInit( &inst,
  142. &handle,
  143. buffer,
  144. sizeof(buffer),
  145. historyBuffer,
  146. sizeof(historyBuffer),
  147. "shellmatta->",
  148. NULL,
  149. writeFct);
  150. contCallCnt = 0u;
  151. busyCallCnt = 0u;
  152. write_callCnt = 0u;
  153. memset(write_data, 0, sizeof(write_data));
  154. write_length = 0u;
  155. shellmatta_addCmd(handle, &continueCmd);
  156. ret = shellmatta_processData(handle, (char*)"continue some arguments meow\r", 29);
  157. CHECK(SHELLMATTA_CONTINUE == ret);
  158. /** -# call without any argument */
  159. ret = shellmatta_processData(handle, NULL, 0);
  160. CHECK(SHELLMATTA_CONTINUE == ret);
  161. /** -# pass some argument */
  162. ret = shellmatta_processData(handle, (char*)"ab", 2);
  163. CHECK(SHELLMATTA_CONTINUE == ret);
  164. ret = shellmatta_processData(handle, (char*)"\x03", 1);
  165. CHECK(SHELLMATTA_OK == ret);
  166. /** -# continue session should be over */
  167. ret = shellmatta_processData(handle, (char*)"\r", 1);
  168. CHECK(SHELLMATTA_OK == ret);
  169. CHECK( 0u == busyCallCnt);
  170. CHECK( 5u == contCallCnt);
  171. CHECK( write_length == strlen(dummyData));
  172. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  173. }
  174. TEST_CASE( "shellmatta continue suspend with busy mode" ) {
  175. shellmatta_retCode_t ret;
  176. shellmatta_instance_t inst;
  177. shellmatta_handle_t handle;
  178. char buffer[1024];
  179. char historyBuffer[1024];
  180. char *dummyData = (char*) "continue some arguments meow\r\n"
  181. "arguments: length: 0\r\n"
  182. "arguments: length: 0\r\n"
  183. "arguments: a length: 1\r\n"
  184. "arguments: b length: 1\r\n"
  185. "arguments: k length: 1\r\n"
  186. "arguments: k length: 1\r\n"
  187. "arguments: length: 0\r\n"
  188. "arguments: a length: 1\r\n"
  189. "arguments: b length: 1\r\n"
  190. "arguments: c length: 1\r\n"
  191. "\r\n"
  192. "shellmatta->\r\n"
  193. "shellmatta->";
  194. shellmatta_doInit( &inst,
  195. &handle,
  196. buffer,
  197. sizeof(buffer),
  198. historyBuffer,
  199. sizeof(historyBuffer),
  200. "shellmatta->",
  201. NULL,
  202. writeFct);
  203. contCallCnt = 0u;
  204. busyCallCnt = 0u;
  205. write_callCnt = 0u;
  206. memset(write_data, 0, sizeof(write_data));
  207. write_length = 0u;
  208. shellmatta_addCmd(handle, &continueCmd);
  209. ret = shellmatta_processData(handle, (char*)"continue some arguments meow\r", 29);
  210. CHECK(SHELLMATTA_CONTINUE == ret);
  211. /** -# call without any argument */
  212. ret = shellmatta_processData(handle, NULL, 0);
  213. CHECK(SHELLMATTA_CONTINUE == ret);
  214. /** -# pass some argument */
  215. ret = shellmatta_processData(handle, (char*)"ab", 2);
  216. CHECK(SHELLMATTA_CONTINUE == ret);
  217. ret = shellmatta_processData(handle, (char*)"k", 1);
  218. CHECK(SHELLMATTA_BUSY == ret);
  219. ret = shellmatta_processData(handle, (char*)"k", 1);
  220. CHECK(SHELLMATTA_CONTINUE == ret);
  221. ret = shellmatta_processData(handle, (char*)"", 0);
  222. CHECK(SHELLMATTA_CONTINUE == ret);
  223. ret = shellmatta_processData(handle, (char*)"abc", 3);
  224. CHECK(SHELLMATTA_OK == ret);
  225. /** -# continue session should be over */
  226. ret = shellmatta_processData(handle, (char*)"\r", 1);
  227. CHECK(SHELLMATTA_OK == ret);
  228. CHECK( 10u == contCallCnt);
  229. CHECK( 2u == busyCallCnt);
  230. CHECK( write_length == strlen(dummyData));
  231. REQUIRE_THAT(write_data, Catch::Matchers::Equals(dummyData));
  232. }