2019-04-04 17:05:43 +01:00
|
|
|
"""
|
|
|
|
Checks that files duplicated across schemes/implementations are consistent.
|
|
|
|
"""
|
|
|
|
|
2019-08-23 14:15:45 +01:00
|
|
|
import difflib
|
2019-04-04 17:05:43 +01:00
|
|
|
import os
|
2019-08-23 14:15:45 +01:00
|
|
|
import sys
|
2019-04-04 17:05:43 +01:00
|
|
|
|
2019-07-29 09:38:25 +01:00
|
|
|
import yaml
|
2019-04-18 09:00:08 +01:00
|
|
|
|
2019-07-29 09:38:25 +01:00
|
|
|
import helpers
|
|
|
|
import pqclean
|
2019-04-18 09:00:08 +01:00
|
|
|
|
|
|
|
|
2019-07-29 09:38:25 +01:00
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
ids = []
|
|
|
|
argvalues = []
|
2019-04-04 17:05:43 +01:00
|
|
|
for scheme in pqclean.Scheme.all_schemes():
|
|
|
|
for implementation in scheme.implementations:
|
2019-04-18 09:00:08 +01:00
|
|
|
if os.path.isfile(
|
|
|
|
os.path.join(
|
|
|
|
'duplicate_consistency',
|
|
|
|
'{}_{}.yml'.format(scheme.name, implementation.name))):
|
|
|
|
metafile = os.path.join(
|
|
|
|
'duplicate_consistency',
|
|
|
|
'{}_{}.yml'.format(scheme.name, implementation.name))
|
|
|
|
with open(metafile, encoding='utf-8') as f:
|
|
|
|
metadata = yaml.safe_load(f.read())
|
|
|
|
for group in metadata['consistency_checks']:
|
|
|
|
source = pqclean.Implementation.by_name(
|
|
|
|
group['source']['scheme'],
|
|
|
|
group['source']['implementation'])
|
|
|
|
for file in group['files']:
|
2019-07-29 09:38:25 +01:00
|
|
|
argvalues.append((implementation, source, file))
|
|
|
|
ids.append(
|
2019-08-23 14:15:45 +01:00
|
|
|
"{scheme.name} {implementation.name} {source.scheme.name}: {file}"
|
2019-07-29 09:38:25 +01:00
|
|
|
.format(scheme=scheme, source=source,
|
2019-08-23 14:15:45 +01:00
|
|
|
implementation=implementation,
|
2019-07-29 09:38:25 +01:00
|
|
|
file=file))
|
|
|
|
metafunc.parametrize(('implementation', 'source', 'file'),
|
|
|
|
argvalues,
|
|
|
|
ids=ids)
|
2019-04-18 09:00:08 +01:00
|
|
|
|
2019-04-04 17:05:43 +01:00
|
|
|
|
|
|
|
def file_get_contents(filename):
|
|
|
|
with open(filename) as f:
|
|
|
|
return f.read()
|
|
|
|
|
2019-04-18 09:00:08 +01:00
|
|
|
|
2019-07-29 09:38:25 +01:00
|
|
|
@helpers.filtered_test
|
|
|
|
def test_duplicate_consistency(implementation, source, file):
|
2019-08-23 14:15:45 +01:00
|
|
|
target_path = os.path.join(source.path(), file)
|
|
|
|
this_path = os.path.join(implementation.path(), file)
|
|
|
|
target_src = file_get_contents(target_path)
|
|
|
|
this_src = file_get_contents(this_path)
|
|
|
|
this_transformed_src = this_src.replace(
|
|
|
|
implementation.namespace_prefix(), '')
|
|
|
|
target_transformed_src = target_src.replace(source.namespace_prefix(), '')
|
|
|
|
|
|
|
|
if not this_transformed_src == target_transformed_src:
|
|
|
|
diff = difflib.unified_diff(
|
|
|
|
this_transformed_src.splitlines(keepends=True),
|
|
|
|
target_transformed_src.splitlines(keepends=True),
|
|
|
|
fromfile=this_path,
|
|
|
|
tofile=target_path)
|
|
|
|
raise AssertionError(
|
|
|
|
"Files differed:\n"
|
|
|
|
+ ''.join(diff))
|
2019-04-04 17:05:43 +01:00
|
|
|
|
2019-04-18 09:00:08 +01:00
|
|
|
|
2019-04-04 17:05:43 +01:00
|
|
|
if __name__ == '__main__':
|
2019-07-29 09:38:25 +01:00
|
|
|
import pytest
|
|
|
|
pytest.main(sys.argv)
|