blob: a433391767a566274a96eeb70a6a6bd2a3118735 [file] [log] [blame]
Alex Gaynorb3016272016-01-13 09:01:42 -05001# This file is dual licensed under the terms of the Apache License, Version
2# 2.0, and the BSD License. See the LICENSE file in the root of this repository
3# for complete details.
4
5from hypothesis import given
Alex Gaynor3066bf42016-01-13 22:22:18 -05006from hypothesis.strategies import binary, integers
Alex Gaynorb3016272016-01-13 09:01:42 -05007
Cédric Krierbf0f4642016-02-26 18:40:20 +01008from cryptography.hazmat.primitives.padding import ANSIX923, PKCS7
Alex Gaynorb3016272016-01-13 09:01:42 -05009
10
Terry Chiae9b87d52016-11-15 09:56:02 +080011@given(integers(min_value=1, max_value=255), binary())
Alex Gaynor3066bf42016-01-13 22:22:18 -050012def test_pkcs7(block_size, data):
13 # Generate in [1, 31] so we can easily get block_size in bits by
14 # multiplying by 8.
15 p = PKCS7(block_size=block_size * 8)
Alex Gaynorb3016272016-01-13 09:01:42 -050016 padder = p.padder()
17 unpadder = p.unpadder()
18
19 padded = padder.update(data) + padder.finalize()
20
21 assert unpadder.update(padded) + unpadder.finalize() == data
Cédric Krierbf0f4642016-02-26 18:40:20 +010022
23
Terry Chiae9b87d52016-11-15 09:56:02 +080024@given(integers(min_value=1, max_value=255), binary())
Cédric Krierbf0f4642016-02-26 18:40:20 +010025def test_ansix923(block_size, data):
26 a = ANSIX923(block_size=block_size * 8)
27 padder = a.padder()
28 unpadder = a.unpadder()
29
30 padded = padder.update(data) + padder.finalize()
31
32 assert unpadder.update(padded) + unpadder.finalize() == data