@@ -3163,39 +3163,59 @@ def _badwriteretry(self, mode: int) -> bool:
3163
3163
bad write error"
3164
3164
)
3165
3165
3166
- # Now, attempt to send application data over the established
3166
+ # Now, attempt to send some application data over the established
3167
3167
# SSL connection. Since the underlying raw socket's buffer is full,
3168
3168
# 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 ) :
3174
3174
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
+
3183
3210
except SSL .Error as e :
3184
3211
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 } " )
3193
3216
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" )
3199
3219
3200
3220
finally :
3201
3221
# Cleanup: shut down SSL connections and close raw sockets
@@ -3225,12 +3245,12 @@ def test_moving_write_buffer_should_pass(self) -> None:
3225
3245
_lib .SSL_MODE_ENABLE_PARTIAL_WRITE
3226
3246
| _lib .SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
3227
3247
)
3228
- result = self ._badwriteretry (mode )
3248
+ bad_retry = self ._badwriteretry (mode )
3229
3249
3230
- if result :
3250
+ if bad_retry :
3231
3251
pytest .fail (
3232
3252
"Using SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER failed to \
3233
- prevent bad write rety "
3253
+ prevent bad write retry "
3234
3254
)
3235
3255
3236
3256
def test_moving_write_buffer_should_fail (self ) -> None :
@@ -3242,12 +3262,11 @@ def test_moving_write_buffer_should_fail(self) -> None:
3242
3262
buffer is presented.
3243
3263
"""
3244
3264
mode = 0
3245
- result = self ._badwriteretry (mode )
3265
+ bad_retry = self ._badwriteretry (mode )
3246
3266
3247
- if not result :
3267
+ if not bad_retry :
3248
3268
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."
3251
3270
)
3252
3271
3253
3272
def test_get_finished_before_connect (self ) -> None :
0 commit comments