blob: b1c5968c990db54e5d99ca3be5b0ac3f3a245909 [file] [log] [blame]
Gilles Peskine952f4092019-05-23 20:25:48 +02001/*
2 * Helper functions for tests that use the PSA API.
3 */
4/* Copyright (C) 2019, ARM Limited, All Rights Reserved
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 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
Gilles Peskine1838e822019-06-20 12:40:56 +020022#ifndef PSA_CRYPTO_HELPERS_H
23#define PSA_CRYPTO_HELPERS_H
24
Gilles Peskine952f4092019-05-23 20:25:48 +020025#if defined(MBEDTLS_PSA_CRYPTO_SPM)
26#include "spm/psa_defs.h"
27#endif
28#include <psa/crypto.h>
29
30/** Evaluate an expression and fail the test case if it returns an error.
31 *
32 * \param expr The expression to evaluate. This is typically a call
33 * to a \c psa_xxx function that returns a value of type
34 * #psa_status_t.
35 */
36#define PSA_ASSERT( expr ) TEST_EQUAL( ( expr ), PSA_SUCCESS )
37
Gilles Peskinedd413d32019-05-28 15:06:43 +020038static int test_helper_is_psa_pristine( int line, const char *file )
Gilles Peskinea6d252a2019-05-23 20:34:30 +020039{
40 mbedtls_psa_stats_t stats;
41 const char *msg = NULL;
42
43 mbedtls_psa_get_stats( &stats );
44
45 if( stats.volatile_slots != 0 )
46 msg = "A volatile slot has not been closed properly.";
47 else if( stats.persistent_slots != 0 )
48 msg = "A persistent slot has not been closed properly.";
49 else if( stats.external_slots != 0 )
50 msg = "An external slot has not been closed properly.";
51 else if( stats.half_filled_slots != 0 )
52 msg = "A half-filled slot has not been cleared properly.";
53
Gilles Peskinedd413d32019-05-28 15:06:43 +020054 /* If the test has already failed, don't overwrite the failure
55 * information. Do keep the stats lookup above, because it can be
56 * convenient to break on it when debugging a failure. */
Gilles Peskinea6d252a2019-05-23 20:34:30 +020057 if( msg != NULL && test_info.failed == 0 )
58 test_fail( msg, line, file );
59
Gilles Peskinedd413d32019-05-28 15:06:43 +020060 return( msg == NULL );
61}
62
63/** Check that no PSA slots are in use.
64 */
65#define ASSERT_PSA_PRISTINE( ) \
66 do \
67 { \
68 if( ! test_helper_is_psa_pristine( __LINE__, __FILE__ ) ) \
69 goto exit; \
70 } \
71 while( 0 )
72
73static void test_helper_psa_done( int line, const char *file )
74{
75 (void) test_helper_is_psa_pristine( line, file );
Gilles Peskinea6d252a2019-05-23 20:34:30 +020076 mbedtls_psa_crypto_free( );
77}
78
79/** Shut down the PSA subsystem. Expect a clean shutdown, with no slots
80 * in use.
81 */
82#define PSA_DONE( ) test_helper_psa_done( __LINE__, __FILE__ )
83
Gilles Peskine1838e822019-06-20 12:40:56 +020084#endif /* PSA_CRYPTO_HELPERS_H */