blob: ae84ec2363bcf488f9e66de6e6b32ddbc7e48552 [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
110 * put `mbedtls_free( pointer )` in the test's cleanup code.
111 *
112 * If \p length is zero, the resulting \p pointer will be \c NULL.
113 * 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 *
119 * \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 length Number of elements to allocate.
123 * This expression may be evaluated multiple times.
124 *
125 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100126#define ASSERT_ALLOC(pointer, length) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000127 do \
128 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 TEST_ASSERT((pointer) == NULL); \
130 if ((length) != 0) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000131 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 (pointer) = mbedtls_calloc(sizeof(*(pointer)), \
133 (length)); \
134 TEST_ASSERT((pointer) != NULL); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000135 } \
136 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000138
139/** Allocate memory dynamically. If the allocation fails, skip the test case.
140 *
141 * This macro behaves like #ASSERT_ALLOC, except that if the allocation
142 * fails, it marks the test as skipped rather than failed.
143 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100144#define ASSERT_ALLOC_WEAK(pointer, length) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000145 do \
146 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 TEST_ASSERT((pointer) == NULL); \
148 if ((length) != 0) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000149 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 (pointer) = mbedtls_calloc(sizeof(*(pointer)), \
151 (length)); \
152 TEST_ASSUME((pointer) != NULL); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000153 } \
154 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 while (0)
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 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100169#define ASSERT_COMPARE(p1, size1, p2, size2) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000170 do \
171 { \
Manuel Pégourié-Gonnarda9a1b212023-02-09 09:15:04 +0100172 TEST_EQUAL((size1), (size2)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 if ((size1) != 0) \
174 TEST_ASSERT(memcmp((p1), (p2), (size1)) == 0); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000175 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000177
178/**
179 * \brief This macro tests the expression passed to it and skips the
180 * running test if it doesn't evaluate to 'true'.
181 *
182 * \param TEST The test expression to be tested.
183 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100184#define TEST_ASSUME(TEST) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000185 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 if (!(TEST)) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000187 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 mbedtls_test_skip( #TEST, __LINE__, __FILE__); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000189 goto exit; \
190 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193#define TEST_HELPER_ASSERT(a) if (!(a)) \
194 { \
195 mbedtls_fprintf(stderr, "Assertion Failed at %s:%d - %s\n", \
196 __FILE__, __LINE__, #a); \
197 mbedtls_exit(1); \
198 }
Ronald Cron849930a2020-06-03 08:06:47 +0200199
Ronald Cron849930a2020-06-03 08:06:47 +0200200/** Return the smaller of two values.
201 *
202 * \param x An integer-valued expression without side effects.
203 * \param y An integer-valued expression without side effects.
204 *
205 * \return The smaller of \p x and \p y.
206 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100207#define MIN(x, y) ((x) < (y) ? (x) : (y))
Ronald Cron849930a2020-06-03 08:06:47 +0200208
209/** Return the larger of two values.
210 *
211 * \param x An integer-valued expression without side effects.
212 * \param y An integer-valued expression without side effects.
213 *
214 * \return The larger of \p x and \p y.
215 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100216#define MAX(x, y) ((x) > (y) ? (x) : (y))
Ronald Cron849930a2020-06-03 08:06:47 +0200217
Ronald Cron4b8b1992020-06-09 13:52:23 +0200218#endif /* TEST_MACROS_H */