test_integration_transport.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 = {(char*)"doSomething", (char*)"do", (char*)"Function does something", (char*)"use me, please", doSomething, NULL};
  46. TEST_CASE( "shellmatta transport crc error" ) {
  47. shellmatta_instance_t inst;
  48. shellmatta_handle_t handle;
  49. char buffer[1024];
  50. char historyBuffer[1024];
  51. char *dummyData = (char*)"crc error\r\n\r\nshellmatta->";
  52. shellmatta_doInit( &inst,
  53. &handle,
  54. buffer,
  55. sizeof(buffer),
  56. historyBuffer,
  57. sizeof(historyBuffer),
  58. "shellmatta->",
  59. NULL,
  60. writeFct);
  61. shellmatta_addCmd(handle, &doSomethingCmd);
  62. write_callCnt = 0u;
  63. memset(write_data, 0, sizeof(write_data));
  64. write_length = 0u;
  65. /* check with invalid payload */
  66. shellmatta_processData(handle, (char*)"\x01\x01\x00\x16\x00\x00\x00\x00"
  67. "doSomething argument\r\n"
  68. "\x00\x00\x00\x00", 34u);
  69. CHECK( write_length == strlen(dummyData));
  70. REQUIRE( strcmp(dummyData, write_data) == 0);
  71. }
  72. TEST_CASE( "shellmatta transport success" ) {
  73. shellmatta_instance_t inst;
  74. shellmatta_handle_t handle;
  75. char buffer[1024];
  76. char historyBuffer[1024];
  77. char *dummyData = (char*)"\x01\x01\x00\x21\x00\x00\x01\x01"
  78. "doSomething argument - length: 20"
  79. "\xac\xf5\xe9\x4f"
  80. "\x01\x01\x00\x02\x00\x00\x01\x02"
  81. "\r\n"
  82. "\x62\xef\x22\x7a"
  83. "\x01\x01\x00\x0C\x00\x00\x01\x03"
  84. "shellmatta->"
  85. "\xd2\x0b\x8f\x3e";
  86. shellmatta_doInit( &inst,
  87. &handle,
  88. buffer,
  89. sizeof(buffer),
  90. historyBuffer,
  91. sizeof(historyBuffer),
  92. "shellmatta->",
  93. NULL,
  94. writeFct);
  95. shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
  96. shellmatta_addCmd(handle, &doSomethingCmd);
  97. write_callCnt = 0u;
  98. memset(write_data, 0, sizeof(write_data));
  99. write_length = 0u;
  100. /* check with invalid payload */
  101. shellmatta_processData(handle, (char*)"\x01\x01\x00\x16\x00\x00\x00\x00"
  102. "doSomething argument\r\n"
  103. "\x7b\x49\xfa\x72", 34u);
  104. for(uint32_t i = 0; i < 83; i ++)
  105. {
  106. if(dummyData[i] != write_data[i])
  107. {
  108. CHECK(dummyData[i] == write_data[i]);
  109. }
  110. }
  111. CHECK( write_length == 83);
  112. REQUIRE( memcmp(write_data, dummyData, 83) == 0);
  113. }