Skip to content

Commit

Permalink
Add tests for subscription identifiers.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Dec 23, 2018
1 parent 7c3666d commit c1baf21
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
68 changes: 68 additions & 0 deletions test/broker/02-subpub-qos0-subscription-id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python

# Do subscription identifiers work as expected?
# MQTT v5

from mosq_test_helper import *

rc = 1
keepalive = 60
connect_packet = mosq_test.gen_connect("subpub-test", keepalive=keepalive, proto_ver=5)
connack_packet = mosq_test.gen_connack(rc=0, proto_ver=5)

mid = 1
props = mqtt5_props.gen_varint_prop(mqtt5_props.PROP_SUBSCRIPTION_IDENTIFIER, 1)
props = mqtt5_props.prop_finalise(props)
subscribe1_packet = mosq_test.gen_subscribe(mid, "subpub/id1", 0, proto_ver=5, properties=props)
suback1_packet = mosq_test.gen_suback(mid, 0, proto_ver=5)

mid = 2
props = mqtt5_props.gen_varint_prop(mqtt5_props.PROP_SUBSCRIPTION_IDENTIFIER, 14)
props = mqtt5_props.prop_finalise(props)
subscribe2_packet = mosq_test.gen_subscribe(mid, "subpub/+/id2", 0, proto_ver=5, properties=props)
suback2_packet = mosq_test.gen_suback(mid, 0, proto_ver=5)

mid = 3
subscribe3_packet = mosq_test.gen_subscribe(mid, "subpub/noid", 0, proto_ver=5)
suback3_packet = mosq_test.gen_suback(mid, 0, proto_ver=5)

publish1_packet = mosq_test.gen_publish("subpub/id1", qos=0, payload="message1", proto_ver=5)

props = mqtt5_props.gen_varint_prop(mqtt5_props.PROP_SUBSCRIPTION_IDENTIFIER, 1)
props = mqtt5_props.prop_finalise(props)
publish1r_packet = mosq_test.gen_publish("subpub/id1", qos=0, payload="message1", proto_ver=5, properties=props)

publish2_packet = mosq_test.gen_publish("subpub/test/id2", qos=0, payload="message2", proto_ver=5)
props = mqtt5_props.gen_varint_prop(mqtt5_props.PROP_SUBSCRIPTION_IDENTIFIER, 14)
props = mqtt5_props.prop_finalise(props)
publish2r_packet = mosq_test.gen_publish("subpub/test/id2", qos=0, payload="message2", proto_ver=5, properties=props)

publish3_packet = mosq_test.gen_publish("subpub/noid", qos=0, payload="message3", 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, timeout=20, port=port)

mosq_test.do_send_receive(sock, subscribe1_packet, suback1_packet, "suback1")
mosq_test.do_send_receive(sock, subscribe2_packet, suback2_packet, "suback2")
mosq_test.do_send_receive(sock, subscribe3_packet, suback3_packet, "suback3")

mosq_test.do_send_receive(sock, publish3_packet, publish3_packet, "publish3")
mosq_test.do_send_receive(sock, publish2_packet, publish2r_packet, "publish2")
mosq_test.do_send_receive(sock, publish1_packet, publish1r_packet, "publish1")

rc = 0

sock.close()
finally:
broker.terminate()
broker.wait()
(stdo, stde) = broker.communicate()
if rc:
print(stde)

exit(rc)

1 change: 1 addition & 0 deletions test/broker/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ endif
./02-subpub-qos1-nolocal.py
./02-subpub-qos0-retain-as-publish.py
./02-subpub-qos0-send-retain.py
./02-subpub-qos0-subscription-id.py
./02-unsubscribe-qos0.py
./02-unsubscribe-qos1.py
./02-unsubscribe-qos2.py
Expand Down
1 change: 1 addition & 0 deletions test/broker/ptest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
(1, './02-subpub-qos1-nolocal.py'),
(1, './02-subpub-qos0-retain-as-publish.py'),
(1, './02-subpub-qos0-send-retain.py'),
(1, './02-subpub-qos0-subscription-id.py'),
(1, './02-unsubscribe-qos0.py'),
(1, './02-unsubscribe-qos1.py'),
(1, './02-unsubscribe-qos2.py'),
Expand Down
10 changes: 7 additions & 3 deletions test/mosq_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,14 @@ def gen_pubrel(mid, dup=False, proto_ver=4, reason_code=0):
def gen_pubcomp(mid, proto_ver=4, reason_code=0):
return _gen_command_with_mid(112, mid, proto_ver, reason_code)

def gen_subscribe(mid, topic, qos, proto_ver=4):
def gen_subscribe(mid, topic, qos, proto_ver=4, properties=""):
if proto_ver == 5:
pack_format = "!BBHBH"+str(len(topic))+"sB"
return struct.pack(pack_format, 130, 2+1+2+len(topic)+1, mid, 0, len(topic), topic, qos)
if properties == "":
pack_format = "!BBHBH"+str(len(topic))+"sB"
return struct.pack(pack_format, 130, 2+1+2+len(topic)+1, mid, 0, len(topic), topic, qos)
else:
pack_format = "!BBH"+str(len(properties))+"s"+"H"+str(len(topic))+"sB"
return struct.pack(pack_format, 130, 2+1+2+len(topic)+len(properties), mid, properties, len(topic), topic, qos)
else:
pack_format = "!BBHH"+str(len(topic))+"sB"
return struct.pack(pack_format, 130, 2+2+len(topic)+1, mid, len(topic), topic, qos)
Expand Down
4 changes: 4 additions & 0 deletions test/mqtt5_props.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def gen_string_pair_prop(identifier, s1, s2):
prop = struct.pack('!BH%dsH%ds'%(len(s1), len(s2)), identifier, len(s1), s1, len(s2), s2)
return prop

def gen_varint_prop(identifier, val):
v = pack_varint(val)
return struct.pack("!B"+str(len(v))+"s", identifier, v)

def pack_varint(varint):
s = ""
while True:
Expand Down

0 comments on commit c1baf21

Please sign in to comment.