blob: f94b8f44347f46d9cf1a36d8c082e0a2fb722e0e [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
Bence Szépkútic662b362021-05-27 11:25:03 +020026#include "mbedtls/build_info.h"
Gilles Peskinec4672fd2019-09-11 13:39:11 +020027
28/** Helper to define a function as static except when building invasive tests.
29 *
30 * If a function is only used inside its own source file and should be
31 * declared `static` to allow the compiler to optimize for code size,
32 * but that function has unit tests, define it with
33 * ```
34 * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... }
35 * ```
36 * and declare it in a header in the `library/` directory with
37 * ```
38 * #if defined(MBEDTLS_TEST_HOOKS)
39 * int mbedtls_foo(...);
40 * #endif
41 * ```
42 */
43#if defined(MBEDTLS_TEST_HOOKS)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020044# define MBEDTLS_STATIC_TESTABLE
Gilles Peskinec4672fd2019-09-11 13:39:11 +020045#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020046# define MBEDTLS_STATIC_TESTABLE static
Gilles Peskinec4672fd2019-09-11 13:39:11 +020047#endif
48
TRodziewicz7871c2e2021-07-07 17:29:43 +020049#if defined(MBEDTLS_TEST_HOOKS)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020050extern void (*mbedtls_test_hook_test_fail)(const char *test,
51 int line,
52 const char *file);
53# define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST) \
54 do { \
55 if ((!(TEST)) && ((*mbedtls_test_hook_test_fail) != NULL)) { \
56 (*mbedtls_test_hook_test_fail)(#TEST, __LINE__, __FILE__); \
57 } \
58 } while (0)
TRodziewicz7871c2e2021-07-07 17:29:43 +020059#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020060# define MBEDTLS_TEST_HOOK_TEST_ASSERT(TEST)
TRodziewicz7871c2e2021-07-07 17:29:43 +020061#endif /* defined(MBEDTLS_TEST_HOOKS) */
62
Mateusz Starzyk57d1d192021-05-27 14:39:53 +020063/** Allow library to access its structs' private members.
Mateusz Starzyk2c09c9b2021-05-14 22:20:10 +020064 *
65 * Although structs defined in header files are publicly available,
66 * their members are private and should not be accessed by the user.
67 */
68#define MBEDTLS_ALLOW_PRIVATE_ACCESS
69
Gilles Peskinec4672fd2019-09-11 13:39:11 +020070#endif /* MBEDTLS_LIBRARY_COMMON_H */