Refactor and self test the CBOR sorting

pull/171/head
Conor Patrick 2019-04-11 13:42:17 -04:00
rodzic ca80329b4c
commit 78579c27dc
2 zmienionych plików z 24 dodań i 7 usunięć

Wyświetl plik

@ -85,16 +85,13 @@ def TestCborKeysSorted(cbor_obj):
l = cbor_obj
l_sorted = sorted(l[:], key=cmp_to_key(cmp_cbor_keys))
print(l)
print(l_sorted)
for i in range(len(l)):
if not isinstance(l[i], (str, int)):
raise ValueError(f"Cbor map key {l[i]} must be int or str for CTAP2")
if l[i] != l_sorted[i]:
print("sorted", l_sorted)
print("real", l)
raise ValueError(f"Cbor map item {i}: {l[i]} is out of order")
return l
@ -136,7 +133,20 @@ class FIDO2Tests(Tester):
"baa",
"bbb",
]
TestCborKeysSorted(cbor_key_list_sorted)
with Test("Self test CBOR sorting"):
TestCborKeysSorted(cbor_key_list_sorted)
with Test("Self test CBOR sorting integers", catch=ValueError):
TestCborKeysSorted([1, 0])
with Test("Self test CBOR sorting major type", catch=ValueError):
TestCborKeysSorted([-1, 0])
with Test("Self test CBOR sorting strings", catch=ValueError):
TestCborKeysSorted(["bb", "a"])
with Test("Self test CBOR sorting same length strings", catch=ValueError):
TestCborKeysSorted(["ab", "aa"])
def run(self,):
self.test_fido2()

Wyświetl plik

@ -28,14 +28,21 @@ class Packet(object):
class Test:
def __init__(self, msg):
def __init__(self, msg, catch=None):
self.msg = msg
self.catch = catch
def __enter__(self,):
print(self.msg)
def __exit__(self, a, b, c):
print("Pass")
if self.catch is None:
print("Pass")
elif isinstance(b, self.catch):
print("Pass")
return b
else:
raise RuntimeError(f"Expected exception {self.catch} did not occur.")
class Tester: