xref: /aosp_15_r20/external/mbedtls/tests/include/test/macros.h (revision 62c56f9862f102b96d72393aff6076c951fb8148)
1*62c56f98SSadaf Ebrahimi /**
2*62c56f98SSadaf Ebrahimi  * \file macros.h
3*62c56f98SSadaf Ebrahimi  *
4*62c56f98SSadaf Ebrahimi  * \brief   This file contains generic macros for the purpose of testing.
5*62c56f98SSadaf Ebrahimi  */
6*62c56f98SSadaf Ebrahimi 
7*62c56f98SSadaf Ebrahimi /*
8*62c56f98SSadaf Ebrahimi  *  Copyright The Mbed TLS Contributors
9*62c56f98SSadaf Ebrahimi  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
10*62c56f98SSadaf Ebrahimi  */
11*62c56f98SSadaf Ebrahimi 
12*62c56f98SSadaf Ebrahimi #ifndef TEST_MACROS_H
13*62c56f98SSadaf Ebrahimi #define TEST_MACROS_H
14*62c56f98SSadaf Ebrahimi 
15*62c56f98SSadaf Ebrahimi #include "mbedtls/build_info.h"
16*62c56f98SSadaf Ebrahimi 
17*62c56f98SSadaf Ebrahimi #include <stdlib.h>
18*62c56f98SSadaf Ebrahimi 
19*62c56f98SSadaf Ebrahimi #include "mbedtls/platform.h"
20*62c56f98SSadaf Ebrahimi 
21*62c56f98SSadaf Ebrahimi #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
22*62c56f98SSadaf Ebrahimi #include "mbedtls/memory_buffer_alloc.h"
23*62c56f98SSadaf Ebrahimi #endif
24*62c56f98SSadaf Ebrahimi #include "common.h"
25*62c56f98SSadaf Ebrahimi 
26*62c56f98SSadaf Ebrahimi /**
27*62c56f98SSadaf Ebrahimi  * \brief   This macro tests the expression passed to it as a test step or
28*62c56f98SSadaf Ebrahimi  *          individual test in a test case.
29*62c56f98SSadaf Ebrahimi  *
30*62c56f98SSadaf Ebrahimi  *          It allows a library function to return a value and return an error
31*62c56f98SSadaf Ebrahimi  *          code that can be tested.
32*62c56f98SSadaf Ebrahimi  *
33*62c56f98SSadaf Ebrahimi  *          Failing the test means:
34*62c56f98SSadaf Ebrahimi  *          - Mark this test case as failed.
35*62c56f98SSadaf Ebrahimi  *          - Print a message identifying the failure.
36*62c56f98SSadaf Ebrahimi  *          - Jump to the \c exit label.
37*62c56f98SSadaf Ebrahimi  *
38*62c56f98SSadaf Ebrahimi  *          This macro expands to an instruction, not an expression.
39*62c56f98SSadaf Ebrahimi  *          It may jump to the \c exit label.
40*62c56f98SSadaf Ebrahimi  *
41*62c56f98SSadaf Ebrahimi  * \param   TEST    The test expression to be tested.
42*62c56f98SSadaf Ebrahimi  */
43*62c56f98SSadaf Ebrahimi #define TEST_ASSERT(TEST)                                 \
44*62c56f98SSadaf Ebrahimi     do {                                                    \
45*62c56f98SSadaf Ebrahimi         if (!(TEST))                                       \
46*62c56f98SSadaf Ebrahimi         {                                                    \
47*62c56f98SSadaf Ebrahimi             mbedtls_test_fail( #TEST, __LINE__, __FILE__);   \
48*62c56f98SSadaf Ebrahimi             goto exit;                                        \
49*62c56f98SSadaf Ebrahimi         }                                                    \
50*62c56f98SSadaf Ebrahimi     } while (0)
51*62c56f98SSadaf Ebrahimi 
52*62c56f98SSadaf Ebrahimi /** This macro asserts fails the test with given output message.
53*62c56f98SSadaf Ebrahimi  *
54*62c56f98SSadaf Ebrahimi  * \param   MESSAGE The message to be outputed on assertion
55*62c56f98SSadaf Ebrahimi  */
56*62c56f98SSadaf Ebrahimi #define TEST_FAIL(MESSAGE)                           \
57*62c56f98SSadaf Ebrahimi     do {                                                  \
58*62c56f98SSadaf Ebrahimi         mbedtls_test_fail(MESSAGE, __LINE__, __FILE__);   \
59*62c56f98SSadaf Ebrahimi         goto exit;                                        \
60*62c56f98SSadaf Ebrahimi     } while (0)
61*62c56f98SSadaf Ebrahimi 
62*62c56f98SSadaf Ebrahimi /** Evaluate two integer expressions and fail the test case if they have
63*62c56f98SSadaf Ebrahimi  * different values.
64*62c56f98SSadaf Ebrahimi  *
65*62c56f98SSadaf Ebrahimi  * The two expressions should have the same signedness, otherwise the
66*62c56f98SSadaf Ebrahimi  * comparison is not meaningful if the signed value is negative.
67*62c56f98SSadaf Ebrahimi  *
68*62c56f98SSadaf Ebrahimi  * \param expr1     An integral-typed expression to evaluate.
69*62c56f98SSadaf Ebrahimi  * \param expr2     Another integral-typed expression to evaluate.
70*62c56f98SSadaf Ebrahimi  */
71*62c56f98SSadaf Ebrahimi #define TEST_EQUAL(expr1, expr2)                                      \
72*62c56f98SSadaf Ebrahimi     do {                                                                \
73*62c56f98SSadaf Ebrahimi         if (!mbedtls_test_equal( #expr1 " == " #expr2, __LINE__, __FILE__, \
74*62c56f98SSadaf Ebrahimi                                  (unsigned long long) (expr1), (unsigned long long) (expr2)))                      \
75*62c56f98SSadaf Ebrahimi         goto exit;                                                  \
76*62c56f98SSadaf Ebrahimi     } while (0)
77*62c56f98SSadaf Ebrahimi 
78*62c56f98SSadaf Ebrahimi /** Evaluate two unsigned integer expressions and fail the test case
79*62c56f98SSadaf Ebrahimi  * if they are not in increasing order (left <= right).
80*62c56f98SSadaf Ebrahimi  *
81*62c56f98SSadaf Ebrahimi  * \param expr1     An integral-typed expression to evaluate.
82*62c56f98SSadaf Ebrahimi  * \param expr2     Another integral-typed expression to evaluate.
83*62c56f98SSadaf Ebrahimi  */
84*62c56f98SSadaf Ebrahimi #define TEST_LE_U(expr1, expr2)                                       \
85*62c56f98SSadaf Ebrahimi     do {                                                                \
86*62c56f98SSadaf Ebrahimi         if (!mbedtls_test_le_u( #expr1 " <= " #expr2, __LINE__, __FILE__, \
87*62c56f98SSadaf Ebrahimi                                 expr1, expr2))                      \
88*62c56f98SSadaf Ebrahimi         goto exit;                                                  \
89*62c56f98SSadaf Ebrahimi     } while (0)
90*62c56f98SSadaf Ebrahimi 
91*62c56f98SSadaf Ebrahimi /** Evaluate two signed integer expressions and fail the test case
92*62c56f98SSadaf Ebrahimi  * if they are not in increasing order (left <= right).
93*62c56f98SSadaf Ebrahimi  *
94*62c56f98SSadaf Ebrahimi  * \param expr1     An integral-typed expression to evaluate.
95*62c56f98SSadaf Ebrahimi  * \param expr2     Another integral-typed expression to evaluate.
96*62c56f98SSadaf Ebrahimi  */
97*62c56f98SSadaf Ebrahimi #define TEST_LE_S(expr1, expr2)                                       \
98*62c56f98SSadaf Ebrahimi     do {                                                                \
99*62c56f98SSadaf Ebrahimi         if (!mbedtls_test_le_s( #expr1 " <= " #expr2, __LINE__, __FILE__, \
100*62c56f98SSadaf Ebrahimi                                 expr1, expr2))                      \
101*62c56f98SSadaf Ebrahimi         goto exit;                                                  \
102*62c56f98SSadaf Ebrahimi     } while (0)
103*62c56f98SSadaf Ebrahimi 
104*62c56f98SSadaf Ebrahimi /** Allocate memory dynamically and fail the test case if this fails.
105*62c56f98SSadaf Ebrahimi  * The allocated memory will be filled with zeros.
106*62c56f98SSadaf Ebrahimi  *
107*62c56f98SSadaf Ebrahimi  * You must set \p pointer to \c NULL before calling this macro and
108*62c56f98SSadaf Ebrahimi  * put `mbedtls_free(pointer)` in the test's cleanup code.
109*62c56f98SSadaf Ebrahimi  *
110*62c56f98SSadaf Ebrahimi  * If \p item_count is zero, the resulting \p pointer will be \c NULL.
111*62c56f98SSadaf Ebrahimi  * This is usually what we want in tests since API functions are
112*62c56f98SSadaf Ebrahimi  * supposed to accept null pointers when a buffer size is zero.
113*62c56f98SSadaf Ebrahimi  *
114*62c56f98SSadaf Ebrahimi  * This macro expands to an instruction, not an expression.
115*62c56f98SSadaf Ebrahimi  * It may jump to the \c exit label.
116*62c56f98SSadaf Ebrahimi  *
117*62c56f98SSadaf Ebrahimi  * \param pointer    An lvalue where the address of the allocated buffer
118*62c56f98SSadaf Ebrahimi  *                   will be stored.
119*62c56f98SSadaf Ebrahimi  *                   This expression may be evaluated multiple times.
120*62c56f98SSadaf Ebrahimi  * \param item_count Number of elements to allocate.
121*62c56f98SSadaf Ebrahimi  *                   This expression may be evaluated multiple times.
122*62c56f98SSadaf Ebrahimi  *
123*62c56f98SSadaf Ebrahimi  */
124*62c56f98SSadaf Ebrahimi #define TEST_CALLOC(pointer, item_count)                    \
125*62c56f98SSadaf Ebrahimi     do {                                                    \
126*62c56f98SSadaf Ebrahimi         TEST_ASSERT((pointer) == NULL);                     \
127*62c56f98SSadaf Ebrahimi         if ((item_count) != 0) {                            \
128*62c56f98SSadaf Ebrahimi             (pointer) = mbedtls_calloc(sizeof(*(pointer)),  \
129*62c56f98SSadaf Ebrahimi                                        (item_count));       \
130*62c56f98SSadaf Ebrahimi             TEST_ASSERT((pointer) != NULL);                 \
131*62c56f98SSadaf Ebrahimi         }                                                   \
132*62c56f98SSadaf Ebrahimi     } while (0)
133*62c56f98SSadaf Ebrahimi 
134*62c56f98SSadaf Ebrahimi /** Allocate memory dynamically and fail the test case if this fails.
135*62c56f98SSadaf Ebrahimi  * The allocated memory will be filled with zeros.
136*62c56f98SSadaf Ebrahimi  *
137*62c56f98SSadaf Ebrahimi  * You must set \p pointer to \c NULL before calling this macro and
138*62c56f98SSadaf Ebrahimi  * put `mbedtls_free(pointer)` in the test's cleanup code.
139*62c56f98SSadaf Ebrahimi  *
140*62c56f98SSadaf Ebrahimi  * If \p item_count is zero, the resulting \p pointer will not be \c NULL.
141*62c56f98SSadaf Ebrahimi  *
142*62c56f98SSadaf Ebrahimi  * This macro expands to an instruction, not an expression.
143*62c56f98SSadaf Ebrahimi  * It may jump to the \c exit label.
144*62c56f98SSadaf Ebrahimi  *
145*62c56f98SSadaf Ebrahimi  * \param pointer    An lvalue where the address of the allocated buffer
146*62c56f98SSadaf Ebrahimi  *                   will be stored.
147*62c56f98SSadaf Ebrahimi  *                   This expression may be evaluated multiple times.
148*62c56f98SSadaf Ebrahimi  * \param item_count Number of elements to allocate.
149*62c56f98SSadaf Ebrahimi  *                   This expression may be evaluated multiple times.
150*62c56f98SSadaf Ebrahimi  *
151*62c56f98SSadaf Ebrahimi  * Note: if passing size 0, mbedtls_calloc may return NULL. In this case,
152*62c56f98SSadaf Ebrahimi  * we reattempt to allocate with the smallest possible buffer to assure a
153*62c56f98SSadaf Ebrahimi  * non-NULL pointer.
154*62c56f98SSadaf Ebrahimi  */
155*62c56f98SSadaf Ebrahimi #define TEST_CALLOC_NONNULL(pointer, item_count)            \
156*62c56f98SSadaf Ebrahimi     do {                                                    \
157*62c56f98SSadaf Ebrahimi         TEST_ASSERT((pointer) == NULL);                     \
158*62c56f98SSadaf Ebrahimi         (pointer) = mbedtls_calloc(sizeof(*(pointer)),      \
159*62c56f98SSadaf Ebrahimi                                    (item_count));           \
160*62c56f98SSadaf Ebrahimi         if (((pointer) == NULL) && ((item_count) == 0)) {   \
161*62c56f98SSadaf Ebrahimi             (pointer) = mbedtls_calloc(1, 1);               \
162*62c56f98SSadaf Ebrahimi         }                                                   \
163*62c56f98SSadaf Ebrahimi         TEST_ASSERT((pointer) != NULL);                     \
164*62c56f98SSadaf Ebrahimi     } while (0)
165*62c56f98SSadaf Ebrahimi 
166*62c56f98SSadaf Ebrahimi /* For backwards compatibility */
167*62c56f98SSadaf Ebrahimi #define ASSERT_ALLOC(pointer, item_count) TEST_CALLOC(pointer, item_count)
168*62c56f98SSadaf Ebrahimi 
169*62c56f98SSadaf Ebrahimi /** Allocate memory dynamically. If the allocation fails, skip the test case.
170*62c56f98SSadaf Ebrahimi  *
171*62c56f98SSadaf Ebrahimi  * This macro behaves like #TEST_CALLOC, except that if the allocation
172*62c56f98SSadaf Ebrahimi  * fails, it marks the test as skipped rather than failed.
173*62c56f98SSadaf Ebrahimi  */
174*62c56f98SSadaf Ebrahimi #define TEST_CALLOC_OR_SKIP(pointer, item_count)            \
175*62c56f98SSadaf Ebrahimi     do {                                                    \
176*62c56f98SSadaf Ebrahimi         TEST_ASSERT((pointer) == NULL);                     \
177*62c56f98SSadaf Ebrahimi         if ((item_count) != 0) {                            \
178*62c56f98SSadaf Ebrahimi             (pointer) = mbedtls_calloc(sizeof(*(pointer)),  \
179*62c56f98SSadaf Ebrahimi                                        (item_count));       \
180*62c56f98SSadaf Ebrahimi             TEST_ASSUME((pointer) != NULL);                 \
181*62c56f98SSadaf Ebrahimi         }                                                   \
182*62c56f98SSadaf Ebrahimi     } while (0)
183*62c56f98SSadaf Ebrahimi 
184*62c56f98SSadaf Ebrahimi /* For backwards compatibility */
185*62c56f98SSadaf Ebrahimi #define ASSERT_ALLOC_WEAK(pointer, item_count) TEST_CALLOC_OR_SKIP(pointer, item_count)
186*62c56f98SSadaf Ebrahimi 
187*62c56f98SSadaf Ebrahimi /** Compare two buffers and fail the test case if they differ.
188*62c56f98SSadaf Ebrahimi  *
189*62c56f98SSadaf Ebrahimi  * This macro expands to an instruction, not an expression.
190*62c56f98SSadaf Ebrahimi  * It may jump to the \c exit label.
191*62c56f98SSadaf Ebrahimi  *
192*62c56f98SSadaf Ebrahimi  * \param p1        Pointer to the start of the first buffer.
193*62c56f98SSadaf Ebrahimi  * \param size1     Size of the first buffer in bytes.
194*62c56f98SSadaf Ebrahimi  *                  This expression may be evaluated multiple times.
195*62c56f98SSadaf Ebrahimi  * \param p2        Pointer to the start of the second buffer.
196*62c56f98SSadaf Ebrahimi  * \param size2     Size of the second buffer in bytes.
197*62c56f98SSadaf Ebrahimi  *                  This expression may be evaluated multiple times.
198*62c56f98SSadaf Ebrahimi  */
199*62c56f98SSadaf Ebrahimi #define TEST_MEMORY_COMPARE(p1, size1, p2, size2)              \
200*62c56f98SSadaf Ebrahimi     do {                                                       \
201*62c56f98SSadaf Ebrahimi         TEST_EQUAL((size1), (size2));                          \
202*62c56f98SSadaf Ebrahimi         if ((size1) != 0) {                                    \
203*62c56f98SSadaf Ebrahimi             TEST_ASSERT(memcmp((p1), (p2), (size1)) == 0);     \
204*62c56f98SSadaf Ebrahimi         }                                                      \
205*62c56f98SSadaf Ebrahimi     } while (0)
206*62c56f98SSadaf Ebrahimi 
207*62c56f98SSadaf Ebrahimi /* For backwards compatibility */
208*62c56f98SSadaf Ebrahimi #define ASSERT_COMPARE(p1, size1, p2, size2) TEST_MEMORY_COMPARE(p1, size1, p2, size2)
209*62c56f98SSadaf Ebrahimi 
210*62c56f98SSadaf Ebrahimi /**
211*62c56f98SSadaf Ebrahimi  * \brief   This macro tests the expression passed to it and skips the
212*62c56f98SSadaf Ebrahimi  *          running test if it doesn't evaluate to 'true'.
213*62c56f98SSadaf Ebrahimi  *
214*62c56f98SSadaf Ebrahimi  * \param   TEST    The test expression to be tested.
215*62c56f98SSadaf Ebrahimi  */
216*62c56f98SSadaf Ebrahimi #define TEST_ASSUME(TEST)                                 \
217*62c56f98SSadaf Ebrahimi     do {                                                    \
218*62c56f98SSadaf Ebrahimi         if (!(TEST))                                      \
219*62c56f98SSadaf Ebrahimi         {                                                   \
220*62c56f98SSadaf Ebrahimi             mbedtls_test_skip( #TEST, __LINE__, __FILE__); \
221*62c56f98SSadaf Ebrahimi             goto exit;                                      \
222*62c56f98SSadaf Ebrahimi         }                                                   \
223*62c56f98SSadaf Ebrahimi     } while (0)
224*62c56f98SSadaf Ebrahimi 
225*62c56f98SSadaf Ebrahimi #define TEST_HELPER_ASSERT(a) if (!(a))                          \
226*62c56f98SSadaf Ebrahimi     {                                                                   \
227*62c56f98SSadaf Ebrahimi         mbedtls_fprintf(stderr, "Assertion Failed at %s:%d - %s\n",    \
228*62c56f98SSadaf Ebrahimi                         __FILE__, __LINE__, #a);              \
229*62c56f98SSadaf Ebrahimi         mbedtls_exit(1);                                              \
230*62c56f98SSadaf Ebrahimi     }
231*62c56f98SSadaf Ebrahimi 
232*62c56f98SSadaf Ebrahimi /** Return the smaller of two values.
233*62c56f98SSadaf Ebrahimi  *
234*62c56f98SSadaf Ebrahimi  * \param x         An integer-valued expression without side effects.
235*62c56f98SSadaf Ebrahimi  * \param y         An integer-valued expression without side effects.
236*62c56f98SSadaf Ebrahimi  *
237*62c56f98SSadaf Ebrahimi  * \return The smaller of \p x and \p y.
238*62c56f98SSadaf Ebrahimi  */
239*62c56f98SSadaf Ebrahimi #define MIN(x, y) ((x) < (y) ? (x) : (y))
240*62c56f98SSadaf Ebrahimi 
241*62c56f98SSadaf Ebrahimi /** Return the larger of two values.
242*62c56f98SSadaf Ebrahimi  *
243*62c56f98SSadaf Ebrahimi  * \param x         An integer-valued expression without side effects.
244*62c56f98SSadaf Ebrahimi  * \param y         An integer-valued expression without side effects.
245*62c56f98SSadaf Ebrahimi  *
246*62c56f98SSadaf Ebrahimi  * \return The larger of \p x and \p y.
247*62c56f98SSadaf Ebrahimi  */
248*62c56f98SSadaf Ebrahimi #define MAX(x, y) ((x) > (y) ? (x) : (y))
249*62c56f98SSadaf Ebrahimi 
250*62c56f98SSadaf Ebrahimi #endif /* TEST_MACROS_H */
251