blob: a73e06fca8cd36a1c02f7422130b0ada09018747 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00009 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Ronald Cron4b8b1992020-06-09 13:52:23 +020010 */
11
12#ifndef TEST_MACROS_H
13#define TEST_MACROS_H
14
Bence Szépkútic662b362021-05-27 11:25:03 +020015#include "mbedtls/build_info.h"
Ronald Cron4b8b1992020-06-09 13:52:23 +020016
Ronald Cron849930a2020-06-03 08:06:47 +020017#include <stdlib.h>
18
Ronald Cron849930a2020-06-03 08:06:47 +020019#include "mbedtls/platform.h"
Ronald Cron849930a2020-06-03 08:06:47 +020020
21#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
22#include "mbedtls/memory_buffer_alloc.h"
23#endif
Andrzej Kurekf1b659e2023-05-30 09:45:17 -040024#include "common.h"
Ronald Cron849930a2020-06-03 08:06:47 +020025
Chris Jonesa6d155f2021-02-09 12:09:33 +000026/**
27 * \brief This macro tests the expression passed to it as a test step or
28 * individual test in a test case.
29 *
30 * It allows a library function to return a value and return an error
31 * code that can be tested.
32 *
Chris Jonesa6d155f2021-02-09 12:09:33 +000033 * Failing the test means:
34 * - Mark this test case as failed.
35 * - Print a message identifying the failure.
36 * - Jump to the \c exit label.
37 *
38 * This macro expands to an instruction, not an expression.
39 * It may jump to the \c exit label.
40 *
41 * \param TEST The test expression to be tested.
42 */
Gilles Peskine449bd832023-01-11 14:50:10 +010043#define TEST_ASSERT(TEST) \
Chris Jonesa6d155f2021-02-09 12:09:33 +000044 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +010045 if (!(TEST)) \
46 { \
47 mbedtls_test_fail( #TEST, __LINE__, __FILE__); \
48 goto exit; \
49 } \
50 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +000051
Agathiyan Bragadeeshebb40bc2023-07-14 17:28:27 +010052/** This macro asserts fails the test with given output message.
53 *
54 * \param MESSAGE The message to be outputed on assertion
55 */
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +010056#define TEST_FAIL(MESSAGE) \
Agathiyan Bragadeeshebb40bc2023-07-14 17:28:27 +010057 do { \
58 mbedtls_test_fail(MESSAGE, __LINE__, __FILE__); \
Agathiyan Bragadeesh3dd3ae22023-07-21 17:07:00 +010059 goto exit; \
60 } while (0)
Agathiyan Bragadeeshebb40bc2023-07-14 17:28:27 +010061
Gilles Peskine89615ee2021-04-29 20:28:54 +020062/** Evaluate two integer expressions and fail the test case if they have
63 * different values.
Chris Jonesa6d155f2021-02-09 12:09:33 +000064 *
Gilles Peskine89615ee2021-04-29 20:28:54 +020065 * The two expressions should have the same signedness, otherwise the
66 * comparison is not meaningful if the signed value is negative.
67 *
68 * \param expr1 An integral-typed expression to evaluate.
69 * \param expr2 Another integral-typed expression to evaluate.
Chris Jonesa6d155f2021-02-09 12:09:33 +000070 */
Gilles Peskine449bd832023-01-11 14:50:10 +010071#define TEST_EQUAL(expr1, expr2) \
Gilles Peskine89615ee2021-04-29 20:28:54 +020072 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +010073 if (!mbedtls_test_equal( #expr1 " == " #expr2, __LINE__, __FILE__, \
Agathiyan Bragadeesh2d310de2023-07-17 18:27:03 +010074 (unsigned long long) (expr1), (unsigned long long) (expr2))) \
Gilles Peskine449bd832023-01-11 14:50:10 +010075 goto exit; \
76 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +000077
Gilles Peskined1465422022-04-13 23:59:52 +020078/** Evaluate two unsigned integer expressions and fail the test case
79 * if they are not in increasing order (left <= right).
80 *
81 * \param expr1 An integral-typed expression to evaluate.
82 * \param expr2 Another integral-typed expression to evaluate.
83 */
Gilles Peskine449bd832023-01-11 14:50:10 +010084#define TEST_LE_U(expr1, expr2) \
Gilles Peskined1465422022-04-13 23:59:52 +020085 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +010086 if (!mbedtls_test_le_u( #expr1 " <= " #expr2, __LINE__, __FILE__, \
87 expr1, expr2)) \
88 goto exit; \
89 } while (0)
Gilles Peskined1465422022-04-13 23:59:52 +020090
91/** Evaluate two signed integer expressions and fail the test case
92 * if they are not in increasing order (left <= right).
93 *
94 * \param expr1 An integral-typed expression to evaluate.
95 * \param expr2 Another integral-typed expression to evaluate.
96 */
Gilles Peskine449bd832023-01-11 14:50:10 +010097#define TEST_LE_S(expr1, expr2) \
Gilles Peskined1465422022-04-13 23:59:52 +020098 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +010099 if (!mbedtls_test_le_s( #expr1 " <= " #expr2, __LINE__, __FILE__, \
100 expr1, expr2)) \
101 goto exit; \
102 } while (0)
Gilles Peskined1465422022-04-13 23:59:52 +0200103
Chris Jonesa6d155f2021-02-09 12:09:33 +0000104/** Allocate memory dynamically and fail the test case if this fails.
105 * The allocated memory will be filled with zeros.
106 *
107 * You must set \p pointer to \c NULL before calling this macro and
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100108 * put `mbedtls_free(pointer)` in the test's cleanup code.
Chris Jonesa6d155f2021-02-09 12:09:33 +0000109 *
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100110 * If \p item_count is zero, the resulting \p pointer will be \c NULL.
Chris Jonesa6d155f2021-02-09 12:09:33 +0000111 * This is usually what we want in tests since API functions are
112 * supposed to accept null pointers when a buffer size is zero.
113 *
114 * This macro expands to an instruction, not an expression.
115 * It may jump to the \c exit label.
116 *
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100117 * \param pointer An lvalue where the address of the allocated buffer
118 * will be stored.
119 * This expression may be evaluated multiple times.
120 * \param item_count Number of elements to allocate.
121 * This expression may be evaluated multiple times.
Chris Jonesa6d155f2021-02-09 12:09:33 +0000122 *
123 */
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100124#define TEST_CALLOC(pointer, item_count) \
Tom Cosgrovef9ffd112023-07-20 16:48:18 +0100125 do { \
126 TEST_ASSERT((pointer) == NULL); \
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100127 if ((item_count) != 0) { \
Sergei Trofimovichda2a33d2024-01-25 20:48:56 +0000128 (pointer) = mbedtls_calloc((item_count), \
129 sizeof(*(pointer))); \
Tom Cosgrovef9ffd112023-07-20 16:48:18 +0100130 TEST_ASSERT((pointer) != NULL); \
131 } \
132 } while (0)
133
Dave Rodgmana3286352023-09-19 17:34:39 +0100134/** Allocate memory dynamically and fail the test case if this fails.
135 * The allocated memory will be filled with zeros.
136 *
137 * You must set \p pointer to \c NULL before calling this macro and
138 * put `mbedtls_free(pointer)` in the test's cleanup code.
139 *
140 * If \p item_count is zero, the resulting \p pointer will not be \c NULL.
141 *
142 * This macro expands to an instruction, not an expression.
143 * It may jump to the \c exit label.
144 *
145 * \param pointer An lvalue where the address of the allocated buffer
146 * will be stored.
147 * This expression may be evaluated multiple times.
148 * \param item_count Number of elements to allocate.
149 * This expression may be evaluated multiple times.
150 *
Dave Rodgman986006e2023-09-19 18:30:25 +0100151 * Note: if passing size 0, mbedtls_calloc may return NULL. In this case,
152 * we reattempt to allocate with the smallest possible buffer to assure a
153 * non-NULL pointer.
Dave Rodgmana3286352023-09-19 17:34:39 +0100154 */
155#define TEST_CALLOC_NONNULL(pointer, item_count) \
156 do { \
157 TEST_ASSERT((pointer) == NULL); \
Sergei Trofimovichda2a33d2024-01-25 20:48:56 +0000158 (pointer) = mbedtls_calloc((item_count), \
159 sizeof(*(pointer))); \
Dave Rodgman986006e2023-09-19 18:30:25 +0100160 if (((pointer) == NULL) && ((item_count) == 0)) { \
161 (pointer) = mbedtls_calloc(1, 1); \
162 } \
Dave Rodgmana3286352023-09-19 17:34:39 +0100163 TEST_ASSERT((pointer) != NULL); \
164 } while (0)
165
Tom Cosgrovef9ffd112023-07-20 16:48:18 +0100166/* For backwards compatibility */
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100167#define ASSERT_ALLOC(pointer, item_count) TEST_CALLOC(pointer, item_count)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000168
169/** Allocate memory dynamically. If the allocation fails, skip the test case.
170 *
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100171 * This macro behaves like #TEST_CALLOC, except that if the allocation
Chris Jonesa6d155f2021-02-09 12:09:33 +0000172 * fails, it marks the test as skipped rather than failed.
173 */
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100174#define TEST_CALLOC_OR_SKIP(pointer, item_count) \
Tom Cosgrove412a8132023-07-20 16:55:14 +0100175 do { \
176 TEST_ASSERT((pointer) == NULL); \
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100177 if ((item_count) != 0) { \
Sergei Trofimovichda2a33d2024-01-25 20:48:56 +0000178 (pointer) = mbedtls_calloc((item_count), \
179 sizeof(*(pointer))); \
Tom Cosgrove412a8132023-07-20 16:55:14 +0100180 TEST_ASSUME((pointer) != NULL); \
181 } \
182 } while (0)
183
184/* For backwards compatibility */
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100185#define ASSERT_ALLOC_WEAK(pointer, item_count) TEST_CALLOC_OR_SKIP(pointer, item_count)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000186
187/** Compare two buffers and fail the test case if they differ.
188 *
189 * This macro expands to an instruction, not an expression.
190 * It may jump to the \c exit label.
191 *
192 * \param p1 Pointer to the start of the first buffer.
193 * \param size1 Size of the first buffer in bytes.
194 * This expression may be evaluated multiple times.
195 * \param p2 Pointer to the start of the second buffer.
196 * \param size2 Size of the second buffer in bytes.
197 * This expression may be evaluated multiple times.
198 */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100199#define TEST_MEMORY_COMPARE(p1, size1, p2, size2) \
Tom Cosgrove65cd8512023-07-20 16:46:01 +0100200 do { \
Manuel Pégourié-Gonnarda9a1b212023-02-09 09:15:04 +0100201 TEST_EQUAL((size1), (size2)); \
Tom Cosgrove65cd8512023-07-20 16:46:01 +0100202 if ((size1) != 0) { \
203 TEST_ASSERT(memcmp((p1), (p2), (size1)) == 0); \
204 } \
205 } while (0)
206
207/* For backwards compatibility */
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100208#define ASSERT_COMPARE(p1, size1, p2, size2) TEST_MEMORY_COMPARE(p1, size1, p2, size2)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000209
210/**
211 * \brief This macro tests the expression passed to it and skips the
212 * running test if it doesn't evaluate to 'true'.
213 *
214 * \param TEST The test expression to be tested.
215 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100216#define TEST_ASSUME(TEST) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000217 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 if (!(TEST)) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000219 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 mbedtls_test_skip( #TEST, __LINE__, __FILE__); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000221 goto exit; \
222 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225#define TEST_HELPER_ASSERT(a) if (!(a)) \
226 { \
227 mbedtls_fprintf(stderr, "Assertion Failed at %s:%d - %s\n", \
228 __FILE__, __LINE__, #a); \
229 mbedtls_exit(1); \
230 }
Ronald Cron849930a2020-06-03 08:06:47 +0200231
Ronald Cron849930a2020-06-03 08:06:47 +0200232/** Return the smaller of two values.
233 *
234 * \param x An integer-valued expression without side effects.
235 * \param y An integer-valued expression without side effects.
236 *
237 * \return The smaller of \p x and \p y.
238 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100239#define MIN(x, y) ((x) < (y) ? (x) : (y))
Ronald Cron849930a2020-06-03 08:06:47 +0200240
241/** Return the larger of two values.
242 *
243 * \param x An integer-valued expression without side effects.
244 * \param y An integer-valued expression without side effects.
245 *
246 * \return The larger of \p x and \p y.
247 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100248#define MAX(x, y) ((x) > (y) ? (x) : (y))
Ronald Cron849930a2020-06-03 08:06:47 +0200249
Ronald Cron4b8b1992020-06-09 13:52:23 +0200250#endif /* TEST_MACROS_H */