Simple connect test for v5.

This commit is contained in:
Roger A. Light 2018-10-25 14:43:43 +01:00
parent b6c667689d
commit 52c25fa899
4 changed files with 54 additions and 5 deletions

View File

@ -0,0 +1,34 @@
#!/usr/bin/env python
# Test whether a valid CONNECT results in the correct CONNACK packet for MQTT v5.
import inspect, os, sys
# From http://stackoverflow.com/questions/279237/python-import-a-module-from-a-folder
cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"..")))
if cmd_subfolder not in sys.path:
sys.path.insert(0, cmd_subfolder)
import mosq_test
rc = 1
keepalive = 10
connect_packet = mosq_test.gen_connect("connect-success-test", proto_ver=5, keepalive=keepalive)
connack_packet = mosq_test.gen_connack(rc=0, proto_ver=5)
port = mosq_test.get_port()
broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port)
try:
sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=port)
sock.close()
rc = 0
finally:
broker.terminate()
broker.wait()
(stdo, stde) = broker.communicate()
if rc:
print(stde)
exit(rc)

View File

@ -19,6 +19,7 @@ test : test-compile 01 02 03 04 05 06 07 08 09 10 11
01 :
./01-connect-success.py
./01-connect-success-v5.py
./01-connect-invalid-protonum.py
./01-connect-invalid-id-0.py
./01-connect-invalid-id-0-311.py

View File

@ -8,6 +8,7 @@ max_running = 10
tests = [
#(ports required, 'path'),
(1, './01-connect-success.py'),
(1, './01-connect-success-v5.py'),
(1, './01-connect-invalid-protonum.py'),
(1, './01-connect-invalid-id-0.py'),
(1, './01-connect-invalid-id-0-311.py'),

View File

@ -72,6 +72,8 @@ def packet_matches(name, recvd, expected):
print("Received: "+to_string(recvd))
except struct.error:
print("Received (not decoded, len=%d): %s" % (len(recvd), recvd))
for i in range(0, len(recvd)):
print('%c'%(recvd[i]),)
try:
print("Expected: "+to_string(expected))
except struct.error:
@ -292,10 +294,10 @@ def to_string(packet):
# Reserved
return "0xF0"
def gen_connect(client_id, clean_session=True, keepalive=60, username=None, password=None, will_topic=None, will_qos=0, will_retain=False, will_payload="", proto_ver=4, connect_reserved=False):
def gen_connect(client_id, clean_session=True, keepalive=60, username=None, password=None, will_topic=None, will_qos=0, will_retain=False, will_payload="", proto_ver=4, connect_reserved=False, properties=None):
if (proto_ver&0x7F) == 3 or proto_ver == 0:
remaining_length = 12
elif (proto_ver&0x7F) == 4:
elif (proto_ver&0x7F) == 4 or proto_ver == 5:
remaining_length = 10
else:
raise ValueError
@ -311,6 +313,9 @@ def gen_connect(client_id, clean_session=True, keepalive=60, username=None, pass
if clean_session:
connect_flags = connect_flags | 0x02
if proto_ver == 5:
remaining_length += 1
if will_topic != None:
remaining_length = remaining_length + 2+len(will_topic) + 2+len(will_payload)
connect_flags = connect_flags | 0x04 | ((will_qos&0x03) << 3)
@ -328,9 +333,12 @@ def gen_connect(client_id, clean_session=True, keepalive=60, username=None, pass
packet = struct.pack("!B"+str(len(rl))+"s", 0x10, rl)
if (proto_ver&0x7F) == 3 or proto_ver == 0:
packet = packet + struct.pack("!H6sBBH", len("MQIsdp"), "MQIsdp", proto_ver, connect_flags, keepalive)
elif (proto_ver&0x7F) == 4:
elif (proto_ver&0x7F) == 4 or proto_ver == 5:
packet = packet + struct.pack("!H4sBBH", len("MQTT"), "MQTT", proto_ver, connect_flags, keepalive)
if proto_ver == 5:
packet += struct.pack("B", 0)
if client_id != None:
packet = packet + struct.pack("!H"+str(len(client_id))+"s", len(client_id), client_id)
@ -347,8 +355,13 @@ def gen_connect(client_id, clean_session=True, keepalive=60, username=None, pass
packet = packet + struct.pack("!H"+str(len(password))+"s", len(password), password)
return packet
def gen_connack(resv=0, rc=0):
return struct.pack('!BBBB', 32, 2, resv, rc);
def gen_connack(resv=0, rc=0, proto_ver=4):
if proto_ver == 5:
packet = struct.pack('!BBBBB', 32, 3, resv, rc, 0);
else:
packet = struct.pack('!BBBB', 32, 2, resv, rc);
return packet
def gen_publish(topic, qos, payload=None, retain=False, dup=False, mid=0):
rl = 2+len(topic)