blob: 47caa05dada1c0778e903ceeade6c545378c7f83 [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,
David Horstmann3b8984a2023-08-29 10:32:26 +01004then compile and run the test suite. The clone is stored at <repository root>/psa-arch-tests.
5Known defects in either the test suite or mbedtls / psa-crypto - identified by their test
6number - are ignored, while unexpected failures AND successes are reported as errors, to help
7keep the list of known defects as up to date as possible.
Bence Szépkúti449781f2021-11-02 13:41:14 +01008"""
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
David Horstmann9cc6b2f2023-08-29 17:36:35 +010031from typing import List
Bence Szépkúti80b31c52021-10-19 15:05:36 +020032
David Horstmanne31014a2023-07-19 11:43:27 +010033#pylint: disable=unused-import
David Horstmann1d091842023-07-18 17:39:35 +010034import scripts_path
35from mbedtls_dev import build_tree
36
David Horstmann3b8984a2023-08-29 10:32:26 +010037# PSA Compliance tests we expect to fail due to known defects in Mbed TLS / PSA Crypto
38# (or the test suite).
Bence Szépkúticb288712021-11-09 21:30:43 +010039# The test numbers correspond to the numbers used by the console output of the test suite.
40# Test number 2xx corresponds to the files in the folder
41# psa-arch-tests/api-tests/dev_apis/crypto/test_c0xx
Bence Szépkúti80b31c52021-10-19 15:05:36 +020042EXPECTED_FAILURES = {
Bence Szépkúticb288712021-11-09 21:30:43 +010043 # psa_hash_suspend() and psa_hash_resume() are not supported.
44 # - Tracked in issue #3274
45 262, 263
Bence Szépkúti80b31c52021-10-19 15:05:36 +020046}
Bence Szépkútie2855c32021-11-09 17:33:57 +010047
48# We currently use a fork of ARM-software/psa-arch-tests, with a couple of downstream patches
49# that allow it to build with MbedTLS 3, and fixes a couple of issues in the compliance test suite.
50# These fixes allow the tests numbered 216, 248 and 249 to complete successfully.
51#
52# 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 +010053# - Tracked in issue #5145
Bence Szépkútie2855c32021-11-09 17:33:57 +010054#
55# Web URL: https://github.com/bensze01/psa-arch-tests/tree/fixes-for-mbedtls-3
56PSA_ARCH_TESTS_REPO = 'https://github.com/bensze01/psa-arch-tests.git'
Gilles Peskine42ed9632022-05-17 17:23:09 +020057PSA_ARCH_TESTS_REF = 'fix-pr-5736'
Bence Szépkúti80b31c52021-10-19 15:05:36 +020058
David Horstmanne31014a2023-07-19 11:43:27 +010059#pylint: disable=too-many-branches,too-many-statements,too-many-locals
David Horstmann4dcddcf2023-08-17 18:08:24 +010060def main(library_build_dir: str):
David Horstmannf7570692023-08-29 10:27:13 +010061 root_dir = os.getcwd()
Bence Szépkúti80b31c52021-10-19 15:05:36 +020062
David Horstmannf7570692023-08-29 10:27:13 +010063 in_psa_crypto_repo = build_tree.looks_like_psa_crypto_root(root_dir)
David Horstmann1d091842023-07-18 17:39:35 +010064
David Horstmann98af1982023-08-29 10:25:26 +010065 if in_psa_crypto_repo:
David Horstmannbeaee262023-08-29 13:56:17 +010066 crypto_name = 'psacrypto'
67 library_subdir = 'core'
David Horstmann98af1982023-08-29 10:25:26 +010068 else:
David Horstmannbeaee262023-08-29 13:56:17 +010069 crypto_name = 'mbedcrypto'
70 library_subdir = 'library'
71
72 crypto_lib_filename = (library_build_dir + '/' +
73 library_subdir + '/' +
74 'lib' + crypto_name + '.a')
David Horstmann98af1982023-08-29 10:25:26 +010075
76 if not os.path.exists(crypto_lib_filename):
David Horstmann2ba89be2023-08-29 10:37:29 +010077 #pylint: disable=bad-continuation
David Horstmann4dcddcf2023-08-17 18:08:24 +010078 subprocess.check_call([
79 'cmake', '.',
80 '-GUnix Makefiles',
David Horstmann41c316d2023-08-29 14:57:23 +010081 '-B' + library_build_dir
David Horstmann4dcddcf2023-08-17 18:08:24 +010082 ])
David Horstmannbeaee262023-08-29 13:56:17 +010083 subprocess.check_call(['cmake', '--build', library_build_dir,
84 '-t', crypto_name])
Bence Szépkúti80b31c52021-10-19 15:05:36 +020085
86 psa_arch_tests_dir = 'psa-arch-tests'
Bence Szépkútic63d1602021-11-02 14:06:40 +010087 os.makedirs(psa_arch_tests_dir, exist_ok=True)
Bence Szépkúti80b31c52021-10-19 15:05:36 +020088 try:
Bence Szépkúti34b5f562021-11-02 13:48:39 +010089 os.chdir(psa_arch_tests_dir)
Bence Szépkúti80b31c52021-10-19 15:05:36 +020090
Bence Szépkútib3818412021-11-03 11:32:51 +010091 # Reuse existing local clone
Bence Szépkúti34b5f562021-11-02 13:48:39 +010092 subprocess.check_call(['git', 'init'])
93 subprocess.check_call(['git', 'fetch', PSA_ARCH_TESTS_REPO, PSA_ARCH_TESTS_REF])
94 subprocess.check_call(['git', 'checkout', 'FETCH_HEAD'])
Bence Szépkúti80b31c52021-10-19 15:05:36 +020095
Bence Szépkúti34b5f562021-11-02 13:48:39 +010096 build_dir = 'api-tests/build'
97 try:
98 shutil.rmtree(build_dir)
99 except FileNotFoundError:
100 pass
101 os.mkdir(build_dir)
102 os.chdir(build_dir)
Bence Szépkúti80b31c52021-10-19 15:05:36 +0200103
David Horstmannf7570692023-08-29 10:27:13 +0100104 extra_includes = (';{}/drivers/builtin/include'.format(root_dir)
David Horstmann0ac57ca2023-08-23 16:24:55 +0100105 if in_psa_crypto_repo else '')
David Horstmann1d091842023-07-18 17:39:35 +0100106
Bence Szépkúti34b5f562021-11-02 13:48:39 +0100107 #pylint: disable=bad-continuation
108 subprocess.check_call([
109 'cmake', '..',
110 '-GUnix Makefiles',
111 '-DTARGET=tgt_dev_apis_stdc',
112 '-DTOOLCHAIN=HOST_GCC',
113 '-DSUITE=CRYPTO',
David Horstmannf7570692023-08-29 10:27:13 +0100114 '-DPSA_CRYPTO_LIB_FILENAME={}/{}'.format(root_dir,
David Horstmann7f93d222023-08-23 16:21:40 +0100115 crypto_lib_filename),
David Horstmannf7570692023-08-29 10:27:13 +0100116 ('-DPSA_INCLUDE_PATHS={}/include' + extra_includes).format(root_dir)
Bence Szépkúti34b5f562021-11-02 13:48:39 +0100117 ])
118 subprocess.check_call(['cmake', '--build', '.'])
Bence Szépkúti80b31c52021-10-19 15:05:36 +0200119
Bence Szépkúti34b5f562021-11-02 13:48:39 +0100120 proc = subprocess.Popen(['./psa-arch-tests-crypto'],
121 bufsize=1, stdout=subprocess.PIPE, universal_newlines=True)
122
123 test_re = re.compile(
124 '^TEST: (?P<test_num>[0-9]*)|'
125 '^TEST RESULT: (?P<test_result>FAILED|PASSED)'
126 )
127 test = -1
128 unexpected_successes = set(EXPECTED_FAILURES)
David Horstmannfd9264e2023-08-29 16:21:15 +0100129 expected_failures = [] # type: List[int]
130 unexpected_failures = [] # type: List[int]
131 if proc.stdout is None:
132 return 1
133
Bence Szépkúti34b5f562021-11-02 13:48:39 +0100134 for line in proc.stdout:
135 print(line, end='')
136 match = test_re.match(line)
137 if match is not None:
138 groupdict = match.groupdict()
139 test_num = groupdict['test_num']
140 if test_num is not None:
141 test = int(test_num)
142 elif groupdict['test_result'] == 'FAILED':
143 try:
144 unexpected_successes.remove(test)
145 expected_failures.append(test)
146 print('Expected failure, ignoring')
147 except KeyError:
148 unexpected_failures.append(test)
149 print('ERROR: Unexpected failure')
150 elif test in unexpected_successes:
151 print('ERROR: Unexpected success')
152 proc.wait()
153
154 print()
155 print('***** test_psa_compliance.py report ******')
156 print()
157 print('Expected failures:', ', '.join(str(i) for i in expected_failures))
158 print('Unexpected failures:', ', '.join(str(i) for i in unexpected_failures))
159 print('Unexpected successes:', ', '.join(str(i) for i in sorted(unexpected_successes)))
160 print()
161 if unexpected_successes or unexpected_failures:
162 if unexpected_successes:
163 print('Unexpected successes encountered.')
164 print('Please remove the corresponding tests from '
165 'EXPECTED_FAILURES in tests/scripts/compliance_test.py')
166 print()
167 print('FAILED')
168 return 1
169 else:
Bence Szépkúti34b5f562021-11-02 13:48:39 +0100170 print('SUCCESS')
171 return 0
172 finally:
David Horstmannf7570692023-08-29 10:27:13 +0100173 os.chdir(root_dir)
Bence Szépkúti80b31c52021-10-19 15:05:36 +0200174
175if __name__ == '__main__':
David Horstmannb48822c2023-08-29 14:12:53 +0100176 BUILD_DIR = 'out_of_source_build'
David Horstmann4dcddcf2023-08-17 18:08:24 +0100177
David Horstmann3ed18712023-08-29 18:20:01 +0100178 # pylint: disable=invalid-name
David Horstmann4dcddcf2023-08-17 18:08:24 +0100179 parser = argparse.ArgumentParser()
180 parser.add_argument('--build-dir', nargs=1,
David Horstmann3b8984a2023-08-29 10:32:26 +0100181 help='path to Mbed TLS / PSA Crypto build directory')
David Horstmann4dcddcf2023-08-17 18:08:24 +0100182 args = parser.parse_args()
183
184 if args.build_dir is not None:
David Horstmannb48822c2023-08-29 14:12:53 +0100185 BUILD_DIR = args.build_dir[0]
David Horstmann4dcddcf2023-08-17 18:08:24 +0100186
David Horstmannb48822c2023-08-29 14:12:53 +0100187 sys.exit(main(BUILD_DIR))