def test_read_after_close(self): with BytesIO() as buf: r = BytesReader(b'12345') self.assertFalse(r.closed) r.close() self.assertTrue(r.closed) self.assertRaises(ValueError, r.read)
def test_decode_zero_nrb(self): buf = bytearray(a2b_hex('c000')) num_bytes_consumed, h = mqtt_codec.packet.MqttFixedHeader.decode( BytesReader(buf)) self.assertEqual(h.remaining_len, 0) self.assertEqual(2, num_bytes_consumed) self.assertEqual(2, h.size)
def test_bad_result(self): # with BytesIO() as f: # mqtt_codec.packet.MqttSuback(3, [mqtt_codec.packet.SubscribeResult.qos0,]).encode(f) # print(repr(f.getvalue())) buf = bytearray(b'\x90\x03\x00\x03\x00') buf[-1] = 0xff self.assertRaises(DecodeError, mqtt_codec.packet.MqttSuback.decode, BytesReader(buf))
def assert_extra_bytes_fail(self, p): with BytesIO() as f: p.encode(f) buf = bytearray(f.getvalue()) buf[1] += 1 buf.append(0) self.assertRaises(DecodeError, p.decode, BytesReader(buf))
def test_qos_dupe_disagree_decode(self): # with BytesIO() as f: # p = mqtt_codec.packet.MqttPublish(3, 'flugelhorn', b'silly_payload', False, 2, False) # p.encode(f) # print(repr(f.getvalue())) buf = bytearray(b'4\x1b\x00\nflugelhorn\x00\x03silly_payload') buf[0] = buf[0] & 0xf0 | 0x08 self.assertRaises(DecodeError, mqtt_codec.packet.MqttPublish.decode, BytesReader(buf))
def test_publish_payload(self): publish = mqtt_codec.packet.MqttPublish(3, 'flugelhorn', b'silly_payload', False, 2, False) with BytesIO() as f: publish.encode(f) buf = f.getvalue() buf = bytearray(buf) num_bytes_consumed, recovered_publish = mqtt_codec.packet.MqttPublish.decode( BytesReader(buf))
def test_bad_will_qos(self): connect = mqtt_codec.packet.MqttConnect('client_id', False, 0) with BytesIO() as f: connect.encode(f) buf = bytearray(f.getvalue()) self.assertEqual(len(buf), connect.size) self.assertEqual( b'\x10\x15\x00\x04MQTT\x04\x00\x00\x00\x00\tclient_id', buf) # Corrupt the connect header. buf[9] = buf[9] | 0x18 self.assertRaises(DecodeError, mqtt_codec.packet.MqttConnect.decode, BytesReader(buf))
def assert_codec_okay(self, p, expected_bytes_hex=None): repr_src = repr(p) str_src = repr(p) buf = self.buffer_packet(p) num_decoded_bytes, decoded_p = p.decode(BytesReader(buf)) repr_recovered = repr(decoded_p) str_recovered = str(decoded_p) # self.assertEqual(repr_encoded, repr_decoded) if expected_bytes_hex: expected_bytes = a2b_hex(expected_bytes_hex) self.assertEqual(expected_bytes, buf) self.assertEqual(num_decoded_bytes, len(buf)) self.assertEqual(p, decoded_p)
def test_extra_bytes(self): buf = bytearray(a2b_hex('c000')) num_bytes_consumed, h = mqtt_codec.packet.MqttFixedHeader.decode( BytesReader(buf)) self.assertEqual(h.remaining_len, 0) self.assertEqual(2, num_bytes_consumed) connect = mqtt_codec.packet.MqttConnect('client_id', False, 0) with BytesIO() as f: num_bytes_written = connect.encode(f) value0 = f.getvalue() f.seek(1) f.write(b'\x7f') f.seek(0) value1 = f.getvalue() self.assertRaises(DecodeError, mqtt_codec.packet.MqttConnect.decode, f)
def test_context(self): with BytesReader(b'asdfasdf') as r: self.assertFalse(r.closed) self.assertTrue(r.closed)
def test_packat(self): buf = bytearray(a2b_hex('c000')) num_bytes_consumed, h = mqtt_codec.packet.MqttFixedHeader.decode( BytesReader(buf)) self.assertEqual(h, h.packet())
def test_invalid_flags(self): buf = bytearray(a2b_hex('cf01')) self.assertRaises(DecodeError, mqtt_codec.packet.MqttFixedHeader.decode, BytesReader(buf))
def test_mqtt_bad_topic(self): buf = bytearray( b'\x82\x12\x00\x07\x00\x05hello\x00\x00\x01x\x01\x00\x01Z\x02') buf[11] = 10 self.assertRaises(DecodeError, mqtt_codec.packet.MqttSubscribe.decode, BytesReader(buf))
def test_bad_return_code(self): buf = a2b_hex('200201ff') self.assertRaises(DecodeError, mqtt_codec.packet.MqttConnack.decode, BytesReader(buf))
def test_bad_session_present(self): buf = a2b_hex('20020200') self.assertRaises(DecodeError, mqtt_codec.packet.MqttConnack.decode, BytesReader(buf))
def test_underflow_0(self): buf = b'' self.assertRaises(mqtt_codec.io.UnderflowDecodeError, mqtt_codec.packet.MqttFixedHeader.decode, BytesReader(buf))