Ronald Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file helpers.h |
| 3 | * |
| 4 | * \brief This file contains the prototypes of helper functions for the |
| 5 | * purpose of testing. |
| 6 | */ |
| 7 | |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 8 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 9 | * Copyright The Mbed TLS Contributors |
Ronald Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: Apache-2.0 |
| 11 | * |
| 12 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 13 | * not use this file except in compliance with the License. |
| 14 | * You may obtain a copy of the License at |
| 15 | * |
| 16 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | * |
| 18 | * Unless required by applicable law or agreed to in writing, software |
| 19 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | * See the License for the specific language governing permissions and |
| 22 | * limitations under the License. |
Ronald Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | #ifndef TEST_HELPERS_H |
| 26 | #define TEST_HELPERS_H |
| 27 | |
Mateusz Starzyk | b198272 | 2021-05-27 14:46:48 +0200 | [diff] [blame] | 28 | /* Most fields of publicly available structs are private and are wrapped with |
| 29 | * MBEDTLS_PRIVATE macro. This define allows tests to access the private fields |
| 30 | * directly (without using the MBEDTLS_PRIVATE wrapper). */ |
Mateusz Starzyk | 2c09c9b | 2021-05-14 22:20:10 +0200 | [diff] [blame] | 31 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS |
| 32 | |
Ronald Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 33 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 34 | #include "mbedtls/config.h" |
| 35 | #else |
| 36 | #include MBEDTLS_CONFIG_FILE |
| 37 | #endif |
| 38 | |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 39 | #if defined(MBEDTLS_THREADING_C) && defined(MBEDTLS_THREADING_PTHREAD) && \ |
| 40 | defined(MBEDTLS_TEST_HOOKS) |
| 41 | #define MBEDTLS_TEST_MUTEX_USAGE |
| 42 | #endif |
| 43 | |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 44 | #if defined(MBEDTLS_PLATFORM_C) |
| 45 | #include "mbedtls/platform.h" |
| 46 | #else |
| 47 | #include <stdio.h> |
| 48 | #define mbedtls_fprintf fprintf |
| 49 | #define mbedtls_snprintf snprintf |
| 50 | #define mbedtls_calloc calloc |
| 51 | #define mbedtls_free free |
| 52 | #define mbedtls_exit exit |
| 53 | #define mbedtls_time time |
| 54 | #define mbedtls_time_t time_t |
| 55 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
| 56 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 57 | #endif |
| 58 | |
| 59 | #include <stddef.h> |
| 60 | #include <stdint.h> |
| 61 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 62 | typedef enum |
| 63 | { |
Chris Jones | e60e2ae | 2021-01-20 17:51:47 +0000 | [diff] [blame] | 64 | MBEDTLS_TEST_RESULT_SUCCESS = 0, |
| 65 | MBEDTLS_TEST_RESULT_FAILED, |
| 66 | MBEDTLS_TEST_RESULT_SKIPPED |
| 67 | } mbedtls_test_result_t; |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 68 | |
| 69 | typedef struct |
| 70 | { |
Chris Jones | e60e2ae | 2021-01-20 17:51:47 +0000 | [diff] [blame] | 71 | mbedtls_test_result_t result; |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 72 | const char *test; |
| 73 | const char *filename; |
| 74 | int line_no; |
| 75 | unsigned long step; |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 76 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 77 | const char *mutex_usage_error; |
| 78 | #endif |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 79 | } |
Chris Jones | e60e2ae | 2021-01-20 17:51:47 +0000 | [diff] [blame] | 80 | mbedtls_test_info_t; |
| 81 | extern mbedtls_test_info_t mbedtls_test_info; |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 82 | |
Ronald Cron | e9c09f1 | 2020-06-08 16:44:58 +0200 | [diff] [blame] | 83 | int mbedtls_test_platform_setup( void ); |
| 84 | void mbedtls_test_platform_teardown( void ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 85 | |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 86 | /** |
Chris Jones | 39ddb0a | 2021-02-03 16:15:00 +0000 | [diff] [blame] | 87 | * \brief Record the current test case as a failure. |
Chris Jones | 567e0ad | 2021-02-03 12:07:01 +0000 | [diff] [blame] | 88 | * |
Chris Jones | 39ddb0a | 2021-02-03 16:15:00 +0000 | [diff] [blame] | 89 | * This function can be called directly however it is usually |
| 90 | * called via macros such as TEST_ASSERT, TEST_EQUAL, |
| 91 | * PSA_ASSERT, etc... |
| 92 | * |
| 93 | * \note If the test case was already marked as failed, calling |
| 94 | * `mbedtls_test_fail( )` again will not overwrite any |
| 95 | * previous information about the failure. |
| 96 | * |
| 97 | * \param test Description of the failure or assertion that failed. This |
| 98 | * MUST be a string literal. |
Chris Jones | 567e0ad | 2021-02-03 12:07:01 +0000 | [diff] [blame] | 99 | * \param line_no Line number where the failure originated. |
| 100 | * \param filename Filename where the failure originated. |
| 101 | */ |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 102 | void mbedtls_test_fail( const char *test, int line_no, const char* filename ); |
Chris Jones | 567e0ad | 2021-02-03 12:07:01 +0000 | [diff] [blame] | 103 | |
| 104 | /** |
Chris Jones | 39ddb0a | 2021-02-03 16:15:00 +0000 | [diff] [blame] | 105 | * \brief Record the current test case as skipped. |
Chris Jones | 567e0ad | 2021-02-03 12:07:01 +0000 | [diff] [blame] | 106 | * |
Chris Jones | 39ddb0a | 2021-02-03 16:15:00 +0000 | [diff] [blame] | 107 | * This function can be called directly however it is usually |
| 108 | * called via the TEST_ASSUME macro. |
| 109 | * |
| 110 | * \param test Description of the assumption that caused the test case to |
| 111 | * be skipped. This MUST be a string literal. |
| 112 | * \param line_no Line number where the test case was skipped. |
| 113 | * \param filename Filename where the test case was skipped. |
Chris Jones | 567e0ad | 2021-02-03 12:07:01 +0000 | [diff] [blame] | 114 | */ |
Chris Jones | a5ab765 | 2021-02-02 16:20:45 +0000 | [diff] [blame] | 115 | void mbedtls_test_skip( const char *test, int line_no, const char* filename ); |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 116 | |
Chris Jones | 567e0ad | 2021-02-03 12:07:01 +0000 | [diff] [blame] | 117 | /** |
| 118 | * \brief Set the test step number for failure reports. |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 119 | * |
Chris Jones | 39ddb0a | 2021-02-03 16:15:00 +0000 | [diff] [blame] | 120 | * Call this function to display "step NNN" in addition to the |
Chris Jones | 567e0ad | 2021-02-03 12:07:01 +0000 | [diff] [blame] | 121 | * line number and file name if a test fails. Typically the "step |
| 122 | * number" is the index of a for loop but it can be whatever you |
| 123 | * want. |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 124 | * |
| 125 | * \param step The step number to report. |
| 126 | */ |
| 127 | void mbedtls_test_set_step( unsigned long step ); |
| 128 | |
Chris Jones | 567e0ad | 2021-02-03 12:07:01 +0000 | [diff] [blame] | 129 | /** |
| 130 | * \brief Reset mbedtls_test_info to a ready/starting state. |
Chris Jones | 567e0ad | 2021-02-03 12:07:01 +0000 | [diff] [blame] | 131 | */ |
Chris Jones | a5ab765 | 2021-02-02 16:20:45 +0000 | [diff] [blame] | 132 | void mbedtls_test_info_reset( void ); |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 133 | |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 134 | /** |
Ronald Cron | ab500cb | 2020-07-01 17:09:10 +0200 | [diff] [blame] | 135 | * \brief This function decodes the hexadecimal representation of |
| 136 | * data. |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 137 | * |
| 138 | * \note The output buffer can be the same as the input buffer. For |
| 139 | * any other overlapping of the input and output buffers, the |
| 140 | * behavior is undefined. |
| 141 | * |
| 142 | * \param obuf Output buffer. |
| 143 | * \param obufmax Size in number of bytes of \p obuf. |
| 144 | * \param ibuf Input buffer. |
| 145 | * \param len The number of unsigned char written in \p obuf. This must |
| 146 | * not be \c NULL. |
| 147 | * |
| 148 | * \return \c 0 on success. |
| 149 | * \return \c -1 if the output buffer is too small or the input string |
Ronald Cron | ab500cb | 2020-07-01 17:09:10 +0200 | [diff] [blame] | 150 | * is not a valid hexadecimal representation. |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 151 | */ |
| 152 | int mbedtls_test_unhexify( unsigned char *obuf, size_t obufmax, |
| 153 | const char *ibuf, size_t *len ); |
| 154 | |
Ronald Cron | 72d628f | 2020-06-08 17:05:57 +0200 | [diff] [blame] | 155 | void mbedtls_test_hexify( unsigned char *obuf, |
| 156 | const unsigned char *ibuf, |
| 157 | int len ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 158 | |
| 159 | /** |
| 160 | * Allocate and zeroize a buffer. |
| 161 | * |
| 162 | * If the size if zero, a pointer to a zeroized 1-byte buffer is returned. |
| 163 | * |
| 164 | * For convenience, dies if allocation fails. |
| 165 | */ |
Ronald Cron | 690f3eb | 2020-06-10 10:42:18 +0200 | [diff] [blame] | 166 | unsigned char *mbedtls_test_zero_alloc( size_t len ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 167 | |
| 168 | /** |
| 169 | * Allocate and fill a buffer from hex data. |
| 170 | * |
| 171 | * The buffer is sized exactly as needed. This allows to detect buffer |
| 172 | * overruns (including overreads) when running the test suite under valgrind. |
| 173 | * |
| 174 | * If the size if zero, a pointer to a zeroized 1-byte buffer is returned. |
| 175 | * |
| 176 | * For convenience, dies if allocation fails. |
| 177 | */ |
Ronald Cron | a256c70 | 2020-06-10 10:53:11 +0200 | [diff] [blame] | 178 | unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 179 | |
Ronald Cron | de70b16 | 2020-06-10 11:03:08 +0200 | [diff] [blame] | 180 | int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b, |
| 181 | uint32_t a_len, uint32_t b_len ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 182 | |
Gilles Peskine | 1dc19ff | 2021-02-08 20:59:39 +0100 | [diff] [blame] | 183 | #if defined(MBEDTLS_PSA_CRYPTO_C) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
Gilles Peskine | 1af872d | 2021-01-20 20:02:01 +0100 | [diff] [blame] | 184 | #include "test/fake_external_rng_for_test.h" |
| 185 | #endif |
| 186 | |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 187 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 188 | /** Permanently activate the mutex usage verification framework. See |
| 189 | * threading_helpers.c for information. */ |
| 190 | void mbedtls_test_mutex_usage_init( void ); |
Gilles Peskine | 2a4c598 | 2021-01-29 21:18:09 +0100 | [diff] [blame] | 191 | |
| 192 | /** Call this function after executing a test case to check for mutex usage |
| 193 | * errors. */ |
| 194 | void mbedtls_test_mutex_usage_check( void ); |
Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame] | 195 | #endif /* MBEDTLS_TEST_MUTEX_USAGE */ |
| 196 | |
Chris Jones | 96ae73b | 2021-01-08 17:04:59 +0000 | [diff] [blame] | 197 | #if defined(MBEDTLS_TEST_HOOKS) |
| 198 | /** |
Chris Jones | 3f613c1 | 2021-03-31 09:34:22 +0100 | [diff] [blame] | 199 | * \brief Check that only a pure high-level error code is being combined with |
| 200 | * a pure low-level error code as otherwise the resultant error code |
Chris Jones | 5e8805a | 2021-01-12 15:21:57 +0000 | [diff] [blame] | 201 | * would be corrupted. |
Chris Jones | 3f613c1 | 2021-03-31 09:34:22 +0100 | [diff] [blame] | 202 | * |
| 203 | * \note Both high-level and low-level error codes cannot be greater than |
| 204 | * zero however can be zero. If one error code is zero then the |
| 205 | * other error code is returned even if both codes are zero. |
| 206 | * |
| 207 | * \note If the check fails, fail the test currently being run. |
Chris Jones | 96ae73b | 2021-01-08 17:04:59 +0000 | [diff] [blame] | 208 | */ |
| 209 | void mbedtls_test_err_add_check( int high, int low, |
| 210 | const char *file, int line); |
| 211 | #endif |
| 212 | |
Ronald Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 213 | #endif /* TEST_HELPERS_H */ |