blob: f40478070388f55b2a1972386431a6deb847b776 [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
27#if !defined(MBEDTLS_CONFIG_FILE)
28#include "mbedtls/config.h"
29#else
30#include MBEDTLS_CONFIG_FILE
31#endif
32
Ronald Cron849930a2020-06-03 08:06:47 +020033#include <stdlib.h>
34
35#if defined(MBEDTLS_PLATFORM_C)
36#include "mbedtls/platform.h"
37#else
38#include <stdio.h>
39#define mbedtls_fprintf fprintf
40#define mbedtls_snprintf snprintf
41#define mbedtls_calloc calloc
42#define mbedtls_free free
43#define mbedtls_exit exit
44#define mbedtls_time time
45#define mbedtls_time_t time_t
46#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
47#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
48#endif
49
50#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
51#include "mbedtls/memory_buffer_alloc.h"
52#endif
53
54#define TEST_HELPER_ASSERT(a) if( !( a ) ) \
55{ \
56 mbedtls_fprintf( stderr, "Assertion Failed at %s:%d - %s\n", \
57 __FILE__, __LINE__, #a ); \
58 mbedtls_exit( 1 ); \
59}
60
61#if defined(__GNUC__)
62/* Test if arg and &(arg)[0] have the same type. This is true if arg is
63 * an array but not if it's a pointer. */
64#define IS_ARRAY_NOT_POINTER( arg ) \
65 ( ! __builtin_types_compatible_p( __typeof__( arg ), \
66 __typeof__( &( arg )[0] ) ) )
67#else
68/* On platforms where we don't know how to implement this check,
69 * omit it. Oh well, a non-portable check is better than nothing. */
70#define IS_ARRAY_NOT_POINTER( arg ) 1
71#endif
72
73/* A compile-time constant with the value 0. If `const_expr` is not a
74 * compile-time constant with a nonzero value, cause a compile-time error. */
75#define STATIC_ASSERT_EXPR( const_expr ) \
makise-homurae74f3722020-08-18 23:57:48 +030076 ( 0 && sizeof( struct { unsigned int STATIC_ASSERT : 1 - 2 * ! ( const_expr ); } ) )
Ronald Cron849930a2020-06-03 08:06:47 +020077/* Return the scalar value `value` (possibly promoted). This is a compile-time
78 * constant if `value` is. `condition` must be a compile-time constant.
79 * If `condition` is false, arrange to cause a compile-time error. */
80#define STATIC_ASSERT_THEN_RETURN( condition, value ) \
81 ( STATIC_ASSERT_EXPR( condition ) ? 0 : ( value ) )
82
83#define ARRAY_LENGTH_UNSAFE( array ) \
84 ( sizeof( array ) / sizeof( *( array ) ) )
85/** Return the number of elements of a static or stack array.
86 *
87 * \param array A value of array (not pointer) type.
88 *
89 * \return The number of elements of the array.
90 */
91#define ARRAY_LENGTH( array ) \
92 ( STATIC_ASSERT_THEN_RETURN( IS_ARRAY_NOT_POINTER( array ), \
93 ARRAY_LENGTH_UNSAFE( array ) ) )
94
95/** Return the smaller of two values.
96 *
97 * \param x An integer-valued expression without side effects.
98 * \param y An integer-valued expression without side effects.
99 *
100 * \return The smaller of \p x and \p y.
101 */
102#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
103
104/** Return the larger of two values.
105 *
106 * \param x An integer-valued expression without side effects.
107 * \param y An integer-valued expression without side effects.
108 *
109 * \return The larger of \p x and \p y.
110 */
111#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
112
113/*
114 * 32-bit integer manipulation macros (big endian)
115 */
116#ifndef GET_UINT32_BE
117#define GET_UINT32_BE(n,b,i) \
118{ \
119 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
120 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
121 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
122 | ( (uint32_t) (b)[(i) + 3] ); \
123}
124#endif
125
126#ifndef PUT_UINT32_BE
127#define PUT_UINT32_BE(n,b,i) \
128{ \
129 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
130 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
131 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
132 (b)[(i) + 3] = (unsigned char) ( (n) ); \
133}
134#endif
135
Ronald Cron4b8b1992020-06-09 13:52:23 +0200136#endif /* TEST_MACROS_H */