Add duplicate consistency check

This commit is contained in:
Douglas Stebila 2019-04-04 12:05:43 -04:00
parent 72ff0dae94
commit bc5d18e7cd
3 changed files with 68 additions and 4 deletions

View File

@ -70,6 +70,11 @@ class Implementation:
self.scheme = scheme
self.name = name
def metadata(self):
for i in self.scheme.metadata()['implementations']:
if i['name'] == self.name:
return i
def path(self, base='..') -> str:
return os.path.join(self.scheme.path(), self.name)

View File

@ -0,0 +1,40 @@
"""
Checks that files duplicated across schemes/implementations are consistent.
"""
import os
import pqclean
import helpers
def test_duplicate_consistency():
for scheme in pqclean.Scheme.all_schemes():
for implementation in scheme.implementations:
yield check_duplicate_consistency, implementation
def file_get_contents(filename):
with open(filename) as f:
return f.read()
def check_duplicate_consistency(implementation):
print(implementation.metadata())
if 'duplicate-consistency' in implementation.metadata():
dc = implementation.metadata()['duplicate-consistency']
for pairs in dc['files']:
transformed_src = helpers.run_subprocess(
['sed', '-e', 's/{}/{}/g'.format(dc['source_namespace'], dc['target_namespace']), pairs['source_file']],
'..',
)
this_src = file_get_contents(os.path.join(implementation.path(), pairs['target_file']))
print(this_src)
assert(transformed_src == this_src)
if __name__ == '__main__':
try:
import nose2
nose2.main()
except ImportError:
import nose
nose.runmodule()

View File

@ -52,6 +52,24 @@ EXPECTED_FIELDS = {
'spec': {
'name': {'type': str},
'version': {'type': str},
'duplicate-consistency': {
'type': dict,
'optional': True,
'spec': {
'source_namespace': {'type': str},
'target_namespace': {'type': str},
'files': {
'type': list,
'elements': {
'type': dict,
'spec': {
'source_file': {'type': str},
'target_file': {'type': str},
},
},
},
},
},
},
},
},
@ -68,14 +86,15 @@ SIGNATURE_FIELDS = {
def check_spec(metadata, spec):
for field, props in spec:
if field not in metadata:
if field not in metadata and 'optional' not in props:
raise AssertionError("Field '{}' not present.".format(field))
# validate element
check_element(field, metadata[field], props)
if field in metadata:
check_element(field, metadata[field], props)
# delete it to detect extras
del metadata[field]
# delete it to detect extras
del metadata[field]
# Done checking all specified fields, check if we have extras
for field, value in metadata.items():