2020-06-25 18:36:28 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
"""Analyze the test outcomes from a full CI run.
|
|
|
|
|
|
|
|
This script can also run on outcomes from a partial run, but the results are
|
|
|
|
less likely to be useful.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import sys
|
|
|
|
import traceback
|
2022-11-17 11:50:23 +01:00
|
|
|
import re
|
2023-03-24 08:20:18 +01:00
|
|
|
import subprocess
|
|
|
|
import os
|
2020-06-25 18:36:28 +02:00
|
|
|
|
2020-06-25 18:37:43 +02:00
|
|
|
import check_test_cases
|
|
|
|
|
2020-06-25 18:36:28 +02:00
|
|
|
class Results:
|
|
|
|
"""Process analysis results."""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.error_count = 0
|
|
|
|
self.warning_count = 0
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def log(fmt, *args, **kwargs):
|
|
|
|
sys.stderr.write((fmt + '\n').format(*args, **kwargs))
|
|
|
|
|
|
|
|
def error(self, fmt, *args, **kwargs):
|
|
|
|
self.log('Error: ' + fmt, *args, **kwargs)
|
|
|
|
self.error_count += 1
|
|
|
|
|
|
|
|
def warning(self, fmt, *args, **kwargs):
|
|
|
|
self.log('Warning: ' + fmt, *args, **kwargs)
|
|
|
|
self.warning_count += 1
|
|
|
|
|
|
|
|
class TestCaseOutcomes:
|
|
|
|
"""The outcomes of one test case across many configurations."""
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
|
|
|
|
def __init__(self):
|
2020-06-26 13:02:30 +02:00
|
|
|
# Collect a list of witnesses of the test case succeeding or failing.
|
|
|
|
# Currently we don't do anything with witnesses except count them.
|
|
|
|
# The format of a witness is determined by the read_outcome_file
|
|
|
|
# function; it's the platform and configuration joined by ';'.
|
2020-06-25 18:36:28 +02:00
|
|
|
self.successes = []
|
|
|
|
self.failures = []
|
|
|
|
|
|
|
|
def hits(self):
|
|
|
|
"""Return the number of times a test case has been run.
|
|
|
|
|
|
|
|
This includes passes and failures, but not skips.
|
|
|
|
"""
|
|
|
|
return len(self.successes) + len(self.failures)
|
|
|
|
|
2023-03-24 08:20:18 +01:00
|
|
|
def execute_reference_driver_tests(ref_component, driver_component, outcome_file):
|
2023-03-29 11:15:28 +02:00
|
|
|
"""Run the tests specified in ref_component and driver_component. Results
|
|
|
|
are stored in the output_file and they will be used for the following
|
2023-03-24 08:20:18 +01:00
|
|
|
coverage analysis"""
|
|
|
|
# If the outcome file already exists, we assume that the user wants to
|
|
|
|
# perform the comparison analysis again without repeating the tests.
|
|
|
|
if os.path.exists(outcome_file):
|
|
|
|
Results.log("Outcome file (" + outcome_file + ") already exists. " + \
|
|
|
|
"Tests will be skipped.")
|
|
|
|
return
|
|
|
|
|
|
|
|
shell_command = "tests/scripts/all.sh --outcome-file " + outcome_file + \
|
|
|
|
" " + ref_component + " " + driver_component
|
2023-03-29 11:15:44 +02:00
|
|
|
Results.log("Running: " + shell_command)
|
2023-03-24 08:20:18 +01:00
|
|
|
ret_val = subprocess.run(shell_command.split(), check=False).returncode
|
|
|
|
|
|
|
|
if ret_val != 0:
|
|
|
|
Results.log("Error: failed to run reference/driver components")
|
|
|
|
sys.exit(ret_val)
|
|
|
|
|
2020-06-25 18:37:43 +02:00
|
|
|
def analyze_coverage(results, outcomes):
|
|
|
|
"""Check that all available test cases are executed at least once."""
|
2022-01-07 15:58:38 +01:00
|
|
|
available = check_test_cases.collect_available_test_cases()
|
2020-06-25 18:37:43 +02:00
|
|
|
for key in available:
|
|
|
|
hits = outcomes[key].hits() if key in outcomes else 0
|
|
|
|
if hits == 0:
|
|
|
|
# Make this a warning, not an error, as long as we haven't
|
|
|
|
# fixed this branch to have full coverage of test cases.
|
|
|
|
results.warning('Test case not executed: {}', key)
|
|
|
|
|
2023-01-18 17:28:36 +01:00
|
|
|
def analyze_driver_vs_reference(outcomes, component_ref, component_driver,
|
|
|
|
ignored_suites, ignored_test=None):
|
2022-10-21 13:42:08 +02:00
|
|
|
"""Check that all tests executed in the reference component are also
|
|
|
|
executed in the corresponding driver component.
|
2023-01-18 17:28:36 +01:00
|
|
|
Skip:
|
|
|
|
- full test suites provided in ignored_suites list
|
|
|
|
- only some specific test inside a test suite, for which the corresponding
|
|
|
|
output string is provided
|
2022-10-21 13:42:08 +02:00
|
|
|
"""
|
|
|
|
available = check_test_cases.collect_available_test_cases()
|
|
|
|
result = True
|
|
|
|
|
|
|
|
for key in available:
|
|
|
|
# Continue if test was not executed by any component
|
|
|
|
hits = outcomes[key].hits() if key in outcomes else 0
|
2022-10-24 09:16:04 +02:00
|
|
|
if hits == 0:
|
2022-10-21 13:42:08 +02:00
|
|
|
continue
|
2023-02-02 11:33:31 +01:00
|
|
|
# Skip ignored test suites
|
|
|
|
full_test_suite = key.split(';')[0] # retrieve full test suite name
|
|
|
|
test_string = key.split(';')[1] # retrieve the text string of this test
|
|
|
|
test_suite = full_test_suite.split('.')[0] # retrieve main part of test suite name
|
2023-03-17 15:13:08 +01:00
|
|
|
if test_suite in ignored_suites or full_test_suite in ignored_suites:
|
2023-02-02 11:33:31 +01:00
|
|
|
continue
|
2023-01-18 17:28:36 +01:00
|
|
|
if ((full_test_suite in ignored_test) and
|
|
|
|
(test_string in ignored_test[full_test_suite])):
|
|
|
|
continue
|
2022-10-21 13:42:08 +02:00
|
|
|
# Search for tests that run in reference component and not in driver component
|
|
|
|
driver_test_passed = False
|
|
|
|
reference_test_passed = False
|
|
|
|
for entry in outcomes[key].successes:
|
2022-11-09 12:07:29 +01:00
|
|
|
if component_driver in entry:
|
2022-10-21 13:42:08 +02:00
|
|
|
driver_test_passed = True
|
2022-11-09 12:07:29 +01:00
|
|
|
if component_ref in entry:
|
2022-10-21 13:42:08 +02:00
|
|
|
reference_test_passed = True
|
2022-12-30 13:40:34 +01:00
|
|
|
if(reference_test_passed and not driver_test_passed):
|
2023-03-13 18:37:34 +01:00
|
|
|
Results.log(key)
|
2022-10-21 13:42:08 +02:00
|
|
|
result = False
|
|
|
|
return result
|
|
|
|
|
2020-06-25 18:36:28 +02:00
|
|
|
def analyze_outcomes(outcomes):
|
|
|
|
"""Run all analyses on the given outcome collection."""
|
|
|
|
results = Results()
|
2020-06-25 18:37:43 +02:00
|
|
|
analyze_coverage(results, outcomes)
|
2020-06-25 18:36:28 +02:00
|
|
|
return results
|
|
|
|
|
|
|
|
def read_outcome_file(outcome_file):
|
|
|
|
"""Parse an outcome file and return an outcome collection.
|
|
|
|
|
|
|
|
An outcome collection is a dictionary mapping keys to TestCaseOutcomes objects.
|
|
|
|
The keys are the test suite name and the test case description, separated
|
|
|
|
by a semicolon.
|
|
|
|
"""
|
|
|
|
outcomes = {}
|
|
|
|
with open(outcome_file, 'r', encoding='utf-8') as input_file:
|
|
|
|
for line in input_file:
|
|
|
|
(platform, config, suite, case, result, _cause) = line.split(';')
|
|
|
|
key = ';'.join([suite, case])
|
|
|
|
setup = ';'.join([platform, config])
|
|
|
|
if key not in outcomes:
|
|
|
|
outcomes[key] = TestCaseOutcomes()
|
|
|
|
if result == 'PASS':
|
|
|
|
outcomes[key].successes.append(setup)
|
|
|
|
elif result == 'FAIL':
|
|
|
|
outcomes[key].failures.append(setup)
|
|
|
|
return outcomes
|
|
|
|
|
2022-10-26 16:11:26 +02:00
|
|
|
def do_analyze_coverage(outcome_file, args):
|
2022-11-09 10:50:29 +01:00
|
|
|
"""Perform coverage analysis."""
|
2022-10-26 16:11:26 +02:00
|
|
|
del args # unused
|
2020-06-25 18:36:28 +02:00
|
|
|
outcomes = read_outcome_file(outcome_file)
|
2023-03-13 18:37:34 +01:00
|
|
|
Results.log("\n*** Analyze coverage ***\n")
|
2022-10-21 13:42:08 +02:00
|
|
|
results = analyze_outcomes(outcomes)
|
2022-10-24 09:16:04 +02:00
|
|
|
return results.error_count == 0
|
2022-10-21 13:42:08 +02:00
|
|
|
|
2022-10-26 16:11:26 +02:00
|
|
|
def do_analyze_driver_vs_reference(outcome_file, args):
|
2022-10-21 13:42:08 +02:00
|
|
|
"""Perform driver vs reference analyze."""
|
2023-03-24 08:20:18 +01:00
|
|
|
execute_reference_driver_tests(args['component_ref'], \
|
|
|
|
args['component_driver'], outcome_file)
|
|
|
|
|
2023-01-18 17:28:36 +01:00
|
|
|
ignored_suites = ['test_suite_' + x for x in args['ignored_suites']]
|
2022-11-09 12:07:29 +01:00
|
|
|
|
2022-10-21 13:42:08 +02:00
|
|
|
outcomes = read_outcome_file(outcome_file)
|
2023-03-13 18:37:34 +01:00
|
|
|
Results.log("\n*** Analyze driver {} vs reference {} ***\n".format(
|
2022-12-30 13:40:34 +01:00
|
|
|
args['component_driver'], args['component_ref']))
|
2022-11-09 12:07:29 +01:00
|
|
|
return analyze_driver_vs_reference(outcomes, args['component_ref'],
|
2023-01-18 17:28:36 +01:00
|
|
|
args['component_driver'], ignored_suites,
|
|
|
|
args['ignored_tests'])
|
2020-06-25 18:36:28 +02:00
|
|
|
|
2022-11-09 10:50:29 +01:00
|
|
|
# List of tasks with a function that can handle this task and additional arguments if required
|
2022-10-26 16:11:26 +02:00
|
|
|
TASKS = {
|
|
|
|
'analyze_coverage': {
|
|
|
|
'test_function': do_analyze_coverage,
|
2022-12-29 12:29:09 +01:00
|
|
|
'args': {}
|
|
|
|
},
|
2023-03-24 08:20:18 +01:00
|
|
|
# There are 2 options to use analyze_driver_vs_reference_xxx locally:
|
|
|
|
# 1. Run tests and then analysis:
|
|
|
|
# - tests/scripts/all.sh --outcome-file "$PWD/out.csv" <component_ref> <component_driver>
|
|
|
|
# - tests/scripts/analyze_outcomes.py out.csv analyze_driver_vs_reference_xxx
|
|
|
|
# 2. Let this script run both automatically:
|
|
|
|
# - tests/scripts/analyze_outcomes.py out.csv analyze_driver_vs_reference_xxx
|
2022-10-26 16:11:26 +02:00
|
|
|
'analyze_driver_vs_reference_hash': {
|
|
|
|
'test_function': do_analyze_driver_vs_reference,
|
|
|
|
'args': {
|
2022-11-09 12:07:29 +01:00
|
|
|
'component_ref': 'test_psa_crypto_config_reference_hash_use_psa',
|
|
|
|
'component_driver': 'test_psa_crypto_config_accel_hash_use_psa',
|
2022-12-29 12:29:09 +01:00
|
|
|
'ignored_suites': [
|
|
|
|
'shax', 'mdx', # the software implementations that are being excluded
|
2023-03-17 15:13:08 +01:00
|
|
|
'md.psa', # purposefully depends on whether drivers are present
|
2023-01-18 17:28:36 +01:00
|
|
|
],
|
|
|
|
'ignored_tests': {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2023-06-14 10:33:10 +02:00
|
|
|
'analyze_driver_vs_reference_ecp_light_only': {
|
2023-03-20 13:54:41 +01:00
|
|
|
'test_function': do_analyze_driver_vs_reference,
|
|
|
|
'args': {
|
2023-06-14 10:33:10 +02:00
|
|
|
'component_ref': 'test_psa_crypto_config_reference_ecc_ecp_light_only',
|
|
|
|
'component_driver': 'test_psa_crypto_config_accel_ecc_ecp_light_only',
|
2023-03-20 13:54:41 +01:00
|
|
|
'ignored_suites': [
|
|
|
|
'ecdsa',
|
|
|
|
'ecdh',
|
|
|
|
'ecjpake',
|
|
|
|
],
|
|
|
|
'ignored_tests': {
|
|
|
|
'test_suite_random': [
|
|
|
|
'PSA classic wrapper: ECDSA signature (SECP256R1)',
|
|
|
|
],
|
2023-04-07 15:54:20 +02:00
|
|
|
# In the accelerated test ECP_C is not set (only ECP_LIGHT is)
|
|
|
|
# so we must ignore disparities in the tests for which ECP_C
|
|
|
|
# is required.
|
|
|
|
'test_suite_ecp': [
|
|
|
|
'ECP check public-private #1 (OK)',
|
|
|
|
'ECP check public-private #2 (group none)',
|
|
|
|
'ECP check public-private #3 (group mismatch)',
|
|
|
|
'ECP check public-private #4 (Qx mismatch)',
|
|
|
|
'ECP check public-private #5 (Qy mismatch)',
|
|
|
|
'ECP check public-private #6 (wrong Qx)',
|
|
|
|
'ECP check public-private #7 (wrong Qy)',
|
|
|
|
'ECP gen keypair [#1]',
|
|
|
|
'ECP gen keypair [#2]',
|
|
|
|
'ECP gen keypair [#3]',
|
|
|
|
'ECP gen keypair wrapper',
|
|
|
|
'ECP point muladd secp256r1 #1',
|
|
|
|
'ECP point muladd secp256r1 #2',
|
|
|
|
'ECP point multiplication Curve25519 (element of order 2: origin) #3',
|
|
|
|
'ECP point multiplication Curve25519 (element of order 4: 1) #4',
|
|
|
|
'ECP point multiplication Curve25519 (element of order 8) #5',
|
|
|
|
'ECP point multiplication Curve25519 (normalized) #1',
|
|
|
|
'ECP point multiplication Curve25519 (not normalized) #2',
|
|
|
|
'ECP point multiplication rng fail Curve25519',
|
|
|
|
'ECP point multiplication rng fail secp256r1',
|
|
|
|
'ECP test vectors Curve25519',
|
|
|
|
'ECP test vectors Curve448 (RFC 7748 6.2, after decodeUCoordinate)',
|
|
|
|
'ECP test vectors brainpoolP256r1 rfc 7027',
|
|
|
|
'ECP test vectors brainpoolP384r1 rfc 7027',
|
|
|
|
'ECP test vectors brainpoolP512r1 rfc 7027',
|
|
|
|
'ECP test vectors secp192k1',
|
|
|
|
'ECP test vectors secp192r1 rfc 5114',
|
|
|
|
'ECP test vectors secp224k1',
|
|
|
|
'ECP test vectors secp224r1 rfc 5114',
|
|
|
|
'ECP test vectors secp256k1',
|
|
|
|
'ECP test vectors secp256r1 rfc 5114',
|
|
|
|
'ECP test vectors secp384r1 rfc 5114',
|
|
|
|
'ECP test vectors secp521r1 rfc 5114',
|
2023-05-19 17:43:06 +02:00
|
|
|
],
|
|
|
|
'test_suite_pkparse': [
|
|
|
|
# This is a known difference for Montgomery curves: in
|
|
|
|
# reference component private keys are parsed using
|
|
|
|
# mbedtls_mpi_read_binary_le(), while in driver version they
|
|
|
|
# they are imported in PSA and there the parsing is done
|
|
|
|
# through mbedtls_ecp_read_key(). Unfortunately the latter
|
|
|
|
# fixes the errors which are intentionally set on the parsed
|
|
|
|
# key and therefore the following test case is not failing
|
|
|
|
# as expected.
|
|
|
|
# This cause the following test to be guarded by ECP_C and
|
|
|
|
# not being executed on the driver version.
|
|
|
|
('Key ASN1 (OneAsymmetricKey X25519, doesn\'t match masking '
|
|
|
|
'requirements, from RFC8410 Appendix A but made into version 0)'),
|
|
|
|
],
|
2023-03-20 13:54:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2023-06-14 10:33:10 +02:00
|
|
|
'analyze_driver_vs_reference_no_ecp_at_all': {
|
2023-04-12 14:59:16 +02:00
|
|
|
'test_function': do_analyze_driver_vs_reference,
|
|
|
|
'args': {
|
2023-06-14 10:33:10 +02:00
|
|
|
'component_ref': 'test_psa_crypto_config_reference_ecc_no_ecp_at_all',
|
|
|
|
'component_driver': 'test_psa_crypto_config_accel_ecc_no_ecp_at_all',
|
2023-04-12 14:59:16 +02:00
|
|
|
'ignored_suites': [
|
|
|
|
# Ignore test suites for the modules that are disabled in the
|
|
|
|
# accelerated test case.
|
|
|
|
'ecp',
|
|
|
|
'ecdsa',
|
|
|
|
'ecdh',
|
|
|
|
'ecjpake',
|
|
|
|
],
|
|
|
|
'ignored_tests': {
|
|
|
|
'test_suite_random': [
|
|
|
|
'PSA classic wrapper: ECDSA signature (SECP256R1)',
|
|
|
|
],
|
|
|
|
'test_suite_psa_crypto': [
|
|
|
|
'PSA key derivation: HKDF-SHA-256 -> ECC secp256r1',
|
|
|
|
'PSA key derivation: HKDF-SHA-256 -> ECC secp256r1 (1 redraw)',
|
|
|
|
'PSA key derivation: HKDF-SHA-256 -> ECC secp256r1, exercise ECDSA',
|
|
|
|
'PSA key derivation: HKDF-SHA-256 -> ECC secp384r1',
|
|
|
|
'PSA key derivation: HKDF-SHA-256 -> ECC secp521r1 #0',
|
|
|
|
'PSA key derivation: HKDF-SHA-256 -> ECC secp521r1 #1',
|
|
|
|
'PSA key derivation: bits=7 invalid for ECC BRAINPOOL_P_R1 (ECC enabled)',
|
|
|
|
'PSA key derivation: bits=7 invalid for ECC SECP_K1 (ECC enabled)',
|
|
|
|
'PSA key derivation: bits=7 invalid for ECC SECP_R1 (ECC enabled)',
|
|
|
|
'PSA key derivation: bits=7 invalid for ECC SECP_R2 (ECC enabled)',
|
|
|
|
'PSA key derivation: bits=7 invalid for ECC SECT_K1 (ECC enabled)',
|
|
|
|
'PSA key derivation: bits=7 invalid for ECC SECT_R1 (ECC enabled)',
|
|
|
|
'PSA key derivation: bits=7 invalid for ECC SECT_R2 (ECC enabled)',
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-10-26 16:11:26 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 18:36:28 +02:00
|
|
|
def main():
|
|
|
|
try:
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
2022-10-24 08:10:10 +02:00
|
|
|
parser.add_argument('outcomes', metavar='OUTCOMES.CSV',
|
2020-06-25 18:36:28 +02:00
|
|
|
help='Outcome file to analyze')
|
2022-11-17 09:43:34 +01:00
|
|
|
parser.add_argument('task', default='all', nargs='?',
|
2022-11-09 13:54:49 +01:00
|
|
|
help='Analysis to be done. By default, run all tasks. '
|
|
|
|
'With one or more TASK, run only those. '
|
|
|
|
'TASK can be the name of a single task or '
|
2022-11-17 11:50:23 +01:00
|
|
|
'comma/space-separated list of tasks. ')
|
2022-11-09 13:54:49 +01:00
|
|
|
parser.add_argument('--list', action='store_true',
|
|
|
|
help='List all available tasks and exit.')
|
2020-06-25 18:36:28 +02:00
|
|
|
options = parser.parse_args()
|
2022-10-21 13:42:08 +02:00
|
|
|
|
2022-11-09 13:54:49 +01:00
|
|
|
if options.list:
|
|
|
|
for task in TASKS:
|
2023-03-13 18:37:34 +01:00
|
|
|
Results.log(task)
|
2022-11-09 13:54:49 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|
2022-10-26 16:11:26 +02:00
|
|
|
result = True
|
2022-10-21 13:42:08 +02:00
|
|
|
|
2022-10-26 16:11:26 +02:00
|
|
|
if options.task == 'all':
|
2022-11-14 16:15:19 +01:00
|
|
|
tasks = TASKS.keys()
|
2022-11-09 13:54:49 +01:00
|
|
|
else:
|
2022-11-17 11:50:23 +01:00
|
|
|
tasks = re.split(r'[, ]+', options.task)
|
2022-11-09 13:54:49 +01:00
|
|
|
|
2022-11-14 16:15:19 +01:00
|
|
|
for task in tasks:
|
|
|
|
if task not in TASKS:
|
2023-03-13 18:37:34 +01:00
|
|
|
Results.log('Error: invalid task: {}'.format(task))
|
2022-11-14 16:15:19 +01:00
|
|
|
sys.exit(1)
|
2022-11-09 13:54:49 +01:00
|
|
|
|
|
|
|
for task in TASKS:
|
|
|
|
if task in tasks:
|
2022-10-26 16:11:26 +02:00
|
|
|
if not TASKS[task]['test_function'](options.outcomes, TASKS[task]['args']):
|
|
|
|
result = False
|
2022-10-21 13:42:08 +02:00
|
|
|
|
2022-10-24 09:16:04 +02:00
|
|
|
if result is False:
|
2020-06-25 18:36:28 +02:00
|
|
|
sys.exit(1)
|
2023-03-13 18:37:34 +01:00
|
|
|
Results.log("SUCCESS :-)")
|
2020-06-25 18:36:28 +02:00
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
# Print the backtrace and exit explicitly with our chosen status.
|
|
|
|
traceback.print_exc()
|
|
|
|
sys.exit(120)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|