123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- /*
- * Copyright (c) 2019 - 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_utils_insertChars.c
- * @brief unittest for shellmatta utils_insertChars
- * @author Stefan Strobel <stefan.strobel@shimatta.net>
- */
- #include "test/framework/catch.hpp"
- #include "src/shellmatta_utils.c"
- #include <string.h>
- static uint32_t write_callCnt = 0u;
- static char write_data[20];
- static uint32_t write_idx;
- static shellmatta_retCode_t writeFct(const char* data, uint32_t length)
- {
- write_callCnt ++;
- strncpy(&write_data[write_idx], data, length);
- write_idx += length;
- return SHELLMATTA_OK;
- }
- TEST_CASE( "shellmatta_insertChars normal call" ) {
- shellmatta_instance_t inst;
- char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
- memset(&inst, 0, sizeof(inst));
- inst.buffer = buffer;
- inst.bufferSize = 20;
- inst.cursor = 8;
- inst.inputCount = 10;
- inst.echoEnabled = true;
- inst.write = writeFct;
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_idx = 0u;
- utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u);
- CHECK( inst.cursor == 12);
- CHECK( inst.inputCount == 14);
- CHECK( write_callCnt == 5u );
- CHECK( strncmp("blks\x1b[K\x1b[sij\x1b[u", write_data, 15u) == 0);
- REQUIRE( strncmp("abcdefghblksij\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);
- }
- TEST_CASE( "shellmatta_insertChars overwrite" ) {
- shellmatta_instance_t inst;
- char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
- memset(&inst, 0, sizeof(inst));
- inst.buffer = buffer;
- inst.bufferSize = 20;
- inst.cursor = 8;
- inst.inputCount = 10;
- inst.echoEnabled = true;
- inst.mode = SHELLMATTA_MODE_OVERWRITE;
- inst.write = writeFct;
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_idx = 0u;
- utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u);
- CHECK( inst.cursor == 12);
- CHECK( inst.inputCount == 12);
- CHECK( write_callCnt == 1u );
- CHECK( strncmp("blks", write_data, 5u) == 0);
- REQUIRE( strncmp("abcdefghblks\0\0\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);
- }
- TEST_CASE( "shellmatta_insertChars append" ) {
- shellmatta_instance_t inst;
- char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
- memset(&inst, 0, sizeof(inst));
- inst.buffer = buffer;
- inst.bufferSize = 20;
- inst.cursor = 10;
- inst.inputCount = 10;
- inst.echoEnabled = true;
- inst.mode = SHELLMATTA_MODE_INSERT;
- inst.write = writeFct;
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_idx = 0u;
- utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 4u);
- CHECK( inst.cursor == 14);
- CHECK( inst.inputCount == 14);
- CHECK( write_callCnt == 1u );
- CHECK( strncmp("blks", write_data, 5u) == 0);
- REQUIRE( strncmp("abcdefghijblks\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);
- }
- TEST_CASE( "shellmatta_insertChars 0 length" ) {
- shellmatta_instance_t inst;
- char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
- memset(&inst, 0, sizeof(inst));
- inst.buffer = buffer;
- inst.bufferSize = 20;
- inst.cursor = 8;
- inst.inputCount = 10;
- inst.echoEnabled = true;
- inst.write = writeFct;
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_idx = 0u;
- utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 0u);
- CHECK( inst.cursor == 8u );
- CHECK( inst.inputCount == 10u );
- CHECK( write_callCnt == 0u );
- CHECK( memcmp("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", write_data, sizeof(write_data) ) == 0u );
- REQUIRE( memcmp("abcdefghij\0\0\0\0\0\0\0\0\0", buffer, sizeof(buffer)) == 0);
- }
- TEST_CASE( "shellmatta_insertChars buffer full" ) {
- shellmatta_instance_t inst;
- char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
- memset(&inst, 0, sizeof(inst));
- inst.buffer = buffer;
- inst.bufferSize = 20;
- inst.cursor = 8;
- inst.inputCount = 10;
- inst.echoEnabled = true;
- inst.write = writeFct;
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_idx = 0u;
- utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 10u);
- CHECK( inst.cursor == 18u );
- CHECK( inst.inputCount == 20u );
- CHECK( write_callCnt == 5u );
- CHECK( memcmp("blksdflsd ", write_data, 10u ) == 0u );
- REQUIRE( memcmp("abcdefghblksdflsd ij", buffer, sizeof(buffer)) == 0);
- }
- TEST_CASE( "shellmatta_insertChars buffer overflow by 1" ) {
- shellmatta_instance_t inst;
- char buffer[20] = "abcdefghij\0\0\0\0\0\0\0\0\0";
- memset(&inst, 0, sizeof(inst));
- inst.buffer = buffer;
- inst.bufferSize = 20;
- inst.cursor = 8;
- inst.inputCount = 10;
- inst.echoEnabled = true;
- inst.write = writeFct;
- write_callCnt = 0u;
- memset(write_data, 0, sizeof(write_data));
- write_idx = 0u;
- utils_insertChars(&inst, (char*)"blksdflsd kfjlk", 11u);
- CHECK( inst.cursor == 18u );
- CHECK( inst.inputCount == 20u );
- CHECK( write_callCnt == 5u );
- CHECK( memcmp("blksdflsd ", write_data, 10u ) == 0u );
- REQUIRE( memcmp("abcdefghblksdflsd ij", buffer, sizeof(buffer)) == 0);
- }
|