From 0a03b84e1756b36d014a83f685da1950a8e8361e Mon Sep 17 00:00:00 2001 From: jaseg Date: Thu, 26 Nov 2020 18:03:41 +0100 Subject: Add cobs decoder --- prototype/fw/test/microcobs.py | 50 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'prototype/fw/test/microcobs.py') diff --git a/prototype/fw/test/microcobs.py b/prototype/fw/test/microcobs.py index 1d09efd..39cffd1 100644 --- a/prototype/fw/test/microcobs.py +++ b/prototype/fw/test/microcobs.py @@ -143,3 +143,53 @@ class MicrocobsTest(unittest.TestCase): for i in range(10000): self.do_test(os.urandom(random.randint(0, 600))) +class MicrocobsDecodeTest(unittest.TestCase): + def do_test(self, data): + enc = cobs.encode(data) + input = binascii.hexlify(enc) + + debug_file = tempfile.NamedTemporaryFile(prefix='microcobs_test_', delete=False) + debug_file.write(input) + + try: + test = subprocess.check_output(os.getenv('MICROCOBS_DECODE_TEST_BINARY', 'build/microcobs_decode_test'), + input=input, stderr=subprocess.DEVNULL) + test = binascii.unhexlify(test.strip()) + + self.assertEqual(data, test, f'Mismatched output for input {debug_file.name}') + + debug_file.close() + os.remove(debug_file.name) + except Exception as e: + raise SystemError(f'Test error for input {debug_file.name}') from e + + + def test_one_byte(self): + for i in range(256): + self.do_test(bytes([i])) + + def test_lengths(self): + for i in range(260): + self.do_test(bytes([0xff] * i)) + + def test_null_then_lengths(self): + for i in range(256): + self.do_test(bytes([0] + [0xff] * i)) + + def test_lengths_then_null(self): + for i in range(256): + self.do_test(bytes([0xff] * i + [0])) + + def test_two_byte(self): + for i in range(4): + for j in range(4): + self.do_test(bytes([i, j])) + + def test_long(self): + for i in range(5): + self.do_test(b'A' * (100 + 256*i)) + + def test_random(self): + for i in range(10000): + self.do_test(os.urandom(random.randint(0, 600))) + -- cgit