Ejemplo n.º 1
0
    def test_251(self):
        tfs = [
            ("+"         , True),
            ("/+"        , True),
            ("+/"        , True),
            ("+/foo"     , True),
            ("/foo/+"    , True),
            ("/foo/+/"   , True),
            ("/foo/+/bar", True),
            ("+/foo/bar" , True),
            ("foo+"      , False),
            ("foo+/bar"  , False),
            ("+foo/bar"  , False),
            ("foo/+bar"  , False),
            # ~
            ("++"        , False),
            ("foo/++/bar", False),
        ]

        for (tf, isvalid) in tfs:
            sub = MqttClient("conformity:{seq}", connect=4)
            sub.subscribe(tf, qos=0, read_response=False)
            ack = sub.recv()

            if (isvalid and not isinstance(ack, EventSuback)) or \
                    (not isvalid and (ack is not None or sub.conn_is_alive())):
                debug("{0}: {1} ({2})".format(tf, ack, sub.conn_is_alive()))
                return False

            sub.disconnect()

        return True
Ejemplo n.º 2
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
Ejemplo n.º 3
0
    def test_251(self):
        tfs = [
            ("+", True),
            ("/+", True),
            ("+/", True),
            ("+/foo", True),
            ("/foo/+", True),
            ("/foo/+/", True),
            ("/foo/+/bar", True),
            ("+/foo/bar", True),
            ("foo+", False),
            ("foo+/bar", False),
            ("+foo/bar", False),
            ("foo/+bar", False),
            # ~
            ("++", False),
            ("foo/++/bar", False),
        ]

        for (tf, isvalid) in tfs:
            sub = MqttClient("conformity:{seq}", connect=4)
            sub.subscribe(tf, qos=0, read_response=False)
            ack = sub.recv()

            if (isvalid and not isinstance(ack, EventSuback)) or \
                    (not isvalid and (ack is not None or sub.conn_is_alive())):
                debug("{0}: {1} ({2})".format(tf, ack, sub.conn_is_alive()))
                return False

            sub.disconnect()

        return True
Ejemplo n.º 4
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
Ejemplo n.º 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
Ejemplo n.º 6
0
    def test_213(self):
        c = MqttClient("conformity:{seq}", connect=4)
        c.publish("foo/+/bar", "", qos=0)
        if c.conn_is_alive():
            debug("connection still alive")
            return False

        c = MqttClient("conformity:{seq}", connect=4)
        c.publish("foo/#/bar", "", qos=0)
        if c.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Ejemplo n.º 7
0
    def test_213(self):
        c = MqttClient("conformity:{seq}", connect=4)
        c.publish("foo/+/bar", "", qos=0)
        if c.conn_is_alive():
            debug("connection still alive")
            return False

        c = MqttClient("conformity:{seq}", connect=4)
        c.publish("foo/#/bar", "", qos=0)
        if c.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Ejemplo n.º 8
0
    def test_109(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

        pkt = MqttPkt()
        pkt.command = NC.CMD_CONNECT
        pkt.remaining_length = 12 + 26  # client_id = "ff"
        pkt.alloc()

        pkt.write_string("MQTT")
        pkt.write_byte(NC.PROTOCOL_VERSION_4)
        pkt.write_byte(0)  # flags
        pkt.write_uint16(10)  # keepalive
        pkt.write_string("ABCDEFGHIJKLMNOPQRSTUVWXYZ")  # client id - 26 chars

        c._c.packet_queue(pkt)
        c._c.packet_write()
        c._c.loop()

        if c.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Ejemplo n.º 9
0
    def test_109(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

        pkt = MqttPkt()
        pkt.command = NC.CMD_CONNECT
        pkt.remaining_length = 12 + 26 # client_id = "ff"
        pkt.alloc()

        pkt.write_string("MQTT")
        pkt.write_byte(NC.PROTOCOL_VERSION_4)
        pkt.write_byte(0)      # flags
        pkt.write_uint16(10)   # keepalive
        pkt.write_string("ABCDEFGHIJKLMNOPQRSTUVWXYZ") # client id - 26 chars

        c._c.packet_queue(pkt)
        c._c.packet_write()
        c._c.loop()

        if c.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Ejemplo n.º 10
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
Ejemplo n.º 11
0
    def test_270(self):
        pub = MqttClient("luser:{seq}", connect=4)
        pub.publish("$foo/bar", "test1")

        if pub.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Ejemplo n.º 12
0
    def test_223(self):
        c = MqttClient("conformity-sub:{seq}", connect=4)
        c.disconnect()

        if c.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Ejemplo n.º 13
0
    def test_270(self):
        pub = MqttClient("luser:{seq}", connect=4)
        pub.publish("$foo/bar", "test1")

        if pub.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Ejemplo n.º 14
0
    def test_223(self):
        c = MqttClient("conformity-sub:{seq}", connect=4)
        c.disconnect()

        if c.conn_is_alive():
            debug("connection still alive")
            return False

        return True
Ejemplo n.º 15
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
Ejemplo n.º 16
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
Ejemplo n.º 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
Ejemplo n.º 18
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
Ejemplo n.º 19
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
Ejemplo n.º 20
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
Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 23
0
    def test_255(self):
        c = MqttClient("conformity:{seq}", connect=4)
        c.subscribe("", qos=0)

        if c.conn_is_alive():
            debug("connection still alive")
            return False

        c = MqttClient("conformity:{seq}", connect=4)
        c.unsubscribe("")

        if c.conn_is_alive():
            debug("connection still alive")
            return False

        c = MqttClient("conformity:{seq}", connect=4)
        c.publish("", "", qos=0)

        if c.conn_is_alive():
            debug("connection stil alive")
            return False

        return True
Ejemplo n.º 24
0
    def test_255(self):
        c = MqttClient("conformity:{seq}", connect=4)
        c.subscribe("", qos=0)

        if c.conn_is_alive():
            debug("connection still alive")
            return False

        c = MqttClient("conformity:{seq}", connect=4)
        c.unsubscribe("")

        if c.conn_is_alive():
            debug("connection still alive")
            return False

        c = MqttClient("conformity:{seq}", connect=4)
        c.publish("", "", qos=0)

        if c.conn_is_alive():
            debug("connection stil alive")
            return False

        return True
Ejemplo n.º 25
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
Ejemplo n.º 26
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
Ejemplo n.º 27
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
Ejemplo n.º 28
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
Ejemplo n.º 29
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
Ejemplo n.º 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
Ejemplo n.º 31
0
    def test_101(self):
        for topic in (u"utf8: \u0000 đ", u"utf8: \u001b đ", u"utf8: \u0081 đ",
                      u"utf8: \u0093 đ", u"utf8: \ud800 đ", u"utf8: \ud8a4 đ",
                      u"utf8: \ud8ff đ"):

            c = MqttClient("unicode:{seq}", connect=4)

            evt = c.subscribe(topic, qos=0)
            if not evt is None:
                debug(evt)
                return False

            # check connection is closed
            if c.conn_is_alive():
                debug("connection still alive")
                return False

        return True
Ejemplo n.º 32
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
Ejemplo n.º 33
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
Ejemplo n.º 34
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
Ejemplo n.º 35
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