From b6de59712717ee1da34fffbe7e887c601c473a4e Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Wed, 30 Nov 2022 18:52:38 +0000 Subject: [PATCH] subscribe_simple helper test --- test/lib/02-subscribe-helper-qos2.py | 60 +++++++++++++++++++++++++++ test/lib/Makefile | 1 + test/lib/c/02-subscribe-helper-qos2.c | 28 +++++++++++++ test/lib/c/CMakeLists.txt | 1 + test/lib/c/Makefile | 1 + 5 files changed, 91 insertions(+) create mode 100755 test/lib/02-subscribe-helper-qos2.py create mode 100644 test/lib/c/02-subscribe-helper-qos2.c diff --git a/test/lib/02-subscribe-helper-qos2.py b/test/lib/02-subscribe-helper-qos2.py new file mode 100755 index 00000000..7789e05b --- /dev/null +++ b/test/lib/02-subscribe-helper-qos2.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 + +# Test whether a client sends a correct SUBSCRIBE to a topic with QoS 2. + +# The client should connect to port 1888 with keepalive=60, clean session set, +# and client id subscribe-qos2-test +# The test will send a CONNACK message to the client with rc=0. Upon receiving +# the CONNACK and verifying that rc=0, the client should send a SUBSCRIBE +# message to subscribe to topic "qos2/test" with QoS=2. If rc!=0, the client +# should exit with an error. +# Upon receiving the correct SUBSCRIBE message, the test will reply with a +# SUBACK message with the accepted QoS set to 2. On receiving the SUBACK +# message, the client should send a DISCONNECT message. + +from mosq_test_helper import * + +port = mosq_test.get_lib_port() + +rc = 1 +keepalive = 60 +connect_packet = mosq_test.gen_connect("subscribe-qos2-test", keepalive=keepalive) +connack_packet = mosq_test.gen_connack(rc=0) + +disconnect_packet = mosq_test.gen_disconnect() + +mid = 1 +subscribe_packet = mosq_test.gen_subscribe(mid, "qos2/test", 2) +suback_packet = mosq_test.gen_suback(mid, 2) + +publish_packet = mosq_test.gen_publish("qos2/test", 0, "message") + +sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) +sock.settimeout(10) +sock.bind(('', port)) +sock.listen(5) + +client_args = sys.argv[1:] +client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, port=port) + +try: + (conn, address) = sock.accept() + conn.settimeout(10) + + mosq_test.do_receive_send(conn, connect_packet, connack_packet, "connect") + mosq_test.do_receive_send(conn, subscribe_packet, suback_packet, "subscribe") + conn.send(publish_packet) + mosq_test.expect_packet(conn, "disconnect", disconnect_packet) + rc = 0 + + conn.close() +except mosq_test.TestError: + pass +finally: + if mosq_test.wait_for_subprocess(client): + print("test client not finished") + rc=1 + sock.close() + +exit(rc) diff --git a/test/lib/Makefile b/test/lib/Makefile index 08b92c37..c9b9c7aa 100644 --- a/test/lib/Makefile +++ b/test/lib/Makefile @@ -41,6 +41,7 @@ c : test-compile ./02-subscribe-qos1.py $@/02-subscribe-qos1-async1.test ./02-subscribe-qos1.py $@/02-subscribe-qos1-async2.test ./02-subscribe-qos2.py $@/02-subscribe-qos2.test + ./02-subscribe-helper-qos2.py $@/02-subscribe-helper-qos2.test ./02-unsubscribe-multiple-v5.py $@/02-unsubscribe-multiple-v5.test ./02-unsubscribe-v5.py $@/02-unsubscribe-v5.test ./02-unsubscribe.py $@/02-unsubscribe.test diff --git a/test/lib/c/02-subscribe-helper-qos2.c b/test/lib/c/02-subscribe-helper-qos2.c new file mode 100644 index 00000000..8a698cf9 --- /dev/null +++ b/test/lib/c/02-subscribe-helper-qos2.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +#define QOS 2 + +int main(int argc, char *argv[]) +{ + int port; + struct mosquitto_message *messages; + + if(argc < 2){ + return 1; + } + port = atoi(argv[1]); + + mosquitto_lib_init(); + + mosquitto_subscribe_simple(&messages, 1, + true, "qos2/test", QOS, "localhost", port, + "subscribe-qos2-test", 60, true, NULL, NULL, NULL, NULL); + + mosquitto_message_free(&messages); + + mosquitto_lib_cleanup(); + return 0; +} diff --git a/test/lib/c/CMakeLists.txt b/test/lib/c/CMakeLists.txt index 123786e5..d6392359 100644 --- a/test/lib/c/CMakeLists.txt +++ b/test/lib/c/CMakeLists.txt @@ -11,6 +11,7 @@ set(BINARIES 01-unpwd-set 01-will-set 01-will-unpwd-set + 02-subscribe-helper-qos2 02-subscribe-qos0 02-subscribe-qos1-async1 02-subscribe-qos1-async2 diff --git a/test/lib/c/Makefile b/test/lib/c/Makefile index c86e502a..71a8c4c4 100644 --- a/test/lib/c/Makefile +++ b/test/lib/c/Makefile @@ -19,6 +19,7 @@ SRC = \ 01-unpwd-set.c \ 01-will-set.c \ 01-will-unpwd-set.c \ + 02-subscribe-helper-qos2.c \ 02-subscribe-qos0.c \ 02-subscribe-qos1-async1.c \ 02-subscribe-qos1-async2.c \