Gilles Peskine | 1061ec6 | 2021-01-29 21:17:11 +0100 | [diff] [blame^] | 1 | /** Mutex usage verification framework. */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright The Mbed TLS Contributors |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
| 20 | #include <test/helpers.h> |
| 21 | #include <test/macros.h> |
| 22 | |
| 23 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 24 | |
| 25 | #include "mbedtls/threading.h" |
| 26 | |
| 27 | typedef struct |
| 28 | { |
| 29 | void (*init)( mbedtls_threading_mutex_t * ); |
| 30 | void (*free)( mbedtls_threading_mutex_t * ); |
| 31 | int (*lock)( mbedtls_threading_mutex_t * ); |
| 32 | int (*unlock)( mbedtls_threading_mutex_t * ); |
| 33 | } mutex_functions_t; |
| 34 | static mutex_functions_t mutex_functions; |
| 35 | |
| 36 | static void mbedtls_test_wrap_mutex_init( mbedtls_threading_mutex_t *mutex ) |
| 37 | { |
| 38 | mutex_functions.init( mutex ); |
| 39 | } |
| 40 | |
| 41 | static void mbedtls_test_wrap_mutex_free( mbedtls_threading_mutex_t *mutex ) |
| 42 | { |
| 43 | mutex_functions.free( mutex ); |
| 44 | } |
| 45 | |
| 46 | static int mbedtls_test_wrap_mutex_lock( mbedtls_threading_mutex_t *mutex ) |
| 47 | { |
| 48 | int ret = mutex_functions.lock( mutex ); |
| 49 | return( ret ); |
| 50 | } |
| 51 | |
| 52 | static int mbedtls_test_wrap_mutex_unlock( mbedtls_threading_mutex_t *mutex ) |
| 53 | { |
| 54 | return( mutex_functions.unlock( mutex ) ); |
| 55 | } |
| 56 | |
| 57 | void mbedtls_test_mutex_usage_init( void ) |
| 58 | { |
| 59 | mutex_functions.init = mbedtls_mutex_init; |
| 60 | mutex_functions.free = mbedtls_mutex_free; |
| 61 | mutex_functions.lock = mbedtls_mutex_lock; |
| 62 | mutex_functions.unlock = mbedtls_mutex_unlock; |
| 63 | mbedtls_mutex_init = &mbedtls_test_wrap_mutex_init; |
| 64 | mbedtls_mutex_free = &mbedtls_test_wrap_mutex_free; |
| 65 | mbedtls_mutex_lock = &mbedtls_test_wrap_mutex_lock; |
| 66 | mbedtls_mutex_unlock = &mbedtls_test_wrap_mutex_unlock; |
| 67 | } |
| 68 | |
| 69 | #endif /* MBEDTLS_TEST_MUTEX_USAGE */ |