test_integration_transport.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\x2f\x00\x00\x01\x01"
  98. "doSomething argument - length: 20"
  99. "\r\n"
  100. "shellmatta->"
  101. "\xc8\xae\xc0\x56";
  102. CHECK( write_length == 59);
  103. REQUIRE( memcmp(write_data, dummyData, 59) == 0);
  104. }
  105. }
  106. WHEN("manual mode is used after transport layer mode")
  107. {
  108. /* check with valid payload - disable echo to reduce cluttering */
  109. shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
  110. shellmatta_processData(handle, (char*)"\x01\x01\x00\x16\x00\x00\x00\x00"
  111. "doSomething argument\r\n"
  112. "\x7b\x49\xfa\x72", 34u);
  113. shellmatta_processData(handle, (char*)"doSomething argument\r\n", 22u);
  114. THEN("The shellmatta responds to the command")
  115. {
  116. char *dummyData = (char*)"\x01\x01\x00\x2f\x00\x00\x01\x01"
  117. "doSomething argument - length: 20"
  118. "\r\n"
  119. "shellmatta->"
  120. "\xc8\xae\xc0\x56"
  121. "doSomething argument - length: 20\r\n"
  122. "shellmatta->";
  123. CHECK( write_length == 106);
  124. REQUIRE( memcmp(write_data, dummyData, 106) == 0);
  125. }
  126. }
  127. WHEN("Sequence counter is requested")
  128. {
  129. /* request sequence counter */
  130. shellmatta_processData(handle, (char*)"\x01\x01\x01\x00\x00\x00\x00\x00"
  131. "\xc4\xa3\x07\xe6", 12u);
  132. THEN("The valid Sequence counter is returned")
  133. {
  134. char *dummyData = (char*)"\x01\x01\x81\x00\x00\x00\x01\x01"
  135. "\xb4\x0f\x12\xe9";
  136. CHECK(write_length == 12);
  137. REQUIRE(memcmp(write_data, dummyData, 12) == 0);
  138. }
  139. AND_WHEN("Sequence counter is requested again")
  140. {
  141. /* request sequence counter again */
  142. write_callCnt = 0u;
  143. memset(write_data, 0, sizeof(write_data));
  144. write_length = 0u;
  145. shellmatta_processData(handle, (char*)"\x01\x01\x01\x00\x00\x00\x00\x00"
  146. "\xc4\xa3\x07\xe6", 12u);
  147. THEN("The next Sequence counter is returned")
  148. {
  149. char *dummyData = (char*)"\x01\x01\x81\x00\x00\x00\x02\x02"
  150. "\x06\x2b\x10\x90";
  151. CHECK(write_length == 12);
  152. REQUIRE(memcmp(write_data, dummyData, 12) == 0);
  153. }
  154. }
  155. }
  156. }
  157. }