From bf0f464ab62d2e69ebfacd80fad2de46e862fcbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Fri, 26 Feb 2016 18:40:20 +0100 Subject: Added support for padding ANSI X.923 --- tests/hazmat/primitives/test_padding.py | 48 +++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'tests/hazmat/primitives/test_padding.py') diff --git a/tests/hazmat/primitives/test_padding.py b/tests/hazmat/primitives/test_padding.py index 392ea737..9da8ea7a 100644 --- a/tests/hazmat/primitives/test_padding.py +++ b/tests/hazmat/primitives/test_padding.py @@ -99,3 +99,51 @@ class TestPKCS7(object): unpadder.update(b"") with pytest.raises(AlreadyFinalized): unpadder.finalize() + + +class TestANSIX923(object): + @pytest.mark.parametrize(("size", "unpadded", "padded"), [ + ( + 128, + b"1111111111", + b"1111111111\x00\x00\x00\x00\x00\x06", + ), + ( + 128, + b"111111111111111122222222222222", + b"111111111111111122222222222222\x00\x02", + ), + ( + 128, + b"1" * 16, + b"1" * 16 + b"\x00" * 15 + b"\x10", + ), + ( + 128, + b"1" * 17, + b"1" * 17 + b"\x00" * 14 + b"\x0F", + ) + ]) + def test_pad(self, size, unpadded, padded): + padder = padding.ANSIX923(size).padder() + result = padder.update(unpadded) + result += padder.finalize() + assert result == padded + + @pytest.mark.parametrize(("size", "unpadded", "padded"), [ + ( + 128, + b"1111111111", + b"1111111111\x00\x00\x00\x00\x00\x06", + ), + ( + 128, + b"111111111111111122222222222222", + b"111111111111111122222222222222\x00\x02", + ), + ]) + def test_unpad(self, size, unpadded, padded): + unpadder = padding.ANSIX923(size).unpadder() + result = unpadder.update(padded) + result += unpadder.finalize() + assert result == unpadded -- cgit v1.2.3 From 94f9ea25b50a3dd2592abfc63385989955e60e68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 27 Feb 2016 00:28:39 +0100 Subject: Add padding check for ANSI X.923 All padding bytes must be 0. --- tests/hazmat/primitives/test_padding.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'tests/hazmat/primitives/test_padding.py') diff --git a/tests/hazmat/primitives/test_padding.py b/tests/hazmat/primitives/test_padding.py index 9da8ea7a..9126e5bf 100644 --- a/tests/hazmat/primitives/test_padding.py +++ b/tests/hazmat/primitives/test_padding.py @@ -102,6 +102,21 @@ class TestPKCS7(object): class TestANSIX923(object): + @pytest.mark.parametrize(("size", "padded"), [ + (128, b"1111"), + (128, b"1111111111111111"), + (128, b"111111111111111\x06"), + (128, b"1111111111\x06\x06\x06\x06\x06\x06"), + (128, b""), + (128, b"\x06" * 6), + (128, b"\x00" * 16), + ]) + def test_invalid_padding(self, size, padded): + unpadder = padding.ANSIX923(size).unpadder() + with pytest.raises(ValueError): + unpadder.update(padded) + unpadder.finalize() + @pytest.mark.parametrize(("size", "unpadded", "padded"), [ ( 128, -- cgit v1.2.3 From 70b3a7dd5ce2a953da1ce19534bcedbb53a8c2bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Wed, 16 Mar 2016 08:34:25 +0100 Subject: Add more tests since there is no more sub-classing --- tests/hazmat/primitives/test_padding.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'tests/hazmat/primitives/test_padding.py') diff --git a/tests/hazmat/primitives/test_padding.py b/tests/hazmat/primitives/test_padding.py index 9126e5bf..e934c0ac 100644 --- a/tests/hazmat/primitives/test_padding.py +++ b/tests/hazmat/primitives/test_padding.py @@ -102,6 +102,11 @@ class TestPKCS7(object): class TestANSIX923(object): + @pytest.mark.parametrize("size", [127, 4096, -2]) + def test_invalid_block_size(self, size): + with pytest.raises(ValueError): + padding.ANSIX923(size) + @pytest.mark.parametrize(("size", "padded"), [ (128, b"1111"), (128, b"1111111111111111"), @@ -117,6 +122,14 @@ class TestANSIX923(object): unpadder.update(padded) unpadder.finalize() + def test_non_bytes(self): + padder = padding.ANSIX923(128).padder() + with pytest.raises(TypeError): + padder.update(u"abc") + unpadder = padding.ANSIX923(128).unpadder() + with pytest.raises(TypeError): + unpadder.update(u"abc") + @pytest.mark.parametrize(("size", "unpadded", "padded"), [ ( 128, @@ -162,3 +175,19 @@ class TestANSIX923(object): result = unpadder.update(padded) result += unpadder.finalize() assert result == unpadded + + def test_use_after_finalize(self): + padder = padding.ANSIX923(128).padder() + b = padder.finalize() + with pytest.raises(AlreadyFinalized): + padder.update(b"") + with pytest.raises(AlreadyFinalized): + padder.finalize() + + unpadder = padding.ANSIX923(128).unpadder() + unpadder.update(b) + unpadder.finalize() + with pytest.raises(AlreadyFinalized): + unpadder.update(b"") + with pytest.raises(AlreadyFinalized): + unpadder.finalize() -- cgit v1.2.3