blob: 7edc991addcef4d695eb3330ee55b7228ad8085d [file] [log] [blame]
Ronald Cron4b8b1992020-06-09 13:52:23 +02001/**
2 * \file macros.h
3 *
4 * \brief This file contains generic macros for the purpose of testing.
5 */
6
Bence Szépkúti86974652020-06-15 11:59:37 +02007/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Ronald Cron4b8b1992020-06-09 13:52:23 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Ronald Cron4b8b1992020-06-09 13:52:23 +020022 */
23
24#ifndef TEST_MACROS_H
25#define TEST_MACROS_H
26
Bence Szépkútic662b362021-05-27 11:25:03 +020027#include "mbedtls/build_info.h"
Ronald Cron4b8b1992020-06-09 13:52:23 +020028
Ronald Cron849930a2020-06-03 08:06:47 +020029#include <stdlib.h>
30
Ronald Cron849930a2020-06-03 08:06:47 +020031#include "mbedtls/platform.h"
Ronald Cron849930a2020-06-03 08:06:47 +020032
33#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
34#include "mbedtls/memory_buffer_alloc.h"
35#endif
Andrzej Kurekf1b659e2023-05-30 09:45:17 -040036#include "common.h"
Ronald Cron849930a2020-06-03 08:06:47 +020037
Chris Jonesa6d155f2021-02-09 12:09:33 +000038/**
39 * \brief This macro tests the expression passed to it as a test step or
40 * individual test in a test case.
41 *
42 * It allows a library function to return a value and return an error
43 * code that can be tested.
44 *
Chris Jonesa6d155f2021-02-09 12:09:33 +000045 * Failing the test means:
46 * - Mark this test case as failed.
47 * - Print a message identifying the failure.
48 * - Jump to the \c exit label.
49 *
50 * This macro expands to an instruction, not an expression.
51 * It may jump to the \c exit label.
52 *
53 * \param TEST The test expression to be tested.
54 */
Gilles Peskine449bd832023-01-11 14:50:10 +010055#define TEST_ASSERT(TEST) \
Chris Jonesa6d155f2021-02-09 12:09:33 +000056 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +010057 if (!(TEST)) \
58 { \
59 mbedtls_test_fail( #TEST, __LINE__, __FILE__); \
60 goto exit; \
61 } \
62 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +000063
Agathiyan Bragadeeshebb40bc2023-07-14 17:28:27 +010064/** This macro asserts fails the test with given output message.
65 *
66 * \param MESSAGE The message to be outputed on assertion
67 */
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +010068#define TEST_FAIL(MESSAGE) \
Agathiyan Bragadeeshebb40bc2023-07-14 17:28:27 +010069 do { \
70 mbedtls_test_fail(MESSAGE, __LINE__, __FILE__); \
Agathiyan Bragadeesh3dd3ae22023-07-21 17:07:00 +010071 goto exit; \
72 } while (0)
Agathiyan Bragadeeshebb40bc2023-07-14 17:28:27 +010073
Gilles Peskine89615ee2021-04-29 20:28:54 +020074/** Evaluate two integer expressions and fail the test case if they have
75 * different values.
Chris Jonesa6d155f2021-02-09 12:09:33 +000076 *
Gilles Peskine89615ee2021-04-29 20:28:54 +020077 * The two expressions should have the same signedness, otherwise the
78 * comparison is not meaningful if the signed value is negative.
79 *
80 * \param expr1 An integral-typed expression to evaluate.
81 * \param expr2 Another integral-typed expression to evaluate.
Chris Jonesa6d155f2021-02-09 12:09:33 +000082 */
Gilles Peskine449bd832023-01-11 14:50:10 +010083#define TEST_EQUAL(expr1, expr2) \
Gilles Peskine89615ee2021-04-29 20:28:54 +020084 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +010085 if (!mbedtls_test_equal( #expr1 " == " #expr2, __LINE__, __FILE__, \
Agathiyan Bragadeesh2d310de2023-07-17 18:27:03 +010086 (unsigned long long) (expr1), (unsigned long long) (expr2))) \
Gilles Peskine449bd832023-01-11 14:50:10 +010087 goto exit; \
88 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +000089
Gilles Peskined1465422022-04-13 23:59:52 +020090/** Evaluate two unsigned integer expressions and fail the test case
91 * if they are not in increasing order (left <= right).
92 *
93 * \param expr1 An integral-typed expression to evaluate.
94 * \param expr2 Another integral-typed expression to evaluate.
95 */
Gilles Peskine449bd832023-01-11 14:50:10 +010096#define TEST_LE_U(expr1, expr2) \
Gilles Peskined1465422022-04-13 23:59:52 +020097 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +010098 if (!mbedtls_test_le_u( #expr1 " <= " #expr2, __LINE__, __FILE__, \
99 expr1, expr2)) \
100 goto exit; \
101 } while (0)
Gilles Peskined1465422022-04-13 23:59:52 +0200102
103/** Evaluate two signed integer expressions and fail the test case
104 * if they are not in increasing order (left <= right).
105 *
106 * \param expr1 An integral-typed expression to evaluate.
107 * \param expr2 Another integral-typed expression to evaluate.
108 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100109#define TEST_LE_S(expr1, expr2) \
Gilles Peskined1465422022-04-13 23:59:52 +0200110 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 if (!mbedtls_test_le_s( #expr1 " <= " #expr2, __LINE__, __FILE__, \
112 expr1, expr2)) \
113 goto exit; \
114 } while (0)
Gilles Peskined1465422022-04-13 23:59:52 +0200115
Chris Jonesa6d155f2021-02-09 12:09:33 +0000116/** Allocate memory dynamically and fail the test case if this fails.
117 * The allocated memory will be filled with zeros.
118 *
119 * You must set \p pointer to \c NULL before calling this macro and
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100120 * put `mbedtls_free(pointer)` in the test's cleanup code.
Chris Jonesa6d155f2021-02-09 12:09:33 +0000121 *
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100122 * If \p item_count is zero, the resulting \p pointer will be \c NULL.
Chris Jonesa6d155f2021-02-09 12:09:33 +0000123 * This is usually what we want in tests since API functions are
124 * supposed to accept null pointers when a buffer size is zero.
125 *
126 * This macro expands to an instruction, not an expression.
127 * It may jump to the \c exit label.
128 *
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100129 * \param pointer An lvalue where the address of the allocated buffer
130 * will be stored.
131 * This expression may be evaluated multiple times.
132 * \param item_count Number of elements to allocate.
133 * This expression may be evaluated multiple times.
Chris Jonesa6d155f2021-02-09 12:09:33 +0000134 *
135 */
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100136#define TEST_CALLOC(pointer, item_count) \
Tom Cosgrovef9ffd112023-07-20 16:48:18 +0100137 do { \
138 TEST_ASSERT((pointer) == NULL); \
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100139 if ((item_count) != 0) { \
Tom Cosgrovef9ffd112023-07-20 16:48:18 +0100140 (pointer) = mbedtls_calloc(sizeof(*(pointer)), \
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100141 (item_count)); \
Tom Cosgrovef9ffd112023-07-20 16:48:18 +0100142 TEST_ASSERT((pointer) != NULL); \
143 } \
144 } while (0)
145
146/* For backwards compatibility */
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100147#define ASSERT_ALLOC(pointer, item_count) TEST_CALLOC(pointer, item_count)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000148
149/** Allocate memory dynamically. If the allocation fails, skip the test case.
150 *
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100151 * This macro behaves like #TEST_CALLOC, except that if the allocation
Chris Jonesa6d155f2021-02-09 12:09:33 +0000152 * fails, it marks the test as skipped rather than failed.
153 */
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100154#define TEST_CALLOC_OR_SKIP(pointer, item_count) \
Tom Cosgrove412a8132023-07-20 16:55:14 +0100155 do { \
156 TEST_ASSERT((pointer) == NULL); \
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100157 if ((item_count) != 0) { \
Tom Cosgrove412a8132023-07-20 16:55:14 +0100158 (pointer) = mbedtls_calloc(sizeof(*(pointer)), \
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100159 (item_count)); \
Tom Cosgrove412a8132023-07-20 16:55:14 +0100160 TEST_ASSUME((pointer) != NULL); \
161 } \
162 } while (0)
163
164/* For backwards compatibility */
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100165#define ASSERT_ALLOC_WEAK(pointer, item_count) TEST_CALLOC_OR_SKIP(pointer, item_count)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000166
167/** Compare two buffers and fail the test case if they differ.
168 *
169 * This macro expands to an instruction, not an expression.
170 * It may jump to the \c exit label.
171 *
172 * \param p1 Pointer to the start of the first buffer.
173 * \param size1 Size of the first buffer in bytes.
174 * This expression may be evaluated multiple times.
175 * \param p2 Pointer to the start of the second buffer.
176 * \param size2 Size of the second buffer in bytes.
177 * This expression may be evaluated multiple times.
178 */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100179#define TEST_MEMORY_COMPARE(p1, size1, p2, size2) \
Tom Cosgrove65cd8512023-07-20 16:46:01 +0100180 do { \
Manuel Pégourié-Gonnarda9a1b212023-02-09 09:15:04 +0100181 TEST_EQUAL((size1), (size2)); \
Tom Cosgrove65cd8512023-07-20 16:46:01 +0100182 if ((size1) != 0) { \
183 TEST_ASSERT(memcmp((p1), (p2), (size1)) == 0); \
184 } \
185 } while (0)
186
187/* For backwards compatibility */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100188#define ASSERT_COMPARE(p1, size1, p2, size2) TEST_MEMORY_COMPARE(p1, size1, p2, size2)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000189
190/**
191 * \brief This macro tests the expression passed to it and skips the
192 * running test if it doesn't evaluate to 'true'.
193 *
194 * \param TEST The test expression to be tested.
195 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196#define TEST_ASSUME(TEST) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000197 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (!(TEST)) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000199 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 mbedtls_test_skip( #TEST, __LINE__, __FILE__); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000201 goto exit; \
202 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205#define TEST_HELPER_ASSERT(a) if (!(a)) \
206 { \
207 mbedtls_fprintf(stderr, "Assertion Failed at %s:%d - %s\n", \
208 __FILE__, __LINE__, #a); \
209 mbedtls_exit(1); \
210 }
Ronald Cron849930a2020-06-03 08:06:47 +0200211
Ronald Cron849930a2020-06-03 08:06:47 +0200212/** Return the smaller of two values.
213 *
214 * \param x An integer-valued expression without side effects.
215 * \param y An integer-valued expression without side effects.
216 *
217 * \return The smaller of \p x and \p y.
218 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100219#define MIN(x, y) ((x) < (y) ? (x) : (y))
Ronald Cron849930a2020-06-03 08:06:47 +0200220
221/** Return the larger of two values.
222 *
223 * \param x An integer-valued expression without side effects.
224 * \param y An integer-valued expression without side effects.
225 *
226 * \return The larger of \p x and \p y.
227 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100228#define MAX(x, y) ((x) > (y) ? (x) : (y))
Ronald Cron849930a2020-06-03 08:06:47 +0200229
Ronald Cron4b8b1992020-06-09 13:52:23 +0200230#endif /* TEST_MACROS_H */