unit test bogus header data
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Tue, 28 Feb 2017 15:34:34 +0000 (16:34 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 2 Apr 2018 11:34:08 +0000 (13:34 +0200)
testing/test_crypto.py

index 559c06b..e0c02a0 100644 (file)
@@ -44,7 +44,12 @@ def fill_mod (n):
 def faux_payload ():
     return "abcd" * 42
 
+
 class CryptoLayerTest (unittest.TestCase):
+    pass
+
+
+class AESGCMTest (CryptoLayerTest):
 
     def test_crypto_aes_gcm_enc_simple (self):
         NaCl = os.urandom (CRYPTO_NACL_SIZE)
@@ -251,6 +256,8 @@ class CryptoLayerTest (unittest.TestCase):
             ok, _, _ = dec.done ()
 
 
+class HeaderTest (CryptoLayerTest):
+
     def test_crypto_fmt_hdr_make (self):
         meta = faux_hdr()
         ok, hdr = crypto.hdr_make (meta)
@@ -258,6 +265,12 @@ class CryptoLayerTest (unittest.TestCase):
         assert len (hdr) == crypto.I2N_HDR_SIZE
 
 
+    def test_crypto_fmt_hdr_make_useless (self):
+        ok, ret = crypto.hdr_make ({ 42: "x" })
+        assert ok is False
+        assert ret.startswith ("error writing header:")
+
+
     def test_crypto_fmt_hdr_read (self):
         meta = faux_hdr()
         ok, hdr = crypto.hdr_make (meta)
@@ -270,3 +283,29 @@ class CryptoLayerTest (unittest.TestCase):
                       % (meta [k], mmeta [k])
 
 
+    def test_crypto_fmt_hdr_read_trailing_garbage (self):
+        meta = faux_hdr()
+        ok, hdr = crypto.hdr_make (meta)
+        hdr += b"-junk"
+        ok, msg = crypto.hdr_read (hdr)
+        assert ok is False
+        assert msg.startswith ("error reading header from")
+
+
+    def test_crypto_fmt_hdr_read_leading_garbage (self):
+        meta = faux_hdr()
+        ok, hdr = crypto.hdr_make (meta)
+        hdr = b"junk-" + hdr
+        ok, msg = crypto.hdr_read (hdr)
+        assert ok is False
+        assert msg.startswith ("error reading header from")
+
+
+    def test_crypto_fmt_hdr_inner_garbage (self):
+        meta = faux_hdr()
+        ok, hdr = crypto.hdr_make (meta)
+        hdr = hdr[:len(hdr)//2] + b"junk-" + hdr[len(hdr)//2:]
+        ok, msg = crypto.hdr_read (hdr)
+        assert ok is False
+        assert msg.startswith ("error reading header from")
+