2018-09-25 16:49:23 +00:00
|
|
|
#!/usr/bin/env python3
|
2019-06-03 09:23:56 +00:00
|
|
|
"""Test the program psa_constant_names.
|
2018-09-25 16:49:23 +00: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 09:23:56 +00:00
|
|
|
or 1 (with a Python backtrace) if there was an operational error.
|
|
|
|
"""
|
2018-09-25 16:49:23 +00:00
|
|
|
|
2020-08-07 11:07:28 +00:00
|
|
|
# Copyright The Mbed TLS Contributors
|
2020-05-25 23:54:15 +00: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-25 22:33:31 +00:00
|
|
|
|
2018-09-25 16:49:23 +00:00
|
|
|
import argparse
|
2019-11-21 16:51:11 +00:00
|
|
|
from collections import namedtuple
|
2018-09-25 16:49:23 +00:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2021-03-30 17:09:05 +00:00
|
|
|
from typing import Iterable, List, Optional, Tuple
|
2020-12-10 23:30:53 +00:00
|
|
|
|
|
|
|
import scripts_path # pylint: disable=unused-import
|
|
|
|
from mbedtls_dev import c_build_helper
|
2021-03-30 17:09:05 +00:00
|
|
|
from mbedtls_dev.macro_collector import InputsForTest, PSAMacroEnumerator
|
2021-03-29 18:37:40 +00:00
|
|
|
from mbedtls_dev import typing_util
|
2021-03-09 23:50:18 +00:00
|
|
|
|
2021-03-29 18:37:40 +00:00
|
|
|
def gather_inputs(headers: Iterable[str],
|
|
|
|
test_suites: Iterable[str],
|
|
|
|
inputs_class=InputsForTest) -> PSAMacroEnumerator:
|
2019-06-03 09:23:56 +00:00
|
|
|
"""Read the list of inputs to test psa_constant_names with."""
|
2019-11-21 18:50:33 +00:00
|
|
|
inputs = inputs_class()
|
2018-09-25 16:49:23 +00:00
|
|
|
for header in headers:
|
|
|
|
inputs.parse_header(header)
|
|
|
|
for test_cases in test_suites:
|
|
|
|
inputs.parse_test_cases(test_cases)
|
2021-03-30 19:46:35 +00:00
|
|
|
inputs.add_numerical_values()
|
2018-09-25 16:49:23 +00:00
|
|
|
inputs.gather_arguments()
|
|
|
|
return inputs
|
|
|
|
|
2021-03-29 18:37:40 +00:00
|
|
|
def run_c(type_word: str,
|
|
|
|
expressions: Iterable[str],
|
|
|
|
include_path: Optional[str] = None,
|
|
|
|
keep_c: bool = False) -> List[str]:
|
2021-01-19 20:19:02 +00:00
|
|
|
"""Generate and run a program to print out numerical values of C expressions."""
|
2019-05-27 16:29:47 +00:00
|
|
|
if type_word == 'status':
|
2019-02-13 17:42:53 +00:00
|
|
|
cast_to = 'long'
|
|
|
|
printf_format = '%ld'
|
|
|
|
else:
|
|
|
|
cast_to = 'unsigned long'
|
|
|
|
printf_format = '0x%08lx'
|
2020-12-10 23:30:53 +00:00
|
|
|
return c_build_helper.get_c_expression_values(
|
2020-12-10 23:27:14 +00: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 16:49:23 +00:00
|
|
|
|
2019-05-27 16:29:47 +00:00
|
|
|
NORMALIZE_STRIP_RE = re.compile(r'\s+')
|
2021-03-29 18:37:40 +00:00
|
|
|
def normalize(expr: str) -> str:
|
2019-06-03 09:23:56 +00:00
|
|
|
"""Normalize the C expression so as not to care about trivial differences.
|
2019-11-21 16:16:21 +00:00
|
|
|
|
2019-06-03 09:23:56 +00:00
|
|
|
Currently "trivial differences" means whitespace.
|
|
|
|
"""
|
2019-11-21 15:48:07 +00:00
|
|
|
return re.sub(NORMALIZE_STRIP_RE, '', expr)
|
2018-09-25 16:49:23 +00:00
|
|
|
|
2021-03-29 18:37:40 +00:00
|
|
|
def collect_values(inputs: InputsForTest,
|
|
|
|
type_word: str,
|
|
|
|
include_path: Optional[str] = None,
|
|
|
|
keep_c: bool = False) -> Tuple[List[str], List[str]]:
|
2019-11-21 16:17:39 +00: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 16:26:10 +00:00
|
|
|
values = run_c(type_word, expressions,
|
|
|
|
include_path=include_path, keep_c=keep_c)
|
2019-11-21 16:17:39 +00:00
|
|
|
return expressions, values
|
|
|
|
|
2019-11-21 16:44:21 +00:00
|
|
|
class Tests:
|
|
|
|
"""An object representing tests and their results."""
|
2019-11-21 16:16:21 +00:00
|
|
|
|
2019-11-21 16:51:11 +00:00
|
|
|
Error = namedtuple('Error',
|
|
|
|
['type', 'expression', 'value', 'output'])
|
|
|
|
|
2021-03-29 18:37:40 +00:00
|
|
|
def __init__(self, options) -> None:
|
2019-11-21 16:44:21 +00:00
|
|
|
self.options = options
|
|
|
|
self.count = 0
|
2021-03-29 18:37:40 +00:00
|
|
|
self.errors = [] #type: List[Tests.Error]
|
2019-11-21 16:16:21 +00:00
|
|
|
|
2021-03-29 18:37:40 +00:00
|
|
|
def run_one(self, inputs: InputsForTest, type_word: str) -> None:
|
2019-11-21 16:44:21 +00:00
|
|
|
"""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)
|
2021-03-29 18:37:40 +00:00
|
|
|
output_bytes = subprocess.check_output([self.options.program,
|
|
|
|
type_word] + values)
|
|
|
|
output = output_bytes.decode('ascii')
|
|
|
|
outputs = output.strip().split('\n')
|
2019-11-21 16:44:21 +00:00
|
|
|
self.count += len(expressions)
|
|
|
|
for expr, value, output in zip(expressions, values, outputs):
|
2019-12-03 18:03:35 +00:00
|
|
|
if self.options.show:
|
|
|
|
sys.stdout.write('{} {}\t{}\n'.format(type_word, value, output))
|
2019-11-21 16:44:21 +00:00
|
|
|
if normalize(expr) != normalize(output):
|
2019-11-21 16:51:11 +00:00
|
|
|
self.errors.append(self.Error(type=type_word,
|
|
|
|
expression=expr,
|
|
|
|
value=value,
|
|
|
|
output=output))
|
2019-11-21 16:44:21 +00:00
|
|
|
|
2021-03-29 18:37:40 +00:00
|
|
|
def run_all(self, inputs: InputsForTest) -> None:
|
2019-11-21 16:44:21 +00:00
|
|
|
"""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)
|
|
|
|
|
2021-03-29 18:37:40 +00:00
|
|
|
def report(self, out: typing_util.Writable) -> None:
|
2019-11-21 16:44:21 +00:00
|
|
|
"""Describe each case where the output is not as expected.
|
|
|
|
|
|
|
|
Write the errors to ``out``.
|
|
|
|
Also write a total.
|
|
|
|
"""
|
2019-11-21 16:51:11 +00:00
|
|
|
for error in self.errors:
|
2019-11-21 16:44:21 +00:00
|
|
|
out.write('For {} "{}", got "{}" (value: {})\n'
|
2019-11-21 16:51:11 +00:00
|
|
|
.format(error.type, error.expression,
|
|
|
|
error.output, error.value))
|
2019-11-21 16:44:21 +00: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 16:49:23 +00:00
|
|
|
|
2019-11-21 15:49:50 +00: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 16:31:59 +00:00
|
|
|
def main():
|
2018-09-25 16:49:23 +00:00
|
|
|
parser = argparse.ArgumentParser(description=globals()['__doc__'])
|
|
|
|
parser.add_argument('--include', '-I',
|
|
|
|
action='append', default=['include'],
|
|
|
|
help='Directory for header files')
|
2018-10-19 09:28:42 +00: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 15:49:10 +00:00
|
|
|
parser.add_argument('--program',
|
|
|
|
default='programs/psa/psa_constant_names',
|
|
|
|
help='Program to test')
|
2019-12-03 18:03:35 +00:00
|
|
|
parser.add_argument('--show',
|
|
|
|
action='store_true',
|
2021-03-17 12:45:32 +00:00
|
|
|
help='Show tested values on stdout')
|
2019-12-03 18:03:35 +00:00
|
|
|
parser.add_argument('--no-show',
|
|
|
|
action='store_false', dest='show',
|
|
|
|
help='Don\'t show tested values (default)')
|
2018-09-25 16:49:23 +00:00
|
|
|
options = parser.parse_args()
|
2019-11-21 15:49:50 +00:00
|
|
|
headers = [os.path.join(options.include[0], h) for h in HEADERS]
|
|
|
|
inputs = gather_inputs(headers, TEST_SUITES)
|
2019-11-21 16:44:21 +00:00
|
|
|
tests = Tests(options)
|
|
|
|
tests.run_all(inputs)
|
|
|
|
tests.report(sys.stdout)
|
|
|
|
if tests.errors:
|
2020-03-24 17:36:56 +00:00
|
|
|
sys.exit(1)
|
2019-05-27 16:31:59 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|