blob: f4cef97b911a09a19fb0891707d3d219de483463 [file] [log] [blame]
Gilles Peskinec4672fd2019-09-11 13:39:11 +02001/**
2 * \file common.h
3 *
4 * \brief Utility macros for internal use in the library
5 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Gilles Peskinec4672fd2019-09-11 13:39:11 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Gilles Peskinec4672fd2019-09-11 13:39:11 +020021 */
22
23#ifndef MBEDTLS_LIBRARY_COMMON_H
24#define MBEDTLS_LIBRARY_COMMON_H
25
26#if defined(MBEDTLS_CONFIG_FILE)
27#include MBEDTLS_CONFIG_FILE
28#else
29#include "mbedtls/config.h"
30#endif
31
Chris Jones5e8805a2021-01-12 15:21:57 +000032#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskinec4672fd2019-09-11 13:39:11 +020033/** Helper to define a function as static except when building invasive tests.
34 *
35 * If a function is only used inside its own source file and should be
36 * declared `static` to allow the compiler to optimize for code size,
37 * but that function has unit tests, define it with
38 * ```
39 * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
40 * ```
41 * and declare it in a header in the `library/` directory with
42 * ```
43 * #if defined(MBEDTLS_TEST_HOOKS)
44 * int mbedtls_foo(...);
45 * #endif
46 * ```
47 */
Gilles Peskinec4672fd2019-09-11 13:39:11 +020048#define MBEDTLS_STATIC_TESTABLE
Chris Jones5e8805a2021-01-12 15:21:57 +000049
50/** Helper macro and function to combine a high and low level error code.
51 *
52 * This function uses a hook (`mbedtls_test_err_add_hook`) to allow invasive
53 * testing of its inputs. This is used in the test infrastructure to report
54 * on errors when combining two error codes of the same level (e.g: two high
55 * or two low level errors).
56 */
57int mbedtls_err_add( int high, int low, const char *file, int line );
58#define MBEDTLS_ERR_ADD( high, low ) \
59 ( mbedtls_err_add( high, low, __FILE__, __LINE__ ) )
60
Gilles Peskinec4672fd2019-09-11 13:39:11 +020061#else
62#define MBEDTLS_STATIC_TESTABLE static
Chris Jones5e8805a2021-01-12 15:21:57 +000063
64#define MBEDTLS_ERR_ADD( high, low ) \
65 ( high + low )
66
67#endif /* MBEDTLS_TEST_HOOKS */
Gilles Peskinec4672fd2019-09-11 13:39:11 +020068
69#endif /* MBEDTLS_LIBRARY_COMMON_H */