blob: ecf6a1753642b50504fbcccbc7702408c66ed5d5 [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
Gilles Peskine89615ee2021-04-29 20:28:54 +020064/** Evaluate two integer expressions and fail the test case if they have
65 * different values.
Chris Jonesa6d155f2021-02-09 12:09:33 +000066 *
Gilles Peskine89615ee2021-04-29 20:28:54 +020067 * The two expressions should have the same signedness, otherwise the
68 * comparison is not meaningful if the signed value is negative.
69 *
70 * \param expr1 An integral-typed expression to evaluate.
71 * \param expr2 Another integral-typed expression to evaluate.
Chris Jonesa6d155f2021-02-09 12:09:33 +000072 */
Gilles Peskine449bd832023-01-11 14:50:10 +010073#define TEST_EQUAL(expr1, expr2) \
Gilles Peskine89615ee2021-04-29 20:28:54 +020074 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +010075 if (!mbedtls_test_equal( #expr1 " == " #expr2, __LINE__, __FILE__, \
76 expr1, expr2)) \
77 goto exit; \
78 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +000079
Gilles Peskined1465422022-04-13 23:59:52 +020080/** Evaluate two unsigned integer expressions and fail the test case
81 * if they are not in increasing order (left <= right).
82 *
83 * \param expr1 An integral-typed expression to evaluate.
84 * \param expr2 Another integral-typed expression to evaluate.
85 */
Gilles Peskine449bd832023-01-11 14:50:10 +010086#define TEST_LE_U(expr1, expr2) \
Gilles Peskined1465422022-04-13 23:59:52 +020087 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if (!mbedtls_test_le_u( #expr1 " <= " #expr2, __LINE__, __FILE__, \
89 expr1, expr2)) \
90 goto exit; \
91 } while (0)
Gilles Peskined1465422022-04-13 23:59:52 +020092
93/** Evaluate two signed integer expressions and fail the test case
94 * if they are not in increasing order (left <= right).
95 *
96 * \param expr1 An integral-typed expression to evaluate.
97 * \param expr2 Another integral-typed expression to evaluate.
98 */
Gilles Peskine449bd832023-01-11 14:50:10 +010099#define TEST_LE_S(expr1, expr2) \
Gilles Peskined1465422022-04-13 23:59:52 +0200100 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if (!mbedtls_test_le_s( #expr1 " <= " #expr2, __LINE__, __FILE__, \
102 expr1, expr2)) \
103 goto exit; \
104 } while (0)
Gilles Peskined1465422022-04-13 23:59:52 +0200105
Chris Jonesa6d155f2021-02-09 12:09:33 +0000106/** Allocate memory dynamically and fail the test case if this fails.
107 * The allocated memory will be filled with zeros.
108 *
109 * You must set \p pointer to \c NULL before calling this macro and
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100110 * put `mbedtls_free(pointer)` in the test's cleanup code.
Chris Jonesa6d155f2021-02-09 12:09:33 +0000111 *
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100112 * If \p item_count is zero, the resulting \p pointer will be \c NULL.
Chris Jonesa6d155f2021-02-09 12:09:33 +0000113 * This is usually what we want in tests since API functions are
114 * supposed to accept null pointers when a buffer size is zero.
115 *
116 * This macro expands to an instruction, not an expression.
117 * It may jump to the \c exit label.
118 *
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100119 * \param pointer An lvalue where the address of the allocated buffer
120 * will be stored.
121 * This expression may be evaluated multiple times.
122 * \param item_count Number of elements to allocate.
123 * This expression may be evaluated multiple times.
Chris Jonesa6d155f2021-02-09 12:09:33 +0000124 *
125 */
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100126#define TEST_CALLOC(pointer, item_count) \
Tom Cosgrovef9ffd112023-07-20 16:48:18 +0100127 do { \
128 TEST_ASSERT((pointer) == NULL); \
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100129 if ((item_count) != 0) { \
Tom Cosgrovef9ffd112023-07-20 16:48:18 +0100130 (pointer) = mbedtls_calloc(sizeof(*(pointer)), \
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100131 (item_count)); \
Tom Cosgrovef9ffd112023-07-20 16:48:18 +0100132 TEST_ASSERT((pointer) != NULL); \
133 } \
134 } while (0)
135
136/* For backwards compatibility */
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100137#define ASSERT_ALLOC(pointer, item_count) TEST_CALLOC(pointer, item_count)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000138
139/** Allocate memory dynamically. If the allocation fails, skip the test case.
140 *
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100141 * This macro behaves like #TEST_CALLOC, except that if the allocation
Chris Jonesa6d155f2021-02-09 12:09:33 +0000142 * fails, it marks the test as skipped rather than failed.
143 */
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100144#define TEST_CALLOC_OR_SKIP(pointer, item_count) \
Tom Cosgrove412a8132023-07-20 16:55:14 +0100145 do { \
146 TEST_ASSERT((pointer) == NULL); \
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100147 if ((item_count) != 0) { \
Tom Cosgrove412a8132023-07-20 16:55:14 +0100148 (pointer) = mbedtls_calloc(sizeof(*(pointer)), \
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100149 (item_count)); \
Tom Cosgrove412a8132023-07-20 16:55:14 +0100150 TEST_ASSUME((pointer) != NULL); \
151 } \
152 } while (0)
153
154/* For backwards compatibility */
Tom Cosgrovea45d9022023-07-21 11:34:44 +0100155#define ASSERT_ALLOC_WEAK(pointer, item_count) TEST_CALLOC_OR_SKIP(pointer, item_count)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000156
157/** Compare two buffers and fail the test case if they differ.
158 *
159 * This macro expands to an instruction, not an expression.
160 * It may jump to the \c exit label.
161 *
162 * \param p1 Pointer to the start of the first buffer.
163 * \param size1 Size of the first buffer in bytes.
164 * This expression may be evaluated multiple times.
165 * \param p2 Pointer to the start of the second buffer.
166 * \param size2 Size of the second buffer in bytes.
167 * This expression may be evaluated multiple times.
168 */
Tom Cosgrove65cd8512023-07-20 16:46:01 +0100169#define TEST_BUFFERS_EQUAL(p1, size1, p2, size2) \
170 do { \
Manuel Pégourié-Gonnarda9a1b212023-02-09 09:15:04 +0100171 TEST_EQUAL((size1), (size2)); \
Tom Cosgrove65cd8512023-07-20 16:46:01 +0100172 if ((size1) != 0) { \
173 TEST_ASSERT(memcmp((p1), (p2), (size1)) == 0); \
174 } \
175 } while (0)
176
177/* For backwards compatibility */
178#define ASSERT_COMPARE(p1, size1, p2, size2) TEST_BUFFERS_EQUAL(p1, size1, p2, size2)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000179
180/**
181 * \brief This macro tests the expression passed to it and skips the
182 * running test if it doesn't evaluate to 'true'.
183 *
184 * \param TEST The test expression to be tested.
185 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186#define TEST_ASSUME(TEST) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000187 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 if (!(TEST)) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000189 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 mbedtls_test_skip( #TEST, __LINE__, __FILE__); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000191 goto exit; \
192 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195#define TEST_HELPER_ASSERT(a) if (!(a)) \
196 { \
197 mbedtls_fprintf(stderr, "Assertion Failed at %s:%d - %s\n", \
198 __FILE__, __LINE__, #a); \
199 mbedtls_exit(1); \
200 }
Ronald Cron849930a2020-06-03 08:06:47 +0200201
Ronald Cron849930a2020-06-03 08:06:47 +0200202/** Return the smaller of two values.
203 *
204 * \param x An integer-valued expression without side effects.
205 * \param y An integer-valued expression without side effects.
206 *
207 * \return The smaller of \p x and \p y.
208 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100209#define MIN(x, y) ((x) < (y) ? (x) : (y))
Ronald Cron849930a2020-06-03 08:06:47 +0200210
211/** Return the larger of two values.
212 *
213 * \param x An integer-valued expression without side effects.
214 * \param y An integer-valued expression without side effects.
215 *
216 * \return The larger of \p x and \p y.
217 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100218#define MAX(x, y) ((x) > (y) ? (x) : (y))
Ronald Cron849930a2020-06-03 08:06:47 +0200219
Ronald Cron4b8b1992020-06-09 13:52:23 +0200220#endif /* TEST_MACROS_H */