def test_getattr_ok(self): data = b'\x10\x3e\x00\x04MQTT\x04\xce\x00\x00\x00\x0a0123456789\x00\x09WillTopic\x00\x0bWillMessage\x00\x04user\x00\x08password' stream = BufferAdapter(data) message = anyio.run(ConnectPacket.from_stream, stream) self.assertEqual(message.variable_header.proto_name, "MQTT") self.assertEqual(message.proto_name, "MQTT") self.assertEqual(message.variable_header.proto_level, 4) self.assertEqual(message.proto_level, 4) self.assertTrue(message.variable_header.username_flag) self.assertTrue(message.username_flag) self.assertTrue(message.variable_header.password_flag) self.assertTrue(message.password_flag) self.assertFalse(message.variable_header.will_retain_flag) self.assertFalse(message.will_retain_flag) self.assertEqual(message.variable_header.will_qos, 1) self.assertEqual(message.will_qos, 1) self.assertTrue(message.variable_header.will_flag) self.assertTrue(message.will_flag) self.assertTrue(message.variable_header.clean_session_flag) self.assertTrue(message.clean_session_flag) self.assertFalse(message.variable_header.reserved_flag) self.assertFalse(message.reserved_flag) self.assertEqual(message.payload.client_id, '0123456789') self.assertEqual(message.client_id, '0123456789') self.assertEqual(message.payload.will_topic, 'WillTopic') self.assertEqual(message.will_topic, 'WillTopic') self.assertEqual(message.payload.will_message, b'WillMessage') self.assertEqual(message.will_message, b'WillMessage') self.assertEqual(message.payload.username, 'user') self.assertEqual(message.username, 'user') self.assertEqual(message.payload.password, 'password') self.assertEqual(message.password, 'password')
def test_from_bytes_with_length(self): data = b'\x10\xff\xff\xff\x7f' stream = BufferAdapter(data) header = anyio.run(MQTTFixedHeader.from_stream, stream) self.assertEqual(header.packet_type, CONNECT) self.assertFalse(header.flags & 0x08) self.assertEqual((header.flags & 0x06) >> 1, 0) self.assertFalse(header.flags & 0x01) self.assertEqual(header.remaining_length, 268435455)
def test_from_stream_qos_2(self): data = b'\x37\x13\x00\x05topic\x00\x0a0123456789' stream = BufferAdapter(data) message = anyio.run(PublishPacket.from_stream, stream) self.assertEqual(message.variable_header.topic_name, 'topic') self.assertEqual(message.variable_header.packet_id, 10) self.assertTrue((message.fixed_header.flags >> 1) & 0x03) self.assertTrue(message.fixed_header.flags & 0x01) self.assertTrue(message.payload.data, b'0123456789')
def test_from_stream(self): data = b'\x80\x0e\x00\x0a\x00\x03a/b\x01\x00\x03c/d\x02' stream = BufferAdapter(data) message = anyio.run(SubscribePacket.from_stream, stream) (topic, qos) = message.payload.topics[0] self.assertEqual(topic, 'a/b') self.assertEqual(qos, QOS_1) (topic, qos) = message.payload.topics[1] self.assertEqual(topic, 'c/d') self.assertEqual(qos, QOS_2)
def test_from_stream(self): data = b'\x90\x06\x00\x0a\x00\x01\x02\x80' stream = BufferAdapter(data) message = anyio.run(SubackPacket.from_stream, stream) self.assertEqual(message.payload.return_codes[0], SubackPayload.RETURN_CODE_00) self.assertEqual(message.payload.return_codes[1], SubackPayload.RETURN_CODE_01) self.assertEqual(message.payload.return_codes[2], SubackPayload.RETURN_CODE_02) self.assertEqual(message.payload.return_codes[3], SubackPayload.RETURN_CODE_80)
def test_from_bytes_ko_with_length(self): data = b'\x10\xff\xff\xff\xff\x7f' stream = BufferAdapter(data) with self.assertRaises(MQTTException): anyio.run(MQTTFixedHeader.from_stream, stream)
def test_from_stream(self): data = b'\x50\x02\x00\x0a' stream = BufferAdapter(data) message = anyio.run(PubrecPacket.from_stream, stream) self.assertEqual(message.variable_header.packet_id, 10)
def test_from_stream(self): data = b'\xa2\x0c\x00\n\x00\x03a/b\x00\x03c/d' stream = BufferAdapter(data) message = anyio.run(UnsubscribePacket.from_stream, stream) self.assertEqual(message.payload.topics[0], 'a/b') self.assertEqual(message.payload.topics[1], 'c/d')
async def test_coro(): stream = BufferAdapter(b'\x00\x02AA') ret = await decode_string(stream) self.assertEqual(ret, 'AA')
def test_decode_fail_miss_password(self): data = b'\x10\x34\x00\x04MQTT\x04\xce\x00\x00\x00\x0a0123456789\x00\x09WillTopic\x00\x0bWillMessage\x00\x04user' stream = BufferAdapter(data) message = anyio.run(ConnectPacket.from_stream, stream) self.assertIs(message.payload.password, None)
def test_decode_fail_miss_willtopic(self): data = b'\x10\x16\x00\x04MQTT\x04\xce\x00\x00\x00\x0a0123456789' stream = BufferAdapter(data) message = anyio.run(ConnectPacket.from_stream, stream) self.assertIs(message.payload.will_topic, None)
def test_decode_fail_miss_clientId(self): data = b'\x10\x0a\x00\x04MQTT\x04\xce\x00\x00' stream = BufferAdapter(data) message = anyio.run(ConnectPacket.from_stream, stream) self.assertIsNot(message.payload.client_id, None)
def test_decode_fail_reserved_flag(self): data = b'\x10\x3e\x00\x04MQTT\x04\xcf\x00\x00\x00\x0a0123456789\x00\x09WillTopic\x00\x0bWillMessage\x00\x04user\x00\x08password' stream = BufferAdapter(data) message = anyio.run(ConnectPacket.from_stream, stream) self.assertTrue(message.variable_header.reserved_flag)