blob: 6291d7898ec1b83ee23fdb64160ccea254c23fbd [file] [log] [blame]
Gilles Peskine51681552019-05-20 19:35:37 +02001#!/usr/bin/env python3
2"""Describe the test coverage of PSA functions in terms of return statuses.
3
Fredrik Hessecc207bc2021-09-28 21:06:08 +020041. Build Mbed TLS with -DRECORD_PSA_STATUS_COVERAGE_LOG
Gilles Peskine51681552019-05-20 19:35:37 +020052. Run psa_collect_statuses.py
6
7The output is a series of line of the form "psa_foo PSA_ERROR_XXX". Each
8function/status combination appears only once.
9
Fredrik Hessecc207bc2021-09-28 21:06:08 +020010This script must be run from the top of an Mbed TLS source tree.
Gilles Peskine51681552019-05-20 19:35:37 +020011The build command is "make -DRECORD_PSA_STATUS_COVERAGE_LOG", which is
12only supported with make (as opposed to CMake or other build methods).
13"""
14
Bence Szépkúti1e148272020-08-07 13:07:28 +020015# Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000016# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Bence Szépkúti700ee442020-05-26 00:33:31 +020017
Gilles Peskine51681552019-05-20 19:35:37 +020018import argparse
19import os
20import subprocess
21import sys
22
23DEFAULT_STATUS_LOG_FILE = 'tests/statuses.log'
24DEFAULT_PSA_CONSTANT_NAMES = 'programs/psa/psa_constant_names'
25
26class Statuses:
27 """Information about observed return statues of API functions."""
28
29 def __init__(self):
30 self.functions = {}
31 self.codes = set()
32 self.status_names = {}
33
34 def collect_log(self, log_file_name):
35 """Read logs from RECORD_PSA_STATUS_COVERAGE_LOG.
36
Fredrik Hessecc207bc2021-09-28 21:06:08 +020037 Read logs produced by running Mbed TLS test suites built with
Gilles Peskine51681552019-05-20 19:35:37 +020038 -DRECORD_PSA_STATUS_COVERAGE_LOG.
39 """
40 with open(log_file_name) as log:
41 for line in log:
42 value, function, tail = line.split(':', 2)
43 if function not in self.functions:
44 self.functions[function] = {}
45 fdata = self.functions[function]
46 if value not in self.functions[function]:
47 fdata[value] = []
48 fdata[value].append(tail)
49 self.codes.add(int(value))
50
51 def get_constant_names(self, psa_constant_names):
52 """Run psa_constant_names to obtain names for observed numerical values."""
53 values = [str(value) for value in self.codes]
54 cmd = [psa_constant_names, 'status'] + values
55 output = subprocess.check_output(cmd).decode('ascii')
56 for value, name in zip(values, output.rstrip().split('\n')):
57 self.status_names[value] = name
58
59 def report(self):
60 """Report observed return values for each function.
61
62 The report is a series of line of the form "psa_foo PSA_ERROR_XXX".
63 """
64 for function in sorted(self.functions.keys()):
65 fdata = self.functions[function]
66 names = [self.status_names[value] for value in fdata.keys()]
67 for name in sorted(names):
68 sys.stdout.write('{} {}\n'.format(function, name))
69
70def collect_status_logs(options):
71 """Build and run unit tests and report observed function return statuses.
72
Fredrik Hessecc207bc2021-09-28 21:06:08 +020073 Build Mbed TLS with -DRECORD_PSA_STATUS_COVERAGE_LOG, run the
Gilles Peskine51681552019-05-20 19:35:37 +020074 test suites and display information about observed return statuses.
75 """
76 rebuilt = False
77 if not options.use_existing_log and os.path.exists(options.log_file):
78 os.remove(options.log_file)
79 if not os.path.exists(options.log_file):
80 if options.clean_before:
81 subprocess.check_call(['make', 'clean'],
82 cwd='tests',
83 stdout=sys.stderr)
84 with open(os.devnull, 'w') as devnull:
Paul Elliott65879592023-12-07 20:08:10 +000085 build_command = ['make', '-q'] + options.make_vars.split(' ') + \
86 ['lib', 'tests']
87 make_q_ret = subprocess.call(build_command, stdout=devnull,
88 stderr=devnull)
89 print("blagh")
Gilles Peskine51681552019-05-20 19:35:37 +020090 if make_q_ret != 0:
Paul Elliott65879592023-12-07 20:08:10 +000091 build_command = ['make'] + options.make_vars.split(' ') + \
92 ['RECORD_PSA_STATUS_COVERAGE_LOG=1']
93 subprocess.check_call(build_command,
Gilles Peskine51681552019-05-20 19:35:37 +020094 stdout=sys.stderr)
95 rebuilt = True
96 subprocess.check_call(['make', 'test'],
97 stdout=sys.stderr)
98 data = Statuses()
99 data.collect_log(options.log_file)
100 data.get_constant_names(options.psa_constant_names)
101 if rebuilt and options.clean_after:
102 subprocess.check_call(['make', 'clean'],
103 cwd='tests',
104 stdout=sys.stderr)
105 return data
106
107def main():
108 parser = argparse.ArgumentParser(description=globals()['__doc__'])
109 parser.add_argument('--clean-after',
110 action='store_true',
111 help='Run "make clean" after rebuilding')
112 parser.add_argument('--clean-before',
113 action='store_true',
114 help='Run "make clean" before regenerating the log file)')
115 parser.add_argument('--log-file', metavar='FILE',
116 default=DEFAULT_STATUS_LOG_FILE,
117 help='Log file location (default: {})'.format(
118 DEFAULT_STATUS_LOG_FILE
119 ))
Paul Elliott65879592023-12-07 20:08:10 +0000120 parser.add_argument('--make-vars',
121 help='optional variable/value pairs to pass to make',
122 action='store', default='')
Gilles Peskine51681552019-05-20 19:35:37 +0200123 parser.add_argument('--psa-constant-names', metavar='PROGRAM',
124 default=DEFAULT_PSA_CONSTANT_NAMES,
125 help='Path to psa_constant_names (default: {})'.format(
126 DEFAULT_PSA_CONSTANT_NAMES
127 ))
128 parser.add_argument('--use-existing-log', '-e',
129 action='store_true',
130 help='Don\'t regenerate the log file if it exists')
131 options = parser.parse_args()
132 data = collect_status_logs(options)
133 data.report()
134
135if __name__ == '__main__':
136 main()