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