blob: dcb42811069b9e6dd4430b4d584ce8b82555aa40 [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
31#if defined(MBEDTLS_PLATFORM_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020032# include "mbedtls/platform.h"
Ronald Cron849930a2020-06-03 08:06:47 +020033#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020034# include <stdio.h>
35# define mbedtls_fprintf fprintf
36# define mbedtls_snprintf snprintf
37# define mbedtls_calloc calloc
38# define mbedtls_free free
39# define mbedtls_exit exit
40# define mbedtls_time time
41# define mbedtls_time_t time_t
42# define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
43# define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Ronald Cron849930a2020-06-03 08:06:47 +020044#endif
45
46#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020047# include "mbedtls/memory_buffer_alloc.h"
Ronald Cron849930a2020-06-03 08:06:47 +020048#endif
49
Chris Jonesa6d155f2021-02-09 12:09:33 +000050/**
51 * \brief This macro tests the expression passed to it as a test step or
52 * individual test in a test case.
53 *
54 * It allows a library function to return a value and return an error
55 * code that can be tested.
56 *
Chris Jonesa6d155f2021-02-09 12:09:33 +000057 * Failing the test means:
58 * - Mark this test case as failed.
59 * - Print a message identifying the failure.
60 * - Jump to the \c exit label.
61 *
62 * This macro expands to an instruction, not an expression.
63 * It may jump to the \c exit label.
64 *
65 * \param TEST The test expression to be tested.
66 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020067#define TEST_ASSERT(TEST) \
68 do { \
69 if (!(TEST)) { \
70 mbedtls_test_fail(#TEST, __LINE__, __FILE__); \
71 goto exit; \
72 } \
73 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +000074
75/** Evaluate two expressions and fail the test case if they have different
76 * values.
77 *
78 * \param expr1 An expression to evaluate.
79 * \param expr2 The expected value of \p expr1. This can be any
80 * expression, but it is typically a constant.
81 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020082#define TEST_EQUAL(expr1, expr2) TEST_ASSERT((expr1) == (expr2))
Chris Jonesa6d155f2021-02-09 12:09:33 +000083
84/** Allocate memory dynamically and fail the test case if this fails.
85 * The allocated memory will be filled with zeros.
86 *
87 * You must set \p pointer to \c NULL before calling this macro and
88 * put `mbedtls_free( pointer )` in the test's cleanup code.
89 *
90 * If \p length is zero, the resulting \p pointer will be \c NULL.
91 * This is usually what we want in tests since API functions are
92 * supposed to accept null pointers when a buffer size is zero.
93 *
94 * This macro expands to an instruction, not an expression.
95 * It may jump to the \c exit label.
96 *
97 * \param pointer An lvalue where the address of the allocated buffer
98 * will be stored.
99 * This expression may be evaluated multiple times.
100 * \param length Number of elements to allocate.
101 * This expression may be evaluated multiple times.
102 *
103 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200104#define ASSERT_ALLOC(pointer, length) \
105 do { \
106 TEST_ASSERT((pointer) == NULL); \
107 if ((length) != 0) { \
108 (pointer) = mbedtls_calloc(sizeof(*(pointer)), (length)); \
109 TEST_ASSERT((pointer) != NULL); \
110 } \
111 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000112
113/** Allocate memory dynamically. If the allocation fails, skip the test case.
114 *
115 * This macro behaves like #ASSERT_ALLOC, except that if the allocation
116 * fails, it marks the test as skipped rather than failed.
117 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200118#define ASSERT_ALLOC_WEAK(pointer, length) \
119 do { \
120 TEST_ASSERT((pointer) == NULL); \
121 if ((length) != 0) { \
122 (pointer) = mbedtls_calloc(sizeof(*(pointer)), (length)); \
123 TEST_ASSUME((pointer) != NULL); \
124 } \
125 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000126
127/** Compare two buffers and fail the test case if they differ.
128 *
129 * This macro expands to an instruction, not an expression.
130 * It may jump to the \c exit label.
131 *
132 * \param p1 Pointer to the start of the first buffer.
133 * \param size1 Size of the first buffer in bytes.
134 * This expression may be evaluated multiple times.
135 * \param p2 Pointer to the start of the second buffer.
136 * \param size2 Size of the second buffer in bytes.
137 * This expression may be evaluated multiple times.
138 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200139#define ASSERT_COMPARE(p1, size1, p2, size2) \
140 do { \
141 TEST_ASSERT((size1) == (size2)); \
142 if ((size1) != 0) \
143 TEST_ASSERT(memcmp((p1), (p2), (size1)) == 0); \
144 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000145
146/**
147 * \brief This macro tests the expression passed to it and skips the
148 * running test if it doesn't evaluate to 'true'.
149 *
150 * \param TEST The test expression to be tested.
151 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200152#define TEST_ASSUME(TEST) \
153 do { \
154 if (!(TEST)) { \
155 mbedtls_test_skip(#TEST, __LINE__, __FILE__); \
156 goto exit; \
157 } \
158 } while (0)
Chris Jonesa6d155f2021-02-09 12:09:33 +0000159
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200160#define TEST_HELPER_ASSERT(a) \
161 if (!(a)) { \
162 mbedtls_fprintf(stderr, "Assertion Failed at %s:%d - %s\n", __FILE__, \
163 __LINE__, #a); \
164 mbedtls_exit(1); \
165 }
Ronald Cron849930a2020-06-03 08:06:47 +0200166
Gilles Peskinec86a1652021-02-15 12:17:00 +0100167/** \def ARRAY_LENGTH
168 * Return the number of elements of a static or stack array.
169 *
170 * \param array A value of array (not pointer) type.
171 *
172 * \return The number of elements of the array.
173 */
174/* A correct implementation of ARRAY_LENGTH, but which silently gives
175 * a nonsensical result if called with a pointer rather than an array. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200176#define ARRAY_LENGTH_UNSAFE(array) (sizeof(array) / sizeof(*(array)))
Gilles Peskinec86a1652021-02-15 12:17:00 +0100177
Ronald Cron849930a2020-06-03 08:06:47 +0200178#if defined(__GNUC__)
179/* Test if arg and &(arg)[0] have the same type. This is true if arg is
180 * an array but not if it's a pointer. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200181# define IS_ARRAY_NOT_POINTER(arg) \
182 (!__builtin_types_compatible_p(__typeof__(arg), __typeof__(&(arg)[0])))
Ronald Cron849930a2020-06-03 08:06:47 +0200183/* A compile-time constant with the value 0. If `const_expr` is not a
184 * compile-time constant with a nonzero value, cause a compile-time error. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200185# define STATIC_ASSERT_EXPR(const_expr) \
186 (0 && sizeof(struct { \
187 unsigned int STATIC_ASSERT : 1 - 2 * !(const_expr); \
188 }))
Gilles Peskinec86a1652021-02-15 12:17:00 +0100189
Ronald Cron849930a2020-06-03 08:06:47 +0200190/* Return the scalar value `value` (possibly promoted). This is a compile-time
191 * constant if `value` is. `condition` must be a compile-time constant.
192 * If `condition` is false, arrange to cause a compile-time error. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200193# define STATIC_ASSERT_THEN_RETURN(condition, value) \
194 (STATIC_ASSERT_EXPR(condition) ? 0 : (value))
Ronald Cron849930a2020-06-03 08:06:47 +0200195
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200196# define ARRAY_LENGTH(array) \
197 (STATIC_ASSERT_THEN_RETURN(IS_ARRAY_NOT_POINTER(array), \
198 ARRAY_LENGTH_UNSAFE(array)))
Ronald Cron849930a2020-06-03 08:06:47 +0200199
Gilles Peskinec86a1652021-02-15 12:17:00 +0100200#else
201/* If we aren't sure the compiler supports our non-standard tricks,
202 * fall back to the unsafe implementation. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200203# define ARRAY_LENGTH(array) ARRAY_LENGTH_UNSAFE(array)
Gilles Peskinec86a1652021-02-15 12:17:00 +0100204#endif
205
Ronald Cron849930a2020-06-03 08:06:47 +0200206/** Return the smaller of two values.
207 *
208 * \param x An integer-valued expression without side effects.
209 * \param y An integer-valued expression without side effects.
210 *
211 * \return The smaller of \p x and \p y.
212 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200213#define MIN(x, y) ((x) < (y) ? (x) : (y))
Ronald Cron849930a2020-06-03 08:06:47 +0200214
215/** Return the larger of two values.
216 *
217 * \param x An integer-valued expression without side effects.
218 * \param y An integer-valued expression without side effects.
219 *
220 * \return The larger of \p x and \p y.
221 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200222#define MAX(x, y) ((x) > (y) ? (x) : (y))
Ronald Cron849930a2020-06-03 08:06:47 +0200223
224/*
225 * 32-bit integer manipulation macros (big endian)
226 */
227#ifndef GET_UINT32_BE
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200228# define GET_UINT32_BE(n, b, i) \
229 { \
230 (n) = ((uint32_t)(b)[(i)] << 24) | \
231 ((uint32_t)(b)[(i) + 1] << 16) | \
232 ((uint32_t)(b)[(i) + 2] << 8) | ((uint32_t)(b)[(i) + 3]); \
233 }
Ronald Cron849930a2020-06-03 08:06:47 +0200234#endif
235
236#ifndef PUT_UINT32_BE
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200237# define PUT_UINT32_BE(n, b, i) \
238 { \
239 (b)[(i)] = (unsigned char)((n) >> 24); \
240 (b)[(i) + 1] = (unsigned char)((n) >> 16); \
241 (b)[(i) + 2] = (unsigned char)((n) >> 8); \
242 (b)[(i) + 3] = (unsigned char)((n)); \
243 }
Ronald Cron849930a2020-06-03 08:06:47 +0200244#endif
245
Ronald Cron4b8b1992020-06-09 13:52:23 +0200246#endif /* TEST_MACROS_H */