summaryrefslogtreecommitdiff
path: root/prototype/fw/test
diff options
context:
space:
mode:
authorjaseg <git@jaseg.de>2020-11-26 12:38:52 +0100
committerjaseg <git@jaseg.de>2020-11-26 12:38:52 +0100
commit451904a0c5fae3bd28d35c7128df3a9db1a04b26 (patch)
treeb406c126b08032219894db3ff16a2ccf87c667d6 /prototype/fw/test
parente282a5eb373ec4b44d36f580fe642483f2e52794 (diff)
downloadihsm-451904a0c5fae3bd28d35c7128df3a9db1a04b26.tar.gz
ihsm-451904a0c5fae3bd28d35c7128df3a9db1a04b26.tar.bz2
ihsm-451904a0c5fae3bd28d35c7128df3a9db1a04b26.zip
Add CRC32 implementation
Diffstat (limited to 'prototype/fw/test')
-rw-r--r--prototype/fw/test/crc32_ref.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/prototype/fw/test/crc32_ref.py b/prototype/fw/test/crc32_ref.py
new file mode 100644
index 0000000..7ac0582
--- /dev/null
+++ b/prototype/fw/test/crc32_ref.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python3
+
+import subprocess
+import zlib
+import os
+import itertools
+import unittest
+
+INTERESTING = bytes([0x00, 0x42, 0xff, 0x01, 0x80])
+
+class RefdataTest(unittest.TestCase):
+ def run_test(self, test_data):
+ ref_val = zlib.crc32(test_data)
+
+ binary = os.environ.get('CRC32_TEST_BINARY', 'build/crc32_test')
+
+ output = subprocess.check_output(binary, input=test_data)
+ test_val = int(output, 16) & 0xffffffff
+
+ self.assertEqual(test_val, ref_val, f'mismatched results checking input {test_data}')
+
+ def test_empty(self):
+ self.run_test(b'')
+
+ def test_one_byte(self):
+ for x in range(256):
+ self.run_test(bytes([x]))
+
+ def test_two_byte(self):
+ for x, y in itertools.product(INTERESTING, repeat=2):
+ self.run_test(bytes([x, y]))
+
+ def test_three_bytes(self):
+ for x, y, z in itertools.product(INTERESTING, repeat=3):
+ self.run_test(bytes([x, y, z]))
+
+ def test_four_bytes(self):
+ for w, x, y, z in itertools.product(INTERESTING, repeat=4):
+ self.run_test(bytes([w, x, y, z]))
+
+ def test_random_strings(self):
+ for length in range(3, 24):
+ for i in range(20):
+ self.run_test(os.urandom(length))
+