2018-09-25 18:49:23 +02:00
|
|
|
#!/usr/bin/env python3
|
2019-06-03 11:23:56 +02:00
|
|
|
"""Test the program psa_constant_names.
|
2018-09-25 18:49:23 +02:00
|
|
|
Gather constant names from header files and test cases. Compile a C program
|
|
|
|
to print out their numerical values, feed these numerical values to
|
|
|
|
psa_constant_names, and check that the output is the original name.
|
|
|
|
Return 0 if all test cases pass, 1 if the output was not always as expected,
|
2019-06-03 11:23:56 +02:00
|
|
|
or 1 (with a Python backtrace) if there was an operational error.
|
|
|
|
"""
|
2018-09-25 18:49:23 +02:00
|
|
|
|
2020-08-07 13:07:28 +02:00
|
|
|
# Copyright The Mbed TLS Contributors
|
2020-05-26 01:54:15 +02:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2020-05-26 00:33:31 +02:00
|
|
|
|
2018-09-25 18:49:23 +02:00
|
|
|
import argparse
|
2019-11-21 17:51:11 +01:00
|
|
|
from collections import namedtuple
|
2018-09-25 18:49:23 +02:00
|
|
|
import itertools
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2020-12-11 00:30:53 +01:00
|
|
|
|
|
|
|
import scripts_path # pylint: disable=unused-import
|
|
|
|
from mbedtls_dev import c_build_helper
|
2018-09-25 18:49:23 +02:00
|
|
|
|
2018-10-19 11:27:10 +02:00
|
|
|
class ReadFileLineException(Exception):
|
|
|
|
def __init__(self, filename, line_number):
|
|
|
|
message = 'in {} at {}'.format(filename, line_number)
|
|
|
|
super(ReadFileLineException, self).__init__(message)
|
|
|
|
self.filename = filename
|
|
|
|
self.line_number = line_number
|
|
|
|
|
|
|
|
class read_file_lines:
|
2019-05-27 18:31:59 +02:00
|
|
|
# Dear Pylint, conventionally, a context manager class name is lowercase.
|
|
|
|
# pylint: disable=invalid-name,too-few-public-methods
|
2019-06-03 11:23:56 +02:00
|
|
|
"""Context manager to read a text file line by line.
|
|
|
|
|
|
|
|
```
|
|
|
|
with read_file_lines(filename) as lines:
|
|
|
|
for line in lines:
|
|
|
|
process(line)
|
|
|
|
```
|
|
|
|
is equivalent to
|
|
|
|
```
|
|
|
|
with open(filename, 'r') as input_file:
|
|
|
|
for line in input_file:
|
|
|
|
process(line)
|
|
|
|
```
|
|
|
|
except that if process(line) raises an exception, then the read_file_lines
|
|
|
|
snippet annotates the exception with the file name and line number.
|
|
|
|
"""
|
2019-12-06 19:20:13 +01:00
|
|
|
def __init__(self, filename, binary=False):
|
2018-10-19 11:27:10 +02:00
|
|
|
self.filename = filename
|
|
|
|
self.line_number = 'entry'
|
2019-05-27 18:31:59 +02:00
|
|
|
self.generator = None
|
2019-12-06 19:20:13 +01:00
|
|
|
self.binary = binary
|
2018-10-19 11:27:10 +02:00
|
|
|
def __enter__(self):
|
2019-12-06 19:20:13 +01:00
|
|
|
self.generator = enumerate(open(self.filename,
|
|
|
|
'rb' if self.binary else 'r'))
|
2018-10-19 11:27:10 +02:00
|
|
|
return self
|
|
|
|
def __iter__(self):
|
|
|
|
for line_number, content in self.generator:
|
|
|
|
self.line_number = line_number
|
|
|
|
yield content
|
|
|
|
self.line_number = 'exit'
|
2019-05-27 18:29:47 +02:00
|
|
|
def __exit__(self, exc_type, exc_value, exc_traceback):
|
|
|
|
if exc_type is not None:
|
2018-10-19 11:27:10 +02:00
|
|
|
raise ReadFileLineException(self.filename, self.line_number) \
|
2019-05-27 18:29:47 +02:00
|
|
|
from exc_value
|
2018-10-19 11:27:10 +02:00
|
|
|
|
2018-09-25 18:49:23 +02:00
|
|
|
class Inputs:
|
2019-11-21 19:22:45 +01:00
|
|
|
# pylint: disable=too-many-instance-attributes
|
2019-06-03 11:23:56 +02:00
|
|
|
"""Accumulate information about macros to test.
|
2019-11-21 17:16:21 +01:00
|
|
|
|
2019-06-03 11:23:56 +02:00
|
|
|
This includes macro names as well as information about their arguments
|
|
|
|
when applicable.
|
|
|
|
"""
|
|
|
|
|
2018-09-25 18:49:23 +02:00
|
|
|
def __init__(self):
|
2019-11-21 19:49:26 +01:00
|
|
|
self.all_declared = set()
|
2018-09-25 18:49:23 +02:00
|
|
|
# Sets of names per type
|
|
|
|
self.statuses = set(['PSA_SUCCESS'])
|
|
|
|
self.algorithms = set(['0xffffffff'])
|
2019-12-04 17:18:41 +01:00
|
|
|
self.ecc_curves = set(['0xff'])
|
|
|
|
self.dh_groups = set(['0xff'])
|
|
|
|
self.key_types = set(['0xffff'])
|
2018-09-25 18:49:23 +02:00
|
|
|
self.key_usage_flags = set(['0x80000000'])
|
2020-12-08 11:10:21 +01:00
|
|
|
# Hard-coded values for unknown algorithms
|
|
|
|
#
|
|
|
|
# These have to have values that are correct for their respective
|
|
|
|
# PSA_ALG_IS_xxx macros, but are also not currently assigned and are
|
|
|
|
# not likely to be assigned in the near future.
|
|
|
|
self.hash_algorithms = set(['0x020000fe']) # 0x020000ff is PSA_ALG_ANY_HASH
|
2021-02-22 18:53:07 +01:00
|
|
|
self.mac_algorithms = set(['0x03007fff'])
|
2020-12-08 07:33:08 +01:00
|
|
|
self.ka_algorithms = set(['0x09fc0000'])
|
|
|
|
self.kdf_algorithms = set(['0x080000ff'])
|
2018-10-19 11:30:26 +02:00
|
|
|
# For AEAD algorithms, the only variability is over the tag length,
|
|
|
|
# and this only applies to known algorithms, so don't test an
|
|
|
|
# unknown algorithm.
|
|
|
|
self.aead_algorithms = set()
|
2018-09-25 18:49:23 +02:00
|
|
|
# Identifier prefixes
|
|
|
|
self.table_by_prefix = {
|
|
|
|
'ERROR': self.statuses,
|
|
|
|
'ALG': self.algorithms,
|
2019-11-21 18:58:36 +01:00
|
|
|
'ECC_CURVE': self.ecc_curves,
|
|
|
|
'DH_GROUP': self.dh_groups,
|
2018-09-25 18:49:23 +02:00
|
|
|
'KEY_TYPE': self.key_types,
|
|
|
|
'KEY_USAGE': self.key_usage_flags,
|
|
|
|
}
|
2019-11-21 19:22:45 +01:00
|
|
|
# Test functions
|
|
|
|
self.table_by_test_function = {
|
2019-11-25 17:10:12 +01:00
|
|
|
# Any function ending in _algorithm also gets added to
|
|
|
|
# self.algorithms.
|
|
|
|
'key_type': [self.key_types],
|
2019-12-02 17:26:44 +01:00
|
|
|
'block_cipher_key_type': [self.key_types],
|
|
|
|
'stream_cipher_key_type': [self.key_types],
|
2019-12-03 17:24:19 +01:00
|
|
|
'ecc_key_family': [self.ecc_curves],
|
2019-11-25 17:10:12 +01:00
|
|
|
'ecc_key_types': [self.ecc_curves],
|
2019-12-03 17:24:19 +01:00
|
|
|
'dh_key_family': [self.dh_groups],
|
2019-11-25 17:10:12 +01:00
|
|
|
'dh_key_types': [self.dh_groups],
|
|
|
|
'hash_algorithm': [self.hash_algorithms],
|
|
|
|
'mac_algorithm': [self.mac_algorithms],
|
|
|
|
'cipher_algorithm': [],
|
|
|
|
'hmac_algorithm': [self.mac_algorithms],
|
|
|
|
'aead_algorithm': [self.aead_algorithms],
|
|
|
|
'key_derivation_algorithm': [self.kdf_algorithms],
|
|
|
|
'key_agreement_algorithm': [self.ka_algorithms],
|
|
|
|
'asymmetric_signature_algorithm': [],
|
|
|
|
'asymmetric_signature_wildcard': [self.algorithms],
|
|
|
|
'asymmetric_encryption_algorithm': [],
|
|
|
|
'other_algorithm': [],
|
2019-11-21 19:22:45 +01:00
|
|
|
}
|
2018-09-25 18:49:23 +02:00
|
|
|
# macro name -> list of argument names
|
|
|
|
self.argspecs = {}
|
|
|
|
# argument name -> list of values
|
2018-10-19 11:30:26 +02:00
|
|
|
self.arguments_for = {
|
|
|
|
'mac_length': ['1', '63'],
|
|
|
|
'tag_length': ['1', '63'],
|
2021-02-22 18:53:07 +01:00
|
|
|
'min_mac_length': ['1', '63'],
|
|
|
|
'min_tag_length': ['1', '63'],
|
2018-10-19 11:30:26 +02:00
|
|
|
}
|
2018-09-25 18:49:23 +02:00
|
|
|
|
2019-11-21 17:17:01 +01:00
|
|
|
def get_names(self, type_word):
|
|
|
|
"""Return the set of known names of values of the given type."""
|
|
|
|
return {
|
|
|
|
'status': self.statuses,
|
|
|
|
'algorithm': self.algorithms,
|
|
|
|
'ecc_curve': self.ecc_curves,
|
|
|
|
'dh_group': self.dh_groups,
|
|
|
|
'key_type': self.key_types,
|
|
|
|
'key_usage': self.key_usage_flags,
|
|
|
|
}[type_word]
|
|
|
|
|
2018-09-25 18:49:23 +02:00
|
|
|
def gather_arguments(self):
|
2019-06-03 11:23:56 +02:00
|
|
|
"""Populate the list of values for macro arguments.
|
2019-11-21 17:16:21 +01:00
|
|
|
|
2019-06-03 11:23:56 +02:00
|
|
|
Call this after parsing all the inputs.
|
|
|
|
"""
|
2018-09-25 18:49:23 +02:00
|
|
|
self.arguments_for['hash_alg'] = sorted(self.hash_algorithms)
|
2018-10-19 11:30:26 +02:00
|
|
|
self.arguments_for['mac_alg'] = sorted(self.mac_algorithms)
|
2019-04-12 00:12:07 +02:00
|
|
|
self.arguments_for['ka_alg'] = sorted(self.ka_algorithms)
|
2019-01-04 19:46:31 +01:00
|
|
|
self.arguments_for['kdf_alg'] = sorted(self.kdf_algorithms)
|
2018-10-19 11:30:26 +02:00
|
|
|
self.arguments_for['aead_alg'] = sorted(self.aead_algorithms)
|
2018-09-25 18:49:23 +02:00
|
|
|
self.arguments_for['curve'] = sorted(self.ecc_curves)
|
2019-05-16 12:55:35 +02:00
|
|
|
self.arguments_for['group'] = sorted(self.dh_groups)
|
2018-09-25 18:49:23 +02:00
|
|
|
|
2019-05-27 18:29:47 +02:00
|
|
|
@staticmethod
|
|
|
|
def _format_arguments(name, arguments):
|
2019-06-03 11:23:56 +02:00
|
|
|
"""Format a macro call with arguments.."""
|
2018-09-25 18:49:23 +02:00
|
|
|
return name + '(' + ', '.join(arguments) + ')'
|
|
|
|
|
|
|
|
def distribute_arguments(self, name):
|
2019-06-03 11:23:56 +02:00
|
|
|
"""Generate macro calls with each tested argument set.
|
2019-11-21 17:16:21 +01:00
|
|
|
|
2019-06-03 11:23:56 +02:00
|
|
|
If name is a macro without arguments, just yield "name".
|
|
|
|
If name is a macro with arguments, yield a series of
|
|
|
|
"name(arg1,...,argN)" where each argument takes each possible
|
|
|
|
value at least once.
|
|
|
|
"""
|
2018-10-19 11:27:10 +02:00
|
|
|
try:
|
|
|
|
if name not in self.argspecs:
|
|
|
|
yield name
|
|
|
|
return
|
|
|
|
argspec = self.argspecs[name]
|
|
|
|
if argspec == []:
|
|
|
|
yield name + '()'
|
|
|
|
return
|
|
|
|
argument_lists = [self.arguments_for[arg] for arg in argspec]
|
|
|
|
arguments = [values[0] for values in argument_lists]
|
2019-05-27 18:29:47 +02:00
|
|
|
yield self._format_arguments(name, arguments)
|
2019-05-27 18:31:59 +02:00
|
|
|
# Dear Pylint, enumerate won't work here since we're modifying
|
|
|
|
# the array.
|
|
|
|
# pylint: disable=consider-using-enumerate
|
2018-10-19 11:27:10 +02:00
|
|
|
for i in range(len(arguments)):
|
|
|
|
for value in argument_lists[i][1:]:
|
|
|
|
arguments[i] = value
|
2019-05-27 18:29:47 +02:00
|
|
|
yield self._format_arguments(name, arguments)
|
2018-10-19 11:29:56 +02:00
|
|
|
arguments[i] = argument_lists[0][0]
|
2018-10-19 11:27:10 +02:00
|
|
|
except BaseException as e:
|
|
|
|
raise Exception('distribute_arguments({})'.format(name)) from e
|
2018-09-25 18:49:23 +02:00
|
|
|
|
2019-11-21 16:46:51 +01:00
|
|
|
def generate_expressions(self, names):
|
|
|
|
return itertools.chain(*map(self.distribute_arguments, names))
|
|
|
|
|
2019-05-27 18:29:47 +02:00
|
|
|
_argument_split_re = re.compile(r' *, *')
|
|
|
|
@classmethod
|
|
|
|
def _argument_split(cls, arguments):
|
|
|
|
return re.split(cls._argument_split_re, arguments)
|
|
|
|
|
2018-09-25 18:49:23 +02:00
|
|
|
# Regex for interesting header lines.
|
|
|
|
# Groups: 1=macro name, 2=type, 3=argument list (optional).
|
2019-05-27 18:29:47 +02:00
|
|
|
_header_line_re = \
|
2018-09-25 18:49:23 +02:00
|
|
|
re.compile(r'#define +' +
|
2019-11-21 18:58:36 +01:00
|
|
|
r'(PSA_((?:(?:DH|ECC|KEY)_)?[A-Z]+)_\w+)' +
|
2018-09-25 18:49:23 +02:00
|
|
|
r'(?:\(([^\n()]*)\))?')
|
|
|
|
# Regex of macro names to exclude.
|
2019-05-27 18:29:47 +02:00
|
|
|
_excluded_name_re = re.compile(r'_(?:GET|IS|OF)_|_(?:BASE|FLAG|MASK)\Z')
|
2018-10-19 11:31:52 +02:00
|
|
|
# Additional excluded macros.
|
2019-05-17 12:04:41 +02:00
|
|
|
_excluded_names = set([
|
|
|
|
# Macros that provide an alternative way to build the same
|
|
|
|
# algorithm as another macro.
|
2020-12-16 11:36:46 +01:00
|
|
|
'PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG',
|
2019-05-17 12:04:41 +02:00
|
|
|
'PSA_ALG_FULL_LENGTH_MAC',
|
|
|
|
# Auxiliary macro whose name doesn't fit the usual patterns for
|
|
|
|
# auxiliary macros.
|
2020-12-16 11:36:46 +01:00
|
|
|
'PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG_CASE',
|
2019-05-17 12:04:41 +02:00
|
|
|
])
|
2018-09-25 18:49:23 +02:00
|
|
|
def parse_header_line(self, line):
|
2019-06-03 11:23:56 +02:00
|
|
|
"""Parse a C header line, looking for "#define PSA_xxx"."""
|
2019-05-27 18:29:47 +02:00
|
|
|
m = re.match(self._header_line_re, line)
|
2018-09-25 18:49:23 +02:00
|
|
|
if not m:
|
|
|
|
return
|
|
|
|
name = m.group(1)
|
2019-11-21 19:49:26 +01:00
|
|
|
self.all_declared.add(name)
|
2019-05-27 18:29:47 +02:00
|
|
|
if re.search(self._excluded_name_re, name) or \
|
|
|
|
name in self._excluded_names:
|
2018-09-25 18:49:23 +02:00
|
|
|
return
|
|
|
|
dest = self.table_by_prefix.get(m.group(2))
|
|
|
|
if dest is None:
|
|
|
|
return
|
|
|
|
dest.add(name)
|
|
|
|
if m.group(3):
|
2019-05-27 18:29:47 +02:00
|
|
|
self.argspecs[name] = self._argument_split(m.group(3))
|
2018-09-25 18:49:23 +02:00
|
|
|
|
2019-12-06 19:20:13 +01:00
|
|
|
_nonascii_re = re.compile(rb'[^\x00-\x7f]+')
|
2018-09-25 18:49:23 +02:00
|
|
|
def parse_header(self, filename):
|
2019-06-03 11:23:56 +02:00
|
|
|
"""Parse a C header file, looking for "#define PSA_xxx"."""
|
2019-12-06 19:20:13 +01:00
|
|
|
with read_file_lines(filename, binary=True) as lines:
|
2018-10-19 11:27:10 +02:00
|
|
|
for line in lines:
|
2019-12-06 19:20:13 +01:00
|
|
|
line = re.sub(self._nonascii_re, rb'', line).decode('ascii')
|
2018-09-25 18:49:23 +02:00
|
|
|
self.parse_header_line(line)
|
|
|
|
|
2019-12-06 19:20:13 +01:00
|
|
|
_macro_identifier_re = re.compile(r'[A-Z]\w+')
|
2019-11-21 19:49:26 +01:00
|
|
|
def generate_undeclared_names(self, expr):
|
|
|
|
for name in re.findall(self._macro_identifier_re, expr):
|
|
|
|
if name not in self.all_declared:
|
|
|
|
yield name
|
|
|
|
|
|
|
|
def accept_test_case_line(self, function, argument):
|
|
|
|
#pylint: disable=unused-argument
|
|
|
|
undeclared = list(self.generate_undeclared_names(argument))
|
|
|
|
if undeclared:
|
|
|
|
raise Exception('Undeclared names in test case', undeclared)
|
|
|
|
return True
|
|
|
|
|
2018-09-25 18:49:23 +02:00
|
|
|
def add_test_case_line(self, function, argument):
|
2019-06-03 11:23:56 +02:00
|
|
|
"""Parse a test case data line, looking for algorithm metadata tests."""
|
2019-11-21 19:22:45 +01:00
|
|
|
sets = []
|
2018-09-25 18:49:23 +02:00
|
|
|
if function.endswith('_algorithm'):
|
2019-11-21 19:22:45 +01:00
|
|
|
sets.append(self.algorithms)
|
2019-11-21 20:08:10 +01:00
|
|
|
if function == 'key_agreement_algorithm' and \
|
|
|
|
argument.startswith('PSA_ALG_KEY_AGREEMENT('):
|
|
|
|
# We only want *raw* key agreement algorithms as such, so
|
|
|
|
# exclude ones that are already chained with a KDF.
|
|
|
|
# Keep the expression as one to test as an algorithm.
|
|
|
|
function = 'other_algorithm'
|
2019-11-25 17:10:12 +01:00
|
|
|
sets += self.table_by_test_function[function]
|
2019-11-21 19:49:26 +01:00
|
|
|
if self.accept_test_case_line(function, argument):
|
|
|
|
for s in sets:
|
|
|
|
s.add(argument)
|
2018-09-25 18:49:23 +02:00
|
|
|
|
|
|
|
# Regex matching a *.data line containing a test function call and
|
|
|
|
# its arguments. The actual definition is partly positional, but this
|
|
|
|
# regex is good enough in practice.
|
2019-05-27 18:29:47 +02:00
|
|
|
_test_case_line_re = re.compile(r'(?!depends_on:)(\w+):([^\n :][^:\n]*)')
|
2018-09-25 18:49:23 +02:00
|
|
|
def parse_test_cases(self, filename):
|
2019-06-03 11:23:56 +02:00
|
|
|
"""Parse a test case file (*.data), looking for algorithm metadata tests."""
|
2018-10-19 11:27:10 +02:00
|
|
|
with read_file_lines(filename) as lines:
|
|
|
|
for line in lines:
|
2019-05-27 18:29:47 +02:00
|
|
|
m = re.match(self._test_case_line_re, line)
|
2018-09-25 18:49:23 +02:00
|
|
|
if m:
|
|
|
|
self.add_test_case_line(m.group(1), m.group(2))
|
|
|
|
|
2019-11-21 19:50:33 +01:00
|
|
|
def gather_inputs(headers, test_suites, inputs_class=Inputs):
|
2019-06-03 11:23:56 +02:00
|
|
|
"""Read the list of inputs to test psa_constant_names with."""
|
2019-11-21 19:50:33 +01:00
|
|
|
inputs = inputs_class()
|
2018-09-25 18:49:23 +02:00
|
|
|
for header in headers:
|
|
|
|
inputs.parse_header(header)
|
|
|
|
for test_cases in test_suites:
|
|
|
|
inputs.parse_test_cases(test_cases)
|
|
|
|
inputs.gather_arguments()
|
|
|
|
return inputs
|
|
|
|
|
2019-11-21 17:26:10 +01:00
|
|
|
def run_c(type_word, expressions, include_path=None, keep_c=False):
|
2021-01-19 21:19:02 +01:00
|
|
|
"""Generate and run a program to print out numerical values of C expressions."""
|
2019-05-27 18:29:47 +02:00
|
|
|
if type_word == 'status':
|
2019-02-13 18:42:53 +01:00
|
|
|
cast_to = 'long'
|
|
|
|
printf_format = '%ld'
|
|
|
|
else:
|
|
|
|
cast_to = 'unsigned long'
|
|
|
|
printf_format = '0x%08lx'
|
2020-12-11 00:30:53 +01:00
|
|
|
return c_build_helper.get_c_expression_values(
|
2020-12-11 00:27:14 +01:00
|
|
|
cast_to, printf_format,
|
|
|
|
expressions,
|
|
|
|
caller='test_psa_constant_names.py for {} values'.format(type_word),
|
|
|
|
file_label=type_word,
|
|
|
|
header='#include <psa/crypto.h>',
|
|
|
|
include_path=include_path,
|
|
|
|
keep_c=keep_c
|
|
|
|
)
|
2018-09-25 18:49:23 +02:00
|
|
|
|
2019-05-27 18:29:47 +02:00
|
|
|
NORMALIZE_STRIP_RE = re.compile(r'\s+')
|
2018-09-25 18:49:23 +02:00
|
|
|
def normalize(expr):
|
2019-06-03 11:23:56 +02:00
|
|
|
"""Normalize the C expression so as not to care about trivial differences.
|
2019-11-21 17:16:21 +01:00
|
|
|
|
2019-06-03 11:23:56 +02:00
|
|
|
Currently "trivial differences" means whitespace.
|
|
|
|
"""
|
2019-11-21 16:48:07 +01:00
|
|
|
return re.sub(NORMALIZE_STRIP_RE, '', expr)
|
2018-09-25 18:49:23 +02:00
|
|
|
|
2019-11-21 17:26:10 +01:00
|
|
|
def collect_values(inputs, type_word, include_path=None, keep_c=False):
|
2019-11-21 17:17:39 +01:00
|
|
|
"""Generate expressions using known macro names and calculate their values.
|
|
|
|
|
|
|
|
Return a list of pairs of (expr, value) where expr is an expression and
|
|
|
|
value is a string representation of its integer value.
|
|
|
|
"""
|
|
|
|
names = inputs.get_names(type_word)
|
|
|
|
expressions = sorted(inputs.generate_expressions(names))
|
2019-11-21 17:26:10 +01:00
|
|
|
values = run_c(type_word, expressions,
|
|
|
|
include_path=include_path, keep_c=keep_c)
|
2019-11-21 17:17:39 +01:00
|
|
|
return expressions, values
|
|
|
|
|
2019-11-21 17:44:21 +01:00
|
|
|
class Tests:
|
|
|
|
"""An object representing tests and their results."""
|
2019-11-21 17:16:21 +01:00
|
|
|
|
2019-11-21 17:51:11 +01:00
|
|
|
Error = namedtuple('Error',
|
|
|
|
['type', 'expression', 'value', 'output'])
|
|
|
|
|
2019-11-21 17:44:21 +01:00
|
|
|
def __init__(self, options):
|
|
|
|
self.options = options
|
|
|
|
self.count = 0
|
|
|
|
self.errors = []
|
2019-11-21 17:16:21 +01:00
|
|
|
|
2019-11-21 17:44:21 +01:00
|
|
|
def run_one(self, inputs, type_word):
|
|
|
|
"""Test psa_constant_names for the specified type.
|
|
|
|
|
|
|
|
Run the program on the names for this type.
|
|
|
|
Use the inputs to figure out what arguments to pass to macros that
|
|
|
|
take arguments.
|
|
|
|
"""
|
|
|
|
expressions, values = collect_values(inputs, type_word,
|
|
|
|
include_path=self.options.include,
|
|
|
|
keep_c=self.options.keep_c)
|
|
|
|
output = subprocess.check_output([self.options.program, type_word] +
|
|
|
|
values)
|
|
|
|
outputs = output.decode('ascii').strip().split('\n')
|
|
|
|
self.count += len(expressions)
|
|
|
|
for expr, value, output in zip(expressions, values, outputs):
|
2019-12-03 19:03:35 +01:00
|
|
|
if self.options.show:
|
|
|
|
sys.stdout.write('{} {}\t{}\n'.format(type_word, value, output))
|
2019-11-21 17:44:21 +01:00
|
|
|
if normalize(expr) != normalize(output):
|
2019-11-21 17:51:11 +01:00
|
|
|
self.errors.append(self.Error(type=type_word,
|
|
|
|
expression=expr,
|
|
|
|
value=value,
|
|
|
|
output=output))
|
2019-11-21 17:44:21 +01:00
|
|
|
|
|
|
|
def run_all(self, inputs):
|
|
|
|
"""Run psa_constant_names on all the gathered inputs."""
|
|
|
|
for type_word in ['status', 'algorithm', 'ecc_curve', 'dh_group',
|
|
|
|
'key_type', 'key_usage']:
|
|
|
|
self.run_one(inputs, type_word)
|
|
|
|
|
|
|
|
def report(self, out):
|
|
|
|
"""Describe each case where the output is not as expected.
|
|
|
|
|
|
|
|
Write the errors to ``out``.
|
|
|
|
Also write a total.
|
|
|
|
"""
|
2019-11-21 17:51:11 +01:00
|
|
|
for error in self.errors:
|
2019-11-21 17:44:21 +01:00
|
|
|
out.write('For {} "{}", got "{}" (value: {})\n'
|
2019-11-21 17:51:11 +01:00
|
|
|
.format(error.type, error.expression,
|
|
|
|
error.output, error.value))
|
2019-11-21 17:44:21 +01:00
|
|
|
out.write('{} test cases'.format(self.count))
|
|
|
|
if self.errors:
|
|
|
|
out.write(', {} FAIL\n'.format(len(self.errors)))
|
|
|
|
else:
|
|
|
|
out.write(' PASS\n')
|
2018-09-25 18:49:23 +02:00
|
|
|
|
2019-11-21 16:49:50 +01:00
|
|
|
HEADERS = ['psa/crypto.h', 'psa/crypto_extra.h', 'psa/crypto_values.h']
|
|
|
|
TEST_SUITES = ['tests/suites/test_suite_psa_crypto_metadata.data']
|
|
|
|
|
2019-05-27 18:31:59 +02:00
|
|
|
def main():
|
2018-09-25 18:49:23 +02:00
|
|
|
parser = argparse.ArgumentParser(description=globals()['__doc__'])
|
|
|
|
parser.add_argument('--include', '-I',
|
|
|
|
action='append', default=['include'],
|
|
|
|
help='Directory for header files')
|
2018-10-19 11:28:42 +02:00
|
|
|
parser.add_argument('--keep-c',
|
|
|
|
action='store_true', dest='keep_c', default=False,
|
|
|
|
help='Keep the intermediate C file')
|
|
|
|
parser.add_argument('--no-keep-c',
|
|
|
|
action='store_false', dest='keep_c',
|
|
|
|
help='Don\'t keep the intermediate C file (default)')
|
2019-11-21 16:49:10 +01:00
|
|
|
parser.add_argument('--program',
|
|
|
|
default='programs/psa/psa_constant_names',
|
|
|
|
help='Program to test')
|
2019-12-03 19:03:35 +01:00
|
|
|
parser.add_argument('--show',
|
|
|
|
action='store_true',
|
|
|
|
help='Keep the intermediate C file')
|
|
|
|
parser.add_argument('--no-show',
|
|
|
|
action='store_false', dest='show',
|
|
|
|
help='Don\'t show tested values (default)')
|
2018-09-25 18:49:23 +02:00
|
|
|
options = parser.parse_args()
|
2019-11-21 16:49:50 +01:00
|
|
|
headers = [os.path.join(options.include[0], h) for h in HEADERS]
|
|
|
|
inputs = gather_inputs(headers, TEST_SUITES)
|
2019-11-21 17:44:21 +01:00
|
|
|
tests = Tests(options)
|
|
|
|
tests.run_all(inputs)
|
|
|
|
tests.report(sys.stdout)
|
|
|
|
if tests.errors:
|
2020-03-24 18:36:56 +01:00
|
|
|
sys.exit(1)
|
2019-05-27 18:31:59 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|