123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- /*
- * Copyright (c) 2021 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.cpp
- * @brief integration test implementation for some general functions
- * @author Stefan Strobel <stefan.strobel@shimatta.net>
- */
- #include "test/framework/catch.hpp"
- extern "C" {
- #include "shellmatta.h"
- }
- #include <string.h>
- using Catch::Matchers::Equals;
- 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 empty(shellmatta_handle_t handle, const char *arguments, uint32_t length)
- {
- shellmatta_printf(handle, "empty - %s - length: %u", arguments, length);
- return SHELLMATTA_OK;
- }
- shellmatta_cmd_t emptyCmd = {(char*)"empty", NULL, NULL, NULL, empty, NULL};
- TEST_CASE( "shellmatta empty function" ) {
- 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);
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- shellmatta_processData(handle, (char*)"\r", 1);
- CHECK( write_length == 14u);
- REQUIRE_THAT( write_data, Equals("\r\nshellmatta->") );
- }
- TEST_CASE( "shellmatta heredoc test" ) {
- 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);
- doSomethingArguments = NULL;
- doSomethingLength = 0u;
- shellmatta_processData(handle, (char*)"do this << EOF\r\n"
- "asdf\r\n"
- "1234\r\n"
- "EOF\r\n"
- , 33);
- CHECK( doSomethingStdinLength == 10u);
- CHECK_THAT( doSomethingStdin, Equals("asdf\r\n1234") );
- CHECK( doSomethingLength == 8u);
- REQUIRE_THAT( doSomethingArguments, Equals("do this ") );
- }
- TEST_CASE( "shellmatta heredoc test empty" ) {
- 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);
- doSomethingArguments = NULL;
- doSomethingLength = 0u;
- shellmatta_processData(handle, (char*)"do this << EOF\r\n"
- "EOF\r\n"
- , 21);
- CHECK( doSomethingStdinLength == 0u);
- CHECK( NULL == doSomethingStdin );
- CHECK( doSomethingLength == 8u);
- REQUIRE_THAT( doSomethingArguments, Equals("do this ") );
- }
- TEST_CASE( "shellmatta remove function" ) {
- shellmatta_instance_t inst;
- shellmatta_handle_t handle;
- char buffer[1024];
- char historyBuffer[1024];
- char *dummyData = (char*)"?\r\n"
- "doSomething do Function does something\r\n"
- "help ? help [command] - print help or usage information\r\n"
- "\r\nshellmatta->";
- 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;
- shellmatta_processData(handle, (char*)"?\r", 2);
- CHECK( write_length == strlen(dummyData));
- CHECK_THAT( write_data, Equals(dummyData) );
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- dummyData = (char*)"? 564 321 56 465 46\r\n"
- "doSomething do Function does something\r\n"
- "help ? help [command] - print help or usage information\r\n"
- "\r\nshellmatta->";
- shellmatta_processData(handle, (char*)"? 564 321 56 465 46\r", 20);
- CHECK( write_length == strlen(dummyData));
- CHECK( strcmp(dummyData, write_data) == 0);
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- shellmatta_removeCmd(handle, &doSomethingCmd);
- shellmatta_processData(handle, (char*)"? 564 321 56 465 46\r", 20);
- dummyData = (char*)"? 564 321 56 465 46\r\n"
- "help ? help [command] - print help or usage information\r\n"
- "\r\nshellmatta->";
- CHECK( write_length == strlen(dummyData));
- REQUIRE( strcmp(dummyData, write_data) == 0);
- }
- TEST_CASE( "shellmatta reset no prompt" ) {
- shellmatta_instance_t inst;
- shellmatta_handle_t handle;
- char buffer[1024];
- char historyBuffer[1024];
- char *dummyData = (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh glkd?\r\n"
- "doSomething do Function does something\r\n"
- "help ? help [command] - print help or usage information\r\n"
- "\r\nshellmatta->";
- 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;
- shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh glkd", 35);
- shellmatta_resetShell(handle, false);
- shellmatta_processData(handle, (char*)"?\r", 2);
- CHECK( write_length == strlen(dummyData));
- REQUIRE( strcmp(dummyData, write_data) == 0);
- }
- TEST_CASE( "shellmatta reset with prompt" ) {
- shellmatta_instance_t inst;
- shellmatta_handle_t handle;
- char buffer[1024];
- char historyBuffer[1024];
- char *dummyData = (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh glkd"
- "\r\nshellmatta->?\r\n"
- "doSomething do Function does something\r\n"
- "help ? help [command] - print help or usage information\r\n"
- "\r\nshellmatta->";
- 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;
- shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh glkd", 35);
- shellmatta_resetShell(handle, true);
- shellmatta_processData(handle, (char*)"?\r", 2);
- CHECK( write_length == strlen(dummyData));
- REQUIRE( strcmp(dummyData, write_data) == 0);
- }
- TEST_CASE( "shellmatta reset no prompt history buffer" ) {
- shellmatta_instance_t inst;
- shellmatta_handle_t handle;
- char buffer[1024];
- char historyBuffer[1024];
- char *dummyData = (char*)"?\r\n"
- "doSomething do Function does something\r\n"
- "help ? help [command] - print help or usage information\r\n"
- "\r\nshellmatta->";
- shellmatta_doInit( &inst,
- &handle,
- buffer,
- sizeof(buffer),
- historyBuffer,
- sizeof(historyBuffer),
- "shellmatta->",
- NULL,
- writeFct);
- shellmatta_addCmd(handle, &doSomethingCmd);
- shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh gl\r\n", 35u);
- shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh gl\r\n", 35u);
- shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh gl\r\n", 35u);
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- shellmatta_resetShell(handle, false);
- /* process arrow key up - if reset worked this should deliver nothing */
- shellmatta_processData(handle, (char*)"\033[A", 3u);
- shellmatta_processData(handle, (char*)"?\r", 2);
- CHECK( write_length == strlen(dummyData));
- REQUIRE( strcmp(dummyData, write_data) == 0);
- }
- TEST_CASE( "shellmatta reset no prompt heredoc" ) {
- shellmatta_instance_t inst;
- shellmatta_handle_t handle;
- char buffer[1024];
- char historyBuffer[1024];
- char *dummyData = (char*)"?\r\n"
- "doSomething do Function does something\r\n"
- "help ? help [command] - print help or usage information\r\n"
- "\r\nshellmatta->";
- shellmatta_doInit( &inst,
- &handle,
- buffer,
- sizeof(buffer),
- historyBuffer,
- sizeof(historyBuffer),
- "shellmatta->",
- NULL,
- writeFct);
- shellmatta_addCmd(handle, &doSomethingCmd);
- shellmatta_processData(handle, (char*)"dummy cmd << EOF\r\n", 18u);
- shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh gl\r\n", 35u);
- shellmatta_processData(handle, (char*)"dkfg hdlsfkgh ldksfjhg lkdjfsh gl\r\n", 35u);
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- /* end the heredoc session by resetting the shell */
- shellmatta_resetShell(handle, false);
- /* now the new command should be processed */
- shellmatta_processData(handle, (char*)"?\r", 2);
- CHECK( write_length == strlen(dummyData));
- REQUIRE( strcmp(dummyData, write_data) == 0);
- }
- TEST_CASE( "shellmatta configure disable echo" ) {
- shellmatta_instance_t inst;
- shellmatta_handle_t handle;
- shellmatta_retCode_t ret;
- char buffer[1024];
- char historyBuffer[1024];
- char *dummyData = (char*)"help this is some dummy Text\r\n"
- "doSomething do Function does something\r\n"
- "help ? help [command] - print help or usage information\r\n"
- "\r\nshellmatta->";
- char *dummyData2 = (char*)"doSomething do Function does something\r\n"
- "help ? help [command] - print help or usage information\r\n"
- "\r\nshellmatta->";
- 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;
- /* check with echo enabled */
- shellmatta_processData(handle, (char*)"help this is some dummy Text\r\n", 30u);
- CHECK( write_length == strlen(dummyData));
- CHECK( strcmp(dummyData, write_data) == 0);
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- /* turn off echo now */
- ret = shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
- CHECK( ret == SHELLMATTA_OK );
- /* check with echo disabled */
- shellmatta_processData(handle, (char*)"help this is some dummy Text\r\n", 30u);
- CHECK( write_length == strlen(dummyData2));
- REQUIRE( strcmp(dummyData2, write_data) == 0);
- }
- TEST_CASE( "shellmatta configure mode" ) {
- shellmatta_instance_t inst;
- shellmatta_handle_t handle;
- shellmatta_retCode_t ret;
- char buffer[1024];
- char historyBuffer[1024];
- char *dummyData = (char*)"\r\nCommand: meow this is some dum123456my Text not found\r\nshellmatta->";
- char *dummyData2 = (char*)"\r\nCommand: meow this is some dum123456t not found\r\nshellmatta->";
- shellmatta_doInit( &inst,
- &handle,
- buffer,
- sizeof(buffer),
- historyBuffer,
- sizeof(historyBuffer),
- "shellmatta->",
- NULL,
- writeFct);
- shellmatta_addCmd(handle, &doSomethingCmd);
- ret = shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- /* check with insert mode */
- shellmatta_processData(handle, (char*)"meow this is some dummy Text\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D123456\r\n", 57u);
- CHECK( write_length == strlen(dummyData));
- CHECK( strcmp(dummyData, write_data) == 0);
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- /* check with overwrite mode */
- ret = shellmatta_configure(handle, SHELLMATTA_MODE_OVERWRITE, false, '\r');
- CHECK( ret == SHELLMATTA_OK );
- /* check with echo disabled */
- shellmatta_processData(handle, (char*)"meow this is some dummy Text\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D123456\r\n", 57u);
- CHECK( write_length == strlen(dummyData2));
- REQUIRE( strcmp(dummyData2, write_data) == 0);
- }
- TEST_CASE( "shellmatta configure delimiter" ) {
- shellmatta_instance_t inst;
- shellmatta_handle_t handle;
- shellmatta_retCode_t ret;
- char buffer[1024];
- char historyBuffer[1024];
- char *dummyData = (char*)"doSomething argument - length: 20\r\nshellmatta->";
- shellmatta_doInit( &inst,
- &handle,
- buffer,
- sizeof(buffer),
- historyBuffer,
- sizeof(historyBuffer),
- "shellmatta->",
- NULL,
- writeFct);
- shellmatta_addCmd(handle, &doSomethingCmd);
- ret = shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- /* check with insert mode */
- shellmatta_processData(handle, (char*)"doSomething argument\n", 21u);
- CHECK( write_length == 0u);
- shellmatta_resetShell(handle, false);
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_length = 0u;
- /* check with changed delimiter mode */
- ret = shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\n');
- CHECK( ret == SHELLMATTA_OK );
- /* check with echo disabled */
- shellmatta_processData(handle, (char*)"doSomething argument\n", 21u);
- CHECK( write_length == strlen(dummyData));
- REQUIRE( strcmp(dummyData, write_data) == 0);
- }
|