2021-01-12 08:43:30 +01:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-02-22 17:49:24 +01:00
|
|
|
#include "mps_reader.h"
|
2021-01-12 08:43:30 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader rd;
|
2021-01-12 08:43:30 +01:00
|
|
|
for( int i=0; (unsigned) i < sizeof( bufA ); i++ )
|
|
|
|
bufA[i] = (unsigned char) i;
|
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
|
|
|
if( with_acc == 0 )
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_init( &rd, NULL, 0 );
|
2021-01-12 08:43:30 +01:00
|
|
|
else
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
|
2021-01-12 08:43:30 +01:00
|
|
|
/* Consumption (upper layer) */
|
|
|
|
/* Consume exactly what's available */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) == 0 );
|
2021-01-12 08:43:30 +01:00
|
|
|
ASSERT_COMPARE( tmp, 100, bufA, 100 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-01-12 08:43:30 +01:00
|
|
|
/* Wrapup (lower layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
|
|
|
|
mbedtls_mps_reader_free( &rd );
|
2021-01-12 08:43:30 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-01-12 08:52:29 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
|
|
|
|
void mbedtls_mps_reader_no_pausing_single_step_multiple_rounds( int with_acc )
|
|
|
|
{
|
|
|
|
/* This test exercises multiple rounds o fthe 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], bufB[100];
|
|
|
|
unsigned char acc[10];
|
|
|
|
unsigned char *tmp;
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader rd;
|
2021-01-12 08:52:29 +01:00
|
|
|
for( int i=0; (unsigned) i < sizeof( bufA ); i++ )
|
|
|
|
bufA[i] = (unsigned char) i;
|
|
|
|
for( int i=0; (unsigned) i < sizeof( bufB ); i++ )
|
|
|
|
bufB[i] = ~ ((unsigned char) i);
|
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
|
|
|
if( with_acc == 0 )
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_init( &rd, NULL, 0 );
|
2021-01-12 08:52:29 +01:00
|
|
|
else
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
|
2021-01-12 08:52:29 +01:00
|
|
|
/* Consumption (upper layer) */
|
|
|
|
/* Consume exactly what's available */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) == 0 );
|
2021-01-12 08:52:29 +01:00
|
|
|
ASSERT_COMPARE( tmp, 100, bufA, 100 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-01-12 08:52:29 +01:00
|
|
|
/* Preparation */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 );
|
2021-01-12 08:52:29 +01:00
|
|
|
/* Consumption */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) == 0 );
|
2021-01-12 08:52:29 +01:00
|
|
|
ASSERT_COMPARE( tmp, 100, bufB, 100 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-01-12 08:52:29 +01:00
|
|
|
/* Wrapup (lower layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
|
|
|
|
mbedtls_mps_reader_free( &rd );
|
2021-01-12 08:52:29 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-01-12 09:01:16 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
|
|
|
|
void mbedtls_mps_reader_no_pausing_multiple_steps_single_round( int with_acc )
|
|
|
|
{
|
|
|
|
/* This test exercises one round of the following:
|
|
|
|
* - The 'producing' layer provides a buffer
|
|
|
|
* - The 'consuming' layer fetches it in multiple calls
|
2021-01-28 10:45:47 +01:00
|
|
|
* to `mbedtls_mps_reader_get()`, without comitting in between.
|
2021-01-12 09:01:16 +01:00
|
|
|
* - 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Lower layer provides data that the upper layer fully consumes
|
|
|
|
* through multiple `get` calls. */
|
|
|
|
unsigned char buf[100];
|
|
|
|
unsigned char acc[10];
|
|
|
|
unsigned char *tmp;
|
|
|
|
mbedtls_mps_size_t tmp_len;
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader rd;
|
2021-01-12 09:01:16 +01:00
|
|
|
for( int i=0; (unsigned) i < sizeof( buf ); i++ )
|
|
|
|
buf[i] = (unsigned char) i;
|
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
|
|
|
if( with_acc == 0 )
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_init( &rd, NULL, 0 );
|
2021-01-12 09:01:16 +01:00
|
|
|
else
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, buf, sizeof( buf ) ) == 0 );
|
2021-01-12 09:01:16 +01:00
|
|
|
/* Consumption (upper layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
|
2021-01-12 09:01:16 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, buf, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 70, &tmp, NULL ) == 0 );
|
2021-01-12 09:01:16 +01:00
|
|
|
ASSERT_COMPARE( tmp, 70, buf + 10, 70 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 30, &tmp, &tmp_len ) == 0 );
|
2021-01-12 09:01:16 +01:00
|
|
|
ASSERT_COMPARE( tmp, tmp_len, buf + 80, 20 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-01-12 09:01:16 +01:00
|
|
|
/* Wrapup (lower layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
|
|
|
|
mbedtls_mps_reader_free( &rd );
|
2021-01-12 09:01:16 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-01-12 09:11:40 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
|
|
|
|
void mbedtls_mps_reader_no_pausing_multiple_steps_multiple_rounds( int with_acc )
|
|
|
|
{
|
|
|
|
/* This test exercises one round of fetching a buffer in multiple chunks
|
|
|
|
* and passing it back to the producer afterwards, followed by another
|
|
|
|
* single-step sequence of feed-fetch-commit-reclaim.
|
|
|
|
*/
|
|
|
|
unsigned char bufA[100], bufB[100];
|
|
|
|
unsigned char acc[10];
|
|
|
|
unsigned char *tmp;
|
|
|
|
mbedtls_mps_size_t tmp_len;
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader rd;
|
2021-01-12 09:11:40 +01:00
|
|
|
for( int i=0; (unsigned) i < sizeof( bufA ); i++ )
|
|
|
|
bufA[i] = (unsigned char) i;
|
|
|
|
for( int i=0; (unsigned) i < sizeof( bufB ); i++ )
|
|
|
|
bufB[i] = ~ ((unsigned char) i);
|
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
|
|
|
if( with_acc == 0 )
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_init( &rd, NULL, 0 );
|
2021-01-12 09:11:40 +01:00
|
|
|
else
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
|
2021-01-12 09:11:40 +01:00
|
|
|
/* Consumption (upper layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
|
2021-01-12 09:11:40 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufA, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 70, &tmp, NULL ) == 0 );
|
2021-01-12 09:11:40 +01:00
|
|
|
ASSERT_COMPARE( tmp, 70, bufA + 10, 70 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 30, &tmp, &tmp_len ) == 0 );
|
2021-01-12 09:11:40 +01:00
|
|
|
ASSERT_COMPARE( tmp, tmp_len, bufA + 80, 20 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-01-12 09:11:40 +01:00
|
|
|
/* Preparation */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 );
|
2021-01-12 09:11:40 +01:00
|
|
|
/* Consumption */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) == 0 );
|
2021-01-12 09:11:40 +01:00
|
|
|
ASSERT_COMPARE( tmp, 100, bufB, 100 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-01-12 09:11:40 +01:00
|
|
|
/* Wrapup */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
|
|
|
|
mbedtls_mps_reader_free( &rd );
|
2021-01-12 09:11:40 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-01-12 09:14:38 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
|
|
|
|
void mbedtls_mps_reader_pausing_needed_disabled()
|
|
|
|
{
|
|
|
|
/* This test exercises the behaviour of the MPS reader when a read requests
|
|
|
|
* of the consumer exceeds what has been provided by the producer, and when
|
|
|
|
* no accumulator is available in the reader.
|
|
|
|
*
|
|
|
|
* In this case, we expect the reader to fail.
|
|
|
|
*/
|
|
|
|
|
|
|
|
unsigned char buf[100];
|
|
|
|
unsigned char *tmp;
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader rd;
|
2021-01-12 09:14:38 +01:00
|
|
|
for( int i=0; (unsigned) i < sizeof( buf ); i++ )
|
|
|
|
buf[i] = (unsigned char) i;
|
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_init( &rd, NULL, 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, buf, sizeof( buf ) ) == 0 );
|
2021-01-12 09:14:38 +01:00
|
|
|
/* Consumption (upper layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, NULL ) == 0 );
|
2021-01-12 09:14:38 +01:00
|
|
|
ASSERT_COMPARE( tmp, 50, buf, 50 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) ==
|
2021-01-12 09:14:38 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
|
|
|
|
/* Wrapup (lower layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) ==
|
2021-01-12 09:14:38 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_NEED_ACCUMULATOR );
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_free( &rd );
|
2021-01-12 09:14:38 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-01-12 09:18:12 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
|
|
|
|
void mbedtls_mps_reader_pausing_needed_buffer_too_small()
|
|
|
|
{
|
|
|
|
/* This test exercises the behaviour of the MPS reader with accumulator
|
|
|
|
* in the situation where a read requests goes beyond the bounds of the
|
|
|
|
* current read buffer, _and_ the reader's accumulator is too small to
|
|
|
|
* hold the requested amount of data.
|
|
|
|
*
|
2021-02-08 07:52:21 +01:00
|
|
|
* In this case, we expect mbedtls_mps_reader_reclaim() to fail,
|
|
|
|
* but it should be possible to continue fetching data as if
|
|
|
|
* there had been no excess request via mbedtls_mps_reader_get()
|
|
|
|
* and the call to mbedtls_mps_reader_reclaim() had been rejected
|
|
|
|
* because of data remaining.
|
|
|
|
*/
|
2021-01-12 09:18:12 +01:00
|
|
|
|
|
|
|
unsigned char buf[100];
|
|
|
|
unsigned char acc[10];
|
|
|
|
unsigned char *tmp;
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader rd;
|
2021-02-08 07:52:21 +01:00
|
|
|
mbedtls_mps_size_t tmp_len;
|
|
|
|
|
2021-01-12 09:18:12 +01:00
|
|
|
for( int i=0; (unsigned) i < sizeof( buf ); i++ )
|
|
|
|
buf[i] = (unsigned char) i;
|
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, buf, sizeof( buf ) ) == 0 );
|
2021-01-12 09:18:12 +01:00
|
|
|
/* Consumption (upper layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, NULL ) == 0 );
|
2021-01-12 09:18:12 +01:00
|
|
|
ASSERT_COMPARE( tmp, 50, buf, 50 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-02-08 07:52:21 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
|
|
|
|
ASSERT_COMPARE( tmp, 10, buf + 50, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) ==
|
2021-01-12 09:18:12 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
|
|
|
|
/* Wrapup (lower layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) ==
|
2021-01-12 09:18:12 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL );
|
2021-02-08 07:52:21 +01:00
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, &tmp_len ) == 0 );
|
|
|
|
ASSERT_COMPARE( tmp, tmp_len, buf + 50, 50 );
|
|
|
|
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_free( &rd );
|
2021-01-12 09:18:12 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-01-12 09:27:29 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
|
|
|
|
void mbedtls_mps_reader_pausing( int option )
|
|
|
|
{
|
|
|
|
/* This test exercises the behaviour of the reader when the
|
|
|
|
* accumulator is used to fufill the consumer's request.
|
|
|
|
*
|
|
|
|
* More detailed:
|
|
|
|
* - The producer feeds some data.
|
|
|
|
* - The consumer asks for more data than what's available.
|
|
|
|
* - The reader remembers the request and goes back to
|
|
|
|
* producing mode, waiting for more data from the producer.
|
|
|
|
* - The producer provides another chunk of data which is
|
|
|
|
* sufficient to fulfill the original read request.
|
|
|
|
* - The consumer retries the original read request, which
|
|
|
|
* should now succeed.
|
|
|
|
*
|
|
|
|
* This test comes in multiple variants controlled by the
|
|
|
|
* `option` parameter and documented below.
|
|
|
|
*/
|
|
|
|
|
|
|
|
unsigned char bufA[100], bufB[100];
|
|
|
|
unsigned char *tmp;
|
|
|
|
unsigned char acc[40];
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader rd;
|
2021-01-12 09:27:29 +01:00
|
|
|
for( int i=0; (unsigned) i < sizeof( bufA ); i++ )
|
|
|
|
bufA[i] = (unsigned char) i;
|
|
|
|
for( int i=0; (unsigned) i < sizeof( bufB ); i++ )
|
|
|
|
bufB[i] = ~ ((unsigned char) i);
|
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
|
2021-01-12 09:27:29 +01:00
|
|
|
|
|
|
|
/* Consumption (upper layer) */
|
|
|
|
/* Ask for more than what's available. */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 80, &tmp, NULL ) == 0 );
|
2021-01-12 09:27:29 +01:00
|
|
|
ASSERT_COMPARE( tmp, 80, bufA, 80 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
|
2021-01-12 09:27:29 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
|
|
|
|
switch( option )
|
|
|
|
{
|
|
|
|
case 0: /* Single uncommitted fetch at pausing */
|
|
|
|
case 1:
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-01-12 09:27:29 +01:00
|
|
|
break;
|
|
|
|
default: /* Multiple uncommitted fetches at pausing */
|
|
|
|
break;
|
|
|
|
}
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) ==
|
2021-01-12 09:27:29 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
|
|
|
|
|
|
|
|
/* Preparation */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 );
|
2021-01-12 09:27:29 +01:00
|
|
|
|
|
|
|
/* Consumption */
|
|
|
|
switch( option )
|
|
|
|
{
|
|
|
|
case 0: /* Single fetch at pausing, re-fetch with commit. */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
|
2021-01-12 09:27:29 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
|
|
|
|
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-01-12 09:27:29 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* Single fetch at pausing, re-fetch without commit. */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
|
2021-01-12 09:27:29 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
|
|
|
|
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* Multiple fetches at pausing, repeat without commit. */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
|
2021-01-12 09:27:29 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
|
2021-01-12 09:27:29 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
|
|
|
|
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: /* Multiple fetches at pausing, repeat with commit 1. */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
|
2021-01-12 09:27:29 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
|
2021-01-12 09:27:29 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
|
|
|
|
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4: /* Multiple fetches at pausing, repeat with commit 2. */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
|
2021-01-12 09:27:29 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
|
2021-01-12 09:27:29 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
|
|
|
|
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-01-12 09:27:29 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 5: /* Multiple fetches at pausing, repeat with commit 3. */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
|
2021-01-12 09:27:29 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
|
2021-01-12 09:27:29 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
|
|
|
|
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-01-12 09:27:29 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
TEST_ASSERT( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In all cases, fetch the rest of the second buffer. */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 90, &tmp, NULL ) == 0 );
|
2021-01-12 09:27:29 +01:00
|
|
|
ASSERT_COMPARE( tmp, 90, bufB + 10, 90 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-01-12 09:27:29 +01:00
|
|
|
|
|
|
|
/* Wrapup */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
|
|
|
|
mbedtls_mps_reader_free( &rd );
|
2021-01-12 09:27:29 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-01-12 09:36:36 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
|
|
|
|
void mbedtls_mps_reader_pausing_multiple_feeds( int option )
|
|
|
|
{
|
|
|
|
/* This test exercises the behaviour of the MPS reader
|
|
|
|
* in the following situation:
|
|
|
|
* - The consumer has asked for mre than what's available, so the
|
|
|
|
* reader pauses and waits for further input data via
|
2021-01-28 10:45:47 +01:00
|
|
|
* `mbedtls_mps_reader_feed()`
|
|
|
|
* - Multiple such calls to `mbedtls_mps_reader_feed()` are necessary
|
2021-01-12 09:36:36 +01:00
|
|
|
* to fulfill the original request, and the reader needs to do
|
|
|
|
* the necessary bookkeeping under the hood.
|
|
|
|
*
|
|
|
|
* This test comes in a few variants differing in the number and
|
|
|
|
* size of feed calls that the producer issues while the reader is
|
|
|
|
* accumulating the necessary data - see the comments below.
|
|
|
|
*/
|
|
|
|
|
|
|
|
unsigned char bufA[100], bufB[100];
|
|
|
|
unsigned char *tmp;
|
|
|
|
unsigned char acc[70];
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader rd;
|
2021-01-12 09:36:36 +01:00
|
|
|
mbedtls_mps_size_t fetch_len;
|
|
|
|
for( int i=0; (unsigned) i < sizeof( bufA ); i++ )
|
|
|
|
bufA[i] = (unsigned char) i;
|
|
|
|
for( int i=0; (unsigned) i < sizeof( bufB ); i++ )
|
|
|
|
bufB[i] = ~ ((unsigned char) i);
|
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
|
2021-01-12 09:36:36 +01:00
|
|
|
|
|
|
|
/* Consumption (upper layer) */
|
|
|
|
/* Ask for more than what's available. */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 80, &tmp, NULL ) == 0 );
|
2021-01-12 09:36:36 +01:00
|
|
|
ASSERT_COMPARE( tmp, 80, bufA, 80 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-01-12 09:36:36 +01:00
|
|
|
/* 20 left, ask for 70 -> 50 overhead */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 70, &tmp, NULL ) ==
|
2021-01-12 09:36:36 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
|
|
|
|
|
|
|
|
/* Preparation */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
|
2021-01-12 09:36:36 +01:00
|
|
|
switch( option )
|
|
|
|
{
|
|
|
|
case 0: /* 10 + 10 + 80 byte feed */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB, 10 ) ==
|
2021-01-12 09:36:36 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_NEED_MORE );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB + 10, 10 ) ==
|
2021-01-12 09:36:36 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_NEED_MORE );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB + 20, 80 ) == 0 );
|
2021-01-12 09:36:36 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* 50 x 1byte */
|
|
|
|
for( int num_feed=0; num_feed<49; num_feed++ )
|
|
|
|
{
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB + num_feed, 1 ) ==
|
2021-01-12 09:36:36 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_NEED_MORE );
|
|
|
|
}
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB + 49, 1 ) == 0 );
|
2021-01-12 09:36:36 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* 49 x 1byte + 51bytes */
|
|
|
|
for( int num_feed=0; num_feed<49; num_feed++ )
|
|
|
|
{
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB + num_feed, 1 ) ==
|
2021-01-12 09:36:36 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_NEED_MORE );
|
|
|
|
}
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB + 49, 51 ) == 0 );
|
2021-01-12 09:36:36 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
TEST_ASSERT( 0 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Consumption */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 70, &tmp, NULL ) == 0 );
|
2021-01-12 09:36:36 +01:00
|
|
|
ASSERT_COMPARE( tmp, 20, bufA + 80, 20 );
|
|
|
|
ASSERT_COMPARE( tmp + 20, 50, bufB, 50 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 1000, &tmp, &fetch_len ) == 0 );
|
2021-01-12 09:36:36 +01:00
|
|
|
switch( option )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
TEST_ASSERT( fetch_len == 50 );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
TEST_ASSERT( fetch_len == 0 );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
TEST_ASSERT( fetch_len == 50 );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
TEST_ASSERT( 0 );
|
|
|
|
break;
|
|
|
|
}
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-01-12 09:36:36 +01:00
|
|
|
|
|
|
|
/* Wrapup */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
|
|
|
|
mbedtls_mps_reader_free( &rd );
|
2021-01-12 09:36:36 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-01-12 09:39:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
|
|
|
|
void mbedtls_mps_reader_reclaim_data_left( int option )
|
|
|
|
{
|
|
|
|
/* This test exercises the behaviour of the MPS reader when a
|
2021-01-28 10:45:47 +01:00
|
|
|
* call to mbedtls_mps_reader_reclaim() is made before all data
|
2021-01-12 09:39:37 +01:00
|
|
|
* provided by the producer has been fetched and committed. */
|
|
|
|
|
|
|
|
unsigned char buf[100];
|
|
|
|
unsigned char *tmp;
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader rd;
|
2021-01-12 09:39:37 +01:00
|
|
|
for( int i=0; (unsigned) i < sizeof( buf ); i++ )
|
|
|
|
buf[i] = (unsigned char) i;
|
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_init( &rd, NULL, 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, buf, sizeof( buf ) ) == 0 );
|
2021-01-12 09:39:37 +01:00
|
|
|
|
|
|
|
/* Consumption (upper layer) */
|
|
|
|
switch( option )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
/* Fetch (but not commit) the entire buffer. */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, sizeof( buf ), &tmp, NULL )
|
2021-01-12 09:39:37 +01:00
|
|
|
== 0 );
|
|
|
|
ASSERT_COMPARE( tmp, 100, buf, 100 );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
/* Fetch (but not commit) parts of the buffer. */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, sizeof( buf ) / 2,
|
2021-01-12 09:39:37 +01:00
|
|
|
&tmp, NULL ) == 0 );
|
|
|
|
ASSERT_COMPARE( tmp, sizeof( buf ) / 2, buf, sizeof( buf ) / 2 );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
/* Fetch and commit parts of the buffer, then
|
|
|
|
* fetch but not commit the rest of the buffer. */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, sizeof( buf ) / 2,
|
2021-01-12 09:39:37 +01:00
|
|
|
&tmp, NULL ) == 0 );
|
|
|
|
ASSERT_COMPARE( tmp, sizeof( buf ) / 2, buf, sizeof( buf ) / 2 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, sizeof( buf ) / 2,
|
2021-01-12 09:39:37 +01:00
|
|
|
&tmp, NULL ) == 0 );
|
|
|
|
ASSERT_COMPARE( tmp, sizeof( buf ) / 2,
|
|
|
|
buf + sizeof( buf ) / 2,
|
|
|
|
sizeof( buf ) / 2 );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
TEST_ASSERT( 0 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wrapup */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) ==
|
2021-01-12 09:39:37 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_DATA_LEFT );
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_free( &rd );
|
2021-01-12 09:39:37 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-01-12 09:43:58 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
|
|
|
|
void mbedtls_mps_reader_reclaim_data_left_retry()
|
|
|
|
{
|
|
|
|
/* This test exercises the behaviour of the MPS reader when an attempt
|
|
|
|
* by the producer to reclaim the reader fails because of more data pending
|
|
|
|
* to be processed, and the consumer subsequently fetches more data. */
|
|
|
|
unsigned char buf[100];
|
|
|
|
unsigned char *tmp;
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader rd;
|
2021-01-12 09:43:58 +01:00
|
|
|
|
|
|
|
for( int i=0; (unsigned) i < sizeof( buf ); i++ )
|
|
|
|
buf[i] = (unsigned char) i;
|
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_init( &rd, NULL, 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, buf, sizeof( buf ) ) == 0 );
|
2021-01-12 09:43:58 +01:00
|
|
|
/* Consumption (upper layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, NULL ) == 0 );
|
2021-01-12 09:43:58 +01:00
|
|
|
ASSERT_COMPARE( tmp, 50, buf, 50 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, NULL ) == 0 );
|
2021-01-12 09:43:58 +01:00
|
|
|
ASSERT_COMPARE( tmp, 50, buf + 50, 50 );
|
|
|
|
/* Preparation */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) ==
|
2021-01-12 09:43:58 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_DATA_LEFT );
|
|
|
|
/* Consumption */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, NULL ) == 0 );
|
2021-01-12 09:43:58 +01:00
|
|
|
ASSERT_COMPARE( tmp, 50, buf + 50, 50 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-01-12 09:43:58 +01:00
|
|
|
/* Wrapup */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
|
|
|
|
mbedtls_mps_reader_free( &rd );
|
2021-01-12 09:43:58 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-01-12 10:17:56 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
|
|
|
|
void mbedtls_mps_reader_multiple_pausing( int option )
|
|
|
|
{
|
|
|
|
/* This test exercises the behaviour of the MPS reader
|
|
|
|
* in the following situation:
|
2021-01-28 10:45:47 +01:00
|
|
|
* - A read request via `mbedtls_mps_reader_get()` can't
|
2021-01-12 10:17:56 +01:00
|
|
|
* be served and the reader is paused to accumulate
|
|
|
|
* the desired amount of data from the producer.
|
|
|
|
* - Once enough data is availble, the consumer successfully
|
|
|
|
* reads the data from the reader, but afterwards exceeds
|
|
|
|
* the available data again - pausing is necessary for a
|
|
|
|
* second time.
|
|
|
|
*/
|
|
|
|
|
|
|
|
unsigned char bufA[100], bufB[20], bufC[10];
|
|
|
|
unsigned char *tmp;
|
|
|
|
unsigned char acc[50];
|
|
|
|
mbedtls_mps_size_t tmp_len;
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader rd;
|
2021-01-12 10:17:56 +01:00
|
|
|
for( int i=0; (unsigned) i < sizeof( bufA ); i++ )
|
|
|
|
bufA[i] = (unsigned char) i;
|
|
|
|
for( int i=0; (unsigned) i < sizeof( bufB ); i++ )
|
|
|
|
bufB[i] = ~ ((unsigned char) i);
|
|
|
|
for( int i=0; (unsigned) i < sizeof( bufC ); i++ )
|
|
|
|
bufC[i] = ~ ((unsigned char) i);
|
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
|
2021-01-12 10:17:56 +01:00
|
|
|
|
|
|
|
/* Consumption (upper layer) */
|
|
|
|
/* Ask for more than what's available. */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 80, &tmp, NULL ) == 0 );
|
2021-01-12 10:17:56 +01:00
|
|
|
ASSERT_COMPARE( tmp, 80, bufA, 80 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
|
2021-01-12 10:17:56 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) ==
|
2021-01-12 10:17:56 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
|
|
|
|
|
|
|
|
/* Preparation */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 );
|
2021-01-12 10:17:56 +01:00
|
|
|
|
|
|
|
switch( option )
|
|
|
|
{
|
|
|
|
case 0: /* Fetch same chunks, commit afterwards, and
|
|
|
|
* then exceed bounds of new buffer; accumulator
|
|
|
|
* large enough. */
|
|
|
|
|
|
|
|
/* Consume */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, &tmp_len ) == 0 );
|
2021-01-12 10:17:56 +01:00
|
|
|
ASSERT_COMPARE( tmp, tmp_len, bufA + 80, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
|
2021-01-12 10:17:56 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
|
|
|
|
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) ==
|
2021-01-12 10:17:56 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
|
|
|
|
|
|
|
|
/* Prepare */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufC, sizeof( bufC ) ) == 0 );;
|
2021-01-12 10:17:56 +01:00
|
|
|
|
|
|
|
/* Consume */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
|
2021-01-12 10:17:56 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufB + 10, 10 );
|
|
|
|
ASSERT_COMPARE( tmp + 10, 10, bufC, 10 );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* Fetch same chunks, commit afterwards, and
|
|
|
|
* then exceed bounds of new buffer; accumulator
|
|
|
|
* not large enough. */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
|
2021-01-12 10:17:56 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
|
2021-01-12 10:17:56 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
|
|
|
|
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 51, &tmp, NULL ) ==
|
2021-01-12 10:17:56 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
|
|
|
|
|
|
|
|
/* Prepare */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) ==
|
2021-01-12 10:17:56 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* Fetch same chunks, don't commit afterwards, and
|
|
|
|
* then exceed bounds of new buffer; accumulator
|
|
|
|
* large enough. */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
|
2021-01-12 10:17:56 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
|
2021-01-12 10:17:56 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
|
|
|
|
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) ==
|
2021-01-12 10:17:56 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
|
|
|
|
|
|
|
|
/* Prepare */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufC, sizeof( bufC ) ) == 0 );;
|
2021-01-12 10:17:56 +01:00
|
|
|
|
|
|
|
/* Consume */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 50, &tmp, NULL ) == 0 );
|
2021-01-12 10:17:56 +01:00
|
|
|
ASSERT_COMPARE( tmp, 20, bufA + 80, 20 );
|
|
|
|
ASSERT_COMPARE( tmp + 20, 20, bufB, 20 );
|
|
|
|
ASSERT_COMPARE( tmp + 40, 10, bufC, 10 );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: /* Fetch same chunks, don't commit afterwards, and
|
|
|
|
* then exceed bounds of new buffer; accumulator
|
|
|
|
* not large enough. */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
|
2021-01-12 10:17:56 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufA + 80, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) == 0 );
|
2021-01-12 10:17:56 +01:00
|
|
|
ASSERT_COMPARE( tmp, 10, bufA + 90, 10 );
|
|
|
|
ASSERT_COMPARE( tmp + 10, 10, bufB, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 21, &tmp, NULL ) ==
|
2021-01-12 10:17:56 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
|
|
|
|
|
|
|
|
/* Prepare */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) ==
|
2021-01-12 10:17:56 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
TEST_ASSERT( 0 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_free( &rd );
|
2021-01-12 10:17:56 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-01-12 10:23:15 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER:MBEDTLS_MPS_STATE_VALIDATION */
|
|
|
|
void mbedtls_mps_reader_random_usage( int num_out_chunks,
|
|
|
|
int max_chunk_size,
|
|
|
|
int max_request,
|
|
|
|
int acc_size )
|
|
|
|
|
|
|
|
{
|
|
|
|
/* Randomly pass a reader object back and forth between lower and
|
|
|
|
* upper layer and let each of them call the respective reader API
|
|
|
|
* functions in a random fashion.
|
|
|
|
*
|
|
|
|
* On the lower layer, we're tracking and concatenating
|
|
|
|
* the data passed to successful feed calls.
|
|
|
|
*
|
|
|
|
* For the upper layer, we track and concatenate buffers
|
|
|
|
* obtained from successful get calls.
|
|
|
|
*
|
|
|
|
* As long as the lower layer calls reclaim at least once, (resetting the
|
|
|
|
* fetched but not-yet-committed data), this should always lead to the same
|
|
|
|
* stream of outgoing/incoming data for the lower/upper layers, even if
|
|
|
|
* most of the random calls fail.
|
|
|
|
*
|
|
|
|
* NOTE: This test uses rand() for random data, which is not optimal.
|
|
|
|
* Instead, it would be better to get the random data from a
|
|
|
|
* static buffer. This both eases reproducibility and allows
|
|
|
|
* simple conversion to a fuzz target.
|
|
|
|
*/
|
|
|
|
int ret;
|
|
|
|
unsigned char *acc = NULL;
|
|
|
|
unsigned char *outgoing = NULL, *incoming = NULL;
|
|
|
|
unsigned char *cur_chunk = NULL;
|
|
|
|
size_t cur_out_chunk, out_pos, in_commit, in_fetch;
|
|
|
|
int rand_op; /* Lower layer:
|
|
|
|
* - Reclaim (0)
|
|
|
|
* - Feed (1)
|
|
|
|
* Upper layer:
|
|
|
|
* - Get, do tolerate smaller output (0)
|
|
|
|
* - Get, don't tolerate smaller output (1)
|
|
|
|
* - Commit (2) */
|
|
|
|
int mode = 0; /* Lower layer (0) or Upper layer (1) */
|
|
|
|
int reclaimed = 1; /* Have to call reclaim at least once before
|
|
|
|
* returning the reader to the upper layer. */
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader rd;
|
2021-01-12 10:23:15 +01:00
|
|
|
|
|
|
|
if( acc_size > 0 )
|
|
|
|
{
|
|
|
|
ASSERT_ALLOC( acc, acc_size );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This probably needs to be changed because we want
|
|
|
|
* our tests to be deterministic. */
|
|
|
|
// srand( time( NULL ) );
|
|
|
|
|
|
|
|
ASSERT_ALLOC( outgoing, num_out_chunks * max_chunk_size );
|
|
|
|
ASSERT_ALLOC( incoming, num_out_chunks * max_chunk_size );
|
|
|
|
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_init( &rd, acc, acc_size );
|
2021-01-12 10:23:15 +01:00
|
|
|
|
|
|
|
cur_out_chunk = 0;
|
|
|
|
in_commit = 0;
|
|
|
|
in_fetch = 0;
|
|
|
|
out_pos = 0;
|
|
|
|
while( cur_out_chunk < (unsigned) num_out_chunks )
|
|
|
|
{
|
|
|
|
if( mode == 0 )
|
|
|
|
{
|
|
|
|
/* Choose randomly between reclaim and feed */
|
|
|
|
rand_op = rand() % 2;
|
|
|
|
|
|
|
|
if( rand_op == 0 )
|
|
|
|
{
|
|
|
|
/* Reclaim */
|
2021-01-28 10:45:47 +01:00
|
|
|
ret = mbedtls_mps_reader_reclaim( &rd, NULL );
|
2021-01-12 10:23:15 +01:00
|
|
|
|
|
|
|
if( ret == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( cur_chunk != NULL );
|
|
|
|
mbedtls_free( cur_chunk );
|
|
|
|
cur_chunk = NULL;
|
|
|
|
}
|
|
|
|
reclaimed = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Feed reader with a random chunk */
|
|
|
|
unsigned char *tmp = NULL;
|
|
|
|
size_t tmp_size;
|
|
|
|
if( cur_out_chunk == (unsigned) num_out_chunks )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tmp_size = ( rand() % max_chunk_size ) + 1;
|
|
|
|
ASSERT_ALLOC( tmp, tmp_size );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_test_rnd_std_rand( NULL, tmp, tmp_size ) == 0 );
|
2021-01-28 10:45:47 +01:00
|
|
|
ret = mbedtls_mps_reader_feed( &rd, tmp, tmp_size );
|
2021-01-12 10:23:15 +01:00
|
|
|
|
|
|
|
if( ret == 0 || ret == MBEDTLS_ERR_MPS_READER_NEED_MORE )
|
|
|
|
{
|
|
|
|
cur_out_chunk++;
|
|
|
|
memcpy( outgoing + out_pos, tmp, tmp_size );
|
|
|
|
out_pos += tmp_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ret == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( cur_chunk == NULL );
|
|
|
|
cur_chunk = tmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mbedtls_free( tmp );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Randomly switch to consumption mode if reclaim
|
|
|
|
* was called at least once. */
|
|
|
|
if( reclaimed == 1 && rand() % 3 == 0 )
|
|
|
|
{
|
|
|
|
in_fetch = 0;
|
|
|
|
mode = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Choose randomly between get tolerating fewer data,
|
|
|
|
* get not tolerating fewer data, and commit. */
|
|
|
|
rand_op = rand() % 3;
|
|
|
|
if( rand_op == 0 || rand_op == 1 )
|
|
|
|
{
|
|
|
|
mbedtls_mps_size_t get_size, real_size;
|
|
|
|
unsigned char *chunk_get;
|
|
|
|
get_size = ( rand() % max_request ) + 1;
|
|
|
|
if( rand_op == 0 )
|
|
|
|
{
|
2021-01-28 10:45:47 +01:00
|
|
|
ret = mbedtls_mps_reader_get( &rd, get_size, &chunk_get,
|
2021-01-12 10:23:15 +01:00
|
|
|
&real_size );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
real_size = get_size;
|
2021-01-28 10:45:47 +01:00
|
|
|
ret = mbedtls_mps_reader_get( &rd, get_size, &chunk_get, NULL );
|
2021-01-12 10:23:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if output is in accordance with what was written */
|
|
|
|
if( ret == 0 )
|
|
|
|
{
|
|
|
|
memcpy( incoming + in_commit + in_fetch,
|
|
|
|
chunk_get, real_size );
|
|
|
|
TEST_ASSERT( memcmp( incoming + in_commit + in_fetch,
|
|
|
|
outgoing + in_commit + in_fetch,
|
|
|
|
real_size ) == 0 );
|
|
|
|
in_fetch += real_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( rand_op == 2 ) /* Commit */
|
|
|
|
{
|
2021-01-28 10:45:47 +01:00
|
|
|
ret = mbedtls_mps_reader_commit( &rd );
|
2021-01-12 10:23:15 +01:00
|
|
|
if( ret == 0 )
|
|
|
|
{
|
|
|
|
in_commit += in_fetch;
|
|
|
|
in_fetch = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Randomly switch back to preparation */
|
|
|
|
if( rand() % 3 == 0 )
|
|
|
|
{
|
|
|
|
reclaimed = 0;
|
|
|
|
mode = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cleanup */
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_free( &rd );
|
2021-01-12 10:23:15 +01:00
|
|
|
mbedtls_free( incoming );
|
|
|
|
mbedtls_free( outgoing );
|
|
|
|
mbedtls_free( acc );
|
|
|
|
mbedtls_free( cur_chunk );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-01-12 10:31:31 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
|
|
|
|
void mbedtls_reader_inconsistent_usage( int option )
|
|
|
|
{
|
|
|
|
/* This test exercises the behaviour of the MPS reader
|
|
|
|
* in the following situation:
|
|
|
|
* - The consumer asks for more data than what's available
|
|
|
|
* - The reader is paused and receives more data from the
|
|
|
|
* producer until the original read request can be fulfilled.
|
|
|
|
* - The consumer does not repeat the original request but
|
|
|
|
* requests data in a different way.
|
|
|
|
*
|
|
|
|
* The reader does not guarantee that inconsistent read requests
|
|
|
|
* after pausing will succeed, and this test triggers some cases
|
|
|
|
* where the request fails.
|
|
|
|
*/
|
|
|
|
|
|
|
|
unsigned char bufA[100], bufB[100];
|
|
|
|
unsigned char *tmp;
|
|
|
|
unsigned char acc[40];
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader rd;
|
2021-01-12 10:31:31 +01:00
|
|
|
int success = 0;
|
|
|
|
for( int i=0; (unsigned) i < sizeof( bufA ); i++ )
|
|
|
|
bufA[i] = (unsigned char) i;
|
|
|
|
for( int i=0; (unsigned) i < sizeof( bufB ); i++ )
|
|
|
|
bufB[i] = ~ ((unsigned char) i);
|
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_init( &rd, acc, sizeof( acc ) );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
|
2021-01-12 10:31:31 +01:00
|
|
|
/* Consumption (upper layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 80, &tmp, NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 20, &tmp, NULL ) ==
|
2021-01-12 10:31:31 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA );
|
|
|
|
/* Preparation */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 );
|
2021-01-12 10:31:31 +01:00
|
|
|
/* Consumption */
|
|
|
|
switch( option )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
/* Ask for buffered data in a single chunk, no commit */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 30, &tmp, NULL ) == 0 );
|
2021-01-12 10:31:31 +01:00
|
|
|
ASSERT_COMPARE( tmp, 20, bufA + 80, 20 );
|
|
|
|
ASSERT_COMPARE( tmp + 20, 10, bufB, 10 );
|
|
|
|
success = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
/* Ask for buffered data in a single chunk, with commit */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 30, &tmp, NULL ) == 0 );
|
2021-01-12 10:31:31 +01:00
|
|
|
ASSERT_COMPARE( tmp, 20, bufA + 80, 20 );
|
|
|
|
ASSERT_COMPARE( tmp + 20, 10, bufB, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-01-12 10:31:31 +01:00
|
|
|
success = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
/* Ask for more than was requested when pausing, #1 */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 31, &tmp, NULL ) ==
|
2021-01-12 10:31:31 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
/* Ask for more than was requested when pausing #2 */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, (mbedtls_mps_size_t) -1, &tmp, NULL ) ==
|
2021-01-12 10:31:31 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
/* Asking for buffered data in different
|
|
|
|
* chunks than before CAN fail. */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
|
2021-01-12 10:31:31 +01:00
|
|
|
ASSERT_COMPARE( tmp, 15, bufA + 80, 15 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 10, &tmp, NULL ) ==
|
2021-01-12 10:31:31 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
/* Asking for buffered data different chunks
|
|
|
|
* than before NEED NOT fail - no commits */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
|
2021-01-12 10:31:31 +01:00
|
|
|
ASSERT_COMPARE( tmp, 15, bufA + 80, 15 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
|
2021-01-12 10:31:31 +01:00
|
|
|
ASSERT_COMPARE( tmp, 5, bufA + 95, 5 );
|
|
|
|
ASSERT_COMPARE( tmp + 5, 10, bufB, 10 );
|
|
|
|
success = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
/* Asking for buffered data different chunks
|
|
|
|
* than before NEED NOT fail - intermediate commit */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
|
2021-01-12 10:31:31 +01:00
|
|
|
ASSERT_COMPARE( tmp, 15, bufA + 80, 15 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
|
2021-01-12 10:31:31 +01:00
|
|
|
ASSERT_COMPARE( tmp, 5, bufA + 95, 5 );
|
|
|
|
ASSERT_COMPARE( tmp + 5, 10, bufB, 10 );
|
|
|
|
success = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 7:
|
|
|
|
/* Asking for buffered data different chunks
|
|
|
|
* than before NEED NOT fail - end commit */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
|
2021-01-12 10:31:31 +01:00
|
|
|
ASSERT_COMPARE( tmp, 15, bufA + 80, 15 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
|
2021-01-12 10:31:31 +01:00
|
|
|
ASSERT_COMPARE( tmp, 5, bufA + 95, 5 );
|
|
|
|
ASSERT_COMPARE( tmp + 5, 10, bufB, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-01-12 10:31:31 +01:00
|
|
|
success = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
/* Asking for buffered data different chunks
|
|
|
|
* than before NEED NOT fail - intermediate & end commit */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
|
2021-01-12 10:31:31 +01:00
|
|
|
ASSERT_COMPARE( tmp, 15, bufA + 80, 15 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 15, &tmp, NULL ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-01-12 10:31:31 +01:00
|
|
|
ASSERT_COMPARE( tmp, 5, bufA + 95, 5 );
|
|
|
|
ASSERT_COMPARE( tmp + 5, 10, bufB, 10 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-01-12 10:31:31 +01:00
|
|
|
success = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
TEST_ASSERT( 0 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( success == 1 )
|
|
|
|
{
|
|
|
|
/* In all succeeding cases, fetch the rest of the second buffer. */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 90, &tmp, NULL ) == 0 );
|
2021-01-12 10:31:31 +01:00
|
|
|
ASSERT_COMPARE( tmp, 90, bufB + 10, 90 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-01-12 10:31:31 +01:00
|
|
|
|
|
|
|
/* Wrapup */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
|
2021-01-12 10:31:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wrapup */
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_free( &rd );
|
2021-01-12 10:31:31 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-01-12 10:40:05 +01:00
|
|
|
|
|
|
|
/* 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;
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader rd;
|
2021-01-12 10:40:05 +01:00
|
|
|
for( int i=0; (unsigned) i < sizeof( buf ); i++ )
|
|
|
|
buf[i] = (unsigned char) i;
|
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader_init( &rd, NULL, 0 );
|
2021-01-12 10:40:05 +01:00
|
|
|
switch( option )
|
|
|
|
{
|
|
|
|
case 0: /* NULL buffer */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, NULL, sizeof( buf ) ) ==
|
2021-01-12 10:40:05 +01:00
|
|
|
MBEDTLS_ERR_MPS_READER_INVALID_ARG );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
TEST_ASSERT( 0 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Subsequent feed-calls should still succeed. */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_feed( &rd, buf, sizeof( buf ) ) == 0 );
|
2021-01-12 10:40:05 +01:00
|
|
|
|
|
|
|
/* Consumption (upper layer) */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_get( &rd, 100, &tmp, NULL ) == 0 );
|
2021-01-12 10:40:05 +01:00
|
|
|
ASSERT_COMPARE( tmp, 100, buf, 100 );
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_commit( &rd ) == 0 );
|
2021-01-12 10:40:05 +01:00
|
|
|
|
|
|
|
/* Wrapup */
|
2021-01-28 10:45:47 +01:00
|
|
|
TEST_ASSERT( mbedtls_mps_reader_reclaim( &rd, NULL ) == 0 );
|
|
|
|
mbedtls_mps_reader_free( &rd );
|
2021-01-12 10:40:05 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|