blob: f29197442bd4b90a1efbd84726a78501ef7699a0 [file] [log] [blame]
Hanno Becker09d880a2021-01-12 07:43:30 +00001/* BEGIN_HEADER */
2
3#include <stdlib.h>
4
5/* TODO: How are test suites supposed to include internal headers? */
6#include "../library/mps/reader.h"
7
8/*
9 * Compile-time configuration for test suite.
10 */
11
12/* Comment/Uncomment this to disable/enable the
13 * testing of the various MPS layers.
14 * This can be useful for time-consuming instrumentation
15 * tasks such as the conversion of E-ACSL annotations
16 * into runtime assertions. */
17#define TEST_SUITE_MPS_READER
18
19/* End of compile-time configuration. */
20
21/* END_HEADER */
22
23/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
24void mbedtls_mps_reader_no_pausing_single_step_single_round( int with_acc )
25{
26 /* This test exercises the most basic use of the MPS reader:
27 * - The 'producing' layer provides a buffer
28 * - The 'consuming' layer fetches it in a single go.
29 * - After processing, the consuming layer commit the data
30 * and returns back to the producing layer.
31 *
32 * Parameters:
33 * - with_acc: 0 if the reader should be initialized without accumulator.
34 * 1 if the reader should be initialized with accumulator.
35 *
36 * Whether the accumulator is present or not should not matter,
37 * since the consumer's request can be fulfilled from the data
38 * that the producer has provided.
39 */
40 unsigned char bufA[100];
41 unsigned char acc[10];
42 unsigned char *tmp;
43 mbedtls_reader rd;
44 for( int i=0; (unsigned) i < sizeof( bufA ); i++ )
45 bufA[i] = (unsigned char) i;
46
47 /* Preparation (lower layer) */
48 if( with_acc == 0 )
49 mbedtls_reader_init( &rd, NULL, 0 );
50 else
51 mbedtls_reader_init( &rd, acc, sizeof( acc ) );
52 TEST_ASSERT( mbedtls_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
53 /* Consumption (upper layer) */
54 /* Consume exactly what's available */
55 TEST_ASSERT( mbedtls_reader_get( &rd, 100, &tmp, NULL ) == 0 );
56 ASSERT_COMPARE( tmp, 100, bufA, 100 );
57 TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 );
58 /* Wrapup (lower layer) */
59 TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 );
60 mbedtls_reader_free( &rd );
61}
62/* END_CASE */
Hanno Becker0e4edfc2021-01-12 07:52:29 +000063
64/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
65void mbedtls_mps_reader_no_pausing_single_step_multiple_rounds( int with_acc )
66{
67 /* This test exercises multiple rounds o fthe basic use of the MPS reader:
68 * - The 'producing' layer provides a buffer
69 * - The 'consuming' layer fetches it in a single go.
70 * - After processing, the consuming layer commit the data
71 * and returns back to the producing layer.
72 *
73 * Parameters:
74 * - with_acc: 0 if the reader should be initialized without accumulator.
75 * 1 if the reader should be initialized with accumulator.
76 *
77 * Whether the accumulator is present or not should not matter,
78 * since the consumer's request can be fulfilled from the data
79 * that the producer has provided.
80 */
81
82 unsigned char bufA[100], bufB[100];
83 unsigned char acc[10];
84 unsigned char *tmp;
85 mbedtls_reader rd;
86 for( int i=0; (unsigned) i < sizeof( bufA ); i++ )
87 bufA[i] = (unsigned char) i;
88 for( int i=0; (unsigned) i < sizeof( bufB ); i++ )
89 bufB[i] = ~ ((unsigned char) i);
90
91 /* Preparation (lower layer) */
92 if( with_acc == 0 )
93 mbedtls_reader_init( &rd, NULL, 0 );
94 else
95 mbedtls_reader_init( &rd, acc, sizeof( acc ) );
96 TEST_ASSERT( mbedtls_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 );
97 /* Consumption (upper layer) */
98 /* Consume exactly what's available */
99 TEST_ASSERT( mbedtls_reader_get( &rd, 100, &tmp, NULL ) == 0 );
100 ASSERT_COMPARE( tmp, 100, bufA, 100 );
101 TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 );
102 /* Preparation */
103 TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 );
104 TEST_ASSERT( mbedtls_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 );
105 /* Consumption */
106 TEST_ASSERT( mbedtls_reader_get( &rd, 100, &tmp, NULL ) == 0 );
107 ASSERT_COMPARE( tmp, 100, bufB, 100 );
108 TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 );
109 /* Wrapup (lower layer) */
110 TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 );
111 mbedtls_reader_free( &rd );
112}
113/* END_CASE */