From 907b754566fcddd1ebaedfe35d27e3ba6762c87e Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Mon, 28 Nov 2022 00:45:14 +0000 Subject: [PATCH] Add test for MOSQ_EVT_PSK_KEY --- test/broker/09-plugin-evt-psk-key.py | 77 ++++++++++++++++++++++++++++ test/broker/Makefile | 1 + test/broker/c/Makefile | 3 +- test/broker/c/plugin_evt_psk_key.c | 40 +++++++++++++++ test/broker/test.py | 1 + 5 files changed, 121 insertions(+), 1 deletion(-) create mode 100755 test/broker/09-plugin-evt-psk-key.py create mode 100644 test/broker/c/plugin_evt_psk_key.c diff --git a/test/broker/09-plugin-evt-psk-key.py b/test/broker/09-plugin-evt-psk-key.py new file mode 100755 index 00000000..83144a95 --- /dev/null +++ b/test/broker/09-plugin-evt-psk-key.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 + +# Test whether a plugin can subscribe to the tick event + +from mosq_test_helper import * + +def write_config(filename, port, per_listener_settings="false"): + with open(filename, 'w') as f: + f.write("per_listener_settings %s\n" % (per_listener_settings)) + f.write("listener %d\n" % (port)) + f.write("plugin c/plugin_evt_psk_key.so\n") + f.write("psk_hint myhint\n") + f.write("allow_anonymous true\n") + + +def do_test(per_listener_settings): + rc = 1 + proto_ver = 5 + port = mosq_test.get_port() + conf_file = os.path.basename(__file__).replace('.py', '.conf') + write_config(conf_file, port, per_listener_settings) + + broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port) + + try: + bad_client_args = [mosq_test.get_build_root() + '/client/mosquitto_pub', + '-t', 'plugin/psk/test', + '-m', 'psk-test', + '--psk', '159445', + '--psk-identity', 'badidentity', + '-p'] + bad_client = mosq_test.start_client(filename=sys.argv[0].replace('/', '-'), cmd=bad_client_args, port=port) + bad_client.wait() + if bad_client.returncode == 0: + raise ValueError('bad client should have failed') + + sub_client_args = [mosq_test.get_build_root() + '/client/mosquitto_sub', + '-t', 'plugin/psk/test', + '-C', '1', + '-W', '2', + '--psk', '159445', + '--psk-identity', 'subidentity', + '-v', '-N', + '-p'] + sub_client = mosq_test.start_client(filename=sys.argv[0].replace('/', '-'), cmd=sub_client_args, port=port) + + pub_client_args = [mosq_test.get_build_root() + '/client/mosquitto_pub', + '-t', 'plugin/psk/test', + '-m', 'psk-test', + '--psk', '297A49', + '--psk-identity', 'pubidentity', + '-p'] + pub_client = mosq_test.start_client(filename=sys.argv[0].replace('/', '-'), cmd=pub_client_args, port=port) + pub_client.wait() + sub_client.wait() + + if pub_client.returncode == 0 and sub_client.returncode == 0: + (stdo, _) = sub_client.communicate() + if stdo.decode('utf-8') != "plugin/psk/test psk-test": + raise ValueError(stdo.decode('utf-8')) + + rc = 0 + except mosq_test.TestError: + pass + except Exception as err: + print(err) + finally: + os.remove(conf_file) + broker.terminate() + broker.wait() + (stdo, stde) = broker.communicate() + if rc: + print(stde.decode('utf-8')) + exit(rc) + +do_test("false") +do_test("true") diff --git a/test/broker/Makefile b/test/broker/Makefile index 183c18fd..4db49586 100644 --- a/test/broker/Makefile +++ b/test/broker/Makefile @@ -202,6 +202,7 @@ endif ./09-plugin-auth-v5-unpwd-success.py ./09-plugin-change-id.py ./09-plugin-delayed-auth.py + ./09-plugin-evt-psk-key.py ./09-plugin-publish.py ./09-plugin-tick.py ./09-pwfile-parse-invalid.py diff --git a/test/broker/c/Makefile b/test/broker/c/Makefile index c65d66f6..1e098408 100644 --- a/test/broker/c/Makefile +++ b/test/broker/c/Makefile @@ -24,7 +24,8 @@ PLUGIN_SRC = \ auth_plugin_v5_control.c \ auth_plugin_v5_handle_message.c \ auth_plugin_v5_handle_tick.c \ - plugin_control.c + plugin_control.c \ + plugin_evt_psk_key.c PLUGINS = ${PLUGIN_SRC:.c=.so} diff --git a/test/broker/c/plugin_evt_psk_key.c b/test/broker/c/plugin_evt_psk_key.c new file mode 100644 index 00000000..5d22f890 --- /dev/null +++ b/test/broker/c/plugin_evt_psk_key.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include +#include +#include + +static mosquitto_plugin_id_t *plg_id = NULL; + +MOSQUITTO_PLUGIN_DECLARE_VERSION(5); + +static int psk_callback(int event, void *event_data, void *userdata) +{ + struct mosquitto_evt_psk_key *ed = event_data; + + (void)userdata; + + if(!strcmp(ed->hint, "myhint") && !strcmp(ed->identity, "subidentity")){ + snprintf(ed->key, ed->max_key_len, "159445"); + }else if(!strcmp(ed->hint, "myhint") && !strcmp(ed->identity, "pubidentity")){ + snprintf(ed->key, ed->max_key_len, "297A49"); + }else{ + return MOSQ_ERR_INVAL; + } + + return 0; +} + +int mosquitto_plugin_init(mosquitto_plugin_id_t *identifier, void **user_data, struct mosquitto_opt *auth_opts, int auth_opt_count) +{ + (void)user_data; + (void)auth_opts; + (void)auth_opt_count; + + plg_id = identifier; + + mosquitto_callback_register(plg_id, MOSQ_EVT_PSK_KEY, psk_callback, NULL, NULL); + return MOSQ_ERR_SUCCESS; +} diff --git a/test/broker/test.py b/test/broker/test.py index e0c65926..95e72a82 100755 --- a/test/broker/test.py +++ b/test/broker/test.py @@ -170,6 +170,7 @@ tests = [ (1, './09-plugin-auth-v5-unpwd-fail.py'), (1, './09-plugin-auth-v5-unpwd-success.py'), (1, './09-plugin-change-id.py'), + (1, './09-plugin-evt-psk-key.py'), (1, './09-plugin-delayed-auth.py'), (1, './09-plugin-publish.py'), (1, './09-plugin-tick.py'),