blob: 01eaff5c20267154431b4ade926e019e32fbf9ab [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
36
Chris Jonesa6d155f2021-02-09 12:09:33 +000037/**
38 * \brief This macro tests the expression passed to it as a test step or
39 * individual test in a test case.
40 *
41 * It allows a library function to return a value and return an error
42 * code that can be tested.
43 *
Chris Jonesa6d155f2021-02-09 12:09:33 +000044 * Failing the test means:
45 * - Mark this test case as failed.
46 * - Print a message identifying the failure.
47 * - Jump to the \c exit label.
48 *
49 * This macro expands to an instruction, not an expression.
50 * It may jump to the \c exit label.
51 *
52 * \param TEST The test expression to be tested.
53 */
Gilles Peskine449bd832023-01-11 14:50:10 +010054#define TEST_ASSERT(TEST) \
Chris Jonesa6d155f2021-02-09 12:09:33 +000055 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +010056 if (!(TEST)) \
57 { \
58 mbedtls_test_fail( #TEST, __LINE__, __FILE__); \
59 goto exit; \
60 } \
61 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +000062
Gilles Peskine89615ee2021-04-29 20:28:54 +020063/** Evaluate two integer expressions and fail the test case if they have
64 * different values.
Chris Jonesa6d155f2021-02-09 12:09:33 +000065 *
Gilles Peskine89615ee2021-04-29 20:28:54 +020066 * The two expressions should have the same signedness, otherwise the
67 * comparison is not meaningful if the signed value is negative.
68 *
69 * \param expr1 An integral-typed expression to evaluate.
70 * \param expr2 Another integral-typed expression to evaluate.
Chris Jonesa6d155f2021-02-09 12:09:33 +000071 */
Gilles Peskine449bd832023-01-11 14:50:10 +010072#define TEST_EQUAL(expr1, expr2) \
Gilles Peskine89615ee2021-04-29 20:28:54 +020073 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +010074 if (!mbedtls_test_equal( #expr1 " == " #expr2, __LINE__, __FILE__, \
75 expr1, expr2)) \
76 goto exit; \
77 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +000078
Gilles Peskined1465422022-04-13 23:59:52 +020079/** Evaluate two unsigned integer expressions and fail the test case
80 * if they are not in increasing order (left <= right).
81 *
82 * \param expr1 An integral-typed expression to evaluate.
83 * \param expr2 Another integral-typed expression to evaluate.
84 */
Gilles Peskine449bd832023-01-11 14:50:10 +010085#define TEST_LE_U(expr1, expr2) \
Gilles Peskined1465422022-04-13 23:59:52 +020086 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +010087 if (!mbedtls_test_le_u( #expr1 " <= " #expr2, __LINE__, __FILE__, \
88 expr1, expr2)) \
89 goto exit; \
90 } while (0)
Gilles Peskined1465422022-04-13 23:59:52 +020091
92/** Evaluate two signed integer expressions and fail the test case
93 * if they are not in increasing order (left <= right).
94 *
95 * \param expr1 An integral-typed expression to evaluate.
96 * \param expr2 Another integral-typed expression to evaluate.
97 */
Gilles Peskine449bd832023-01-11 14:50:10 +010098#define TEST_LE_S(expr1, expr2) \
Gilles Peskined1465422022-04-13 23:59:52 +020099 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 if (!mbedtls_test_le_s( #expr1 " <= " #expr2, __LINE__, __FILE__, \
101 expr1, expr2)) \
102 goto exit; \
103 } while (0)
Gilles Peskined1465422022-04-13 23:59:52 +0200104
Chris Jonesa6d155f2021-02-09 12:09:33 +0000105/** Allocate memory dynamically and fail the test case if this fails.
106 * The allocated memory will be filled with zeros.
107 *
108 * You must set \p pointer to \c NULL before calling this macro and
109 * put `mbedtls_free( pointer )` in the test's cleanup code.
110 *
111 * If \p length is zero, the resulting \p pointer will be \c NULL.
112 * This is usually what we want in tests since API functions are
113 * supposed to accept null pointers when a buffer size is zero.
114 *
115 * This macro expands to an instruction, not an expression.
116 * It may jump to the \c exit label.
117 *
118 * \param pointer An lvalue where the address of the allocated buffer
119 * will be stored.
120 * This expression may be evaluated multiple times.
121 * \param length Number of elements to allocate.
122 * This expression may be evaluated multiple times.
123 *
124 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125#define ASSERT_ALLOC(pointer, length) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000126 do \
127 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 TEST_ASSERT((pointer) == NULL); \
129 if ((length) != 0) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000130 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 (pointer) = mbedtls_calloc(sizeof(*(pointer)), \
132 (length)); \
133 TEST_ASSERT((pointer) != NULL); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000134 } \
135 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000137
138/** Allocate memory dynamically. If the allocation fails, skip the test case.
139 *
140 * This macro behaves like #ASSERT_ALLOC, except that if the allocation
141 * fails, it marks the test as skipped rather than failed.
142 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100143#define ASSERT_ALLOC_WEAK(pointer, length) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000144 do \
145 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 TEST_ASSERT((pointer) == NULL); \
147 if ((length) != 0) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000148 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 (pointer) = mbedtls_calloc(sizeof(*(pointer)), \
150 (length)); \
151 TEST_ASSUME((pointer) != NULL); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000152 } \
153 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000155
156/** Compare two buffers and fail the test case if they differ.
157 *
158 * This macro expands to an instruction, not an expression.
159 * It may jump to the \c exit label.
160 *
161 * \param p1 Pointer to the start of the first buffer.
162 * \param size1 Size of the first buffer in bytes.
163 * This expression may be evaluated multiple times.
164 * \param p2 Pointer to the start of the second buffer.
165 * \param size2 Size of the second buffer in bytes.
166 * This expression may be evaluated multiple times.
167 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168#define ASSERT_COMPARE(p1, size1, p2, size2) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000169 do \
170 { \
Manuel Pégourié-Gonnarda9a1b212023-02-09 09:15:04 +0100171 TEST_EQUAL((size1), (size2)); \
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 if ((size1) != 0) \
173 TEST_ASSERT(memcmp((p1), (p2), (size1)) == 0); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000174 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000176
177/**
178 * \brief This macro tests the expression passed to it and skips the
179 * running test if it doesn't evaluate to 'true'.
180 *
181 * \param TEST The test expression to be tested.
182 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100183#define TEST_ASSUME(TEST) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000184 do { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 if (!(TEST)) \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000186 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 mbedtls_test_skip( #TEST, __LINE__, __FILE__); \
Chris Jonesa6d155f2021-02-09 12:09:33 +0000188 goto exit; \
189 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192#define TEST_HELPER_ASSERT(a) if (!(a)) \
193 { \
194 mbedtls_fprintf(stderr, "Assertion Failed at %s:%d - %s\n", \
195 __FILE__, __LINE__, #a); \
196 mbedtls_exit(1); \
197 }
Ronald Cron849930a2020-06-03 08:06:47 +0200198
Ronald Cron849930a2020-06-03 08:06:47 +0200199/** Return the smaller of two values.
200 *
201 * \param x An integer-valued expression without side effects.
202 * \param y An integer-valued expression without side effects.
203 *
204 * \return The smaller of \p x and \p y.
205 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100206#define MIN(x, y) ((x) < (y) ? (x) : (y))
Ronald Cron849930a2020-06-03 08:06:47 +0200207
208/** Return the larger of two values.
209 *
210 * \param x An integer-valued expression without side effects.
211 * \param y An integer-valued expression without side effects.
212 *
213 * \return The larger of \p x and \p y.
214 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100215#define MAX(x, y) ((x) > (y) ? (x) : (y))
Ronald Cron849930a2020-06-03 08:06:47 +0200216
Ronald Cron4b8b1992020-06-09 13:52:23 +0200217#endif /* TEST_MACROS_H */