11"""Tests for nextBlock, block retry logic, and encrypt sanity check."""
22
3- import asyncio
4- import base64
5- import io
6- import sys
73from types import SimpleNamespace
8- from unittest .mock import patch , AsyncMock , MagicMock
4+ from unittest .mock import patch , AsyncMock
95
106import pytest
117
12- from tests .conftest import make_job , PaddingOracle , AES_KEY , AES_IV , ORACLE
8+ from tests .conftest import make_job
139
1410
1511class TestNextBlockDecrypt :
@@ -24,8 +20,9 @@ async def test_successful_decrypt_returns_0(self):
2420 j .blocks = [[0 ] * 16 ]
2521 j .iv = [0 ] * 16
2622
27- with patch .object (j , 'decryptBlock' , new_callable = AsyncMock ,
28- return_value = b"decrypted_block!" ):
23+ with patch .object (
24+ j , "decryptBlock" , new_callable = AsyncMock , return_value = b"decrypted_block!"
25+ ):
2926 result = await j .nextBlock ()
3027
3128 assert result == 0
@@ -40,8 +37,12 @@ async def test_failed_decrypt_returns_1(self):
4037 j .blocks = [[0 ] * 16 ]
4138 j .iv = [0 ] * 16
4239
43- with patch .object (j , 'decryptBlock' , new_callable = AsyncMock ,
44- side_effect = Exception ("Block failed" )):
40+ with patch .object (
41+ j ,
42+ "decryptBlock" ,
43+ new_callable = AsyncMock ,
44+ side_effect = Exception ("Block failed" ),
45+ ):
4546 result = await j .nextBlock ()
4647
4748 assert result == 1
@@ -53,7 +54,9 @@ class TestNextBlockEncrypt:
5354
5455 @pytest .mark .asyncio
5556 async def test_successful_encrypt_with_passing_sanity_check (self ):
56- j = make_job (mode = "encrypt" , oracleMode = "negative" , oracleText = "Invalid padding" )
57+ j = make_job (
58+ mode = "encrypt" , oracleMode = "negative" , oracleText = "Invalid padding"
59+ )
5760 j .initialize_client ()
5861 j .blockCount = 1
5962 j .currentBlock = 0
@@ -65,16 +68,20 @@ async def test_successful_encrypt_with_passing_sanity_check(self):
6568 def mock_request (token ):
6669 return SimpleNamespace (text = "OK" , status_code = 200 )
6770
68- with patch .object (j , 'encryptBlock' , new_callable = AsyncMock , return_value = block_result ):
69- with patch .object (j , 'makeRequest' , side_effect = mock_request ):
71+ with patch .object (
72+ j , "encryptBlock" , new_callable = AsyncMock , return_value = block_result
73+ ):
74+ with patch .object (j , "makeRequest" , side_effect = mock_request ):
7075 result = await j .nextBlock ()
7176
7277 assert result == 0
7378 assert j .solvedBlocks [0 ] == block_result
7479
7580 @pytest .mark .asyncio
7681 async def test_failed_sanity_check_backs_out_block (self ):
77- j = make_job (mode = "encrypt" , oracleMode = "negative" , oracleText = "Invalid padding" )
82+ j = make_job (
83+ mode = "encrypt" , oracleMode = "negative" , oracleText = "Invalid padding"
84+ )
7885 j .initialize_client ()
7986 j .blockCount = 1
8087 j .currentBlock = 0
@@ -87,8 +94,10 @@ def mock_request(token):
8794 # Sanity check fails — the oracle text IS found (negative mode: pass = text absent)
8895 return SimpleNamespace (text = "Invalid padding" , status_code = 200 )
8996
90- with patch .object (j , 'encryptBlock' , new_callable = AsyncMock , return_value = block_result ):
91- with patch .object (j , 'makeRequest' , side_effect = mock_request ):
97+ with patch .object (
98+ j , "encryptBlock" , new_callable = AsyncMock , return_value = block_result
99+ ):
100+ with patch .object (j , "makeRequest" , side_effect = mock_request ):
92101 result = await j .nextBlock ()
93102
94103 assert result == 1
@@ -103,8 +112,12 @@ async def test_encrypt_exception_returns_1(self):
103112 j .blocks = [[0 ] * 16 ]
104113 j .iv = [0 ] * 16
105114
106- with patch .object (j , 'encryptBlock' , new_callable = AsyncMock ,
107- side_effect = Exception ("Encrypt failed" )):
115+ with patch .object (
116+ j ,
117+ "encryptBlock" ,
118+ new_callable = AsyncMock ,
119+ side_effect = Exception ("Encrypt failed" ),
120+ ):
108121 result = await j .nextBlock ()
109122
110123 assert result == 1
@@ -136,8 +149,8 @@ async def mock_next_block():
136149 max_block_retries = 3
137150 block_failures = 0
138151
139- with patch .object (j , ' nextBlock' , side_effect = mock_next_block ):
140- with patch (' blockbuster.blockbuster.saveState' ):
152+ with patch .object (j , " nextBlock" , side_effect = mock_next_block ):
153+ with patch (" blockbuster.blockbuster.saveState" ):
141154 while j .currentBlock < j .blockCount :
142155 result = await j .nextBlock ()
143156 if result == 0 :
@@ -167,7 +180,7 @@ async def always_fail():
167180 max_block_retries = 3
168181 block_failures = 0
169182
170- with patch .object (j , ' nextBlock' , side_effect = always_fail ):
183+ with patch .object (j , " nextBlock" , side_effect = always_fail ):
171184 while j .currentBlock < j .blockCount :
172185 result = await j .nextBlock ()
173186 if result == 0 :
@@ -188,8 +201,13 @@ class TestNextBlockEncryptKnownIV:
188201 @pytest .mark .asyncio
189202 async def test_encrypt_knowniv_appends_iv (self ):
190203 iv = list (range (16 ))
191- j = make_job (mode = "encrypt" , oracleMode = "negative" , oracleText = "Invalid padding" ,
192- ivMode = "knownIV" , iv = iv )
204+ j = make_job (
205+ mode = "encrypt" ,
206+ oracleMode = "negative" ,
207+ oracleText = "Invalid padding" ,
208+ ivMode = "knownIV" ,
209+ iv = iv ,
210+ )
193211 j .initialize_client ()
194212 j .blockCount = 1
195213 j .currentBlock = 0
@@ -202,8 +220,10 @@ def mock_request(token):
202220 captured_tokens .append (token )
203221 return SimpleNamespace (text = "OK" , status_code = 200 )
204222
205- with patch .object (j , 'encryptBlock' , new_callable = AsyncMock , return_value = block_result ):
206- with patch .object (j , 'makeRequest' , side_effect = mock_request ):
223+ with patch .object (
224+ j , "encryptBlock" , new_callable = AsyncMock , return_value = block_result
225+ ):
226+ with patch .object (j , "makeRequest" , side_effect = mock_request ):
207227 result = await j .nextBlock ()
208228
209229 assert result == 0
0 commit comments