Example #1
0
 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)
Example #2
0
 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)
Example #3
0
    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))
Example #4
0
    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))
Example #5
0
    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))
Example #6
0
    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))
Example #7
0
    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))
Example #8
0
    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)
Example #9
0
    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)
Example #10
0
 def test_context(self):
     with BytesReader(b'asdfasdf') as r:
         self.assertFalse(r.closed)
     self.assertTrue(r.closed)
Example #11
0
 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())
Example #12
0
 def test_invalid_flags(self):
     buf = bytearray(a2b_hex('cf01'))
     self.assertRaises(DecodeError,
                       mqtt_codec.packet.MqttFixedHeader.decode,
                       BytesReader(buf))
Example #13
0
 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))
Example #14
0
 def test_bad_return_code(self):
     buf = a2b_hex('200201ff')
     self.assertRaises(DecodeError, mqtt_codec.packet.MqttConnack.decode,
                       BytesReader(buf))
Example #15
0
 def test_bad_session_present(self):
     buf = a2b_hex('20020200')
     self.assertRaises(DecodeError, mqtt_codec.packet.MqttConnack.decode,
                       BytesReader(buf))
Example #16
0
 def test_underflow_0(self):
     buf = b''
     self.assertRaises(mqtt_codec.io.UnderflowDecodeError,
                       mqtt_codec.packet.MqttFixedHeader.decode,
                       BytesReader(buf))