blob: d94f6c242229b9b1e26ce0b97c32dca623bc562f [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,
4then complie and run the test suite.
5Known 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 = {
32 216, 221, 224, 225, 248, 249, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263
33}
34PSA_ARCH_TESTS_REPO = 'https://github.com/ronald-cron-arm/psa-arch-tests.git'
35PSA_ARCH_TESTS_REF = 'crypto1.0-3.0'
36
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010037#pylint: disable=too-many-branches,too-many-statements
Bence Szépkúti9f849112021-10-19 15:05:36 +020038def main():
39 mbedtls_dir = os.getcwd()
40
Bence Szépkútiab796e62021-10-25 19:29:07 +020041 if not os.path.exists('library/libmbedcrypto.a'):
42 subprocess.check_call(['make', '-C', 'library', 'libmbedcrypto.a'])
Bence Szépkúti9f849112021-10-19 15:05:36 +020043
44 psa_arch_tests_dir = 'psa-arch-tests'
45 try:
46 os.mkdir(psa_arch_tests_dir)
47 except FileExistsError:
48 pass
Bence Szépkúti9f849112021-10-19 15:05:36 +020049 try:
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010050 os.chdir(psa_arch_tests_dir)
Bence Szépkúti9f849112021-10-19 15:05:36 +020051
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010052 subprocess.check_call(['git', 'init'])
53 subprocess.check_call(['git', 'fetch', PSA_ARCH_TESTS_REPO, PSA_ARCH_TESTS_REF])
54 subprocess.check_call(['git', 'checkout', 'FETCH_HEAD'])
Bence Szépkúti9f849112021-10-19 15:05:36 +020055
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010056 build_dir = 'api-tests/build'
57 try:
58 shutil.rmtree(build_dir)
59 except FileNotFoundError:
60 pass
61 os.mkdir(build_dir)
62 os.chdir(build_dir)
Bence Szépkúti9f849112021-10-19 15:05:36 +020063
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010064 #pylint: disable=bad-continuation
65 subprocess.check_call([
66 'cmake', '..',
67 '-GUnix Makefiles',
68 '-DTARGET=tgt_dev_apis_stdc',
69 '-DTOOLCHAIN=HOST_GCC',
70 '-DSUITE=CRYPTO',
71 '-DPSA_CRYPTO_LIB_FILENAME={}/library/libmbedcrypto.a'.format(mbedtls_dir),
72 '-DPSA_INCLUDE_PATHS={}/include'.format(mbedtls_dir)
73 ])
74 subprocess.check_call(['cmake', '--build', '.'])
Bence Szépkúti9f849112021-10-19 15:05:36 +020075
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010076 proc = subprocess.Popen(['./psa-arch-tests-crypto'],
77 bufsize=1, stdout=subprocess.PIPE, universal_newlines=True)
78
79 test_re = re.compile(
80 '^TEST: (?P<test_num>[0-9]*)|'
81 '^TEST RESULT: (?P<test_result>FAILED|PASSED)'
82 )
83 test = -1
84 unexpected_successes = set(EXPECTED_FAILURES)
85 expected_failures = []
86 unexpected_failures = []
87 for line in proc.stdout:
88 print(line, end='')
89 match = test_re.match(line)
90 if match is not None:
91 groupdict = match.groupdict()
92 test_num = groupdict['test_num']
93 if test_num is not None:
94 test = int(test_num)
95 elif groupdict['test_result'] == 'FAILED':
96 try:
97 unexpected_successes.remove(test)
98 expected_failures.append(test)
99 print('Expected failure, ignoring')
100 except KeyError:
101 unexpected_failures.append(test)
102 print('ERROR: Unexpected failure')
103 elif test in unexpected_successes:
104 print('ERROR: Unexpected success')
105 proc.wait()
106
107 print()
108 print('***** test_psa_compliance.py report ******')
109 print()
110 print('Expected failures:', ', '.join(str(i) for i in expected_failures))
111 print('Unexpected failures:', ', '.join(str(i) for i in unexpected_failures))
112 print('Unexpected successes:', ', '.join(str(i) for i in sorted(unexpected_successes)))
113 print()
114 if unexpected_successes or unexpected_failures:
115 if unexpected_successes:
116 print('Unexpected successes encountered.')
117 print('Please remove the corresponding tests from '
118 'EXPECTED_FAILURES in tests/scripts/compliance_test.py')
119 print()
120 print('FAILED')
121 return 1
122 else:
123 shutil.rmtree(psa_arch_tests_dir)
124 print('SUCCESS')
125 return 0
126 finally:
Bence Szépkúti9f849112021-10-19 15:05:36 +0200127 os.chdir(mbedtls_dir)
Bence Szépkúti9f849112021-10-19 15:05:36 +0200128
129if __name__ == '__main__':
Bence Szépkúti559f1ce2021-11-02 13:48:39 +0100130 sys.exit(main())