blob: 31e3fce774bf215e851f1c3c7d47f638d182cc4a [file] [log] [blame]
Bence Szépkúti9f849112021-10-19 15:05:36 +02001#!/usr/bin/env python3
Bence Szépkúti19a124d2021-11-02 13:41:14 +01002"""Run the PSA Cryto API compliance test suite.
3Clone the repo and check out the commit specified by PSA_ARCH_TEST_REPO and PSA_ARCH_TEST_REF,
Bence Szépkútibd66d182021-11-03 11:32:51 +01004then complie and run the test suite. The clone is stored at <Mbed TLS root>/psa-arch-tests.
Bence Szépkúti19a124d2021-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útic2ca1352021-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úti9f849112021-10-19 15:05:36 +020025import os
26import re
27import shutil
28import subprocess
29import sys
30
Bence Szépkúti7ccbea62021-11-09 21:30:43 +010031# PSA Compliance tests we expect to fail due to known defects in Mbed TLS (or the test suite)
32# The test numbers correspond to the numbers used by the console output of the test suite.
33# Test number 2xx corresponds to the files in the folder
34# psa-arch-tests/api-tests/dev_apis/crypto/test_c0xx
Bence Szépkúti9f849112021-10-19 15:05:36 +020035EXPECTED_FAILURES = {
Bence Szépkúti7ccbea62021-11-09 21:30:43 +010036 # psa_key_derivation_output_key() returns PSA_ERROR_NOT_PERMITTED instead of
37 # PSA_ERROR_BAD_STATE when called after the operation was aborted.
38 # - Tracked in issue #5143
39 221,
40
41 # psa_aead_[encrypt/decrypt]() returns PSA_ERROR_NOT_SUPPORTED instead of
42 # PSA_ERROR_INVALID_ARGUMENT when called with an invalid nonce.
43 # - Tracked in issue #5144
44 224, 225,
45
46 # Multipart CCM is not supported.
47 # - Tracked in issue #3721
48 252, 253, 254, 255, 256, 257, 258, 259, 261,
49
50 # psa_hash_suspend() and psa_hash_resume() are not supported.
51 # - Tracked in issue #3274
52 262, 263
Bence Szépkúti9f849112021-10-19 15:05:36 +020053}
Bence Szépkúti355f8052021-11-09 17:33:57 +010054
55# We currently use a fork of ARM-software/psa-arch-tests, with a couple of downstream patches
56# that allow it to build with MbedTLS 3, and fixes a couple of issues in the compliance test suite.
57# These fixes allow the tests numbered 216, 248 and 249 to complete successfully.
58#
59# Once all the fixes are upstreamed, this fork should be replaced with an upstream commit/tag.
60#
61# Web URL: https://github.com/bensze01/psa-arch-tests/tree/fixes-for-mbedtls-3
62PSA_ARCH_TESTS_REPO = 'https://github.com/bensze01/psa-arch-tests.git'
63PSA_ARCH_TESTS_REF = 'fixes-for-mbedtls-3'
Bence Szépkúti9f849112021-10-19 15:05:36 +020064
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010065#pylint: disable=too-many-branches,too-many-statements
Bence Szépkúti9f849112021-10-19 15:05:36 +020066def main():
67 mbedtls_dir = os.getcwd()
68
Bence Szépkútiab796e62021-10-25 19:29:07 +020069 if not os.path.exists('library/libmbedcrypto.a'):
70 subprocess.check_call(['make', '-C', 'library', 'libmbedcrypto.a'])
Bence Szépkúti9f849112021-10-19 15:05:36 +020071
72 psa_arch_tests_dir = 'psa-arch-tests'
Bence Szépkútieda2fb92021-11-02 14:06:40 +010073 os.makedirs(psa_arch_tests_dir, exist_ok=True)
Bence Szépkúti9f849112021-10-19 15:05:36 +020074 try:
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010075 os.chdir(psa_arch_tests_dir)
Bence Szépkúti9f849112021-10-19 15:05:36 +020076
Bence Szépkútibd66d182021-11-03 11:32:51 +010077 # Reuse existing local clone
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010078 subprocess.check_call(['git', 'init'])
79 subprocess.check_call(['git', 'fetch', PSA_ARCH_TESTS_REPO, PSA_ARCH_TESTS_REF])
80 subprocess.check_call(['git', 'checkout', 'FETCH_HEAD'])
Bence Szépkúti9f849112021-10-19 15:05:36 +020081
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010082 build_dir = 'api-tests/build'
83 try:
84 shutil.rmtree(build_dir)
85 except FileNotFoundError:
86 pass
87 os.mkdir(build_dir)
88 os.chdir(build_dir)
Bence Szépkúti9f849112021-10-19 15:05:36 +020089
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010090 #pylint: disable=bad-continuation
91 subprocess.check_call([
92 'cmake', '..',
93 '-GUnix Makefiles',
94 '-DTARGET=tgt_dev_apis_stdc',
95 '-DTOOLCHAIN=HOST_GCC',
96 '-DSUITE=CRYPTO',
97 '-DPSA_CRYPTO_LIB_FILENAME={}/library/libmbedcrypto.a'.format(mbedtls_dir),
98 '-DPSA_INCLUDE_PATHS={}/include'.format(mbedtls_dir)
99 ])
100 subprocess.check_call(['cmake', '--build', '.'])
Bence Szépkúti9f849112021-10-19 15:05:36 +0200101
Bence Szépkúti559f1ce2021-11-02 13:48:39 +0100102 proc = subprocess.Popen(['./psa-arch-tests-crypto'],
103 bufsize=1, stdout=subprocess.PIPE, universal_newlines=True)
104
105 test_re = re.compile(
106 '^TEST: (?P<test_num>[0-9]*)|'
107 '^TEST RESULT: (?P<test_result>FAILED|PASSED)'
108 )
109 test = -1
110 unexpected_successes = set(EXPECTED_FAILURES)
111 expected_failures = []
112 unexpected_failures = []
113 for line in proc.stdout:
114 print(line, end='')
115 match = test_re.match(line)
116 if match is not None:
117 groupdict = match.groupdict()
118 test_num = groupdict['test_num']
119 if test_num is not None:
120 test = int(test_num)
121 elif groupdict['test_result'] == 'FAILED':
122 try:
123 unexpected_successes.remove(test)
124 expected_failures.append(test)
125 print('Expected failure, ignoring')
126 except KeyError:
127 unexpected_failures.append(test)
128 print('ERROR: Unexpected failure')
129 elif test in unexpected_successes:
130 print('ERROR: Unexpected success')
131 proc.wait()
132
133 print()
134 print('***** test_psa_compliance.py report ******')
135 print()
136 print('Expected failures:', ', '.join(str(i) for i in expected_failures))
137 print('Unexpected failures:', ', '.join(str(i) for i in unexpected_failures))
138 print('Unexpected successes:', ', '.join(str(i) for i in sorted(unexpected_successes)))
139 print()
140 if unexpected_successes or unexpected_failures:
141 if unexpected_successes:
142 print('Unexpected successes encountered.')
143 print('Please remove the corresponding tests from '
144 'EXPECTED_FAILURES in tests/scripts/compliance_test.py')
145 print()
146 print('FAILED')
147 return 1
148 else:
Bence Szépkúti559f1ce2021-11-02 13:48:39 +0100149 print('SUCCESS')
150 return 0
151 finally:
Bence Szépkúti9f849112021-10-19 15:05:36 +0200152 os.chdir(mbedtls_dir)
Bence Szépkúti9f849112021-10-19 15:05:36 +0200153
154if __name__ == '__main__':
Bence Szépkúti559f1ce2021-11-02 13:48:39 +0100155 sys.exit(main())