-
-
Notifications
You must be signed in to change notification settings - Fork 589
Expand file tree
/
Copy pathtest_protocol.py
More file actions
1908 lines (1645 loc) · 73.2 KB
/
test_protocol.py
File metadata and controls
1908 lines (1645 loc) · 73.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging
import unittest
from unittest.mock import patch
from websockets.exceptions import (
ConnectionClosedError,
ConnectionClosedOK,
InvalidState,
PayloadTooBig,
ProtocolError,
)
from websockets.frames import (
OP_BINARY,
OP_CLOSE,
OP_CONT,
OP_PING,
OP_PONG,
OP_TEXT,
Close,
CloseCode,
Frame,
)
from websockets.protocol import *
from websockets.protocol import CLIENT, CLOSED, CLOSING, CONNECTING, SERVER
from .extensions.utils import Rsv2Extension
from .test_frames import FramesTestCase
class ProtocolTestCase(FramesTestCase):
def assertFrameSent(self, connection, frame, eof=False):
"""
Outgoing data for ``connection`` contains the given frame.
``frame`` may be ``None`` if no frame is expected.
When ``eof`` is ``True``, the end of the stream is also expected.
"""
frames_sent = [
(
None
if write is SEND_EOF
else self.parse(
write,
mask=connection.side is CLIENT,
extensions=connection.extensions,
)
)
for write in connection.data_to_send()
]
frames_expected = [] if frame is None else [frame]
if eof:
frames_expected += [None]
self.assertEqual(frames_sent, frames_expected)
def assertFrameReceived(self, connection, frame):
"""
Incoming data for ``connection`` contains the given frame.
``frame`` may be ``None`` if no frame is expected.
"""
frames_received = connection.events_received()
frames_expected = [] if frame is None else [frame]
self.assertEqual(frames_received, frames_expected)
def assertConnectionClosing(self, connection, code=None, reason=""):
"""
Incoming data caused the "Start the WebSocket Closing Handshake" process.
"""
close_frame = Frame(
OP_CLOSE,
b"" if code is None else Close(code, reason).serialize(),
)
# A close frame was received.
self.assertFrameReceived(connection, close_frame)
# A close frame and possibly the end of stream were sent.
self.assertFrameSent(connection, close_frame, eof=connection.side is SERVER)
def assertConnectionFailing(self, connection, code=None, reason=""):
"""
Incoming data caused the "Fail the WebSocket Connection" process.
"""
close_frame = Frame(
OP_CLOSE,
b"" if code is None else Close(code, reason).serialize(),
)
# No frame was received.
self.assertFrameReceived(connection, None)
# A close frame and possibly the end of stream were sent.
self.assertFrameSent(connection, close_frame, eof=connection.side is SERVER)
class MaskingTests(ProtocolTestCase):
"""
Test frame masking.
5.1. Overview
"""
unmasked_text_frame_date = b"\x81\x04Spam"
masked_text_frame_data = b"\x81\x84\x00\xff\x00\xff\x53\x8f\x61\x92"
def test_client_sends_masked_frame(self):
client = Protocol(CLIENT)
with patch("secrets.token_bytes", return_value=b"\x00\xff\x00\xff"):
client.send_text(b"Spam", True)
self.assertEqual(client.data_to_send(), [self.masked_text_frame_data])
def test_server_sends_unmasked_frame(self):
server = Protocol(SERVER)
server.send_text(b"Spam", True)
self.assertEqual(server.data_to_send(), [self.unmasked_text_frame_date])
def test_client_receives_unmasked_frame(self):
client = Protocol(CLIENT)
client.receive_data(self.unmasked_text_frame_date)
self.assertFrameReceived(
client,
Frame(OP_TEXT, b"Spam"),
)
def test_server_receives_masked_frame(self):
server = Protocol(SERVER)
server.receive_data(self.masked_text_frame_data)
self.assertFrameReceived(
server,
Frame(OP_TEXT, b"Spam"),
)
def test_client_receives_masked_frame(self):
client = Protocol(CLIENT)
client.receive_data(self.masked_text_frame_data)
self.assertIsInstance(client.parser_exc, ProtocolError)
self.assertEqual(str(client.parser_exc), "incorrect masking")
self.assertConnectionFailing(
client, CloseCode.PROTOCOL_ERROR, "incorrect masking"
)
def test_server_receives_unmasked_frame(self):
server = Protocol(SERVER)
server.receive_data(self.unmasked_text_frame_date)
self.assertIsInstance(server.parser_exc, ProtocolError)
self.assertEqual(str(server.parser_exc), "incorrect masking")
self.assertConnectionFailing(
server, CloseCode.PROTOCOL_ERROR, "incorrect masking"
)
class ContinuationTests(ProtocolTestCase):
"""
Test continuation frames without text or binary frames.
"""
def test_client_sends_unexpected_continuation(self):
client = Protocol(CLIENT)
with self.assertRaises(ProtocolError) as raised:
client.send_continuation(b"", fin=False)
self.assertEqual(str(raised.exception), "unexpected continuation frame")
def test_server_sends_unexpected_continuation(self):
server = Protocol(SERVER)
with self.assertRaises(ProtocolError) as raised:
server.send_continuation(b"", fin=False)
self.assertEqual(str(raised.exception), "unexpected continuation frame")
def test_client_receives_unexpected_continuation(self):
client = Protocol(CLIENT)
client.receive_data(b"\x00\x00")
self.assertIsInstance(client.parser_exc, ProtocolError)
self.assertEqual(str(client.parser_exc), "unexpected continuation frame")
self.assertConnectionFailing(
client, CloseCode.PROTOCOL_ERROR, "unexpected continuation frame"
)
def test_server_receives_unexpected_continuation(self):
server = Protocol(SERVER)
server.receive_data(b"\x00\x80\x00\x00\x00\x00")
self.assertIsInstance(server.parser_exc, ProtocolError)
self.assertEqual(str(server.parser_exc), "unexpected continuation frame")
self.assertConnectionFailing(
server, CloseCode.PROTOCOL_ERROR, "unexpected continuation frame"
)
def test_client_sends_continuation_after_sending_close(self):
client = Protocol(CLIENT)
# Since it isn't possible to send a close frame in a fragmented
# message (see test_client_send_close_in_fragmented_message), in fact,
# this is the same test as test_client_sends_unexpected_continuation.
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"):
client.send_close(CloseCode.GOING_AWAY)
self.assertEqual(client.data_to_send(), [b"\x88\x82\x00\x00\x00\x00\x03\xe9"])
with self.assertRaises(ProtocolError) as raised:
client.send_continuation(b"", fin=False)
self.assertEqual(str(raised.exception), "unexpected continuation frame")
def test_server_sends_continuation_after_sending_close(self):
# Since it isn't possible to send a close frame in a fragmented
# message (see test_server_send_close_in_fragmented_message), in fact,
# this is the same test as test_server_sends_unexpected_continuation.
server = Protocol(SERVER)
server.send_close(CloseCode.NORMAL_CLOSURE)
self.assertEqual(server.data_to_send(), [b"\x88\x02\x03\xe8"])
with self.assertRaises(ProtocolError) as raised:
server.send_continuation(b"", fin=False)
self.assertEqual(str(raised.exception), "unexpected continuation frame")
def test_client_receives_continuation_after_receiving_close(self):
client = Protocol(CLIENT)
client.receive_data(b"\x88\x02\x03\xe8")
self.assertConnectionClosing(client, CloseCode.NORMAL_CLOSURE)
client.receive_data(b"\x00\x00")
self.assertFrameReceived(client, None)
self.assertFrameSent(client, None)
def test_server_receives_continuation_after_receiving_close(self):
server = Protocol(SERVER)
server.receive_data(b"\x88\x82\x00\x00\x00\x00\x03\xe9")
self.assertConnectionClosing(server, CloseCode.GOING_AWAY)
server.receive_data(b"\x00\x80\x00\xff\x00\xff")
self.assertFrameReceived(server, None)
self.assertFrameSent(server, None)
class TextTests(ProtocolTestCase):
"""
Test text frames and continuation frames.
"""
def test_client_sends_text(self):
client = Protocol(CLIENT)
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"):
client.send_text("😀".encode())
self.assertEqual(
client.data_to_send(), [b"\x81\x84\x00\x00\x00\x00\xf0\x9f\x98\x80"]
)
def test_server_sends_text(self):
server = Protocol(SERVER)
server.send_text("😀".encode())
self.assertEqual(server.data_to_send(), [b"\x81\x04\xf0\x9f\x98\x80"])
def test_client_receives_text(self):
client = Protocol(CLIENT)
client.receive_data(b"\x81\x04\xf0\x9f\x98\x80")
self.assertFrameReceived(
client,
Frame(OP_TEXT, "😀".encode()),
)
def test_server_receives_text(self):
server = Protocol(SERVER)
server.receive_data(b"\x81\x84\x00\x00\x00\x00\xf0\x9f\x98\x80")
self.assertFrameReceived(
server,
Frame(OP_TEXT, "😀".encode()),
)
def test_client_receives_text_over_size_limit(self):
client = Protocol(CLIENT, max_size=3)
client.receive_data(b"\x81\x04\xf0\x9f\x98\x80")
self.assertIsInstance(client.parser_exc, PayloadTooBig)
self.assertEqual(
str(client.parser_exc),
"frame with 4 bytes exceeds limit of 3 bytes",
)
self.assertConnectionFailing(
client,
CloseCode.MESSAGE_TOO_BIG,
"frame with 4 bytes exceeds limit of 3 bytes",
)
def test_server_receives_text_over_size_limit(self):
server = Protocol(SERVER, max_size=3)
server.receive_data(b"\x81\x84\x00\x00\x00\x00\xf0\x9f\x98\x80")
self.assertIsInstance(server.parser_exc, PayloadTooBig)
self.assertEqual(
str(server.parser_exc),
"frame with 4 bytes exceeds limit of 3 bytes",
)
self.assertConnectionFailing(
server,
CloseCode.MESSAGE_TOO_BIG,
"frame with 4 bytes exceeds limit of 3 bytes",
)
def test_client_receives_text_without_size_limit(self):
client = Protocol(CLIENT, max_size=None)
client.receive_data(b"\x81\x04\xf0\x9f\x98\x80")
self.assertFrameReceived(
client,
Frame(OP_TEXT, "😀".encode()),
)
def test_server_receives_text_without_size_limit(self):
server = Protocol(SERVER, max_size=None)
server.receive_data(b"\x81\x84\x00\x00\x00\x00\xf0\x9f\x98\x80")
self.assertFrameReceived(
server,
Frame(OP_TEXT, "😀".encode()),
)
def test_client_sends_fragmented_text(self):
client = Protocol(CLIENT)
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"):
client.send_text("😀".encode()[:2], fin=False)
self.assertEqual(client.data_to_send(), [b"\x01\x82\x00\x00\x00\x00\xf0\x9f"])
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"):
client.send_continuation("😀😀".encode()[2:6], fin=False)
self.assertEqual(
client.data_to_send(), [b"\x00\x84\x00\x00\x00\x00\x98\x80\xf0\x9f"]
)
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"):
client.send_continuation("😀".encode()[2:], fin=True)
self.assertEqual(client.data_to_send(), [b"\x80\x82\x00\x00\x00\x00\x98\x80"])
def test_server_sends_fragmented_text(self):
server = Protocol(SERVER)
server.send_text("😀".encode()[:2], fin=False)
self.assertEqual(server.data_to_send(), [b"\x01\x02\xf0\x9f"])
server.send_continuation("😀😀".encode()[2:6], fin=False)
self.assertEqual(server.data_to_send(), [b"\x00\x04\x98\x80\xf0\x9f"])
server.send_continuation("😀".encode()[2:], fin=True)
self.assertEqual(server.data_to_send(), [b"\x80\x02\x98\x80"])
def test_client_receives_fragmented_text(self):
client = Protocol(CLIENT)
client.receive_data(b"\x01\x02\xf0\x9f")
self.assertFrameReceived(
client,
Frame(OP_TEXT, "😀".encode()[:2], fin=False),
)
client.receive_data(b"\x00\x04\x98\x80\xf0\x9f")
self.assertFrameReceived(
client,
Frame(OP_CONT, "😀😀".encode()[2:6], fin=False),
)
client.receive_data(b"\x80\x02\x98\x80")
self.assertFrameReceived(
client,
Frame(OP_CONT, "😀".encode()[2:]),
)
def test_server_receives_fragmented_text(self):
server = Protocol(SERVER)
server.receive_data(b"\x01\x82\x00\x00\x00\x00\xf0\x9f")
self.assertFrameReceived(
server,
Frame(OP_TEXT, "😀".encode()[:2], fin=False),
)
server.receive_data(b"\x00\x84\x00\x00\x00\x00\x98\x80\xf0\x9f")
self.assertFrameReceived(
server,
Frame(OP_CONT, "😀😀".encode()[2:6], fin=False),
)
server.receive_data(b"\x80\x82\x00\x00\x00\x00\x98\x80")
self.assertFrameReceived(
server,
Frame(OP_CONT, "😀".encode()[2:]),
)
def test_client_receives_fragmented_text_over_fragment_size_limit(self):
client = Protocol(CLIENT, max_size=(None, 3))
client.receive_data(b"\x01\x04\xf0\x9f\x98\x80")
self.assertIsInstance(client.parser_exc, PayloadTooBig)
self.assertEqual(
str(client.parser_exc),
"frame with 4 bytes exceeds limit of 3 bytes",
)
def test_server_receives_fragmented_text_over_fragment_size_limit(self):
server = Protocol(SERVER, max_size=(None, 3))
server.receive_data(b"\x01\x84\x00\x00\x00\x00\xf0\x9f\x98\x80")
self.assertIsInstance(server.parser_exc, PayloadTooBig)
self.assertEqual(
str(server.parser_exc),
"frame with 4 bytes exceeds limit of 3 bytes",
)
def test_client_receives_fragmented_text_over_message_size_limit(self):
client = Protocol(CLIENT, max_size=(3, 2))
client.receive_data(b"\x01\x02\xf0\x9f")
self.assertFrameReceived(
client,
Frame(OP_TEXT, "😀".encode()[:2], fin=False),
)
client.receive_data(b"\x80\x02\x98\x80")
self.assertIsInstance(client.parser_exc, PayloadTooBig)
self.assertEqual(
str(client.parser_exc),
"frame with 2 bytes after reading 2 bytes exceeds limit of 3 bytes",
)
self.assertConnectionFailing(
client,
CloseCode.MESSAGE_TOO_BIG,
"frame with 2 bytes after reading 2 bytes exceeds limit of 3 bytes",
)
def test_server_receives_fragmented_text_over_message_size_limit(self):
server = Protocol(SERVER, max_size=(3, 2))
server.receive_data(b"\x01\x82\x00\x00\x00\x00\xf0\x9f")
self.assertFrameReceived(
server,
Frame(OP_TEXT, "😀".encode()[:2], fin=False),
)
server.receive_data(b"\x80\x82\x00\x00\x00\x00\x98\x80")
self.assertIsInstance(server.parser_exc, PayloadTooBig)
self.assertEqual(
str(server.parser_exc),
"frame with 2 bytes after reading 2 bytes exceeds limit of 3 bytes",
)
self.assertConnectionFailing(
server,
CloseCode.MESSAGE_TOO_BIG,
"frame with 2 bytes after reading 2 bytes exceeds limit of 3 bytes",
)
def test_client_receives_fragmented_text_without_size_limit(self):
client = Protocol(CLIENT, max_size=None)
client.receive_data(b"\x01\x02\xf0\x9f")
self.assertFrameReceived(
client,
Frame(OP_TEXT, "😀".encode()[:2], fin=False),
)
client.receive_data(b"\x00\x04\x98\x80\xf0\x9f")
self.assertFrameReceived(
client,
Frame(OP_CONT, "😀😀".encode()[2:6], fin=False),
)
client.receive_data(b"\x80\x02\x98\x80")
self.assertFrameReceived(
client,
Frame(OP_CONT, "😀".encode()[2:]),
)
def test_server_receives_fragmented_text_without_size_limit(self):
server = Protocol(SERVER, max_size=None)
server.receive_data(b"\x01\x82\x00\x00\x00\x00\xf0\x9f")
self.assertFrameReceived(
server,
Frame(OP_TEXT, "😀".encode()[:2], fin=False),
)
server.receive_data(b"\x00\x84\x00\x00\x00\x00\x98\x80\xf0\x9f")
self.assertFrameReceived(
server,
Frame(OP_CONT, "😀😀".encode()[2:6], fin=False),
)
server.receive_data(b"\x80\x82\x00\x00\x00\x00\x98\x80")
self.assertFrameReceived(
server,
Frame(OP_CONT, "😀".encode()[2:]),
)
def test_client_sends_unexpected_text(self):
client = Protocol(CLIENT)
client.send_text(b"", fin=False)
with self.assertRaises(ProtocolError) as raised:
client.send_text(b"", fin=False)
self.assertEqual(str(raised.exception), "expected a continuation frame")
def test_server_sends_unexpected_text(self):
server = Protocol(SERVER)
server.send_text(b"", fin=False)
with self.assertRaises(ProtocolError) as raised:
server.send_text(b"", fin=False)
self.assertEqual(str(raised.exception), "expected a continuation frame")
def test_client_receives_unexpected_text(self):
client = Protocol(CLIENT)
client.receive_data(b"\x01\x00")
self.assertFrameReceived(
client,
Frame(OP_TEXT, b"", fin=False),
)
client.receive_data(b"\x01\x00")
self.assertIsInstance(client.parser_exc, ProtocolError)
self.assertEqual(str(client.parser_exc), "expected a continuation frame")
self.assertConnectionFailing(
client, CloseCode.PROTOCOL_ERROR, "expected a continuation frame"
)
def test_server_receives_unexpected_text(self):
server = Protocol(SERVER)
server.receive_data(b"\x01\x80\x00\x00\x00\x00")
self.assertFrameReceived(
server,
Frame(OP_TEXT, b"", fin=False),
)
server.receive_data(b"\x01\x80\x00\x00\x00\x00")
self.assertIsInstance(server.parser_exc, ProtocolError)
self.assertEqual(str(server.parser_exc), "expected a continuation frame")
self.assertConnectionFailing(
server, CloseCode.PROTOCOL_ERROR, "expected a continuation frame"
)
def test_client_sends_text_after_sending_close(self):
client = Protocol(CLIENT)
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"):
client.send_close(CloseCode.GOING_AWAY)
self.assertEqual(client.data_to_send(), [b"\x88\x82\x00\x00\x00\x00\x03\xe9"])
with self.assertRaises(InvalidState) as raised:
client.send_text(b"")
self.assertEqual(str(raised.exception), "connection is closing")
def test_server_sends_text_after_sending_close(self):
server = Protocol(SERVER)
server.send_close(CloseCode.NORMAL_CLOSURE)
self.assertEqual(server.data_to_send(), [b"\x88\x02\x03\xe8"])
with self.assertRaises(InvalidState) as raised:
server.send_text(b"")
self.assertEqual(str(raised.exception), "connection is closing")
def test_client_receives_text_after_receiving_close(self):
client = Protocol(CLIENT)
client.receive_data(b"\x88\x02\x03\xe8")
self.assertConnectionClosing(client, CloseCode.NORMAL_CLOSURE)
client.receive_data(b"\x81\x00")
self.assertFrameReceived(client, None)
self.assertFrameSent(client, None)
def test_server_receives_text_after_receiving_close(self):
server = Protocol(SERVER)
server.receive_data(b"\x88\x82\x00\x00\x00\x00\x03\xe9")
self.assertConnectionClosing(server, CloseCode.GOING_AWAY)
server.receive_data(b"\x81\x80\x00\xff\x00\xff")
self.assertFrameReceived(server, None)
self.assertFrameSent(server, None)
class BinaryTests(ProtocolTestCase):
"""
Test binary frames and continuation frames.
"""
def test_client_sends_binary(self):
client = Protocol(CLIENT)
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"):
client.send_binary(b"\x01\x02\xfe\xff")
self.assertEqual(
client.data_to_send(), [b"\x82\x84\x00\x00\x00\x00\x01\x02\xfe\xff"]
)
def test_server_sends_binary(self):
server = Protocol(SERVER)
server.send_binary(b"\x01\x02\xfe\xff")
self.assertEqual(server.data_to_send(), [b"\x82\x04\x01\x02\xfe\xff"])
def test_client_receives_binary(self):
client = Protocol(CLIENT)
client.receive_data(b"\x82\x04\x01\x02\xfe\xff")
self.assertFrameReceived(
client,
Frame(OP_BINARY, b"\x01\x02\xfe\xff"),
)
def test_server_receives_binary(self):
server = Protocol(SERVER)
server.receive_data(b"\x82\x84\x00\x00\x00\x00\x01\x02\xfe\xff")
self.assertFrameReceived(
server,
Frame(OP_BINARY, b"\x01\x02\xfe\xff"),
)
def test_client_receives_binary_over_size_limit(self):
client = Protocol(CLIENT, max_size=3)
client.receive_data(b"\x82\x04\x01\x02\xfe\xff")
self.assertIsInstance(client.parser_exc, PayloadTooBig)
self.assertEqual(
str(client.parser_exc),
"frame with 4 bytes exceeds limit of 3 bytes",
)
self.assertConnectionFailing(
client,
CloseCode.MESSAGE_TOO_BIG,
"frame with 4 bytes exceeds limit of 3 bytes",
)
def test_server_receives_binary_over_size_limit(self):
server = Protocol(SERVER, max_size=3)
server.receive_data(b"\x82\x84\x00\x00\x00\x00\x01\x02\xfe\xff")
self.assertIsInstance(server.parser_exc, PayloadTooBig)
self.assertEqual(
str(server.parser_exc),
"frame with 4 bytes exceeds limit of 3 bytes",
)
self.assertConnectionFailing(
server,
CloseCode.MESSAGE_TOO_BIG,
"frame with 4 bytes exceeds limit of 3 bytes",
)
def test_client_sends_fragmented_binary(self):
client = Protocol(CLIENT)
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"):
client.send_binary(b"\x01\x02", fin=False)
self.assertEqual(client.data_to_send(), [b"\x02\x82\x00\x00\x00\x00\x01\x02"])
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"):
client.send_continuation(b"\xee\xff\x01\x02", fin=False)
self.assertEqual(
client.data_to_send(), [b"\x00\x84\x00\x00\x00\x00\xee\xff\x01\x02"]
)
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"):
client.send_continuation(b"\xee\xff", fin=True)
self.assertEqual(client.data_to_send(), [b"\x80\x82\x00\x00\x00\x00\xee\xff"])
def test_server_sends_fragmented_binary(self):
server = Protocol(SERVER)
server.send_binary(b"\x01\x02", fin=False)
self.assertEqual(server.data_to_send(), [b"\x02\x02\x01\x02"])
server.send_continuation(b"\xee\xff\x01\x02", fin=False)
self.assertEqual(server.data_to_send(), [b"\x00\x04\xee\xff\x01\x02"])
server.send_continuation(b"\xee\xff", fin=True)
self.assertEqual(server.data_to_send(), [b"\x80\x02\xee\xff"])
def test_client_receives_fragmented_binary(self):
client = Protocol(CLIENT)
client.receive_data(b"\x02\x02\x01\x02")
self.assertFrameReceived(
client,
Frame(OP_BINARY, b"\x01\x02", fin=False),
)
client.receive_data(b"\x00\x04\xfe\xff\x01\x02")
self.assertFrameReceived(
client,
Frame(OP_CONT, b"\xfe\xff\x01\x02", fin=False),
)
client.receive_data(b"\x80\x02\xfe\xff")
self.assertFrameReceived(
client,
Frame(OP_CONT, b"\xfe\xff"),
)
def test_server_receives_fragmented_binary(self):
server = Protocol(SERVER)
server.receive_data(b"\x02\x82\x00\x00\x00\x00\x01\x02")
self.assertFrameReceived(
server,
Frame(OP_BINARY, b"\x01\x02", fin=False),
)
server.receive_data(b"\x00\x84\x00\x00\x00\x00\xee\xff\x01\x02")
self.assertFrameReceived(
server,
Frame(OP_CONT, b"\xee\xff\x01\x02", fin=False),
)
server.receive_data(b"\x80\x82\x00\x00\x00\x00\xfe\xff")
self.assertFrameReceived(
server,
Frame(OP_CONT, b"\xfe\xff"),
)
def test_client_receives_fragmented_binary_over_size_limit(self):
client = Protocol(CLIENT, max_size=3)
client.receive_data(b"\x02\x02\x01\x02")
self.assertFrameReceived(
client,
Frame(OP_BINARY, b"\x01\x02", fin=False),
)
client.receive_data(b"\x80\x02\xfe\xff")
self.assertIsInstance(client.parser_exc, PayloadTooBig)
self.assertEqual(
str(client.parser_exc),
"frame with 2 bytes after reading 2 bytes exceeds limit of 3 bytes",
)
self.assertConnectionFailing(
client,
CloseCode.MESSAGE_TOO_BIG,
"frame with 2 bytes after reading 2 bytes exceeds limit of 3 bytes",
)
def test_server_receives_fragmented_binary_over_size_limit(self):
server = Protocol(SERVER, max_size=3)
server.receive_data(b"\x02\x82\x00\x00\x00\x00\x01\x02")
self.assertFrameReceived(
server,
Frame(OP_BINARY, b"\x01\x02", fin=False),
)
server.receive_data(b"\x80\x82\x00\x00\x00\x00\xfe\xff")
self.assertIsInstance(server.parser_exc, PayloadTooBig)
self.assertEqual(
str(server.parser_exc),
"frame with 2 bytes after reading 2 bytes exceeds limit of 3 bytes",
)
self.assertConnectionFailing(
server,
CloseCode.MESSAGE_TOO_BIG,
"frame with 2 bytes after reading 2 bytes exceeds limit of 3 bytes",
)
def test_client_sends_unexpected_binary(self):
client = Protocol(CLIENT)
client.send_binary(b"", fin=False)
with self.assertRaises(ProtocolError) as raised:
client.send_binary(b"", fin=False)
self.assertEqual(str(raised.exception), "expected a continuation frame")
def test_server_sends_unexpected_binary(self):
server = Protocol(SERVER)
server.send_binary(b"", fin=False)
with self.assertRaises(ProtocolError) as raised:
server.send_binary(b"", fin=False)
self.assertEqual(str(raised.exception), "expected a continuation frame")
def test_client_receives_unexpected_binary(self):
client = Protocol(CLIENT)
client.receive_data(b"\x02\x00")
self.assertFrameReceived(
client,
Frame(OP_BINARY, b"", fin=False),
)
client.receive_data(b"\x02\x00")
self.assertIsInstance(client.parser_exc, ProtocolError)
self.assertEqual(str(client.parser_exc), "expected a continuation frame")
self.assertConnectionFailing(
client, CloseCode.PROTOCOL_ERROR, "expected a continuation frame"
)
def test_server_receives_unexpected_binary(self):
server = Protocol(SERVER)
server.receive_data(b"\x02\x80\x00\x00\x00\x00")
self.assertFrameReceived(
server,
Frame(OP_BINARY, b"", fin=False),
)
server.receive_data(b"\x02\x80\x00\x00\x00\x00")
self.assertIsInstance(server.parser_exc, ProtocolError)
self.assertEqual(str(server.parser_exc), "expected a continuation frame")
self.assertConnectionFailing(
server, CloseCode.PROTOCOL_ERROR, "expected a continuation frame"
)
def test_client_sends_binary_after_sending_close(self):
client = Protocol(CLIENT)
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"):
client.send_close(CloseCode.GOING_AWAY)
self.assertEqual(client.data_to_send(), [b"\x88\x82\x00\x00\x00\x00\x03\xe9"])
with self.assertRaises(InvalidState) as raised:
client.send_binary(b"")
self.assertEqual(str(raised.exception), "connection is closing")
def test_server_sends_binary_after_sending_close(self):
server = Protocol(SERVER)
server.send_close(CloseCode.NORMAL_CLOSURE)
self.assertEqual(server.data_to_send(), [b"\x88\x02\x03\xe8"])
with self.assertRaises(InvalidState) as raised:
server.send_binary(b"")
self.assertEqual(str(raised.exception), "connection is closing")
def test_client_receives_binary_after_receiving_close(self):
client = Protocol(CLIENT)
client.receive_data(b"\x88\x02\x03\xe8")
self.assertConnectionClosing(client, CloseCode.NORMAL_CLOSURE)
client.receive_data(b"\x82\x00")
self.assertFrameReceived(client, None)
self.assertFrameSent(client, None)
def test_server_receives_binary_after_receiving_close(self):
server = Protocol(SERVER)
server.receive_data(b"\x88\x82\x00\x00\x00\x00\x03\xe9")
self.assertConnectionClosing(server, CloseCode.GOING_AWAY)
server.receive_data(b"\x82\x80\x00\xff\x00\xff")
self.assertFrameReceived(server, None)
self.assertFrameSent(server, None)
class CloseTests(ProtocolTestCase):
"""
Test close frames.
See RFC 6455:
5.5.1. Close
7.1.6. The WebSocket Connection Close Reason
7.1.7. Fail the WebSocket Connection
"""
def test_close_code(self):
client = Protocol(CLIENT)
client.receive_data(b"\x88\x04\x03\xe8OK")
client.receive_eof()
self.assertEqual(client.close_code, CloseCode.NORMAL_CLOSURE)
def test_close_reason(self):
server = Protocol(SERVER)
server.receive_data(b"\x88\x84\x00\x00\x00\x00\x03\xe8OK")
server.receive_eof()
self.assertEqual(server.close_reason, "OK")
def test_close_code_not_provided(self):
server = Protocol(SERVER)
server.receive_data(b"\x88\x80\x00\x00\x00\x00")
server.receive_eof()
self.assertEqual(server.close_code, CloseCode.NO_STATUS_RCVD)
def test_close_reason_not_provided(self):
client = Protocol(CLIENT)
client.receive_data(b"\x88\x00")
client.receive_eof()
self.assertEqual(client.close_reason, "")
def test_close_code_not_available(self):
client = Protocol(CLIENT)
client.receive_eof()
self.assertEqual(client.close_code, CloseCode.ABNORMAL_CLOSURE)
def test_close_reason_not_available(self):
server = Protocol(SERVER)
server.receive_eof()
self.assertEqual(server.close_reason, "")
def test_close_code_not_available_yet(self):
server = Protocol(SERVER)
self.assertIsNone(server.close_code)
def test_close_reason_not_available_yet(self):
client = Protocol(CLIENT)
self.assertIsNone(client.close_reason)
def test_client_sends_close(self):
client = Protocol(CLIENT)
with patch("secrets.token_bytes", return_value=b"\x3c\x3c\x3c\x3c"):
client.send_close()
self.assertEqual(client.data_to_send(), [b"\x88\x80\x3c\x3c\x3c\x3c"])
self.assertIs(client.state, CLOSING)
def test_server_sends_close(self):
server = Protocol(SERVER)
server.send_close()
self.assertEqual(server.data_to_send(), [b"\x88\x00"])
self.assertIs(server.state, CLOSING)
def test_client_receives_close(self):
client = Protocol(CLIENT)
with patch("secrets.token_bytes", return_value=b"\x3c\x3c\x3c\x3c"):
client.receive_data(b"\x88\x00")
self.assertEqual(client.events_received(), [Frame(OP_CLOSE, b"")])
self.assertEqual(client.data_to_send(), [b"\x88\x80\x3c\x3c\x3c\x3c"])
self.assertIs(client.state, CLOSING)
def test_server_receives_close(self):
server = Protocol(SERVER)
server.receive_data(b"\x88\x80\x3c\x3c\x3c\x3c")
self.assertEqual(server.events_received(), [Frame(OP_CLOSE, b"")])
self.assertEqual(server.data_to_send(), [b"\x88\x00", b""])
self.assertIs(server.state, CLOSING)
def test_client_sends_close_then_receives_close(self):
# Client-initiated close handshake on the client side.
client = Protocol(CLIENT)
client.send_close()
self.assertFrameReceived(client, None)
self.assertFrameSent(client, Frame(OP_CLOSE, b""))
client.receive_data(b"\x88\x00")
self.assertFrameReceived(client, Frame(OP_CLOSE, b""))
self.assertFrameSent(client, None)
client.receive_eof()
self.assertFrameReceived(client, None)
self.assertFrameSent(client, None, eof=True)
def test_server_sends_close_then_receives_close(self):
# Server-initiated close handshake on the server side.
server = Protocol(SERVER)
server.send_close()
self.assertFrameReceived(server, None)
self.assertFrameSent(server, Frame(OP_CLOSE, b""))
server.receive_data(b"\x88\x80\x3c\x3c\x3c\x3c")
self.assertFrameReceived(server, Frame(OP_CLOSE, b""))
self.assertFrameSent(server, None, eof=True)
server.receive_eof()
self.assertFrameReceived(server, None)
self.assertFrameSent(server, None)
def test_client_receives_close_then_sends_close(self):
# Server-initiated close handshake on the client side.
client = Protocol(CLIENT)
client.receive_data(b"\x88\x00")
self.assertFrameReceived(client, Frame(OP_CLOSE, b""))
self.assertFrameSent(client, Frame(OP_CLOSE, b""))
client.receive_eof()
self.assertFrameReceived(client, None)
self.assertFrameSent(client, None, eof=True)
def test_server_receives_close_then_sends_close(self):
# Client-initiated close handshake on the server side.
server = Protocol(SERVER)
server.receive_data(b"\x88\x80\x3c\x3c\x3c\x3c")
self.assertFrameReceived(server, Frame(OP_CLOSE, b""))
self.assertFrameSent(server, Frame(OP_CLOSE, b""), eof=True)
server.receive_eof()
self.assertFrameReceived(server, None)
self.assertFrameSent(server, None)
def test_client_sends_close_with_code(self):
client = Protocol(CLIENT)
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"):
client.send_close(CloseCode.GOING_AWAY)
self.assertEqual(client.data_to_send(), [b"\x88\x82\x00\x00\x00\x00\x03\xe9"])
self.assertIs(client.state, CLOSING)
def test_server_sends_close_with_code(self):
server = Protocol(SERVER)
server.send_close(CloseCode.NORMAL_CLOSURE)
self.assertEqual(server.data_to_send(), [b"\x88\x02\x03\xe8"])
self.assertIs(server.state, CLOSING)
def test_client_receives_close_with_code(self):
client = Protocol(CLIENT)
client.receive_data(b"\x88\x02\x03\xe8")
self.assertConnectionClosing(client, CloseCode.NORMAL_CLOSURE, "")
self.assertIs(client.state, CLOSING)
def test_server_receives_close_with_code(self):
server = Protocol(SERVER)
server.receive_data(b"\x88\x82\x00\x00\x00\x00\x03\xe9")
self.assertConnectionClosing(server, CloseCode.GOING_AWAY, "")
self.assertIs(server.state, CLOSING)
def test_client_sends_close_with_code_and_reason(self):
client = Protocol(CLIENT)
with patch("secrets.token_bytes", return_value=b"\x00\x00\x00\x00"):
client.send_close(CloseCode.GOING_AWAY, "going away")
self.assertEqual(
client.data_to_send(), [b"\x88\x8c\x00\x00\x00\x00\x03\xe9going away"]
)
self.assertIs(client.state, CLOSING)
def test_server_sends_close_with_code_and_reason(self):
server = Protocol(SERVER)
server.send_close(CloseCode.NORMAL_CLOSURE, "OK")
self.assertEqual(server.data_to_send(), [b"\x88\x04\x03\xe8OK"])
self.assertIs(server.state, CLOSING)
def test_client_receives_close_with_code_and_reason(self):
client = Protocol(CLIENT)
client.receive_data(b"\x88\x04\x03\xe8OK")
self.assertConnectionClosing(client, CloseCode.NORMAL_CLOSURE, "OK")
self.assertIs(client.state, CLOSING)
def test_server_receives_close_with_code_and_reason(self):
server = Protocol(SERVER)
server.receive_data(b"\x88\x8c\x00\x00\x00\x00\x03\xe9going away")
self.assertConnectionClosing(server, CloseCode.GOING_AWAY, "going away")
self.assertIs(server.state, CLOSING)
def test_client_sends_close_with_reason_only(self):
client = Protocol(CLIENT)
with self.assertRaises(ProtocolError) as raised:
client.send_close(reason="going away")
self.assertEqual(str(raised.exception), "cannot send a reason without a code")
def test_server_sends_close_with_reason_only(self):
server = Protocol(SERVER)
with self.assertRaises(ProtocolError) as raised:
server.send_close(reason="OK")
self.assertEqual(str(raised.exception), "cannot send a reason without a code")
def test_client_receives_close_with_truncated_code(self):
client = Protocol(CLIENT)
client.receive_data(b"\x88\x01\x03")
self.assertIsInstance(client.parser_exc, ProtocolError)
self.assertEqual(str(client.parser_exc), "close frame too short")
self.assertConnectionFailing(
client, CloseCode.PROTOCOL_ERROR, "close frame too short"
)
self.assertIs(client.state, CLOSING)
def test_server_receives_close_with_truncated_code(self):
server = Protocol(SERVER)
server.receive_data(b"\x88\x81\x00\x00\x00\x00\x03")
self.assertIsInstance(server.parser_exc, ProtocolError)
self.assertEqual(str(server.parser_exc), "close frame too short")
self.assertConnectionFailing(
server, CloseCode.PROTOCOL_ERROR, "close frame too short"
)
self.assertIs(server.state, CLOSING)
def test_client_receives_close_with_non_utf8_reason(self):
client = Protocol(CLIENT)
client.receive_data(b"\x88\x04\x03\xe8\xff\xff")
self.assertIsInstance(client.parser_exc, UnicodeDecodeError)
self.assertEqual(
str(client.parser_exc),