blob: 58cb8f1a900dd5d676ff387f4138fd4666d36b66 [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
31EXPECTED_FAILURES = {
Bence Szépkúti355f8052021-11-09 17:33:57 +010032 221, 224, 225, 252, 253, 254, 255, 256, 257, 258, 259, 261, 262, 263
Bence Szépkúti9f849112021-10-19 15:05:36 +020033}
Bence Szépkúti355f8052021-11-09 17:33:57 +010034
35# We currently use a fork of ARM-software/psa-arch-tests, with a couple of downstream patches
36# that allow it to build with MbedTLS 3, and fixes a couple of issues in the compliance test suite.
37# These fixes allow the tests numbered 216, 248 and 249 to complete successfully.
38#
39# Once all the fixes are upstreamed, this fork should be replaced with an upstream commit/tag.
40#
41# Web URL: https://github.com/bensze01/psa-arch-tests/tree/fixes-for-mbedtls-3
42PSA_ARCH_TESTS_REPO = 'https://github.com/bensze01/psa-arch-tests.git'
43PSA_ARCH_TESTS_REF = 'fixes-for-mbedtls-3'
Bence Szépkúti9f849112021-10-19 15:05:36 +020044
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010045#pylint: disable=too-many-branches,too-many-statements
Bence Szépkúti9f849112021-10-19 15:05:36 +020046def main():
47 mbedtls_dir = os.getcwd()
48
Bence Szépkútiab796e62021-10-25 19:29:07 +020049 if not os.path.exists('library/libmbedcrypto.a'):
50 subprocess.check_call(['make', '-C', 'library', 'libmbedcrypto.a'])
Bence Szépkúti9f849112021-10-19 15:05:36 +020051
52 psa_arch_tests_dir = 'psa-arch-tests'
Bence Szépkútieda2fb92021-11-02 14:06:40 +010053 os.makedirs(psa_arch_tests_dir, exist_ok=True)
Bence Szépkúti9f849112021-10-19 15:05:36 +020054 try:
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010055 os.chdir(psa_arch_tests_dir)
Bence Szépkúti9f849112021-10-19 15:05:36 +020056
Bence Szépkútibd66d182021-11-03 11:32:51 +010057 # Reuse existing local clone
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010058 subprocess.check_call(['git', 'init'])
59 subprocess.check_call(['git', 'fetch', PSA_ARCH_TESTS_REPO, PSA_ARCH_TESTS_REF])
60 subprocess.check_call(['git', 'checkout', 'FETCH_HEAD'])
Bence Szépkúti9f849112021-10-19 15:05:36 +020061
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010062 build_dir = 'api-tests/build'
63 try:
64 shutil.rmtree(build_dir)
65 except FileNotFoundError:
66 pass
67 os.mkdir(build_dir)
68 os.chdir(build_dir)
Bence Szépkúti9f849112021-10-19 15:05:36 +020069
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010070 #pylint: disable=bad-continuation
71 subprocess.check_call([
72 'cmake', '..',
73 '-GUnix Makefiles',
74 '-DTARGET=tgt_dev_apis_stdc',
75 '-DTOOLCHAIN=HOST_GCC',
76 '-DSUITE=CRYPTO',
77 '-DPSA_CRYPTO_LIB_FILENAME={}/library/libmbedcrypto.a'.format(mbedtls_dir),
78 '-DPSA_INCLUDE_PATHS={}/include'.format(mbedtls_dir)
79 ])
80 subprocess.check_call(['cmake', '--build', '.'])
Bence Szépkúti9f849112021-10-19 15:05:36 +020081
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010082 proc = subprocess.Popen(['./psa-arch-tests-crypto'],
83 bufsize=1, stdout=subprocess.PIPE, universal_newlines=True)
84
85 test_re = re.compile(
86 '^TEST: (?P<test_num>[0-9]*)|'
87 '^TEST RESULT: (?P<test_result>FAILED|PASSED)'
88 )
89 test = -1
90 unexpected_successes = set(EXPECTED_FAILURES)
91 expected_failures = []
92 unexpected_failures = []
93 for line in proc.stdout:
94 print(line, end='')
95 match = test_re.match(line)
96 if match is not None:
97 groupdict = match.groupdict()
98 test_num = groupdict['test_num']
99 if test_num is not None:
100 test = int(test_num)
101 elif groupdict['test_result'] == 'FAILED':
102 try:
103 unexpected_successes.remove(test)
104 expected_failures.append(test)
105 print('Expected failure, ignoring')
106 except KeyError:
107 unexpected_failures.append(test)
108 print('ERROR: Unexpected failure')
109 elif test in unexpected_successes:
110 print('ERROR: Unexpected success')
111 proc.wait()
112
113 print()
114 print('***** test_psa_compliance.py report ******')
115 print()
116 print('Expected failures:', ', '.join(str(i) for i in expected_failures))
117 print('Unexpected failures:', ', '.join(str(i) for i in unexpected_failures))
118 print('Unexpected successes:', ', '.join(str(i) for i in sorted(unexpected_successes)))
119 print()
120 if unexpected_successes or unexpected_failures:
121 if unexpected_successes:
122 print('Unexpected successes encountered.')
123 print('Please remove the corresponding tests from '
124 'EXPECTED_FAILURES in tests/scripts/compliance_test.py')
125 print()
126 print('FAILED')
127 return 1
128 else:
Bence Szépkúti559f1ce2021-11-02 13:48:39 +0100129 print('SUCCESS')
130 return 0
131 finally:
Bence Szépkúti9f849112021-10-19 15:05:36 +0200132 os.chdir(mbedtls_dir)
Bence Szépkúti9f849112021-10-19 15:05:36 +0200133
134if __name__ == '__main__':
Bence Szépkúti559f1ce2021-11-02 13:48:39 +0100135 sys.exit(main())