blob: e38beeac384d0b96512ba7790d2c6cb21e809e94 [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
41. Build Mbed Crypto with -DRECORD_PSA_STATUS_COVERAGE_LOG
52. 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
10This script must be run from the top of an Mbed Crypto source tree.
11The 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
15import argparse
16import os
17import subprocess
18import sys
19
20DEFAULT_STATUS_LOG_FILE = 'tests/statuses.log'
21DEFAULT_PSA_CONSTANT_NAMES = 'programs/psa/psa_constant_names'
22
23class Statuses:
24 """Information about observed return statues of API functions."""
25
26 def __init__(self):
27 self.functions = {}
28 self.codes = set()
29 self.status_names = {}
30
31 def collect_log(self, log_file_name):
32 """Read logs from RECORD_PSA_STATUS_COVERAGE_LOG.
33
34 Read logs produced by running Mbed Crypto test suites built with
35 -DRECORD_PSA_STATUS_COVERAGE_LOG.
36 """
37 with open(log_file_name) as log:
38 for line in log:
39 value, function, tail = line.split(':', 2)
40 if function not in self.functions:
41 self.functions[function] = {}
42 fdata = self.functions[function]
43 if value not in self.functions[function]:
44 fdata[value] = []
45 fdata[value].append(tail)
46 self.codes.add(int(value))
47
48 def get_constant_names(self, psa_constant_names):
49 """Run psa_constant_names to obtain names for observed numerical values."""
50 values = [str(value) for value in self.codes]
51 cmd = [psa_constant_names, 'status'] + values
52 output = subprocess.check_output(cmd).decode('ascii')
53 for value, name in zip(values, output.rstrip().split('\n')):
54 self.status_names[value] = name
55
56 def report(self):
57 """Report observed return values for each function.
58
59 The report is a series of line of the form "psa_foo PSA_ERROR_XXX".
60 """
61 for function in sorted(self.functions.keys()):
62 fdata = self.functions[function]
63 names = [self.status_names[value] for value in fdata.keys()]
64 for name in sorted(names):
65 sys.stdout.write('{} {}\n'.format(function, name))
66
67def collect_status_logs(options):
68 """Build and run unit tests and report observed function return statuses.
69
70 Build Mbed Crypto with -DRECORD_PSA_STATUS_COVERAGE_LOG, run the
71 test suites and display information about observed return statuses.
72 """
73 rebuilt = False
74 if not options.use_existing_log and os.path.exists(options.log_file):
75 os.remove(options.log_file)
76 if not os.path.exists(options.log_file):
77 if options.clean_before:
78 subprocess.check_call(['make', 'clean'],
79 cwd='tests',
80 stdout=sys.stderr)
81 with open(os.devnull, 'w') as devnull:
82 make_q_ret = subprocess.call(['make', '-q', 'lib', 'tests'],
83 stdout=devnull, stderr=devnull)
84 if make_q_ret != 0:
85 subprocess.check_call(['make', 'RECORD_PSA_STATUS_COVERAGE_LOG=1'],
86 stdout=sys.stderr)
87 rebuilt = True
88 subprocess.check_call(['make', 'test'],
89 stdout=sys.stderr)
90 data = Statuses()
91 data.collect_log(options.log_file)
92 data.get_constant_names(options.psa_constant_names)
93 if rebuilt and options.clean_after:
94 subprocess.check_call(['make', 'clean'],
95 cwd='tests',
96 stdout=sys.stderr)
97 return data
98
99def main():
100 parser = argparse.ArgumentParser(description=globals()['__doc__'])
101 parser.add_argument('--clean-after',
102 action='store_true',
103 help='Run "make clean" after rebuilding')
104 parser.add_argument('--clean-before',
105 action='store_true',
106 help='Run "make clean" before regenerating the log file)')
107 parser.add_argument('--log-file', metavar='FILE',
108 default=DEFAULT_STATUS_LOG_FILE,
109 help='Log file location (default: {})'.format(
110 DEFAULT_STATUS_LOG_FILE
111 ))
112 parser.add_argument('--psa-constant-names', metavar='PROGRAM',
113 default=DEFAULT_PSA_CONSTANT_NAMES,
114 help='Path to psa_constant_names (default: {})'.format(
115 DEFAULT_PSA_CONSTANT_NAMES
116 ))
117 parser.add_argument('--use-existing-log', '-e',
118 action='store_true',
119 help='Don\'t regenerate the log file if it exists')
120 options = parser.parse_args()
121 data = collect_status_logs(options)
122 data.report()
123
124if __name__ == '__main__':
125 main()