Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_invalid_encoded_bytes_on_continuation(self):
s = Stream()
f = Frame(opcode=OPCODE_TEXT, body=b'hello',
fin=0, masking_key=os.urandom(4)).build()
s.parser.send(f)
next(s.parser)
f = Frame(opcode=OPCODE_CONTINUATION, body=b'h\xc3llo',
fin=1, masking_key=os.urandom(4)).build()
s.parser.send(f)
next(s.parser)
self.assertNotEqual(s.errors, [])
self.assertIsInstance(s.errors[0], CloseControlMessage)
self.assertEqual(s.errors[0].code, 1007)
def test_incremental_parsing_small_7_bit_length(self):
bytes = Frame(opcode=OPCODE_TEXT, body=b'hello', fin=1).build()
f = Frame()
map_on_bytes(f.parser.send, bytes)
self.assertTrue(f.masking_key is None)
self.assertEqual(f.payload_length, 5)
msg = os.urandom(16)
key = os.urandom(4)
f = Frame(opcode=OPCODE_BINARY, body=msg, fin=0, masking_key=key).build()
s = Stream()
self.assertEqual(s.has_message, False)
s.parser.send(f)
self.assertEqual(s.has_message, False)
for i in range(3):
f = Frame(opcode=OPCODE_CONTINUATION, body=msg, fin=0, masking_key=key).build()
s.parser.send(f)
self.assertEqual(s.has_message, False)
self.assertEqual(s.message.completed, False)
self.assertEqual(s.message.opcode, OPCODE_BINARY)
f = Frame(opcode=OPCODE_CONTINUATION, body=msg, fin=1, masking_key=key).build()
s.parser.send(f)
self.assertEqual(s.has_message, True)
self.assertEqual(s.message.completed, True)
self.assertEqual(s.message.opcode, OPCODE_BINARY)
def test_non_zero_nor_one_fin(self):
f = Frame(opcode=OPCODE_TEXT,
body=b'', fin=2)
self.assertRaises(ValueError, f.build)
def test_incremental_text_message_received(self):
msg = b'hello there'
f = Frame(opcode=OPCODE_TEXT, body=msg, fin=1, masking_key=os.urandom(4)).build()
s = Stream()
self.assertEqual(s.has_message, False)
bytes = f
for index, byte in enumerate(bytes):
s.parser.send(bytes[index:index+1])
self.assertEqual(s.has_message, True)
def test_text_message_with_continuation_received(self):
msg = b'hello there'
f = Frame(opcode=OPCODE_TEXT, body=msg, fin=0, masking_key=os.urandom(4)).build()
s = Stream()
self.assertEqual(s.has_message, False)
s.parser.send(f)
self.assertEqual(s.message.completed, False)
for i in range(3):
f = Frame(opcode=OPCODE_CONTINUATION, body=msg, fin=0, masking_key=os.urandom(4)).build()
s.parser.send(f)
self.assertEqual(s.has_message, False)
self.assertEqual(s.message.completed, False)
self.assertEqual(s.message.opcode, OPCODE_TEXT)
f = Frame(opcode=OPCODE_CONTINUATION, body=msg, fin=1, masking_key=os.urandom(4)).build()
s.parser.send(f)
self.assertEqual(s.has_message, True)
self.assertEqual(s.message.completed, True)
def test_63_bit_length(self):
f = Frame(opcode=OPCODE_TEXT,
body=b'*' * 65536, fin=1)
self.assertEqual(len(f.build()), 65546)
mask = os.urandom(4)
f = Frame(opcode=OPCODE_TEXT,
body=b'*' * 65536, masking_key=mask, fin=1)
self.assertEqual(len(f.build()), 65550)
def test_using_masking_key_when_unexpected(self):
f = Frame(opcode=OPCODE_TEXT, body=b'hello', fin=1, masking_key=os.urandom(4)).build()
s = Stream(expect_masking=False)
s.parser.send(f)
next(s.parser)
self.assertNotEqual(s.errors, [])
self.assertIsInstance(s.errors[0], CloseControlMessage)
self.assertEqual(s.errors[0].code, 1002)
def _exchange2(self, *args, **kwargs):
yield Frame(opcode=OPCODE_CLOSE, body=b'',
fin=1).build()
def test_frame_header_parsing(self):
bytes = Frame(opcode=OPCODE_TEXT, body=b'hello', fin=1).build()
f = Frame()
self.assertEqual(f.parser.send(bytes[0:1]), 1)
self.assertEqual(f.fin, 1)
self.assertEqual(f.rsv1, 0)
self.assertEqual(f.rsv2, 0)
self.assertEqual(f.rsv3, 0)
self.assertEqual(f.parser.send(bytes[1:2]), 5)
self.assertTrue(f.masking_key is None)
self.assertEqual(f.payload_length, 5)
f.parser.close()