From 2b8bad3e80a591c126c258e2d43b22acd1de6028 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Tue, 12 Jan 2021 09:40:05 +0000 Subject: [PATCH] MPS Reader Tests: Test feed() of NULL buffer Signed-off-by: Hanno Becker --- tests/suites/test_suite_mps.data | 3 +++ tests/suites/test_suite_mps.function | 38 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/tests/suites/test_suite_mps.data b/tests/suites/test_suite_mps.data index a1a6e5c26..158302b8e 100644 --- a/tests/suites/test_suite_mps.data +++ b/tests/suites/test_suite_mps.data @@ -117,3 +117,6 @@ mbedtls_reader_inconsistent_usage:7 MPS Reader: Pausing, inconsistent continuation, #8 mbedtls_reader_inconsistent_usage:8 + +MPS Reader: Feed with invalid buffer (NULL) +mbedtls_mps_reader_feed_empty:0 diff --git a/tests/suites/test_suite_mps.function b/tests/suites/test_suite_mps.function index 6f2eb653a..f2040f8f6 100644 --- a/tests/suites/test_suite_mps.function +++ b/tests/suites/test_suite_mps.function @@ -1059,3 +1059,41 @@ void mbedtls_reader_inconsistent_usage( int option ) mbedtls_reader_free( &rd ); } /* END_CASE */ + +/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */ +void mbedtls_mps_reader_feed_empty( int option ) +{ + /* This test exercises the behaviour of the reader when it is + * fed a NULL buffer. */ + unsigned char buf[100]; + unsigned char *tmp; + mbedtls_reader rd; + for( int i=0; (unsigned) i < sizeof( buf ); i++ ) + buf[i] = (unsigned char) i; + + /* Preparation (lower layer) */ + mbedtls_reader_init( &rd, NULL, 0 ); + switch( option ) + { + case 0: /* NULL buffer */ + TEST_ASSERT( mbedtls_reader_feed( &rd, NULL, sizeof( buf ) ) == + MBEDTLS_ERR_MPS_READER_INVALID_ARG ); + break; + + default: + TEST_ASSERT( 0 ); + break; + } + /* Subsequent feed-calls should still succeed. */ + TEST_ASSERT( mbedtls_reader_feed( &rd, buf, sizeof( buf ) ) == 0 ); + + /* Consumption (upper layer) */ + TEST_ASSERT( mbedtls_reader_get( &rd, 100, &tmp, NULL ) == 0 ); + ASSERT_COMPARE( tmp, 100, buf, 100 ); + TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); + + /* Wrapup */ + TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); + mbedtls_reader_free( &rd ); +} +/* END_CASE */