From 5d020040cadec10d8f3d98eacb0838fbc059b4bb Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 11 Nov 2023 18:26:29 -0500 Subject: [PATCH] Rewrite the chunking test to use mmap This makes it no longer rely on implementation details --- tests/hazmat/primitives/test_ciphers.py | 55 +++++++++---------------- 1 file changed, 19 insertions(+), 36 deletions(-) diff --git a/tests/hazmat/primitives/test_ciphers.py b/tests/hazmat/primitives/test_ciphers.py index 19affeb7d07a9..556fe38df6ba5 100644 --- a/tests/hazmat/primitives/test_ciphers.py +++ b/tests/hazmat/primitives/test_ciphers.py @@ -4,7 +4,9 @@ import binascii +import mmap import os +import sys import pytest @@ -355,39 +357,20 @@ def test_update_into_buffer_too_small_gcm(self, backend): with pytest.raises(ValueError): encryptor.update_into(b"testing", buf) - def test_update_into_auto_chunking(self, backend, monkeypatch): - key = b"\x00" * 16 - c = ciphers.Cipher(AES(key), modes.ECB(), backend) - encryptor = c.encryptor() - # Lower max chunk size so we can test chunking - monkeypatch.setattr( - encryptor._ctx, # type: ignore[attr-defined] - "_MAX_CHUNK_SIZE", - 40, - ) - buf = bytearray(527) - pt = b"abcdefghijklmnopqrstuvwxyz012345" * 16 # 512 bytes - processed = encryptor.update_into(pt, buf) - assert processed == 512 - decryptor = c.decryptor() - # Change max chunk size to verify alternate boundaries don't matter - monkeypatch.setattr( - decryptor._ctx, # type: ignore[attr-defined] - "_MAX_CHUNK_SIZE", - 73, - ) - decbuf = bytearray(527) - decprocessed = decryptor.update_into(buf[:processed], decbuf) - assert decbuf[:decprocessed] == pt - - def test_max_chunk_size_fits_in_int32(self, backend): - # max chunk must fit in signed int32 or else a call large enough to - # cause chunking will result in the very OverflowError we want to - # avoid with chunking. - key = b"\x00" * 16 - c = ciphers.Cipher(AES(key), modes.ECB(), backend) - encryptor = c.encryptor() - backend._ffi.new( - "int *", - encryptor._ctx._MAX_CHUNK_SIZE, # type: ignore[attr-defined] - ) + +@pytest.mark.skipif( + sys.platform not in {"linux", "darwin"}, reason="mmap required" +) +def test_update_auto_chunking(self, backend, monkeypatch): + large_data = mmap.mmap(-1, 2**31, prot=mmap.PROT_READ) + + key = b"\x00" * 16 + c = ciphers.Cipher(AES(key), modes.ECB(), backend) + encryptor = c.encryptor() + + result = encryptor.update(memoryview(large_data)) + assert len(result) == len(large_data) + + decryptor = c.decryptor() + result = decryptor.update(result) + assert result == large_data[:]