blob: 11b0c6a438e076819224e75f016eb4f384fdbc65 [file] [log] [blame]
Bence Szépkúti80b31c52021-10-19 15:05:36 +02001#!/usr/bin/env python3
Bence Szépkúti449781f2021-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útib3818412021-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ú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
Bence Szépkúticb288712021-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úti80b31c52021-10-19 15:05:36 +020035EXPECTED_FAILURES = {
Bence Szépkúticb288712021-11-09 21:30:43 +010036 # psa_aead_[encrypt/decrypt]() returns PSA_ERROR_NOT_SUPPORTED instead of
37 # PSA_ERROR_INVALID_ARGUMENT when called with an invalid nonce.
38 # - Tracked in issue #5144
39 224, 225,
40
41 # Multipart CCM is not supported.
42 # - Tracked in issue #3721
43 252, 253, 254, 255, 256, 257, 258, 259, 261,
44
45 # psa_hash_suspend() and psa_hash_resume() are not supported.
46 # - Tracked in issue #3274
47 262, 263
Bence Szépkúti80b31c52021-10-19 15:05:36 +020048}
Bence Szépkútie2855c32021-11-09 17:33:57 +010049
50# We currently use a fork of ARM-software/psa-arch-tests, with a couple of downstream patches
51# that allow it to build with MbedTLS 3, and fixes a couple of issues in the compliance test suite.
52# These fixes allow the tests numbered 216, 248 and 249 to complete successfully.
53#
54# 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 +010055# - Tracked in issue #5145
Bence Szépkútie2855c32021-11-09 17:33:57 +010056#
57# Web URL: https://github.com/bensze01/psa-arch-tests/tree/fixes-for-mbedtls-3
58PSA_ARCH_TESTS_REPO = 'https://github.com/bensze01/psa-arch-tests.git'
59PSA_ARCH_TESTS_REF = 'fixes-for-mbedtls-3'
Bence Szépkúti80b31c52021-10-19 15:05:36 +020060
Bence Szépkúti34b5f562021-11-02 13:48:39 +010061#pylint: disable=too-many-branches,too-many-statements
Bence Szépkúti80b31c52021-10-19 15:05:36 +020062def main():
63 mbedtls_dir = os.getcwd()
64
Bence Szépkútica9236b2021-10-25 19:29:07 +020065 if not os.path.exists('library/libmbedcrypto.a'):
66 subprocess.check_call(['make', '-C', 'library', 'libmbedcrypto.a'])
Bence Szépkúti80b31c52021-10-19 15:05:36 +020067
68 psa_arch_tests_dir = 'psa-arch-tests'
Bence Szépkútic63d1602021-11-02 14:06:40 +010069 os.makedirs(psa_arch_tests_dir, exist_ok=True)
Bence Szépkúti80b31c52021-10-19 15:05:36 +020070 try:
Bence Szépkúti34b5f562021-11-02 13:48:39 +010071 os.chdir(psa_arch_tests_dir)
Bence Szépkúti80b31c52021-10-19 15:05:36 +020072
Bence Szépkútib3818412021-11-03 11:32:51 +010073 # Reuse existing local clone
Bence Szépkúti34b5f562021-11-02 13:48:39 +010074 subprocess.check_call(['git', 'init'])
75 subprocess.check_call(['git', 'fetch', PSA_ARCH_TESTS_REPO, PSA_ARCH_TESTS_REF])
76 subprocess.check_call(['git', 'checkout', 'FETCH_HEAD'])
Bence Szépkúti80b31c52021-10-19 15:05:36 +020077
Bence Szépkúti34b5f562021-11-02 13:48:39 +010078 build_dir = 'api-tests/build'
79 try:
80 shutil.rmtree(build_dir)
81 except FileNotFoundError:
82 pass
83 os.mkdir(build_dir)
84 os.chdir(build_dir)
Bence Szépkúti80b31c52021-10-19 15:05:36 +020085
Bence Szépkúti34b5f562021-11-02 13:48:39 +010086 #pylint: disable=bad-continuation
87 subprocess.check_call([
88 'cmake', '..',
89 '-GUnix Makefiles',
90 '-DTARGET=tgt_dev_apis_stdc',
91 '-DTOOLCHAIN=HOST_GCC',
92 '-DSUITE=CRYPTO',
93 '-DPSA_CRYPTO_LIB_FILENAME={}/library/libmbedcrypto.a'.format(mbedtls_dir),
94 '-DPSA_INCLUDE_PATHS={}/include'.format(mbedtls_dir)
95 ])
96 subprocess.check_call(['cmake', '--build', '.'])
Bence Szépkúti80b31c52021-10-19 15:05:36 +020097
Bence Szépkúti34b5f562021-11-02 13:48:39 +010098 proc = subprocess.Popen(['./psa-arch-tests-crypto'],
99 bufsize=1, stdout=subprocess.PIPE, universal_newlines=True)
100
101 test_re = re.compile(
102 '^TEST: (?P<test_num>[0-9]*)|'
103 '^TEST RESULT: (?P<test_result>FAILED|PASSED)'
104 )
105 test = -1
106 unexpected_successes = set(EXPECTED_FAILURES)
107 expected_failures = []
108 unexpected_failures = []
109 for line in proc.stdout:
110 print(line, end='')
111 match = test_re.match(line)
112 if match is not None:
113 groupdict = match.groupdict()
114 test_num = groupdict['test_num']
115 if test_num is not None:
116 test = int(test_num)
117 elif groupdict['test_result'] == 'FAILED':
118 try:
119 unexpected_successes.remove(test)
120 expected_failures.append(test)
121 print('Expected failure, ignoring')
122 except KeyError:
123 unexpected_failures.append(test)
124 print('ERROR: Unexpected failure')
125 elif test in unexpected_successes:
126 print('ERROR: Unexpected success')
127 proc.wait()
128
129 print()
130 print('***** test_psa_compliance.py report ******')
131 print()
132 print('Expected failures:', ', '.join(str(i) for i in expected_failures))
133 print('Unexpected failures:', ', '.join(str(i) for i in unexpected_failures))
134 print('Unexpected successes:', ', '.join(str(i) for i in sorted(unexpected_successes)))
135 print()
136 if unexpected_successes or unexpected_failures:
137 if unexpected_successes:
138 print('Unexpected successes encountered.')
139 print('Please remove the corresponding tests from '
140 'EXPECTED_FAILURES in tests/scripts/compliance_test.py')
141 print()
142 print('FAILED')
143 return 1
144 else:
Bence Szépkúti34b5f562021-11-02 13:48:39 +0100145 print('SUCCESS')
146 return 0
147 finally:
Bence Szépkúti80b31c52021-10-19 15:05:36 +0200148 os.chdir(mbedtls_dir)
Bence Szépkúti80b31c52021-10-19 15:05:36 +0200149
150if __name__ == '__main__':
Bence Szépkúti34b5f562021-11-02 13:48:39 +0100151 sys.exit(main())