/* * Copyright (c) 2019 - 2024 Stefan Strobel * * 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 */ #include "test/framework/catch.hpp" #include "src/shellmatta_opt.c" #include 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 ); }