Skip to content

Commit 5063e8a

Browse files
committed
simplified the logic of the bad write retry test to be pass or fail
1 parent 7d6365d commit 5063e8a

File tree

1 file changed

+53
-34
lines changed

1 file changed

+53
-34
lines changed

tests/test_ssl.py

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3163,39 +3163,59 @@ def _badwriteretry(self, mode: int) -> bool:
31633163
bad write error"
31643164
)
31653165

3166-
# Now, attempt to send application data over the established
3166+
# Now, attempt to send some application data over the established
31673167
# SSL connection. Since the underlying raw socket's buffer is full,
31683168
# this should cause a WantWriteError.
3169-
msg2 = b"Y" * 65536
3170-
3171-
try:
3172-
written = client.send(msg2)
3173-
except SSL.WantWriteError:
3169+
result = None
3170+
msg2 = b"Y" * 64
3171+
print(f"ID of msg2: {id(msg2)}")
3172+
# try multiple times as may not get a WantWriteError the first time
3173+
for i in range(100):
31743174
try:
3175-
# After a WantWriteError if the connection has partially
3176-
# written the last buffer it will expect a retry write.
3177-
# This next write should fail but for two different reasons
3178-
# depending on whether SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
3179-
# was set
3180-
msg3 = b"Z" * 65536
3181-
written = client.send(msg3)
3182-
pytest.fail("Retry succeeded unexpectedly")
3175+
written = client.send(msg2)
3176+
except SSL.WantWriteError:
3177+
try:
3178+
# After a WantWriteError if the connection has
3179+
# partially written the last buffer it will expect
3180+
# a retry write. We try with a new buffer so that the
3181+
# next write should pass if
3182+
# SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER was set but fail
3183+
# otherwise. Note if the buffer has a different length
3184+
# the write generates a "bad length" SSL.Error rather
3185+
# than "bad write retry".
3186+
# Create a new buffer that is guaranteed to be at a
3187+
# different location (because of different contents)
3188+
msg3 = b"Z" * 64
3189+
print(f"ID of msg3: {id(msg2)}")
3190+
written = client.send(msg3)
3191+
# the retry was successful
3192+
result = False
3193+
break
3194+
except SSL.WantWriteError:
3195+
# if this write generated another WantWriteError
3196+
# rather than a bad retry allow for loop to try again
3197+
pass
3198+
except SSL.Error as e:
3199+
reason = get_ssl_error_reason(e)
3200+
if reason == "bad write retry":
3201+
# Got SSL error on retry (expected if not using \
3202+
# SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)
3203+
result = True
3204+
break
3205+
else:
3206+
# the write unexpectedly failed
3207+
pytest.fail(f"Got unexpected SSL error on retry: \
3208+
{e} {reason}")
3209+
31833210
except SSL.Error as e:
31843211
reason = get_ssl_error_reason(e)
3185-
if reason == "bad write retry":
3186-
# Got SSL error on retry (expected if not using \
3187-
# SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)
3188-
result = True
3189-
else:
3190-
# when using SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
3191-
# we expect this to fail for a WantWriteError
3192-
result = False
3212+
pytest.fail(f"Got unexpected SSL error on retry: \
3213+
{e} {reason}")
3214+
except Exception as e:
3215+
pytest.fail(f"Unexpected exception during send: {e}")
31933216

3194-
except SSL.Error as e:
3195-
reason = get_ssl_error_reason(e)
3196-
pytest.fail(f"Got unexpected SSL error on retry: {e} {reason}")
3197-
except Exception as e:
3198-
pytest.fail(f"Unexpected exception during send: {e}")
3217+
if result is None:
3218+
pytest.fail("Failed to induce WantWriteError")
31993219

32003220
finally:
32013221
# Cleanup: shut down SSL connections and close raw sockets
@@ -3225,12 +3245,12 @@ def test_moving_write_buffer_should_pass(self) -> None:
32253245
_lib.SSL_MODE_ENABLE_PARTIAL_WRITE
32263246
| _lib.SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
32273247
)
3228-
result = self._badwriteretry(mode)
3248+
bad_retry = self._badwriteretry(mode)
32293249

3230-
if result:
3250+
if bad_retry:
32313251
pytest.fail(
32323252
"Using SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER failed to \
3233-
prevent bad write rety"
3253+
prevent bad write retry"
32343254
)
32353255

32363256
def test_moving_write_buffer_should_fail(self) -> None:
@@ -3242,12 +3262,11 @@ def test_moving_write_buffer_should_fail(self) -> None:
32423262
buffer is presented.
32433263
"""
32443264
mode = 0
3245-
result = self._badwriteretry(mode)
3265+
bad_retry = self._badwriteretry(mode)
32463266

3247-
if not result:
3267+
if not bad_retry:
32483268
pytest.fail(
3249-
"Using SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER failed to \
3250-
prevent bad write rety"
3269+
"Failed to generate bad write retry error."
32513270
)
32523271

32533272
def test_get_finished_before_connect(self) -> None:

0 commit comments

Comments
 (0)