From 6a75eb377b8d0c3a5d00dda0718b5003b4539c0a Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 9 Aug 2018 13:01:20 +0100 Subject: [PATCH] Add test for issue in #828. Signed-off-by: Roger A. Light --- test/broker/09-plugin-auth-msg-params.py | 61 +++++++++++++++++++ test/broker/Makefile | 1 + test/broker/c/Makefile | 5 +- test/broker/c/auth_plugin_msg_params.c | 76 ++++++++++++++++++++++++ test/broker/ptest.py | 1 + 5 files changed, 143 insertions(+), 1 deletion(-) create mode 100755 test/broker/09-plugin-auth-msg-params.py create mode 100644 test/broker/c/auth_plugin_msg_params.c diff --git a/test/broker/09-plugin-auth-msg-params.py b/test/broker/09-plugin-auth-msg-params.py new file mode 100755 index 00000000..5aa008e1 --- /dev/null +++ b/test/broker/09-plugin-auth-msg-params.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +# Test whether message parameters are passed to the plugin acl check function. + +import inspect, os, sys +# From http://stackoverflow.com/questions/279237/python-import-a-module-from-a-folder +cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],".."))) +if cmd_subfolder not in sys.path: + sys.path.insert(0, cmd_subfolder) + +import mosq_test + +def write_config(filename, port): + with open(filename, 'w') as f: + f.write("port %d\n" % (port)) + f.write("auth_plugin c/auth_plugin_msg_params.so\n") + f.write("allow_anonymous true\n") + +port = mosq_test.get_port() +conf_file = os.path.basename(__file__).replace('.py', '.conf') +write_config(conf_file, port) + +rc = 1 +keepalive = 10 +connect_packet = mosq_test.gen_connect("msg-param-test", keepalive=keepalive) +connack_packet = mosq_test.gen_connack(rc=0) + +mid = 2 +subscribe_packet = mosq_test.gen_subscribe(mid, "param/topic", 1) +suback_packet = mosq_test.gen_suback(mid, 1) + +mid = 3 +publish_packet = mosq_test.gen_publish(topic="param/topic", qos=1, payload="payload contents", retain=1, mid=mid) +puback_packet = mosq_test.gen_puback(mid) + +mid = 1 +publish_packet_recv = mosq_test.gen_publish(topic="param/topic", qos=1, payload="payload contents", retain=0, mid=mid) + + +broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port) + +try: + sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=20, port=port) + mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback") + mosq_test.do_send_receive(sock, publish_packet, puback_packet, "puback") + + if mosq_test.expect_packet(sock, "publish receive", publish_packet_recv): + rc = 0 + + sock.close() +finally: + os.remove(conf_file) + broker.terminate() + broker.wait() + (stdo, stde) = broker.communicate() + if rc: + print(stde) + + +exit(rc) + diff --git a/test/broker/Makefile b/test/broker/Makefile index f762cb64..97f7c1e4 100644 --- a/test/broker/Makefile +++ b/test/broker/Makefile @@ -119,6 +119,7 @@ endif ./09-plugin-auth-v2-unpwd-fail.py ./09-plugin-auth-defer-unpwd-success.py ./09-plugin-auth-defer-unpwd-fail.py + ./09-plugin-auth-msg-params.py 10 : ./10-listener-mount-point.py diff --git a/test/broker/c/Makefile b/test/broker/c/Makefile index ff11318b..1e6ec296 100644 --- a/test/broker/c/Makefile +++ b/test/broker/c/Makefile @@ -2,7 +2,7 @@ CFLAGS=-I../../../lib -I../../../src -Wall -Werror -all : auth_plugin.so auth_plugin_pwd.so auth_plugin_acl.so auth_plugin_v2.so 08 +all : auth_plugin.so auth_plugin_pwd.so auth_plugin_acl.so auth_plugin_v2.so auth_plugin_msg_params.so 08 08 : 08-tls-psk-pub.test 08-tls-psk-bridge.test @@ -18,6 +18,9 @@ auth_plugin_acl.so : auth_plugin_acl.c auth_plugin_v2.so : auth_plugin_v2.c $(CC) ${CFLAGS} -fPIC -shared $^ -o $@ +auth_plugin_msg_params.so : auth_plugin_msg_params.c + $(CC) ${CFLAGS} -fPIC -shared $^ -o $@ + 08-tls-psk-pub.test : 08-tls-psk-pub.c $(CC) ${CFLAGS} $^ -o $@ ../../../lib/libmosquitto.so.1 diff --git a/test/broker/c/auth_plugin_msg_params.c b/test/broker/c/auth_plugin_msg_params.c new file mode 100644 index 00000000..fcd0ccd1 --- /dev/null +++ b/test/broker/c/auth_plugin_msg_params.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +#include + +int mosquitto_auth_plugin_version(void) +{ + return MOSQ_AUTH_PLUGIN_VERSION; +} + +int mosquitto_auth_plugin_init(void **user_data, struct mosquitto_opt *auth_opts, int auth_opt_count) +{ + return MOSQ_ERR_SUCCESS; +} + +int mosquitto_auth_plugin_cleanup(void *user_data, struct mosquitto_opt *auth_opts, int auth_opt_count) +{ + return MOSQ_ERR_SUCCESS; +} + +int mosquitto_auth_security_init(void *user_data, struct mosquitto_opt *auth_opts, int auth_opt_count, bool reload) +{ + return MOSQ_ERR_SUCCESS; +} + +int mosquitto_auth_security_cleanup(void *user_data, struct mosquitto_opt *auth_opts, int auth_opt_count, bool reload) +{ + return MOSQ_ERR_SUCCESS; +} + +int mosquitto_auth_acl_check(void *user_data, int access, const struct mosquitto *client, const struct mosquitto_acl_msg *msg) +{ + if(access == MOSQ_ACL_SUBSCRIBE){ + return MOSQ_ERR_SUCCESS; + } + + if(!msg->topic || strcmp(msg->topic, "param/topic")){ + abort(); + return MOSQ_ERR_ACL_DENIED; + } + + if(!msg->payload || strncmp(msg->payload, "payload contents", strlen("payload contents"))){ + abort(); + return MOSQ_ERR_ACL_DENIED; + } + + if(msg->payloadlen != strlen("payload contents")){ + abort(); + return MOSQ_ERR_ACL_DENIED; + } + + if(msg->qos != 1){ + abort(); + return MOSQ_ERR_ACL_DENIED; + } + + if(!msg->retain){ + abort(); + return MOSQ_ERR_ACL_DENIED; + } + + return MOSQ_ERR_SUCCESS; +} + +int mosquitto_auth_unpwd_check(void *user_data, const struct mosquitto *client, const char *username, const char *password) +{ + return MOSQ_ERR_PLUGIN_DEFER; +} + +int mosquitto_auth_psk_key_get(void *user_data, const struct mosquitto *client, const char *hint, const char *identity, char *key, int max_key_len) +{ + return MOSQ_ERR_AUTH; +} + diff --git a/test/broker/ptest.py b/test/broker/ptest.py index 84b418c1..da662b88 100755 --- a/test/broker/ptest.py +++ b/test/broker/ptest.py @@ -91,6 +91,7 @@ tests = [ (1, './09-plugin-auth-v2-unpwd-fail.py'), (1, './09-plugin-auth-defer-unpwd-success.py'), (1, './09-plugin-auth-defer-unpwd-fail.py'), + (1, './09-plugin-auth-msg-params.py'), (2, './10-listener-mount-point.py'),