From e49f741cd2513296e10a2de7c0f4062500b66c23 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 10 Nov 2022 12:48:47 +0000 Subject: [PATCH] Individual basic WS tests for clients --- test/client/02-subscribe-qos1-ws.py | 82 +++++++++++++++++++++++++ test/client/03-publish-qos1-ws.py | 82 +++++++++++++++++++++++++ test/client/04-rr-qos1-ws.py | 94 +++++++++++++++++++++++++++++ test/client/Makefile | 9 +++ test/client/test.py | 3 + 5 files changed, 270 insertions(+) create mode 100755 test/client/02-subscribe-qos1-ws.py create mode 100755 test/client/03-publish-qos1-ws.py create mode 100755 test/client/04-rr-qos1-ws.py diff --git a/test/client/02-subscribe-qos1-ws.py b/test/client/02-subscribe-qos1-ws.py new file mode 100755 index 00000000..bdc20452 --- /dev/null +++ b/test/client/02-subscribe-qos1-ws.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python3 + +# + +from mosq_test_helper import * + +def write_config(filename, port1, port2): + with open(filename, 'w') as f: + f.write("allow_anonymous true\n") + f.write(f"listener {port1}\n") + f.write("protocol websockets\n") + f.write(f"listener {port2}\n") + +def do_test(proto_ver): + rc = 1 + + ports = mosq_test.get_port(2) + conf_file = os.path.basename(__file__).replace('.py', '.conf') + + if proto_ver == 5: + V = 'mqttv5' + elif proto_ver == 4: + V = 'mqttv311' + else: + V = 'mqttv31' + + env = { + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib:'+ os.getenv("LD_LIBRARY_PATH", ""), + 'XDG_CONFIG_HOME':'/tmp/missing' + } + cmd = ['../../client/mosquitto_sub', + '-p', str(ports[0]), + '-q', '1', + '-t', '02/sub/qos1/test', + '-V', V, + '-C', '1', + '--ws' + ] + + payload = "message" + publish_packet_s = mosq_test.gen_publish("02/sub/qos1/test", qos=1, mid=1, payload=payload, proto_ver=proto_ver) + puback_packet_s = mosq_test.gen_puback(1, proto_ver=proto_ver) + + write_config(conf_file, ports[0], ports[1]) + + try: + broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=ports[1], use_conf=True) + + sock = mosq_test.pub_helper(port=ports[1], proto_ver=proto_ver) + + sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) + time.sleep(0.1) + sock.send(publish_packet_s) + mosq_test.expect_packet(sock, "puback", puback_packet_s) + sub_terminate_rc = 0 + if mosq_test.wait_for_subprocess(sub): + print("sub not terminated") + sub_terminate_rc = 1 + (stdo, stde) = sub.communicate() + if stdo.decode('utf-8') == payload + '\n': + rc = sub_terminate_rc + sock.close() + except mosq_test.TestError: + pass + except Exception as e: + print(e) + finally: + broker.terminate() + os.remove(conf_file) + if mosq_test.wait_for_subprocess(broker): + print("broker not terminated") + if rc == 0: rc=1 + (stdo, stde) = broker.communicate() + if rc: + print(stde.decode('utf-8')) + print("proto_ver=%d" % (proto_ver)) + exit(rc) + + +do_test(proto_ver=3) +do_test(proto_ver=4) +do_test(proto_ver=5) diff --git a/test/client/03-publish-qos1-ws.py b/test/client/03-publish-qos1-ws.py new file mode 100755 index 00000000..b1905e89 --- /dev/null +++ b/test/client/03-publish-qos1-ws.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python3 + +# + +from mosq_test_helper import * + +def write_config(filename, port1, port2): + with open(filename, 'w') as f: + f.write("allow_anonymous true\n") + f.write(f"listener {port1}\n") + f.write("protocol websockets\n") + f.write(f"listener {port2}\n") + +def do_test(proto_ver): + rc = 1 + + ports = mosq_test.get_port(2) + conf_file = os.path.basename(__file__).replace('.py', '.conf') + + if proto_ver == 5: + V = 'mqttv5' + elif proto_ver == 4: + V = 'mqttv311' + else: + V = 'mqttv31' + + env = { + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib:'+ os.getenv("LD_LIBRARY_PATH", ""), + 'XDG_CONFIG_HOME':'/tmp/missing' + } + cmd = ['../../client/mosquitto_pub', + '-p', str(ports[0]), + '-q', '1', + '-t', '03/pub/qos1/test', + '-m', 'message', + '-V', V, + '--ws' + ] + + mid = 1 + publish_packet = mosq_test.gen_publish("03/pub/qos1/test", qos=1, mid=mid, payload="message", proto_ver=proto_ver) + if proto_ver == 5: + puback_packet = mosq_test.gen_puback(mid, proto_ver=proto_ver, reason_code=mqtt5_rc.MQTT_RC_NO_MATCHING_SUBSCRIBERS) + else: + puback_packet = mosq_test.gen_puback(mid, proto_ver=proto_ver) + + write_config(conf_file, ports[0], ports[1]) + + try: + broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=ports[1], use_conf=True) + sock = mosq_test.sub_helper(port=ports[1], topic="#", qos=1, proto_ver=proto_ver) + + pub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) + pub_terminate_rc = 0 + if mosq_test.wait_for_subprocess(pub): + print("pub not terminated") + pub_terminate_rc = 1 + (stdo, stde) = pub.communicate() + + mosq_test.expect_packet(sock, "publish", publish_packet) + rc = pub_terminate_rc + sock.close() + except mosq_test.TestError: + pass + except Exception as e: + print(e) + finally: + broker.terminate() + os.remove(conf_file) + if mosq_test.wait_for_subprocess(broker): + print("broker not terminated") + if rc == 0: rc=1 + (stdo, stde) = broker.communicate() + if rc: + print(stde.decode('utf-8')) + print("proto_ver=%d" % (proto_ver)) + exit(rc) + + +do_test(proto_ver=3) +do_test(proto_ver=4) +do_test(proto_ver=5) diff --git a/test/client/04-rr-qos1-ws.py b/test/client/04-rr-qos1-ws.py new file mode 100755 index 00000000..c886beb0 --- /dev/null +++ b/test/client/04-rr-qos1-ws.py @@ -0,0 +1,94 @@ +#!/usr/bin/env python3 + +# + +from mosq_test_helper import * + +def write_config(filename, port1, port2): + with open(filename, 'w') as f: + f.write("allow_anonymous true\n") + f.write(f"listener {port1}\n") + f.write("protocol websockets\n") + f.write(f"listener {port2}\n") + +def do_test(proto_ver): + rc = 1 + + ports = mosq_test.get_port(2) + conf_file = os.path.basename(__file__).replace('.py', '.conf') + + if proto_ver == 5: + V = 'mqttv5' + elif proto_ver == 4: + V = 'mqttv311' + else: + V = 'mqttv31' + + env = { + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib:'+ os.getenv("LD_LIBRARY_PATH", ""), + 'XDG_CONFIG_HOME':'/tmp/missing' + } + payload = "message" + cmd = ['../../client/mosquitto_rr', + '-p', str(ports[0]), + '-q', '1', + '-t', '04/rr/qos1/test/request', + '-e', '04/rr/qos1/test/response', + '-V', V, + '-m', payload, + '--ws' + ] + + if proto_ver == 5: + props = mqtt5_props.gen_string_prop(mqtt5_props.PROP_RESPONSE_TOPIC, "04/rr/qos1/test/response") + else: + props = None + publish_packet_req = mosq_test.gen_publish("04/rr/qos1/test/request", qos=1, mid=1, payload=payload, proto_ver=proto_ver, properties=props) + payload = "the response" + publish_packet_resp = mosq_test.gen_publish("04/rr/qos1/test/response", qos=1, mid=2, payload=payload, proto_ver=proto_ver) + puback_packet_req = mosq_test.gen_puback(1, proto_ver=proto_ver) + puback_packet_resp = mosq_test.gen_puback(2, proto_ver=proto_ver) + + write_config(conf_file, ports[0], ports[1]) + + try: + broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=ports[1], use_conf=True) + sock = mosq_test.sub_helper(port=ports[1], topic="04/rr/qos1/test/request", qos=1, proto_ver=proto_ver) + + rr = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) + + mosq_test.expect_packet(sock, "publish", publish_packet_req) + sock.send(puback_packet_req) + + sock.send(publish_packet_resp) + mosq_test.expect_packet(sock, "puback", puback_packet_resp) + + time.sleep(0.1) + rr_terminate_rc = 0 + if mosq_test.wait_for_subprocess(rr): + print("rr not terminated") + rr_terminate_rc = 1 + (stdo, stde) = rr.communicate() + if stdo.decode('utf-8') == payload + '\n': + rc = rr_terminate_rc + sock.close() + except mosq_test.TestError: + pass + except Exception as e: + print(e) + finally: + broker.terminate() + os.remove(conf_file) + if mosq_test.wait_for_subprocess(broker): + print("broker not terminated") + if rc == 0: rc=1 + (stdo, stde) = broker.communicate() + if rc: + print(stde.decode('utf-8')) + print("proto_ver=%d" % (proto_ver)) + exit(rc) + + +do_test(proto_ver=3) +do_test(proto_ver=4) +do_test(proto_ver=5) diff --git a/test/client/Makefile b/test/client/Makefile index a03ff08e..34625e8b 100644 --- a/test/client/Makefile +++ b/test/client/Makefile @@ -29,6 +29,9 @@ endif ./02-subscribe-format-json-properties.py ./02-subscribe-format-json-retain.py ./02-subscribe-qos1.py +ifeq ($(WITH_WEBSOCKETS),yes) + ./02-subscribe-qos1-ws.py +endif ./02-subscribe-format.py ./02-subscribe-null.py ./02-subscribe-verbose.py @@ -48,6 +51,9 @@ endif ./03-publish-qos0-empty.py ./03-publish-qos1-properties.py ./03-publish-qos1.py +ifeq ($(WITH_WEBSOCKETS),yes) + ./03-publish-qos1-ws.py +endif ./03-publish-repeat.py ./03-publish-socks.py ./03-publish-stdin-file.py @@ -64,6 +70,9 @@ endif endif ./04-rr-env.py ./04-rr-qos1.py +ifeq ($(WITH_WEBSOCKETS),yes) + ./04-rr-qos1-ws.py +endif ptest : ./test.sh diff --git a/test/client/test.py b/test/client/test.py index ce8993cc..b2fd8522 100755 --- a/test/client/test.py +++ b/test/client/test.py @@ -17,6 +17,7 @@ tests = [ (1, './02-subscribe-format-json-retain.py'), (1, './02-subscribe-null.py'), (1, './02-subscribe-qos1.py'), + (1, './02-subscribe-qos1-ws.py'), (1, './02-subscribe-verbose.py'), (1, './03-publish-argv-errors-tls-psk.py'), @@ -29,6 +30,7 @@ tests = [ (1, './03-publish-qos0-empty.py'), (1, './03-publish-qos1-properties.py'), (1, './03-publish-qos1.py'), + (1, './03-publish-qos1-ws.py'), (1, './03-publish-repeat.py'), (1, './03-publish-url.py'), @@ -41,6 +43,7 @@ tests = [ (1, './04-rr-argv-errors-without-tls.py'), (1, './04-rr-env.py'), (1, './04-rr-qos1.py'), + (1, './04-rr-qos1-ws.py'), ] ptest.run_tests(tests)