test_psa_constant_names.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/usr/bin/env python3
  2. """Test the program psa_constant_names.
  3. Gather constant names from header files and test cases. Compile a C program
  4. to print out their numerical values, feed these numerical values to
  5. psa_constant_names, and check that the output is the original name.
  6. Return 0 if all test cases pass, 1 if the output was not always as expected,
  7. or 1 (with a Python backtrace) if there was an operational error.
  8. """
  9. # Copyright The Mbed TLS Contributors
  10. # SPDX-License-Identifier: Apache-2.0
  11. #
  12. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  13. # not use this file except in compliance with the License.
  14. # You may obtain a copy of the License at
  15. #
  16. # http://www.apache.org/licenses/LICENSE-2.0
  17. #
  18. # Unless required by applicable law or agreed to in writing, software
  19. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  20. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. # See the License for the specific language governing permissions and
  22. # limitations under the License.
  23. import argparse
  24. from collections import namedtuple
  25. import os
  26. import re
  27. import subprocess
  28. import sys
  29. from typing import Iterable, List, Optional, Tuple
  30. import scripts_path # pylint: disable=unused-import
  31. from mbedtls_dev import c_build_helper
  32. from mbedtls_dev.macro_collector import InputsForTest, PSAMacroEnumerator
  33. from mbedtls_dev import typing_util
  34. def gather_inputs(headers: Iterable[str],
  35. test_suites: Iterable[str],
  36. inputs_class=InputsForTest) -> PSAMacroEnumerator:
  37. """Read the list of inputs to test psa_constant_names with."""
  38. inputs = inputs_class()
  39. for header in headers:
  40. inputs.parse_header(header)
  41. for test_cases in test_suites:
  42. inputs.parse_test_cases(test_cases)
  43. inputs.add_numerical_values()
  44. inputs.gather_arguments()
  45. return inputs
  46. def run_c(type_word: str,
  47. expressions: Iterable[str],
  48. include_path: Optional[str] = None,
  49. keep_c: bool = False) -> List[str]:
  50. """Generate and run a program to print out numerical values of C expressions."""
  51. if type_word == 'status':
  52. cast_to = 'long'
  53. printf_format = '%ld'
  54. else:
  55. cast_to = 'unsigned long'
  56. printf_format = '0x%08lx'
  57. return c_build_helper.get_c_expression_values(
  58. cast_to, printf_format,
  59. expressions,
  60. caller='test_psa_constant_names.py for {} values'.format(type_word),
  61. file_label=type_word,
  62. header='#include <psa/crypto.h>',
  63. include_path=include_path,
  64. keep_c=keep_c
  65. )
  66. NORMALIZE_STRIP_RE = re.compile(r'\s+')
  67. def normalize(expr: str) -> str:
  68. """Normalize the C expression so as not to care about trivial differences.
  69. Currently "trivial differences" means whitespace.
  70. """
  71. return re.sub(NORMALIZE_STRIP_RE, '', expr)
  72. def collect_values(inputs: InputsForTest,
  73. type_word: str,
  74. include_path: Optional[str] = None,
  75. keep_c: bool = False) -> Tuple[List[str], List[str]]:
  76. """Generate expressions using known macro names and calculate their values.
  77. Return a list of pairs of (expr, value) where expr is an expression and
  78. value is a string representation of its integer value.
  79. """
  80. names = inputs.get_names(type_word)
  81. expressions = sorted(inputs.generate_expressions(names))
  82. values = run_c(type_word, expressions,
  83. include_path=include_path, keep_c=keep_c)
  84. return expressions, values
  85. class Tests:
  86. """An object representing tests and their results."""
  87. Error = namedtuple('Error',
  88. ['type', 'expression', 'value', 'output'])
  89. def __init__(self, options) -> None:
  90. self.options = options
  91. self.count = 0
  92. self.errors = [] #type: List[Tests.Error]
  93. def run_one(self, inputs: InputsForTest, type_word: str) -> None:
  94. """Test psa_constant_names for the specified type.
  95. Run the program on the names for this type.
  96. Use the inputs to figure out what arguments to pass to macros that
  97. take arguments.
  98. """
  99. expressions, values = collect_values(inputs, type_word,
  100. include_path=self.options.include,
  101. keep_c=self.options.keep_c)
  102. output_bytes = subprocess.check_output([self.options.program,
  103. type_word] + values)
  104. output = output_bytes.decode('ascii')
  105. outputs = output.strip().split('\n')
  106. self.count += len(expressions)
  107. for expr, value, output in zip(expressions, values, outputs):
  108. if self.options.show:
  109. sys.stdout.write('{} {}\t{}\n'.format(type_word, value, output))
  110. if normalize(expr) != normalize(output):
  111. self.errors.append(self.Error(type=type_word,
  112. expression=expr,
  113. value=value,
  114. output=output))
  115. def run_all(self, inputs: InputsForTest) -> None:
  116. """Run psa_constant_names on all the gathered inputs."""
  117. for type_word in ['status', 'algorithm', 'ecc_curve', 'dh_group',
  118. 'key_type', 'key_usage']:
  119. self.run_one(inputs, type_word)
  120. def report(self, out: typing_util.Writable) -> None:
  121. """Describe each case where the output is not as expected.
  122. Write the errors to ``out``.
  123. Also write a total.
  124. """
  125. for error in self.errors:
  126. out.write('For {} "{}", got "{}" (value: {})\n'
  127. .format(error.type, error.expression,
  128. error.output, error.value))
  129. out.write('{} test cases'.format(self.count))
  130. if self.errors:
  131. out.write(', {} FAIL\n'.format(len(self.errors)))
  132. else:
  133. out.write(' PASS\n')
  134. HEADERS = ['psa/crypto.h', 'psa/crypto_extra.h', 'psa/crypto_values.h']
  135. TEST_SUITES = ['tests/suites/test_suite_psa_crypto_metadata.data']
  136. def main():
  137. parser = argparse.ArgumentParser(description=globals()['__doc__'])
  138. parser.add_argument('--include', '-I',
  139. action='append', default=['include'],
  140. help='Directory for header files')
  141. parser.add_argument('--keep-c',
  142. action='store_true', dest='keep_c', default=False,
  143. help='Keep the intermediate C file')
  144. parser.add_argument('--no-keep-c',
  145. action='store_false', dest='keep_c',
  146. help='Don\'t keep the intermediate C file (default)')
  147. parser.add_argument('--program',
  148. default='programs/psa/psa_constant_names',
  149. help='Program to test')
  150. parser.add_argument('--show',
  151. action='store_true',
  152. help='Show tested values on stdout')
  153. parser.add_argument('--no-show',
  154. action='store_false', dest='show',
  155. help='Don\'t show tested values (default)')
  156. options = parser.parse_args()
  157. headers = [os.path.join(options.include[0], h) for h in HEADERS]
  158. inputs = gather_inputs(headers, TEST_SUITES)
  159. tests = Tests(options)
  160. tests.run_all(inputs)
  161. tests.report(sys.stdout)
  162. if tests.errors:
  163. sys.exit(1)
  164. if __name__ == '__main__':
  165. main()