From 596e8fbb480f5278dbbccc221fb18bb8b03b802c Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Tue, 4 Jul 2023 23:12:58 -0400 Subject: [PATCH] Alter tests to not rely on len() of a bytes subclass (#9178) * Alter tests to not rely on len() of a bytes subclass * Trigger RTD * Trigger RTD --- tests/hazmat/primitives/test_aead.py | 56 ++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/tests/hazmat/primitives/test_aead.py b/tests/hazmat/primitives/test_aead.py index 5ae306254468..79d077065a68 100644 --- a/tests/hazmat/primitives/test_aead.py +++ b/tests/hazmat/primitives/test_aead.py @@ -4,7 +4,9 @@ import binascii +import mmap import os +import sys import pytest @@ -26,11 +28,6 @@ from .utils import _load_all_params -class FakeData(bytes): - def __len__(self): - return 2**31 - - def _aead_supported(cls): try: cls(b"0" * 32) @@ -39,6 +36,10 @@ def _aead_supported(cls): return False +def large_mmap(): + return mmap.mmap(-1, 2**32, prot=mmap.PROT_READ) + + @pytest.mark.skipif( _aead_supported(ChaCha20Poly1305), reason="Requires OpenSSL without ChaCha20Poly1305 support", @@ -53,16 +54,21 @@ def test_chacha20poly1305_unsupported_on_older_openssl(backend): reason="Does not support ChaCha20Poly1305", ) class TestChaCha20Poly1305: + @pytest.mark.skipif( + sys.platform not in {"linux", "darwin"}, reason="mmap required" + ) def test_data_too_large(self): key = ChaCha20Poly1305.generate_key() chacha = ChaCha20Poly1305(key) nonce = b"0" * 12 + large_data = large_mmap() + with pytest.raises(OverflowError): - chacha.encrypt(nonce, FakeData(), b"") + chacha.encrypt(nonce, large_data, b"") with pytest.raises(OverflowError): - chacha.encrypt(nonce, b"", FakeData()) + chacha.encrypt(nonce, b"", large_data) def test_generate_key(self): key = ChaCha20Poly1305.generate_key() @@ -189,16 +195,21 @@ def test_buffer_protocol(self, backend): reason="Does not support AESCCM", ) class TestAESCCM: + @pytest.mark.skipif( + sys.platform not in {"linux", "darwin"}, reason="mmap required" + ) def test_data_too_large(self): key = AESCCM.generate_key(128) aesccm = AESCCM(key) nonce = b"0" * 12 + large_data = large_mmap() + with pytest.raises(OverflowError): - aesccm.encrypt(nonce, FakeData(), b"") + aesccm.encrypt(nonce, large_data, b"") with pytest.raises(OverflowError): - aesccm.encrypt(nonce, b"", FakeData()) + aesccm.encrypt(nonce, b"", large_data) def test_default_tag_length(self, backend): key = AESCCM.generate_key(128) @@ -362,16 +373,21 @@ def _load_gcm_vectors(): class TestAESGCM: + @pytest.mark.skipif( + sys.platform not in {"linux", "darwin"}, reason="mmap required" + ) def test_data_too_large(self): key = AESGCM.generate_key(128) aesgcm = AESGCM(key) nonce = b"0" * 12 + large_data = large_mmap() + with pytest.raises(OverflowError): - aesgcm.encrypt(nonce, FakeData(), b"") + aesgcm.encrypt(nonce, large_data, b"") with pytest.raises(OverflowError): - aesgcm.encrypt(nonce, b"", FakeData()) + aesgcm.encrypt(nonce, b"", large_data) def test_vectors(self, backend, subtests): vectors = _load_gcm_vectors() @@ -496,16 +512,21 @@ def test_aesocb3_unsupported_on_older_openssl(backend): reason="Does not support AESOCB3", ) class TestAESOCB3: + @pytest.mark.skipif( + sys.platform not in {"linux", "darwin"}, reason="mmap required" + ) def test_data_too_large(self): key = AESOCB3.generate_key(128) aesocb3 = AESOCB3(key) nonce = b"0" * 12 + large_data = large_mmap() + with pytest.raises(OverflowError): - aesocb3.encrypt(nonce, FakeData(), b"") + aesocb3.encrypt(nonce, large_data, b"") with pytest.raises(OverflowError): - aesocb3.encrypt(nonce, b"", FakeData()) + aesocb3.encrypt(nonce, b"", large_data) def test_vectors(self, backend, subtests): vectors = [] @@ -629,15 +650,20 @@ def test_buffer_protocol(self, backend): reason="Does not support AESSIV", ) class TestAESSIV: + @pytest.mark.skipif( + sys.platform not in {"linux", "darwin"}, reason="mmap required" + ) def test_data_too_large(self): key = AESSIV.generate_key(256) aessiv = AESSIV(key) + large_data = large_mmap() + with pytest.raises(OverflowError): - aessiv.encrypt(FakeData(), None) + aessiv.encrypt(large_data, None) with pytest.raises(OverflowError): - aessiv.encrypt(b"irrelevant", [FakeData()]) + aessiv.encrypt(b"irrelevant", [large_data]) def test_no_empty_encryption(self): key = AESSIV.generate_key(256)