| 1 |
import logging |
| 2 |
import unittest |
| 3 |
from unittest.mock import patch |
| 4 |
|
| 5 |
from websockets.exceptions import ( |
| 6 |
ConnectionClosedError, |
| 7 |
ConnectionClosedOK, |
| 8 |
InvalidState, |
| 9 |
PayloadTooBig, |
| 10 |
ProtocolError, |
| 11 |
) |
| 12 |
from websockets.frames import ( |
| 13 |
BINARY, |
| 14 |
CLOSE, |
| 15 |
CONT, |
| 16 |
PING, |
| 17 |
PONG, |
| 18 |
TEXT, |
| 19 |
Close, |
| 20 |
CloseCode, |
| 21 |
Frame, |
| 22 |
) |
| 23 |
from websockets.protocol import * |
| 24 |
from websockets.protocol import CLIENT, CLOSED, CLOSING, CONNECTING, SERVER |
| 25 |
|
| 26 |
from .extensions.utils import Rsv2Extension |
| 27 |
from .test_frames import FramesTestCase |
| 28 |
|
| 29 |
|
| 30 |
class ProtocolTestCase(FramesTestCase): |
| 31 |
def assertFrameSent(self, connection, frame, eof=False): |
| 32 |
""" |
| 33 |
Outgoing data for ``connection`` contains the given frame. |
| 34 |
|
| 35 |
``frame`` may be ``None`` if no frame is expected. |
| 36 |
|
| 37 |
When ``eof`` is ``True``, the end of the stream is also expected. |
| 38 |
|
| 39 |
""" |
| 40 |
frames_sent = [ |
| 41 |
( |
| 42 |
None |
| 43 |
if write is SEND_EOF |
| 44 |
else self.parse( |
| 45 |
write, |
| 46 |
mask=connection.side is CLIENT, |
| 47 |
extensions=connection.extensions, |
| 48 |
) |
| 49 |
) |
| 50 |
for write in connection.data_to_send() |
| 51 |
] |
| 52 |
frames_expected = [] if frame is None else [frame] |
| 53 |
if eof: |
| 54 |
frames_expected += [None] |
| 55 |
self.assertEqual(frames_sent, frames_expected) |
| 56 |
|
| 57 |
def assertFrameReceived(self, connection, frame): |
| 58 |
""" |
| 59 |
Incoming data for ``connection`` contains the given frame. |
| 60 |
|
| 61 |
``frame`` may be ``None`` if no frame is expected. |
| 62 |
|
| 63 |
""" |
| 64 |
frames_received = connection.events_received() |
| 65 |
frames_expected = [] if frame is None else [frame] |
| 66 |
self.assertEqual(frames_received, frames_expected) |
| 67 |
|
| 68 |
def assertConnectionClosing(self, connection, code=None, reason=""): |
| 69 |
""" |
| 70 |
Incoming data caused the "Start the WebSocket Closing Handshake" process. |
| 71 |
|
| 72 |
""" |
| 73 |
close_frame = Frame( |
| 74 |
CLOSE, |
| 75 |
b"" if code is None else Close(code, reason).serialize(), |
| 76 |
) |
| 77 |
# A close frame was received. |
| 78 |
self.assertFrameReceived(connection, close_frame) |
| 79 |
# A close frame and possibly the end of stream were sent. |
| 80 |
self.assertFrameSent(connection, close_frame, eof=connection.side is SERVER) |
| 81 |
|
| 82 |
def assertConnectionFailing(self, connection, code=None, reason=""): |
| 83 |
""" |
| 84 |
Incoming data caused the "Fail the WebSocket Connection" process. |
| 85 |
|
| 86 |
""" |
| 87 |
close_frame = Frame( |
| 88 |
CLOSE, |
| 89 |
b"" if code is None else Close(code, reason).serialize(), |
| 90 |
) |
| 91 |
# No frame was received. |
| 92 |
self.assertFrameReceived(connection, None) |
| 93 |
# A close frame and possibly the end of stream were sent. |
| 94 |
self.assertFrameSent(connection, close_frame, eof=connection.side is SERVER) |
| 95 |
|
| 96 |
|
| 97 |
class MaskingTests(ProtocolTestCase): |
| 98 |
""" |
| 99 |
Test frame masking. |
| 100 |
|
| 101 |
5.1. Overview |
| 102 |
|
| 103 |
""" |
| 104 |
|
| 105 |
unmasked_text_frame_date = b"\x81\x04Spam" |
| 106 |
masked_text_frame_data = b"\x81\x84\x00\xff\x00\xff\x53\x8f\x61\x92" |
| 107 |
|
| 108 |
def test_client_sends_masked_frame(self): |
| 109 |
client = Protocol(CLIENT) |
| 110 |
with patch("secrets.token_bytes", return_value=b"\x00\xff\x00\xff"): |
| 111 |
client.send_text(b"Spam", True) |
| 112 |
self.assertEqual(client.data_to_send(), [self.masked_text_frame_data]) |
| 113 |
|
| 114 |
def test_server_sends_unmasked_frame(self): |
| 115 |
server = Protocol(SERVER) |
| 116 |
server.send_text(b"Spam", True) |
| 117 |
self.assertEqual(server.data_to_send(), [self.unmasked_text_frame_date]) |
| 118 |
|
| 119 |
def test_client_receives_unmasked_frame(self): |
| 120 |
client = Protocol(CLIENT) |
| 121 |
client.receive_data(self.unmasked_text_frame_date) |
| 122 |
self.assertFrameReceived( |
| 123 |
client, |
| 124 |
Frame(TEXT, b"Spam"), |
| 125 |
) |
| 126 |
|
| 127 |
def test_server_receives_masked_frame(self): |
| 128 |
server = Protocol(SERVER) |
| 129 |
server.receive_data(self.masked_text_frame_data) |
| 130 |
self.assertFrameReceived( |
| 131 |
server, |
| 132 |
Frame(TEXT, b"Spam"), |
| 133 |
) |
| 134 |
|
| 135 |
def test_client_receives_masked_frame(self): |
| 136 |
client = Protocol(CLIENT) |
| 137 |
client.receive_data(self.masked_text_frame_data) |
| 138 |
self.assertIsInstance(client.parser_exc, ProtocolError) |
| 139 |
self.assertEqual(str(client.parser_exc), "incorrect masking") |
| 140 |
self.assertConnectionFailing( |
| 141 |
client, CloseCode.PROTOCOL_ERROR, "incorrect masking" |
| 142 |
) |
| 143 |
|
| 144 |
def test_server_receives_unmasked_frame(self): |
| 145 |
server = Protocol(SERVER) |
| 146 |
server.receive_data(self.unmasked_text_frame_date) |
| 147 |
self.assertIsInstance(server.parser_exc, ProtocolError) |
| 148 |
self.assertEqual(str(server.parser_exc), "incorrect masking") |
| 149 |
self.assertConnectionFailing( |
| 150 |
server, CloseCode.PROTOCOL_ERROR, "incorrect masking" |
| 151 |
) |
| 152 |
|
| 153 |
|
| 154 |
class ContinuationTests(ProtocolTestCase): |
| 155 |
""" |
| 156 |
Test continuation frames without text or binary frames. |
| 157 |
|
| 158 |
""" |
| 159 |
|
| 160 |
def test_client_sends_unexpected_continuation(self): |
| 161 |
client = Protocol(CLIENT) |
| 162 |
with self.assertRaises(ProtocolError) as raised: |
| 163 |
client.send_continuation(b"", fin=False) |
| 164 |
self.assertEqual(str(raised.exception), "unexpected continuation frame") |
| 165 |
|
| 166 |
def test_server_sends_unexpected_continuation(self): |
| 167 |
server = Protocol(SERVER) |
| 168 |
with self.assertRaises(ProtocolError) as raised: |
| 169 |
server.send_continuation(b"", fin=False) |
| 170 |
self.assertEqual(str(raised.exception), "unexpected continuation frame") |
| 171 |
|
| 172 |
def test_client_receives_unexpected_continuation(self): |
| 173 |
client = Protocol(CLIENT) |
| 174 |
client.receive_data(b"\x00\x00") |
| 175 |
self.assertIsInstance(client.parser_exc, ProtocolError) |
| 176 |
self.assertEqual(str(client.parser_exc), "unexpected continuation frame") |
| 177 |
self.assertConnectionFailing( |
| 178 |
client, CloseCode.PROTOCOL_ERROR, "unexpected continuation frame" |
| 179 |
) |
| 180 |
|
| 181 |
def test_server_receives_unexpected_continuation(self): |
| 182 |
server = Protocol(SERVER) |
| 183 |
server.receive_data(b"\x00\x80\x00\x00\x00\x00") |
| 184 |
self.assertIsInstance(server.parser_exc, ProtocolError) |
| 185 |
self.assertEqual(str(server.parser_exc), "unexpected continuation frame") |
| 186 |
self.assertConnectionFailing( |
| 187 |
server, CloseCode.PROTOCOL_ERROR, "unexpected continuation frame" |
| 188 |
) |
| 189 |
|
| 190 |
def test_client_sends_continuation_after_sending_close(self): |
| 191 |
client = Protocol(CLIENT) |
| 192 |
# Since it isn't possible to send a close frame in a fragmented |
| 193 |
# message (see test_client_send_close_in_fragmented_message), in fact, |
| 194 |
# this is the same test as test_client_sends_unexpected_continuation. |
| 195 |
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"): |
| 196 |
client.send_close(CloseCode.GOING_AWAY) |
| 197 |
self.assertEqual(client.data_to_send(), [b"\x88\x82\x00\x00\x00\x00\x03\xe9"]) |
| 198 |
with self.assertRaises(ProtocolError) as raised: |
| 199 |
client.send_continuation(b"", fin=False) |
| 200 |
self.assertEqual(str(raised.exception), "unexpected continuation frame") |
| 201 |
|
| 202 |
def test_server_sends_continuation_after_sending_close(self): |
| 203 |
# Since it isn't possible to send a close frame in a fragmented |
| 204 |
# message (see test_server_send_close_in_fragmented_message), in fact, |
| 205 |
# this is the same test as test_server_sends_unexpected_continuation. |
| 206 |
server = Protocol(SERVER) |
| 207 |
server.send_close(CloseCode.NORMAL_CLOSURE) |
| 208 |
self.assertEqual(server.data_to_send(), [b"\x88\x02\x03\xe8"]) |
| 209 |
with self.assertRaises(ProtocolError) as raised: |
| 210 |
server.send_continuation(b"", fin=False) |
| 211 |
self.assertEqual(str(raised.exception), "unexpected continuation frame") |
| 212 |
|
| 213 |
def test_client_receives_continuation_after_receiving_close(self): |
| 214 |
client = Protocol(CLIENT) |
| 215 |
client.receive_data(b"\x88\x02\x03\xe8") |
| 216 |
self.assertConnectionClosing(client, CloseCode.NORMAL_CLOSURE) |
| 217 |
client.receive_data(b"\x00\x00") |
| 218 |
self.assertFrameReceived(client, None) |
| 219 |
self.assertFrameSent(client, None) |
| 220 |
|
| 221 |
def test_server_receives_continuation_after_receiving_close(self): |
| 222 |
server = Protocol(SERVER) |
| 223 |
server.receive_data(b"\x88\x82\x00\x00\x00\x00\x03\xe9") |
| 224 |
self.assertConnectionClosing(server, CloseCode.GOING_AWAY) |
| 225 |
server.receive_data(b"\x00\x80\x00\xff\x00\xff") |
| 226 |
self.assertFrameReceived(server, None) |
| 227 |
self.assertFrameSent(server, None) |
| 228 |
|
| 229 |
|
| 230 |
class TextTests(ProtocolTestCase): |
| 231 |
""" |
| 232 |
Test text frames and continuation frames. |
| 233 |
|
| 234 |
""" |
| 235 |
|
| 236 |
def test_client_sends_text(self): |
| 237 |
client = Protocol(CLIENT) |
| 238 |
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"): |
| 239 |
client.send_text("😀".encode()) |
| 240 |
self.assertEqual( |
| 241 |
client.data_to_send(), [b"\x81\x84\x00\x00\x00\x00\xf0\x9f\x98\x80"] |
| 242 |
) |
| 243 |
|
| 244 |
def test_server_sends_text(self): |
| 245 |
server = Protocol(SERVER) |
| 246 |
server.send_text("😀".encode()) |
| 247 |
self.assertEqual(server.data_to_send(), [b"\x81\x04\xf0\x9f\x98\x80"]) |
| 248 |
|
| 249 |
def test_client_receives_text(self): |
| 250 |
client = Protocol(CLIENT) |
| 251 |
client.receive_data(b"\x81\x04\xf0\x9f\x98\x80") |
| 252 |
self.assertFrameReceived( |
| 253 |
client, |
| 254 |
Frame(TEXT, "😀".encode()), |
| 255 |
) |
| 256 |
|
| 257 |
def test_server_receives_text(self): |
| 258 |
server = Protocol(SERVER) |
| 259 |
server.receive_data(b"\x81\x84\x00\x00\x00\x00\xf0\x9f\x98\x80") |
| 260 |
self.assertFrameReceived( |
| 261 |
server, |
| 262 |
Frame(TEXT, "😀".encode()), |
| 263 |
) |
| 264 |
|
| 265 |
def test_client_receives_text_over_size_limit(self): |
| 266 |
client = Protocol(CLIENT, max_size=3) |
| 267 |
client.receive_data(b"\x81\x04\xf0\x9f\x98\x80") |
| 268 |
self.assertIsInstance(client.parser_exc, PayloadTooBig) |
| 269 |
self.assertEqual( |
| 270 |
str(client.parser_exc), |
| 271 |
"frame with 4 bytes exceeds limit of 3 bytes", |
| 272 |
) |
| 273 |
self.assertConnectionFailing( |
| 274 |
client, |
| 275 |
CloseCode.MESSAGE_TOO_BIG, |
| 276 |
"frame with 4 bytes exceeds limit of 3 bytes", |
| 277 |
) |
| 278 |
|
| 279 |
def test_server_receives_text_over_size_limit(self): |
| 280 |
server = Protocol(SERVER, max_size=3) |
| 281 |
server.receive_data(b"\x81\x84\x00\x00\x00\x00\xf0\x9f\x98\x80") |
| 282 |
self.assertIsInstance(server.parser_exc, PayloadTooBig) |
| 283 |
self.assertEqual( |
| 284 |
str(server.parser_exc), |
| 285 |
"frame with 4 bytes exceeds limit of 3 bytes", |
| 286 |
) |
| 287 |
self.assertConnectionFailing( |
| 288 |
server, |
| 289 |
CloseCode.MESSAGE_TOO_BIG, |
| 290 |
"frame with 4 bytes exceeds limit of 3 bytes", |
| 291 |
) |
| 292 |
|
| 293 |
def test_client_receives_text_without_size_limit(self): |
| 294 |
client = Protocol(CLIENT, max_size=None) |
| 295 |
client.receive_data(b"\x81\x04\xf0\x9f\x98\x80") |
| 296 |
self.assertFrameReceived( |
| 297 |
client, |
| 298 |
Frame(TEXT, "😀".encode()), |
| 299 |
) |
| 300 |
|
| 301 |
def test_server_receives_text_without_size_limit(self): |
| 302 |
server = Protocol(SERVER, max_size=None) |
| 303 |
server.receive_data(b"\x81\x84\x00\x00\x00\x00\xf0\x9f\x98\x80") |
| 304 |
self.assertFrameReceived( |
| 305 |
server, |
| 306 |
Frame(TEXT, "😀".encode()), |
| 307 |
) |
| 308 |
|
| 309 |
def test_client_sends_fragmented_text(self): |
| 310 |
client = Protocol(CLIENT) |
| 311 |
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"): |
| 312 |
client.send_text("😀".encode()[:2], fin=False) |
| 313 |
self.assertEqual(client.data_to_send(), [b"\x01\x82\x00\x00\x00\x00\xf0\x9f"]) |
| 314 |
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"): |
| 315 |
client.send_continuation("😀😀".encode()[2:6], fin=False) |
| 316 |
self.assertEqual( |
| 317 |
client.data_to_send(), [b"\x00\x84\x00\x00\x00\x00\x98\x80\xf0\x9f"] |
| 318 |
) |
| 319 |
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"): |
| 320 |
client.send_continuation("😀".encode()[2:], fin=True) |
| 321 |
self.assertEqual(client.data_to_send(), [b"\x80\x82\x00\x00\x00\x00\x98\x80"]) |
| 322 |
|
| 323 |
def test_server_sends_fragmented_text(self): |
| 324 |
server = Protocol(SERVER) |
| 325 |
server.send_text("😀".encode()[:2], fin=False) |
| 326 |
self.assertEqual(server.data_to_send(), [b"\x01\x02\xf0\x9f"]) |
| 327 |
server.send_continuation("😀😀".encode()[2:6], fin=False) |
| 328 |
self.assertEqual(server.data_to_send(), [b"\x00\x04\x98\x80\xf0\x9f"]) |
| 329 |
server.send_continuation("😀".encode()[2:], fin=True) |
| 330 |
self.assertEqual(server.data_to_send(), [b"\x80\x02\x98\x80"]) |
| 331 |
|
| 332 |
def test_client_receives_fragmented_text(self): |
| 333 |
client = Protocol(CLIENT) |
| 334 |
client.receive_data(b"\x01\x02\xf0\x9f") |
| 335 |
self.assertFrameReceived( |
| 336 |
client, |
| 337 |
Frame(TEXT, "😀".encode()[:2], fin=False), |
| 338 |
) |
| 339 |
client.receive_data(b"\x00\x04\x98\x80\xf0\x9f") |
| 340 |
self.assertFrameReceived( |
| 341 |
client, |
| 342 |
Frame(CONT, "😀😀".encode()[2:6], fin=False), |
| 343 |
) |
| 344 |
client.receive_data(b"\x80\x02\x98\x80") |
| 345 |
self.assertFrameReceived( |
| 346 |
client, |
| 347 |
Frame(CONT, "😀".encode()[2:]), |
| 348 |
) |
| 349 |
|
| 350 |
def test_server_receives_fragmented_text(self): |
| 351 |
server = Protocol(SERVER) |
| 352 |
server.receive_data(b"\x01\x82\x00\x00\x00\x00\xf0\x9f") |
| 353 |
self.assertFrameReceived( |
| 354 |
server, |
| 355 |
Frame(TEXT, "😀".encode()[:2], fin=False), |
| 356 |
) |
| 357 |
server.receive_data(b"\x00\x84\x00\x00\x00\x00\x98\x80\xf0\x9f") |
| 358 |
self.assertFrameReceived( |
| 359 |
server, |
| 360 |
Frame(CONT, "😀😀".encode()[2:6], fin=False), |
| 361 |
) |
| 362 |
server.receive_data(b"\x80\x82\x00\x00\x00\x00\x98\x80") |
| 363 |
self.assertFrameReceived( |
| 364 |
server, |
| 365 |
Frame(CONT, "😀".encode()[2:]), |
| 366 |
) |
| 367 |
|
| 368 |
def test_client_receives_fragmented_text_over_fragment_size_limit(self): |
| 369 |
client = Protocol(CLIENT, max_size=(None, 3)) |
| 370 |
client.receive_data(b"\x01\x04\xf0\x9f\x98\x80") |
| 371 |
self.assertIsInstance(client.parser_exc, PayloadTooBig) |
| 372 |
self.assertEqual( |
| 373 |
str(client.parser_exc), |
| 374 |
"frame with 4 bytes exceeds limit of 3 bytes", |
| 375 |
) |
| 376 |
|
| 377 |
def test_server_receives_fragmented_text_over_fragment_size_limit(self): |
| 378 |
server = Protocol(SERVER, max_size=(None, 3)) |
| 379 |
server.receive_data(b"\x01\x84\x00\x00\x00\x00\xf0\x9f\x98\x80") |
| 380 |
self.assertIsInstance(server.parser_exc, PayloadTooBig) |
| 381 |
self.assertEqual( |
| 382 |
str(server.parser_exc), |
| 383 |
"frame with 4 bytes exceeds limit of 3 bytes", |
| 384 |
) |
| 385 |
|
| 386 |
def test_client_receives_fragmented_text_over_message_size_limit(self): |
| 387 |
client = Protocol(CLIENT, max_size=(3, 2)) |
| 388 |
client.receive_data(b"\x01\x02\xf0\x9f") |
| 389 |
self.assertFrameReceived( |
| 390 |
client, |
| 391 |
Frame(TEXT, "😀".encode()[:2], fin=False), |
| 392 |
) |
| 393 |
client.receive_data(b"\x80\x02\x98\x80") |
| 394 |
self.assertIsInstance(client.parser_exc, PayloadTooBig) |
| 395 |
self.assertEqual( |
| 396 |
str(client.parser_exc), |
| 397 |
"frame with 2 bytes after reading 2 bytes exceeds limit of 3 bytes", |
| 398 |
) |
| 399 |
self.assertConnectionFailing( |
| 400 |
client, |
| 401 |
CloseCode.MESSAGE_TOO_BIG, |
| 402 |
"frame with 2 bytes after reading 2 bytes exceeds limit of 3 bytes", |
| 403 |
) |
| 404 |
|
| 405 |
def test_server_receives_fragmented_text_over_message_size_limit(self): |
| 406 |
server = Protocol(SERVER, max_size=(3, 2)) |
| 407 |
server.receive_data(b"\x01\x82\x00\x00\x00\x00\xf0\x9f") |
| 408 |
self.assertFrameReceived( |
| 409 |
server, |
| 410 |
Frame(TEXT, "😀".encode()[:2], fin=False), |
| 411 |
) |
| 412 |
server.receive_data(b"\x80\x82\x00\x00\x00\x00\x98\x80") |
| 413 |
self.assertIsInstance(server.parser_exc, PayloadTooBig) |
| 414 |
self.assertEqual( |
| 415 |
str(server.parser_exc), |
| 416 |
"frame with 2 bytes after reading 2 bytes exceeds limit of 3 bytes", |
| 417 |
) |
| 418 |
self.assertConnectionFailing( |
| 419 |
server, |
| 420 |
CloseCode.MESSAGE_TOO_BIG, |
| 421 |
"frame with 2 bytes after reading 2 bytes exceeds limit of 3 bytes", |
| 422 |
) |
| 423 |
|
| 424 |
def test_client_receives_fragmented_text_without_size_limit(self): |
| 425 |
client = Protocol(CLIENT, max_size=None) |
| 426 |
client.receive_data(b"\x01\x02\xf0\x9f") |
| 427 |
self.assertFrameReceived( |
| 428 |
client, |
| 429 |
Frame(TEXT, "😀".encode()[:2], fin=False), |
| 430 |
) |
| 431 |
client.receive_data(b"\x00\x04\x98\x80\xf0\x9f") |
| 432 |
self.assertFrameReceived( |
| 433 |
client, |
| 434 |
Frame(CONT, "😀😀".encode()[2:6], fin=False), |
| 435 |
) |
| 436 |
client.receive_data(b"\x80\x02\x98\x80") |
| 437 |
self.assertFrameReceived( |
| 438 |
client, |
| 439 |
Frame(CONT, "😀".encode()[2:]), |
| 440 |
) |
| 441 |
|
| 442 |
def test_server_receives_fragmented_text_without_size_limit(self): |
| 443 |
server = Protocol(SERVER, max_size=None) |
| 444 |
server.receive_data(b"\x01\x82\x00\x00\x00\x00\xf0\x9f") |
| 445 |
self.assertFrameReceived( |
| 446 |
server, |
| 447 |
Frame(TEXT, "😀".encode()[:2], fin=False), |
| 448 |
) |
| 449 |
server.receive_data(b"\x00\x84\x00\x00\x00\x00\x98\x80\xf0\x9f") |
| 450 |
self.assertFrameReceived( |
| 451 |
server, |
| 452 |
Frame(CONT, "😀😀".encode()[2:6], fin=False), |
| 453 |
) |
| 454 |
server.receive_data(b"\x80\x82\x00\x00\x00\x00\x98\x80") |
| 455 |
self.assertFrameReceived( |
| 456 |
server, |
| 457 |
Frame(CONT, "😀".encode()[2:]), |
| 458 |
) |
| 459 |
|
| 460 |
def test_client_sends_unexpected_text(self): |
| 461 |
client = Protocol(CLIENT) |
| 462 |
client.send_text(b"", fin=False) |
| 463 |
with self.assertRaises(ProtocolError) as raised: |
| 464 |
client.send_text(b"", fin=False) |
| 465 |
self.assertEqual(str(raised.exception), "expected a continuation frame") |
| 466 |
|
| 467 |
def test_server_sends_unexpected_text(self): |
| 468 |
server = Protocol(SERVER) |
| 469 |
server.send_text(b"", fin=False) |
| 470 |
with self.assertRaises(ProtocolError) as raised: |
| 471 |
server.send_text(b"", fin=False) |
| 472 |
self.assertEqual(str(raised.exception), "expected a continuation frame") |
| 473 |
|
| 474 |
def test_client_receives_unexpected_text(self): |
| 475 |
client = Protocol(CLIENT) |
| 476 |
client.receive_data(b"\x01\x00") |
| 477 |
self.assertFrameReceived( |
| 478 |
client, |
| 479 |
Frame(TEXT, b"", fin=False), |
| 480 |
) |
| 481 |
client.receive_data(b"\x01\x00") |
| 482 |
self.assertIsInstance(client.parser_exc, ProtocolError) |
| 483 |
self.assertEqual(str(client.parser_exc), "expected a continuation frame") |
| 484 |
self.assertConnectionFailing( |
| 485 |
client, CloseCode.PROTOCOL_ERROR, "expected a continuation frame" |
| 486 |
) |
| 487 |
|
| 488 |
def test_server_receives_unexpected_text(self): |
| 489 |
server = Protocol(SERVER) |
| 490 |
server.receive_data(b"\x01\x80\x00\x00\x00\x00") |
| 491 |
self.assertFrameReceived( |
| 492 |
server, |
| 493 |
Frame(TEXT, b"", fin=False), |
| 494 |
) |
| 495 |
server.receive_data(b"\x01\x80\x00\x00\x00\x00") |
| 496 |
self.assertIsInstance(server.parser_exc, ProtocolError) |
| 497 |
self.assertEqual(str(server.parser_exc), "expected a continuation frame") |
| 498 |
self.assertConnectionFailing( |
| 499 |
server, CloseCode.PROTOCOL_ERROR, "expected a continuation frame" |
| 500 |
) |
| 501 |
|
| 502 |
def test_client_sends_text_after_sending_close(self): |
| 503 |
client = Protocol(CLIENT) |
| 504 |
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"): |
| 505 |
client.send_close(CloseCode.GOING_AWAY) |
| 506 |
self.assertEqual(client.data_to_send(), [b"\x88\x82\x00\x00\x00\x00\x03\xe9"]) |
| 507 |
with self.assertRaises(InvalidState) as raised: |
| 508 |
client.send_text(b"") |
| 509 |
self.assertEqual(str(raised.exception), "connection is closing") |
| 510 |
|
| 511 |
def test_server_sends_text_after_sending_close(self): |
| 512 |
server = Protocol(SERVER) |
| 513 |
server.send_close(CloseCode.NORMAL_CLOSURE) |
| 514 |
self.assertEqual(server.data_to_send(), [b"\x88\x02\x03\xe8"]) |
| 515 |
with self.assertRaises(InvalidState) as raised: |
| 516 |
server.send_text(b"") |
| 517 |
self.assertEqual(str(raised.exception), "connection is closing") |
| 518 |
|
| 519 |
def test_client_receives_text_after_receiving_close(self): |
| 520 |
client = Protocol(CLIENT) |
| 521 |
client.receive_data(b"\x88\x02\x03\xe8") |
| 522 |
self.assertConnectionClosing(client, CloseCode.NORMAL_CLOSURE) |
| 523 |
client.receive_data(b"\x81\x00") |
| 524 |
self.assertFrameReceived(client, None) |
| 525 |
self.assertFrameSent(client, None) |
| 526 |
|
| 527 |
def test_server_receives_text_after_receiving_close(self): |
| 528 |
server = Protocol(SERVER) |
| 529 |
server.receive_data(b"\x88\x82\x00\x00\x00\x00\x03\xe9") |
| 530 |
self.assertConnectionClosing(server, CloseCode.GOING_AWAY) |
| 531 |
server.receive_data(b"\x81\x80\x00\xff\x00\xff") |
| 532 |
self.assertFrameReceived(server, None) |
| 533 |
self.assertFrameSent(server, None) |
| 534 |
|
| 535 |
|
| 536 |
class BinaryTests(ProtocolTestCase): |
| 537 |
""" |
| 538 |
Test binary frames and continuation frames. |
| 539 |
|
| 540 |
""" |
| 541 |
|
| 542 |
def test_client_sends_binary(self): |
| 543 |
client = Protocol(CLIENT) |
| 544 |
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"): |
| 545 |
client.send_binary(b"\x01\x02\xfe\xff") |
| 546 |
self.assertEqual( |
| 547 |
client.data_to_send(), [b"\x82\x84\x00\x00\x00\x00\x01\x02\xfe\xff"] |
| 548 |
) |
| 549 |
|
| 550 |
def test_server_sends_binary(self): |
| 551 |
server = Protocol(SERVER) |
| 552 |
server.send_binary(b"\x01\x02\xfe\xff") |
| 553 |
self.assertEqual(server.data_to_send(), [b"\x82\x04\x01\x02\xfe\xff"]) |
| 554 |
|
| 555 |
def test_client_receives_binary(self): |
| 556 |
client = Protocol(CLIENT) |
| 557 |
client.receive_data(b"\x82\x04\x01\x02\xfe\xff") |
| 558 |
self.assertFrameReceived( |
| 559 |
client, |
| 560 |
Frame(BINARY, b"\x01\x02\xfe\xff"), |
| 561 |
) |
| 562 |
|
| 563 |
def test_server_receives_binary(self): |
| 564 |
server = Protocol(SERVER) |
| 565 |
server.receive_data(b"\x82\x84\x00\x00\x00\x00\x01\x02\xfe\xff") |
| 566 |
self.assertFrameReceived( |
| 567 |
server, |
| 568 |
Frame(BINARY, b"\x01\x02\xfe\xff"), |
| 569 |
) |
| 570 |
|
| 571 |
def test_client_receives_binary_over_size_limit(self): |
| 572 |
client = Protocol(CLIENT, max_size=3) |
| 573 |
client.receive_data(b"\x82\x04\x01\x02\xfe\xff") |
| 574 |
self.assertIsInstance(client.parser_exc, PayloadTooBig) |
| 575 |
self.assertEqual( |
| 576 |
str(client.parser_exc), |
| 577 |
"frame with 4 bytes exceeds limit of 3 bytes", |
| 578 |
) |
| 579 |
self.assertConnectionFailing( |
| 580 |
client, |
| 581 |
CloseCode.MESSAGE_TOO_BIG, |
| 582 |
"frame with 4 bytes exceeds limit of 3 bytes", |
| 583 |
) |
| 584 |
|
| 585 |
def test_server_receives_binary_over_size_limit(self): |
| 586 |
server = Protocol(SERVER, max_size=3) |
| 587 |
server.receive_data(b"\x82\x84\x00\x00\x00\x00\x01\x02\xfe\xff") |
| 588 |
self.assertIsInstance(server.parser_exc, PayloadTooBig) |
| 589 |
self.assertEqual( |
| 590 |
str(server.parser_exc), |
| 591 |
"frame with 4 bytes exceeds limit of 3 bytes", |
| 592 |
) |
| 593 |
self.assertConnectionFailing( |
| 594 |
server, |
| 595 |
CloseCode.MESSAGE_TOO_BIG, |
| 596 |
"frame with 4 bytes exceeds limit of 3 bytes", |
| 597 |
) |
| 598 |
|
| 599 |
def test_client_sends_fragmented_binary(self): |
| 600 |
client = Protocol(CLIENT) |
| 601 |
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"): |
| 602 |
client.send_binary(b"\x01\x02", fin=False) |
| 603 |
self.assertEqual(client.data_to_send(), [b"\x02\x82\x00\x00\x00\x00\x01\x02"]) |
| 604 |
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"): |
| 605 |
client.send_continuation(b"\xee\xff\x01\x02", fin=False) |
| 606 |
self.assertEqual( |
| 607 |
client.data_to_send(), [b"\x00\x84\x00\x00\x00\x00\xee\xff\x01\x02"] |
| 608 |
) |
| 609 |
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"): |
| 610 |
client.send_continuation(b"\xee\xff", fin=True) |
| 611 |
self.assertEqual(client.data_to_send(), [b"\x80\x82\x00\x00\x00\x00\xee\xff"]) |
| 612 |
|
| 613 |
def test_server_sends_fragmented_binary(self): |
| 614 |
server = Protocol(SERVER) |
| 615 |
server.send_binary(b"\x01\x02", fin=False) |
| 616 |
self.assertEqual(server.data_to_send(), [b"\x02\x02\x01\x02"]) |
| 617 |
server.send_continuation(b"\xee\xff\x01\x02", fin=False) |
| 618 |
self.assertEqual(server.data_to_send(), [b"\x00\x04\xee\xff\x01\x02"]) |
| 619 |
server.send_continuation(b"\xee\xff", fin=True) |
| 620 |
self.assertEqual(server.data_to_send(), [b"\x80\x02\xee\xff"]) |
| 621 |
|
| 622 |
def test_client_receives_fragmented_binary(self): |
| 623 |
client = Protocol(CLIENT) |
| 624 |
client.receive_data(b"\x02\x02\x01\x02") |
| 625 |
self.assertFrameReceived( |
| 626 |
client, |
| 627 |
Frame(BINARY, b"\x01\x02", fin=False), |
| 628 |
) |
| 629 |
client.receive_data(b"\x00\x04\xfe\xff\x01\x02") |
| 630 |
self.assertFrameReceived( |
| 631 |
client, |
| 632 |
Frame(CONT, b"\xfe\xff\x01\x02", fin=False), |
| 633 |
) |
| 634 |
client.receive_data(b"\x80\x02\xfe\xff") |
| 635 |
self.assertFrameReceived( |
| 636 |
client, |
| 637 |
Frame(CONT, b"\xfe\xff"), |
| 638 |
) |
| 639 |
|
| 640 |
def test_server_receives_fragmented_binary(self): |
| 641 |
server = Protocol(SERVER) |
| 642 |
server.receive_data(b"\x02\x82\x00\x00\x00\x00\x01\x02") |
| 643 |
self.assertFrameReceived( |
| 644 |
server, |
| 645 |
Frame(BINARY, b"\x01\x02", fin=False), |
| 646 |
) |
| 647 |
server.receive_data(b"\x00\x84\x00\x00\x00\x00\xee\xff\x01\x02") |
| 648 |
self.assertFrameReceived( |
| 649 |
server, |
| 650 |
Frame(CONT, b"\xee\xff\x01\x02", fin=False), |
| 651 |
) |
| 652 |
server.receive_data(b"\x80\x82\x00\x00\x00\x00\xfe\xff") |
| 653 |
self.assertFrameReceived( |
| 654 |
server, |
| 655 |
Frame(CONT, b"\xfe\xff"), |
| 656 |
) |
| 657 |
|
| 658 |
def test_client_receives_fragmented_binary_over_size_limit(self): |
| 659 |
client = Protocol(CLIENT, max_size=3) |
| 660 |
client.receive_data(b"\x02\x02\x01\x02") |
| 661 |
self.assertFrameReceived( |
| 662 |
client, |
| 663 |
Frame(BINARY, b"\x01\x02", fin=False), |
| 664 |
) |
| 665 |
client.receive_data(b"\x80\x02\xfe\xff") |
| 666 |
self.assertIsInstance(client.parser_exc, PayloadTooBig) |
| 667 |
self.assertEqual( |
| 668 |
str(client.parser_exc), |
| 669 |
"frame with 2 bytes after reading 2 bytes exceeds limit of 3 bytes", |
| 670 |
) |
| 671 |
self.assertConnectionFailing( |
| 672 |
client, |
| 673 |
CloseCode.MESSAGE_TOO_BIG, |
| 674 |
"frame with 2 bytes after reading 2 bytes exceeds limit of 3 bytes", |
| 675 |
) |
| 676 |
|
| 677 |
def test_server_receives_fragmented_binary_over_size_limit(self): |
| 678 |
server = Protocol(SERVER, max_size=3) |
| 679 |
server.receive_data(b"\x02\x82\x00\x00\x00\x00\x01\x02") |
| 680 |
self.assertFrameReceived( |
| 681 |
server, |
| 682 |
Frame(BINARY, b"\x01\x02", fin=False), |
| 683 |
) |
| 684 |
server.receive_data(b"\x80\x82\x00\x00\x00\x00\xfe\xff") |
| 685 |
self.assertIsInstance(server.parser_exc, PayloadTooBig) |
| 686 |
self.assertEqual( |
| 687 |
str(server.parser_exc), |
| 688 |
"frame with 2 bytes after reading 2 bytes exceeds limit of 3 bytes", |
| 689 |
) |
| 690 |
self.assertConnectionFailing( |
| 691 |
server, |
| 692 |
CloseCode.MESSAGE_TOO_BIG, |
| 693 |
"frame with 2 bytes after reading 2 bytes exceeds limit of 3 bytes", |
| 694 |
) |
| 695 |
|
| 696 |
def test_client_sends_unexpected_binary(self): |
| 697 |
client = Protocol(CLIENT) |
| 698 |
client.send_binary(b"", fin=False) |
| 699 |
with self.assertRaises(ProtocolError) as raised: |
| 700 |
client.send_binary(b"", fin=False) |
| 701 |
self.assertEqual(str(raised.exception), "expected a continuation frame") |
| 702 |
|
| 703 |
def test_server_sends_unexpected_binary(self): |
| 704 |
server = Protocol(SERVER) |
| 705 |
server.send_binary(b"", fin=False) |
| 706 |
with self.assertRaises(ProtocolError) as raised: |
| 707 |
server.send_binary(b"", fin=False) |
| 708 |
self.assertEqual(str(raised.exception), "expected a continuation frame") |
| 709 |
|
| 710 |
def test_client_receives_unexpected_binary(self): |
| 711 |
client = Protocol(CLIENT) |
| 712 |
client.receive_data(b"\x02\x00") |
| 713 |
self.assertFrameReceived( |
| 714 |
client, |
| 715 |
Frame(BINARY, b"", fin=False), |
| 716 |
) |
| 717 |
client.receive_data(b"\x02\x00") |
| 718 |
self.assertIsInstance(client.parser_exc, ProtocolError) |
| 719 |
self.assertEqual(str(client.parser_exc), "expected a continuation frame") |
| 720 |
self.assertConnectionFailing( |
| 721 |
client, CloseCode.PROTOCOL_ERROR, "expected a continuation frame" |
| 722 |
) |
| 723 |
|
| 724 |
def test_server_receives_unexpected_binary(self): |
| 725 |
server = Protocol(SERVER) |
| 726 |
server.receive_data(b"\x02\x80\x00\x00\x00\x00") |
| 727 |
self.assertFrameReceived( |
| 728 |
server, |
| 729 |
Frame(BINARY, b"", fin=False), |
| 730 |
) |
| 731 |
server.receive_data(b"\x02\x80\x00\x00\x00\x00") |
| 732 |
self.assertIsInstance(server.parser_exc, ProtocolError) |
| 733 |
self.assertEqual(str(server.parser_exc), "expected a continuation frame") |
| 734 |
self.assertConnectionFailing( |
| 735 |
server, CloseCode.PROTOCOL_ERROR, "expected a continuation frame" |
| 736 |
) |
| 737 |
|
| 738 |
def test_client_sends_binary_after_sending_close(self): |
| 739 |
client = Protocol(CLIENT) |
| 740 |
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"): |
| 741 |
client.send_close(CloseCode.GOING_AWAY) |
| 742 |
self.assertEqual(client.data_to_send(), [b"\x88\x82\x00\x00\x00\x00\x03\xe9"]) |
| 743 |
with self.assertRaises(InvalidState) as raised: |
| 744 |
client.send_binary(b"") |
| 745 |
self.assertEqual(str(raised.exception), "connection is closing") |
| 746 |
|
| 747 |
def test_server_sends_binary_after_sending_close(self): |
| 748 |
server = Protocol(SERVER) |
| 749 |
server.send_close(CloseCode.NORMAL_CLOSURE) |
| 750 |
self.assertEqual(server.data_to_send(), [b"\x88\x02\x03\xe8"]) |
| 751 |
with self.assertRaises(InvalidState) as raised: |
| 752 |
server.send_binary(b"") |
| 753 |
self.assertEqual(str(raised.exception), "connection is closing") |
| 754 |
|
| 755 |
def test_client_receives_binary_after_receiving_close(self): |
| 756 |
client = Protocol(CLIENT) |
| 757 |
client.receive_data(b"\x88\x02\x03\xe8") |
| 758 |
self.assertConnectionClosing(client, CloseCode.NORMAL_CLOSURE) |
| 759 |
client.receive_data(b"\x82\x00") |
| 760 |
self.assertFrameReceived(client, None) |
| 761 |
self.assertFrameSent(client, None) |
| 762 |
|
| 763 |
def test_server_receives_binary_after_receiving_close(self): |
| 764 |
server = Protocol(SERVER) |
| 765 |
server.receive_data(b"\x88\x82\x00\x00\x00\x00\x03\xe9") |
| 766 |
self.assertConnectionClosing(server, CloseCode.GOING_AWAY) |
| 767 |
server.receive_data(b"\x82\x80\x00\xff\x00\xff") |
| 768 |
self.assertFrameReceived(server, None) |
| 769 |
self.assertFrameSent(server, None) |
| 770 |
|
| 771 |
|
| 772 |
class CloseTests(ProtocolTestCase): |
| 773 |
""" |
| 774 |
Test close frames. |
| 775 |
|
| 776 |
See RFC 6455: |
| 777 |
|
| 778 |
5.5.1. Close |
| 779 |
7.1.6. The WebSocket Connection Close Reason |
| 780 |
7.1.7. Fail the WebSocket Connection |
| 781 |
|
| 782 |
""" |
| 783 |
|
| 784 |
def test_close_code(self): |
| 785 |
client = Protocol(CLIENT) |
| 786 |
client.receive_data(b"\x88\x04\x03\xe8OK") |
| 787 |
client.receive_eof() |
| 788 |
self.assertEqual(client.close_code, CloseCode.NORMAL_CLOSURE) |
| 789 |
|
| 790 |
def test_close_reason(self): |
| 791 |
server = Protocol(SERVER) |
| 792 |
server.receive_data(b"\x88\x84\x00\x00\x00\x00\x03\xe8OK") |
| 793 |
server.receive_eof() |
| 794 |
self.assertEqual(server.close_reason, "OK") |
| 795 |
|
| 796 |
def test_close_code_not_provided(self): |
| 797 |
server = Protocol(SERVER) |
| 798 |
server.receive_data(b"\x88\x80\x00\x00\x00\x00") |
| 799 |
server.receive_eof() |
| 800 |
self.assertEqual(server.close_code, CloseCode.NO_STATUS_RCVD) |
| 801 |
|
| 802 |
def test_close_reason_not_provided(self): |
| 803 |
client = Protocol(CLIENT) |
| 804 |
client.receive_data(b"\x88\x00") |
| 805 |
client.receive_eof() |
| 806 |
self.assertEqual(client.close_reason, "") |
| 807 |
|
| 808 |
def test_close_code_not_available(self): |
| 809 |
client = Protocol(CLIENT) |
| 810 |
client.receive_eof() |
| 811 |
self.assertEqual(client.close_code, CloseCode.ABNORMAL_CLOSURE) |
| 812 |
|
| 813 |
def test_close_reason_not_available(self): |
| 814 |
server = Protocol(SERVER) |
| 815 |
server.receive_eof() |
| 816 |
self.assertEqual(server.close_reason, "") |
| 817 |
|
| 818 |
def test_close_code_not_available_yet(self): |
| 819 |
server = Protocol(SERVER) |
| 820 |
self.assertIsNone(server.close_code) |
| 821 |
|
| 822 |
def test_close_reason_not_available_yet(self): |
| 823 |
client = Protocol(CLIENT) |
| 824 |
self.assertIsNone(client.close_reason) |
| 825 |
|
| 826 |
def test_client_sends_close(self): |
| 827 |
client = Protocol(CLIENT) |
| 828 |
with patch("secrets.token_bytes", return_value=b"\x3c\x3c\x3c\x3c"): |
| 829 |
client.send_close() |
| 830 |
self.assertEqual(client.data_to_send(), [b"\x88\x80\x3c\x3c\x3c\x3c"]) |
| 831 |
self.assertIs(client.state, CLOSING) |
| 832 |
|
| 833 |
def test_server_sends_close(self): |
| 834 |
server = Protocol(SERVER) |
| 835 |
server.send_close() |
| 836 |
self.assertEqual(server.data_to_send(), [b"\x88\x00"]) |
| 837 |
self.assertIs(server.state, CLOSING) |
| 838 |
|
| 839 |
def test_client_receives_close(self): |
| 840 |
client = Protocol(CLIENT) |
| 841 |
with patch("secrets.token_bytes", return_value=b"\x3c\x3c\x3c\x3c"): |
| 842 |
client.receive_data(b"\x88\x00") |
| 843 |
self.assertEqual(client.events_received(), [Frame(CLOSE, b"")]) |
| 844 |
self.assertEqual(client.data_to_send(), [b"\x88\x80\x3c\x3c\x3c\x3c"]) |
| 845 |
self.assertIs(client.state, CLOSING) |
| 846 |
|
| 847 |
def test_server_receives_close(self): |
| 848 |
server = Protocol(SERVER) |
| 849 |
server.receive_data(b"\x88\x80\x3c\x3c\x3c\x3c") |
| 850 |
self.assertEqual(server.events_received(), [Frame(CLOSE, b"")]) |
| 851 |
self.assertEqual(server.data_to_send(), [b"\x88\x00", b""]) |
| 852 |
self.assertIs(server.state, CLOSING) |
| 853 |
|
| 854 |
def test_client_sends_close_then_receives_close(self): |
| 855 |
# Client-initiated close handshake on the client side. |
| 856 |
client = Protocol(CLIENT) |
| 857 |
|
| 858 |
client.send_close() |
| 859 |
self.assertFrameReceived(client, None) |
| 860 |
self.assertFrameSent(client, Frame(CLOSE, b"")) |
| 861 |
|
| 862 |
client.receive_data(b"\x88\x00") |
| 863 |
self.assertFrameReceived(client, Frame(CLOSE, b"")) |
| 864 |
self.assertFrameSent(client, None) |
| 865 |
|
| 866 |
client.receive_eof() |
| 867 |
self.assertFrameReceived(client, None) |
| 868 |
self.assertFrameSent(client, None, eof=True) |
| 869 |
|
| 870 |
def test_server_sends_close_then_receives_close(self): |
| 871 |
# Server-initiated close handshake on the server side. |
| 872 |
server = Protocol(SERVER) |
| 873 |
|
| 874 |
server.send_close() |
| 875 |
self.assertFrameReceived(server, None) |
| 876 |
self.assertFrameSent(server, Frame(CLOSE, b"")) |
| 877 |
|
| 878 |
server.receive_data(b"\x88\x80\x3c\x3c\x3c\x3c") |
| 879 |
self.assertFrameReceived(server, Frame(CLOSE, b"")) |
| 880 |
self.assertFrameSent(server, None, eof=True) |
| 881 |
|
| 882 |
server.receive_eof() |
| 883 |
self.assertFrameReceived(server, None) |
| 884 |
self.assertFrameSent(server, None) |
| 885 |
|
| 886 |
def test_client_receives_close_then_sends_close(self): |
| 887 |
# Server-initiated close handshake on the client side. |
| 888 |
client = Protocol(CLIENT) |
| 889 |
|
| 890 |
client.receive_data(b"\x88\x00") |
| 891 |
self.assertFrameReceived(client, Frame(CLOSE, b"")) |
| 892 |
self.assertFrameSent(client, Frame(CLOSE, b"")) |
| 893 |
|
| 894 |
client.receive_eof() |
| 895 |
self.assertFrameReceived(client, None) |
| 896 |
self.assertFrameSent(client, None, eof=True) |
| 897 |
|
| 898 |
def test_server_receives_close_then_sends_close(self): |
| 899 |
# Client-initiated close handshake on the server side. |
| 900 |
server = Protocol(SERVER) |
| 901 |
|
| 902 |
server.receive_data(b"\x88\x80\x3c\x3c\x3c\x3c") |
| 903 |
self.assertFrameReceived(server, Frame(CLOSE, b"")) |
| 904 |
self.assertFrameSent(server, Frame(CLOSE, b""), eof=True) |
| 905 |
|
| 906 |
server.receive_eof() |
| 907 |
self.assertFrameReceived(server, None) |
| 908 |
self.assertFrameSent(server, None) |
| 909 |
|
| 910 |
def test_client_sends_close_with_code(self): |
| 911 |
client = Protocol(CLIENT) |
| 912 |
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"): |
| 913 |
client.send_close(CloseCode.GOING_AWAY) |
| 914 |
self.assertEqual(client.data_to_send(), [b"\x88\x82\x00\x00\x00\x00\x03\xe9"]) |
| 915 |
self.assertIs(client.state, CLOSING) |
| 916 |
|
| 917 |
def test_server_sends_close_with_code(self): |
| 918 |
server = Protocol(SERVER) |
| 919 |
server.send_close(CloseCode.NORMAL_CLOSURE) |
| 920 |
self.assertEqual(server.data_to_send(), [b"\x88\x02\x03\xe8"]) |
| 921 |
self.assertIs(server.state, CLOSING) |
| 922 |
|
| 923 |
def test_client_receives_close_with_code(self): |
| 924 |
client = Protocol(CLIENT) |
| 925 |
client.receive_data(b"\x88\x02\x03\xe8") |
| 926 |
self.assertConnectionClosing(client, CloseCode.NORMAL_CLOSURE, "") |
| 927 |
self.assertIs(client.state, CLOSING) |
| 928 |
|
| 929 |
def test_server_receives_close_with_code(self): |
| 930 |
server = Protocol(SERVER) |
| 931 |
server.receive_data(b"\x88\x82\x00\x00\x00\x00\x03\xe9") |
| 932 |
self.assertConnectionClosing(server, CloseCode.GOING_AWAY, "") |
| 933 |
self.assertIs(server.state, CLOSING) |
| 934 |
|
| 935 |
def test_client_sends_close_with_code_and_reason(self): |
| 936 |
client = Protocol(CLIENT) |
| 937 |
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"): |
| 938 |
client.send_close(CloseCode.GOING_AWAY, "going away") |
| 939 |
self.assertEqual( |
| 940 |
client.data_to_send(), [b"\x88\x8c\x00\x00\x00\x00\x03\xe9going away"] |
| 941 |
) |
| 942 |
self.assertIs(client.state, CLOSING) |
| 943 |
|
| 944 |
def test_server_sends_close_with_code_and_reason(self): |
| 945 |
server = Protocol(SERVER) |
| 946 |
server.send_close(CloseCode.NORMAL_CLOSURE, "OK") |
| 947 |
self.assertEqual(server.data_to_send(), [b"\x88\x04\x03\xe8OK"]) |
| 948 |
self.assertIs(server.state, CLOSING) |
| 949 |
|
| 950 |
def test_client_receives_close_with_code_and_reason(self): |
| 951 |
client = Protocol(CLIENT) |
| 952 |
client.receive_data(b"\x88\x04\x03\xe8OK") |
| 953 |
self.assertConnectionClosing(client, CloseCode.NORMAL_CLOSURE, "OK") |
| 954 |
self.assertIs(client.state, CLOSING) |
| 955 |
|
| 956 |
def test_server_receives_close_with_code_and_reason(self): |
| 957 |
server = Protocol(SERVER) |
| 958 |
server.receive_data(b"\x88\x8c\x00\x00\x00\x00\x03\xe9going away") |
| 959 |
self.assertConnectionClosing(server, CloseCode.GOING_AWAY, "going away") |
| 960 |
self.assertIs(server.state, CLOSING) |
| 961 |
|
| 962 |
def test_client_sends_close_with_reason_only(self): |
| 963 |
client = Protocol(CLIENT) |
| 964 |
with self.assertRaises(ProtocolError) as raised: |
| 965 |
client.send_close(reason="going away") |
| 966 |
self.assertEqual(str(raised.exception), "cannot send a reason without a code") |
| 967 |
|
| 968 |
def test_server_sends_close_with_reason_only(self): |
| 969 |
server = Protocol(SERVER) |
| 970 |
with self.assertRaises(ProtocolError) as raised: |
| 971 |
server.send_close(reason="OK") |
| 972 |
self.assertEqual(str(raised.exception), "cannot send a reason without a code") |
| 973 |
|
| 974 |
def test_client_receives_close_with_truncated_code(self): |
| 975 |
client = Protocol(CLIENT) |
| 976 |
client.receive_data(b"\x88\x01\x03") |
| 977 |
self.assertIsInstance(client.parser_exc, ProtocolError) |
| 978 |
self.assertEqual(str(client.parser_exc), "close frame too short") |
| 979 |
self.assertConnectionFailing( |
| 980 |
client, CloseCode.PROTOCOL_ERROR, "close frame too short" |
| 981 |
) |
| 982 |
self.assertIs(client.state, CLOSING) |
| 983 |
|
| 984 |
def test_server_receives_close_with_truncated_code(self): |
| 985 |
server = Protocol(SERVER) |
| 986 |
server.receive_data(b"\x88\x81\x00\x00\x00\x00\x03") |
| 987 |
self.assertIsInstance(server.parser_exc, ProtocolError) |
| 988 |
self.assertEqual(str(server.parser_exc), "close frame too short") |
| 989 |
self.assertConnectionFailing( |
| 990 |
server, CloseCode.PROTOCOL_ERROR, "close frame too short" |
| 991 |
) |
| 992 |
self.assertIs(server.state, CLOSING) |
| 993 |
|
| 994 |
def test_client_receives_close_with_non_utf8_reason(self): |
| 995 |
client = Protocol(CLIENT) |
| 996 |
|
| 997 |
client.receive_data(b"\x88\x04\x03\xe8\xff\xff") |
| 998 |
self.assertIsInstance(client.parser_exc, UnicodeDecodeError) |
| 999 |
self.assertEqual( |
| 1000 |
str(client.parser_exc), |
| 1001 |
"'utf-8' codec can't decode byte 0xff in position 0: invalid start byte", |
| 1002 |
) |
| 1003 |
self.assertConnectionFailing( |
| 1004 |
client, CloseCode.INVALID_DATA, "invalid start byte at position 0" |
| 1005 |
) |
| 1006 |
self.assertIs(client.state, CLOSING) |
| 1007 |
|
| 1008 |
def test_server_receives_close_with_non_utf8_reason(self): |
| 1009 |
server = Protocol(SERVER) |
| 1010 |
|
| 1011 |
server.receive_data(b"\x88\x84\x00\x00\x00\x00\x03\xe9\xff\xff") |
| 1012 |
self.assertIsInstance(server.parser_exc, UnicodeDecodeError) |
| 1013 |
self.assertEqual( |
| 1014 |
str(server.parser_exc), |
| 1015 |
"'utf-8' codec can't decode byte 0xff in position 0: invalid start byte", |
| 1016 |
) |
| 1017 |
self.assertConnectionFailing( |
| 1018 |
server, CloseCode.INVALID_DATA, "invalid start byte at position 0" |
| 1019 |
) |
| 1020 |
self.assertIs(server.state, CLOSING) |
| 1021 |
|
| 1022 |
def test_client_sends_close_twice(self): |
| 1023 |
client = Protocol(CLIENT) |
| 1024 |
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"): |
| 1025 |
client.send_close(CloseCode.GOING_AWAY) |
| 1026 |
self.assertEqual(client.data_to_send(), [b"\x88\x82\x00\x00\x00\x00\x03\xe9"]) |
| 1027 |
with self.assertRaises(InvalidState) as raised: |
| 1028 |
client.send_close(CloseCode.GOING_AWAY) |
| 1029 |
self.assertEqual(str(raised.exception), "connection is closing") |
| 1030 |
|
| 1031 |
def test_server_sends_close_twice(self): |
| 1032 |
server = Protocol(SERVER) |
| 1033 |
server.send_close(CloseCode.NORMAL_CLOSURE) |
| 1034 |
self.assertEqual(server.data_to_send(), [b"\x88\x02\x03\xe8"]) |
| 1035 |
with self.assertRaises(InvalidState) as raised: |
| 1036 |
server.send_close(CloseCode.NORMAL_CLOSURE) |
| 1037 |
self.assertEqual(str(raised.exception), "connection is closing") |
| 1038 |
|
| 1039 |
def test_client_sends_close_after_connection_is_closed(self): |
| 1040 |
client = Protocol(CLIENT) |
| 1041 |
client.receive_eof() |
| 1042 |
with self.assertRaises(InvalidState) as raised: |
| 1043 |
client.send_close(CloseCode.GOING_AWAY) |
| 1044 |
self.assertEqual(str(raised.exception), "connection is closed") |
| 1045 |
|
| 1046 |
def test_server_sends_close_after_connection_is_closed(self): |
| 1047 |
server = Protocol(SERVER) |
| 1048 |
server.receive_eof() |
| 1049 |
with self.assertRaises(InvalidState) as raised: |
| 1050 |
server.send_close(CloseCode.NORMAL_CLOSURE) |
| 1051 |
self.assertEqual(str(raised.exception), "connection is closed") |
| 1052 |
|
| 1053 |
|
| 1054 |
class PingTests(ProtocolTestCase): |
| 1055 |
""" |
| 1056 |
Test ping. See 5.5.2. Ping in RFC 6455. |
| 1057 |
|
| 1058 |
""" |
| 1059 |
|
| 1060 |
def test_client_sends_ping(self): |
| 1061 |
client = Protocol(CLIENT) |
| 1062 |
with patch("secrets.token_bytes", return_value=b"\x00\x44\x88\xcc"): |
| 1063 |
client.send_ping(b"") |
| 1064 |
self.assertEqual(client.data_to_send(), [b"\x89\x80\x00\x44\x88\xcc"]) |
| 1065 |
|
| 1066 |
def test_server_sends_ping(self): |
| 1067 |
server = Protocol(SERVER) |
| 1068 |
server.send_ping(b"") |
| 1069 |
self.assertEqual(server.data_to_send(), [b"\x89\x00"]) |
| 1070 |
|
| 1071 |
def test_client_receives_ping(self): |
| 1072 |
client = Protocol(CLIENT) |
| 1073 |
client.receive_data(b"\x89\x00") |
| 1074 |
self.assertFrameReceived( |
| 1075 |
client, |
| 1076 |
Frame(PING, b""), |
| 1077 |
) |
| 1078 |
self.assertFrameSent( |
| 1079 |
client, |
| 1080 |
Frame(PONG, b""), |
| 1081 |
) |
| 1082 |
|
| 1083 |
def test_server_receives_ping(self): |
| 1084 |
server = Protocol(SERVER) |
| 1085 |
server.receive_data(b"\x89\x80\x00\x44\x88\xcc") |
| 1086 |
self.assertFrameReceived( |
| 1087 |
server, |
| 1088 |
Frame(PING, b""), |
| 1089 |
) |
| 1090 |
self.assertFrameSent( |
| 1091 |
server, |
| 1092 |
Frame(PONG, b""), |
| 1093 |
) |
| 1094 |
|
| 1095 |
def test_client_sends_ping_with_data(self): |
| 1096 |
client = Protocol(CLIENT) |
| 1097 |
with patch("secrets.token_bytes", return_value=b"\x00\x44\x88\xcc"): |
| 1098 |
client.send_ping(b"\x22\x66\xaa\xee") |
| 1099 |
self.assertEqual( |
| 1100 |
client.data_to_send(), [b"\x89\x84\x00\x44\x88\xcc\x22\x22\x22\x22"] |
| 1101 |
) |
| 1102 |
|
| 1103 |
def test_server_sends_ping_with_data(self): |
| 1104 |
server = Protocol(SERVER) |
| 1105 |
server.send_ping(b"\x22\x66\xaa\xee") |
| 1106 |
self.assertEqual(server.data_to_send(), [b"\x89\x04\x22\x66\xaa\xee"]) |
| 1107 |
|
| 1108 |
def test_client_receives_ping_with_data(self): |
| 1109 |
client = Protocol(CLIENT) |
| 1110 |
client.receive_data(b"\x89\x04\x22\x66\xaa\xee") |
| 1111 |
self.assertFrameReceived( |
| 1112 |
client, |
| 1113 |
Frame(PING, b"\x22\x66\xaa\xee"), |
| 1114 |
) |
| 1115 |
self.assertFrameSent( |
| 1116 |
client, |
| 1117 |
Frame(PONG, b"\x22\x66\xaa\xee"), |
| 1118 |
) |
| 1119 |
|
| 1120 |
def test_server_receives_ping_with_data(self): |
| 1121 |
server = Protocol(SERVER) |
| 1122 |
server.receive_data(b"\x89\x84\x00\x44\x88\xcc\x22\x22\x22\x22") |
| 1123 |
self.assertFrameReceived( |
| 1124 |
server, |
| 1125 |
Frame(PING, b"\x22\x66\xaa\xee"), |
| 1126 |
) |
| 1127 |
self.assertFrameSent( |
| 1128 |
server, |
| 1129 |
Frame(PONG, b"\x22\x66\xaa\xee"), |
| 1130 |
) |
| 1131 |
|
| 1132 |
def test_client_sends_fragmented_ping_frame(self): |
| 1133 |
client = Protocol(CLIENT) |
| 1134 |
# This is only possible through a private API. |
| 1135 |
with self.assertRaises(ProtocolError) as raised: |
| 1136 |
client.send_frame(Frame(PING, b"", fin=False)) |
| 1137 |
self.assertEqual(str(raised.exception), "fragmented control frame") |
| 1138 |
|
| 1139 |
def test_server_sends_fragmented_ping_frame(self): |
| 1140 |
server = Protocol(SERVER) |
| 1141 |
# This is only possible through a private API. |
| 1142 |
with self.assertRaises(ProtocolError) as raised: |
| 1143 |
server.send_frame(Frame(PING, b"", fin=False)) |
| 1144 |
self.assertEqual(str(raised.exception), "fragmented control frame") |
| 1145 |
|
| 1146 |
def test_client_receives_fragmented_ping_frame(self): |
| 1147 |
client = Protocol(CLIENT) |
| 1148 |
client.receive_data(b"\x09\x00") |
| 1149 |
self.assertIsInstance(client.parser_exc, ProtocolError) |
| 1150 |
self.assertEqual(str(client.parser_exc), "fragmented control frame") |
| 1151 |
self.assertConnectionFailing( |
| 1152 |
client, CloseCode.PROTOCOL_ERROR, "fragmented control frame" |
| 1153 |
) |
| 1154 |
|
| 1155 |
def test_server_receives_fragmented_ping_frame(self): |
| 1156 |
server = Protocol(SERVER) |
| 1157 |
server.receive_data(b"\x09\x80\x3c\x3c\x3c\x3c") |
| 1158 |
self.assertIsInstance(server.parser_exc, ProtocolError) |
| 1159 |
self.assertEqual(str(server.parser_exc), "fragmented control frame") |
| 1160 |
self.assertConnectionFailing( |
| 1161 |
server, CloseCode.PROTOCOL_ERROR, "fragmented control frame" |
| 1162 |
) |
| 1163 |
|
| 1164 |
def test_client_sends_ping_after_sending_close(self): |
| 1165 |
client = Protocol(CLIENT) |
| 1166 |
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"): |
| 1167 |
client.send_close(CloseCode.GOING_AWAY) |
| 1168 |
self.assertEqual(client.data_to_send(), [b"\x88\x82\x00\x00\x00\x00\x03\xe9"]) |
| 1169 |
with patch("secrets.token_bytes", return_value=b"\x00\x44\x88\xcc"): |
| 1170 |
client.send_ping(b"") |
| 1171 |
self.assertEqual(client.data_to_send(), [b"\x89\x80\x00\x44\x88\xcc"]) |
| 1172 |
|
| 1173 |
def test_server_sends_ping_after_sending_close(self): |
| 1174 |
server = Protocol(SERVER) |
| 1175 |
server.send_close(CloseCode.NORMAL_CLOSURE) |
| 1176 |
self.assertEqual(server.data_to_send(), [b"\x88\x02\x03\xe8"]) |
| 1177 |
server.send_ping(b"") |
| 1178 |
self.assertEqual(server.data_to_send(), [b"\x89\x00"]) |
| 1179 |
|
| 1180 |
def test_client_receives_ping_after_receiving_close(self): |
| 1181 |
client = Protocol(CLIENT) |
| 1182 |
client.receive_data(b"\x88\x02\x03\xe8") |
| 1183 |
self.assertConnectionClosing(client, CloseCode.NORMAL_CLOSURE) |
| 1184 |
client.receive_data(b"\x89\x04\x22\x66\xaa\xee") |
| 1185 |
# websockets ignores control frames after a close frame. |
| 1186 |
self.assertFrameReceived(client, None) |
| 1187 |
self.assertFrameSent(client, None) |
| 1188 |
|
| 1189 |
def test_server_receives_ping_after_receiving_close(self): |
| 1190 |
server = Protocol(SERVER) |
| 1191 |
server.receive_data(b"\x88\x82\x00\x00\x00\x00\x03\xe9") |
| 1192 |
self.assertConnectionClosing(server, CloseCode.GOING_AWAY) |
| 1193 |
server.receive_data(b"\x89\x84\x00\x44\x88\xcc\x22\x22\x22\x22") |
| 1194 |
# websockets ignores control frames after a close frame. |
| 1195 |
self.assertFrameReceived(server, None) |
| 1196 |
self.assertFrameSent(server, None) |
| 1197 |
|
| 1198 |
def test_client_sends_ping_after_connection_is_closed(self): |
| 1199 |
client = Protocol(CLIENT) |
| 1200 |
client.receive_eof() |
| 1201 |
with self.assertRaises(InvalidState) as raised: |
| 1202 |
client.send_ping(b"") |
| 1203 |
self.assertEqual(str(raised.exception), "connection is closed") |
| 1204 |
|
| 1205 |
def test_server_sends_ping_after_connection_is_closed(self): |
| 1206 |
server = Protocol(SERVER) |
| 1207 |
server.receive_eof() |
| 1208 |
with self.assertRaises(InvalidState) as raised: |
| 1209 |
server.send_ping(b"") |
| 1210 |
self.assertEqual(str(raised.exception), "connection is closed") |
| 1211 |
|
| 1212 |
|
| 1213 |
class PongTests(ProtocolTestCase): |
| 1214 |
""" |
| 1215 |
Test pong frames. See 5.5.3. Pong in RFC 6455. |
| 1216 |
|
| 1217 |
""" |
| 1218 |
|
| 1219 |
def test_client_sends_pong(self): |
| 1220 |
client = Protocol(CLIENT) |
| 1221 |
with patch("secrets.token_bytes", return_value=b"\x00\x44\x88\xcc"): |
| 1222 |
client.send_pong(b"") |
| 1223 |
self.assertEqual(client.data_to_send(), [b"\x8a\x80\x00\x44\x88\xcc"]) |
| 1224 |
|
| 1225 |
def test_server_sends_pong(self): |
| 1226 |
server = Protocol(SERVER) |
| 1227 |
server.send_pong(b"") |
| 1228 |
self.assertEqual(server.data_to_send(), [b"\x8a\x00"]) |
| 1229 |
|
| 1230 |
def test_client_receives_pong(self): |
| 1231 |
client = Protocol(CLIENT) |
| 1232 |
client.receive_data(b"\x8a\x00") |
| 1233 |
self.assertFrameReceived( |
| 1234 |
client, |
| 1235 |
Frame(PONG, b""), |
| 1236 |
) |
| 1237 |
|
| 1238 |
def test_server_receives_pong(self): |
| 1239 |
server = Protocol(SERVER) |
| 1240 |
server.receive_data(b"\x8a\x80\x00\x44\x88\xcc") |
| 1241 |
self.assertFrameReceived( |
| 1242 |
server, |
| 1243 |
Frame(PONG, b""), |
| 1244 |
) |
| 1245 |
|
| 1246 |
def test_client_sends_pong_with_data(self): |
| 1247 |
client = Protocol(CLIENT) |
| 1248 |
with patch("secrets.token_bytes", return_value=b"\x00\x44\x88\xcc"): |
| 1249 |
client.send_pong(b"\x22\x66\xaa\xee") |
| 1250 |
self.assertEqual( |
| 1251 |
client.data_to_send(), [b"\x8a\x84\x00\x44\x88\xcc\x22\x22\x22\x22"] |
| 1252 |
) |
| 1253 |
|
| 1254 |
def test_server_sends_pong_with_data(self): |
| 1255 |
server = Protocol(SERVER) |
| 1256 |
server.send_pong(b"\x22\x66\xaa\xee") |
| 1257 |
self.assertEqual(server.data_to_send(), [b"\x8a\x04\x22\x66\xaa\xee"]) |
| 1258 |
|
| 1259 |
def test_client_receives_pong_with_data(self): |
| 1260 |
client = Protocol(CLIENT) |
| 1261 |
client.receive_data(b"\x8a\x04\x22\x66\xaa\xee") |
| 1262 |
self.assertFrameReceived( |
| 1263 |
client, |
| 1264 |
Frame(PONG, b"\x22\x66\xaa\xee"), |
| 1265 |
) |
| 1266 |
|
| 1267 |
def test_server_receives_pong_with_data(self): |
| 1268 |
server = Protocol(SERVER) |
| 1269 |
server.receive_data(b"\x8a\x84\x00\x44\x88\xcc\x22\x22\x22\x22") |
| 1270 |
self.assertFrameReceived( |
| 1271 |
server, |
| 1272 |
Frame(PONG, b"\x22\x66\xaa\xee"), |
| 1273 |
) |
| 1274 |
|
| 1275 |
def test_client_sends_fragmented_pong_frame(self): |
| 1276 |
client = Protocol(CLIENT) |
| 1277 |
# This is only possible through a private API. |
| 1278 |
with self.assertRaises(ProtocolError) as raised: |
| 1279 |
client.send_frame(Frame(PONG, b"", fin=False)) |
| 1280 |
self.assertEqual(str(raised.exception), "fragmented control frame") |
| 1281 |
|
| 1282 |
def test_server_sends_fragmented_pong_frame(self): |
| 1283 |
server = Protocol(SERVER) |
| 1284 |
# This is only possible through a private API. |
| 1285 |
with self.assertRaises(ProtocolError) as raised: |
| 1286 |
server.send_frame(Frame(PONG, b"", fin=False)) |
| 1287 |
self.assertEqual(str(raised.exception), "fragmented control frame") |
| 1288 |
|
| 1289 |
def test_client_receives_fragmented_pong_frame(self): |
| 1290 |
client = Protocol(CLIENT) |
| 1291 |
client.receive_data(b"\x0a\x00") |
| 1292 |
self.assertIsInstance(client.parser_exc, ProtocolError) |
| 1293 |
self.assertEqual(str(client.parser_exc), "fragmented control frame") |
| 1294 |
self.assertConnectionFailing( |
| 1295 |
client, CloseCode.PROTOCOL_ERROR, "fragmented control frame" |
| 1296 |
) |
| 1297 |
|
| 1298 |
def test_server_receives_fragmented_pong_frame(self): |
| 1299 |
server = Protocol(SERVER) |
| 1300 |
server.receive_data(b"\x0a\x80\x3c\x3c\x3c\x3c") |
| 1301 |
self.assertIsInstance(server.parser_exc, ProtocolError) |
| 1302 |
self.assertEqual(str(server.parser_exc), "fragmented control frame") |
| 1303 |
self.assertConnectionFailing( |
| 1304 |
server, CloseCode.PROTOCOL_ERROR, "fragmented control frame" |
| 1305 |
) |
| 1306 |
|
| 1307 |
def test_client_sends_pong_after_sending_close(self): |
| 1308 |
client = Protocol(CLIENT) |
| 1309 |
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"): |
| 1310 |
client.send_close(CloseCode.GOING_AWAY) |
| 1311 |
self.assertEqual(client.data_to_send(), [b"\x88\x82\x00\x00\x00\x00\x03\xe9"]) |
| 1312 |
with patch("secrets.token_bytes", return_value=b"\x00\x44\x88\xcc"): |
| 1313 |
client.send_pong(b"") |
| 1314 |
self.assertEqual(client.data_to_send(), [b"\x8a\x80\x00\x44\x88\xcc"]) |
| 1315 |
|
| 1316 |
def test_server_sends_pong_after_sending_close(self): |
| 1317 |
server = Protocol(SERVER) |
| 1318 |
server.send_close(CloseCode.NORMAL_CLOSURE) |
| 1319 |
self.assertEqual(server.data_to_send(), [b"\x88\x02\x03\xe8"]) |
| 1320 |
server.send_pong(b"") |
| 1321 |
self.assertEqual(server.data_to_send(), [b"\x8a\x00"]) |
| 1322 |
|
| 1323 |
def test_client_receives_pong_after_receiving_close(self): |
| 1324 |
client = Protocol(CLIENT) |
| 1325 |
client.receive_data(b"\x88\x02\x03\xe8") |
| 1326 |
self.assertConnectionClosing(client, CloseCode.NORMAL_CLOSURE) |
| 1327 |
client.receive_data(b"\x8a\x04\x22\x66\xaa\xee") |
| 1328 |
# websockets ignores control frames after a close frame. |
| 1329 |
self.assertFrameReceived(client, None) |
| 1330 |
self.assertFrameSent(client, None) |
| 1331 |
|
| 1332 |
def test_server_receives_pong_after_receiving_close(self): |
| 1333 |
server = Protocol(SERVER) |
| 1334 |
server.receive_data(b"\x88\x82\x00\x00\x00\x00\x03\xe9") |
| 1335 |
self.assertConnectionClosing(server, CloseCode.GOING_AWAY) |
| 1336 |
server.receive_data(b"\x8a\x84\x00\x44\x88\xcc\x22\x22\x22\x22") |
| 1337 |
# websockets ignores control frames after a close frame. |
| 1338 |
self.assertFrameReceived(server, None) |
| 1339 |
self.assertFrameSent(server, None) |
| 1340 |
|
| 1341 |
def test_client_sends_pong_after_connection_is_closed(self): |
| 1342 |
client = Protocol(CLIENT) |
| 1343 |
client.receive_eof() |
| 1344 |
with self.assertRaises(InvalidState) as raised: |
| 1345 |
client.send_pong(b"") |
| 1346 |
self.assertEqual(str(raised.exception), "connection is closed") |
| 1347 |
|
| 1348 |
def test_server_sends_pong_after_connection_is_closed(self): |
| 1349 |
server = Protocol(SERVER) |
| 1350 |
server.receive_eof() |
| 1351 |
with self.assertRaises(InvalidState) as raised: |
| 1352 |
server.send_pong(b"") |
| 1353 |
self.assertEqual(str(raised.exception), "connection is closed") |
| 1354 |
|
| 1355 |
|
| 1356 |
class FailTests(ProtocolTestCase): |
| 1357 |
""" |
| 1358 |
Test failing the connection. |
| 1359 |
|
| 1360 |
See 7.1.7. Fail the WebSocket Connection in RFC 6455. |
| 1361 |
|
| 1362 |
""" |
| 1363 |
|
| 1364 |
def test_client_stops_processing_frames_after_fail(self): |
| 1365 |
client = Protocol(CLIENT) |
| 1366 |
client.fail(CloseCode.PROTOCOL_ERROR) |
| 1367 |
self.assertConnectionFailing(client, CloseCode.PROTOCOL_ERROR) |
| 1368 |
client.receive_data(b"\x88\x02\x03\xea") |
| 1369 |
self.assertFrameReceived(client, None) |
| 1370 |
|
| 1371 |
def test_server_stops_processing_frames_after_fail(self): |
| 1372 |
server = Protocol(SERVER) |
| 1373 |
server.fail(CloseCode.PROTOCOL_ERROR) |
| 1374 |
self.assertConnectionFailing(server, CloseCode.PROTOCOL_ERROR) |
| 1375 |
server.receive_data(b"\x88\x82\x00\x00\x00\x00\x03\xea") |
| 1376 |
self.assertFrameReceived(server, None) |
| 1377 |
|
| 1378 |
|
| 1379 |
class FragmentationTests(ProtocolTestCase): |
| 1380 |
""" |
| 1381 |
Test message fragmentation. |
| 1382 |
|
| 1383 |
See 5.4. Fragmentation in RFC 6455. |
| 1384 |
|
| 1385 |
""" |
| 1386 |
|
| 1387 |
def test_client_send_ping_pong_in_fragmented_message(self): |
| 1388 |
client = Protocol(CLIENT) |
| 1389 |
client.send_text(b"Spam", fin=False) |
| 1390 |
self.assertFrameSent(client, Frame(TEXT, b"Spam", fin=False)) |
| 1391 |
client.send_ping(b"Ping") |
| 1392 |
self.assertFrameSent(client, Frame(PING, b"Ping")) |
| 1393 |
client.send_continuation(b"Ham", fin=False) |
| 1394 |
self.assertFrameSent(client, Frame(CONT, b"Ham", fin=False)) |
| 1395 |
client.send_pong(b"Pong") |
| 1396 |
self.assertFrameSent(client, Frame(PONG, b"Pong")) |
| 1397 |
client.send_continuation(b"Eggs", fin=True) |
| 1398 |
self.assertFrameSent(client, Frame(CONT, b"Eggs")) |
| 1399 |
|
| 1400 |
def test_server_send_ping_pong_in_fragmented_message(self): |
| 1401 |
server = Protocol(SERVER) |
| 1402 |
server.send_text(b"Spam", fin=False) |
| 1403 |
self.assertFrameSent(server, Frame(TEXT, b"Spam", fin=False)) |
| 1404 |
server.send_ping(b"Ping") |
| 1405 |
self.assertFrameSent(server, Frame(PING, b"Ping")) |
| 1406 |
server.send_continuation(b"Ham", fin=False) |
| 1407 |
self.assertFrameSent(server, Frame(CONT, b"Ham", fin=False)) |
| 1408 |
server.send_pong(b"Pong") |
| 1409 |
self.assertFrameSent(server, Frame(PONG, b"Pong")) |
| 1410 |
server.send_continuation(b"Eggs", fin=True) |
| 1411 |
self.assertFrameSent(server, Frame(CONT, b"Eggs")) |
| 1412 |
|
| 1413 |
def test_client_receive_ping_pong_in_fragmented_message(self): |
| 1414 |
client = Protocol(CLIENT) |
| 1415 |
client.receive_data(b"\x01\x04Spam") |
| 1416 |
self.assertFrameReceived( |
| 1417 |
client, |
| 1418 |
Frame(TEXT, b"Spam", fin=False), |
| 1419 |
) |
| 1420 |
client.receive_data(b"\x89\x04Ping") |
| 1421 |
self.assertFrameReceived( |
| 1422 |
client, |
| 1423 |
Frame(PING, b"Ping"), |
| 1424 |
) |
| 1425 |
self.assertFrameSent( |
| 1426 |
client, |
| 1427 |
Frame(PONG, b"Ping"), |
| 1428 |
) |
| 1429 |
client.receive_data(b"\x00\x03Ham") |
| 1430 |
self.assertFrameReceived( |
| 1431 |
client, |
| 1432 |
Frame(CONT, b"Ham", fin=False), |
| 1433 |
) |
| 1434 |
client.receive_data(b"\x8a\x04Pong") |
| 1435 |
self.assertFrameReceived( |
| 1436 |
client, |
| 1437 |
Frame(PONG, b"Pong"), |
| 1438 |
) |
| 1439 |
client.receive_data(b"\x80\x04Eggs") |
| 1440 |
self.assertFrameReceived( |
| 1441 |
client, |
| 1442 |
Frame(CONT, b"Eggs"), |
| 1443 |
) |
| 1444 |
|
| 1445 |
def test_server_receive_ping_pong_in_fragmented_message(self): |
| 1446 |
server = Protocol(SERVER) |
| 1447 |
server.receive_data(b"\x01\x84\x00\x00\x00\x00Spam") |
| 1448 |
self.assertFrameReceived( |
| 1449 |
server, |
| 1450 |
Frame(TEXT, b"Spam", fin=False), |
| 1451 |
) |
| 1452 |
server.receive_data(b"\x89\x84\x00\x00\x00\x00Ping") |
| 1453 |
self.assertFrameReceived( |
| 1454 |
server, |
| 1455 |
Frame(PING, b"Ping"), |
| 1456 |
) |
| 1457 |
self.assertFrameSent( |
| 1458 |
server, |
| 1459 |
Frame(PONG, b"Ping"), |
| 1460 |
) |
| 1461 |
server.receive_data(b"\x00\x83\x00\x00\x00\x00Ham") |
| 1462 |
self.assertFrameReceived( |
| 1463 |
server, |
| 1464 |
Frame(CONT, b"Ham", fin=False), |
| 1465 |
) |
| 1466 |
server.receive_data(b"\x8a\x84\x00\x00\x00\x00Pong") |
| 1467 |
self.assertFrameReceived( |
| 1468 |
server, |
| 1469 |
Frame(PONG, b"Pong"), |
| 1470 |
) |
| 1471 |
server.receive_data(b"\x80\x84\x00\x00\x00\x00Eggs") |
| 1472 |
self.assertFrameReceived( |
| 1473 |
server, |
| 1474 |
Frame(CONT, b"Eggs"), |
| 1475 |
) |
| 1476 |
|
| 1477 |
def test_client_send_close_in_fragmented_message(self): |
| 1478 |
client = Protocol(CLIENT) |
| 1479 |
client.send_text(b"Spam", fin=False) |
| 1480 |
self.assertFrameSent(client, Frame(TEXT, b"Spam", fin=False)) |
| 1481 |
with patch("secrets.token_bytes", return_value=b"\x3c\x3c\x3c\x3c"): |
| 1482 |
client.send_close() |
| 1483 |
self.assertEqual(client.data_to_send(), [b"\x88\x80\x3c\x3c\x3c\x3c"]) |
| 1484 |
self.assertIs(client.state, CLOSING) |
| 1485 |
with self.assertRaises(InvalidState) as raised: |
| 1486 |
client.send_continuation(b"Eggs", fin=True) |
| 1487 |
self.assertEqual(str(raised.exception), "connection is closing") |
| 1488 |
|
| 1489 |
def test_server_send_close_in_fragmented_message(self): |
| 1490 |
server = Protocol(SERVER) |
| 1491 |
server.send_text(b"Spam", fin=False) |
| 1492 |
self.assertFrameSent(server, Frame(TEXT, b"Spam", fin=False)) |
| 1493 |
server.send_close() |
| 1494 |
self.assertEqual(server.data_to_send(), [b"\x88\x00"]) |
| 1495 |
self.assertIs(server.state, CLOSING) |
| 1496 |
with self.assertRaises(InvalidState) as raised: |
| 1497 |
server.send_continuation(b"Eggs", fin=True) |
| 1498 |
self.assertEqual(str(raised.exception), "connection is closing") |
| 1499 |
|
| 1500 |
def test_client_receive_close_in_fragmented_message(self): |
| 1501 |
client = Protocol(CLIENT) |
| 1502 |
client.receive_data(b"\x01\x04Spam") |
| 1503 |
self.assertFrameReceived( |
| 1504 |
client, |
| 1505 |
Frame(TEXT, b"Spam", fin=False), |
| 1506 |
) |
| 1507 |
client.receive_data(b"\x88\x02\x03\xe8") |
| 1508 |
self.assertIsInstance(client.parser_exc, ProtocolError) |
| 1509 |
self.assertEqual(str(client.parser_exc), "incomplete fragmented message") |
| 1510 |
self.assertConnectionFailing( |
| 1511 |
client, CloseCode.PROTOCOL_ERROR, "incomplete fragmented message" |
| 1512 |
) |
| 1513 |
|
| 1514 |
def test_server_receive_close_in_fragmented_message(self): |
| 1515 |
server = Protocol(SERVER) |
| 1516 |
server.receive_data(b"\x01\x84\x00\x00\x00\x00Spam") |
| 1517 |
self.assertFrameReceived( |
| 1518 |
server, |
| 1519 |
Frame(TEXT, b"Spam", fin=False), |
| 1520 |
) |
| 1521 |
server.receive_data(b"\x88\x82\x00\x00\x00\x00\x03\xe9") |
| 1522 |
self.assertIsInstance(server.parser_exc, ProtocolError) |
| 1523 |
self.assertEqual(str(server.parser_exc), "incomplete fragmented message") |
| 1524 |
self.assertConnectionFailing( |
| 1525 |
server, CloseCode.PROTOCOL_ERROR, "incomplete fragmented message" |
| 1526 |
) |
| 1527 |
|
| 1528 |
|
| 1529 |
class EOFTests(ProtocolTestCase): |
| 1530 |
""" |
| 1531 |
Test half-closes on connection termination. |
| 1532 |
|
| 1533 |
""" |
| 1534 |
|
| 1535 |
def test_client_receives_eof(self): |
| 1536 |
client = Protocol(CLIENT) |
| 1537 |
client.receive_data(b"\x88\x00") |
| 1538 |
self.assertConnectionClosing(client) |
| 1539 |
client.receive_eof() |
| 1540 |
self.assertIs(client.state, CLOSED) |
| 1541 |
|
| 1542 |
def test_server_receives_eof(self): |
| 1543 |
server = Protocol(SERVER) |
| 1544 |
server.receive_data(b"\x88\x80\x3c\x3c\x3c\x3c") |
| 1545 |
self.assertConnectionClosing(server) |
| 1546 |
server.receive_eof() |
| 1547 |
self.assertIs(server.state, CLOSED) |
| 1548 |
|
| 1549 |
def test_client_receives_eof_between_frames(self): |
| 1550 |
client = Protocol(CLIENT) |
| 1551 |
client.receive_eof() |
| 1552 |
self.assertIsInstance(client.parser_exc, EOFError) |
| 1553 |
self.assertEqual(str(client.parser_exc), "unexpected end of stream") |
| 1554 |
self.assertIs(client.state, CLOSED) |
| 1555 |
|
| 1556 |
def test_server_receives_eof_between_frames(self): |
| 1557 |
server = Protocol(SERVER) |
| 1558 |
server.receive_eof() |
| 1559 |
self.assertIsInstance(server.parser_exc, EOFError) |
| 1560 |
self.assertEqual(str(server.parser_exc), "unexpected end of stream") |
| 1561 |
self.assertIs(server.state, CLOSED) |
| 1562 |
|
| 1563 |
def test_client_receives_eof_inside_frame(self): |
| 1564 |
client = Protocol(CLIENT) |
| 1565 |
client.receive_data(b"\x81") |
| 1566 |
client.receive_eof() |
| 1567 |
self.assertIsInstance(client.parser_exc, EOFError) |
| 1568 |
self.assertEqual( |
| 1569 |
str(client.parser_exc), |
| 1570 |
"stream ends after 1 bytes, expected 2 bytes", |
| 1571 |
) |
| 1572 |
self.assertIs(client.state, CLOSED) |
| 1573 |
|
| 1574 |
def test_server_receives_eof_inside_frame(self): |
| 1575 |
server = Protocol(SERVER) |
| 1576 |
server.receive_data(b"\x81") |
| 1577 |
server.receive_eof() |
| 1578 |
self.assertIsInstance(server.parser_exc, EOFError) |
| 1579 |
self.assertEqual( |
| 1580 |
str(server.parser_exc), |
| 1581 |
"stream ends after 1 bytes, expected 2 bytes", |
| 1582 |
) |
| 1583 |
self.assertIs(server.state, CLOSED) |
| 1584 |
|
| 1585 |
def test_client_receives_data_after_exception(self): |
| 1586 |
client = Protocol(CLIENT) |
| 1587 |
client.receive_data(b"\xff\xff") |
| 1588 |
self.assertConnectionFailing(client, CloseCode.PROTOCOL_ERROR, "invalid opcode") |
| 1589 |
client.receive_data(b"\x00\x00") |
| 1590 |
self.assertFrameSent(client, None) |
| 1591 |
|
| 1592 |
def test_server_receives_data_after_exception(self): |
| 1593 |
server = Protocol(SERVER) |
| 1594 |
server.receive_data(b"\xff\xff") |
| 1595 |
self.assertConnectionFailing(server, CloseCode.PROTOCOL_ERROR, "invalid opcode") |
| 1596 |
server.receive_data(b"\x00\x00") |
| 1597 |
self.assertFrameSent(server, None) |
| 1598 |
|
| 1599 |
def test_client_receives_eof_after_exception(self): |
| 1600 |
client = Protocol(CLIENT) |
| 1601 |
client.receive_data(b"\xff\xff") |
| 1602 |
self.assertConnectionFailing(client, CloseCode.PROTOCOL_ERROR, "invalid opcode") |
| 1603 |
client.receive_eof() |
| 1604 |
self.assertFrameSent(client, None, eof=True) |
| 1605 |
|
| 1606 |
def test_server_receives_eof_after_exception(self): |
| 1607 |
server = Protocol(SERVER) |
| 1608 |
server.receive_data(b"\xff\xff") |
| 1609 |
self.assertConnectionFailing(server, CloseCode.PROTOCOL_ERROR, "invalid opcode") |
| 1610 |
server.receive_eof() |
| 1611 |
self.assertFrameSent(server, None) |
| 1612 |
|
| 1613 |
def test_client_receives_data_and_eof_after_exception(self): |
| 1614 |
client = Protocol(CLIENT) |
| 1615 |
client.receive_data(b"\xff\xff") |
| 1616 |
self.assertConnectionFailing(client, CloseCode.PROTOCOL_ERROR, "invalid opcode") |
| 1617 |
client.receive_data(b"\x00\x00") |
| 1618 |
client.receive_eof() |
| 1619 |
self.assertFrameSent(client, None, eof=True) |
| 1620 |
|
| 1621 |
def test_server_receives_data_and_eof_after_exception(self): |
| 1622 |
server = Protocol(SERVER) |
| 1623 |
server.receive_data(b"\xff\xff") |
| 1624 |
self.assertConnectionFailing(server, CloseCode.PROTOCOL_ERROR, "invalid opcode") |
| 1625 |
server.receive_data(b"\x00\x00") |
| 1626 |
server.receive_eof() |
| 1627 |
self.assertFrameSent(server, None) |
| 1628 |
|
| 1629 |
def test_client_receives_data_after_eof(self): |
| 1630 |
client = Protocol(CLIENT) |
| 1631 |
client.receive_data(b"\x88\x00") |
| 1632 |
self.assertConnectionClosing(client) |
| 1633 |
client.receive_eof() |
| 1634 |
with self.assertRaises(EOFError) as raised: |
| 1635 |
client.receive_data(b"\x88\x00") |
| 1636 |
self.assertEqual(str(raised.exception), "stream ended") |
| 1637 |
|
| 1638 |
def test_server_receives_data_after_eof(self): |
| 1639 |
server = Protocol(SERVER) |
| 1640 |
server.receive_data(b"\x88\x80\x3c\x3c\x3c\x3c") |
| 1641 |
self.assertConnectionClosing(server) |
| 1642 |
server.receive_eof() |
| 1643 |
with self.assertRaises(EOFError) as raised: |
| 1644 |
server.receive_data(b"\x88\x80\x00\x00\x00\x00") |
| 1645 |
self.assertEqual(str(raised.exception), "stream ended") |
| 1646 |
|
| 1647 |
def test_client_receives_eof_after_eof(self): |
| 1648 |
client = Protocol(CLIENT) |
| 1649 |
client.receive_data(b"\x88\x00") |
| 1650 |
self.assertConnectionClosing(client) |
| 1651 |
client.receive_eof() |
| 1652 |
client.receive_eof() # this is idempotent |
| 1653 |
|
| 1654 |
def test_server_receives_eof_after_eof(self): |
| 1655 |
server = Protocol(SERVER) |
| 1656 |
server.receive_data(b"\x88\x80\x3c\x3c\x3c\x3c") |
| 1657 |
self.assertConnectionClosing(server) |
| 1658 |
server.receive_eof() |
| 1659 |
server.receive_eof() # this is idempotent |
| 1660 |
|
| 1661 |
|
| 1662 |
class TCPCloseTests(ProtocolTestCase): |
| 1663 |
""" |
| 1664 |
Test expectation of TCP close on connection termination. |
| 1665 |
|
| 1666 |
""" |
| 1667 |
|
| 1668 |
def test_client_default(self): |
| 1669 |
client = Protocol(CLIENT) |
| 1670 |
self.assertFalse(client.close_expected()) |
| 1671 |
|
| 1672 |
def test_server_default(self): |
| 1673 |
server = Protocol(SERVER) |
| 1674 |
self.assertFalse(server.close_expected()) |
| 1675 |
|
| 1676 |
def test_client_sends_close(self): |
| 1677 |
client = Protocol(CLIENT) |
| 1678 |
client.send_close() |
| 1679 |
self.assertTrue(client.close_expected()) |
| 1680 |
|
| 1681 |
def test_server_sends_close(self): |
| 1682 |
server = Protocol(SERVER) |
| 1683 |
server.send_close() |
| 1684 |
self.assertTrue(server.close_expected()) |
| 1685 |
|
| 1686 |
def test_client_receives_close(self): |
| 1687 |
client = Protocol(CLIENT) |
| 1688 |
client.receive_data(b"\x88\x00") |
| 1689 |
self.assertTrue(client.close_expected()) |
| 1690 |
|
| 1691 |
def test_client_receives_close_then_eof(self): |
| 1692 |
client = Protocol(CLIENT) |
| 1693 |
client.receive_data(b"\x88\x00") |
| 1694 |
client.receive_eof() |
| 1695 |
self.assertFalse(client.close_expected()) |
| 1696 |
|
| 1697 |
def test_server_receives_close_then_eof(self): |
| 1698 |
server = Protocol(SERVER) |
| 1699 |
server.receive_data(b"\x88\x80\x3c\x3c\x3c\x3c") |
| 1700 |
server.receive_eof() |
| 1701 |
self.assertFalse(server.close_expected()) |
| 1702 |
|
| 1703 |
def test_server_receives_close(self): |
| 1704 |
server = Protocol(SERVER) |
| 1705 |
server.receive_data(b"\x88\x80\x3c\x3c\x3c\x3c") |
| 1706 |
self.assertTrue(server.close_expected()) |
| 1707 |
|
| 1708 |
def test_client_fails_connection(self): |
| 1709 |
client = Protocol(CLIENT) |
| 1710 |
client.fail(CloseCode.PROTOCOL_ERROR) |
| 1711 |
self.assertTrue(client.close_expected()) |
| 1712 |
|
| 1713 |
def test_server_fails_connection(self): |
| 1714 |
server = Protocol(SERVER) |
| 1715 |
server.fail(CloseCode.PROTOCOL_ERROR) |
| 1716 |
self.assertTrue(server.close_expected()) |
| 1717 |
|
| 1718 |
def test_client_is_connecting(self): |
| 1719 |
client = Protocol(CLIENT, state=CONNECTING) |
| 1720 |
self.assertFalse(client.close_expected()) |
| 1721 |
|
| 1722 |
def test_server_is_connecting(self): |
| 1723 |
server = Protocol(SERVER, state=CONNECTING) |
| 1724 |
self.assertFalse(server.close_expected()) |
| 1725 |
|
| 1726 |
def test_client_failed_connecting(self): |
| 1727 |
client = Protocol(CLIENT, state=CONNECTING) |
| 1728 |
client.send_eof() |
| 1729 |
self.assertTrue(client.close_expected()) |
| 1730 |
|
| 1731 |
def test_server_failed_connecting(self): |
| 1732 |
server = Protocol(SERVER, state=CONNECTING) |
| 1733 |
server.send_eof() |
| 1734 |
self.assertTrue(server.close_expected()) |
| 1735 |
|
| 1736 |
|
| 1737 |
class ConnectionClosedTests(ProtocolTestCase): |
| 1738 |
""" |
| 1739 |
Test connection closed exception. |
| 1740 |
|
| 1741 |
""" |
| 1742 |
|
| 1743 |
def test_client_sends_close_then_receives_close(self): |
| 1744 |
# Client-initiated close handshake on the client side complete. |
| 1745 |
client = Protocol(CLIENT) |
| 1746 |
client.send_close(CloseCode.NORMAL_CLOSURE, "") |
| 1747 |
client.receive_data(b"\x88\x02\x03\xe8") |
| 1748 |
client.receive_eof() |
| 1749 |
exc = client.close_exc |
| 1750 |
self.assertIsInstance(exc, ConnectionClosedOK) |
| 1751 |
self.assertEqual(exc.rcvd, Close(CloseCode.NORMAL_CLOSURE, "")) |
| 1752 |
self.assertEqual(exc.sent, Close(CloseCode.NORMAL_CLOSURE, "")) |
| 1753 |
self.assertFalse(exc.rcvd_then_sent) |
| 1754 |
|
| 1755 |
def test_server_sends_close_then_receives_close(self): |
| 1756 |
# Server-initiated close handshake on the server side complete. |
| 1757 |
server = Protocol(SERVER) |
| 1758 |
server.send_close(CloseCode.NORMAL_CLOSURE, "") |
| 1759 |
server.receive_data(b"\x88\x82\x00\x00\x00\x00\x03\xe8") |
| 1760 |
server.receive_eof() |
| 1761 |
exc = server.close_exc |
| 1762 |
self.assertIsInstance(exc, ConnectionClosedOK) |
| 1763 |
self.assertEqual(exc.rcvd, Close(CloseCode.NORMAL_CLOSURE, "")) |
| 1764 |
self.assertEqual(exc.sent, Close(CloseCode.NORMAL_CLOSURE, "")) |
| 1765 |
self.assertFalse(exc.rcvd_then_sent) |
| 1766 |
|
| 1767 |
def test_client_receives_close_then_sends_close(self): |
| 1768 |
# Server-initiated close handshake on the client side complete. |
| 1769 |
client = Protocol(CLIENT) |
| 1770 |
client.receive_data(b"\x88\x02\x03\xe8") |
| 1771 |
client.receive_eof() |
| 1772 |
exc = client.close_exc |
| 1773 |
self.assertIsInstance(exc, ConnectionClosedOK) |
| 1774 |
self.assertEqual(exc.rcvd, Close(CloseCode.NORMAL_CLOSURE, "")) |
| 1775 |
self.assertEqual(exc.sent, Close(CloseCode.NORMAL_CLOSURE, "")) |
| 1776 |
self.assertTrue(exc.rcvd_then_sent) |
| 1777 |
|
| 1778 |
def test_server_receives_close_then_sends_close(self): |
| 1779 |
# Client-initiated close handshake on the server side complete. |
| 1780 |
server = Protocol(SERVER) |
| 1781 |
server.receive_data(b"\x88\x82\x00\x00\x00\x00\x03\xe8") |
| 1782 |
server.receive_eof() |
| 1783 |
exc = server.close_exc |
| 1784 |
self.assertIsInstance(exc, ConnectionClosedOK) |
| 1785 |
self.assertEqual(exc.rcvd, Close(CloseCode.NORMAL_CLOSURE, "")) |
| 1786 |
self.assertEqual(exc.sent, Close(CloseCode.NORMAL_CLOSURE, "")) |
| 1787 |
self.assertTrue(exc.rcvd_then_sent) |
| 1788 |
|
| 1789 |
def test_client_sends_close_then_receives_eof(self): |
| 1790 |
# Client-initiated close handshake on the client side times out. |
| 1791 |
client = Protocol(CLIENT) |
| 1792 |
client.send_close(CloseCode.NORMAL_CLOSURE, "") |
| 1793 |
client.receive_eof() |
| 1794 |
exc = client.close_exc |
| 1795 |
self.assertIsInstance(exc, ConnectionClosedError) |
| 1796 |
self.assertIsNone(exc.rcvd) |
| 1797 |
self.assertEqual(exc.sent, Close(CloseCode.NORMAL_CLOSURE, "")) |
| 1798 |
self.assertIsNone(exc.rcvd_then_sent) |
| 1799 |
|
| 1800 |
def test_server_sends_close_then_receives_eof(self): |
| 1801 |
# Server-initiated close handshake on the server side times out. |
| 1802 |
server = Protocol(SERVER) |
| 1803 |
server.send_close(CloseCode.NORMAL_CLOSURE, "") |
| 1804 |
server.receive_eof() |
| 1805 |
exc = server.close_exc |
| 1806 |
self.assertIsInstance(exc, ConnectionClosedError) |
| 1807 |
self.assertIsNone(exc.rcvd) |
| 1808 |
self.assertEqual(exc.sent, Close(CloseCode.NORMAL_CLOSURE, "")) |
| 1809 |
self.assertIsNone(exc.rcvd_then_sent) |
| 1810 |
|
| 1811 |
def test_client_receives_eof(self): |
| 1812 |
# Server-initiated close handshake on the client side times out. |
| 1813 |
client = Protocol(CLIENT) |
| 1814 |
client.receive_eof() |
| 1815 |
exc = client.close_exc |
| 1816 |
self.assertIsInstance(exc, ConnectionClosedError) |
| 1817 |
self.assertIsNone(exc.rcvd) |
| 1818 |
self.assertIsNone(exc.sent) |
| 1819 |
self.assertIsNone(exc.rcvd_then_sent) |
| 1820 |
|
| 1821 |
def test_server_receives_eof(self): |
| 1822 |
# Client-initiated close handshake on the server side times out. |
| 1823 |
server = Protocol(SERVER) |
| 1824 |
server.receive_eof() |
| 1825 |
exc = server.close_exc |
| 1826 |
self.assertIsInstance(exc, ConnectionClosedError) |
| 1827 |
self.assertIsNone(exc.rcvd) |
| 1828 |
self.assertIsNone(exc.sent) |
| 1829 |
self.assertIsNone(exc.rcvd_then_sent) |
| 1830 |
|
| 1831 |
|
| 1832 |
class ErrorTests(ProtocolTestCase): |
| 1833 |
""" |
| 1834 |
Test other error cases. |
| 1835 |
|
| 1836 |
""" |
| 1837 |
|
| 1838 |
def test_client_hits_internal_error_reading_frame(self): |
| 1839 |
client = Protocol(CLIENT) |
| 1840 |
# This isn't supposed to happen, so we're simulating it. |
| 1841 |
with patch("struct.unpack", side_effect=RuntimeError("BOOM")): |
| 1842 |
client.receive_data(b"\x81\x00") |
| 1843 |
self.assertIsInstance(client.parser_exc, RuntimeError) |
| 1844 |
self.assertEqual(str(client.parser_exc), "BOOM") |
| 1845 |
self.assertConnectionFailing(client, CloseCode.INTERNAL_ERROR, "") |
| 1846 |
|
| 1847 |
def test_server_hits_internal_error_reading_frame(self): |
| 1848 |
server = Protocol(SERVER) |
| 1849 |
# This isn't supposed to happen, so we're simulating it. |
| 1850 |
with patch("struct.unpack", side_effect=RuntimeError("BOOM")): |
| 1851 |
server.receive_data(b"\x81\x80\x00\x00\x00\x00") |
| 1852 |
self.assertIsInstance(server.parser_exc, RuntimeError) |
| 1853 |
self.assertEqual(str(server.parser_exc), "BOOM") |
| 1854 |
self.assertConnectionFailing(server, CloseCode.INTERNAL_ERROR, "") |
| 1855 |
|
| 1856 |
|
| 1857 |
class ExtensionsTests(ProtocolTestCase): |
| 1858 |
""" |
| 1859 |
Test how extensions affect frames. |
| 1860 |
|
| 1861 |
""" |
| 1862 |
|
| 1863 |
def test_client_extension_encodes_frame(self): |
| 1864 |
client = Protocol(CLIENT) |
| 1865 |
client.extensions = [Rsv2Extension()] |
| 1866 |
with patch("secrets.token_bytes", return_value=b"\x00\x44\x88\xcc"): |
| 1867 |
client.send_ping(b"") |
| 1868 |
self.assertEqual(client.data_to_send(), [b"\xa9\x80\x00\x44\x88\xcc"]) |
| 1869 |
|
| 1870 |
def test_server_extension_encodes_frame(self): |
| 1871 |
server = Protocol(SERVER) |
| 1872 |
server.extensions = [Rsv2Extension()] |
| 1873 |
server.send_ping(b"") |
| 1874 |
self.assertEqual(server.data_to_send(), [b"\xa9\x00"]) |
| 1875 |
|
| 1876 |
def test_client_extension_decodes_frame(self): |
| 1877 |
client = Protocol(CLIENT) |
| 1878 |
client.extensions = [Rsv2Extension()] |
| 1879 |
client.receive_data(b"\xaa\x00") |
| 1880 |
self.assertEqual(client.events_received(), [Frame(PONG, b"")]) |
| 1881 |
|
| 1882 |
def test_server_extension_decodes_frame(self): |
| 1883 |
server = Protocol(SERVER) |
| 1884 |
server.extensions = [Rsv2Extension()] |
| 1885 |
server.receive_data(b"\xaa\x80\x00\x44\x88\xcc") |
| 1886 |
self.assertEqual(server.events_received(), [Frame(PONG, b"")]) |
| 1887 |
|
| 1888 |
|
| 1889 |
class MiscTests(unittest.TestCase): |
| 1890 |
def test_client_default_logger(self): |
| 1891 |
client = Protocol(CLIENT) |
| 1892 |
logger = logging.getLogger("websockets.client") |
| 1893 |
self.assertIs(client.logger, logger) |
| 1894 |
|
| 1895 |
def test_server_default_logger(self): |
| 1896 |
server = Protocol(SERVER) |
| 1897 |
logger = logging.getLogger("websockets.server") |
| 1898 |
self.assertIs(server.logger, logger) |
| 1899 |
|
| 1900 |
def test_client_custom_logger(self): |
| 1901 |
logger = logging.getLogger("test") |
| 1902 |
client = Protocol(CLIENT, logger=logger) |
| 1903 |
self.assertIs(client.logger, logger) |
| 1904 |
|
| 1905 |
def test_server_custom_logger(self): |
| 1906 |
logger = logging.getLogger("test") |
| 1907 |
server = Protocol(SERVER, logger=logger) |
| 1908 |
self.assertIs(server.logger, logger) |