blob: 7d7192f063ec81962abf6174fc0f546175428243 [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,
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úti80b31c52021-10-19 15:05:36 +02009import os
10import re
11import shutil
12import subprocess
13import sys
14
15EXPECTED_FAILURES = {
16 216, 221, 224, 225, 248, 249, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263
17}
18PSA_ARCH_TESTS_REPO = 'https://github.com/ronald-cron-arm/psa-arch-tests.git'
19PSA_ARCH_TESTS_REF = 'crypto1.0-3.0'
20
Bence Szépkúti34b5f562021-11-02 13:48:39 +010021#pylint: disable=too-many-branches,too-many-statements
Bence Szépkúti80b31c52021-10-19 15:05:36 +020022def main():
23 mbedtls_dir = os.getcwd()
24
Bence Szépkútica9236b2021-10-25 19:29:07 +020025 if not os.path.exists('library/libmbedcrypto.a'):
26 subprocess.check_call(['make', '-C', 'library', 'libmbedcrypto.a'])
Bence Szépkúti80b31c52021-10-19 15:05:36 +020027
28 psa_arch_tests_dir = 'psa-arch-tests'
29 try:
30 os.mkdir(psa_arch_tests_dir)
31 except FileExistsError:
32 pass
Bence Szépkúti80b31c52021-10-19 15:05:36 +020033 try:
Bence Szépkúti34b5f562021-11-02 13:48:39 +010034 os.chdir(psa_arch_tests_dir)
Bence Szépkúti80b31c52021-10-19 15:05:36 +020035
Bence Szépkúti34b5f562021-11-02 13:48:39 +010036 subprocess.check_call(['git', 'init'])
37 subprocess.check_call(['git', 'fetch', PSA_ARCH_TESTS_REPO, PSA_ARCH_TESTS_REF])
38 subprocess.check_call(['git', 'checkout', 'FETCH_HEAD'])
Bence Szépkúti80b31c52021-10-19 15:05:36 +020039
Bence Szépkúti34b5f562021-11-02 13:48:39 +010040 build_dir = 'api-tests/build'
41 try:
42 shutil.rmtree(build_dir)
43 except FileNotFoundError:
44 pass
45 os.mkdir(build_dir)
46 os.chdir(build_dir)
Bence Szépkúti80b31c52021-10-19 15:05:36 +020047
Bence Szépkúti34b5f562021-11-02 13:48:39 +010048 #pylint: disable=bad-continuation
49 subprocess.check_call([
50 'cmake', '..',
51 '-GUnix Makefiles',
52 '-DTARGET=tgt_dev_apis_stdc',
53 '-DTOOLCHAIN=HOST_GCC',
54 '-DSUITE=CRYPTO',
55 '-DPSA_CRYPTO_LIB_FILENAME={}/library/libmbedcrypto.a'.format(mbedtls_dir),
56 '-DPSA_INCLUDE_PATHS={}/include'.format(mbedtls_dir)
57 ])
58 subprocess.check_call(['cmake', '--build', '.'])
Bence Szépkúti80b31c52021-10-19 15:05:36 +020059
Bence Szépkúti34b5f562021-11-02 13:48:39 +010060 proc = subprocess.Popen(['./psa-arch-tests-crypto'],
61 bufsize=1, stdout=subprocess.PIPE, universal_newlines=True)
62
63 test_re = re.compile(
64 '^TEST: (?P<test_num>[0-9]*)|'
65 '^TEST RESULT: (?P<test_result>FAILED|PASSED)'
66 )
67 test = -1
68 unexpected_successes = set(EXPECTED_FAILURES)
69 expected_failures = []
70 unexpected_failures = []
71 for line in proc.stdout:
72 print(line, end='')
73 match = test_re.match(line)
74 if match is not None:
75 groupdict = match.groupdict()
76 test_num = groupdict['test_num']
77 if test_num is not None:
78 test = int(test_num)
79 elif groupdict['test_result'] == 'FAILED':
80 try:
81 unexpected_successes.remove(test)
82 expected_failures.append(test)
83 print('Expected failure, ignoring')
84 except KeyError:
85 unexpected_failures.append(test)
86 print('ERROR: Unexpected failure')
87 elif test in unexpected_successes:
88 print('ERROR: Unexpected success')
89 proc.wait()
90
91 print()
92 print('***** test_psa_compliance.py report ******')
93 print()
94 print('Expected failures:', ', '.join(str(i) for i in expected_failures))
95 print('Unexpected failures:', ', '.join(str(i) for i in unexpected_failures))
96 print('Unexpected successes:', ', '.join(str(i) for i in sorted(unexpected_successes)))
97 print()
98 if unexpected_successes or unexpected_failures:
99 if unexpected_successes:
100 print('Unexpected successes encountered.')
101 print('Please remove the corresponding tests from '
102 'EXPECTED_FAILURES in tests/scripts/compliance_test.py')
103 print()
104 print('FAILED')
105 return 1
106 else:
107 shutil.rmtree(psa_arch_tests_dir)
108 print('SUCCESS')
109 return 0
110 finally:
Bence Szépkúti80b31c52021-10-19 15:05:36 +0200111 os.chdir(mbedtls_dir)
Bence Szépkúti80b31c52021-10-19 15:05:36 +0200112
113if __name__ == '__main__':
Bence Szépkúti34b5f562021-11-02 13:48:39 +0100114 sys.exit(main())