Exemple #1
0
    def test_008(self):
        client  = MqttClient("rabbit:{seq}", raw_connect=True)
        client.forge(NC.CMD_CONNECT, 0, [
            ('string', 'MQTT'),
            ('byte'  , 4),         # protocol level
            ('byte'  , 28),        # will=1, will-qos=3
            ('uint16', 60),        # keepalive
        ], send=True)
        if client.conn_is_alive():
            debug("connection still alive")
            return False

        client  = MqttClient("rabbit:{seq}", raw_connect=True)
        client.forge(NC.CMD_CONNECT, 0, [
            ('string', 'MQTT'),
            ('byte'  , 4),         # protocol level
            ('byte'  , 12),        # will=1, will-qos=1
            ('uint16', 60),        # keepalive
            ('string', client._c.client_id),   # clientid
            ('string', '/foo/bar'),# will topic
            ('uint16', 0),         # will payload len
            ('bytes' , ''),        # will payload
        ], send=True)

        evt = client.recv()
        if not isinstance(evt, EventConnack):
            debug(evt)
            return False

        client.disconnect()
        return True
Exemple #2
0
    def test_103(self):
        c = MqttClient("conformity:{seq}")
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock.connect(('127.0.0.1', 1883))
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
            sock.setblocking(0)
        except Exception as e:
            debug(e)
            return False

        c._c.sock = sock
        c.forge(
            NC.CMD_CONNECT,
            0,
            [
                ('string', 'MQTT'),
                ('byte', NC.PROTOCOL_VERSION_3),
                ('byte', 0),  # flags
                ('uint16', 60),  # keepalive
                ('string', 'ff')  # client id
            ],
            send=True)  # should return CONN_REFUSED

        evt = c._c.pop_event()
        if not isinstance(evt, EventConnack) or evt.ret_code != 1:
            debug(evt)
            return False

        ret = c._c.loop()
        if ret != NC.ERR_CONN_LOST:
            debug("invalid error code: {0}".format(ret))
            return False

        return True
Exemple #3
0
    def test_021(self):
        """
            throwing "anonymous" exception on binary pattern matching
            (mqtt_msg:decode_connect2())
        """
        client  = MqttClient("rabbit:{seq}", raw_connect=True)
        client.forge(NC.CMD_CONNECT, 0, [
            ('string', 'MQTT'),
            ('byte'  , 4),         # protocol level
            ('byte'  , 4),         # will=1
            ('uint16', 60),        # keepalive
            ('string', client._c.client_id),
            ('string', '/will/topic'), # will-topic
        ], send=True)
        if client.conn_is_alive():
            debug("connection still alive")
            return False

        client  = MqttClient("rabbit:{seq}", raw_connect=True)
        client.forge(NC.CMD_CONNECT, 0, [
            ('string', 'MQTT'),
            ('byte'  , 4),         # protocol level
            ('byte'  , 4),         # will=1
            ('uint16', 60),        # keepalive
            ('string', client._c.client_id),
            ('string', '/will/topic'), # will-topic
            ('uint16', 4),         # 4 bytes msg, BUT not message following
        ], send=True)
        if client.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Exemple #4
0
    def test_108(self):
        c = MqttClient("conformity:{seq}")
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock.connect(('127.0.0.1', 1883))
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
            sock.setblocking(0)
        except Exception as e:
            debug(e)
            return False

        c._c.sock = sock
        c.forge(NC.CMD_CONNECT, 0, [
            ('string', 'MQTT'),
            ('byte'  , NC.PROTOCOL_VERSION_4),
            ('byte'  , 0),    # flags
            ('uint16', 10),   # keepalive
            ('string', '')    # client id
        ], send=True)

        evt = c._c.pop_event()
        if not isinstance(evt, EventConnack) or evt.ret_code != 2:
            debug(evt); return False

        return True
Exemple #5
0
    def test_112(self):
        ## PINGREG
        c = MqttClient("conformity:{seq}", raw_connect=True)
        evt = c.connect(version=4)

        # flags shoud be 0
        c.forge(NC.CMD_PINGREQ, 4, [], send=True)
        if c.conn_is_alive():
            debug("connection still alive")
            return False

        ## SUBSCRIBE
        c = MqttClient("conformity2:{seq}", raw_connect=True)
        evt = c.connect(version=4)

        # flags shoud be 2
        c.forge(NC.CMD_SUBSCRIBE, 3, [
            ('uint16', 42),         # identifier
            ('string', '/foo/bar'), # topic filter
            ('byte'  , 0)           # qos
        ], send=True)
        if c.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Exemple #6
0
    def test_108(self):
        c = MqttClient("conformity:{seq}")
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock.connect(('127.0.0.1', 1883))
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
            sock.setblocking(0)
        except Exception as e:
            debug(e)
            return False

        c._c.sock = sock
        c.forge(
            NC.CMD_CONNECT,
            0,
            [
                ('string', 'MQTT'),
                ('byte', NC.PROTOCOL_VERSION_4),
                ('byte', 0),  # flags
                ('uint16', 10),  # keepalive
                ('string', '')  # client id
            ],
            send=True)

        evt = c._c.pop_event()
        if not isinstance(evt, EventConnack) or evt.ret_code != 2:
            debug(evt)
            return False

        return True
Exemple #7
0
    def test_103(self):
        c = MqttClient("conformity:{seq}")
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock.connect(('127.0.0.1', 1883))
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
            sock.setblocking(0)
        except Exception as e:
            debug(e)
            return False

        c._c.sock = sock
        c.forge(NC.CMD_CONNECT, 0, [
            ('string', 'MQTT'),
            ('byte'  , NC.PROTOCOL_VERSION_3),
            ('byte'  , 0),   # flags
            ('uint16', 60),  # keepalive
            ('string', 'ff') # client id
        ], send=True) # should return CONN_REFUSED

        evt = c._c.pop_event()
        if not isinstance(evt, EventConnack) or evt.ret_code != 1:
            debug(evt)
            return False

        ret = c._c.loop()
        if ret != NC.ERR_CONN_LOST:
            debug("invalid error code: {0}".format(ret))
            return False

        return True
Exemple #8
0
    def test_102(self):
        c = MqttClient("conformity:{seq}")
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock.connect(('127.0.0.1', 1883))
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
            sock.setblocking(0)
        except Exception as e:
            debug(e)
            return False

        c._c.sock = sock
        c.forge(NC.CMD_CONNECT, 0, [
            ('string', 'MqTt'),
            ('byte'  , NC.PROTOCOL_VERSION_4),
            ('string', 'ff') # client id
        ], send=True)

        try:
            c.send_pingreq()
            c._c.sock.getpeername()
        except socket.error as e:
            return True

        debug("connection still alive")
        return False
Exemple #9
0
    def test_203(self):
        pub = MqttClient("conformity-pub:{seq}", connect=4)
        sub = MqttClient("conformity-sub:{seq}", connect=4)

        sub.subscribe("foo/bar", qos=2)
        pub.publish("foo/bar", "wootwoot", qos=2, read_response=False)

        # PUB PUBREC
        evt = pub.recv()
        pub.pubrel(pub.get_last_mid(), read_response=False)

        # subscr: receiving PUBLISH
        evt = sub.recv()
        sub.pubrec(evt.msg.mid, read_response=False)

        # subscr: receiving PUBREL
        evt = sub.recv()

        # sending PUBCOMP with wrong pktid
        sub.forge(NC.CMD_PUBCOMP, 0, [
            ('uint16', (evt.mid+10)%65535) # wrong pktid
        ], send=True)


        evt = pub.recv()
        # publisher: PUBCOMP never received
        if evt is not None:
            debug(evt)
            return False

        pub.disconnect(); sub.disconnect()
        return True
Exemple #10
0
    def test_202(self):
        pub = MqttClient("conformity-pub:{seq}", connect=4)
        sub = MqttClient("conformity-sub:{seq}", connect=4)

        sub.subscribe("foo/bar", qos=2)
        pub.publish("foo/bar", "wootwoot", qos=2, read_response=False)

        # PUB PUBREC
        evt = pub.recv()
        # sending PUBREL with wrong pktid
        pub.forge(NC.CMD_PUBREL, 2, [
            ('uint16', (evt.mid+10)%65535) # wrong pktid
        ], send=True)

        # subscriber: PUBLISH never received
        evt = sub.recv()
        if evt is not None:
            debug(evt)
            return False

        evt = pub.recv()
        # publisher: PUBCOMP never received
        if evt is not None:
            debug(evt)
            return False

        pub.disconnect(); sub.disconnect()
        return True
Exemple #11
0
    def test_203(self):
        pub = MqttClient("conformity-pub:{seq}", connect=4)
        sub = MqttClient("conformity-sub:{seq}", connect=4)

        sub.subscribe("foo/bar", qos=2)
        pub.publish("foo/bar", "wootwoot", qos=2, read_response=False)

        # PUB PUBREC
        evt = pub.recv()
        pub.pubrel(pub.get_last_mid(), read_response=False)

        # subscr: receiving PUBLISH
        evt = sub.recv()
        sub.pubrec(evt.msg.mid, read_response=False)

        # subscr: receiving PUBREL
        evt = sub.recv()

        # sending PUBCOMP with wrong pktid
        sub.forge(
            NC.CMD_PUBCOMP,
            0,
            [('uint16', (evt.mid + 10) % 65535)  # wrong pktid
             ],
            send=True)

        evt = pub.recv()
        # publisher: PUBCOMP never received
        if evt is not None:
            debug(evt)
            return False

        pub.disconnect()
        sub.disconnect()
        return True
Exemple #12
0
    def test_112(self):
        ## PINGREG
        c = MqttClient("conformity:{seq}", raw_connect=True)
        evt = c.connect(version=4)

        # flags shoud be 0
        c.forge(NC.CMD_PINGREQ, 4, [], send=True)
        if c.conn_is_alive():
            debug("connection still alive")
            return False

        ## SUBSCRIBE
        c = MqttClient("conformity2:{seq}", raw_connect=True)
        evt = c.connect(version=4)

        # flags shoud be 2
        c.forge(
            NC.CMD_SUBSCRIBE,
            3,
            [
                ('uint16', 42),  # identifier
                ('string', '/foo/bar'),  # topic filter
                ('byte', 0)  # qos
            ],
            send=True)
        if c.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Exemple #13
0
    def test_200(self):
        pub = MqttClient("conformity-pub:{seq}", connect=4)
        sub = MqttClient("conformity-sub:{seq}", connect=4)

        sub.subscribe("foo/bar", qos=1)
        pub.publish("foo/bar", "wootwoot", qos=1)

        # reading PUBLISH
        evt = sub.recv()

        # sending PUBACK with wrong pktid
        sub.forge(
            NC.CMD_PUBACK,
            0,
            [('uint16', (evt.msg.mid + 10) % 65535)  # wrong pktid
             ],
            send=True)

        evt = pub.recv()
        # PUBACK from server is never received
        if evt is not None:
            debug(evt)
            return False

        pub.disconnect()
        sub.disconnect()
        return True
Exemple #14
0
    def test_102(self):
        c = MqttClient("conformity:{seq}")
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock.connect(('127.0.0.1', 1883))
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
            sock.setblocking(0)
        except Exception as e:
            debug(e)
            return False

        c._c.sock = sock
        c.forge(
            NC.CMD_CONNECT,
            0,
            [
                ('string', 'MqTt'),
                ('byte', NC.PROTOCOL_VERSION_4),
                ('string', 'ff')  # client id
            ],
            send=True)

        try:
            c.send_pingreq()
            c._c.sock.getpeername()
        except socket.error as e:
            return True

        debug("connection still alive")
        return False
Exemple #15
0
    def test_212(self):
        c = MqttClient("conformity:{seq}", connect=4)

        # qos 1
        c.forge(NC.CMD_PUBLISH, 2, [], send=True)
        #            ('uint16', 0),          # identifier
        #        ], send=True)
        if c.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Exemple #16
0
    def test_220(self):
        c = MqttClient("conformity:{seq}", connect=4)

        c.forge(NC.CMD_UNSUBSCRIBE, 2, [
            ('uint16', 10),         # identifier
            # NOT TOPIC FILTER/QOS
        ], send=True)
        if c.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Exemple #17
0
    def test_212(self):
        c = MqttClient("conformity:{seq}", connect=4)

        # qos 1
        c.forge(NC.CMD_PUBLISH, 2, [], send=True)
#            ('uint16', 0),          # identifier
#        ], send=True)
        if c.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Exemple #18
0
    def test_211(self):
        c = MqttClient("conformity:{seq}", connect=4)

        c.forge(NC.CMD_PUBLISH, 6, [
            ('string', '/foo/bar'), # topic
            ('uint16', 0),          # identifier
        ], send=True)
        if c.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Exemple #19
0
    def test_210(self):
        c = MqttClient("conformity:{seq}", raw_connect=True)

        c.forge(NC.CMD_CONNECT, 0, [
            ('string', 'MQTT'),
            ('byte'  , 4),         # protocol level
            #('byte'  , 128),       # connect flags:  username flag set
            ('byte'  , 0),       # no flags, no ClientId
            ('uint16', 60),        # keepalive
        ], send=True)

        return False
Exemple #20
0
    def test_215(self):
        c = MqttClient("conformity:{seq}", connect=4)
        c.forge(NC.CMD_SUBSCRIBE, 2, [
            ('uint16', 42),         # identifier
            ('string', '/foo/bar'), # topic filter
            ('byte'  , 3)           # qos
        ], send=True)
        if c.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Exemple #21
0
    def test_115(self):
        c = MqttClient("conformity:{seq}", raw_connect=True)
        evt = c.connect(version=4)

        c.forge(NC.CMD_UNSUBSCRIBE, 2, [
            ('uint16', 0),         # identifier
            ('string', '/foo/bar'), # topic filter
        ], send=True)
        if c.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Exemple #22
0
    def test_010(self):
        client  = MqttClient("rabbit:{seq}", raw_connect=True)
        client.forge(NC.CMD_CONNECT, 0, [
            ('string', 'MQTT'),
            ('byte'  , 4),         # protocol level
            ('byte'  , 32),        # will=0, will-retain=1
            ('uint16', 60),        # keepalive
        ], send=True)
        if client.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Exemple #23
0
    def test_211(self):
        c = MqttClient("conformity:{seq}", connect=4)

        c.forge(
            NC.CMD_PUBLISH,
            6,
            [
                ('string', '/foo/bar'),  # topic
                ('uint16', 0),  # identifier
            ],
            send=True)
        if c.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Exemple #24
0
    def test_105(self):
        c = MqttClient("conformity:{seq}")
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock.connect(('127.0.0.1', 1883))
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
            sock.setblocking(0)
        except Exception as e:
            debug(e)
            return False

        c._c.sock = sock
        ret = c.forge(NC.CMD_CONNECT, 0, [
            ('string', 'MQTT'),
            ('byte'  , NC.PROTOCOL_VERSION_4),
            ('byte'  , 1 << 6), # set password flag
            ('uint16', 60),     # keepalive
            ('string', 'ff')    # client id
        ], send=True)

        if ret != NC.ERR_CONN_LOST:
            debug("invalid error code: {0}".format(ret))
            return False

        return True
Exemple #25
0
    def test_105(self):
        c = MqttClient("conformity:{seq}")
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            sock.connect(('127.0.0.1', 1883))
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
            sock.setblocking(0)
        except Exception as e:
            debug(e)
            return False

        c._c.sock = sock
        ret = c.forge(
            NC.CMD_CONNECT,
            0,
            [
                ('string', 'MQTT'),
                ('byte', NC.PROTOCOL_VERSION_4),
                ('byte', 1 << 6),  # set password flag
                ('uint16', 60),  # keepalive
                ('string', 'ff')  # client id
            ],
            send=True)

        if ret != NC.ERR_CONN_LOST:
            debug("invalid error code: {0}".format(ret))
            return False

        return True
Exemple #26
0
    def test_210(self):
        c = MqttClient("conformity:{seq}", raw_connect=True)

        c.forge(
            NC.CMD_CONNECT,
            0,
            [
                ('string', 'MQTT'),
                ('byte', 4),  # protocol level
                #('byte'  , 128),       # connect flags:  username flag set
                ('byte', 0),  # no flags, no ClientId
                ('uint16', 60),  # keepalive
            ],
            send=True)

        return False
Exemple #27
0
    def test_215(self):
        c = MqttClient("conformity:{seq}", connect=4)
        c.forge(
            NC.CMD_SUBSCRIBE,
            2,
            [
                ('uint16', 42),  # identifier
                ('string', '/foo/bar'),  # topic filter
                ('byte', 3)  # qos
            ],
            send=True)
        if c.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Exemple #28
0
    def test_220(self):
        c = MqttClient("conformity:{seq}", connect=4)

        c.forge(
            NC.CMD_UNSUBSCRIBE,
            2,
            [
                ('uint16', 10),  # identifier
                # NOT TOPIC FILTER/QOS
            ],
            send=True)
        if c.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Exemple #29
0
    def test_261(self):
        pub = MqttClient("conformity:{seq}", connect=4)
        sub = MqttClient("test:{seq}", connect=4)
        sub.subscribe("/foo/bar", qos=0)

        pub.forge(
            NC.CMD_PUBLISH,
            2,
            [
                ('string', '/foo/bar'),  # topic
                ('uint16', 42),  # identifier
            ],
            send=True)

        ack = pub.recv()
        if not isinstance(ack, EventPuback):
            debug(ack)
            return False

        # ensure message has been delivered
        evt = sub.recv()
        if not isinstance(evt, EventPublish) or evt.msg.topic != '/foo/bar':
            debug(evt)
            return False

        # sending again same packet (same id) with dup=1
        pub.forge(
            NC.CMD_PUBLISH,
            10,
            [
                ('string', '/foo/bar'),  # topic
                ('uint16', 42),  # identifier
            ],
            send=True)

        ack = pub.recv()
        if not isinstance(ack, EventPuback):
            debug(ack)
            return False

        # ensure message has been delivered
        evt = sub.recv()
        if not isinstance(evt, EventPublish) or evt.msg.topic != '/foo/bar':
            debug(evt)
            return False

        return True
Exemple #30
0
    def test_020(self):
        """
            broker throwing exception (mqtt_msg:decode_string())
        """
        client  = MqttClient("rabbit:{seq}", raw_connect=True)
        client.forge(NC.CMD_CONNECT, 0, [
            ('string', 'MQTT'),
            ('byte'  , 4),         # protocol level
            ('byte'  , 4),         # will=1
            ('uint16', 60),        # keepalive
            ('string', client._c.client_id),
        ], send=True)
        if client.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Exemple #31
0
    def test_262(self):
        pub = MqttClient("conformity:{seq}", connect=4)
        sub = MqttClient("test:{seq}", connect=4)
        sub.subscribe("/foo/bar", qos=1)

        pub.forge(
            NC.CMD_PUBLISH,
            2,
            [
                ('string', '/foo/bar'),  # topic
                ('uint16', 42),  # identifier
            ],
            send=True)

        ack = pub.recv()
        if ack is not None:
            debug(ack)
            return False

        evt = sub.recv()
        if not isinstance(evt, EventPublish) or evt.msg.topic != '/foo/bar':
            debug(evt)
            return False

        ## reemit message with dup=1 (same msgid)
        ## message must be discarded as previous on is still inflight
        pub.forge(
            NC.CMD_PUBLISH,
            2,
            [
                ('string', '/foo/bar'),  # topic
                ('uint16', 42),  # identifier
            ],
            send=True)

        ack = pub.recv()
        if ack is not None:
            debug(ack)
            return False

        evt = sub.recv()
        if evt is not None:
            debug(evt)
            return False

        return True
Exemple #32
0
    def test_010(self):
        client = MqttClient("rabbit:{seq}", raw_connect=True)
        client.forge(
            NC.CMD_CONNECT,
            0,
            [
                ('string', 'MQTT'),
                ('byte', 4),  # protocol level
                ('byte', 32),  # will=0, will-retain=1
                ('uint16', 60),  # keepalive
            ],
            send=True)
        if client.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Exemple #33
0
    def test_115(self):
        c = MqttClient("conformity:{seq}", raw_connect=True)
        evt = c.connect(version=4)

        c.forge(
            NC.CMD_UNSUBSCRIBE,
            2,
            [
                ('uint16', 0),  # identifier
                ('string', '/foo/bar'),  # topic filter
            ],
            send=True)
        if c.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Exemple #34
0
    def test_003(self):
        c = MqttClient("cs:{seq}", raw_connect=True)
        c.forge(
            NC.CMD_CONNECT,
            0,
            [
                ('string', 'MQTT'),
                ('byte', 4),  # protocol level
                ('byte', 2),  # cleansession 1
                ('uint16', 60),  # keepalive
                ('string', ''),  # 0-length client-if
            ],
            send=True)

        ack = c.recv()
        if not isinstance(ack, EventConnack) or\
                ack.ret_code != 0 or\
                ack.session_present != 0:
            debug(ack)
            return False

        c2 = MqttClient("cs:{seq}", raw_connect=True)
        c2.forge(
            NC.CMD_CONNECT,
            0,
            [
                ('string', 'MQTT'),
                ('byte', 4),  # protocol level
                ('byte', 2),  # cleansession 1
                ('uint16', 60),  # keepalive
                ('string', ''),  # 0-length client-if
            ],
            send=True)

        ack = c2.recv()
        if not isinstance(ack, EventConnack) or\
                ack.ret_code != 0 or\
                ack.session_present != 0:
            debug(ack)
            return False

        return True
Exemple #35
0
    def test_021(self):
        """
            throwing "anonymous" exception on binary pattern matching
            (mqtt_msg:decode_connect2())
        """
        client = MqttClient("rabbit:{seq}", raw_connect=True)
        client.forge(
            NC.CMD_CONNECT,
            0,
            [
                ('string', 'MQTT'),
                ('byte', 4),  # protocol level
                ('byte', 4),  # will=1
                ('uint16', 60),  # keepalive
                ('string', client._c.client_id),
                ('string', '/will/topic'),  # will-topic
            ],
            send=True)
        if client.conn_is_alive():
            debug("connection still alive")
            return False

        client = MqttClient("rabbit:{seq}", raw_connect=True)
        client.forge(
            NC.CMD_CONNECT,
            0,
            [
                ('string', 'MQTT'),
                ('byte', 4),  # protocol level
                ('byte', 4),  # will=1
                ('uint16', 60),  # keepalive
                ('string', client._c.client_id),
                ('string', '/will/topic'),  # will-topic
                ('uint16', 4),  # 4 bytes msg, BUT not message following
            ],
            send=True)
        if client.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Exemple #36
0
    def test_020(self):
        """
            broker throwing exception (mqtt_msg:decode_string())
        """
        client = MqttClient("rabbit:{seq}", raw_connect=True)
        client.forge(
            NC.CMD_CONNECT,
            0,
            [
                ('string', 'MQTT'),
                ('byte', 4),  # protocol level
                ('byte', 4),  # will=1
                ('uint16', 60),  # keepalive
                ('string', client._c.client_id),
            ],
            send=True)
        if client.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Exemple #37
0
    def test_261(self):
        pub = MqttClient("conformity:{seq}", connect=4)
        sub = MqttClient("test:{seq}", connect=4)
        sub.subscribe("/foo/bar", qos=0)

        pub.forge(NC.CMD_PUBLISH, 2, [
            ('string', '/foo/bar'), # topic
            ('uint16', 42),         # identifier
        ], send=True)

        ack = pub.recv()
        if not isinstance(ack, EventPuback):
            debug(ack)
            return False

        # ensure message has been delivered
        evt = sub.recv()
        if not isinstance(evt, EventPublish) or evt.msg.topic != '/foo/bar':
            debug(evt)
            return False

        # sending again same packet (same id) with dup=1
        pub.forge(NC.CMD_PUBLISH, 10, [
            ('string', '/foo/bar'), # topic
            ('uint16', 42),         # identifier
        ], send=True)

        ack = pub.recv()
        if not isinstance(ack, EventPuback):
            debug(ack)
            return False

        # ensure message has been delivered
        evt = sub.recv()
        if not isinstance(evt, EventPublish) or evt.msg.topic != '/foo/bar':
            debug(evt)
            return False

        return True
Exemple #38
0
    def test_008(self):
        client = MqttClient("rabbit:{seq}", raw_connect=True)
        client.forge(
            NC.CMD_CONNECT,
            0,
            [
                ('string', 'MQTT'),
                ('byte', 4),  # protocol level
                ('byte', 28),  # will=1, will-qos=3
                ('uint16', 60),  # keepalive
            ],
            send=True)
        if client.conn_is_alive():
            debug("connection still alive")
            return False

        client = MqttClient("rabbit:{seq}", raw_connect=True)
        client.forge(
            NC.CMD_CONNECT,
            0,
            [
                ('string', 'MQTT'),
                ('byte', 4),  # protocol level
                ('byte', 12),  # will=1, will-qos=1
                ('uint16', 60),  # keepalive
                ('string', client._c.client_id),  # clientid
                ('string', '/foo/bar'),  # will topic
                ('uint16', 0),  # will payload len
                ('bytes', ''),  # will payload
            ],
            send=True)

        evt = client.recv()
        if not isinstance(evt, EventConnack):
            debug(evt)
            return False

        client.disconnect()
        return True
Exemple #39
0
    def test_262(self):
        pub = MqttClient("conformity:{seq}", connect=4)
        sub = MqttClient("test:{seq}", connect=4)
        sub.subscribe("/foo/bar", qos=1)

        pub.forge(NC.CMD_PUBLISH, 2, [
            ('string', '/foo/bar'), # topic
            ('uint16', 42),         # identifier
        ], send=True)

        ack = pub.recv()
        if ack is not None:
            debug(ack)
            return False

        evt = sub.recv()
        if not isinstance(evt, EventPublish) or evt.msg.topic != '/foo/bar':
            debug(evt)
            return False

        ## reemit message with dup=1 (same msgid)
        ## message must be discarded as previous on is still inflight
        pub.forge(NC.CMD_PUBLISH, 2, [
            ('string', '/foo/bar'), # topic
            ('uint16', 42),         # identifier
        ], send=True)

        ack = pub.recv()
        if ack is not None:
            debug(ack)
            return False

        evt = sub.recv()
        if evt is not None:
            debug(evt)
            return False

        return True
Exemple #40
0
    def test_200(self):
        pub = MqttClient("conformity-pub:{seq}", connect=4)
        sub = MqttClient("conformity-sub:{seq}", connect=4)

        sub.subscribe("foo/bar", qos=1)
        pub.publish("foo/bar", "wootwoot", qos=1)

        # reading PUBLISH
        evt = sub.recv()

        # sending PUBACK with wrong pktid
        sub.forge(NC.CMD_PUBACK, 0, [
            ('uint16', (evt.msg.mid+10)%65535) # wrong pktid
        ], send=True)

        evt = pub.recv()
        # PUBACK from server is never received
        if evt is not None:
            debug(evt)
            return False

        pub.disconnect(); sub.disconnect()
        return True
Exemple #41
0
    def test_004(self):
        monitor = MqttClient("monitor:{seq}", connect=4)
        # NOTE: '/' prefix skips $ messages
        # TODO: remove it when '$' filter will be impl.
        monitor.subscribe("/#", qos=0)

        client  = MqttClient("rabbit:{seq}") # no keepalive
        will    = {'topic': '/node/disconnect', 'message': client.clientid()}
        client.connect(version=4, will=will)

        # protocol errlr flags shoud be 0
        client.forge(NC.CMD_PINGREQ, 4, [], send=True)
        if client.conn_is_alive():
            debug("connection still alive")
            return False

        evt = monitor.recv()
        if not isinstance(evt, EventPublish) or evt.msg.topic != will['topic'] or \
                evt.msg.payload != will['message']:
            debug(evt)
            return False

        monitor.disconnect()
        return True
Exemple #42
0
    def test_004(self):
        monitor = MqttClient("monitor:{seq}", connect=4)
        # NOTE: '/' prefix skips $ messages
        # TODO: remove it when '$' filter will be impl.
        monitor.subscribe("/#", qos=0)

        client = MqttClient("rabbit:{seq}")  # no keepalive
        will = {'topic': '/node/disconnect', 'message': client.clientid()}
        client.connect(version=4, will=will)

        # protocol errlr flags shoud be 0
        client.forge(NC.CMD_PINGREQ, 4, [], send=True)
        if client.conn_is_alive():
            debug("connection still alive")
            return False

        evt = monitor.recv()
        if not isinstance(evt, EventPublish) or evt.msg.topic != will['topic'] or \
                evt.msg.payload != will['message']:
            debug(evt)
            return False

        monitor.disconnect()
        return True