From 78579c27dc4439fc38f5d4d50b993e345b07d836 Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Thu, 11 Apr 2019 13:42:17 -0400 Subject: [PATCH] Refactor and self test the CBOR sorting --- tools/testing/tests/fido2.py | 20 +++++++++++++++----- tools/testing/tests/tester.py | 11 +++++++++-- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/tools/testing/tests/fido2.py b/tools/testing/tests/fido2.py index d3f0865..60fdb05 100644 --- a/tools/testing/tests/fido2.py +++ b/tools/testing/tests/fido2.py @@ -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() diff --git a/tools/testing/tests/tester.py b/tools/testing/tests/tester.py index e0ae813..10cd1d3 100644 --- a/tools/testing/tests/tester.py +++ b/tools/testing/tests/tester.py @@ -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: