| 1 |
import asyncio |
| 2 |
import codecs |
| 3 |
import dataclasses |
| 4 |
import unittest |
| 5 |
import unittest.mock |
| 6 |
import warnings |
| 7 |
|
| 8 |
from websockets.exceptions import PayloadTooBig, ProtocolError |
| 9 |
from websockets.frames import ( |
| 10 |
BINARY as OP_BINARY, |
| 11 |
CLOSE as OP_CLOSE, |
| 12 |
PING as OP_PING, |
| 13 |
PONG as OP_PONG, |
| 14 |
TEXT as OP_TEXT, |
| 15 |
CloseCode, |
| 16 |
) |
| 17 |
from websockets.legacy.framing import * |
| 18 |
|
| 19 |
from .utils import AsyncioTestCase |
| 20 |
|
| 21 |
|
| 22 |
class FramingTests(AsyncioTestCase): |
| 23 |
def decode(self, message, mask=False, max_size=None, extensions=None): |
| 24 |
stream = asyncio.StreamReader(loop=self.loop) |
| 25 |
stream.feed_data(message) |
| 26 |
stream.feed_eof() |
| 27 |
with warnings.catch_warnings(): |
| 28 |
warnings.simplefilter("ignore") |
| 29 |
frame = self.loop.run_until_complete( |
| 30 |
Frame.read( |
| 31 |
stream.readexactly, |
| 32 |
mask=mask, |
| 33 |
max_size=max_size, |
| 34 |
extensions=extensions, |
| 35 |
) |
| 36 |
) |
| 37 |
# Make sure all the data was consumed. |
| 38 |
self.assertTrue(stream.at_eof()) |
| 39 |
return frame |
| 40 |
|
| 41 |
def encode(self, frame, mask=False, extensions=None): |
| 42 |
write = unittest.mock.Mock() |
| 43 |
with warnings.catch_warnings(): |
| 44 |
warnings.simplefilter("ignore") |
| 45 |
frame.write(write, mask=mask, extensions=extensions) |
| 46 |
# Ensure the entire frame is sent with a single call to write(). |
| 47 |
# Multiple calls cause TCP fragmentation and degrade performance. |
| 48 |
self.assertEqual(write.call_count, 1) |
| 49 |
# The frame data is the single positional argument of that call. |
| 50 |
self.assertEqual(len(write.call_args[0]), 1) |
| 51 |
self.assertEqual(len(write.call_args[1]), 0) |
| 52 |
return write.call_args[0][0] |
| 53 |
|
| 54 |
def round_trip(self, message, expected, mask=False, extensions=None): |
| 55 |
decoded = self.decode(message, mask, extensions=extensions) |
| 56 |
decoded.check() |
| 57 |
self.assertEqual(decoded, expected) |
| 58 |
encoded = self.encode(decoded, mask, extensions=extensions) |
| 59 |
if mask: # non-deterministic encoding |
| 60 |
decoded = self.decode(encoded, mask, extensions=extensions) |
| 61 |
self.assertEqual(decoded, expected) |
| 62 |
else: # deterministic encoding |
| 63 |
self.assertEqual(encoded, message) |
| 64 |
|
| 65 |
def test_text(self): |
| 66 |
self.round_trip(b"\x81\x04Spam", Frame(True, OP_TEXT, b"Spam")) |
| 67 |
|
| 68 |
def test_text_masked(self): |
| 69 |
self.round_trip( |
| 70 |
b"\x81\x84\x5b\xfb\xe1\xa8\x08\x8b\x80\xc5", |
| 71 |
Frame(True, OP_TEXT, b"Spam"), |
| 72 |
mask=True, |
| 73 |
) |
| 74 |
|
| 75 |
def test_binary(self): |
| 76 |
self.round_trip(b"\x82\x04Eggs", Frame(True, OP_BINARY, b"Eggs")) |
| 77 |
|
| 78 |
def test_binary_masked(self): |
| 79 |
self.round_trip( |
| 80 |
b"\x82\x84\x53\xcd\xe2\x89\x16\xaa\x85\xfa", |
| 81 |
Frame(True, OP_BINARY, b"Eggs"), |
| 82 |
mask=True, |
| 83 |
) |
| 84 |
|
| 85 |
def test_non_ascii_text(self): |
| 86 |
self.round_trip(b"\x81\x05caf\xc3\xa9", Frame(True, OP_TEXT, "café".encode())) |
| 87 |
|
| 88 |
def test_non_ascii_text_masked(self): |
| 89 |
self.round_trip( |
| 90 |
b"\x81\x85\x64\xbe\xee\x7e\x07\xdf\x88\xbd\xcd", |
| 91 |
Frame(True, OP_TEXT, "café".encode()), |
| 92 |
mask=True, |
| 93 |
) |
| 94 |
|
| 95 |
def test_close(self): |
| 96 |
self.round_trip(b"\x88\x00", Frame(True, OP_CLOSE, b"")) |
| 97 |
|
| 98 |
def test_ping(self): |
| 99 |
self.round_trip(b"\x89\x04ping", Frame(True, OP_PING, b"ping")) |
| 100 |
|
| 101 |
def test_pong(self): |
| 102 |
self.round_trip(b"\x8a\x04pong", Frame(True, OP_PONG, b"pong")) |
| 103 |
|
| 104 |
def test_long(self): |
| 105 |
self.round_trip( |
| 106 |
b"\x82\x7e\x00\x7e" + 126 * b"a", Frame(True, OP_BINARY, 126 * b"a") |
| 107 |
) |
| 108 |
|
| 109 |
def test_very_long(self): |
| 110 |
self.round_trip( |
| 111 |
b"\x82\x7f\x00\x00\x00\x00\x00\x01\x00\x00" + 65536 * b"a", |
| 112 |
Frame(True, OP_BINARY, 65536 * b"a"), |
| 113 |
) |
| 114 |
|
| 115 |
def test_payload_too_big(self): |
| 116 |
with self.assertRaises(PayloadTooBig): |
| 117 |
self.decode(b"\x82\x7e\x04\x01" + 1025 * b"a", max_size=1024) |
| 118 |
|
| 119 |
def test_bad_reserved_bits(self): |
| 120 |
for encoded in [b"\xc0\x00", b"\xa0\x00", b"\x90\x00"]: |
| 121 |
with self.subTest(encoded=encoded): |
| 122 |
with self.assertRaises(ProtocolError): |
| 123 |
self.decode(encoded) |
| 124 |
|
| 125 |
def test_good_opcode(self): |
| 126 |
for opcode in list(range(0x00, 0x03)) + list(range(0x08, 0x0B)): |
| 127 |
encoded = bytes([0x80 | opcode, 0]) |
| 128 |
with self.subTest(encoded=encoded): |
| 129 |
self.decode(encoded) # does not raise an exception |
| 130 |
|
| 131 |
def test_bad_opcode(self): |
| 132 |
for opcode in list(range(0x03, 0x08)) + list(range(0x0B, 0x10)): |
| 133 |
encoded = bytes([0x80 | opcode, 0]) |
| 134 |
with self.subTest(encoded=encoded): |
| 135 |
with self.assertRaises(ProtocolError): |
| 136 |
self.decode(encoded) |
| 137 |
|
| 138 |
def test_mask_flag(self): |
| 139 |
# Mask flag correctly set. |
| 140 |
self.decode(b"\x80\x80\x00\x00\x00\x00", mask=True) |
| 141 |
# Mask flag incorrectly unset. |
| 142 |
with self.assertRaises(ProtocolError): |
| 143 |
self.decode(b"\x80\x80\x00\x00\x00\x00") |
| 144 |
# Mask flag correctly unset. |
| 145 |
self.decode(b"\x80\x00") |
| 146 |
# Mask flag incorrectly set. |
| 147 |
with self.assertRaises(ProtocolError): |
| 148 |
self.decode(b"\x80\x00", mask=True) |
| 149 |
|
| 150 |
def test_control_frame_max_length(self): |
| 151 |
# At maximum allowed length. |
| 152 |
self.decode(b"\x88\x7e\x00\x7d" + 125 * b"a") |
| 153 |
# Above maximum allowed length. |
| 154 |
with self.assertRaises(ProtocolError): |
| 155 |
self.decode(b"\x88\x7e\x00\x7e" + 126 * b"a") |
| 156 |
|
| 157 |
def test_fragmented_control_frame(self): |
| 158 |
# Fin bit correctly set. |
| 159 |
self.decode(b"\x88\x00") |
| 160 |
# Fin bit incorrectly unset. |
| 161 |
with self.assertRaises(ProtocolError): |
| 162 |
self.decode(b"\x08\x00") |
| 163 |
|
| 164 |
def test_extensions(self): |
| 165 |
class Rot13: |
| 166 |
@staticmethod |
| 167 |
def encode(frame): |
| 168 |
assert frame.opcode == OP_TEXT |
| 169 |
text = frame.data.decode() |
| 170 |
data = codecs.encode(text, "rot13").encode() |
| 171 |
return dataclasses.replace(frame, data=data) |
| 172 |
|
| 173 |
# This extensions is symmetrical. |
| 174 |
@staticmethod |
| 175 |
def decode(frame, *, max_size=None): |
| 176 |
return Rot13.encode(frame) |
| 177 |
|
| 178 |
self.round_trip( |
| 179 |
b"\x81\x05uryyb", Frame(True, OP_TEXT, b"hello"), extensions=[Rot13()] |
| 180 |
) |
| 181 |
|
| 182 |
|
| 183 |
class PrepareDataTests(unittest.TestCase): |
| 184 |
def test_prepare_data_str(self): |
| 185 |
self.assertEqual( |
| 186 |
prepare_data("café"), |
| 187 |
(OP_TEXT, b"caf\xc3\xa9"), |
| 188 |
) |
| 189 |
|
| 190 |
def test_prepare_data_bytes(self): |
| 191 |
self.assertEqual( |
| 192 |
prepare_data(b"tea"), |
| 193 |
(OP_BINARY, b"tea"), |
| 194 |
) |
| 195 |
|
| 196 |
def test_prepare_data_bytearray(self): |
| 197 |
self.assertEqual( |
| 198 |
prepare_data(bytearray(b"tea")), |
| 199 |
(OP_BINARY, bytearray(b"tea")), |
| 200 |
) |
| 201 |
|
| 202 |
def test_prepare_data_memoryview(self): |
| 203 |
self.assertEqual( |
| 204 |
prepare_data(memoryview(b"tea")), |
| 205 |
(OP_BINARY, memoryview(b"tea")), |
| 206 |
) |
| 207 |
|
| 208 |
def test_prepare_data_list(self): |
| 209 |
with self.assertRaises(TypeError): |
| 210 |
prepare_data([]) |
| 211 |
|
| 212 |
def test_prepare_data_none(self): |
| 213 |
with self.assertRaises(TypeError): |
| 214 |
prepare_data(None) |
| 215 |
|
| 216 |
|
| 217 |
class PrepareCtrlTests(unittest.TestCase): |
| 218 |
def test_prepare_ctrl_str(self): |
| 219 |
self.assertEqual(prepare_ctrl("café"), b"caf\xc3\xa9") |
| 220 |
|
| 221 |
def test_prepare_ctrl_bytes(self): |
| 222 |
self.assertEqual(prepare_ctrl(b"tea"), b"tea") |
| 223 |
|
| 224 |
def test_prepare_ctrl_bytearray(self): |
| 225 |
self.assertEqual(prepare_ctrl(bytearray(b"tea")), b"tea") |
| 226 |
|
| 227 |
def test_prepare_ctrl_memoryview(self): |
| 228 |
self.assertEqual(prepare_ctrl(memoryview(b"tea")), b"tea") |
| 229 |
|
| 230 |
def test_prepare_ctrl_list(self): |
| 231 |
with self.assertRaises(TypeError): |
| 232 |
prepare_ctrl([]) |
| 233 |
|
| 234 |
def test_prepare_ctrl_none(self): |
| 235 |
with self.assertRaises(TypeError): |
| 236 |
prepare_ctrl(None) |
| 237 |
|
| 238 |
|
| 239 |
class ParseAndSerializeCloseTests(unittest.TestCase): |
| 240 |
def assertCloseData(self, code, reason, data): |
| 241 |
""" |
| 242 |
Serializing code / reason yields data. Parsing data yields code / reason. |
| 243 |
|
| 244 |
""" |
| 245 |
serialized = serialize_close(code, reason) |
| 246 |
self.assertEqual(serialized, data) |
| 247 |
parsed = parse_close(data) |
| 248 |
self.assertEqual(parsed, (code, reason)) |
| 249 |
|
| 250 |
def test_parse_close_and_serialize_close(self): |
| 251 |
self.assertCloseData(CloseCode.NORMAL_CLOSURE, "", b"\x03\xe8") |
| 252 |
self.assertCloseData(CloseCode.NORMAL_CLOSURE, "OK", b"\x03\xe8OK") |
| 253 |
|
| 254 |
def test_parse_close_empty(self): |
| 255 |
self.assertEqual(parse_close(b""), (CloseCode.NO_STATUS_RCVD, "")) |
| 256 |
|
| 257 |
def test_parse_close_errors(self): |
| 258 |
with self.assertRaises(ProtocolError): |
| 259 |
parse_close(b"\x03") |
| 260 |
with self.assertRaises(ProtocolError): |
| 261 |
parse_close(b"\x03\xe7") |
| 262 |
with self.assertRaises(UnicodeDecodeError): |
| 263 |
parse_close(b"\x03\xe8\xff\xff") |
| 264 |
|
| 265 |
def test_serialize_close_errors(self): |
| 266 |
with self.assertRaises(ProtocolError): |
| 267 |
serialize_close(999, "") |