From cb7397d5273b07ae7d6e9bd40d87c22571a06c99 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Tue, 28 Feb 2017 16:34:34 +0100 Subject: [PATCH] unit test bogus header data --- testing/test_crypto.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 files changed, 39 insertions(+), 0 deletions(-) diff --git a/testing/test_crypto.py b/testing/test_crypto.py index 559c06b..e0c02a0 100644 --- a/testing/test_crypto.py +++ b/testing/test_crypto.py @@ -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") + -- 1.7.1