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
def test_100(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 e = c.subscribe("foo/bar", qos=0) if e is not None: debug(e) return False # socket MUST be disconnected try: e = c.publish("foo", "bar") sock.getpeername() except socket.error as e: return True debug("connection still alive") return False
def test_007(self): tmp = tempfile.mktemp(prefix='wave-testsuite-') subprocess.Popen("echo \"bar\"|../bin/mkpasswd -c {0} foo".format(tmp), shell=True, stdout=subprocess.PIPE).wait() yield app.set_auth(required= True, filename= tmp) yield auth.switch(tmp) c = MqttClient("auth:{seq}", connect=False, username="******", password="******") ret = c.connect(version=4) # auth rejected if not isinstance(ret, EventConnack) or ret.ret_code == 0: debug(ret) defer.returnValue(False) # updating password subprocess.Popen("echo \"baz\"|../bin/mkpasswd {0} foo".format(tmp), shell=True, stdout=subprocess.PIPE).wait() # file is monitored each 2 secs in debug context time.sleep(3) ret = c.connect(version=4) # auth accepted if not isinstance(ret, EventConnack) or ret.ret_code != 0: debug(ret) defer.returnValue(False) defer.returnValue(True)
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
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
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
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
def test_011(self): c = MqttClient("reg:{seq}", connect=4) e = c.publish("/foo/bar", "plop") # QOS = 0 : no response indented c.disconnect() return (e is None)
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
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
def test_221(self): c = MqttClient("conformity:{seq}", connect=4) ack = c.unsubscribe("foo/bar") if not isinstance(ack, EventUnsuback): debug(ack) return False return True
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
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
def test_010(self): cli = MqttClient("ws:{seq}", port=8884, websocket=True, ssl=True, ssl_opts={'ssl_version': SSL_VERSION}) evt = cli.connect(version=4) if not isinstance(evt, EventConnack): debug(evt) return False cli.disconnect() return True
def test_001(self): sub = MqttClient("sub:{seq}", connect=4) suback_evt = sub.subscribe('foo/bar', qos=1) if not isinstance(suback_evt, EventSuback) or \ suback_evt.mid != sub.get_last_mid() or \ suback_evt.granted_qos[0] != 1: debug('failing event: {0}'.format(suback_evt)) return False return True
def test_222(self): sub = MqttClient("conformity-sub:{seq}", connect=4) ack = sub.unsubscribe_multi(["foo/bar", "bar/baz", "paper/+/scissor"]) if not isinstance(ack, EventUnsuback) or ack.mid != sub.get_last_mid(): debug(ack) return False sub.disconnect() return True
def test_001(self): c = MqttClient("v311:{seq}") evt = c.connect(version=4) if not isinstance(evt, EventConnack) or \ evt.ret_code: debug(evt) return False return True
def test_002(self): yield app.set_auth(required=True) c = MqttClient("auth:{seq}", connect=False) ret = c.connect(version=4) if isinstance(ret, EventConnack) and ret.ret_code == 4: defer.returnValue(True) debug(ret) defer.returnValue(False)
def test_001(self): c = MqttClient("reg:{seq}", ssl=True, ssl_opts={'ssl_version': SSL_VERSION}) evt = c.connect() debug("Using SSL: version={0}, cipher={1}".format(version(c._c.sock), c._c.sock.cipher())) if not isinstance(evt, EventConnack): return False c.disconnect() return True
def test_010(self): c = MqttClient("v311:{seq}", connect=4) evt = c.subscribe("/foo/bar", qos=0) c.disconnect() if not isinstance(evt, EventSuback): debug(evt) return False return True
def test_003(self): tmp = tempfile.mktemp(prefix='wave-testsuite-') yield app.set_auth(required=True, filename=tmp); yield auth.switch(tmp) c = MqttClient("auth:{seq}", connect=False) ret = c.connect(version=4) if isinstance(ret, EventConnack) and ret.ret_code == 4: defer.returnValue(True) debug(ret) defer.returnValue(False)
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
def test_002(self): c = MqttClient("reg:{seq}", ssl=True, ssl_opts={ 'ssl_version': SSL_VERSION, 'cert_reqs': ssl.CERT_REQUIRED }) evt = c.connect() if not isinstance(evt, EventConnack): return True c.disconnect() return False
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
def test_001(self): c = MqttClient("reg:{seq}") evt = c.connect() # [MQTT-3.1.4-4]: CONNACK retcode MUST be 0 # [MQTT-3.2.2-3]: CONNACK session_present IS 0 if not isinstance(evt, EventConnack) or \ evt.ret_code != 0 or \ evt.session_present != 0: return False return True
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
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
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
def test_003(self): c = MqttClient("conformity:{seq}", connect=4) c.publish("/foo/bar/2", "plop", qos=2, retain=True) c.disconnect() # checking message as been store in db store = env.db.hgetall('retain:/foo/bar/2') if store.get('data') != 'plop' or store.get('qos') != '2': debug(store) return False return True
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
def test_260(self): c = MqttClient("conformity:{seq}", connect=4) c.forge( NC.CMD_PUBLISH, 8, [ ('string', '/foo/bar'), # topic ('uint16', 10), # identifier ], send=True) if c.conn_is_alive(): debug("connection still alive") return False return True
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
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
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
def test_003(self): c = MqttClient("reg:{seq}", ssl=True, ssl_opts={ 'ssl_version': SSL_VERSION, 'cert_reqs': ssl.CERT_REQUIRED, 'ca_certs': os.path.join(os.path.dirname(__file__), "../../", "etc/wave_cert.pem") }) evt = c.connect() if not isinstance(evt, EventConnack): return False c.disconnect() return True
def test_005(self): tmp = tempfile.mktemp(prefix='wave-testsuite-') subprocess.Popen("echo \"bar\"|../bin/mkpasswd -c {0} foo".format(tmp), shell=True, stdout=subprocess.PIPE).wait() yield app.set_auth(required=True, filename=tmp) yield auth.switch(tmp) c = MqttClient("auth:{seq}", connect=False, username="******", password="******") ret = c.connect(version=4) # auth rejected if isinstance(ret, EventConnack) and ret.ret_code == 4: defer.returnValue(True) debug(ret) defer.returnValue(False)
def test_020(self): metrics = [ '$SYS/broker/uptime', '$SYS/broker/version', '$SYS/broker/clients/total', '$SYS/broker/clients/connected', '$SYS/broker/clients/disconnected', '$SYS/broker/messages/sent', '$SYS/broker/messages/received', '$SYS/broker/messages/inflight', '$SYS/broker/messages/stored', '$SYS/broker/publish/messages/sent', '$SYS/broker/publish/messages/sent/qos 0', '$SYS/broker/publish/messages/sent/qos 1', '$SYS/broker/publish/messages/sent/qos 2', '$SYS/broker/publish/messages/received', '$SYS/broker/publish/messages/received/qos 0', '$SYS/broker/publish/messages/received/qos 1', '$SYS/broker/publish/messages/received/qos 2', '$SYS/broker/retained messages/count', '$SYS/broker/subscriptions/count', ] c = MqttClient("metrics:{seq}", connect=4) c.subscribe('$SYS/#', qos=0) while True: evt = c.recv() if evt is not None and isinstance(evt, EventPublish): break stats = {evt.msg.topic: evt.msg.payload} while True: evt = c.recv() if evt is None: break if isinstance(evt, EventPingResp): continue stats[evt.msg.topic] = evt.msg.payload #pprint(stats) for topic in metrics: if topic not in stats: debug("{0} metric not in $SYS".format(topic)) return False del stats[topic] if len(stats) > 0: debug("extra $SYS metrics: {0}".format(stats)) c.disconnect() return True
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
def test_107(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', 2), # keepalive ('string', 'ff') # client id ], send=True) evt = c._c.pop_event() if not isinstance(evt, EventConnack) or evt.ret_code != 0: debug(evt) return False time.sleep(1) evt = c.publish("foo", "bar", qos=1) if not isinstance(evt, EventPuback): debug(evt) return False time.sleep(3.5) ret = c._c.loop() if ret != NC.ERR_CONN_LOST: debug("invalid error code: {0}".format(ret)) return False return True
def test_100(self): c = MqttClient("unicode:{seq}", connect=4) topic = u"utf8/Какво е Unicode ?" evt = c.subscribe(topic, qos=0) if not isinstance(evt, EventSuback) or \ evt.mid != c.get_last_mid(): debug(evt) return False c.unsubscribe(topic) c.disconnect() return True
def test_216(self): pub = MqttClient("conformity-pub:{seq}", connect=4) sub = MqttClient("conformity-sub:{seq}", connect=4) ack = sub.subscribe_multi([("foo/bar", 2), ("bar/baz", 0), ("paper/+/scissor", 1)]) if not isinstance(ack, EventSuback) or ack.mid != sub.get_last_mid(): debug(ack) return False # checking granted qos if len(ack.granted_qos) != 3 or \ ack.granted_qos[0] != 2 or \ ack.granted_qos[1] != 0 or \ ack.granted_qos[2] != 1: debug(ack) return False return True
def test_001(self): topic = "$/mqtt/CONNECT" qos = [0, 1, 2] subs = [] for i in range(len(qos)): subs.append(MqttClient("sub{0}:{{seq}}".format(qos[i]), connect=4)) for i in range(len(qos)): subs[i].subscribe(topic, qos[i]) pub = MqttClient("pub:{seq}", connect=4) # qos 0 client for i in range(len(qos)): #print "qos {0}: receiving message".format(qos[i]) e = subs[i].recv() if not isinstance(e, EventPublish): debug( "qos {0}: message received should be EventPublish (is {1})".format(qos[i], e)) return False if e.msg.topic != topic or\ e.msg.qos != 0: #qos[i]: debug("qos {0}: invalid packet received (topic= {1}, qos={2})".format(qos[i], e.msg.topic,\ e.msg.qos)) return False #FUTURE: internal events supporting qos > 0 # if e.msg.qos == 1: # subs[i].puback(e.msg.mid) # elif e.msg.qos == 2: # e2 = subs[i].pubrec(e.msg.mid) # if not isinstance(e2, EventPubrel) or e2.mid != e.msg.mid: # print "qos2 PUBREC response: {0} (should be EventPubrel)".format(e2) # return False # # subs[i].pubcomp(e.msg.mid) #TODO: check subs connectivity return True
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
def test_250(self): tfs = [("#", True), ("/#", True), ("/foo/#", True), ("#/foo", False), ("/#/foo", False), ("/foo#", False), ("/#foo", 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
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
def test_001(self): sub = MqttClient("sub:{seq}", connect=4) sub.subscribe("foo/bar", qos=0) pub = MqttClient("pub:{seq}", connect=4) pub.publish("foo/bar", env.gen_msg(42), qos=2) # PUBREL not sent pub.destroy(); del pub cnt = yield supervisor.count('wave_msgworkers_sup') if cnt != 1: debug("wrong msgworkers count: {0}".format(cnt)) defer.returnValue(False) # msg worker is destroyed after 5 secs time.sleep(6) if (yield supervisor.count('wave_msgworkers_sup')) != 0: debug("wrong msgworkers count") defer.returnValue(False) defer.returnValue(True)
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
def test_010(self): pub = MqttClient("pub:{seq}", connect=4) sub = MqttClient("sub:{seq}", connect=4) sub.subscribe('a/b', 0) # NOTE: remaining length value is message length + 5 bytes (topic encoded) + 2 bytes (msgid) puback_evt = pub.publish('a/b', payload='', qos=1) if not isinstance(puback_evt, EventPuback) or \ puback_evt.mid != pub.get_last_mid(): return False publish_evt = sub.recv() if not isinstance(publish_evt, EventPublish) or \ publish_evt.msg.payloadlen != 0 or \ publish_evt.msg.payload is not None: return False return True
def test_011(self): c = MqttClient("v311:{seq}", connect=4) e = c.publish("/foo/bar", payload="plop") # QOS = 0 : no response indented if e is not None: debug(e) c.disconnect() return False c.disconnect() return True
def test_010(self): c = MqttClient("reg:{seq}", connect=4) evt = c.subscribe("/foo/bar", qos=0) # validating [MQTT-2.3.1-7] if not isinstance(evt, EventSuback) or evt.mid != c.get_last_mid(): return False evt = c.unsubscribe("/foo/bar") # validating [MQTT-2.3.1-7] if not isinstance(evt, EventUnsuback) or evt.mid != c.get_last_mid(): return False c.disconnect() return True
def test_010(self): sub = MqttClient("sub:{seq}", connect=4, clean_session=0) sub.subscribe("foo/+", qos=2) sub.disconnect() if (yield supervisor.count('wave_msgworkers_sup')) != 0: debug("wrong msgworkers count") defer.returnValue(False) pub = MqttClient("pub:{seq}", connect=4) rec = pub.publish("foo/bar", env.gen_msg(42), qos=2) ack = pub.pubrel(rec.mid) print ack if not isinstance(ack, EventPubcomp): debug(ack) defer.returnValue(False) # msg is published to offline storage, msg worker should exit immediately if (yield supervisor.count('wave_msgworkers_sup')) != 0: debug("wrong msgworkers count") defer.returnValue(False) defer.returnValue(True)
def test_004(self): f = open('../log/wave.access.log', 'r') f.seek(0, os.SEEK_END) # here we start # NOTE: invalid publish cause disconnection for qos in (0, 1, 2): c = MqttClient("conformity:{seq}", connect=4) time.sleep(.5) f.readline() # skip CONNECT c.publish("$SYS/bar", "baz", qos=qos) if not _match( f, { 'request_method': 'PUBLISH', 'request_url': '$SYS/bar', 'response_bytes_clf': '3', 'status': '403', 'request_header_user_agent': c.client_id }): return False return True
def test_010(self): """ > exometer:get_value([wave,sessions]). {ok,[{active,0},{offline,0}]} """ v_start = yield exometer.value('wave.sessions') c = MqttClient("metrics:{seq}", connect=4, clean_session=0) v = yield exometer.value('wave.sessions') if v['active'] != v_start['active'] + 1 or v['offline'] != v_start[ 'offline']: debug("{0}, {1}".format(v, v_start)) defer.returnValue(False) c.disconnect() time.sleep(.5) v = yield exometer.value('wave.sessions') if v['active'] != v_start['active'] or v[ 'offline'] != v_start['offline'] + 1: debug("{0}, {1}".format(v, v_start)) defer.returnValue(False) defer.returnValue(True)
def test_101(self): c = MqttClient("conformity:{seq}") evt = c.connect(version=4) if not isinstance(evt, EventConnack) or \ evt.ret_code: debug(e) return False # 2d connect pkt pkt = MqttPkt() pkt.connect_build(c._c, keepalive=60, clean_session=1, version=4) c._c.packet_queue(pkt) c._c.packet_write() c._c.loop() try: c.publish("foo", "bar") c._c.sock.getpeername() except socket.error as e: return True debug("connection still alive") return False
def test_110(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 clientid = "é!;~«ä" #clientid = clientid.decode('utf8') pkt = MqttPkt() pkt.command = NC.CMD_CONNECT pkt.remaining_length = 12 + len(clientid) # client_id 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(clientid) # client id - 6 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
def test_011(self): c = MqttClient("conformity:{seq}", connect=4) # initial state c.publish("/retain/no/2", 'waze', retain=True) if env.db.keys('retain:/retain/no/*') != ['retain:/retain/no/2']: debug(env.db.keys('retain:/retain/no/*')) return False # not stored c.publish('/retain/no/1', 'whaa', retain=False) if env.db.keys('retain:/retain/no/*') != ['retain:/retain/no/2']: debug(env.db.keys('retain:/retain/no/*')) return False # no replace or remove c.publish('/retain/no/2', 'whaa', retain=False) store = env.db.hgetall("retain:/retain/no/2") if store != {'data': 'waze', 'qos': '0'}: debug(store) return False c.disconnect() return True
def test_222(self): sub = MqttClient("conformity-sub:{seq}", connect=4) ack = sub.unsubscribe_multi(["foo/bar", "bar/baz", "paper/+/scissor"]) if not isinstance(ack, EventUnsuback) or ack.mid != sub.get_last_mid(): debug(ack) return False sub.disconnect() return True
def test_013(self): pub = MqttClient("pub:{seq}", connect=4) sub = MqttClient("sub:{seq}", connect=4) sub.subscribe('a/b', 0) # NOTE: remaining length value is message length + 5 bytes (topic encoded) + 2 bytes (msgid) msg = gen_msg(17000) #print "msg=", msg, len(msg) puback_evt = pub.publish('a/b', msg, qos=1) if not isinstance(puback_evt, EventPuback) or \ puback_evt.mid != pub.get_last_mid(): return False publish_evt = sub.recv() if not isinstance(publish_evt, EventPublish) or \ publish_evt.msg.payloadlen != len(msg) or \ publish_evt.msg.payload != msg: return False return True
def test_010(self): c = MqttClient("conformity:{seq}", connect=4) c.publish("/retain/delete", 'waze', qos=0, retain=True) store = env.db.hgetall("retain:/retain/delete") if store != {'data': 'waze', 'qos': '0'}: debug(store) return False # deleting message c.publish("/retain/delete", '', retain=True) retain = env.db.keys('retain:/retain/delete') if retain != []: debug("retain= {0}".format(retain)) return False c.disconnect() return True
def test_003(self): c = MqttClient("conformity:{seq}", connect=4) c.publish("/foo/bar/2", "plop", qos=2, retain=True) c.disconnect() # checking message as been store in db store = env.db.hgetall('retain:/foo/bar/2') if store.get('data') != 'plop' or store.get('qos') != '2': debug(store) return False return True