blob: 7673236872be81951ab719e4daa8eae83ff9a60a [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
Bence Szépkútic7da1fe2020-05-26 01:54:15 +020016# SPDX-License-Identifier: Apache-2.0
17#
18# Licensed under the Apache License, Version 2.0 (the "License"); you may
19# not use this file except in compliance with the License.
20# You may obtain a copy of the License at
21#
22# http://www.apache.org/licenses/LICENSE-2.0
23#
24# Unless required by applicable law or agreed to in writing, software
25# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
26# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27# See the License for the specific language governing permissions and
28# limitations under the License.
Bence Szépkúti700ee442020-05-26 00:33:31 +020029#
30# This file is part of Mbed TLS (https://tls.mbed.org)
31
Gilles Peskine51681552019-05-20 19:35:37 +020032import argparse
33import os
34import subprocess
35import sys
36
37DEFAULT_STATUS_LOG_FILE = 'tests/statuses.log'
38DEFAULT_PSA_CONSTANT_NAMES = 'programs/psa/psa_constant_names'
39
40class Statuses:
41 """Information about observed return statues of API functions."""
42
43 def __init__(self):
44 self.functions = {}
45 self.codes = set()
46 self.status_names = {}
47
48 def collect_log(self, log_file_name):
49 """Read logs from RECORD_PSA_STATUS_COVERAGE_LOG.
50
51 Read logs produced by running Mbed Crypto test suites built with
52 -DRECORD_PSA_STATUS_COVERAGE_LOG.
53 """
54 with open(log_file_name) as log:
55 for line in log:
56 value, function, tail = line.split(':', 2)
57 if function not in self.functions:
58 self.functions[function] = {}
59 fdata = self.functions[function]
60 if value not in self.functions[function]:
61 fdata[value] = []
62 fdata[value].append(tail)
63 self.codes.add(int(value))
64
65 def get_constant_names(self, psa_constant_names):
66 """Run psa_constant_names to obtain names for observed numerical values."""
67 values = [str(value) for value in self.codes]
68 cmd = [psa_constant_names, 'status'] + values
69 output = subprocess.check_output(cmd).decode('ascii')
70 for value, name in zip(values, output.rstrip().split('\n')):
71 self.status_names[value] = name
72
73 def report(self):
74 """Report observed return values for each function.
75
76 The report is a series of line of the form "psa_foo PSA_ERROR_XXX".
77 """
78 for function in sorted(self.functions.keys()):
79 fdata = self.functions[function]
80 names = [self.status_names[value] for value in fdata.keys()]
81 for name in sorted(names):
82 sys.stdout.write('{} {}\n'.format(function, name))
83
84def collect_status_logs(options):
85 """Build and run unit tests and report observed function return statuses.
86
87 Build Mbed Crypto with -DRECORD_PSA_STATUS_COVERAGE_LOG, run the
88 test suites and display information about observed return statuses.
89 """
90 rebuilt = False
91 if not options.use_existing_log and os.path.exists(options.log_file):
92 os.remove(options.log_file)
93 if not os.path.exists(options.log_file):
94 if options.clean_before:
95 subprocess.check_call(['make', 'clean'],
96 cwd='tests',
97 stdout=sys.stderr)
98 with open(os.devnull, 'w') as devnull:
99 make_q_ret = subprocess.call(['make', '-q', 'lib', 'tests'],
100 stdout=devnull, stderr=devnull)
101 if make_q_ret != 0:
102 subprocess.check_call(['make', 'RECORD_PSA_STATUS_COVERAGE_LOG=1'],
103 stdout=sys.stderr)
104 rebuilt = True
105 subprocess.check_call(['make', 'test'],
106 stdout=sys.stderr)
107 data = Statuses()
108 data.collect_log(options.log_file)
109 data.get_constant_names(options.psa_constant_names)
110 if rebuilt and options.clean_after:
111 subprocess.check_call(['make', 'clean'],
112 cwd='tests',
113 stdout=sys.stderr)
114 return data
115
116def main():
117 parser = argparse.ArgumentParser(description=globals()['__doc__'])
118 parser.add_argument('--clean-after',
119 action='store_true',
120 help='Run "make clean" after rebuilding')
121 parser.add_argument('--clean-before',
122 action='store_true',
123 help='Run "make clean" before regenerating the log file)')
124 parser.add_argument('--log-file', metavar='FILE',
125 default=DEFAULT_STATUS_LOG_FILE,
126 help='Log file location (default: {})'.format(
127 DEFAULT_STATUS_LOG_FILE
128 ))
129 parser.add_argument('--psa-constant-names', metavar='PROGRAM',
130 default=DEFAULT_PSA_CONSTANT_NAMES,
131 help='Path to psa_constant_names (default: {})'.format(
132 DEFAULT_PSA_CONSTANT_NAMES
133 ))
134 parser.add_argument('--use-existing-log', '-e',
135 action='store_true',
136 help='Don\'t regenerate the log file if it exists')
137 options = parser.parse_args()
138 data = collect_status_logs(options)
139 data.report()
140
141if __name__ == '__main__':
142 main()