Skip to content

Commit 596e8fb

Browse files
authored
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
1 parent 60d82af commit 596e8fb

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

tests/hazmat/primitives/test_aead.py

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55

66
import binascii
7+
import mmap
78
import os
9+
import sys
810

911
import pytest
1012

@@ -26,11 +28,6 @@
2628
from .utils import _load_all_params
2729

2830

29-
class FakeData(bytes):
30-
def __len__(self):
31-
return 2**31
32-
33-
3431
def _aead_supported(cls):
3532
try:
3633
cls(b"0" * 32)
@@ -39,6 +36,10 @@ def _aead_supported(cls):
3936
return False
4037

4138

39+
def large_mmap():
40+
return mmap.mmap(-1, 2**32, prot=mmap.PROT_READ)
41+
42+
4243
@pytest.mark.skipif(
4344
_aead_supported(ChaCha20Poly1305),
4445
reason="Requires OpenSSL without ChaCha20Poly1305 support",
@@ -53,16 +54,21 @@ def test_chacha20poly1305_unsupported_on_older_openssl(backend):
5354
reason="Does not support ChaCha20Poly1305",
5455
)
5556
class TestChaCha20Poly1305:
57+
@pytest.mark.skipif(
58+
sys.platform not in {"linux", "darwin"}, reason="mmap required"
59+
)
5660
def test_data_too_large(self):
5761
key = ChaCha20Poly1305.generate_key()
5862
chacha = ChaCha20Poly1305(key)
5963
nonce = b"0" * 12
6064

65+
large_data = large_mmap()
66+
6167
with pytest.raises(OverflowError):
62-
chacha.encrypt(nonce, FakeData(), b"")
68+
chacha.encrypt(nonce, large_data, b"")
6369

6470
with pytest.raises(OverflowError):
65-
chacha.encrypt(nonce, b"", FakeData())
71+
chacha.encrypt(nonce, b"", large_data)
6672

6773
def test_generate_key(self):
6874
key = ChaCha20Poly1305.generate_key()
@@ -189,16 +195,21 @@ def test_buffer_protocol(self, backend):
189195
reason="Does not support AESCCM",
190196
)
191197
class TestAESCCM:
198+
@pytest.mark.skipif(
199+
sys.platform not in {"linux", "darwin"}, reason="mmap required"
200+
)
192201
def test_data_too_large(self):
193202
key = AESCCM.generate_key(128)
194203
aesccm = AESCCM(key)
195204
nonce = b"0" * 12
196205

206+
large_data = large_mmap()
207+
197208
with pytest.raises(OverflowError):
198-
aesccm.encrypt(nonce, FakeData(), b"")
209+
aesccm.encrypt(nonce, large_data, b"")
199210

200211
with pytest.raises(OverflowError):
201-
aesccm.encrypt(nonce, b"", FakeData())
212+
aesccm.encrypt(nonce, b"", large_data)
202213

203214
def test_default_tag_length(self, backend):
204215
key = AESCCM.generate_key(128)
@@ -362,16 +373,21 @@ def _load_gcm_vectors():
362373

363374

364375
class TestAESGCM:
376+
@pytest.mark.skipif(
377+
sys.platform not in {"linux", "darwin"}, reason="mmap required"
378+
)
365379
def test_data_too_large(self):
366380
key = AESGCM.generate_key(128)
367381
aesgcm = AESGCM(key)
368382
nonce = b"0" * 12
369383

384+
large_data = large_mmap()
385+
370386
with pytest.raises(OverflowError):
371-
aesgcm.encrypt(nonce, FakeData(), b"")
387+
aesgcm.encrypt(nonce, large_data, b"")
372388

373389
with pytest.raises(OverflowError):
374-
aesgcm.encrypt(nonce, b"", FakeData())
390+
aesgcm.encrypt(nonce, b"", large_data)
375391

376392
def test_vectors(self, backend, subtests):
377393
vectors = _load_gcm_vectors()
@@ -496,16 +512,21 @@ def test_aesocb3_unsupported_on_older_openssl(backend):
496512
reason="Does not support AESOCB3",
497513
)
498514
class TestAESOCB3:
515+
@pytest.mark.skipif(
516+
sys.platform not in {"linux", "darwin"}, reason="mmap required"
517+
)
499518
def test_data_too_large(self):
500519
key = AESOCB3.generate_key(128)
501520
aesocb3 = AESOCB3(key)
502521
nonce = b"0" * 12
503522

523+
large_data = large_mmap()
524+
504525
with pytest.raises(OverflowError):
505-
aesocb3.encrypt(nonce, FakeData(), b"")
526+
aesocb3.encrypt(nonce, large_data, b"")
506527

507528
with pytest.raises(OverflowError):
508-
aesocb3.encrypt(nonce, b"", FakeData())
529+
aesocb3.encrypt(nonce, b"", large_data)
509530

510531
def test_vectors(self, backend, subtests):
511532
vectors = []
@@ -629,15 +650,20 @@ def test_buffer_protocol(self, backend):
629650
reason="Does not support AESSIV",
630651
)
631652
class TestAESSIV:
653+
@pytest.mark.skipif(
654+
sys.platform not in {"linux", "darwin"}, reason="mmap required"
655+
)
632656
def test_data_too_large(self):
633657
key = AESSIV.generate_key(256)
634658
aessiv = AESSIV(key)
635659

660+
large_data = large_mmap()
661+
636662
with pytest.raises(OverflowError):
637-
aessiv.encrypt(FakeData(), None)
663+
aessiv.encrypt(large_data, None)
638664

639665
with pytest.raises(OverflowError):
640-
aessiv.encrypt(b"irrelevant", [FakeData()])
666+
aessiv.encrypt(b"irrelevant", [large_data])
641667

642668
def test_no_empty_encryption(self):
643669
key = AESSIV.generate_key(256)

0 commit comments

Comments
 (0)