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