blob: 33207c014b88d5e9f74085a4ab599faf99cf4ba9 [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'
Bence Szépkútieda2fb92021-11-02 14:06:40 +010045 os.makedirs(psa_arch_tests_dir, exist_ok=True)
Bence Szépkúti9f849112021-10-19 15:05:36 +020046 try:
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010047 os.chdir(psa_arch_tests_dir)
Bence Szépkúti9f849112021-10-19 15:05:36 +020048
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010049 subprocess.check_call(['git', 'init'])
50 subprocess.check_call(['git', 'fetch', PSA_ARCH_TESTS_REPO, PSA_ARCH_TESTS_REF])
51 subprocess.check_call(['git', 'checkout', 'FETCH_HEAD'])
Bence Szépkúti9f849112021-10-19 15:05:36 +020052
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010053 build_dir = 'api-tests/build'
54 try:
55 shutil.rmtree(build_dir)
56 except FileNotFoundError:
57 pass
58 os.mkdir(build_dir)
59 os.chdir(build_dir)
Bence Szépkúti9f849112021-10-19 15:05:36 +020060
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010061 #pylint: disable=bad-continuation
62 subprocess.check_call([
63 'cmake', '..',
64 '-GUnix Makefiles',
65 '-DTARGET=tgt_dev_apis_stdc',
66 '-DTOOLCHAIN=HOST_GCC',
67 '-DSUITE=CRYPTO',
68 '-DPSA_CRYPTO_LIB_FILENAME={}/library/libmbedcrypto.a'.format(mbedtls_dir),
69 '-DPSA_INCLUDE_PATHS={}/include'.format(mbedtls_dir)
70 ])
71 subprocess.check_call(['cmake', '--build', '.'])
Bence Szépkúti9f849112021-10-19 15:05:36 +020072
Bence Szépkúti559f1ce2021-11-02 13:48:39 +010073 proc = subprocess.Popen(['./psa-arch-tests-crypto'],
74 bufsize=1, stdout=subprocess.PIPE, universal_newlines=True)
75
76 test_re = re.compile(
77 '^TEST: (?P<test_num>[0-9]*)|'
78 '^TEST RESULT: (?P<test_result>FAILED|PASSED)'
79 )
80 test = -1
81 unexpected_successes = set(EXPECTED_FAILURES)
82 expected_failures = []
83 unexpected_failures = []
84 for line in proc.stdout:
85 print(line, end='')
86 match = test_re.match(line)
87 if match is not None:
88 groupdict = match.groupdict()
89 test_num = groupdict['test_num']
90 if test_num is not None:
91 test = int(test_num)
92 elif groupdict['test_result'] == 'FAILED':
93 try:
94 unexpected_successes.remove(test)
95 expected_failures.append(test)
96 print('Expected failure, ignoring')
97 except KeyError:
98 unexpected_failures.append(test)
99 print('ERROR: Unexpected failure')
100 elif test in unexpected_successes:
101 print('ERROR: Unexpected success')
102 proc.wait()
103
104 print()
105 print('***** test_psa_compliance.py report ******')
106 print()
107 print('Expected failures:', ', '.join(str(i) for i in expected_failures))
108 print('Unexpected failures:', ', '.join(str(i) for i in unexpected_failures))
109 print('Unexpected successes:', ', '.join(str(i) for i in sorted(unexpected_successes)))
110 print()
111 if unexpected_successes or unexpected_failures:
112 if unexpected_successes:
113 print('Unexpected successes encountered.')
114 print('Please remove the corresponding tests from '
115 'EXPECTED_FAILURES in tests/scripts/compliance_test.py')
116 print()
117 print('FAILED')
118 return 1
119 else:
120 shutil.rmtree(psa_arch_tests_dir)
121 print('SUCCESS')
122 return 0
123 finally:
Bence Szépkúti9f849112021-10-19 15:05:36 +0200124 os.chdir(mbedtls_dir)
Bence Szépkúti9f849112021-10-19 15:05:36 +0200125
126if __name__ == '__main__':
Bence Szépkúti559f1ce2021-11-02 13:48:39 +0100127 sys.exit(main())