blob: 225e37ceb0c8ba2871a5e8ed7143c4fc2995a552 [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
Bence Szépkúti80b31c52021-10-19 15:05:36 +020025import os
26import re
27import shutil
28import subprocess
29import sys
30
David Horstmanne31014a2023-07-19 11:43:27 +010031#pylint: disable=unused-import
David Horstmann1d091842023-07-18 17:39:35 +010032import scripts_path
33from mbedtls_dev import build_tree
34
Bence Szépkúticb288712021-11-09 21:30:43 +010035# PSA Compliance tests we expect to fail due to known defects in Mbed TLS (or the test suite)
36# The test numbers correspond to the numbers used by the console output of the test suite.
37# Test number 2xx corresponds to the files in the folder
38# psa-arch-tests/api-tests/dev_apis/crypto/test_c0xx
Bence Szépkúti80b31c52021-10-19 15:05:36 +020039EXPECTED_FAILURES = {
Bence Szépkúticb288712021-11-09 21:30:43 +010040 # psa_hash_suspend() and psa_hash_resume() are not supported.
41 # - Tracked in issue #3274
42 262, 263
Bence Szépkúti80b31c52021-10-19 15:05:36 +020043}
Bence Szépkútie2855c32021-11-09 17:33:57 +010044
45# We currently use a fork of ARM-software/psa-arch-tests, with a couple of downstream patches
46# that allow it to build with MbedTLS 3, and fixes a couple of issues in the compliance test suite.
47# These fixes allow the tests numbered 216, 248 and 249 to complete successfully.
48#
49# 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 +010050# - Tracked in issue #5145
Bence Szépkútie2855c32021-11-09 17:33:57 +010051#
52# Web URL: https://github.com/bensze01/psa-arch-tests/tree/fixes-for-mbedtls-3
53PSA_ARCH_TESTS_REPO = 'https://github.com/bensze01/psa-arch-tests.git'
Gilles Peskine42ed9632022-05-17 17:23:09 +020054PSA_ARCH_TESTS_REF = 'fix-pr-5736'
Bence Szépkúti80b31c52021-10-19 15:05:36 +020055
David Horstmanne31014a2023-07-19 11:43:27 +010056#pylint: disable=too-many-branches,too-many-statements,too-many-locals
Bence Szépkúti80b31c52021-10-19 15:05:36 +020057def main():
58 mbedtls_dir = os.getcwd()
59
David Horstmanne31014a2023-07-19 11:43:27 +010060 is_psa_crypto = build_tree.looks_like_psa_crypto_root(mbedtls_dir)
David Horstmann1d091842023-07-18 17:39:35 +010061
62 if not is_psa_crypto:
63 if not os.path.exists('library/libmbedcrypto.a'):
64 subprocess.check_call(['make', '-C', 'library', 'libmbedcrypto.a'])
Bence Szépkúti80b31c52021-10-19 15:05:36 +020065
66 psa_arch_tests_dir = 'psa-arch-tests'
Bence Szépkútic63d1602021-11-02 14:06:40 +010067 os.makedirs(psa_arch_tests_dir, exist_ok=True)
Bence Szépkúti80b31c52021-10-19 15:05:36 +020068 try:
Bence Szépkúti34b5f562021-11-02 13:48:39 +010069 os.chdir(psa_arch_tests_dir)
Bence Szépkúti80b31c52021-10-19 15:05:36 +020070
Bence Szépkútib3818412021-11-03 11:32:51 +010071 # Reuse existing local clone
Bence Szépkúti34b5f562021-11-02 13:48:39 +010072 subprocess.check_call(['git', 'init'])
73 subprocess.check_call(['git', 'fetch', PSA_ARCH_TESTS_REPO, PSA_ARCH_TESTS_REF])
74 subprocess.check_call(['git', 'checkout', 'FETCH_HEAD'])
Bence Szépkúti80b31c52021-10-19 15:05:36 +020075
Bence Szépkúti34b5f562021-11-02 13:48:39 +010076 build_dir = 'api-tests/build'
77 try:
78 shutil.rmtree(build_dir)
79 except FileNotFoundError:
80 pass
81 os.mkdir(build_dir)
82 os.chdir(build_dir)
Bence Szépkúti80b31c52021-10-19 15:05:36 +020083
David Horstmann1d091842023-07-18 17:39:35 +010084 if is_psa_crypto:
85 psa_crypto_lib_filename = \
86 'mbedtls_out_of_source_build/core/libpsacrypto.a'
87 else:
88 psa_crypto_lib_filename = 'library/libmbedcrypto.a'
89
90 extra_includes = (';{}/drivers/builtin/include'.format(mbedtls_dir)
91 if is_psa_crypto else '')
92
Bence Szépkúti34b5f562021-11-02 13:48:39 +010093 #pylint: disable=bad-continuation
94 subprocess.check_call([
95 'cmake', '..',
96 '-GUnix Makefiles',
97 '-DTARGET=tgt_dev_apis_stdc',
98 '-DTOOLCHAIN=HOST_GCC',
99 '-DSUITE=CRYPTO',
David Horstmann1d091842023-07-18 17:39:35 +0100100 '-DPSA_CRYPTO_LIB_FILENAME={}/{}'.format(mbedtls_dir,
101 psa_crypto_lib_filename),
102 ('-DPSA_INCLUDE_PATHS={}/include' + extra_includes).format(mbedtls_dir)
Bence Szépkúti34b5f562021-11-02 13:48:39 +0100103 ])
104 subprocess.check_call(['cmake', '--build', '.'])
Bence Szépkúti80b31c52021-10-19 15:05:36 +0200105
Bence Szépkúti34b5f562021-11-02 13:48:39 +0100106 proc = subprocess.Popen(['./psa-arch-tests-crypto'],
107 bufsize=1, stdout=subprocess.PIPE, universal_newlines=True)
108
109 test_re = re.compile(
110 '^TEST: (?P<test_num>[0-9]*)|'
111 '^TEST RESULT: (?P<test_result>FAILED|PASSED)'
112 )
113 test = -1
114 unexpected_successes = set(EXPECTED_FAILURES)
115 expected_failures = []
116 unexpected_failures = []
117 for line in proc.stdout:
118 print(line, end='')
119 match = test_re.match(line)
120 if match is not None:
121 groupdict = match.groupdict()
122 test_num = groupdict['test_num']
123 if test_num is not None:
124 test = int(test_num)
125 elif groupdict['test_result'] == 'FAILED':
126 try:
127 unexpected_successes.remove(test)
128 expected_failures.append(test)
129 print('Expected failure, ignoring')
130 except KeyError:
131 unexpected_failures.append(test)
132 print('ERROR: Unexpected failure')
133 elif test in unexpected_successes:
134 print('ERROR: Unexpected success')
135 proc.wait()
136
137 print()
138 print('***** test_psa_compliance.py report ******')
139 print()
140 print('Expected failures:', ', '.join(str(i) for i in expected_failures))
141 print('Unexpected failures:', ', '.join(str(i) for i in unexpected_failures))
142 print('Unexpected successes:', ', '.join(str(i) for i in sorted(unexpected_successes)))
143 print()
144 if unexpected_successes or unexpected_failures:
145 if unexpected_successes:
146 print('Unexpected successes encountered.')
147 print('Please remove the corresponding tests from '
148 'EXPECTED_FAILURES in tests/scripts/compliance_test.py')
149 print()
150 print('FAILED')
151 return 1
152 else:
Bence Szépkúti34b5f562021-11-02 13:48:39 +0100153 print('SUCCESS')
154 return 0
155 finally:
Bence Szépkúti80b31c52021-10-19 15:05:36 +0200156 os.chdir(mbedtls_dir)
Bence Szépkúti80b31c52021-10-19 15:05:36 +0200157
158if __name__ == '__main__':
Bence Szépkúti34b5f562021-11-02 13:48:39 +0100159 sys.exit(main())