blob: 46d476360a94ebac03a461ba0be2d84fd10dc62c [file] [log] [blame]
Bence Szépkúti80b31c52021-10-19 15:05:36 +02001#!/usr/bin/env python3
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002"""Run the PSA Crypto API compliance test suite.
Bence Szépkúti449781f2021-11-02 13:41:14 +01003Clone the repo and check out the commit specified by PSA_ARCH_TEST_REPO and PSA_ARCH_TEST_REF,
Tom Cosgrove1797b052022-12-04 17:19:59 +00004then compile and run the test suite. The clone is stored at <Mbed TLS root>/psa-arch-tests.
Bence Szépkúti449781f2021-11-02 13:41:14 +01005Known defects in either the test suite or mbedtls - identified by their test number - are ignored,
6while unexpected failures AND successes are reported as errors,
7to help keep the list of known defects as up to date as possible.
8"""
Bence Szépkúti67fb3142021-11-02 14:01:08 +01009
10# Copyright The Mbed TLS Contributors
11# SPDX-License-Identifier: Apache-2.0
12#
13# Licensed under the Apache License, Version 2.0 (the "License"); you may
14# not use this file except in compliance with the License.
15# You may obtain a copy of the License at
16#
17# http://www.apache.org/licenses/LICENSE-2.0
18#
19# Unless required by applicable law or agreed to in writing, software
20# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22# See the License for the specific language governing permissions and
23# limitations under the License.
24
David Horstmann4dcddcf2023-08-17 18:08:24 +010025import argparse
Bence Szépkúti80b31c52021-10-19 15:05:36 +020026import os
27import re
28import shutil
29import subprocess
30import sys
31
David Horstmanne31014a2023-07-19 11:43:27 +010032#pylint: disable=unused-import
David Horstmann1d091842023-07-18 17:39:35 +010033import scripts_path
34from mbedtls_dev import build_tree
35
Bence Szépkúticb288712021-11-09 21:30:43 +010036# PSA Compliance tests we expect to fail due to known defects in Mbed TLS (or the test suite)
37# The test numbers correspond to the numbers used by the console output of the test suite.
38# Test number 2xx corresponds to the files in the folder
39# psa-arch-tests/api-tests/dev_apis/crypto/test_c0xx
Bence Szépkúti80b31c52021-10-19 15:05:36 +020040EXPECTED_FAILURES = {
Bence Szépkúticb288712021-11-09 21:30:43 +010041 # psa_hash_suspend() and psa_hash_resume() are not supported.
42 # - Tracked in issue #3274
43 262, 263
Bence Szépkúti80b31c52021-10-19 15:05:36 +020044}
Bence Szépkútie2855c32021-11-09 17:33:57 +010045
46# We currently use a fork of ARM-software/psa-arch-tests, with a couple of downstream patches
47# that allow it to build with MbedTLS 3, and fixes a couple of issues in the compliance test suite.
48# These fixes allow the tests numbered 216, 248 and 249 to complete successfully.
49#
50# Once all the fixes are upstreamed, this fork should be replaced with an upstream commit/tag.
Bence Szépkútib376eac2021-11-09 22:13:46 +010051# - Tracked in issue #5145
Bence Szépkútie2855c32021-11-09 17:33:57 +010052#
53# Web URL: https://github.com/bensze01/psa-arch-tests/tree/fixes-for-mbedtls-3
54PSA_ARCH_TESTS_REPO = 'https://github.com/bensze01/psa-arch-tests.git'
Gilles Peskine42ed9632022-05-17 17:23:09 +020055PSA_ARCH_TESTS_REF = 'fix-pr-5736'
Bence Szépkúti80b31c52021-10-19 15:05:36 +020056
David Horstmanne31014a2023-07-19 11:43:27 +010057#pylint: disable=too-many-branches,too-many-statements,too-many-locals
David Horstmann4dcddcf2023-08-17 18:08:24 +010058def main(library_build_dir: str):
Bence Szépkúti80b31c52021-10-19 15:05:36 +020059 mbedtls_dir = os.getcwd()
60
David Horstmann0ac57ca2023-08-23 16:24:55 +010061 in_psa_crypto_repo = build_tree.looks_like_psa_crypto_root(mbedtls_dir)
David Horstmann1d091842023-07-18 17:39:35 +010062
David Horstmann4dcddcf2023-08-17 18:08:24 +010063 if not os.path.exists(library_build_dir + '/library/libmbedcrypto.a'):
64 subprocess.check_call([
65 'cmake', '.',
66 '-GUnix Makefiles',
67 '-B', library_build_dir
68 ])
69 subprocess.check_call(['cmake', '--build', library_build_dir])
Bence Szépkúti80b31c52021-10-19 15:05:36 +020070
71 psa_arch_tests_dir = 'psa-arch-tests'
Bence Szépkútic63d1602021-11-02 14:06:40 +010072 os.makedirs(psa_arch_tests_dir, exist_ok=True)
Bence Szépkúti80b31c52021-10-19 15:05:36 +020073 try:
Bence Szépkúti34b5f562021-11-02 13:48:39 +010074 os.chdir(psa_arch_tests_dir)
Bence Szépkúti80b31c52021-10-19 15:05:36 +020075
Bence Szépkútib3818412021-11-03 11:32:51 +010076 # Reuse existing local clone
Bence Szépkúti34b5f562021-11-02 13:48:39 +010077 subprocess.check_call(['git', 'init'])
78 subprocess.check_call(['git', 'fetch', PSA_ARCH_TESTS_REPO, PSA_ARCH_TESTS_REF])
79 subprocess.check_call(['git', 'checkout', 'FETCH_HEAD'])
Bence Szépkúti80b31c52021-10-19 15:05:36 +020080
Bence Szépkúti34b5f562021-11-02 13:48:39 +010081 build_dir = 'api-tests/build'
82 try:
83 shutil.rmtree(build_dir)
84 except FileNotFoundError:
85 pass
86 os.mkdir(build_dir)
87 os.chdir(build_dir)
Bence Szépkúti80b31c52021-10-19 15:05:36 +020088
David Horstmann0ac57ca2023-08-23 16:24:55 +010089 if in_psa_crypto_repo:
David Horstmann7f93d222023-08-23 16:21:40 +010090 crypto_lib_filename = \
David Horstmann4dcddcf2023-08-17 18:08:24 +010091 library_build_dir + '/core/libpsacrypto.a'
David Horstmann1d091842023-07-18 17:39:35 +010092 else:
David Horstmann7f93d222023-08-23 16:21:40 +010093 crypto_lib_filename = library_build_dir + '/library/libmbedcrypto.a'
David Horstmann1d091842023-07-18 17:39:35 +010094
95 extra_includes = (';{}/drivers/builtin/include'.format(mbedtls_dir)
David Horstmann0ac57ca2023-08-23 16:24:55 +010096 if in_psa_crypto_repo else '')
David Horstmann1d091842023-07-18 17:39:35 +010097
Bence Szépkúti34b5f562021-11-02 13:48:39 +010098 #pylint: disable=bad-continuation
99 subprocess.check_call([
100 'cmake', '..',
101 '-GUnix Makefiles',
102 '-DTARGET=tgt_dev_apis_stdc',
103 '-DTOOLCHAIN=HOST_GCC',
104 '-DSUITE=CRYPTO',
David Horstmann1d091842023-07-18 17:39:35 +0100105 '-DPSA_CRYPTO_LIB_FILENAME={}/{}'.format(mbedtls_dir,
David Horstmann7f93d222023-08-23 16:21:40 +0100106 crypto_lib_filename),
David Horstmann1d091842023-07-18 17:39:35 +0100107 ('-DPSA_INCLUDE_PATHS={}/include' + extra_includes).format(mbedtls_dir)
Bence Szépkúti34b5f562021-11-02 13:48:39 +0100108 ])
109 subprocess.check_call(['cmake', '--build', '.'])
Bence Szépkúti80b31c52021-10-19 15:05:36 +0200110
Bence Szépkúti34b5f562021-11-02 13:48:39 +0100111 proc = subprocess.Popen(['./psa-arch-tests-crypto'],
112 bufsize=1, stdout=subprocess.PIPE, universal_newlines=True)
113
114 test_re = re.compile(
115 '^TEST: (?P<test_num>[0-9]*)|'
116 '^TEST RESULT: (?P<test_result>FAILED|PASSED)'
117 )
118 test = -1
119 unexpected_successes = set(EXPECTED_FAILURES)
120 expected_failures = []
121 unexpected_failures = []
122 for line in proc.stdout:
123 print(line, end='')
124 match = test_re.match(line)
125 if match is not None:
126 groupdict = match.groupdict()
127 test_num = groupdict['test_num']
128 if test_num is not None:
129 test = int(test_num)
130 elif groupdict['test_result'] == 'FAILED':
131 try:
132 unexpected_successes.remove(test)
133 expected_failures.append(test)
134 print('Expected failure, ignoring')
135 except KeyError:
136 unexpected_failures.append(test)
137 print('ERROR: Unexpected failure')
138 elif test in unexpected_successes:
139 print('ERROR: Unexpected success')
140 proc.wait()
141
142 print()
143 print('***** test_psa_compliance.py report ******')
144 print()
145 print('Expected failures:', ', '.join(str(i) for i in expected_failures))
146 print('Unexpected failures:', ', '.join(str(i) for i in unexpected_failures))
147 print('Unexpected successes:', ', '.join(str(i) for i in sorted(unexpected_successes)))
148 print()
149 if unexpected_successes or unexpected_failures:
150 if unexpected_successes:
151 print('Unexpected successes encountered.')
152 print('Please remove the corresponding tests from '
153 'EXPECTED_FAILURES in tests/scripts/compliance_test.py')
154 print()
155 print('FAILED')
156 return 1
157 else:
Bence Szépkúti34b5f562021-11-02 13:48:39 +0100158 print('SUCCESS')
159 return 0
160 finally:
Bence Szépkúti80b31c52021-10-19 15:05:36 +0200161 os.chdir(mbedtls_dir)
Bence Szépkúti80b31c52021-10-19 15:05:36 +0200162
163if __name__ == '__main__':
David Horstmann4dcddcf2023-08-17 18:08:24 +0100164 # Default build directory
165 library_build_dir = 'mbedtls_out_of_source_build'
166
167 parser = argparse.ArgumentParser()
168 parser.add_argument('--build-dir', nargs=1,
169 help='path to Mbed TLS build directory')
170 args = parser.parse_args()
171
172 if args.build_dir is not None:
173 library_build_dir = args.build_dir[0]
174
175 sys.exit(main(library_build_dir))