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 */
|
|
|
|
|
2021-03-05 06:16:45 +01:00
|
|
|
/* BEGIN_DEPENDENCIES
|
2021-12-08 16:57:54 +01:00
|
|
|
* depends_on:MBEDTLS_SSL_PROTO_TLS1_3
|
2021-03-05 06:16:45 +01:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
2021-01-12 08:43:30 +01:00
|
|
|
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mps_reader_no_pausing_single_step_single_round(int with_acc)
|
2021-01-12 08:43:30 +01:00
|
|
|
{
|
|
|
|
/* 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.
|
2021-02-22 17:52:02 +01:00
|
|
|
* - After processing, the consuming layer commits the data
|
|
|
|
* and the reader is moved back to producing mode.
|
2021-01-12 08:43:30 +01:00
|
|
|
*
|
|
|
|
* 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-03-08 17:25:38 +01:00
|
|
|
int paused;
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader rd;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(bufA); i++) {
|
2021-01-12 08:43:30 +01:00
|
|
|
bufA[i] = (unsigned char) i;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-01-12 08:43:30 +01:00
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (with_acc == 0) {
|
|
|
|
mbedtls_mps_reader_init(&rd, NULL, 0);
|
|
|
|
} else {
|
|
|
|
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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 100, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 100, bufA, 100);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
2021-01-12 08:43:30 +01:00
|
|
|
/* Wrapup (lower layer) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_reclaim(&rd, &paused) == 0);
|
|
|
|
TEST_ASSERT(paused == 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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mps_reader_no_pausing_single_step_multiple_rounds(int with_acc)
|
2021-01-12 08:52:29 +01:00
|
|
|
{
|
2021-02-22 17:52:02 +01:00
|
|
|
/* This test exercises multiple rounds of the basic use of the MPS reader:
|
2021-01-12 08:52:29 +01:00
|
|
|
* - The 'producing' layer provides a buffer
|
|
|
|
* - The 'consuming' layer fetches it in a single go.
|
2021-02-22 17:52:02 +01:00
|
|
|
* - After processing, the consuming layer commits the data
|
|
|
|
* and the reader is moved back to producing mode.
|
2021-01-12 08:52:29 +01:00
|
|
|
*
|
|
|
|
* 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;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(bufA); i++) {
|
2021-01-12 08:52:29 +01:00
|
|
|
bufA[i] = (unsigned char) i;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(bufB); i++) {
|
|
|
|
bufB[i] = ~((unsigned char) i);
|
|
|
|
}
|
2021-01-12 08:52:29 +01:00
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (with_acc == 0) {
|
|
|
|
mbedtls_mps_reader_init(&rd, NULL, 0);
|
|
|
|
} else {
|
|
|
|
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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 100, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 100, bufA, 100);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
2021-01-12 08:52:29 +01:00
|
|
|
/* Preparation */
|
2023-01-11 14:50:10 +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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 100, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 100, bufB, 100);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
2021-01-12 08:52:29 +01:00
|
|
|
/* Wrapup (lower layer) */
|
2023-01-11 14:50:10 +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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mps_reader_no_pausing_multiple_steps_single_round(int with_acc)
|
2021-01-12 09:01:16 +01:00
|
|
|
{
|
|
|
|
/* This test exercises one round of the following:
|
|
|
|
* - The 'producing' layer provides a buffer
|
|
|
|
* - The 'consuming' layer fetches it in multiple calls
|
2021-02-22 17:52:02 +01:00
|
|
|
* to `mbedtls_mps_reader_get()`, without committing in between.
|
|
|
|
* - After processing, the consuming layer commits the data
|
|
|
|
* and the reader is moved back to producing mode.
|
2021-01-12 09:01:16 +01:00
|
|
|
*
|
|
|
|
* 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,
|
2021-02-22 17:52:02 +01:00
|
|
|
* since the consumer's requests can be fulfilled from the data
|
2021-01-12 09:01:16 +01:00
|
|
|
* 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;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(buf); i++) {
|
2021-01-12 09:01:16 +01:00
|
|
|
buf[i] = (unsigned char) i;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-01-12 09:01:16 +01:00
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (with_acc == 0) {
|
|
|
|
mbedtls_mps_reader_init(&rd, NULL, 0);
|
|
|
|
} else {
|
|
|
|
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) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 10, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, buf, 10);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 70, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 70, buf + 10, 70);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 30, &tmp, &tmp_len) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, tmp_len, buf + 80, 20);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
2021-01-12 09:01:16 +01:00
|
|
|
/* Wrapup (lower layer) */
|
2023-01-11 14:50:10 +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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mps_reader_no_pausing_multiple_steps_multiple_rounds(int with_acc)
|
2021-01-12 09:11:40 +01:00
|
|
|
{
|
|
|
|
/* 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;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(bufA); i++) {
|
2021-01-12 09:11:40 +01:00
|
|
|
bufA[i] = (unsigned char) i;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(bufB); i++) {
|
|
|
|
bufB[i] = ~((unsigned char) i);
|
|
|
|
}
|
2021-01-12 09:11:40 +01:00
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (with_acc == 0) {
|
|
|
|
mbedtls_mps_reader_init(&rd, NULL, 0);
|
|
|
|
} else {
|
|
|
|
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) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 10, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufA, 10);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 70, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 70, bufA + 10, 70);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 30, &tmp, &tmp_len) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, tmp_len, bufA + 80, 20);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
2021-01-12 09:11:40 +01:00
|
|
|
/* Preparation */
|
2023-01-11 14:50:10 +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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 100, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 100, bufB, 100);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
2021-01-12 09:11:40 +01:00
|
|
|
/* Wrapup */
|
2023-01-11 14:50:10 +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()
|
|
|
|
{
|
2021-02-22 17:52:02 +01:00
|
|
|
/* This test exercises the behaviour of the MPS reader when a read request
|
2021-01-12 09:14:38 +01:00
|
|
|
* 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;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(buf); i++) {
|
2021-01-12 09:14:38 +01:00
|
|
|
buf[i] = (unsigned char) i;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-01-12 09:14:38 +01:00
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2023-01-11 14:50:10 +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) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 50, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 50, buf, 50);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 100, &tmp, NULL) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA);
|
2021-01-12 09:14:38 +01:00
|
|
|
/* Wrapup (lower layer) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_reclaim(&rd, NULL) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_NEED_ACCUMULATOR);
|
|
|
|
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
|
2021-02-22 17:52:02 +01:00
|
|
|
* in the situation where a read request goes beyond the bounds of the
|
2021-01-12 09:18:12 +01:00
|
|
|
* 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;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(buf); i++) {
|
2021-01-12 09:18:12 +01:00
|
|
|
buf[i] = (unsigned char) i;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-01-12 09:18:12 +01:00
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2023-01-11 14:50:10 +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) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 50, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 50, buf, 50);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 10, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, buf + 50, 10);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 100, &tmp, NULL) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA);
|
2021-01-12 09:18:12 +01:00
|
|
|
/* Wrapup (lower layer) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_reclaim(&rd, NULL) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL);
|
2021-02-08 07:52:21 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 50, &tmp, &tmp_len) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, tmp_len, buf + 50, 50);
|
2021-02-08 07:52:21 +01:00
|
|
|
|
2023-01-11 14:50:10 +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
|
|
|
|
2021-03-08 17:57:08 +01:00
|
|
|
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
|
|
|
|
void mbedtls_mps_reader_reclaim_overflow()
|
|
|
|
{
|
|
|
|
/* This test exercises the behaviour of the MPS reader with accumulator
|
|
|
|
* in the situation where upon calling mbedtls_mps_reader_reclaim(), the
|
|
|
|
* uncommitted data together with the excess data missing in the last
|
2022-12-04 18:19:59 +01:00
|
|
|
* call to mbedtls_mps_reader_get() exceeds the bounds of the type
|
2021-03-08 17:57:08 +01:00
|
|
|
* holding the buffer length.
|
|
|
|
*/
|
|
|
|
|
|
|
|
unsigned char buf[100];
|
|
|
|
unsigned char acc[50];
|
|
|
|
unsigned char *tmp;
|
|
|
|
mbedtls_mps_reader rd;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(buf); i++) {
|
2021-03-19 06:23:30 +01:00
|
|
|
buf[i] = (unsigned char) i;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-03-19 06:23:30 +01:00
|
|
|
|
2021-03-08 17:57:08 +01:00
|
|
|
/* Preparation (lower layer) */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mps_reader_init(&rd, acc, sizeof(acc));
|
|
|
|
TEST_ASSERT(mbedtls_mps_reader_feed(&rd, buf, sizeof(buf)) == 0);
|
2021-03-08 17:57:08 +01:00
|
|
|
/* Consumption (upper layer) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 50, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 50, buf, 50);
|
2021-03-08 17:57:08 +01:00
|
|
|
/* Excess request */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, (mbedtls_mps_size_t) -1, &tmp, NULL) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA);
|
2021-03-08 17:57:08 +01:00
|
|
|
/* Wrapup (lower layer) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_reclaim(&rd, NULL) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL);
|
2021-03-08 17:57:08 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mps_reader_free(&rd);
|
2021-03-08 17:57:08 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-01-12 09:27:29 +01:00
|
|
|
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mps_reader_pausing(int option)
|
2021-01-12 09:27:29 +01:00
|
|
|
{
|
|
|
|
/* This test exercises the behaviour of the reader when the
|
2021-03-08 17:28:09 +01:00
|
|
|
* accumulator is used to fulfill a consumer's request.
|
2021-01-12 09:27:29 +01:00
|
|
|
*
|
|
|
|
* 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-03-08 17:25:38 +01:00
|
|
|
int paused;
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader rd;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(bufA); i++) {
|
2021-01-12 09:27:29 +01:00
|
|
|
bufA[i] = (unsigned char) i;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(bufB); i++) {
|
|
|
|
bufB[i] = ~((unsigned char) i);
|
|
|
|
}
|
2021-01-12 09:27:29 +01:00
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2023-01-11 14:50:10 +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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 80, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 80, bufA, 80);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 10, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufA + 80, 10);
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (option) {
|
2021-01-12 09:27:29 +01:00
|
|
|
case 0: /* Single uncommitted fetch at pausing */
|
|
|
|
case 1:
|
2023-01-11 14:50:10 +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;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 20, &tmp, NULL) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA);
|
2021-01-12 09:27:29 +01:00
|
|
|
|
|
|
|
/* Preparation */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_reclaim(&rd, &paused) == 0);
|
|
|
|
TEST_ASSERT(paused == 1);
|
|
|
|
TEST_ASSERT(mbedtls_mps_reader_feed(&rd, bufB, sizeof(bufB)) == 0);
|
2021-01-12 09:27:29 +01:00
|
|
|
|
|
|
|
/* Consumption */
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (option) {
|
2021-01-12 09:27:29 +01:00
|
|
|
case 0: /* Single fetch at pausing, re-fetch with commit. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 20, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufA + 90, 10);
|
|
|
|
TEST_MEMORY_COMPARE(tmp + 10, 10, bufB, 10);
|
2023-01-11 14:50:10 +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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 20, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufA + 90, 10);
|
|
|
|
TEST_MEMORY_COMPARE(tmp + 10, 10, bufB, 10);
|
2021-01-12 09:27:29 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* Multiple fetches at pausing, repeat without commit. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 10, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufA + 80, 10);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 20, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufA + 90, 10);
|
|
|
|
TEST_MEMORY_COMPARE(tmp + 10, 10, bufB, 10);
|
2021-01-12 09:27:29 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: /* Multiple fetches at pausing, repeat with commit 1. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 10, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufA + 80, 10);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 20, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufA + 90, 10);
|
|
|
|
TEST_MEMORY_COMPARE(tmp + 10, 10, bufB, 10);
|
2021-01-12 09:27:29 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 4: /* Multiple fetches at pausing, repeat with commit 2. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 10, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufA + 80, 10);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 20, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufA + 90, 10);
|
|
|
|
TEST_MEMORY_COMPARE(tmp + 10, 10, bufB, 10);
|
2023-01-11 14:50:10 +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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 10, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufA + 80, 10);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 20, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufA + 90, 10);
|
|
|
|
TEST_MEMORY_COMPARE(tmp + 10, 10, bufB, 10);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
2021-01-12 09:27:29 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(0);
|
2021-01-12 09:27:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* In all cases, fetch the rest of the second buffer. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 90, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 90, bufB + 10, 90);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
2021-01-12 09:27:29 +01:00
|
|
|
|
|
|
|
/* Wrapup */
|
2023-01-11 14:50:10 +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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mps_reader_pausing_multiple_feeds(int option)
|
2021-01-12 09:36:36 +01:00
|
|
|
{
|
|
|
|
/* This test exercises the behaviour of the MPS reader
|
|
|
|
* in the following situation:
|
2021-02-22 17:52:02 +01:00
|
|
|
* - The consumer has asked for more than what's available, so the
|
2021-01-12 09:36:36 +01:00
|
|
|
* 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;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(bufA); i++) {
|
2021-01-12 09:36:36 +01:00
|
|
|
bufA[i] = (unsigned char) i;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(bufB); i++) {
|
|
|
|
bufB[i] = ~((unsigned char) i);
|
|
|
|
}
|
2021-01-12 09:36:36 +01:00
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2023-01-11 14:50:10 +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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 80, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 80, bufA, 80);
|
2023-01-11 14:50:10 +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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 70, &tmp, NULL) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA);
|
2021-01-12 09:36:36 +01:00
|
|
|
|
|
|
|
/* Preparation */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_reclaim(&rd, NULL) == 0);
|
|
|
|
switch (option) {
|
2021-01-12 09:36:36 +01:00
|
|
|
case 0: /* 10 + 10 + 80 byte feed */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_feed(&rd, bufB, 10) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_NEED_MORE);
|
|
|
|
TEST_ASSERT(mbedtls_mps_reader_feed(&rd, bufB + 10, 10) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_NEED_MORE);
|
|
|
|
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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t num_feed = 0; num_feed < 49; num_feed++) {
|
|
|
|
TEST_ASSERT(mbedtls_mps_reader_feed(&rd, bufB + num_feed, 1) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_NEED_MORE);
|
2021-01-12 09:36:36 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t num_feed = 0; num_feed < 49; num_feed++) {
|
|
|
|
TEST_ASSERT(mbedtls_mps_reader_feed(&rd, bufB + num_feed, 1) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_NEED_MORE);
|
2021-01-12 09:36:36 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_feed(&rd, bufB + 49, 51) == 0);
|
2021-01-12 09:36:36 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(0);
|
2021-01-12 09:36:36 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Consumption */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 70, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 20, bufA + 80, 20);
|
|
|
|
TEST_MEMORY_COMPARE(tmp + 20, 50, bufB, 50);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 1000, &tmp, &fetch_len) == 0);
|
|
|
|
switch (option) {
|
2021-01-12 09:36:36 +01:00
|
|
|
case 0:
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(fetch_len == 50);
|
2021-01-12 09:36:36 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(fetch_len == 0);
|
2021-01-12 09:36:36 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(fetch_len == 50);
|
2021-01-12 09:36:36 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(0);
|
2021-01-12 09:36:36 +01:00
|
|
|
break;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
2021-01-12 09:36:36 +01:00
|
|
|
|
|
|
|
/* Wrapup */
|
2023-01-11 14:50:10 +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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mps_reader_reclaim_data_left(int option)
|
2021-01-12 09:39:37 +01:00
|
|
|
{
|
|
|
|
/* 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;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(buf); i++) {
|
2021-01-12 09:39:37 +01:00
|
|
|
buf[i] = (unsigned char) i;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-01-12 09:39:37 +01:00
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2023-01-11 14:50:10 +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) */
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (option) {
|
2021-01-12 09:39:37 +01:00
|
|
|
case 0:
|
|
|
|
/* Fetch (but not commit) the entire buffer. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, sizeof(buf), &tmp, NULL)
|
|
|
|
== 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 100, buf, 100);
|
2021-01-12 09:39:37 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
/* Fetch (but not commit) parts of the buffer. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, sizeof(buf) / 2,
|
|
|
|
&tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, sizeof(buf) / 2, buf, sizeof(buf) / 2);
|
2021-01-12 09:39:37 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
/* Fetch and commit parts of the buffer, then
|
|
|
|
* fetch but not commit the rest of the buffer. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, sizeof(buf) / 2,
|
|
|
|
&tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, sizeof(buf) / 2, buf, sizeof(buf) / 2);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, sizeof(buf) / 2,
|
|
|
|
&tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, sizeof(buf) / 2,
|
2023-07-27 15:17:27 +02:00
|
|
|
buf + sizeof(buf) / 2,
|
|
|
|
sizeof(buf) / 2);
|
2021-01-12 09:39:37 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(0);
|
2021-01-12 09:39:37 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wrapup */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_reclaim(&rd, NULL) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_DATA_LEFT);
|
|
|
|
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
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(buf); i++) {
|
2021-01-12 09:43:58 +01:00
|
|
|
buf[i] = (unsigned char) i;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-01-12 09:43:58 +01:00
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2023-01-11 14:50:10 +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) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 50, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 50, buf, 50);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 50, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 50, buf + 50, 50);
|
2021-01-12 09:43:58 +01:00
|
|
|
/* Preparation */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_reclaim(&rd, NULL) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_DATA_LEFT);
|
2021-01-12 09:43:58 +01:00
|
|
|
/* Consumption */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 50, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 50, buf + 50, 50);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
2021-01-12 09:43:58 +01:00
|
|
|
/* Wrapup */
|
2023-01-11 14:50:10 +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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mps_reader_multiple_pausing(int option)
|
2021-01-12 10:17:56 +01:00
|
|
|
{
|
|
|
|
/* 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.
|
2021-02-22 17:52:02 +01:00
|
|
|
* - Once enough data is available, the consumer successfully
|
2021-01-12 10:17:56 +01:00
|
|
|
* 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;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(bufA); i++) {
|
2021-01-12 10:17:56 +01:00
|
|
|
bufA[i] = (unsigned char) i;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(bufB); i++) {
|
|
|
|
bufB[i] = ~((unsigned char) i);
|
|
|
|
}
|
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(bufC); i++) {
|
|
|
|
bufC[i] = ~((unsigned char) i);
|
|
|
|
}
|
2021-01-12 10:17:56 +01:00
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2023-01-11 14:50:10 +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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 80, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 80, bufA, 80);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 10, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufA + 80, 10);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 20, &tmp, NULL) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA);
|
2021-01-12 10:17:56 +01:00
|
|
|
|
|
|
|
/* Preparation */
|
2023-01-11 14:50:10 +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
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (option) {
|
2021-01-12 10:17:56 +01:00
|
|
|
case 0: /* Fetch same chunks, commit afterwards, and
|
|
|
|
* then exceed bounds of new buffer; accumulator
|
|
|
|
* large enough. */
|
|
|
|
|
|
|
|
/* Consume */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 10, &tmp, &tmp_len) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, tmp_len, bufA + 80, 10);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 20, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufA + 90, 10);
|
|
|
|
TEST_MEMORY_COMPARE(tmp + 10, 10, bufB, 10);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 20, &tmp, NULL) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA);
|
2021-01-12 10:17:56 +01:00
|
|
|
|
|
|
|
/* Prepare */
|
2023-01-11 14:50:10 +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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 20, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufB + 10, 10);
|
|
|
|
TEST_MEMORY_COMPARE(tmp + 10, 10, bufC, 10);
|
2021-01-12 10:17:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* Fetch same chunks, commit afterwards, and
|
|
|
|
* then exceed bounds of new buffer; accumulator
|
|
|
|
* not large enough. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 10, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufA + 80, 10);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 20, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufA + 90, 10);
|
|
|
|
TEST_MEMORY_COMPARE(tmp + 10, 10, bufB, 10);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 51, &tmp, NULL) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA);
|
2021-01-12 10:17:56 +01:00
|
|
|
|
|
|
|
/* Prepare */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_reclaim(&rd, NULL) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL);
|
2021-01-12 10:17:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* Fetch same chunks, don't commit afterwards, and
|
|
|
|
* then exceed bounds of new buffer; accumulator
|
|
|
|
* large enough. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 10, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufA + 80, 10);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 20, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufA + 90, 10);
|
|
|
|
TEST_MEMORY_COMPARE(tmp + 10, 10, bufB, 10);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 20, &tmp, NULL) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA);
|
2021-01-12 10:17:56 +01:00
|
|
|
|
|
|
|
/* Prepare */
|
2023-01-11 14:50:10 +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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 50, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 20, bufA + 80, 20);
|
|
|
|
TEST_MEMORY_COMPARE(tmp + 20, 20, bufB, 20);
|
|
|
|
TEST_MEMORY_COMPARE(tmp + 40, 10, bufC, 10);
|
2021-01-12 10:17:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: /* Fetch same chunks, don't commit afterwards, and
|
|
|
|
* then exceed bounds of new buffer; accumulator
|
|
|
|
* not large enough. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 10, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufA + 80, 10);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 20, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 10, bufA + 90, 10);
|
|
|
|
TEST_MEMORY_COMPARE(tmp + 10, 10, bufB, 10);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 21, &tmp, NULL) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA);
|
2021-01-12 10:17:56 +01:00
|
|
|
|
|
|
|
/* Prepare */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_reclaim(&rd, NULL) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_ACCUMULATOR_TOO_SMALL);
|
2021-01-12 10:17:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(0);
|
2021-01-12 10:17:56 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mps_reader_random_usage(int num_out_chunks,
|
|
|
|
int max_chunk_size,
|
|
|
|
int max_request,
|
|
|
|
int acc_size)
|
2021-01-12 10:23:15 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
/* 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
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (acc_size > 0) {
|
2023-07-21 12:31:13 +02:00
|
|
|
TEST_CALLOC(acc, acc_size);
|
2021-01-12 10:23:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This probably needs to be changed because we want
|
|
|
|
* our tests to be deterministic. */
|
|
|
|
// srand( time( NULL ) );
|
|
|
|
|
2023-07-21 12:31:13 +02:00
|
|
|
TEST_CALLOC(outgoing, num_out_chunks * max_chunk_size);
|
|
|
|
TEST_CALLOC(incoming, num_out_chunks * max_chunk_size);
|
2021-01-12 10:23:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +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;
|
2023-01-11 14:50:10 +01:00
|
|
|
while (cur_out_chunk < (unsigned) num_out_chunks) {
|
|
|
|
if (mode == 0) {
|
2021-01-12 10:23:15 +01:00
|
|
|
/* Choose randomly between reclaim and feed */
|
|
|
|
rand_op = rand() % 2;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rand_op == 0) {
|
2021-01-12 10:23:15 +01:00
|
|
|
/* Reclaim */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_mps_reader_reclaim(&rd, NULL);
|
2021-01-12 10:23:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0) {
|
|
|
|
TEST_ASSERT(cur_chunk != NULL);
|
|
|
|
mbedtls_free(cur_chunk);
|
2021-01-12 10:23:15 +01:00
|
|
|
cur_chunk = NULL;
|
|
|
|
}
|
|
|
|
reclaimed = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2021-01-12 10:23:15 +01:00
|
|
|
/* Feed reader with a random chunk */
|
|
|
|
unsigned char *tmp = NULL;
|
|
|
|
size_t tmp_size;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (cur_out_chunk == (unsigned) num_out_chunks) {
|
2021-01-12 10:23:15 +01:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-01-12 10:23:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
tmp_size = (rand() % max_chunk_size) + 1;
|
2023-07-21 12:31:13 +02:00
|
|
|
TEST_CALLOC(tmp, tmp_size);
|
2021-01-12 10:23:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_test_rnd_std_rand(NULL, tmp, tmp_size) == 0);
|
|
|
|
ret = mbedtls_mps_reader_feed(&rd, tmp, tmp_size);
|
2021-01-12 10:23:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0 || ret == MBEDTLS_ERR_MPS_READER_NEED_MORE) {
|
2021-01-12 10:23:15 +01:00
|
|
|
cur_out_chunk++;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(outgoing + out_pos, tmp, tmp_size);
|
2021-01-12 10:23:15 +01:00
|
|
|
out_pos += tmp_size;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0) {
|
|
|
|
TEST_ASSERT(cur_chunk == NULL);
|
2021-01-12 10:23:15 +01:00
|
|
|
cur_chunk = tmp;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
mbedtls_free(tmp);
|
2021-01-12 10:23:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Randomly switch to consumption mode if reclaim
|
|
|
|
* was called at least once. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (reclaimed == 1 && rand() % 3 == 0) {
|
2021-01-12 10:23:15 +01:00
|
|
|
in_fetch = 0;
|
|
|
|
mode = 1;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2021-01-12 10:23:15 +01:00
|
|
|
/* Choose randomly between get tolerating fewer data,
|
|
|
|
* get not tolerating fewer data, and commit. */
|
|
|
|
rand_op = rand() % 3;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rand_op == 0 || rand_op == 1) {
|
2021-01-12 10:23:15 +01:00
|
|
|
mbedtls_mps_size_t get_size, real_size;
|
|
|
|
unsigned char *chunk_get;
|
2023-01-11 14:50:10 +01:00
|
|
|
get_size = (rand() % max_request) + 1;
|
|
|
|
if (rand_op == 0) {
|
|
|
|
ret = mbedtls_mps_reader_get(&rd, get_size, &chunk_get,
|
|
|
|
&real_size);
|
|
|
|
} else {
|
2021-01-12 10:23:15 +01:00
|
|
|
real_size = get_size;
|
2023-01-11 14:50:10 +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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
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);
|
2021-01-12 10:23:15 +01:00
|
|
|
in_fetch += real_size;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (rand_op == 2) { /* Commit */
|
|
|
|
ret = mbedtls_mps_reader_commit(&rd);
|
|
|
|
if (ret == 0) {
|
2021-01-12 10:23:15 +01:00
|
|
|
in_commit += in_fetch;
|
|
|
|
in_fetch = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Randomly switch back to preparation */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rand() % 3 == 0) {
|
2021-01-12 10:23:15 +01:00
|
|
|
reclaimed = 0;
|
|
|
|
mode = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cleanup */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mps_reader_free(&rd);
|
|
|
|
mbedtls_free(incoming);
|
|
|
|
mbedtls_free(outgoing);
|
|
|
|
mbedtls_free(acc);
|
|
|
|
mbedtls_free(cur_chunk);
|
2021-01-12 10:23:15 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-01-12 10:31:31 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:TEST_SUITE_MPS_READER */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_reader_inconsistent_usage(int option)
|
2021-01-12 10:31:31 +01:00
|
|
|
{
|
|
|
|
/* 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;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(bufA); i++) {
|
2021-01-12 10:31:31 +01:00
|
|
|
bufA[i] = (unsigned char) i;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(bufB); i++) {
|
|
|
|
bufB[i] = ~((unsigned char) i);
|
|
|
|
}
|
2021-01-12 10:31:31 +01:00
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2023-01-11 14:50:10 +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) */
|
2023-01-11 14:50:10 +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) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_OUT_OF_DATA);
|
2021-01-12 10:31:31 +01:00
|
|
|
/* Preparation */
|
2023-01-11 14:50:10 +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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (option) {
|
2021-01-12 10:31:31 +01:00
|
|
|
case 0:
|
|
|
|
/* Ask for buffered data in a single chunk, no commit */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 30, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 20, bufA + 80, 20);
|
|
|
|
TEST_MEMORY_COMPARE(tmp + 20, 10, bufB, 10);
|
2021-01-12 10:31:31 +01:00
|
|
|
success = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
/* Ask for buffered data in a single chunk, with commit */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 30, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 20, bufA + 80, 20);
|
|
|
|
TEST_MEMORY_COMPARE(tmp + 20, 10, bufB, 10);
|
2023-01-11 14:50:10 +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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 31, &tmp, NULL) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS);
|
2021-01-12 10:31:31 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
/* Ask for more than was requested when pausing #2 */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, (mbedtls_mps_size_t) -1, &tmp, NULL) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS);
|
2021-01-12 10:31:31 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
/* Asking for buffered data in different
|
|
|
|
* chunks than before CAN fail. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 15, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 15, bufA + 80, 15);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 10, &tmp, NULL) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_INCONSISTENT_REQUESTS);
|
2021-01-12 10:31:31 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
/* Asking for buffered data different chunks
|
|
|
|
* than before NEED NOT fail - no commits */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 15, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 15, bufA + 80, 15);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 15, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 5, bufA + 95, 5);
|
|
|
|
TEST_MEMORY_COMPARE(tmp + 5, 10, bufB, 10);
|
2021-01-12 10:31:31 +01:00
|
|
|
success = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
/* Asking for buffered data different chunks
|
|
|
|
* than before NEED NOT fail - intermediate commit */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 15, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 15, bufA + 80, 15);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 15, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 5, bufA + 95, 5);
|
|
|
|
TEST_MEMORY_COMPARE(tmp + 5, 10, bufB, 10);
|
2021-01-12 10:31:31 +01:00
|
|
|
success = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 7:
|
|
|
|
/* Asking for buffered data different chunks
|
|
|
|
* than before NEED NOT fail - end commit */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 15, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 15, bufA + 80, 15);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 15, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 5, bufA + 95, 5);
|
|
|
|
TEST_MEMORY_COMPARE(tmp + 5, 10, bufB, 10);
|
2023-01-11 14:50:10 +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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 15, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 15, bufA + 80, 15);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 15, &tmp, NULL) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 5, bufA + 95, 5);
|
|
|
|
TEST_MEMORY_COMPARE(tmp + 5, 10, bufB, 10);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
2021-01-12 10:31:31 +01:00
|
|
|
success = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(0);
|
2021-01-12 10:31:31 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (success == 1) {
|
2021-01-12 10:31:31 +01:00
|
|
|
/* In all succeeding cases, fetch the rest of the second buffer. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 90, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 90, bufB + 10, 90);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
2021-01-12 10:31:31 +01:00
|
|
|
|
|
|
|
/* Wrapup */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_reclaim(&rd, NULL) == 0);
|
2021-01-12 10:31:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wrapup */
|
2023-01-11 14:50:10 +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 */
|
2021-02-22 17:57:14 +01:00
|
|
|
void mbedtls_mps_reader_feed_empty()
|
2021-01-12 10:40:05 +01:00
|
|
|
{
|
|
|
|
/* This test exercises the behaviour of the reader when it is
|
2021-02-22 17:52:02 +01:00
|
|
|
* fed with a NULL buffer. */
|
2021-01-12 10:40:05 +01:00
|
|
|
unsigned char buf[100];
|
|
|
|
unsigned char *tmp;
|
2021-01-28 10:45:47 +01:00
|
|
|
mbedtls_mps_reader rd;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; (unsigned) i < sizeof(buf); i++) {
|
2021-01-12 10:40:05 +01:00
|
|
|
buf[i] = (unsigned char) i;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-01-12 10:40:05 +01:00
|
|
|
|
|
|
|
/* Preparation (lower layer) */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mps_reader_init(&rd, NULL, 0);
|
2021-01-12 10:40:05 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_feed(&rd, NULL, sizeof(buf)) ==
|
|
|
|
MBEDTLS_ERR_MPS_READER_INVALID_ARG);
|
2021-02-22 17:57:14 +01:00
|
|
|
|
2021-01-12 10:40:05 +01:00
|
|
|
/* Subsequent feed-calls should still succeed. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_feed(&rd, buf, sizeof(buf)) == 0);
|
2021-01-12 10:40:05 +01:00
|
|
|
|
|
|
|
/* Consumption (upper layer) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_get(&rd, 100, &tmp, NULL) == 0);
|
2023-07-21 12:40:20 +02:00
|
|
|
TEST_MEMORY_COMPARE(tmp, 100, buf, 100);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mps_reader_commit(&rd) == 0);
|
2021-01-12 10:40:05 +01:00
|
|
|
|
|
|
|
/* Wrapup */
|
2023-01-11 14:50:10 +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 */
|