Hanno Becker | 09d880a | 2021-01-12 07:43:30 +0000 | [diff] [blame] | 1 | /* 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 */ |
| 24 | void 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 Becker | 0e4edfc | 2021-01-12 07:52:29 +0000 | [diff] [blame] | 63 | |
| 64 | /* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */ |
| 65 | void 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 */ |
Hanno Becker | dbd8a96 | 2021-01-12 08:01:16 +0000 | [diff] [blame] | 114 | |
| 115 | /* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */ |
| 116 | void mbedtls_mps_reader_no_pausing_multiple_steps_single_round( int with_acc ) |
| 117 | { |
| 118 | /* This test exercises one round of the following: |
| 119 | * - The 'producing' layer provides a buffer |
| 120 | * - The 'consuming' layer fetches it in multiple calls |
| 121 | * to `mbedtls_reader_get()`, without comitting in between. |
| 122 | * - After processing, the consuming layer commit the data |
| 123 | * and returns back to the producing layer. |
| 124 | * |
| 125 | * Parameters: |
| 126 | * - with_acc: 0 if the reader should be initialized without accumulator. |
| 127 | * 1 if the reader should be initialized with accumulator. |
| 128 | * |
| 129 | * Whether the accumulator is present or not should not matter, |
| 130 | * since the consumer's request can be fulfilled from the data |
| 131 | * that the producer has provided. |
| 132 | */ |
| 133 | |
| 134 | /* Lower layer provides data that the upper layer fully consumes |
| 135 | * through multiple `get` calls. */ |
| 136 | unsigned char buf[100]; |
| 137 | unsigned char acc[10]; |
| 138 | unsigned char *tmp; |
| 139 | mbedtls_mps_size_t tmp_len; |
| 140 | mbedtls_reader rd; |
| 141 | for( int i=0; (unsigned) i < sizeof( buf ); i++ ) |
| 142 | buf[i] = (unsigned char) i; |
| 143 | |
| 144 | /* Preparation (lower layer) */ |
| 145 | if( with_acc == 0 ) |
| 146 | mbedtls_reader_init( &rd, NULL, 0 ); |
| 147 | else |
| 148 | mbedtls_reader_init( &rd, acc, sizeof( acc ) ); |
| 149 | TEST_ASSERT( mbedtls_reader_feed( &rd, buf, sizeof( buf ) ) == 0 ); |
| 150 | /* Consumption (upper layer) */ |
| 151 | TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); |
| 152 | ASSERT_COMPARE( tmp, 10, buf, 10 ); |
| 153 | TEST_ASSERT( mbedtls_reader_get( &rd, 70, &tmp, NULL ) == 0 ); |
| 154 | ASSERT_COMPARE( tmp, 70, buf + 10, 70 ); |
| 155 | TEST_ASSERT( mbedtls_reader_get( &rd, 30, &tmp, &tmp_len ) == 0 ); |
| 156 | ASSERT_COMPARE( tmp, tmp_len, buf + 80, 20 ); |
| 157 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 158 | /* Wrapup (lower layer) */ |
| 159 | TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); |
| 160 | mbedtls_reader_free( &rd ); |
| 161 | } |
| 162 | /* END_CASE */ |
Hanno Becker | 7973b2d | 2021-01-12 08:11:40 +0000 | [diff] [blame^] | 163 | |
| 164 | /* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */ |
| 165 | void mbedtls_mps_reader_no_pausing_multiple_steps_multiple_rounds( int with_acc ) |
| 166 | { |
| 167 | /* This test exercises one round of fetching a buffer in multiple chunks |
| 168 | * and passing it back to the producer afterwards, followed by another |
| 169 | * single-step sequence of feed-fetch-commit-reclaim. |
| 170 | */ |
| 171 | unsigned char bufA[100], bufB[100]; |
| 172 | unsigned char acc[10]; |
| 173 | unsigned char *tmp; |
| 174 | mbedtls_mps_size_t tmp_len; |
| 175 | mbedtls_reader rd; |
| 176 | for( int i=0; (unsigned) i < sizeof( bufA ); i++ ) |
| 177 | bufA[i] = (unsigned char) i; |
| 178 | for( int i=0; (unsigned) i < sizeof( bufB ); i++ ) |
| 179 | bufB[i] = ~ ((unsigned char) i); |
| 180 | |
| 181 | /* Preparation (lower layer) */ |
| 182 | if( with_acc == 0 ) |
| 183 | mbedtls_reader_init( &rd, NULL, 0 ); |
| 184 | else |
| 185 | mbedtls_reader_init( &rd, acc, sizeof( acc ) ); |
| 186 | TEST_ASSERT( mbedtls_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 ); |
| 187 | /* Consumption (upper layer) */ |
| 188 | TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); |
| 189 | ASSERT_COMPARE( tmp, 10, bufA, 10 ); |
| 190 | TEST_ASSERT( mbedtls_reader_get( &rd, 70, &tmp, NULL ) == 0 ); |
| 191 | ASSERT_COMPARE( tmp, 70, bufA + 10, 70 ); |
| 192 | TEST_ASSERT( mbedtls_reader_get( &rd, 30, &tmp, &tmp_len ) == 0 ); |
| 193 | ASSERT_COMPARE( tmp, tmp_len, bufA + 80, 20 ); |
| 194 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 195 | /* Preparation */ |
| 196 | TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); |
| 197 | TEST_ASSERT( mbedtls_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 ); |
| 198 | /* Consumption */ |
| 199 | TEST_ASSERT( mbedtls_reader_get( &rd, 100, &tmp, NULL ) == 0 ); |
| 200 | ASSERT_COMPARE( tmp, 100, bufB, 100 ); |
| 201 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 202 | /* Wrapup */ |
| 203 | TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); |
| 204 | mbedtls_reader_free( &rd ); |
| 205 | } |
| 206 | /* END_CASE */ |