123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- /*
- * 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_opt_findNextHunk.c
- * @brief unittest for shellmatta opt_findNextHunk
- * @author Stefan Strobel <stefan.strobel@shimatta.net>
- */
- #include "test/framework/catch.hpp"
- #include "src/shellmatta_opt.c"
- #include <string.h>
- TEST_CASE( "shellmatta_opt findNextHunk easy" ) {
- shellmatta_retCode_t ret = SHELLMATTA_OK;
- shellmatta_instance_t inst;
- char *dummyData = (char*) "This is Sparta";
- char buffer[1024u];
- memcpy(buffer, dummyData, strlen(dummyData));
- inst.buffer = buffer;
- inst.bufferSize = sizeof(buffer);
- inst.inputCount = 14;
- inst.optionParser.nextOffset = 4u;
- ret = findNextHunk(&inst);
- CHECK( ret == SHELLMATTA_OK );
- CHECK( inst.optionParser.offset == 5u);
- CHECK( inst.optionParser.len == 2u);
- ret = findNextHunk(&inst);
- CHECK( ret == SHELLMATTA_OK );
- CHECK( inst.optionParser.offset == 8u);
- CHECK( inst.optionParser.len == 6u);
- ret = findNextHunk(&inst);
- REQUIRE( ret == SHELLMATTA_ERROR );
- }
- TEST_CASE( "shellmatta_opt findNextHunk quotation 1" ) {
- shellmatta_retCode_t ret = SHELLMATTA_OK;
- shellmatta_instance_t inst;
- char *dummyData = (char*) "This is Sparta \"argument with spaces\"";
- char buffer[1024u];
- memcpy(buffer, dummyData, strlen(dummyData));
- inst.buffer = buffer;
- inst.bufferSize = sizeof(buffer);
- inst.inputCount = strlen(dummyData);
- inst.optionParser.nextOffset = 4u;
- ret = findNextHunk(&inst);
- CHECK( ret == SHELLMATTA_OK );
- CHECK( inst.optionParser.offset == 5u);
- CHECK( inst.optionParser.len == 2u);
- ret = findNextHunk(&inst);
- CHECK( ret == SHELLMATTA_OK );
- CHECK( inst.optionParser.offset == 8u);
- CHECK( inst.optionParser.len == 6u);
- ret = findNextHunk(&inst);
- CHECK( ret == SHELLMATTA_OK );
- CHECK( inst.optionParser.offset == 15u);
- CHECK( inst.optionParser.len == 20u);
- CHECK( 0 == memcmp(&(inst.buffer[inst.optionParser.offset]), "argument with spaces", 20));
- ret = findNextHunk(&inst);
- REQUIRE( ret == SHELLMATTA_ERROR );
- }
- TEST_CASE( "shellmatta_opt findNextHunk quotation 2" ) {
- shellmatta_retCode_t ret = SHELLMATTA_OK;
- shellmatta_instance_t inst;
- char *dummyData = (char*) "This is Sparta 'argument with spaces'";
- char buffer[1024u];
- memcpy(buffer, dummyData, strlen(dummyData));
- inst.buffer = buffer;
- inst.bufferSize = sizeof(buffer);
- inst.inputCount = strlen(dummyData);
- inst.optionParser.nextOffset = 4u;
- ret = findNextHunk(&inst);
- CHECK( ret == SHELLMATTA_OK );
- CHECK( inst.optionParser.offset == 5u);
- CHECK( inst.optionParser.len == 2u);
- ret = findNextHunk(&inst);
- CHECK( ret == SHELLMATTA_OK );
- CHECK( inst.optionParser.offset == 8u);
- CHECK( inst.optionParser.len == 6u);
- ret = findNextHunk(&inst);
- CHECK( ret == SHELLMATTA_OK );
- CHECK( inst.optionParser.offset == 15u);
- CHECK( inst.optionParser.len == 20u);
- CHECK( 0 == memcmp(&(inst.buffer[inst.optionParser.offset]), "argument with spaces", 20));
- ret = findNextHunk(&inst);
- REQUIRE( ret == SHELLMATTA_ERROR );
- }
- TEST_CASE( "shellmatta_opt findNextHunk quotation escaped" ) {
- shellmatta_retCode_t ret = SHELLMATTA_OK;
- shellmatta_instance_t inst;
- char *dummyData = (char*) "This is Sparta \"argument with \\\"spaces\"";
- char buffer[1024u];
- memcpy(buffer, dummyData, strlen(dummyData));
- inst.buffer = buffer;
- inst.bufferSize = sizeof(buffer);
- inst.inputCount = strlen(dummyData);
- inst.optionParser.nextOffset = 4u;
- ret = findNextHunk(&inst);
- CHECK( ret == SHELLMATTA_OK );
- CHECK( inst.optionParser.offset == 5u);
- CHECK( inst.optionParser.len == 2u);
- ret = findNextHunk(&inst);
- CHECK( ret == SHELLMATTA_OK );
- CHECK( inst.optionParser.offset == 8u);
- CHECK( inst.optionParser.len == 6u);
- ret = findNextHunk(&inst);
- CHECK( ret == SHELLMATTA_OK );
- CHECK( inst.optionParser.offset == 15u);
- CHECK( inst.optionParser.len == 21u);
- CHECK( 0 == memcmp(&(inst.buffer[inst.optionParser.offset]), "argument with \"spaces", 21));
- ret = findNextHunk(&inst);
- REQUIRE( ret == SHELLMATTA_ERROR );
- }
- TEST_CASE( "shellmatta_opt findNextHunk quotation missing closing quotation" ) {
- shellmatta_retCode_t ret = SHELLMATTA_OK;
- shellmatta_instance_t inst;
- char *dummyData = (char*) "This is Sparta \"argument with \\\"spaces";
- char buffer[1024u];
- memcpy(buffer, dummyData, strlen(dummyData));
- inst.buffer = buffer;
- inst.bufferSize = sizeof(buffer);
- inst.inputCount = strlen(dummyData);
- inst.optionParser.nextOffset = 4u;
- ret = findNextHunk(&inst);
- CHECK( ret == SHELLMATTA_OK );
- CHECK( inst.optionParser.offset == 5u);
- CHECK( inst.optionParser.len == 2u);
- ret = findNextHunk(&inst);
- CHECK( ret == SHELLMATTA_OK );
- CHECK( inst.optionParser.offset == 8u);
- CHECK( inst.optionParser.len == 6u);
- ret = findNextHunk(&inst);
- REQUIRE( ret == SHELLMATTA_ERROR );
- }
|