Add test for MOSQ_EVT_PSK_KEY

pull/2345/merge
Roger A. Light 3 years ago
parent 6823e721e7
commit 907b754566

@ -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")

@ -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

@ -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}

@ -0,0 +1,40 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <mqtt_protocol.h>
#include <mosquitto.h>
#include <mosquitto_broker.h>
#include <mosquitto_plugin.h>
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;
}

@ -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'),

Loading…
Cancel
Save