test_integration_transport.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2021 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_transport.cpp
  10. * @brief integration test implementation for the transport layer
  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 const char *doSomethingArguments;
  22. static uint32_t doSomethingLength;
  23. static char *doSomethingStdin;
  24. static uint32_t doSomethingStdinLength;
  25. static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  26. {
  27. write_callCnt ++;
  28. while((length > 0) && (write_length < sizeof(write_data)))
  29. {
  30. write_data[write_length] = *data;
  31. data ++;
  32. length --;
  33. write_length ++;
  34. }
  35. return SHELLMATTA_OK;
  36. }
  37. static shellmatta_retCode_t doSomething(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  38. {
  39. doSomethingArguments = arguments;
  40. doSomethingLength = length;
  41. shellmatta_read(handle, &doSomethingStdin, &doSomethingStdinLength);
  42. shellmatta_printf(handle, "%s - length: %u", arguments, length);
  43. return SHELLMATTA_OK;
  44. }
  45. shellmatta_cmd_t doSomethingCmd =
  46. {
  47. (char*)"doSomething",
  48. (char*)"do",
  49. (char*)"Function does something",
  50. (char*)"use me, please",
  51. doSomething,
  52. NULL
  53. };
  54. SCENARIO("Integration test of Transport layer", "[integration, transport]")
  55. {
  56. GIVEN("Shellmatta up and running with one command")
  57. {
  58. shellmatta_instance_t inst;
  59. shellmatta_handle_t handle;
  60. char buffer[1024];
  61. char historyBuffer[1024];
  62. shellmatta_doInit( &inst,
  63. &handle,
  64. buffer,
  65. sizeof(buffer),
  66. historyBuffer,
  67. sizeof(historyBuffer),
  68. "shellmatta->",
  69. NULL,
  70. writeFct);
  71. shellmatta_addCmd(handle, &doSomethingCmd);
  72. write_callCnt = 0u;
  73. memset(write_data, 0, sizeof(write_data));
  74. write_length = 0u;
  75. WHEN("Invalid CRC is passed")
  76. {
  77. /* check with invalid payload */
  78. shellmatta_processData(handle, (char*)"\x01\x01\x00\x16\x00\x00\x00\x00"
  79. "doSomething argument\r\n"
  80. "\x00\x00\x00\x00", 34u);
  81. THEN("Shellmatta responds with CRC error")
  82. {
  83. char *dummyData = (char*)"crc error\r\n\r\nshellmatta->";
  84. CHECK( write_length == strlen(dummyData));
  85. REQUIRE( strcmp(dummyData, write_data) == 0);
  86. }
  87. }
  88. WHEN("Valid CRC is passed")
  89. {
  90. /* check with valid payload - disable echo to reduce cluttering */
  91. shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
  92. shellmatta_processData(handle, (char*)"\x01\x01\x00\x16\x00\x00\x00\x00"
  93. "doSomething argument\r\n"
  94. "\x7b\x49\xfa\x72", 34u);
  95. THEN("The shellmatta responds to the command")
  96. {
  97. char *dummyData = (char*)"\x01\x01\x00\x21\x00\x00\x01\x01"
  98. "doSomething argument - length: 20"
  99. "\xac\xf5\xe9\x4f"
  100. "\x01\x01\x00\x02\x00\x00\x01\x02"
  101. "\r\n"
  102. "\x62\xef\x22\x7a"
  103. "\x01\x01\x00\x0C\x00\x00\x01\x03"
  104. "shellmatta->"
  105. "\xd2\x0b\x8f\x3e";
  106. CHECK( write_length == 83);
  107. REQUIRE( memcmp(write_data, dummyData, 83) == 0);
  108. }
  109. }
  110. WHEN("manual mode is used after transport layer mode")
  111. {
  112. /* check with valid payload - disable echo to reduce cluttering */
  113. shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
  114. shellmatta_processData(handle, (char*)"\x01\x01\x00\x16\x00\x00\x00\x00"
  115. "doSomething argument\r\n"
  116. "\x7b\x49\xfa\x72", 34u);
  117. shellmatta_processData(handle, (char*)"doSomething argument\r\n", 22u);
  118. THEN("The shellmatta responds to the command")
  119. {
  120. char *dummyData = (char*)"\x01\x01\x00\x21\x00\x00\x01\x01"
  121. "doSomething argument - length: 20"
  122. "\xac\xf5\xe9\x4f"
  123. "\x01\x01\x00\x02\x00\x00\x01\x02"
  124. "\r\n"
  125. "\x62\xef\x22\x7a"
  126. "\x01\x01\x00\x0C\x00\x00\x01\x03"
  127. "shellmatta->"
  128. "\xd2\x0b\x8f\x3e"
  129. "doSomething argument - length: 20\r\n"
  130. "shellmatta->";
  131. CHECK( write_length == 130);
  132. REQUIRE( memcmp(write_data, dummyData, 130) == 0);
  133. }
  134. }
  135. WHEN("Sequence counter is requested")
  136. {
  137. /* request sequence counter */
  138. shellmatta_processData(handle, (char*)"\x01\x01\x01\x00\x00\x00\x00\x00"
  139. "\xc4\xa3\x07\xe6", 12u);
  140. THEN("The valid Sequence counter is returned")
  141. {
  142. char *dummyData = (char*)"\x01\x01\x81\x00\x00\x00\x01\x01"
  143. "\xb4\x0f\x12\xe9";
  144. CHECK(write_length == 12);
  145. REQUIRE(memcmp(write_data, dummyData, 12) == 0);
  146. }
  147. AND_WHEN("Sequence counter is requested again")
  148. {
  149. /* request sequence counter again */
  150. write_callCnt = 0u;
  151. memset(write_data, 0, sizeof(write_data));
  152. write_length = 0u;
  153. shellmatta_processData(handle, (char*)"\x01\x01\x01\x00\x00\x00\x00\x00"
  154. "\xc4\xa3\x07\xe6", 12u);
  155. THEN("The next Sequence counter is returned")
  156. {
  157. char *dummyData = (char*)"\x01\x01\x81\x00\x00\x00\x02\x02"
  158. "\x06\x2b\x10\x90";
  159. CHECK(write_length == 12);
  160. REQUIRE(memcmp(write_data, dummyData, 12) == 0);
  161. }
  162. }
  163. }
  164. }
  165. }