Bence Szépkúti | 80b31c5 | 2021-10-19 15:05:36 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 2 | """Run the PSA Crypto API compliance test suite. |
Bence Szépkúti | 449781f | 2021-11-02 13:41:14 +0100 | [diff] [blame] | 3 | Clone the repo and check out the commit specified by PSA_ARCH_TEST_REPO and PSA_ARCH_TEST_REF, |
David Horstmann | 3b8984a | 2023-08-29 10:32:26 +0100 | [diff] [blame] | 4 | then compile and run the test suite. The clone is stored at <repository root>/psa-arch-tests. |
Ronald Cron | 070e865 | 2023-10-09 10:25:45 +0200 | [diff] [blame] | 5 | Known defects in either the test suite or mbedtls / TF-PSA-Crypto - identified by their test |
David Horstmann | 3b8984a | 2023-08-29 10:32:26 +0100 | [diff] [blame] | 6 | number - are ignored, while unexpected failures AND successes are reported as errors, to help |
| 7 | keep the list of known defects as up to date as possible. |
Bence Szépkúti | 449781f | 2021-11-02 13:41:14 +0100 | [diff] [blame] | 8 | """ |
Bence Szépkúti | 67fb314 | 2021-11-02 14:01:08 +0100 | [diff] [blame] | 9 | |
| 10 | # Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 11 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Bence Szépkúti | 67fb314 | 2021-11-02 14:01:08 +0100 | [diff] [blame] | 12 | |
David Horstmann | 4dcddcf | 2023-08-17 18:08:24 +0100 | [diff] [blame] | 13 | import argparse |
Bence Szépkúti | 80b31c5 | 2021-10-19 15:05:36 +0200 | [diff] [blame] | 14 | import os |
| 15 | import re |
| 16 | import shutil |
| 17 | import subprocess |
| 18 | import sys |
David Horstmann | 9cc6b2f | 2023-08-29 17:36:35 +0100 | [diff] [blame] | 19 | from typing import List |
Bence Szépkúti | 80b31c5 | 2021-10-19 15:05:36 +0200 | [diff] [blame] | 20 | |
David Horstmann | e31014a | 2023-07-19 11:43:27 +0100 | [diff] [blame] | 21 | #pylint: disable=unused-import |
David Horstmann | 1d09184 | 2023-07-18 17:39:35 +0100 | [diff] [blame] | 22 | import scripts_path |
| 23 | from mbedtls_dev import build_tree |
| 24 | |
Ronald Cron | 070e865 | 2023-10-09 10:25:45 +0200 | [diff] [blame] | 25 | # PSA Compliance tests we expect to fail due to known defects in Mbed TLS / |
| 26 | # TF-PSA-Crypto (or the test suite). |
Bence Szépkúti | cb28871 | 2021-11-09 21:30:43 +0100 | [diff] [blame] | 27 | # The test numbers correspond to the numbers used by the console output of the test suite. |
| 28 | # Test number 2xx corresponds to the files in the folder |
| 29 | # psa-arch-tests/api-tests/dev_apis/crypto/test_c0xx |
Bence Szépkúti | 80b31c5 | 2021-10-19 15:05:36 +0200 | [diff] [blame] | 30 | EXPECTED_FAILURES = { |
Bence Szépkúti | cb28871 | 2021-11-09 21:30:43 +0100 | [diff] [blame] | 31 | # psa_hash_suspend() and psa_hash_resume() are not supported. |
| 32 | # - Tracked in issue #3274 |
| 33 | 262, 263 |
Bence Szépkúti | 80b31c5 | 2021-10-19 15:05:36 +0200 | [diff] [blame] | 34 | } |
Bence Szépkúti | e2855c3 | 2021-11-09 17:33:57 +0100 | [diff] [blame] | 35 | |
| 36 | # We currently use a fork of ARM-software/psa-arch-tests, with a couple of downstream patches |
Thomas Daubney | 540324c | 2023-10-06 17:07:24 +0100 | [diff] [blame] | 37 | # that allow it to build with Mbed TLS 3, and fixes a couple of issues in the compliance test suite. |
Bence Szépkúti | e2855c3 | 2021-11-09 17:33:57 +0100 | [diff] [blame] | 38 | # These fixes allow the tests numbered 216, 248 and 249 to complete successfully. |
| 39 | # |
| 40 | # Once all the fixes are upstreamed, this fork should be replaced with an upstream commit/tag. |
Bence Szépkúti | b376eac | 2021-11-09 22:13:46 +0100 | [diff] [blame] | 41 | # - Tracked in issue #5145 |
Bence Szépkúti | e2855c3 | 2021-11-09 17:33:57 +0100 | [diff] [blame] | 42 | # |
| 43 | # Web URL: https://github.com/bensze01/psa-arch-tests/tree/fixes-for-mbedtls-3 |
| 44 | PSA_ARCH_TESTS_REPO = 'https://github.com/bensze01/psa-arch-tests.git' |
Gilles Peskine | 42ed963 | 2022-05-17 17:23:09 +0200 | [diff] [blame] | 45 | PSA_ARCH_TESTS_REF = 'fix-pr-5736' |
Bence Szépkúti | 80b31c5 | 2021-10-19 15:05:36 +0200 | [diff] [blame] | 46 | |
David Horstmann | e31014a | 2023-07-19 11:43:27 +0100 | [diff] [blame] | 47 | #pylint: disable=too-many-branches,too-many-statements,too-many-locals |
David Horstmann | 4dcddcf | 2023-08-17 18:08:24 +0100 | [diff] [blame] | 48 | def main(library_build_dir: str): |
David Horstmann | f757069 | 2023-08-29 10:27:13 +0100 | [diff] [blame] | 49 | root_dir = os.getcwd() |
Bence Szépkúti | 80b31c5 | 2021-10-19 15:05:36 +0200 | [diff] [blame] | 50 | |
Thomas Daubney | e8f3789 | 2023-11-24 11:41:23 +0000 | [diff] [blame] | 51 | in_tf_psa_crypto_repo = build_tree.looks_like_tf_psa_crypto_root(root_dir) |
| 52 | |
Thomas Daubney | 08c6dc4 | 2023-11-30 13:56:09 +0000 | [diff] [blame^] | 53 | crypto_name = build_tree.crypto_library_filename(root_dir) |
Thomas Daubney | fc60e9b | 2023-11-24 10:56:04 +0000 | [diff] [blame] | 54 | library_subdir = build_tree.crypto_core_directory(root_dir) |
David Horstmann | beaee26 | 2023-08-29 13:56:17 +0100 | [diff] [blame] | 55 | |
| 56 | crypto_lib_filename = (library_build_dir + '/' + |
| 57 | library_subdir + '/' + |
| 58 | 'lib' + crypto_name + '.a') |
David Horstmann | 98af198 | 2023-08-29 10:25:26 +0100 | [diff] [blame] | 59 | |
| 60 | if not os.path.exists(crypto_lib_filename): |
David Horstmann | 2ba89be | 2023-08-29 10:37:29 +0100 | [diff] [blame] | 61 | #pylint: disable=bad-continuation |
David Horstmann | 4dcddcf | 2023-08-17 18:08:24 +0100 | [diff] [blame] | 62 | subprocess.check_call([ |
| 63 | 'cmake', '.', |
| 64 | '-GUnix Makefiles', |
David Horstmann | 41c316d | 2023-08-29 14:57:23 +0100 | [diff] [blame] | 65 | '-B' + library_build_dir |
David Horstmann | 4dcddcf | 2023-08-17 18:08:24 +0100 | [diff] [blame] | 66 | ]) |
David Horstmann | beaee26 | 2023-08-29 13:56:17 +0100 | [diff] [blame] | 67 | subprocess.check_call(['cmake', '--build', library_build_dir, |
David Horstmann | 8f3ec8e | 2023-08-30 09:46:20 +0100 | [diff] [blame] | 68 | '--target', crypto_name]) |
Bence Szépkúti | 80b31c5 | 2021-10-19 15:05:36 +0200 | [diff] [blame] | 69 | |
| 70 | psa_arch_tests_dir = 'psa-arch-tests' |
Bence Szépkúti | c63d160 | 2021-11-02 14:06:40 +0100 | [diff] [blame] | 71 | os.makedirs(psa_arch_tests_dir, exist_ok=True) |
Bence Szépkúti | 80b31c5 | 2021-10-19 15:05:36 +0200 | [diff] [blame] | 72 | try: |
Bence Szépkúti | 34b5f56 | 2021-11-02 13:48:39 +0100 | [diff] [blame] | 73 | os.chdir(psa_arch_tests_dir) |
Bence Szépkúti | 80b31c5 | 2021-10-19 15:05:36 +0200 | [diff] [blame] | 74 | |
Bence Szépkúti | b381841 | 2021-11-03 11:32:51 +0100 | [diff] [blame] | 75 | # Reuse existing local clone |
Bence Szépkúti | 34b5f56 | 2021-11-02 13:48:39 +0100 | [diff] [blame] | 76 | subprocess.check_call(['git', 'init']) |
| 77 | subprocess.check_call(['git', 'fetch', PSA_ARCH_TESTS_REPO, PSA_ARCH_TESTS_REF]) |
| 78 | subprocess.check_call(['git', 'checkout', 'FETCH_HEAD']) |
Bence Szépkúti | 80b31c5 | 2021-10-19 15:05:36 +0200 | [diff] [blame] | 79 | |
Bence Szépkúti | 34b5f56 | 2021-11-02 13:48:39 +0100 | [diff] [blame] | 80 | build_dir = 'api-tests/build' |
| 81 | try: |
| 82 | shutil.rmtree(build_dir) |
| 83 | except FileNotFoundError: |
| 84 | pass |
| 85 | os.mkdir(build_dir) |
| 86 | os.chdir(build_dir) |
Bence Szépkúti | 80b31c5 | 2021-10-19 15:05:36 +0200 | [diff] [blame] | 87 | |
David Horstmann | f757069 | 2023-08-29 10:27:13 +0100 | [diff] [blame] | 88 | extra_includes = (';{}/drivers/builtin/include'.format(root_dir) |
Ronald Cron | 070e865 | 2023-10-09 10:25:45 +0200 | [diff] [blame] | 89 | if in_tf_psa_crypto_repo else '') |
David Horstmann | 1d09184 | 2023-07-18 17:39:35 +0100 | [diff] [blame] | 90 | |
Bence Szépkúti | 34b5f56 | 2021-11-02 13:48:39 +0100 | [diff] [blame] | 91 | #pylint: disable=bad-continuation |
| 92 | subprocess.check_call([ |
| 93 | 'cmake', '..', |
| 94 | '-GUnix Makefiles', |
| 95 | '-DTARGET=tgt_dev_apis_stdc', |
| 96 | '-DTOOLCHAIN=HOST_GCC', |
| 97 | '-DSUITE=CRYPTO', |
David Horstmann | f757069 | 2023-08-29 10:27:13 +0100 | [diff] [blame] | 98 | '-DPSA_CRYPTO_LIB_FILENAME={}/{}'.format(root_dir, |
David Horstmann | 7f93d22 | 2023-08-23 16:21:40 +0100 | [diff] [blame] | 99 | crypto_lib_filename), |
David Horstmann | f757069 | 2023-08-29 10:27:13 +0100 | [diff] [blame] | 100 | ('-DPSA_INCLUDE_PATHS={}/include' + extra_includes).format(root_dir) |
Bence Szépkúti | 34b5f56 | 2021-11-02 13:48:39 +0100 | [diff] [blame] | 101 | ]) |
| 102 | subprocess.check_call(['cmake', '--build', '.']) |
Bence Szépkúti | 80b31c5 | 2021-10-19 15:05:36 +0200 | [diff] [blame] | 103 | |
Bence Szépkúti | 34b5f56 | 2021-11-02 13:48:39 +0100 | [diff] [blame] | 104 | proc = subprocess.Popen(['./psa-arch-tests-crypto'], |
| 105 | bufsize=1, stdout=subprocess.PIPE, universal_newlines=True) |
| 106 | |
| 107 | test_re = re.compile( |
| 108 | '^TEST: (?P<test_num>[0-9]*)|' |
| 109 | '^TEST RESULT: (?P<test_result>FAILED|PASSED)' |
| 110 | ) |
| 111 | test = -1 |
| 112 | unexpected_successes = set(EXPECTED_FAILURES) |
David Horstmann | fd9264e | 2023-08-29 16:21:15 +0100 | [diff] [blame] | 113 | expected_failures = [] # type: List[int] |
| 114 | unexpected_failures = [] # type: List[int] |
| 115 | if proc.stdout is None: |
| 116 | return 1 |
| 117 | |
Bence Szépkúti | 34b5f56 | 2021-11-02 13:48:39 +0100 | [diff] [blame] | 118 | for line in proc.stdout: |
| 119 | print(line, end='') |
| 120 | match = test_re.match(line) |
| 121 | if match is not None: |
| 122 | groupdict = match.groupdict() |
| 123 | test_num = groupdict['test_num'] |
| 124 | if test_num is not None: |
| 125 | test = int(test_num) |
| 126 | elif groupdict['test_result'] == 'FAILED': |
| 127 | try: |
| 128 | unexpected_successes.remove(test) |
| 129 | expected_failures.append(test) |
| 130 | print('Expected failure, ignoring') |
| 131 | except KeyError: |
| 132 | unexpected_failures.append(test) |
| 133 | print('ERROR: Unexpected failure') |
| 134 | elif test in unexpected_successes: |
| 135 | print('ERROR: Unexpected success') |
| 136 | proc.wait() |
| 137 | |
| 138 | print() |
| 139 | print('***** test_psa_compliance.py report ******') |
| 140 | print() |
| 141 | print('Expected failures:', ', '.join(str(i) for i in expected_failures)) |
| 142 | print('Unexpected failures:', ', '.join(str(i) for i in unexpected_failures)) |
| 143 | print('Unexpected successes:', ', '.join(str(i) for i in sorted(unexpected_successes))) |
| 144 | print() |
| 145 | if unexpected_successes or unexpected_failures: |
| 146 | if unexpected_successes: |
| 147 | print('Unexpected successes encountered.') |
| 148 | print('Please remove the corresponding tests from ' |
| 149 | 'EXPECTED_FAILURES in tests/scripts/compliance_test.py') |
| 150 | print() |
| 151 | print('FAILED') |
| 152 | return 1 |
| 153 | else: |
Bence Szépkúti | 34b5f56 | 2021-11-02 13:48:39 +0100 | [diff] [blame] | 154 | print('SUCCESS') |
| 155 | return 0 |
| 156 | finally: |
David Horstmann | f757069 | 2023-08-29 10:27:13 +0100 | [diff] [blame] | 157 | os.chdir(root_dir) |
Bence Szépkúti | 80b31c5 | 2021-10-19 15:05:36 +0200 | [diff] [blame] | 158 | |
| 159 | if __name__ == '__main__': |
David Horstmann | b48822c | 2023-08-29 14:12:53 +0100 | [diff] [blame] | 160 | BUILD_DIR = 'out_of_source_build' |
David Horstmann | 4dcddcf | 2023-08-17 18:08:24 +0100 | [diff] [blame] | 161 | |
David Horstmann | 3ed1871 | 2023-08-29 18:20:01 +0100 | [diff] [blame] | 162 | # pylint: disable=invalid-name |
David Horstmann | 4dcddcf | 2023-08-17 18:08:24 +0100 | [diff] [blame] | 163 | parser = argparse.ArgumentParser() |
| 164 | parser.add_argument('--build-dir', nargs=1, |
Ronald Cron | 070e865 | 2023-10-09 10:25:45 +0200 | [diff] [blame] | 165 | help='path to Mbed TLS / TF-PSA-Crypto build directory') |
David Horstmann | 4dcddcf | 2023-08-17 18:08:24 +0100 | [diff] [blame] | 166 | args = parser.parse_args() |
| 167 | |
| 168 | if args.build_dir is not None: |
David Horstmann | b48822c | 2023-08-29 14:12:53 +0100 | [diff] [blame] | 169 | BUILD_DIR = args.build_dir[0] |
David Horstmann | 4dcddcf | 2023-08-17 18:08:24 +0100 | [diff] [blame] | 170 | |
David Horstmann | b48822c | 2023-08-29 14:12:53 +0100 | [diff] [blame] | 171 | sys.exit(main(BUILD_DIR)) |