kopia lustrzana https://github.com/solokeys/solo1
Refactor and self test the CBOR sorting
rodzic
ca80329b4c
commit
78579c27dc
|
@ -85,16 +85,13 @@ def TestCborKeysSorted(cbor_obj):
|
||||||
l = cbor_obj
|
l = cbor_obj
|
||||||
|
|
||||||
l_sorted = sorted(l[:], key=cmp_to_key(cmp_cbor_keys))
|
l_sorted = sorted(l[:], key=cmp_to_key(cmp_cbor_keys))
|
||||||
print(l)
|
|
||||||
print(l_sorted)
|
|
||||||
for i in range(len(l)):
|
for i in range(len(l)):
|
||||||
|
|
||||||
if not isinstance(l[i], (str, int)):
|
if not isinstance(l[i], (str, int)):
|
||||||
raise ValueError(f"Cbor map key {l[i]} must be int or str for CTAP2")
|
raise ValueError(f"Cbor map key {l[i]} must be int or str for CTAP2")
|
||||||
|
|
||||||
if l[i] != l_sorted[i]:
|
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")
|
raise ValueError(f"Cbor map item {i}: {l[i]} is out of order")
|
||||||
|
|
||||||
return l
|
return l
|
||||||
|
@ -136,7 +133,20 @@ class FIDO2Tests(Tester):
|
||||||
"baa",
|
"baa",
|
||||||
"bbb",
|
"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,):
|
def run(self,):
|
||||||
self.test_fido2()
|
self.test_fido2()
|
||||||
|
|
|
@ -28,14 +28,21 @@ class Packet(object):
|
||||||
|
|
||||||
|
|
||||||
class Test:
|
class Test:
|
||||||
def __init__(self, msg):
|
def __init__(self, msg, catch=None):
|
||||||
self.msg = msg
|
self.msg = msg
|
||||||
|
self.catch = catch
|
||||||
|
|
||||||
def __enter__(self,):
|
def __enter__(self,):
|
||||||
print(self.msg)
|
print(self.msg)
|
||||||
|
|
||||||
def __exit__(self, a, b, c):
|
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:
|
class Tester:
|
||||||
|
|
Ładowanie…
Reference in New Issue