test_integration.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. "help ? Print this help text help\r\n"
  67. "\r\nshellmatta->";
  68. shellmatta_doInit( &inst,
  69. &handle,
  70. buffer,
  71. sizeof(buffer),
  72. historyBuffer,
  73. sizeof(historyBuffer),
  74. "shellmatta->",
  75. NULL,
  76. writeFct);
  77. shellmatta_addCmd(handle, &doSomethingCmd);
  78. write_callCnt = 0u;
  79. memset(write_data, 0, sizeof(write_data));
  80. write_length = 0u;
  81. shellmatta_processData(handle, (char*)"?\r", 2);
  82. CHECK( write_length == 123u);
  83. CHECK( strcmp(dummyData, write_data) == 0);
  84. shellmatta_addCmd(handle, &emptyCmd);
  85. write_callCnt = 0u;
  86. memset(write_data, 0, sizeof(write_data));
  87. write_length = 0u;
  88. dummyData = (char*)"? 564 321 56 465 46\r\n"
  89. "doSomething do Function does something use me, please\r\n"
  90. "empty \r\n"
  91. "help ? Print this help text help\r\n"
  92. "\r\nshellmatta->";
  93. shellmatta_processData(handle, (char*)"? 564 321 56 465 46\r", 20);
  94. CHECK( write_length == strlen(dummyData));
  95. CHECK( strcmp(dummyData, write_data) == 0);
  96. write_callCnt = 0u;
  97. memset(write_data, 0, sizeof(write_data));
  98. write_length = 0u;
  99. dummyData = (char*)"?r\r\n"
  100. "Command: ?r not found"
  101. "\r\nshellmatta->";
  102. shellmatta_processData(handle, (char*)"?r\r", 3);
  103. CHECK( write_length == 39u);
  104. REQUIRE( strcmp(dummyData, write_data) == 0);
  105. }
  106. TEST_CASE( "shellmatta heredoc test" ) {
  107. shellmatta_instance_t inst;
  108. shellmatta_handle_t handle;
  109. char buffer[1024];
  110. char historyBuffer[1024];
  111. char *dummyData = (char*)"do this \r\n"
  112. "asdf\r\n"
  113. "1234";
  114. shellmatta_doInit( &inst,
  115. &handle,
  116. buffer,
  117. sizeof(buffer),
  118. historyBuffer,
  119. sizeof(historyBuffer),
  120. "shellmatta->",
  121. NULL,
  122. writeFct);
  123. shellmatta_addCmd(handle, &doSomethingCmd);
  124. doSomethingArguments = NULL;
  125. doSomethingLength = 0u;
  126. shellmatta_processData(handle, (char*)"do this << EOF\r\n"
  127. "asdf\r\n"
  128. "1234\r\n"
  129. "EOF\r\n"
  130. , 34);
  131. CHECK( doSomethingLength == 20u);
  132. REQUIRE( strcmp(dummyData, doSomethingArguments) == 0);
  133. }
  134. TEST_CASE( "shellmatta remove function" ) {
  135. shellmatta_instance_t inst;
  136. shellmatta_handle_t handle;
  137. char buffer[1024];
  138. char historyBuffer[1024];
  139. char *dummyData = (char*)"?\r\n"
  140. "doSomething do Function does something use me, please\r\n"
  141. "help ? Print this help text help\r\n"
  142. "\r\nshellmatta->";
  143. shellmatta_doInit( &inst,
  144. &handle,
  145. buffer,
  146. sizeof(buffer),
  147. historyBuffer,
  148. sizeof(historyBuffer),
  149. "shellmatta->",
  150. NULL,
  151. writeFct);
  152. shellmatta_addCmd(handle, &doSomethingCmd);
  153. write_callCnt = 0u;
  154. memset(write_data, 0, sizeof(write_data));
  155. write_length = 0u;
  156. shellmatta_processData(handle, (char*)"?\r", 2);
  157. CHECK( write_length == 123u);
  158. CHECK( strcmp(dummyData, write_data) == 0);
  159. write_callCnt = 0u;
  160. memset(write_data, 0, sizeof(write_data));
  161. write_length = 0u;
  162. dummyData = (char*)"? 564 321 56 465 46\r\n"
  163. "doSomething do Function does something use me, please\r\n"
  164. "help ? Print this help text help\r\n"
  165. "\r\nshellmatta->";
  166. shellmatta_processData(handle, (char*)"? 564 321 56 465 46\r", 20);
  167. CHECK( write_length == 141u);
  168. CHECK( strcmp(dummyData, write_data) == 0);
  169. write_callCnt = 0u;
  170. memset(write_data, 0, sizeof(write_data));
  171. write_length = 0u;
  172. shellmatta_removeCmd(handle, &doSomethingCmd);
  173. shellmatta_processData(handle, (char*)"? 564 321 56 465 46\r", 20);
  174. dummyData = (char*)"? 564 321 56 465 46\r\n"
  175. "help ? Print this help text help\r\n"
  176. "\r\nshellmatta->";
  177. printf("sdfsd sdf sdf sdf sdf sd fds\n%s", write_data);
  178. CHECK( write_length == 72u);
  179. REQUIRE( strcmp(dummyData, write_data) == 0);
  180. }