psa_collect_statuses.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env python3
  2. """Describe the test coverage of PSA functions in terms of return statuses.
  3. 1. Build Mbed Crypto with -DRECORD_PSA_STATUS_COVERAGE_LOG
  4. 2. Run psa_collect_statuses.py
  5. The output is a series of line of the form "psa_foo PSA_ERROR_XXX". Each
  6. function/status combination appears only once.
  7. This script must be run from the top of an Mbed Crypto source tree.
  8. The build command is "make -DRECORD_PSA_STATUS_COVERAGE_LOG", which is
  9. only supported with make (as opposed to CMake or other build methods).
  10. """
  11. # Copyright The Mbed TLS Contributors
  12. # SPDX-License-Identifier: Apache-2.0
  13. #
  14. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  15. # not use this file except in compliance with the License.
  16. # You may obtain a copy of the License at
  17. #
  18. # http://www.apache.org/licenses/LICENSE-2.0
  19. #
  20. # Unless required by applicable law or agreed to in writing, software
  21. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  22. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. # See the License for the specific language governing permissions and
  24. # limitations under the License.
  25. import argparse
  26. import os
  27. import subprocess
  28. import sys
  29. DEFAULT_STATUS_LOG_FILE = 'tests/statuses.log'
  30. DEFAULT_PSA_CONSTANT_NAMES = 'programs/psa/psa_constant_names'
  31. class Statuses:
  32. """Information about observed return statues of API functions."""
  33. def __init__(self):
  34. self.functions = {}
  35. self.codes = set()
  36. self.status_names = {}
  37. def collect_log(self, log_file_name):
  38. """Read logs from RECORD_PSA_STATUS_COVERAGE_LOG.
  39. Read logs produced by running Mbed Crypto test suites built with
  40. -DRECORD_PSA_STATUS_COVERAGE_LOG.
  41. """
  42. with open(log_file_name) as log:
  43. for line in log:
  44. value, function, tail = line.split(':', 2)
  45. if function not in self.functions:
  46. self.functions[function] = {}
  47. fdata = self.functions[function]
  48. if value not in self.functions[function]:
  49. fdata[value] = []
  50. fdata[value].append(tail)
  51. self.codes.add(int(value))
  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. def report(self):
  60. """Report observed return values for each function.
  61. The report is a series of line of the form "psa_foo PSA_ERROR_XXX".
  62. """
  63. for function in sorted(self.functions.keys()):
  64. fdata = self.functions[function]
  65. names = [self.status_names[value] for value in fdata.keys()]
  66. for name in sorted(names):
  67. sys.stdout.write('{} {}\n'.format(function, name))
  68. def collect_status_logs(options):
  69. """Build and run unit tests and report observed function return statuses.
  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. def main():
  99. parser = argparse.ArgumentParser(description=globals()['__doc__'])
  100. parser.add_argument('--clean-after',
  101. action='store_true',
  102. help='Run "make clean" after rebuilding')
  103. parser.add_argument('--clean-before',
  104. action='store_true',
  105. help='Run "make clean" before regenerating the log file)')
  106. parser.add_argument('--log-file', metavar='FILE',
  107. default=DEFAULT_STATUS_LOG_FILE,
  108. help='Log file location (default: {})'.format(
  109. DEFAULT_STATUS_LOG_FILE
  110. ))
  111. parser.add_argument('--psa-constant-names', metavar='PROGRAM',
  112. default=DEFAULT_PSA_CONSTANT_NAMES,
  113. help='Path to psa_constant_names (default: {})'.format(
  114. DEFAULT_PSA_CONSTANT_NAMES
  115. ))
  116. parser.add_argument('--use-existing-log', '-e',
  117. action='store_true',
  118. help='Don\'t regenerate the log file if it exists')
  119. options = parser.parse_args()
  120. data = collect_status_logs(options)
  121. data.report()
  122. if __name__ == '__main__':
  123. main()