Add test for MOSQ_EVT_PSK_KEY
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")
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue