diff --git a/source/sigv4.c b/source/sigv4.c index 0d039f93..b5867b46 100644 --- a/source/sigv4.c +++ b/source/sigv4.c @@ -2030,7 +2030,7 @@ static void generateCredentialScope( const SigV4Parameters_t * pSigV4Params, returnStatus = setQueryStringFieldsAndValues( pQuery, queryLen, &numberOfParameters, pCanonicalContext ); - if( returnStatus == SigV4Success ) + if( ( returnStatus == SigV4Success ) && ( numberOfParameters > 0U ) ) { /* Sort the parameter names by character code point in ascending order. * Parameters with duplicate names should be sorted by value. */ diff --git a/source/sigv4_quicksort.c b/source/sigv4_quicksort.c index 1618e468..f117b269 100644 --- a/source/sigv4_quicksort.c +++ b/source/sigv4_quicksort.c @@ -237,8 +237,10 @@ void quickSort( void * pArray, size_t itemSize, ComparisonFunc_t comparator ) { - if( ( numItems != 0U ) && ( pArray != NULL ) ) - { - quickSortHelper( pArray, 0U, numItems - 1U, itemSize, comparator ); - } + assert( pArray != NULL ); + assert( numItems > 0U ); + assert( itemSize > 0U ); + assert( comparator != NULL ); + + quickSortHelper( pArray, 0U, numItems - 1U, itemSize, comparator ); } diff --git a/test/unit-test/sigv4_config.h b/test/unit-test/sigv4_config.h index ea2f32b7..d5eeae7b 100644 --- a/test/unit-test/sigv4_config.h +++ b/test/unit-test/sigv4_config.h @@ -90,7 +90,7 @@ * Default value: `100` */ #ifndef SIGV4_MAX_HTTP_HEADER_COUNT - #define SIGV4_MAX_HTTP_HEADER_COUNT 5U + #define SIGV4_MAX_HTTP_HEADER_COUNT 7U #endif /** diff --git a/test/unit-test/sigv4_utest.c b/test/unit-test/sigv4_utest.c index 499a4120..9dbd0f4a 100644 --- a/test/unit-test/sigv4_utest.c +++ b/test/unit-test/sigv4_utest.c @@ -82,14 +82,18 @@ #define HEADERS_WITH_TRIMMABLE_SPACES " Header-1 : Value \t - \t 1 \r\n" /* Examples with more header pairs than SIGV4_MAX_HTTP_HEADER_COUNT=5. */ -#define PRECAN_HEADERS_PAIRS_GT_THAN_MAX "H1:a\nH2:b\nH3:c\nH4:d\nH5:e\nH6:\n" -#define HEADERS_PAIRS_GT_THAN_MAX "H1:a\r\nH2:b\r\nH3:c\r\nH4:d\r\nH5:e\r\nH6:\r\n\r\n" +#define PRECAN_HEADERS_PAIRS_GT_THAN_MAX "H1:a\nH2:b\nH3:c\nH4:d\nH5:e\nH6:\nH7:\nH8:\n" +#define HEADERS_PAIRS_GT_THAN_MAX "H1:a\r\nH2:b\r\nH3:c\r\nH4:d\r\nH5:e\r\nH6:\r\nH7:\r\n:H8:\r\n\r\n" /* Examples of invalid HTTP headers data. */ #define INVALID_HEADERS_NO_HEADER_VAL "Header1: Value1\r\nHeader2: Value2\n" #define INVALID_HEADERS_NO_HEADER_KEY "Header=Value\r\n" #define INVALID_PRECANON_HEADERS_NO_HEADER_KEY "Header=Value\n" +#define HEADERS_SORTED_COVERAGE_1 "A:a\r\nB:b\r\nC:c\r\nE:e\r\nF:f\r\nD:d\r\n\r\n" +#define HEADERS_SORTED_COVERAGE_2 "A:a\r\nC:c\r\nE:e\r\nF:f\r\nD:d\r\n\r\n" + + #define STRING_TO_SIGN_LEN_WITH_DEFAULT_REGION \ SIGV4_AWS4_HMAC_SHA256_LENGTH + 1U + \ SIGV4_ISO_STRING_LEN + 1U + \ @@ -1233,3 +1237,20 @@ void test_SigV4_GenerateAuthorization_Headers_Greater_Than_Configured_Max() TEST_ASSERT_EQUAL( SigV4MaxHeaderPairCountExceeded, SigV4_GenerateHTTPAuthorization( ¶ms, authBuf, &authBufLen, &signature, &signatureLen ) ); } + +void test_SigV4_GenerateAuthorization_Sorting_Algorithm_Coverage() +{ + params.pHttpParameters->pHeaders = HEADERS_SORTED_COVERAGE_1; + params.pHttpParameters->headersLen = strlen( HEADERS_SORTED_COVERAGE_1 ); + TEST_ASSERT_EQUAL( SigV4Success, SigV4_GenerateHTTPAuthorization( + ¶ms, authBuf, &authBufLen, &signature, &signatureLen ) ); + + params.pHttpParameters->pPath = NULL; + params.pHttpParameters->pathLen = 0U; + params.pHttpParameters->pQuery = NULL; + params.pHttpParameters->queryLen = 0U; + params.pHttpParameters->pHeaders = HEADERS_SORTED_COVERAGE_2; + params.pHttpParameters->headersLen = strlen( HEADERS_SORTED_COVERAGE_2 ); + TEST_ASSERT_EQUAL( SigV4Success, SigV4_GenerateHTTPAuthorization( + ¶ms, authBuf, &authBufLen, &signature, &signatureLen ) ); +}