From 223d9eae66e401c2616e80492e725c3945003131 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Sun, 23 Jan 2022 23:07:36 +0000 Subject: [PATCH] mosquitto_sub json output tests, and user-properties fix. --- client/sub_client_output.c | 16 ++- .../02-subscribe-format-json-properties.py | 97 +++++++++++++++++++ test/client/02-subscribe-format-json-qos0.py | 70 +++++++++++++ test/client/02-subscribe-format-json-qos1.py | 72 ++++++++++++++ .../client/02-subscribe-format-json-retain.py | 69 +++++++++++++ test/client/Makefile | 5 + test/client/test.py | 4 + 7 files changed, 328 insertions(+), 5 deletions(-) create mode 100755 test/client/02-subscribe-format-json-properties.py create mode 100755 test/client/02-subscribe-format-json-qos0.py create mode 100755 test/client/02-subscribe-format-json-qos1.py create mode 100755 test/client/02-subscribe-format-json-retain.py diff --git a/client/sub_client_output.c b/client/sub_client_output.c index d0c3ec0e..a499fdeb 100644 --- a/client/sub_client_output.c +++ b/client/sub_client_output.c @@ -174,7 +174,7 @@ static int json_print_properties(cJSON *root, const mosquitto_property *properti uint32_t i32value = 0; char *strname = NULL, *strvalue = NULL; char *binvalue = NULL; - cJSON *tmp, *prop_json, *user_json = NULL; + cJSON *tmp, *prop_json, *user_props = NULL, *user_json; const mosquitto_property *prop = NULL; prop_json = cJSON_CreateObject(); @@ -226,13 +226,19 @@ static int json_print_properties(cJSON *root, const mosquitto_property *properti break; case MQTT_PROP_USER_PROPERTY: - if(user_json == NULL){ - user_json = cJSON_CreateObject(); - if(user_json == NULL){ + if(user_props == NULL){ + user_props = cJSON_CreateArray(); + if(user_props == NULL){ return MOSQ_ERR_NOMEM; } - cJSON_AddItemToObject(prop_json, "user-properties", user_json); + cJSON_AddItemToObject(prop_json, "user-properties", user_props); } + + user_json = cJSON_CreateObject(); + if(user_props == NULL){ + return MOSQ_ERR_NOMEM; + } + cJSON_AddItemToArray(user_props, user_json); mosquitto_property_read_string_pair(prop, MQTT_PROP_USER_PROPERTY, &strname, &strvalue, false); if(strname == NULL || strvalue == NULL) return MOSQ_ERR_NOMEM; diff --git a/test/client/02-subscribe-format-json-properties.py b/test/client/02-subscribe-format-json-properties.py new file mode 100755 index 00000000..77f3d5e8 --- /dev/null +++ b/test/client/02-subscribe-format-json-properties.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python3 + +# + +from mosq_test_helper import * +import json + +def do_test(proto_ver): + rc = 1 + + port = mosq_test.get_port() + + if proto_ver == 5: + V = 'mqttv5' + elif proto_ver == 4: + V = 'mqttv311' + else: + V = 'mqttv31' + + env = { + 'LD_LIBRARY_PATH':'../../lib', + 'XDG_CONFIG_HOME':'/tmp/missing' + } + cmd = ['../../client/mosquitto_sub', + '-p', str(port), + '-q', '1', + '-F', '%j', + '-t', '02/sub/format/json/properties/test', + '-V', V, + '-C', '1' + ] + + props = mqtt5_props.gen_byte_prop(mqtt5_props.PROP_PAYLOAD_FORMAT_INDICATOR, 1) + props += mqtt5_props.gen_string_prop(mqtt5_props.PROP_CONTENT_TYPE, "plain/text") + props += mqtt5_props.gen_string_prop(mqtt5_props.PROP_RESPONSE_TOPIC, "/dev/null") + #props += mqtt5_props.gen_string_prop(mqtt5_props.PROP_CORRELATION_DATA, "2357289375902345") + props += mqtt5_props.gen_string_pair_prop(mqtt5_props.PROP_USER_PROPERTY, "name", "value") + props += mqtt5_props.gen_string_pair_prop(mqtt5_props.PROP_USER_PROPERTY, "name", "value") + props += mqtt5_props.gen_string_pair_prop(mqtt5_props.PROP_USER_PROPERTY, "name", "value") + props += mqtt5_props.gen_string_pair_prop(mqtt5_props.PROP_USER_PROPERTY, "name", "value") + publish_packet = mosq_test.gen_publish("02/sub/format/json/properties/test", mid=1, qos=1, payload="message", proto_ver=proto_ver, properties=props) + + broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port) + + expected = { + "tst": "", + "topic": "02/sub/format/json/properties/test", + "qos": 1, + "retain": 0, + "payloadlen": 7, + "mid": 1, + "properties": { + "payload-format-indicator": 1, + "content-type": "plain/text", + "response-topic": "/dev/null", + "user-properties": [ + {"name": "value"}, + {"name": "value"}, + {"name": "value"}, + {"name": "value"} + ] + }, + "payload": "message" + } + + try: + sock = mosq_test.pub_helper(port=port, proto_ver=proto_ver) + + sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) + time.sleep(0.1) + sock.send(publish_packet) + sub.wait() + (stdo, stde) = sub.communicate() + j = json.loads(stdo.decode('utf-8')) + j['tst'] = "" + + if j == expected: + rc = 0 + else: + print(json.dumps(j)) + print(json.dumps(expected)) + sock.close() + except mosq_test.TestError: + pass + except Exception as e: + print(e) + finally: + broker.terminate() + broker.wait() + (stdo, stde) = broker.communicate() + if rc: + print(stde.decode('utf-8')) + print("proto_ver=%d" % (proto_ver)) + exit(rc) + + +do_test(proto_ver=5) diff --git a/test/client/02-subscribe-format-json-qos0.py b/test/client/02-subscribe-format-json-qos0.py new file mode 100755 index 00000000..19529301 --- /dev/null +++ b/test/client/02-subscribe-format-json-qos0.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 + +# + +from mosq_test_helper import * +import json + +def do_test(proto_ver): + rc = 1 + + port = mosq_test.get_port() + + if proto_ver == 5: + V = 'mqttv5' + elif proto_ver == 4: + V = 'mqttv311' + else: + V = 'mqttv31' + + env = { + 'LD_LIBRARY_PATH':'../../lib', + 'XDG_CONFIG_HOME':'/tmp/missing' + } + cmd = ['../../client/mosquitto_sub', + '-p', str(port), + '-F', '%j', + '-t', '02/sub/format/json/test', + '-V', V, + '-C', '1' + ] + + publish_packet = mosq_test.gen_publish("02/sub/format/json/test", qos=0, payload="message", proto_ver=proto_ver) + + broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port) + + expected = {"tst": "", "topic": "02/sub/format/json/test", "qos": 0, "retain": 0, "payloadlen": 7, "payload": "message"} + + try: + sock = mosq_test.pub_helper(port=port, proto_ver=proto_ver) + + sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) + time.sleep(0.1) + sock.send(publish_packet) + sub.wait() + (stdo, stde) = sub.communicate() + j = json.loads(stdo.decode('utf-8')) + j['tst'] = "" + + if j == expected: + rc = 0 + else: + print(json.dumps(j)) + sock.close() + except mosq_test.TestError: + pass + except Exception as e: + print(e) + finally: + broker.terminate() + broker.wait() + (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/02-subscribe-format-json-qos1.py b/test/client/02-subscribe-format-json-qos1.py new file mode 100755 index 00000000..d893f7c0 --- /dev/null +++ b/test/client/02-subscribe-format-json-qos1.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 + +# + +from mosq_test_helper import * +import json + +def do_test(proto_ver): + rc = 1 + + port = mosq_test.get_port() + + if proto_ver == 5: + V = 'mqttv5' + elif proto_ver == 4: + V = 'mqttv311' + else: + V = 'mqttv31' + + env = { + 'LD_LIBRARY_PATH':'../../lib', + 'XDG_CONFIG_HOME':'/tmp/missing' + } + cmd = ['../../client/mosquitto_sub', + '-p', str(port), + '-q', '1', + '-F', '%j', + '-t', '02/sub/format/json/qos1/test', + '-V', V, + '-C', '1' + ] + + publish_packet = mosq_test.gen_publish("02/sub/format/json/qos1/test", mid=1, qos=1, payload="message", proto_ver=proto_ver) + + broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port) + + expected = {"tst": "", "topic": "02/sub/format/json/qos1/test", "qos": 1, "mid": 1, "retain": 0, "payloadlen": 7, "payload": "message"} + + try: + sock = mosq_test.pub_helper(port=port, proto_ver=proto_ver) + + sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) + time.sleep(0.1) + sock.send(publish_packet) + sub.wait() + (stdo, stde) = sub.communicate() + j = json.loads(stdo.decode('utf-8')) + j['tst'] = "" + + if j == expected: + rc = 0 + else: + print(json.dumps(expected)) + print(json.dumps(j)) + sock.close() + except mosq_test.TestError: + pass + except Exception as e: + print(e) + finally: + broker.terminate() + broker.wait() + (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/02-subscribe-format-json-retain.py b/test/client/02-subscribe-format-json-retain.py new file mode 100755 index 00000000..ce9ff5a8 --- /dev/null +++ b/test/client/02-subscribe-format-json-retain.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 + +# + +from mosq_test_helper import * +import json + +def do_test(proto_ver): + rc = 1 + + port = mosq_test.get_port() + + if proto_ver == 5: + V = 'mqttv5' + elif proto_ver == 4: + V = 'mqttv311' + else: + V = 'mqttv31' + + env = { + 'LD_LIBRARY_PATH':'../../lib', + 'XDG_CONFIG_HOME':'/tmp/missing' + } + cmd = ['../../client/mosquitto_sub', + '-p', str(port), + '-F', '%j', + '-t', '02/sub/format/json/retain/test', + '-V', V, + '-C', '1' + ] + + publish_packet = mosq_test.gen_publish("02/sub/format/json/retain/test", qos=0, payload="message", proto_ver=proto_ver, retain=True) + + broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port) + + expected = {"tst": "", "topic": "02/sub/format/json/retain/test", "qos": 0, "retain": 1, "payloadlen": 7, "payload": "message"} + + try: + sock = mosq_test.pub_helper(port=port, proto_ver=proto_ver) + sock.send(publish_packet) + + sub = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) + sub.wait() + (stdo, stde) = sub.communicate() + j = json.loads(stdo.decode('utf-8')) + j['tst'] = "" + + if j == expected: + rc = 0 + else: + print(json.dumps(j)) + sock.close() + except mosq_test.TestError: + pass + except Exception as e: + print(e) + finally: + broker.terminate() + broker.wait() + (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 5e2d9c4e..f5c9feaa 100644 --- a/test/client/Makefile +++ b/test/client/Makefile @@ -10,6 +10,11 @@ test : 02 03 02 : ./02-subscribe-argv-errors.py ./02-subscribe-filter-out.py + ./02-subscribe-format.py + ./02-subscribe-format-json-qos0.py + ./02-subscribe-format-json-qos1.py + ./02-subscribe-format-json-properties.py + ./02-subscribe-format-json-retain.py ./02-subscribe-qos1.py ./02-subscribe-format.py ./02-subscribe-null.py diff --git a/test/client/test.py b/test/client/test.py index 4be10b09..26ec68ae 100755 --- a/test/client/test.py +++ b/test/client/test.py @@ -8,6 +8,10 @@ tests = [ (1, './02-subscribe-argv-errors.py'), (1, './02-subscribe-filter-out.py'), (1, './02-subscribe-format.py'), + (1, './02-subscribe-format-json-qos0.py'), + (1, './02-subscribe-format-json-qos1.py'), + (1, './02-subscribe-format-json-properties.py'), + (1, './02-subscribe-format-json-retain.py'), (1, './02-subscribe-null.py'), (1, './02-subscribe-qos1.py'), (1, './02-subscribe-verbose.py'),