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