123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- /*
- * Copyright (c) 2021 - 2024 Stefan Strobel <stefan.strobel@shimatta.net>
- *
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at https://mozilla.org/MPL/2.0/.
- */
- /**
- * @file test_integration_transport.cpp
- * @brief integration test implementation for the transport layer
- * @author Stefan Strobel <stefan.strobel@shimatta.net>
- */
- #include "test/framework/catch.hpp"
- extern "C" {
- #include "shellmatta.h"
- }
- #include <string.h>
- static uint32_t write_callCnt = 0u;
- static char write_data[1024];
- static uint32_t write_length;
- static const char *doSomethingArguments;
- static uint32_t doSomethingLength;
- static char *doSomethingStdin;
- static uint32_t doSomethingStdinLength;
- static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
- {
- write_callCnt ++;
- while((length > 0) && (write_length < sizeof(write_data)))
- {
- write_data[write_length] = *data;
- data ++;
- length --;
- write_length ++;
- }
- return SHELLMATTA_OK;
- }
- static shellmatta_retCode_t doSomething(shellmatta_handle_t handle, const char *arguments, uint32_t length)
- {
- doSomethingArguments = arguments;
- doSomethingLength = length;
- shellmatta_read(handle, &doSomethingStdin, &doSomethingStdinLength);
- shellmatta_printf(handle, "%s - length: %u", arguments, length);
- return SHELLMATTA_OK;
- }
- shellmatta_cmd_t doSomethingCmd =
- {
- (char*)"doSomething",
- (char*)"do",
- (char*)"Function does something",
- (char*)"use me, please",
- doSomething,
- NULL
- };
- static shellmatta_retCode_t largePayload(shellmatta_handle_t handle, const char *arguments, uint32_t length)
- {
- uint32_t i;
- for (i = 0u; i < 8; i ++)
- {
- shellmatta_printf(handle, "This is my very very very very large payload\r\n");
- }
- (void)arguments;
- (void)length;
- return SHELLMATTA_OK;
- }
- shellmatta_cmd_t largePayloadCmd =
- {
- (char*)"largePayload",
- (char*)"lp",
- (char*)"Function returns large payload",
- (char*)"i have much to say",
- largePayload,
- NULL
- };
- static shellmatta_retCode_t largePayloadAtOnce(shellmatta_handle_t handle, const char *arguments, uint32_t length)
- {
- shellmatta_write(handle,
- (char*)"This is my very very very very large payload\r\n"
- "This is my very very very very large payload\r\n"
- "This is my very very very very large payload\r\n"
- "This is my very very very very large payload\r\n"
- "This is my very very very very large payload\r\n"
- "This is my very very very very large payload\r\n"
- "This is my very very very very large payload\r\n"
- "This is my very very very very large payload\r\n",
- 46u*8u);
- (void)arguments;
- (void)length;
- return SHELLMATTA_OK;
- }
- shellmatta_cmd_t largePayloadAtOnceCmd =
- {
- (char*)"largePayload",
- (char*)"lp",
- (char*)"Function returns large payload",
- (char*)"i have much to say",
- largePayloadAtOnce,
- NULL
- };
- SCENARIO("Integration test of Transport layer", "[integration, transport]")
- {
- GIVEN("Shellmatta up and running with one command")
- {
- shellmatta_instance_t inst;
- shellmatta_handle_t handle;
- char buffer[1024];
- char historyBuffer[1024];
- shellmatta_doInit( &inst,
- &handle,
- buffer,
- sizeof(buffer),
- historyBuffer,
- sizeof(historyBuffer),
- "shellmatta->",
- NULL,
- writeFct);
- shellmatta_addCmd(handle, &doSomethingCmd);
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- WHEN("Invalid CRC is passed")
- {
- /* check with invalid payload */
- shellmatta_processData(handle, (char*)"\x01\x01\x00\x16\x00\x00\x00\x00"
- "doSomething argument\r\n"
- "\x00\x00\x00\x00", 34u);
- THEN("Shellmatta responds with CRC error")
- {
- char *dummyData = (char*)"crc error\r\n\r\nshellmatta->";
- CHECK(write_length == strlen(dummyData));
- REQUIRE(strcmp(dummyData, write_data) == 0);
- }
- }
- WHEN("Valid CRC is passed")
- {
- /* check with valid payload - disable echo to reduce cluttering */
- shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
- shellmatta_processData(handle, (char*)"\x01\x01\x00\x16\x00\x00\x00\x00"
- "doSomething argument\r\n"
- "\x7b\x49\xfa\x72", 34u);
- THEN("The shellmatta responds to the command")
- {
- char *dummyData = (char*)"\x01\x01\x00\x2f\x00\x01\x01\x01"
- "doSomething argument - length: 20"
- "\r\n"
- "shellmatta->"
- "\x4c\x9f\xd9\xa7";
- CHECK(write_length == 59u);
- REQUIRE(memcmp(write_data, dummyData, 59) == 0);
- }
- }
- WHEN("Large payload is written by shellmatta in steps")
- {
- /* add large payload command */
- shellmatta_addCmd(handle, &largePayloadCmd);
- /* check with valid payload - disable echo to reduce cluttering */
- shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
- shellmatta_processData(handle, (char*)"\x01\x01\x00\x0e\x00\x00\x00\x00"
- "largePayload\r\n"
- "\xad\x33\x31\xcc", 26u);
- THEN("The shellmatta responds multiple telegrams")
- {
- char *dummyData = (char*)"\x01\x01\x00\xff\x00\x01\x01\x01"
- "This is my very very very very large payload\r\n"
- "This is my very very very very large payload\r\n"
- "This is my very very very very large payload\r\n"
- "This is my very very very very large payload\r\n"
- "This is my very very very very large payload\r\n"
- "This is my very very very"
- "\x00\x9c\x41\xe1"
- "\x01\x01\x00\x7f\x00\x01\x01\x02"
- " very large payload\r\n"
- "This is my very very very very large payload\r\n"
- "This is my very very very very large payload\r\n"
- "\r\n"
- "shellmatta->"
- "\x39\x75\x0d\x6b";
- CHECK(write_length == 406u);
- REQUIRE(memcmp(write_data, dummyData, 406u) == 0);
- }
- }
- WHEN("Large payload is written by shellmatta at once")
- {
- /* add large payload command */
- shellmatta_addCmd(handle, &largePayloadAtOnceCmd);
- /* check with valid payload - disable echo to reduce cluttering */
- shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
- shellmatta_processData(handle, (char*)"\x01\x01\x00\x0e\x00\x00\x00\x00"
- "largePayload\r\n"
- "\xad\x33\x31\xcc", 26u);
- THEN("The shellmatta responds multiple telegrams")
- {
- char *dummyData = (char*)"\x01\x01\x00\xff\x00\x01\x01\x01"
- "This is my very very very very large payload\r\n"
- "This is my very very very very large payload\r\n"
- "This is my very very very very large payload\r\n"
- "This is my very very very very large payload\r\n"
- "This is my very very very very large payload\r\n"
- "This is my very very very"
- "\x00\x9c\x41\xe1"
- "\x01\x01\x00\x7f\x00\x01\x01\x02"
- " very large payload\r\n"
- "This is my very very very very large payload\r\n"
- "This is my very very very very large payload\r\n"
- "\r\n"
- "shellmatta->"
- "\x39\x75\x0d\x6b";
- CHECK(write_length == 406u);
- REQUIRE(memcmp(write_data, dummyData, 406u) == 0);
- }
- }
- WHEN("manual mode is used after transport layer mode")
- {
- /* check with valid payload - disable echo to reduce cluttering */
- shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
- shellmatta_processData(handle, (char*)"\x01\x01\x00\x16\x00\x00\x00\x00"
- "doSomething argument\r\n"
- "\x7b\x49\xfa\x72", 34u);
- shellmatta_processData(handle, (char*)"doSomething argument\r\n", 22u);
- THEN("The shellmatta responds to the command")
- {
- char *dummyData = (char*)"\x01\x01\x00\x2f\x00\x01\x01\x01"
- "doSomething argument - length: 20"
- "\r\n"
- "shellmatta->"
- "\x4c\x9f\xd9\xa7"
- "doSomething argument - length: 20\r\n"
- "shellmatta->";
- CHECK(write_length == 106);
- REQUIRE(memcmp(write_data, dummyData, 106) == 0);
- }
- }
- WHEN("disabling auto flush")
- {
- /* check with valid payload - disable echo to reduce cluttering */
- shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
- shellmatta_transport_configure(handle, false, true, NULL, NULL);
- shellmatta_processData(handle, (char*)"\x01\x01\x00\x16\x00\x00\x00\x00"
- "doSomething argument\r\n"
- "\x7b\x49\xfa\x72", 34u);
- THEN("The Shellmatta does not respond")
- {
- CHECK(write_length == 0u);
- AND_WHEN("The flush method is called")
- {
- shellmatta_transport_flush(handle);
- THEN("The shellmatta returns the data")
- {
- char *dummyData = (char*)"\x01\x01\x00\x2f\x00\x01\x01\x01"
- "doSomething argument - length: 20"
- "\r\n"
- "shellmatta->"
- "\x4c\x9f\xd9\xa7";
- CHECK(write_length == 59u);
- REQUIRE(memcmp(write_data, dummyData, 59) == 0);
- }
- }
- }
- }
- WHEN("Sequence counter is requested")
- {
- /* request sequence counter */
- shellmatta_processData(handle, (char*)"\x01\x01\x01\x00\x00\x00\x00\x00"
- "\xc4\xa3\x07\xe6", 12u);
- THEN("The valid Sequence counter is returned")
- {
- char *dummyData = (char*)"\x01\x01\x81\x00\x00\x01\x01\x01"
- "\xb5\xcd\x78\xde";
- CHECK(write_length == 12);
- REQUIRE(memcmp(write_data, dummyData, 12) == 0);
- }
- AND_WHEN("Sequence counter is requested again")
- {
- /* request sequence counter again */
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- shellmatta_processData(handle, (char*)"\x01\x01\x01\x00\x00\x00\x00\x00"
- "\xc4\xa3\x07\xe6", 12u);
- THEN("The next Sequence counter is returned")
- {
- char *dummyData = (char*)"\x01\x01\x81\x00\x00\x01\x02\x02"
- "\x07\xe9\x7a\xa7";
- CHECK(write_length == 12);
- REQUIRE(memcmp(write_data, dummyData, 12) == 0);
- }
- }
- }
- }
- }
- SCENARIO("Integration test of Transport layer with mandatory transport layer", "[integration, transport]")
- {
- GIVEN("Shellmatta up and running with one command")
- {
- shellmatta_instance_t inst;
- shellmatta_handle_t handle;
- shellmatta_retCode_t ret;
- char buffer[1024];
- char historyBuffer[1024];
- shellmatta_doInit( &inst,
- &handle,
- buffer,
- sizeof(buffer),
- historyBuffer,
- sizeof(historyBuffer),
- "shellmatta->",
- NULL,
- writeFct);
- shellmatta_addCmd(handle, &doSomethingCmd);
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- WHEN("The tansport layer is set to mandatory") {
- ret = shellmatta_transport_configure(handle, true, false, NULL, NULL);
- CHECK(ret == SHELLMATTA_OK);
- AND_WHEN("manual mode is used after transport layer mode")
- {
- /* check with valid payload - disable echo to reduce cluttering */
- shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
- shellmatta_processData(handle, (char*)"\x01\x01\x00\x16\x00\x00\x00\x00"
- "doSomething argument\r\n"
- "\x7b\x49\xfa\x72", 34u);
- shellmatta_processData(handle, (char*)"doSomething argument\r\n", 22u);
- THEN("The shellmatta responds only to the first command - and waits for a new telegram to start")
- {
- char *dummyData = (char*)"\x01\x01\x00\x2f\x00\x01\x01\x01"
- "doSomething argument - length: 20"
- "\r\n"
- "shellmatta->"
- "\x4c\x9f\xd9\xa7";
- CHECK(write_length == 59);
- CHECK(memcmp(write_data, dummyData, write_length) == 0);
- REQUIRE(inst.transportLayer.state == SHELLMATTA_TRANSPORT_STATE_WAIT);
- }
- }
- }
- }
- }
- SCENARIO("Integration test of Transport layer - reset transport layer", "[integration, transport]")
- {
- GIVEN("Shellmatta up and running with one command")
- {
- shellmatta_instance_t inst;
- shellmatta_handle_t handle;
- shellmatta_retCode_t ret;
- char buffer[1024];
- char historyBuffer[1024];
- shellmatta_doInit( &inst,
- &handle,
- buffer,
- sizeof(buffer),
- historyBuffer,
- sizeof(historyBuffer),
- "shellmatta->",
- NULL,
- writeFct);
- shellmatta_addCmd(handle, &doSomethingCmd);
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- WHEN("The tansport layer is set to mandatory") {
- ret = shellmatta_transport_configure(handle, true, false, NULL, NULL);
- CHECK(ret == SHELLMATTA_OK);
- AND_WHEN("A partial telegram is passed")
- {
- /* check with valid payload - disable echo to reduce cluttering */
- shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
- shellmatta_processData(handle, (char*)"\x01\x01\x00\x16\x00\x00\x00\x00"
- "doSomething argument", 28u);
- THEN("The transport layer is in state SHELLMATTA_TRANSPORT_STATE_GET_PAYLOAD")
- {
- CHECK(write_length == 0);
- CHECK_THAT(inst.transportLayer.inPacket.payload, Catch::Matchers::Equals("doSomething argument"));
- REQUIRE(inst.transportLayer.state == SHELLMATTA_TRANSPORT_STATE_GET_PAYLOAD);
- AND_WHEN("The transport layer is resetted")
- {
- ret = shellmatta_transport_reset(handle);
- CHECK(ret == SHELLMATTA_OK);
- THEN("The transport layer is in state wait again and no payload is set") {
- CHECK(write_length == 0);
- CHECK(inst.transportLayer.inPacket.payload[0] == '\0');
- REQUIRE(inst.transportLayer.state == SHELLMATTA_TRANSPORT_STATE_WAIT);
- }
- }
- }
- }
- }
- }
- }
- SCENARIO("Integration test of Transport layer - addressing", "[integration, transport]")
- {
- GIVEN("Shellmatta up and running with one command - transport layer with UUID")
- {
- shellmatta_instance_t inst;
- shellmatta_handle_t handle;
- shellmatta_retCode_t ret;
- char buffer[1024];
- char historyBuffer[1024];
- shellmatta_doInit( &inst,
- &handle,
- buffer,
- sizeof(buffer),
- historyBuffer,
- sizeof(historyBuffer),
- "shellmatta->",
- NULL,
- writeFct);
- shellmatta_addCmd(handle, &doSomethingCmd);
- ret = shellmatta_transport_configure(handle, true, false, (uint8_t *)"123456789012345", NULL);
- CHECK(ret == SHELLMATTA_OK);
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- WHEN("The shellmatta is searched using an invalid range") {
- ret = shellmatta_transport_configure(handle, true, false, NULL, NULL);
- /* check with valid payload - disable echo to reduce cluttering */
- shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
- shellmatta_processData(handle, (char*)"\x01\x01\x03\x20\x00\x00\x00\x00"
- "234567890123456\0"
- "345678901234567\0"
- "\x93\x0c\xc3\x04", 44u);
- THEN("The shellmatta does not respond")
- {
- CHECK(write_length == 0);
- CHECK(inst.transportLayer.sequenceH2S == 1);
- REQUIRE(inst.transportLayer.state == SHELLMATTA_TRANSPORT_STATE_WAIT);
- }
- }
- WHEN("The shellmatta is searched using a valid range") {
- ret = shellmatta_transport_configure(handle, true, false, NULL, NULL);
- /* check with valid payload - disable echo to reduce cluttering */
- shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
- shellmatta_processData(handle, (char*)"\x01\x01\x03\x20\x00\x00\x00\x00"
- "123456789012344\0"
- "345678901234567\0"
- "\x9b\x69\x59\x5f", 44u);
- THEN("The shellmatta does not respond")
- {
- CHECK(write_length == 28);
- CHECK(inst.transportLayer.sequenceH2S == 1);
- REQUIRE(inst.transportLayer.state == SHELLMATTA_TRANSPORT_STATE_WAIT);
- }
- }
- }
- }
|