From 3944a8ce014f9f665a2300e1fa994b872cffa92b Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Tue, 29 Oct 2013 10:52:24 -0700 Subject: Initial implementation with tests, docs to follow --- tests/hazmat/primitives/test_padding.py | 85 +++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 tests/hazmat/primitives/test_padding.py (limited to 'tests') diff --git a/tests/hazmat/primitives/test_padding.py b/tests/hazmat/primitives/test_padding.py new file mode 100644 index 00000000..a41edac1 --- /dev/null +++ b/tests/hazmat/primitives/test_padding.py @@ -0,0 +1,85 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest + +from cryptography.hazmat.primitives import padding + + +class TestPKCS7(object): + @pytest.mark.parametrize("size", [127, 4096, -2]) + def test_invalid_block_size(self, size): + with pytest.raises(ValueError): + padding.PKCS7(size) + + @pytest.mark.parametrize(("size", "padded"), [ + (128, b"1111"), + (128, b"1111111111111111"), + (128, b"111111111111111\x06"), + (128, b""), + ]) + def test_invalid_padding(self, size, padded): + padder = padding.PKCS7(size) + + with pytest.raises(ValueError): + padder.unpad(padded) + + @pytest.mark.parametrize(("size", "unpadded", "padded"), [ + ( + 128, + b"1111111111", + b"1111111111\x06\x06\x06\x06\x06\x06", + ), + ( + 128, + b"111111111111111122222222222222", + b"111111111111111122222222222222\x02\x02", + ), + ]) + def test_pad(self, size, unpadded, padded): + padder = padding.PKCS7(size) + assert padder.pad(unpadded) == padded + + @pytest.mark.parametrize(("size", "unpadded", "padded"), [ + ( + 128, + b"1111111111", + b"1111111111\x06\x06\x06\x06\x06\x06", + ), + ( + 128, + b"111111111111111122222222222222", + b"111111111111111122222222222222\x02\x02", + ), + ]) + def test_unpad(self, size, unpadded, padded): + padder = padding.PKCS7(size) + assert padder.unpad(padded) == unpadded + + def test_use_after_finalize(self): + p = padding.PKCS7(128) + + padder = p.padder() + b = padder.finalize() + with pytest.raises(ValueError): + padder.update(b"") + with pytest.raises(ValueError): + padder.finalize() + + unpadder = p.unpadder() + unpadder.update(b) + unpadder.finalize() + with pytest.raises(ValueError): + unpadder.update(b"") + with pytest.raises(ValueError): + unpadder.finalize() -- cgit v1.2.3 From 22e2eaee0b48318c3a3e5eda7ce9174ac8cfce6a Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Tue, 29 Oct 2013 11:42:14 -0700 Subject: Removed helper --- tests/hazmat/primitives/test_padding.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/hazmat/primitives/test_padding.py b/tests/hazmat/primitives/test_padding.py index a41edac1..c47b6c5c 100644 --- a/tests/hazmat/primitives/test_padding.py +++ b/tests/hazmat/primitives/test_padding.py @@ -31,8 +31,10 @@ class TestPKCS7(object): def test_invalid_padding(self, size, padded): padder = padding.PKCS7(size) + unpadder = padder.unpadder() with pytest.raises(ValueError): - padder.unpad(padded) + unpadder.update(padded) + unpadder.finalize() @pytest.mark.parametrize(("size", "unpadded", "padded"), [ ( @@ -48,7 +50,10 @@ class TestPKCS7(object): ]) def test_pad(self, size, unpadded, padded): padder = padding.PKCS7(size) - assert padder.pad(unpadded) == padded + padder = padder.padder() + result = padder.update(unpadded) + result += padder.finalize() + assert result == padded @pytest.mark.parametrize(("size", "unpadded", "padded"), [ ( @@ -64,7 +69,10 @@ class TestPKCS7(object): ]) def test_unpad(self, size, unpadded, padded): padder = padding.PKCS7(size) - assert padder.unpad(padded) == unpadded + unpadder = padder.unpadder() + result = unpadder.update(padded) + result += unpadder.finalize() + assert result == unpadded def test_use_after_finalize(self): p = padding.PKCS7(128) -- cgit v1.2.3 From 25f96e511f1e1bf70393cdd34c83f57a4b45d458 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Tue, 29 Oct 2013 14:10:37 -0700 Subject: Optimize + test cases --- tests/hazmat/primitives/test_padding.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'tests') diff --git a/tests/hazmat/primitives/test_padding.py b/tests/hazmat/primitives/test_padding.py index c47b6c5c..f689dda0 100644 --- a/tests/hazmat/primitives/test_padding.py +++ b/tests/hazmat/primitives/test_padding.py @@ -47,6 +47,16 @@ class TestPKCS7(object): b"111111111111111122222222222222", b"111111111111111122222222222222\x02\x02", ), + ( + 128, + b"1" * 16, + b"1" * 16 + b"\x10" * 16, + ), + ( + 128, + b"1" * 17, + b"1" * 17 + b"\x0F" * 15, + ) ]) def test_pad(self, size, unpadded, padded): padder = padding.PKCS7(size) -- cgit v1.2.3 From 60ad3e182476d84daeb7cff9d333623a688edd61 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Tue, 29 Oct 2013 14:26:11 -0700 Subject: Clean up var naming --- tests/hazmat/primitives/test_padding.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'tests') diff --git a/tests/hazmat/primitives/test_padding.py b/tests/hazmat/primitives/test_padding.py index f689dda0..798b2a77 100644 --- a/tests/hazmat/primitives/test_padding.py +++ b/tests/hazmat/primitives/test_padding.py @@ -29,9 +29,7 @@ class TestPKCS7(object): (128, b""), ]) def test_invalid_padding(self, size, padded): - padder = padding.PKCS7(size) - - unpadder = padder.unpadder() + unpadder = padding.PKCS7(size).unpadder() with pytest.raises(ValueError): unpadder.update(padded) unpadder.finalize() @@ -59,8 +57,7 @@ class TestPKCS7(object): ) ]) def test_pad(self, size, unpadded, padded): - padder = padding.PKCS7(size) - padder = padder.padder() + padder = padding.PKCS7(size).padder() result = padder.update(unpadded) result += padder.finalize() assert result == padded @@ -78,23 +75,20 @@ class TestPKCS7(object): ), ]) def test_unpad(self, size, unpadded, padded): - padder = padding.PKCS7(size) - unpadder = padder.unpadder() + unpadder = padding.PKCS7(size).unpadder() result = unpadder.update(padded) result += unpadder.finalize() assert result == unpadded def test_use_after_finalize(self): - p = padding.PKCS7(128) - - padder = p.padder() + padder = padding.PKCS7(128).padder() b = padder.finalize() with pytest.raises(ValueError): padder.update(b"") with pytest.raises(ValueError): padder.finalize() - unpadder = p.unpadder() + unpadder = padding.PKCS7(128).unpadder() unpadder.update(b) unpadder.finalize() with pytest.raises(ValueError): -- cgit v1.2.3 From 5787fb5dcde454404bfa9c2ec1a601bbafd62404 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Tue, 29 Oct 2013 14:38:06 -0700 Subject: raise an error if you unicode --- tests/hazmat/primitives/test_padding.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'tests') diff --git a/tests/hazmat/primitives/test_padding.py b/tests/hazmat/primitives/test_padding.py index 798b2a77..3cefafaf 100644 --- a/tests/hazmat/primitives/test_padding.py +++ b/tests/hazmat/primitives/test_padding.py @@ -13,6 +13,8 @@ import pytest +import six + from cryptography.hazmat.primitives import padding @@ -34,6 +36,14 @@ class TestPKCS7(object): unpadder.update(padded) unpadder.finalize() + def test_non_bytes(self): + padder = padding.PKCS7(128).padder() + with pytest.raises(TypeError): + padder.update(six.u("abc")) + unpadder = padding.PKCS7(128).unpadder() + with pytest.raises(TypeError): + unpadder.update(six.u("abc")) + @pytest.mark.parametrize(("size", "unpadded", "padded"), [ ( 128, -- cgit v1.2.3