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? */ |
Hanno Becker | c518c3b | 2021-01-28 07:08:08 +0000 | [diff] [blame^] | 6 | #include "../library/mps_reader.h" |
Hanno Becker | 09d880a | 2021-01-12 07:43:30 +0000 | [diff] [blame] | 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 */ |
Hanno Becker | b6fdd35 | 2021-01-12 09:17:56 +0000 | [diff] [blame] | 597 | |
| 598 | /* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */ |
| 599 | void mbedtls_mps_reader_multiple_pausing( int option ) |
| 600 | { |
| 601 | /* This test exercises the behaviour of the MPS reader |
| 602 | * in the following situation: |
| 603 | * - A read request via `mbedtls_reader_get()` can't |
| 604 | * be served and the reader is paused to accumulate |
| 605 | * the desired amount of data from the producer. |
| 606 | * - Once enough data is availble, the consumer successfully |
| 607 | * reads the data from the reader, but afterwards exceeds |
| 608 | * the available data again - pausing is necessary for a |
| 609 | * second time. |
| 610 | */ |
| 611 | |
| 612 | unsigned char bufA[100], bufB[20], bufC[10]; |
| 613 | unsigned char *tmp; |
| 614 | unsigned char acc[50]; |
| 615 | mbedtls_mps_size_t tmp_len; |
| 616 | mbedtls_reader rd; |
| 617 | for( int i=0; (unsigned) i < sizeof( bufA ); i++ ) |
| 618 | bufA[i] = (unsigned char) i; |
| 619 | for( int i=0; (unsigned) i < sizeof( bufB ); i++ ) |
| 620 | bufB[i] = ~ ((unsigned char) i); |
| 621 | for( int i=0; (unsigned) i < sizeof( bufC ); i++ ) |
| 622 | bufC[i] = ~ ((unsigned char) i); |
| 623 | |
| 624 | /* Preparation (lower layer) */ |
| 625 | mbedtls_reader_init( &rd, acc, sizeof( acc ) ); |
| 626 | TEST_ASSERT( mbedtls_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 ); |
| 627 | |
| 628 | /* Consumption (upper layer) */ |
| 629 | /* Ask for more than what's available. */ |
| 630 | TEST_ASSERT( mbedtls_reader_get( &rd, 80, &tmp, NULL ) == 0 ); |
| 631 | ASSERT_COMPARE( tmp, 80, bufA, 80 ); |
| 632 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 633 | TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); |
| 634 | ASSERT_COMPARE( tmp, 10, bufA + 80, 10 ); |
| 635 | TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == |
| 636 | MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); |
| 637 | |
| 638 | /* Preparation */ |
| 639 | TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); |
| 640 | TEST_ASSERT( mbedtls_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 ); |
| 641 | |
| 642 | switch( option ) |
| 643 | { |
| 644 | case 0: /* Fetch same chunks, commit afterwards, and |
| 645 | * then exceed bounds of new buffer; accumulator |
| 646 | * large enough. */ |
| 647 | |
| 648 | /* Consume */ |
| 649 | TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, &tmp_len ) == 0 ); |
| 650 | ASSERT_COMPARE( tmp, tmp_len, bufA + 80, 10 ); |
| 651 | TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); |
| 652 | ASSERT_COMPARE( tmp, 10, bufA + 90, 10 ); |
| 653 | ASSERT_COMPARE( tmp + 10, 10, bufB, 10 ); |
| 654 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 655 | TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == |
| 656 | MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); |
| 657 | |
| 658 | /* Prepare */ |
| 659 | TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); |
| 660 | TEST_ASSERT( mbedtls_reader_feed( &rd, bufC, sizeof( bufC ) ) == 0 );; |
| 661 | |
| 662 | /* Consume */ |
| 663 | TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); |
| 664 | ASSERT_COMPARE( tmp, 10, bufB + 10, 10 ); |
| 665 | ASSERT_COMPARE( tmp + 10, 10, bufC, 10 ); |
| 666 | break; |
| 667 | |
| 668 | case 1: /* Fetch same chunks, commit afterwards, and |
| 669 | * then exceed bounds of new buffer; accumulator |
| 670 | * not large enough. */ |
| 671 | TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); |
| 672 | ASSERT_COMPARE( tmp, 10, bufA + 80, 10 ); |
| 673 | TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); |
| 674 | ASSERT_COMPARE( tmp, 10, bufA + 90, 10 ); |
| 675 | ASSERT_COMPARE( tmp + 10, 10, bufB, 10 ); |
| 676 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 677 | TEST_ASSERT( mbedtls_reader_get( &rd, 51, &tmp, NULL ) == |
| 678 | MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); |
| 679 | |
| 680 | /* Prepare */ |
| 681 | TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == |
| 682 | MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL ); |
| 683 | break; |
| 684 | |
| 685 | case 2: /* Fetch same chunks, don't commit afterwards, and |
| 686 | * then exceed bounds of new buffer; accumulator |
| 687 | * large enough. */ |
| 688 | TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); |
| 689 | ASSERT_COMPARE( tmp, 10, bufA + 80, 10 ); |
| 690 | TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); |
| 691 | ASSERT_COMPARE( tmp, 10, bufA + 90, 10 ); |
| 692 | ASSERT_COMPARE( tmp + 10, 10, bufB, 10 ); |
| 693 | TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == |
| 694 | MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); |
| 695 | |
| 696 | /* Prepare */ |
| 697 | TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); |
| 698 | TEST_ASSERT( mbedtls_reader_feed( &rd, bufC, sizeof( bufC ) ) == 0 );; |
| 699 | |
| 700 | /* Consume */ |
| 701 | TEST_ASSERT( mbedtls_reader_get( &rd, 50, &tmp, NULL ) == 0 ); |
| 702 | ASSERT_COMPARE( tmp, 20, bufA + 80, 20 ); |
| 703 | ASSERT_COMPARE( tmp + 20, 20, bufB, 20 ); |
| 704 | ASSERT_COMPARE( tmp + 40, 10, bufC, 10 ); |
| 705 | break; |
| 706 | |
| 707 | case 3: /* Fetch same chunks, don't commit afterwards, and |
| 708 | * then exceed bounds of new buffer; accumulator |
| 709 | * not large enough. */ |
| 710 | TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); |
| 711 | ASSERT_COMPARE( tmp, 10, bufA + 80, 10 ); |
| 712 | TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == 0 ); |
| 713 | ASSERT_COMPARE( tmp, 10, bufA + 90, 10 ); |
| 714 | ASSERT_COMPARE( tmp + 10, 10, bufB, 10 ); |
| 715 | TEST_ASSERT( mbedtls_reader_get( &rd, 21, &tmp, NULL ) == |
| 716 | MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); |
| 717 | |
| 718 | /* Prepare */ |
| 719 | TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == |
| 720 | MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL ); |
| 721 | break; |
| 722 | |
| 723 | default: |
| 724 | TEST_ASSERT( 0 ); |
| 725 | break; |
| 726 | } |
| 727 | |
| 728 | mbedtls_reader_free( &rd ); |
| 729 | } |
| 730 | /* END_CASE */ |
Hanno Becker | 714cbeb | 2021-01-12 09:23:15 +0000 | [diff] [blame] | 731 | |
| 732 | /* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER:MBEDTLS_MPS_STATE_VALIDATION */ |
| 733 | void mbedtls_mps_reader_random_usage( int num_out_chunks, |
| 734 | int max_chunk_size, |
| 735 | int max_request, |
| 736 | int acc_size ) |
| 737 | |
| 738 | { |
| 739 | /* Randomly pass a reader object back and forth between lower and |
| 740 | * upper layer and let each of them call the respective reader API |
| 741 | * functions in a random fashion. |
| 742 | * |
| 743 | * On the lower layer, we're tracking and concatenating |
| 744 | * the data passed to successful feed calls. |
| 745 | * |
| 746 | * For the upper layer, we track and concatenate buffers |
| 747 | * obtained from successful get calls. |
| 748 | * |
| 749 | * As long as the lower layer calls reclaim at least once, (resetting the |
| 750 | * fetched but not-yet-committed data), this should always lead to the same |
| 751 | * stream of outgoing/incoming data for the lower/upper layers, even if |
| 752 | * most of the random calls fail. |
| 753 | * |
| 754 | * NOTE: This test uses rand() for random data, which is not optimal. |
| 755 | * Instead, it would be better to get the random data from a |
| 756 | * static buffer. This both eases reproducibility and allows |
| 757 | * simple conversion to a fuzz target. |
| 758 | */ |
| 759 | int ret; |
| 760 | unsigned char *acc = NULL; |
| 761 | unsigned char *outgoing = NULL, *incoming = NULL; |
| 762 | unsigned char *cur_chunk = NULL; |
| 763 | size_t cur_out_chunk, out_pos, in_commit, in_fetch; |
| 764 | int rand_op; /* Lower layer: |
| 765 | * - Reclaim (0) |
| 766 | * - Feed (1) |
| 767 | * Upper layer: |
| 768 | * - Get, do tolerate smaller output (0) |
| 769 | * - Get, don't tolerate smaller output (1) |
| 770 | * - Commit (2) */ |
| 771 | int mode = 0; /* Lower layer (0) or Upper layer (1) */ |
| 772 | int reclaimed = 1; /* Have to call reclaim at least once before |
| 773 | * returning the reader to the upper layer. */ |
| 774 | mbedtls_reader rd; |
| 775 | |
| 776 | if( acc_size > 0 ) |
| 777 | { |
| 778 | ASSERT_ALLOC( acc, acc_size ); |
| 779 | } |
| 780 | |
| 781 | /* This probably needs to be changed because we want |
| 782 | * our tests to be deterministic. */ |
| 783 | // srand( time( NULL ) ); |
| 784 | |
| 785 | ASSERT_ALLOC( outgoing, num_out_chunks * max_chunk_size ); |
| 786 | ASSERT_ALLOC( incoming, num_out_chunks * max_chunk_size ); |
| 787 | |
| 788 | mbedtls_reader_init( &rd, acc, acc_size ); |
| 789 | |
| 790 | cur_out_chunk = 0; |
| 791 | in_commit = 0; |
| 792 | in_fetch = 0; |
| 793 | out_pos = 0; |
| 794 | while( cur_out_chunk < (unsigned) num_out_chunks ) |
| 795 | { |
| 796 | if( mode == 0 ) |
| 797 | { |
| 798 | /* Choose randomly between reclaim and feed */ |
| 799 | rand_op = rand() % 2; |
| 800 | |
| 801 | if( rand_op == 0 ) |
| 802 | { |
| 803 | /* Reclaim */ |
| 804 | ret = mbedtls_reader_reclaim( &rd, NULL ); |
| 805 | |
| 806 | if( ret == 0 ) |
| 807 | { |
| 808 | TEST_ASSERT( cur_chunk != NULL ); |
| 809 | mbedtls_free( cur_chunk ); |
| 810 | cur_chunk = NULL; |
| 811 | } |
| 812 | reclaimed = 1; |
| 813 | } |
| 814 | else |
| 815 | { |
| 816 | /* Feed reader with a random chunk */ |
| 817 | unsigned char *tmp = NULL; |
| 818 | size_t tmp_size; |
| 819 | if( cur_out_chunk == (unsigned) num_out_chunks ) |
| 820 | continue; |
| 821 | |
| 822 | tmp_size = ( rand() % max_chunk_size ) + 1; |
| 823 | ASSERT_ALLOC( tmp, tmp_size ); |
| 824 | |
| 825 | TEST_ASSERT( mbedtls_test_rnd_std_rand( NULL, tmp, tmp_size ) == 0 ); |
| 826 | ret = mbedtls_reader_feed( &rd, tmp, tmp_size ); |
| 827 | |
| 828 | if( ret == 0 || ret == MBEDTLS_ERR_MPS_READER_NEED_MORE ) |
| 829 | { |
| 830 | cur_out_chunk++; |
| 831 | memcpy( outgoing + out_pos, tmp, tmp_size ); |
| 832 | out_pos += tmp_size; |
| 833 | } |
| 834 | |
| 835 | if( ret == 0 ) |
| 836 | { |
| 837 | TEST_ASSERT( cur_chunk == NULL ); |
| 838 | cur_chunk = tmp; |
| 839 | } |
| 840 | else |
| 841 | { |
| 842 | mbedtls_free( tmp ); |
| 843 | } |
| 844 | |
| 845 | } |
| 846 | |
| 847 | /* Randomly switch to consumption mode if reclaim |
| 848 | * was called at least once. */ |
| 849 | if( reclaimed == 1 && rand() % 3 == 0 ) |
| 850 | { |
| 851 | in_fetch = 0; |
| 852 | mode = 1; |
| 853 | } |
| 854 | } |
| 855 | else |
| 856 | { |
| 857 | /* Choose randomly between get tolerating fewer data, |
| 858 | * get not tolerating fewer data, and commit. */ |
| 859 | rand_op = rand() % 3; |
| 860 | if( rand_op == 0 || rand_op == 1 ) |
| 861 | { |
| 862 | mbedtls_mps_size_t get_size, real_size; |
| 863 | unsigned char *chunk_get; |
| 864 | get_size = ( rand() % max_request ) + 1; |
| 865 | if( rand_op == 0 ) |
| 866 | { |
| 867 | ret = mbedtls_reader_get( &rd, get_size, &chunk_get, |
| 868 | &real_size ); |
| 869 | } |
| 870 | else |
| 871 | { |
| 872 | real_size = get_size; |
| 873 | ret = mbedtls_reader_get( &rd, get_size, &chunk_get, NULL ); |
| 874 | } |
| 875 | |
| 876 | /* Check if output is in accordance with what was written */ |
| 877 | if( ret == 0 ) |
| 878 | { |
| 879 | memcpy( incoming + in_commit + in_fetch, |
| 880 | chunk_get, real_size ); |
| 881 | TEST_ASSERT( memcmp( incoming + in_commit + in_fetch, |
| 882 | outgoing + in_commit + in_fetch, |
| 883 | real_size ) == 0 ); |
| 884 | in_fetch += real_size; |
| 885 | } |
| 886 | } |
| 887 | else if( rand_op == 2 ) /* Commit */ |
| 888 | { |
| 889 | ret = mbedtls_reader_commit( &rd ); |
| 890 | if( ret == 0 ) |
| 891 | { |
| 892 | in_commit += in_fetch; |
| 893 | in_fetch = 0; |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | /* Randomly switch back to preparation */ |
| 898 | if( rand() % 3 == 0 ) |
| 899 | { |
| 900 | reclaimed = 0; |
| 901 | mode = 0; |
| 902 | } |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | /* Cleanup */ |
| 907 | mbedtls_reader_free( &rd ); |
| 908 | mbedtls_free( incoming ); |
| 909 | mbedtls_free( outgoing ); |
| 910 | mbedtls_free( acc ); |
| 911 | mbedtls_free( cur_chunk ); |
| 912 | } |
| 913 | /* END_CASE */ |
Hanno Becker | 223b72e | 2021-01-12 09:31:31 +0000 | [diff] [blame] | 914 | |
| 915 | /* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */ |
| 916 | void mbedtls_reader_inconsistent_usage( int option ) |
| 917 | { |
| 918 | /* This test exercises the behaviour of the MPS reader |
| 919 | * in the following situation: |
| 920 | * - The consumer asks for more data than what's available |
| 921 | * - The reader is paused and receives more data from the |
| 922 | * producer until the original read request can be fulfilled. |
| 923 | * - The consumer does not repeat the original request but |
| 924 | * requests data in a different way. |
| 925 | * |
| 926 | * The reader does not guarantee that inconsistent read requests |
| 927 | * after pausing will succeed, and this test triggers some cases |
| 928 | * where the request fails. |
| 929 | */ |
| 930 | |
| 931 | unsigned char bufA[100], bufB[100]; |
| 932 | unsigned char *tmp; |
| 933 | unsigned char acc[40]; |
| 934 | mbedtls_reader rd; |
| 935 | int success = 0; |
| 936 | for( int i=0; (unsigned) i < sizeof( bufA ); i++ ) |
| 937 | bufA[i] = (unsigned char) i; |
| 938 | for( int i=0; (unsigned) i < sizeof( bufB ); i++ ) |
| 939 | bufB[i] = ~ ((unsigned char) i); |
| 940 | |
| 941 | /* Preparation (lower layer) */ |
| 942 | mbedtls_reader_init( &rd, acc, sizeof( acc ) ); |
| 943 | TEST_ASSERT( mbedtls_reader_feed( &rd, bufA, sizeof( bufA ) ) == 0 ); |
| 944 | /* Consumption (upper layer) */ |
| 945 | TEST_ASSERT( mbedtls_reader_get( &rd, 80, &tmp, NULL ) == 0 ); |
| 946 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 947 | TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == 0 ); |
| 948 | TEST_ASSERT( mbedtls_reader_get( &rd, 20, &tmp, NULL ) == |
| 949 | MBEDTLS_ERR_MPS_READER_OUT_OF_DATA ); |
| 950 | /* Preparation */ |
| 951 | TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); |
| 952 | TEST_ASSERT( mbedtls_reader_feed( &rd, bufB, sizeof( bufB ) ) == 0 ); |
| 953 | /* Consumption */ |
| 954 | switch( option ) |
| 955 | { |
| 956 | case 0: |
| 957 | /* Ask for buffered data in a single chunk, no commit */ |
| 958 | TEST_ASSERT( mbedtls_reader_get( &rd, 30, &tmp, NULL ) == 0 ); |
| 959 | ASSERT_COMPARE( tmp, 20, bufA + 80, 20 ); |
| 960 | ASSERT_COMPARE( tmp + 20, 10, bufB, 10 ); |
| 961 | success = 1; |
| 962 | break; |
| 963 | |
| 964 | case 1: |
| 965 | /* Ask for buffered data in a single chunk, with commit */ |
| 966 | TEST_ASSERT( mbedtls_reader_get( &rd, 30, &tmp, NULL ) == 0 ); |
| 967 | ASSERT_COMPARE( tmp, 20, bufA + 80, 20 ); |
| 968 | ASSERT_COMPARE( tmp + 20, 10, bufB, 10 ); |
| 969 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 970 | success = 1; |
| 971 | break; |
| 972 | |
| 973 | case 2: |
| 974 | /* Ask for more than was requested when pausing, #1 */ |
| 975 | TEST_ASSERT( mbedtls_reader_get( &rd, 31, &tmp, NULL ) == |
| 976 | MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS ); |
| 977 | break; |
| 978 | |
| 979 | case 3: |
| 980 | /* Ask for more than was requested when pausing #2 */ |
| 981 | TEST_ASSERT( mbedtls_reader_get( &rd, (mbedtls_mps_size_t) -1, &tmp, NULL ) == |
| 982 | MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS ); |
| 983 | break; |
| 984 | |
| 985 | case 4: |
| 986 | /* Asking for buffered data in different |
| 987 | * chunks than before CAN fail. */ |
| 988 | TEST_ASSERT( mbedtls_reader_get( &rd, 15, &tmp, NULL ) == 0 ); |
| 989 | ASSERT_COMPARE( tmp, 15, bufA + 80, 15 ); |
| 990 | TEST_ASSERT( mbedtls_reader_get( &rd, 10, &tmp, NULL ) == |
| 991 | MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS ); |
| 992 | break; |
| 993 | |
| 994 | case 5: |
| 995 | /* Asking for buffered data different chunks |
| 996 | * than before NEED NOT fail - no commits */ |
| 997 | TEST_ASSERT( mbedtls_reader_get( &rd, 15, &tmp, NULL ) == 0 ); |
| 998 | ASSERT_COMPARE( tmp, 15, bufA + 80, 15 ); |
| 999 | TEST_ASSERT( mbedtls_reader_get( &rd, 15, &tmp, NULL ) == 0 ); |
| 1000 | ASSERT_COMPARE( tmp, 5, bufA + 95, 5 ); |
| 1001 | ASSERT_COMPARE( tmp + 5, 10, bufB, 10 ); |
| 1002 | success = 1; |
| 1003 | break; |
| 1004 | |
| 1005 | case 6: |
| 1006 | /* Asking for buffered data different chunks |
| 1007 | * than before NEED NOT fail - intermediate commit */ |
| 1008 | TEST_ASSERT( mbedtls_reader_get( &rd, 15, &tmp, NULL ) == 0 ); |
| 1009 | ASSERT_COMPARE( tmp, 15, bufA + 80, 15 ); |
| 1010 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 1011 | TEST_ASSERT( mbedtls_reader_get( &rd, 15, &tmp, NULL ) == 0 ); |
| 1012 | ASSERT_COMPARE( tmp, 5, bufA + 95, 5 ); |
| 1013 | ASSERT_COMPARE( tmp + 5, 10, bufB, 10 ); |
| 1014 | success = 1; |
| 1015 | break; |
| 1016 | |
| 1017 | case 7: |
| 1018 | /* Asking for buffered data different chunks |
| 1019 | * than before NEED NOT fail - end commit */ |
| 1020 | TEST_ASSERT( mbedtls_reader_get( &rd, 15, &tmp, NULL ) == 0 ); |
| 1021 | ASSERT_COMPARE( tmp, 15, bufA + 80, 15 ); |
| 1022 | TEST_ASSERT( mbedtls_reader_get( &rd, 15, &tmp, NULL ) == 0 ); |
| 1023 | ASSERT_COMPARE( tmp, 5, bufA + 95, 5 ); |
| 1024 | ASSERT_COMPARE( tmp + 5, 10, bufB, 10 ); |
| 1025 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 1026 | success = 1; |
| 1027 | break; |
| 1028 | |
| 1029 | case 8: |
| 1030 | /* Asking for buffered data different chunks |
| 1031 | * than before NEED NOT fail - intermediate & end commit */ |
| 1032 | TEST_ASSERT( mbedtls_reader_get( &rd, 15, &tmp, NULL ) == 0 ); |
| 1033 | ASSERT_COMPARE( tmp, 15, bufA + 80, 15 ); |
| 1034 | TEST_ASSERT( mbedtls_reader_get( &rd, 15, &tmp, NULL ) == 0 ); |
| 1035 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 1036 | ASSERT_COMPARE( tmp, 5, bufA + 95, 5 ); |
| 1037 | ASSERT_COMPARE( tmp + 5, 10, bufB, 10 ); |
| 1038 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 1039 | success = 1; |
| 1040 | break; |
| 1041 | |
| 1042 | default: |
| 1043 | TEST_ASSERT( 0 ); |
| 1044 | break; |
| 1045 | } |
| 1046 | |
| 1047 | if( success == 1 ) |
| 1048 | { |
| 1049 | /* In all succeeding cases, fetch the rest of the second buffer. */ |
| 1050 | TEST_ASSERT( mbedtls_reader_get( &rd, 90, &tmp, NULL ) == 0 ); |
| 1051 | ASSERT_COMPARE( tmp, 90, bufB + 10, 90 ); |
| 1052 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 1053 | |
| 1054 | /* Wrapup */ |
| 1055 | TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); |
| 1056 | } |
| 1057 | |
| 1058 | /* Wrapup */ |
| 1059 | mbedtls_reader_free( &rd ); |
| 1060 | } |
| 1061 | /* END_CASE */ |
Hanno Becker | 2b8bad3 | 2021-01-12 09:40:05 +0000 | [diff] [blame] | 1062 | |
| 1063 | /* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */ |
| 1064 | void mbedtls_mps_reader_feed_empty( int option ) |
| 1065 | { |
| 1066 | /* This test exercises the behaviour of the reader when it is |
| 1067 | * fed a NULL buffer. */ |
| 1068 | unsigned char buf[100]; |
| 1069 | unsigned char *tmp; |
| 1070 | mbedtls_reader rd; |
| 1071 | for( int i=0; (unsigned) i < sizeof( buf ); i++ ) |
| 1072 | buf[i] = (unsigned char) i; |
| 1073 | |
| 1074 | /* Preparation (lower layer) */ |
| 1075 | mbedtls_reader_init( &rd, NULL, 0 ); |
| 1076 | switch( option ) |
| 1077 | { |
| 1078 | case 0: /* NULL buffer */ |
| 1079 | TEST_ASSERT( mbedtls_reader_feed( &rd, NULL, sizeof( buf ) ) == |
| 1080 | MBEDTLS_ERR_MPS_READER_INVALID_ARG ); |
| 1081 | break; |
| 1082 | |
| 1083 | default: |
| 1084 | TEST_ASSERT( 0 ); |
| 1085 | break; |
| 1086 | } |
| 1087 | /* Subsequent feed-calls should still succeed. */ |
| 1088 | TEST_ASSERT( mbedtls_reader_feed( &rd, buf, sizeof( buf ) ) == 0 ); |
| 1089 | |
| 1090 | /* Consumption (upper layer) */ |
| 1091 | TEST_ASSERT( mbedtls_reader_get( &rd, 100, &tmp, NULL ) == 0 ); |
| 1092 | ASSERT_COMPARE( tmp, 100, buf, 100 ); |
| 1093 | TEST_ASSERT( mbedtls_reader_commit( &rd ) == 0 ); |
| 1094 | |
| 1095 | /* Wrapup */ |
| 1096 | TEST_ASSERT( mbedtls_reader_reclaim( &rd, NULL ) == 0 ); |
| 1097 | mbedtls_reader_free( &rd ); |
| 1098 | } |
| 1099 | /* END_CASE */ |