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 */ |
Hanno Becker | 7d86b74 | 2021-01-12 08:14:38 +0000 | [diff] [blame] | 207 | |
| 208 | /* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */ |
| 209 | void mbedtls_mps_reader_pausing_needed_disabled() |
| 210 | { |
| 211 | /* This test exercises the behaviour of the MPS reader when a read requests |
| 212 | * of the consumer exceeds what has been provided by the producer, and when |
| 213 | * no accumulator is available in the reader. |
| 214 | * |
| 215 | * In this case, we expect the reader to fail. |
| 216 | */ |
| 217 | |
| 218 | unsigned char buf[100]; |
| 219 | unsigned char *tmp; |
| 220 | mbedtls_reader rd; |
| 221 | for( int i=0; (unsigned) i < sizeof( buf ); i++ ) |
| 222 | buf[i] = (unsigned char) i; |
| 223 | |
| 224 | /* Preparation (lower layer) */ |
| 225 | mbedtls_reader_init( &rd, NULL, 0 ); |
| 226 | TEST_ASSERT( mbedtls_reader_feed( &rd, buf, sizeof( buf ) ) == 0 ); |
| 227 | /* Consumption (upper layer) */ |
| 228 | TEST_ASSERT( mbedtls_reader_get( &rd, 50, &tmp, NULL ) == 0 ); |
| 229 | ASSERT_COMPARE( tmp, 50, buf, 50 ); |
| 230 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 231 | TEST_ASSERT( mbedtls_reader_get( &rd, 100, &tmp, NULL ) == |
| 232 | MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); |
| 233 | /* Wrapup (lower layer) */ |
| 234 | TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == |
| 235 | MBEDTLS_ERR_MPS_READER_NEED_ACCUMULATOR ); |
| 236 | mbedtls_reader_free( &rd ); |
| 237 | } |
| 238 | /* END_CASE */ |
Hanno Becker | caf1a3f | 2021-01-12 08:18:12 +0000 | [diff] [blame] | 239 | |
| 240 | /* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */ |
| 241 | void mbedtls_mps_reader_pausing_needed_buffer_too_small() |
| 242 | { |
| 243 | /* This test exercises the behaviour of the MPS reader with accumulator |
| 244 | * in the situation where a read requests goes beyond the bounds of the |
| 245 | * current read buffer, _and_ the reader's accumulator is too small to |
| 246 | * hold the requested amount of data. |
| 247 | * |
| 248 | * In this case, we expect the reader to fail. */ |
| 249 | |
| 250 | unsigned char buf[100]; |
| 251 | unsigned char acc[10]; |
| 252 | unsigned char *tmp; |
| 253 | mbedtls_reader rd; |
| 254 | for( int i=0; (unsigned) i < sizeof( buf ); i++ ) |
| 255 | buf[i] = (unsigned char) i; |
| 256 | |
| 257 | /* Preparation (lower layer) */ |
| 258 | mbedtls_reader_init( &rd, acc, sizeof( acc ) ); |
| 259 | TEST_ASSERT( mbedtls_reader_feed( &rd, buf, sizeof( buf ) ) == 0 ); |
| 260 | /* Consumption (upper layer) */ |
| 261 | TEST_ASSERT( mbedtls_reader_get( &rd, 50, &tmp, NULL ) == 0 ); |
| 262 | ASSERT_COMPARE( tmp, 50, buf, 50 ); |
| 263 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 264 | TEST_ASSERT( mbedtls_reader_get( &rd, 100, &tmp, NULL ) == |
| 265 | MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); |
| 266 | /* Wrapup (lower layer) */ |
| 267 | TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == |
| 268 | MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL ); |
| 269 | mbedtls_reader_free( &rd ); |
| 270 | } |
| 271 | /* END_CASE */ |
Hanno Becker | e82952a | 2021-01-12 08:27:29 +0000 | [diff] [blame] | 272 | |
| 273 | /* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */ |
| 274 | void mbedtls_mps_reader_pausing( int option ) |
| 275 | { |
| 276 | /* This test exercises the behaviour of the reader when the |
| 277 | * accumulator is used to fufill the consumer's request. |
| 278 | * |
| 279 | * More detailed: |
| 280 | * - The producer feeds some data. |
| 281 | * - The consumer asks for more data than what's available. |
| 282 | * - The reader remembers the request and goes back to |
| 283 | * producing mode, waiting for more data from the producer. |
| 284 | * - The producer provides another chunk of data which is |
| 285 | * sufficient to fulfill the original read request. |
| 286 | * - The consumer retries the original read request, which |
| 287 | * should now succeed. |
| 288 | * |
| 289 | * This test comes in multiple variants controlled by the |
| 290 | * `option` parameter and documented below. |
| 291 | */ |
| 292 | |
| 293 | unsigned char bufA[100], bufB[100]; |
| 294 | unsigned char *tmp; |
| 295 | unsigned char acc[40]; |
| 296 | mbedtls_reader rd; |
| 297 | for( int i=0; (unsigned) i < sizeof( bufA ); i++ ) |
| 298 | bufA[i] = (unsigned char) i; |
| 299 | for( int i=0; (unsigned) i < sizeof( bufB ); i++ ) |
| 300 | bufB[i] = ~ ((unsigned char) i); |
| 301 | |
| 302 | /* Preparation (lower layer) */ |
| 303 | mbedtls_reader_init( &rd, acc, sizeof( acc ) ); |
| 304 | TEST_ASSERT( mbedtls_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 ); |
| 305 | |
| 306 | /* Consumption (upper layer) */ |
| 307 | /* Ask for more than what's available. */ |
| 308 | TEST_ASSERT( mbedtls_reader_get( &rd, 80, &tmp, NULL ) == 0 ); |
| 309 | ASSERT_COMPARE( tmp, 80, bufA, 80 ); |
| 310 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 311 | TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); |
| 312 | ASSERT_COMPARE( tmp, 10, bufA + 80, 10 ); |
| 313 | switch( option ) |
| 314 | { |
| 315 | case 0: /* Single uncommitted fetch at pausing */ |
| 316 | case 1: |
| 317 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 318 | break; |
| 319 | default: /* Multiple uncommitted fetches at pausing */ |
| 320 | break; |
| 321 | } |
| 322 | TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == |
| 323 | MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); |
| 324 | |
| 325 | /* Preparation */ |
| 326 | TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); |
| 327 | TEST_ASSERT( mbedtls_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 ); |
| 328 | |
| 329 | /* Consumption */ |
| 330 | switch( option ) |
| 331 | { |
| 332 | case 0: /* Single fetch at pausing, re-fetch with commit. */ |
| 333 | TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); |
| 334 | ASSERT_COMPARE( tmp, 10, bufA + 90, 10 ); |
| 335 | ASSERT_COMPARE( tmp + 10, 10, bufB, 10 ); |
| 336 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 337 | break; |
| 338 | |
| 339 | case 1: /* Single fetch at pausing, re-fetch without commit. */ |
| 340 | TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); |
| 341 | ASSERT_COMPARE( tmp, 10, bufA + 90, 10 ); |
| 342 | ASSERT_COMPARE( tmp + 10, 10, bufB, 10 ); |
| 343 | break; |
| 344 | |
| 345 | case 2: /* Multiple fetches at pausing, repeat without commit. */ |
| 346 | TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); |
| 347 | ASSERT_COMPARE( tmp, 10, bufA + 80, 10 ); |
| 348 | TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); |
| 349 | ASSERT_COMPARE( tmp, 10, bufA + 90, 10 ); |
| 350 | ASSERT_COMPARE( tmp + 10, 10, bufB, 10 ); |
| 351 | break; |
| 352 | |
| 353 | case 3: /* Multiple fetches at pausing, repeat with commit 1. */ |
| 354 | TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); |
| 355 | ASSERT_COMPARE( tmp, 10, bufA + 80, 10 ); |
| 356 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 357 | TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); |
| 358 | ASSERT_COMPARE( tmp, 10, bufA + 90, 10 ); |
| 359 | ASSERT_COMPARE( tmp + 10, 10, bufB, 10 ); |
| 360 | break; |
| 361 | |
| 362 | case 4: /* Multiple fetches at pausing, repeat with commit 2. */ |
| 363 | TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); |
| 364 | ASSERT_COMPARE( tmp, 10, bufA + 80, 10 ); |
| 365 | TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); |
| 366 | ASSERT_COMPARE( tmp, 10, bufA + 90, 10 ); |
| 367 | ASSERT_COMPARE( tmp + 10, 10, bufB, 10 ); |
| 368 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 369 | break; |
| 370 | |
| 371 | case 5: /* Multiple fetches at pausing, repeat with commit 3. */ |
| 372 | TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); |
| 373 | ASSERT_COMPARE( tmp, 10, bufA + 80, 10 ); |
| 374 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 375 | TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); |
| 376 | ASSERT_COMPARE( tmp, 10, bufA + 90, 10 ); |
| 377 | ASSERT_COMPARE( tmp + 10, 10, bufB, 10 ); |
| 378 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 379 | break; |
| 380 | |
| 381 | default: |
| 382 | TEST_ASSERT( 0 ); |
| 383 | } |
| 384 | |
| 385 | /* In all cases, fetch the rest of the second buffer. */ |
| 386 | TEST_ASSERT( mbedtls_reader_get( &rd, 90, &tmp, NULL ) == 0 ); |
| 387 | ASSERT_COMPARE( tmp, 90, bufB + 10, 90 ); |
| 388 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 389 | |
| 390 | /* Wrapup */ |
| 391 | TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); |
| 392 | mbedtls_reader_free( &rd ); |
| 393 | } |
| 394 | /* END_CASE */ |
Hanno Becker | aac4122 | 2021-01-12 08:36:36 +0000 | [diff] [blame] | 395 | |
| 396 | /* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */ |
| 397 | void mbedtls_mps_reader_pausing_multiple_feeds( int option ) |
| 398 | { |
| 399 | /* This test exercises the behaviour of the MPS reader |
| 400 | * in the following situation: |
| 401 | * - The consumer has asked for mre than what's available, so the |
| 402 | * reader pauses and waits for further input data via |
| 403 | * `mbedtls_reader_feed()` |
| 404 | * - Multiple such calls to `mbedtls_reader_feed()` are necessary |
| 405 | * to fulfill the original request, and the reader needs to do |
| 406 | * the necessary bookkeeping under the hood. |
| 407 | * |
| 408 | * This test comes in a few variants differing in the number and |
| 409 | * size of feed calls that the producer issues while the reader is |
| 410 | * accumulating the necessary data - see the comments below. |
| 411 | */ |
| 412 | |
| 413 | unsigned char bufA[100], bufB[100]; |
| 414 | unsigned char *tmp; |
| 415 | unsigned char acc[70]; |
| 416 | mbedtls_reader rd; |
| 417 | mbedtls_mps_size_t fetch_len; |
| 418 | for( int i=0; (unsigned) i < sizeof( bufA ); i++ ) |
| 419 | bufA[i] = (unsigned char) i; |
| 420 | for( int i=0; (unsigned) i < sizeof( bufB ); i++ ) |
| 421 | bufB[i] = ~ ((unsigned char) i); |
| 422 | |
| 423 | /* Preparation (lower layer) */ |
| 424 | mbedtls_reader_init( &rd, acc, sizeof( acc ) ); |
| 425 | TEST_ASSERT( mbedtls_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 ); |
| 426 | |
| 427 | /* Consumption (upper layer) */ |
| 428 | /* Ask for more than what's available. */ |
| 429 | TEST_ASSERT( mbedtls_reader_get( &rd, 80, &tmp, NULL ) == 0 ); |
| 430 | ASSERT_COMPARE( tmp, 80, bufA, 80 ); |
| 431 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 432 | /* 20 left, ask for 70 -> 50 overhead */ |
| 433 | TEST_ASSERT( mbedtls_reader_get( &rd, 70, &tmp, NULL ) == |
| 434 | MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); |
| 435 | |
| 436 | /* Preparation */ |
| 437 | TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); |
| 438 | switch( option ) |
| 439 | { |
| 440 | case 0: /* 10 + 10 + 80 byte feed */ |
| 441 | TEST_ASSERT( mbedtls_reader_feed( &rd, bufB, 10 ) == |
| 442 | MBEDTLS_ERR_MPS_READER_NEED_MORE ); |
| 443 | TEST_ASSERT( mbedtls_reader_feed( &rd, bufB + 10, 10 ) == |
| 444 | MBEDTLS_ERR_MPS_READER_NEED_MORE ); |
| 445 | TEST_ASSERT( mbedtls_reader_feed( &rd, bufB + 20, 80 ) == 0 ); |
| 446 | break; |
| 447 | |
| 448 | case 1: /* 50 x 1byte */ |
| 449 | for( int num_feed=0; num_feed<49; num_feed++ ) |
| 450 | { |
| 451 | TEST_ASSERT( mbedtls_reader_feed( &rd, bufB + num_feed, 1 ) == |
| 452 | MBEDTLS_ERR_MPS_READER_NEED_MORE ); |
| 453 | } |
| 454 | TEST_ASSERT( mbedtls_reader_feed( &rd, bufB + 49, 1 ) == 0 ); |
| 455 | break; |
| 456 | |
| 457 | case 2: /* 49 x 1byte + 51bytes */ |
| 458 | for( int num_feed=0; num_feed<49; num_feed++ ) |
| 459 | { |
| 460 | TEST_ASSERT( mbedtls_reader_feed( &rd, bufB + num_feed, 1 ) == |
| 461 | MBEDTLS_ERR_MPS_READER_NEED_MORE ); |
| 462 | } |
| 463 | TEST_ASSERT( mbedtls_reader_feed( &rd, bufB + 49, 51 ) == 0 ); |
| 464 | break; |
| 465 | |
| 466 | default: |
| 467 | TEST_ASSERT( 0 ); |
| 468 | break; |
| 469 | } |
| 470 | |
| 471 | /* Consumption */ |
| 472 | TEST_ASSERT( mbedtls_reader_get( &rd, 70, &tmp, NULL ) == 0 ); |
| 473 | ASSERT_COMPARE( tmp, 20, bufA + 80, 20 ); |
| 474 | ASSERT_COMPARE( tmp + 20, 50, bufB, 50 ); |
| 475 | TEST_ASSERT( mbedtls_reader_get( &rd, 1000, &tmp, &fetch_len ) == 0 ); |
| 476 | switch( option ) |
| 477 | { |
| 478 | case 0: |
| 479 | TEST_ASSERT( fetch_len == 50 ); |
| 480 | break; |
| 481 | |
| 482 | case 1: |
| 483 | TEST_ASSERT( fetch_len == 0 ); |
| 484 | break; |
| 485 | |
| 486 | case 2: |
| 487 | TEST_ASSERT( fetch_len == 50 ); |
| 488 | break; |
| 489 | |
| 490 | default: |
| 491 | TEST_ASSERT( 0 ); |
| 492 | break; |
| 493 | } |
| 494 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 495 | |
| 496 | /* Wrapup */ |
| 497 | TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); |
| 498 | mbedtls_reader_free( &rd ); |
| 499 | } |
| 500 | /* END_CASE */ |
Hanno Becker | cb2a88e | 2021-01-12 08:39:37 +0000 | [diff] [blame] | 501 | |
| 502 | |
| 503 | /* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */ |
| 504 | void mbedtls_mps_reader_reclaim_data_left( int option ) |
| 505 | { |
| 506 | /* This test exercises the behaviour of the MPS reader when a |
| 507 | * call to mbedtls_reader_reclaim() is made before all data |
| 508 | * provided by the producer has been fetched and committed. */ |
| 509 | |
| 510 | unsigned char buf[100]; |
| 511 | unsigned char *tmp; |
| 512 | mbedtls_reader rd; |
| 513 | for( int i=0; (unsigned) i < sizeof( buf ); i++ ) |
| 514 | buf[i] = (unsigned char) i; |
| 515 | |
| 516 | /* Preparation (lower layer) */ |
| 517 | mbedtls_reader_init( &rd, NULL, 0 ); |
| 518 | TEST_ASSERT( mbedtls_reader_feed( &rd, buf, sizeof( buf ) ) == 0 ); |
| 519 | |
| 520 | /* Consumption (upper layer) */ |
| 521 | switch( option ) |
| 522 | { |
| 523 | case 0: |
| 524 | /* Fetch (but not commit) the entire buffer. */ |
| 525 | TEST_ASSERT( mbedtls_reader_get( &rd, sizeof( buf ), &tmp, NULL ) |
| 526 | == 0 ); |
| 527 | ASSERT_COMPARE( tmp, 100, buf, 100 ); |
| 528 | break; |
| 529 | |
| 530 | case 1: |
| 531 | /* Fetch (but not commit) parts of the buffer. */ |
| 532 | TEST_ASSERT( mbedtls_reader_get( &rd, sizeof( buf ) / 2, |
| 533 | &tmp, NULL ) == 0 ); |
| 534 | ASSERT_COMPARE( tmp, sizeof( buf ) / 2, buf, sizeof( buf ) / 2 ); |
| 535 | break; |
| 536 | |
| 537 | case 2: |
| 538 | /* Fetch and commit parts of the buffer, then |
| 539 | * fetch but not commit the rest of the buffer. */ |
| 540 | TEST_ASSERT( mbedtls_reader_get( &rd, sizeof( buf ) / 2, |
| 541 | &tmp, NULL ) == 0 ); |
| 542 | ASSERT_COMPARE( tmp, sizeof( buf ) / 2, buf, sizeof( buf ) / 2 ); |
| 543 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 544 | TEST_ASSERT( mbedtls_reader_get( &rd, sizeof( buf ) / 2, |
| 545 | &tmp, NULL ) == 0 ); |
| 546 | ASSERT_COMPARE( tmp, sizeof( buf ) / 2, |
| 547 | buf + sizeof( buf ) / 2, |
| 548 | sizeof( buf ) / 2 ); |
| 549 | break; |
| 550 | |
| 551 | default: |
| 552 | TEST_ASSERT( 0 ); |
| 553 | break; |
| 554 | } |
| 555 | |
| 556 | /* Wrapup */ |
| 557 | TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == |
| 558 | MBEDTLS_ERR_MPS_READER_DATA_LEFT ); |
| 559 | mbedtls_reader_free( &rd ); |
| 560 | } |
| 561 | /* END_CASE */ |
Hanno Becker | e1f173c | 2021-01-12 08:43:58 +0000 | [diff] [blame^] | 562 | |
| 563 | /* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */ |
| 564 | void mbedtls_mps_reader_reclaim_data_left_retry() |
| 565 | { |
| 566 | /* This test exercises the behaviour of the MPS reader when an attempt |
| 567 | * by the producer to reclaim the reader fails because of more data pending |
| 568 | * to be processed, and the consumer subsequently fetches more data. */ |
| 569 | unsigned char buf[100]; |
| 570 | unsigned char *tmp; |
| 571 | mbedtls_reader rd; |
| 572 | |
| 573 | for( int i=0; (unsigned) i < sizeof( buf ); i++ ) |
| 574 | buf[i] = (unsigned char) i; |
| 575 | |
| 576 | /* Preparation (lower layer) */ |
| 577 | mbedtls_reader_init( &rd, NULL, 0 ); |
| 578 | TEST_ASSERT( mbedtls_reader_feed( &rd, buf, sizeof( buf ) ) == 0 ); |
| 579 | /* Consumption (upper layer) */ |
| 580 | TEST_ASSERT( mbedtls_reader_get( &rd, 50, &tmp, NULL ) == 0 ); |
| 581 | ASSERT_COMPARE( tmp, 50, buf, 50 ); |
| 582 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 583 | TEST_ASSERT( mbedtls_reader_get( &rd, 50, &tmp, NULL ) == 0 ); |
| 584 | ASSERT_COMPARE( tmp, 50, buf + 50, 50 ); |
| 585 | /* Preparation */ |
| 586 | TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == |
| 587 | MBEDTLS_ERR_MPS_READER_DATA_LEFT ); |
| 588 | /* Consumption */ |
| 589 | TEST_ASSERT( mbedtls_reader_get( &rd, 50, &tmp, NULL ) == 0 ); |
| 590 | ASSERT_COMPARE( tmp, 50, buf + 50, 50 ); |
| 591 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 592 | /* Wrapup */ |
| 593 | TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); |
| 594 | mbedtls_reader_free( &rd ); |
| 595 | } |
| 596 | /* END_CASE */ |