From 09d880aa38bbf4621d785538ca7b44814177ae55 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Tue, 12 Jan 2021 07:43:30 +0000 Subject: [PATCH] MPS Reader Tests: Test basic feed-get-commit-reclaim cycle This commit adds an MPS unit test suite `test_suite_mps` which will subsequently be populated with unit tests for all components of MPS. As a start, a test case ``` mbedtls_mps_reader_no_pausing_single_step_single_round() ``` is added which exercises the most basic usage of the MPS reader component; see the test case description for more details. Signed-off-by: Hanno Becker --- tests/CMakeLists.txt | 1 + tests/suites/test_suite_mps.data | 5 +++ tests/suites/test_suite_mps.function | 62 ++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 tests/suites/test_suite_mps.data create mode 100644 tests/suites/test_suite_mps.function diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c141704b57..049b1306b9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -134,6 +134,7 @@ add_test_suite(md) add_test_suite(mdx) add_test_suite(memory_buffer_alloc) add_test_suite(mpi) +add_test_suite(mps) add_test_suite(net) add_test_suite(nist_kw) add_test_suite(oid) diff --git a/tests/suites/test_suite_mps.data b/tests/suites/test_suite_mps.data new file mode 100644 index 0000000000..ba8958235b --- /dev/null +++ b/tests/suites/test_suite_mps.data @@ -0,0 +1,5 @@ +MPS Reader: Single step, single round, pausing disabled +mbedtls_mps_reader_no_pausing_single_step_single_round:0 + +MPS Reader: Single step, single round, pausing enabled but unused +mbedtls_mps_reader_no_pausing_single_step_single_round:1 diff --git a/tests/suites/test_suite_mps.function b/tests/suites/test_suite_mps.function new file mode 100644 index 0000000000..9b06c9ca86 --- /dev/null +++ b/tests/suites/test_suite_mps.function @@ -0,0 +1,62 @@ +/* BEGIN_HEADER */ + +#include + +/* TODO: How are test suites supposed to include internal headers? */ +#include "../library/mps/reader.h" + +/* + * Compile-time configuration for test suite. + */ + +/* Comment/Uncomment this to disable/enable the + * testing of the various MPS layers. + * This can be useful for time-consuming instrumentation + * tasks such as the conversion of E-ACSL annotations + * into runtime assertions. */ +#define TEST_SUITE_MPS_READER + +/* End of compile-time configuration. */ + +/* END_HEADER */ + +/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */ +void mbedtls_mps_reader_no_pausing_single_step_single_round( int with_acc ) +{ + /* This test exercises the most basic use of the MPS reader: + * - The 'producing' layer provides a buffer + * - The 'consuming' layer fetches it in a single go. + * - After processing, the consuming layer commit the data + * and returns back to the producing layer. + * + * Parameters: + * - with_acc: 0 if the reader should be initialized without accumulator. + * 1 if the reader should be initialized with accumulator. + * + * Whether the accumulator is present or not should not matter, + * since the consumer's request can be fulfilled from the data + * that the producer has provided. + */ + unsigned char bufA[100]; + unsigned char acc[10]; + unsigned char *tmp; + mbedtls_reader rd; + for( int i=0; (unsigned) i < sizeof( bufA ); i++ ) + bufA[i] = (unsigned char) i; + + /* Preparation (lower layer) */ + if( with_acc == 0 ) + mbedtls_reader_init( &rd, NULL, 0 ); + else + mbedtls_reader_init( &rd, acc, sizeof( acc ) ); + TEST_ASSERT( mbedtls_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 ); + /* Consumption (upper layer) */ + /* Consume exactly what's available */ + TEST_ASSERT( mbedtls_reader_get( &rd, 100, &tmp, NULL ) == 0 ); + ASSERT_COMPARE( tmp, 100, bufA, 100 ); + TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); + /* Wrapup (lower layer) */ + TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); + mbedtls_reader_free( &rd ); +} +/* END_CASE */