From 26b007f3fc83c8cb8b6bbb913f493ceb3357cd9f Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Fri, 13 May 2022 16:54:29 +0100 Subject: [PATCH 01/20] Fix Coverity 1488816, use of uninitialised value. --- client/sub_client_output.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/sub_client_output.c b/client/sub_client_output.c index a749caaf..eb3313e5 100644 --- a/client/sub_client_output.c +++ b/client/sub_client_output.c @@ -426,7 +426,8 @@ static void formatted_print_blank(char pad, int field_width) static int formatted_print_float(const unsigned char *payload, int payloadlen, char format, char align, char pad, int field_width, int precision) { float float_value; - double value; + double value = 0.0; + if (format == 'f'){ if (sizeof(float_value) != payloadlen) { return -1; From 2c8dc3968e627a665c19a9ab14063213bfd7fb12 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Wed, 18 May 2022 15:11:13 +0100 Subject: [PATCH 02/20] Migrate persist-sqlite to use persistence_location. --- plugins/persist-sqlite/plugin.c | 39 +++++++++++++++++++++++--------- plugins/persist-sqlite/test.conf | 4 ++-- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/plugins/persist-sqlite/plugin.c b/plugins/persist-sqlite/plugin.c index 809896ec..eb5eb099 100644 --- a/plugins/persist-sqlite/plugin.c +++ b/plugins/persist-sqlite/plugin.c @@ -31,6 +31,8 @@ Contributors: #include "persist_sqlite.h" +MOSQUITTO_PLUGIN_DECLARE_VERSION(5); + static mosquitto_plugin_id_t *plg_id = NULL; static struct mosquitto_sqlite plg_data; @@ -57,16 +59,32 @@ static void set_defaults(void) plg_data.page_size = 4 * 1024; } -int mosquitto_plugin_version(int supported_version_count, const int *supported_versions) +static int get_db_file(struct mosquitto_opt *options, int option_count) { + const char *persistence_location; int i; - for(i=0; i Date: Thu, 19 May 2022 15:28:37 +0100 Subject: [PATCH 03/20] Add more mosquitto_passwd examples --- man/mosquitto_passwd.1.xml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/man/mosquitto_passwd.1.xml b/man/mosquitto_passwd.1.xml index bd90fa51..30cb6bb2 100644 --- a/man/mosquitto_passwd.1.xml +++ b/man/mosquitto_passwd.1.xml @@ -191,6 +191,22 @@ mosquitto_passwd -c /etc/mosquitto/passwd ral + Add a user to an existing password file: + + mosquitto_passwd /etc/mosquitto/passwd ral + + Add a user to an existing password file, passing the password on the command line: + + mosquitto_passwd -b /etc/mosquitto/passwd ral z2Dr0BsvtZ + + Update the password for a user in an existing password file: + + mosquitto_passwd /etc/mosquitto/passwd ral + + Add a user to an existing password file using the sha512 hash for Mosquitto 1.6 compatibility: + + mosquitto_passwd -H sha512 /etc/mosquitto/passwd ral + Delete a user from a password file mosquitto_passwd -D /etc/mosquitto/passwd ral From e3246f547c986ad2d0c7088c1a114768363b0bac Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Wed, 25 May 2022 17:15:04 +0100 Subject: [PATCH 04/20] Print messages in mosquitto_passwd when adding/updating passwords. Closes #2544. Thanks to Shruti Nanda. --- ChangeLog.txt | 2 ++ apps/mosquitto_passwd/mosquitto_passwd.c | 3 +++ 2 files changed, 5 insertions(+) diff --git a/ChangeLog.txt b/ChangeLog.txt index 19d5ffff..9865d2cc 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -57,6 +57,8 @@ Broker: to trust default CA certificates. Closes #2473. - Add `--test-config` option which can be used to test a configuration file before trying to use it in a live broker. Closes #2521. +- Print messages in mosquitto_passwd when adding/updating passwords. + Closes #2544. Plugins / plugin interface: - Add persist-sqlite plugin. diff --git a/apps/mosquitto_passwd/mosquitto_passwd.c b/apps/mosquitto_passwd/mosquitto_passwd.c index 15ca90c4..7edeae03 100644 --- a/apps/mosquitto_passwd/mosquitto_passwd.c +++ b/apps/mosquitto_passwd/mosquitto_passwd.c @@ -336,8 +336,10 @@ static int update_pwuser(FILE *fptr, FILE *ftmp, const char *username, const cha rc = pwfile_iterate(fptr, ftmp, update_pwuser_cb, &helper); if(helper.found){ + printf("Updating password for user %s\n", username); return rc; }else{ + printf("Adding password for user %s\n", username); return output_new_password(ftmp, username, password, iterations); } } @@ -606,6 +608,7 @@ int main(int argc, char *argv[]) return 1; } free(password_file); + printf("Adding password for user %s\n", username); rc = output_new_password(fptr, username, password_cmd, iterations); fclose(fptr); return rc; From 4099f8d1b698b9d2008c90001c50efe950c3c2cd Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 2 Jun 2022 08:44:08 +0100 Subject: [PATCH 05/20] Store out_packet bytes rather than having to calculate it. --- lib/mosquitto.c | 1 + lib/mosquitto_internal.h | 1 + lib/packet_mosq.c | 39 +++++++++++++++++++++------------------ src/bridge.c | 1 + src/context.c | 2 ++ src/xtreport.c | 8 +------- 6 files changed, 27 insertions(+), 25 deletions(-) diff --git a/lib/mosquitto.c b/lib/mosquitto.c index e24a6916..1fecc3c1 100644 --- a/lib/mosquitto.c +++ b/lib/mosquitto.c @@ -183,6 +183,7 @@ int mosquitto_reinitialise(struct mosquitto *mosq, const char *id, bool clean_st packet__cleanup(&mosq->in_packet); mosq->out_packet = NULL; mosq->out_packet_count = 0; + mosq->out_packet_bytes = 0; mosq->last_msg_in = mosquitto_time(); mosq->next_msg_out = mosquitto_time() + mosq->keepalive; mosq->ping_t = 0; diff --git a/lib/mosquitto_internal.h b/lib/mosquitto_internal.h index 7f0cdd86..5db31bc4 100644 --- a/lib/mosquitto_internal.h +++ b/lib/mosquitto_internal.h @@ -307,6 +307,7 @@ struct mosquitto { uint16_t alias_max_l2r; uint32_t will_delay_interval; int out_packet_count; + int64_t out_packet_bytes; time_t will_delay_time; #ifdef WITH_TLS SSL *ssl; diff --git a/lib/packet_mosq.c b/lib/packet_mosq.c index afbd0a16..41764f5e 100644 --- a/lib/packet_mosq.c +++ b/lib/packet_mosq.c @@ -121,6 +121,7 @@ void packet__cleanup_all_no_locks(struct mosquitto *mosq) mosquitto__FREE(packet); } mosq->out_packet_count = 0; + mosq->out_packet_bytes = 0; mosq->out_packet_last = NULL; packet__cleanup(&mosq->in_packet); @@ -134,6 +135,21 @@ void packet__cleanup_all(struct mosquitto *mosq) } +static void packet__queue_append(struct mosquitto *mosq, struct mosquitto__packet *packet) +{ + pthread_mutex_lock(&mosq->out_packet_mutex); + if(mosq->out_packet){ + mosq->out_packet_last->next = packet; + }else{ + mosq->out_packet = packet; + } + mosq->out_packet_last = packet; + mosq->out_packet_count++; + mosq->out_packet_bytes += packet->packet_length; + pthread_mutex_unlock(&mosq->out_packet_mutex); +} + + int packet__queue(struct mosquitto *mosq, struct mosquitto__packet *packet) { #ifndef WITH_BROKER @@ -148,15 +164,7 @@ int packet__queue(struct mosquitto *mosq, struct mosquitto__packet *packet) packet->pos = WS_PACKET_OFFSET; packet->to_process = packet->packet_length - WS_PACKET_OFFSET; - pthread_mutex_lock(&mosq->out_packet_mutex); - if(mosq->out_packet){ - mosq->out_packet_last->next = packet; - }else{ - mosq->out_packet = packet; - } - mosq->out_packet_last = packet; - mosq->out_packet_count++; - pthread_mutex_unlock(&mosq->out_packet_mutex); + packet__queue_append(mosq, packet); lws_callback_on_writable(mosq->wsi); return MOSQ_ERR_SUCCESS; @@ -173,14 +181,7 @@ int packet__queue(struct mosquitto *mosq, struct mosquitto__packet *packet) packet->to_process = packet->packet_length - WS_PACKET_OFFSET; } - pthread_mutex_lock(&mosq->out_packet_mutex); - if(mosq->out_packet){ - mosq->out_packet_last->next = packet; - }else{ - mosq->out_packet = packet; - } - mosq->out_packet_last = packet; - pthread_mutex_unlock(&mosq->out_packet_mutex); + packet__queue_append(mosq, packet); #ifdef WITH_BROKER return packet__write(mosq); @@ -225,11 +226,13 @@ struct mosquitto__packet *packet__get_next_out(struct mosquitto *mosq) pthread_mutex_lock(&mosq->out_packet_mutex); if(mosq->out_packet){ + mosq->out_packet_count--; + mosq->out_packet_bytes -= mosq->out_packet->packet_length; + mosq->out_packet = mosq->out_packet->next; if(!mosq->out_packet){ mosq->out_packet_last = NULL; } - mosq->out_packet_count--; packet = mosq->out_packet; } pthread_mutex_unlock(&mosq->out_packet_mutex); diff --git a/src/bridge.c b/src/bridge.c index 793a15fc..9bb2ff1d 100644 --- a/src/bridge.c +++ b/src/bridge.c @@ -848,6 +848,7 @@ static void bridge__packet_cleanup(struct mosquitto *context) context->out_packet = NULL; context->out_packet_last = NULL; context->out_packet_count = 0; + context->out_packet_bytes = 0; packet__cleanup(&(context->in_packet)); } diff --git a/src/context.c b/src/context.c index 9d3a07ec..e078acc3 100644 --- a/src/context.c +++ b/src/context.c @@ -99,6 +99,7 @@ struct mosquitto *context__init(void) packet__cleanup(&context->in_packet); context->out_packet = NULL; context->out_packet_count = 0; + context->out_packet_bytes = 0; context->address = NULL; context->bridge = NULL; @@ -164,6 +165,7 @@ void context__cleanup(struct mosquitto *context, bool force_free) mosquitto__FREE(packet); } context->out_packet_count = 0; + context->out_packet_bytes = 0; #if defined(WITH_BROKER) && defined(__GLIBC__) && defined(WITH_ADNS) if(context->adns){ gai_cancel(context->adns); diff --git a/src/xtreport.c b/src/xtreport.c index a9093b06..67334abf 100644 --- a/src/xtreport.c +++ b/src/xtreport.c @@ -43,13 +43,7 @@ static void client_cost(FILE *fptr, struct mosquitto *context, int fn_index) long tBytes; pkt_count = 1; - pkt_bytes = context->in_packet.packet_length; - pkt_tmp = context->out_packet; - while(pkt_tmp){ - pkt_count++; - pkt_bytes += pkt_tmp->packet_length; - pkt_tmp = pkt_tmp->next; - } + pkt_bytes = context->in_packet.packet_length + context->out_packet_bytes; cmsg_count = context->msgs_in.inflight_count + context->msgs_in.queued_count; cmsg_bytes = context->msgs_in.inflight_bytes + context->msgs_in.queued_bytes; From 29f49bf6abf17446b1c22ea9f1562249dcc2d651 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 2 Jun 2022 23:23:56 +0100 Subject: [PATCH 06/20] Publish global out_packet values to $SYS --- lib/packet_mosq.c | 10 ++++++++++ man/mosquitto.8.xml | 22 ++++++++++++++++++++++ src/bridge.c | 3 +++ src/context.c | 3 +++ src/sys_tree.c | 16 ++++++++++++++++ src/sys_tree.h | 10 ++++++++++ 6 files changed, 64 insertions(+) diff --git a/lib/packet_mosq.c b/lib/packet_mosq.c index 41764f5e..d5ef8792 100644 --- a/lib/packet_mosq.c +++ b/lib/packet_mosq.c @@ -46,6 +46,10 @@ Contributors: # define G_BYTES_SENT_INC(A) # define G_MSGS_SENT_INC(A) # define G_PUB_MSGS_SENT_INC(A) +# define G_OUT_PACKET_COUNT_INC(A) +# define G_OUT_PACKET_COUNT_DEC(A) +# define G_OUT_PACKET_BYTES_INC(A) +# define G_OUT_PACKET_BYTES_DEC(A) #endif int packet__alloc(struct mosquitto__packet **packet, uint8_t command, uint32_t remaining_length) @@ -120,6 +124,8 @@ void packet__cleanup_all_no_locks(struct mosquitto *mosq) mosquitto__FREE(packet); } + G_OUT_PACKET_COUNT_DEC(mosq->out_packet_count); + G_OUT_PACKET_BYTES_DEC(mosq->out_packet_bytes); mosq->out_packet_count = 0; mosq->out_packet_bytes = 0; mosq->out_packet_last = NULL; @@ -146,6 +152,8 @@ static void packet__queue_append(struct mosquitto *mosq, struct mosquitto__packe mosq->out_packet_last = packet; mosq->out_packet_count++; mosq->out_packet_bytes += packet->packet_length; + G_OUT_PACKET_COUNT_INC(1); + G_OUT_PACKET_BYTES_INC(packet->packet_length); pthread_mutex_unlock(&mosq->out_packet_mutex); } @@ -228,6 +236,8 @@ struct mosquitto__packet *packet__get_next_out(struct mosquitto *mosq) if(mosq->out_packet){ mosq->out_packet_count--; mosq->out_packet_bytes -= mosq->out_packet->packet_length; + G_OUT_PACKET_COUNT_DEC(1); + G_OUT_PACKET_BYTES_DEC(mosq->out_packet->packet_length); mosq->out_packet = mosq->out_packet->next; if(!mosq->out_packet){ diff --git a/man/mosquitto.8.xml b/man/mosquitto.8.xml index 7d358a79..3696c17a 100644 --- a/man/mosquitto.8.xml +++ b/man/mosquitto.8.xml @@ -556,6 +556,28 @@ The total number of messages of any type sent since the broker started. + + + + + The current number of packets queued for delivery across + all clients. A large and increasing value here may + indicate messages are being sent faster than the network + can handle. + + + + + + + + The current number of bytes in packets queued for + delivery across all clients. A large and increasing + value here may indicate messages are being sent faster + than the network can handle. + + + diff --git a/src/bridge.c b/src/bridge.c index 9bb2ff1d..7370cc9d 100644 --- a/src/bridge.c +++ b/src/bridge.c @@ -49,6 +49,7 @@ Contributors: #include "memory_mosq.h" #include "packet_mosq.h" #include "send_mosq.h" +#include "sys_tree.h" #include "time_mosq.h" #include "tls_mosq.h" #include "util_mosq.h" @@ -847,6 +848,8 @@ static void bridge__packet_cleanup(struct mosquitto *context) } context->out_packet = NULL; context->out_packet_last = NULL; + G_OUT_PACKET_COUNT_DEC(context->out_packet_count); + G_OUT_PACKET_BYTES_DEC(context->out_packet_bytes); context->out_packet_count = 0; context->out_packet_bytes = 0; diff --git a/src/context.c b/src/context.c index e078acc3..1aad869b 100644 --- a/src/context.c +++ b/src/context.c @@ -29,6 +29,7 @@ Contributors: #include "memory_mosq.h" #include "packet_mosq.h" #include "property_mosq.h" +#include "sys_tree.h" #include "time_mosq.h" #include "util_mosq.h" #include "will_mosq.h" @@ -164,6 +165,8 @@ void context__cleanup(struct mosquitto *context, bool force_free) context->out_packet = context->out_packet->next; mosquitto__FREE(packet); } + G_OUT_PACKET_COUNT_DEC(context->out_packet_count); + G_OUT_PACKET_BYTES_DEC(context->out_packet_bytes); context->out_packet_count = 0; context->out_packet_bytes = 0; #if defined(WITH_BROKER) && defined(__GLIBC__) && defined(WITH_ADNS) diff --git a/src/sys_tree.c b/src/sys_tree.c index bd03787f..314c80b5 100644 --- a/src/sys_tree.c +++ b/src/sys_tree.c @@ -37,11 +37,13 @@ uint64_t g_bytes_received = 0; uint64_t g_bytes_sent = 0; uint64_t g_pub_bytes_received = 0; uint64_t g_pub_bytes_sent = 0; +int64_t g_out_packet_bytes = 0; unsigned long g_msgs_received = 0; unsigned long g_msgs_sent = 0; unsigned long g_pub_msgs_received = 0; unsigned long g_pub_msgs_sent = 0; unsigned long g_msgs_dropped = 0; +long g_out_packet_count = 0; unsigned int g_clients_expired = 0; unsigned int g_socket_connections = 0; unsigned int g_connection_count = 0; @@ -179,6 +181,8 @@ void sys_tree__update(void) static int subscription_count = INT_MAX; static int shared_subscription_count = INT_MAX; static int retained_count = INT_MAX; + static long out_packet_count = LONG_MAX; + static long long out_packet_bytes = LLONG_MAX; static double msgs_received_load1 = 0; static double msgs_received_load5 = 0; @@ -391,6 +395,18 @@ void sys_tree__update(void) db__messages_easy_queue(NULL, "$SYS/broker/publish/bytes/sent", SYS_TREE_QOS, len, buf, 1, 60, NULL); } + if(out_packet_count != g_out_packet_count){ + out_packet_count = g_out_packet_count; + len = (uint32_t)snprintf(buf, BUFLEN, "%llu", out_packet_count); + db__messages_easy_queue(NULL, "$SYS/broker/packet/out/count", SYS_TREE_QOS, len, buf, 1, 60, NULL); + } + + if(out_packet_bytes != g_out_packet_bytes){ + out_packet_bytes = g_out_packet_bytes; + len = (uint32_t)snprintf(buf, BUFLEN, "%llu", out_packet_bytes); + db__messages_easy_queue(NULL, "$SYS/broker/packet/out/bytes", SYS_TREE_QOS, len, buf, 1, 60, NULL); + } + last_update = db.now_s; } } diff --git a/src/sys_tree.h b/src/sys_tree.h index d0d91200..ed5e7a21 100644 --- a/src/sys_tree.h +++ b/src/sys_tree.h @@ -24,11 +24,13 @@ extern uint64_t g_bytes_received; extern uint64_t g_bytes_sent; extern uint64_t g_pub_bytes_received; extern uint64_t g_pub_bytes_sent; +extern int64_t g_out_packet_bytes; extern unsigned long g_msgs_received; extern unsigned long g_msgs_sent; extern unsigned long g_pub_msgs_received; extern unsigned long g_pub_msgs_sent; extern unsigned long g_msgs_dropped; +extern long g_out_packet_count; extern unsigned int g_clients_expired; extern unsigned int g_socket_connections; extern unsigned int g_connection_count; @@ -45,6 +47,10 @@ extern unsigned int g_connection_count; #define G_CLIENTS_EXPIRED_INC() (g_clients_expired++) #define G_SOCKET_CONNECTIONS_INC() (g_socket_connections++) #define G_CONNECTION_COUNT_INC() (g_connection_count++) +#define G_OUT_PACKET_COUNT_INC(A) (g_out_packet_count+=(A)) +#define G_OUT_PACKET_COUNT_DEC(A) (g_out_packet_count-=(A)) +#define G_OUT_PACKET_BYTES_INC(A) (g_out_packet_bytes+=(A)) +#define G_OUT_PACKET_BYTES_DEC(A) (g_out_packet_bytes-=(A)) #else @@ -60,6 +66,10 @@ extern unsigned int g_connection_count; #define G_CLIENTS_EXPIRED_INC() #define G_SOCKET_CONNECTIONS_INC() #define G_CONNECTION_COUNT_INC() +#define G_OUT_PACKET_COUNT_INC(A) +#define G_OUT_PACKET_COUNT_DEC(A) +#define G_OUT_PACKET_BYTES_INC(A) +#define G_OUT_PACKET_BYTES_DEC(A) #endif From bb33c503b6c62823656f407c649738e85db0161a Mon Sep 17 00:00:00 2001 From: Alex Martens Date: Sun, 5 Jun 2022 15:39:48 -0700 Subject: [PATCH 07/20] Fix install path to mosquittopp.h Signed-off-by: Alex Martens --- lib/cpp/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cpp/CMakeLists.txt b/lib/cpp/CMakeLists.txt index c7fe00c4..99a2eb28 100644 --- a/lib/cpp/CMakeLists.txt +++ b/lib/cpp/CMakeLists.txt @@ -69,4 +69,4 @@ if(WITH_STATIC_LIBRARIES) ) endif() -install(FILES mosquittopp.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") +install(FILES ../../mosquittopp.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") From 546df9db85578928dc6a6b617acb216714cf4355 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Tue, 14 Jun 2022 22:29:05 +0100 Subject: [PATCH 08/20] Build fixes, particularly pedantic compiler warnings. --- config.h | 2 +- lib/http_client.c | 1 + lib/mosquitto_internal.h | 4 ++-- lib/net_ws.c | 2 +- lib/options.c | 4 ++-- plugins/common/plugin_common.c | 2 ++ src/context.c | 2 +- src/loop.c | 4 ++-- src/mosquitto_broker_internal.h | 1 - src/plugin_public.c | 5 +++-- src/sys_tree.c | 2 +- 11 files changed, 16 insertions(+), 13 deletions(-) diff --git a/config.h b/config.h index 83d4adb0..280b78d7 100644 --- a/config.h +++ b/config.h @@ -88,7 +88,7 @@ #endif #define WS_IS_LWS 1 -#define WS_IS_WSLAY 2 +#define WS_IS_BUILTIN 2 #ifdef WITH_BROKER # ifdef __GNUC__ diff --git a/lib/http_client.c b/lib/http_client.c index 5b74dcdf..c4a32a58 100644 --- a/lib/http_client.c +++ b/lib/http_client.c @@ -26,6 +26,7 @@ Contributors: #include "mosquitto_internal.h" #include "base64_mosq.h" +#include "http_client.h" #include "memory_mosq.h" #include "mqtt_protocol.h" #include "net_mosq.h" diff --git a/lib/mosquitto_internal.h b/lib/mosquitto_internal.h index 5db31bc4..680e879b 100644 --- a/lib/mosquitto_internal.h +++ b/lib/mosquitto_internal.h @@ -243,7 +243,7 @@ struct mosquitto_msg_data{ #define WS_PING 0x09 #define WS_PONG 0x0A -#if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == LWS_IS_BUILTIN +#if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == WS_IS_BUILTIN struct ws_data{ struct mosquitto__packet *out_packet; char *http_path; @@ -281,7 +281,7 @@ struct mosquitto { struct gaicb *adns; /* For getaddrinfo_a */ #endif uint64_t last_cmsg_id; -#if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == LWS_IS_BUILTIN +#if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == WS_IS_BUILTIN struct ws_data wsd; #endif enum mosquitto__protocol protocol; diff --git a/lib/net_ws.c b/lib/net_ws.c index c13f9082..e645f722 100644 --- a/lib/net_ws.c +++ b/lib/net_ws.c @@ -204,7 +204,7 @@ static ssize_t read_ws_payloadlen_extended(struct mosquitto *mosq) } -ssize_t read_ws_mask(struct mosquitto *mosq) +static ssize_t read_ws_mask(struct mosquitto *mosq) { ssize_t len; diff --git a/lib/options.c b/lib/options.c index bbf190ff..f6fdd77b 100644 --- a/lib/options.c +++ b/lib/options.c @@ -504,7 +504,7 @@ int mosquitto_int_option(struct mosquitto *mosq, enum mosq_opt_t option, int val break; case MOSQ_OPT_TRANSPORT: -#if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == LWS_IS_BUILTIN +#if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == WS_IS_BUILTIN if(value == mosq_t_tcp || value == mosq_t_ws){ mosq->transport = (uint8_t)value; }else{ @@ -516,7 +516,7 @@ int mosquitto_int_option(struct mosquitto *mosq, enum mosq_opt_t option, int val break; case MOSQ_OPT_HTTP_HEADER_SIZE: -#if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == LWS_IS_BUILTIN +#if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == WS_IS_BUILTIN if(value < 100){ /* arbitrary limit */ return MOSQ_ERR_INVAL; }else if(mosq->http_request){ diff --git a/plugins/common/plugin_common.c b/plugins/common/plugin_common.c index 198edc43..f6ad3460 100644 --- a/plugins/common/plugin_common.c +++ b/plugins/common/plugin_common.c @@ -1,3 +1,5 @@ +#include "config.h" + #include "plugin_common.h" #include "json_help.h" #include diff --git a/src/context.c b/src/context.c index 1aad869b..75883a19 100644 --- a/src/context.c +++ b/src/context.c @@ -224,7 +224,7 @@ void context__disconnect(struct mosquitto *context) return; } -#if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == LWS_IS_BUILTIN +#if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == WS_IS_BUILTIN if(context->transport == mosq_t_ws){ uint8_t buf[4] = {0x88, 0x02, 0x03, context->wsd.disconnect_reason}; /* Send the disconnect reason, but don't care if it fails */ diff --git a/src/loop.c b/src/loop.c index 51f0acc1..a8b9ce0c 100644 --- a/src/loop.c +++ b/src/loop.c @@ -57,7 +57,7 @@ Contributors: extern int g_run; -#if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == WS_IS_WEBSOCKETS && LWS_LIBRARY_VERSION_NUMBER == 3002000 +#if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == WS_IS_LWS && LWS_LIBRARY_VERSION_NUMBER == 3002000 void lws__sul_callback(struct lws_sorted_usec_list *l) { } @@ -175,7 +175,7 @@ int mosquitto_main_loop(struct mosquitto__listener_sock *listensock, int listens int rc; -#if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == WS_IS_WEBSOCKETS && LWS_LIBRARY_VERSION_NUMBER == 3002000 +#if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == WS_IS_LWS && LWS_LIBRARY_VERSION_NUMBER == 3002000 memset(&sul, 0, sizeof(struct lws_sorted_usec_list)); #endif diff --git a/src/mosquitto_broker_internal.h b/src/mosquitto_broker_internal.h index f1e0b2c3..fb864c6a 100644 --- a/src/mosquitto_broker_internal.h +++ b/src/mosquitto_broker_internal.h @@ -977,7 +977,6 @@ int http__context_init(struct mosquitto *context); int http__context_cleanup(struct mosquitto *context); int http__read(struct mosquitto *context); int http__write(struct mosquitto *context); -void ws__context_init(struct mosquitto *context); #endif void do_disconnect(struct mosquitto *context, int reason); diff --git a/src/plugin_public.c b/src/plugin_public.c index 0da33858..1568911b 100644 --- a/src/plugin_public.c +++ b/src/plugin_public.c @@ -323,6 +323,7 @@ BROKER_EXPORT int mosquitto_set_clientid(struct mosquitto *client, const char *c struct mosquitto *found_client; char *id_dup; bool in_by_id; + int clientid_len; if(!client || !clientid) return MOSQ_ERR_INVAL; @@ -343,7 +344,7 @@ BROKER_EXPORT int mosquitto_set_clientid(struct mosquitto *client, const char *c } } - int clientid_len = (int)strlen(clientid); + clientid_len = (int)strlen(clientid); if(mosquitto_validate_utf8(clientid, clientid_len)){ return MOSQ_ERR_INVAL; } @@ -601,7 +602,7 @@ BROKER_EXPORT int mosquitto_persist_client_delete(const char *client_id) } -struct mosquitto_base_msg *find_store_msg(uint64_t store_id) +static struct mosquitto_base_msg *find_store_msg(uint64_t store_id) { struct mosquitto_base_msg *base_msg; diff --git a/src/sys_tree.c b/src/sys_tree.c index 314c80b5..599f5f38 100644 --- a/src/sys_tree.c +++ b/src/sys_tree.c @@ -397,7 +397,7 @@ void sys_tree__update(void) if(out_packet_count != g_out_packet_count){ out_packet_count = g_out_packet_count; - len = (uint32_t)snprintf(buf, BUFLEN, "%llu", out_packet_count); + len = (uint32_t)snprintf(buf, BUFLEN, "%lu", out_packet_count); db__messages_easy_queue(NULL, "$SYS/broker/packet/out/count", SYS_TREE_QOS, len, buf, 1, 60, NULL); } From a7304083f835f85e9ea555fe9fd03f580df500ef Mon Sep 17 00:00:00 2001 From: Kai Buschulte Date: Mon, 20 Jun 2022 11:18:36 +0200 Subject: [PATCH 09/20] throw BrokenPipeError if nothing received on sock instead of ignoring this error, we throw an exception the previous way might cause hard to find issues Signed-off-by: Kai Buschulte --- test/broker/01-connect-global-max-clients.py | 9 +++---- .../01-connect-global-max-connections.py | 9 +++---- test/broker/01-connect-max-connections.py | 9 +++---- test/broker/02-subpub-qos0-long-topic.py | 27 ++++++++++--------- test/broker/02-subscribe-invalid-utf8.py | 25 ++++++++--------- test/broker/02-subscribe-long-topic.py | 24 ++++++++--------- test/broker/03-publish-invalid-utf8.py | 25 ++++++++--------- test/broker/03-publish-long-topic.py | 24 ++++++++--------- .../03-publish-qos2-max-inflight-exceeded.py | 21 ++++++++++----- test/broker/07-will-delay-invalid-573191.py | 6 ++--- test/broker/07-will-invalid-utf8.py | 22 ++++++--------- test/broker/07-will-no-flag.py | 19 +++++-------- test/broker/07-will-null-topic.py | 23 ++++++---------- test/broker/09-extended-auth-single.py | 15 ++++++----- test/mosq_test.py | 2 +- 15 files changed, 120 insertions(+), 140 deletions(-) diff --git a/test/broker/01-connect-global-max-clients.py b/test/broker/01-connect-global-max-clients.py index 3ad021d0..3e105f32 100755 --- a/test/broker/01-connect-global-max-clients.py +++ b/test/broker/01-connect-global-max-clients.py @@ -37,7 +37,7 @@ def do_test(): # Try to open an 11th connection try: sock_bad = mosq_test.do_client_connect(connect_packet_bad, connack_packet_bad, port=port) - except ConnectionResetError: + except (ConnectionResetError, BrokenPipeError): # Expected behaviour pass @@ -50,7 +50,7 @@ def do_test(): # Try to open an 11th connection try: sock_bad = mosq_test.do_client_connect(connect_packet_bad, connack_packet_bad, port=port) - except ConnectionResetError: + except (ConnectionResetError, BrokenPipeError): # Expected behaviour pass @@ -66,7 +66,6 @@ def do_test(): (stdo, stde) = broker.communicate() if rc: print(stde.decode('utf-8')) - exit(rc) + return rc -do_test() -exit(0) +sys.exit(do_test()) diff --git a/test/broker/01-connect-global-max-connections.py b/test/broker/01-connect-global-max-connections.py index 860d33bb..065eb1a0 100755 --- a/test/broker/01-connect-global-max-connections.py +++ b/test/broker/01-connect-global-max-connections.py @@ -36,7 +36,7 @@ def do_test(): # Try to open an 11th connection try: sock_bad = mosq_test.do_client_connect(connect_packet_bad, connack_packet_bad, port=port) - except ConnectionResetError: + except (ConnectionResetError, BrokenPipeError): # Expected behaviour pass @@ -53,7 +53,7 @@ def do_test(): # Try to open an 11th connection try: sock_bad = mosq_test.do_client_connect(connect_packet_bad, connack_packet_bad, port=port) - except ConnectionResetError: + except (ConnectionResetError, BrokenPipeError): # Expected behaviour pass @@ -73,7 +73,6 @@ def do_test(): (stdo, stde) = broker.communicate() if rc: print(stde.decode('utf-8')) - exit(rc) + return rc -do_test() -exit(0) +sys.exit(do_test()) diff --git a/test/broker/01-connect-max-connections.py b/test/broker/01-connect-max-connections.py index a41fea13..61846757 100755 --- a/test/broker/01-connect-max-connections.py +++ b/test/broker/01-connect-max-connections.py @@ -36,7 +36,7 @@ def do_test(): # Try to open an 11th connection try: sock_bad = mosq_test.do_client_connect(connect_packet_bad, connack_packet_bad, port=port) - except ConnectionResetError: + except (ConnectionResetError, BrokenPipeError): # Expected behaviour pass @@ -53,7 +53,7 @@ def do_test(): # Try to open an 11th connection try: sock_bad = mosq_test.do_client_connect(connect_packet_bad, connack_packet_bad, port=port) - except ConnectionResetError: + except (ConnectionResetError, BrokenPipeError): # Expected behaviour pass @@ -73,7 +73,6 @@ def do_test(): (stdo, stde) = broker.communicate() if rc: print(stde.decode('utf-8')) - exit(rc) + return rc -do_test() -exit(0) +sys.exit(do_test()) diff --git a/test/broker/02-subpub-qos0-long-topic.py b/test/broker/02-subpub-qos0-long-topic.py index d9b9d2ec..68fe81f3 100755 --- a/test/broker/02-subpub-qos0-long-topic.py +++ b/test/broker/02-subpub-qos0-long-topic.py @@ -16,50 +16,51 @@ def do_test(start_broker, topic, succeeds): publish_packet = mosq_test.gen_publish(topic, qos=0, payload="message") port = mosq_test.get_port() + broker = None if start_broker: broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port) try: sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=20, port=port) - if succeeds == True: + if succeeds: mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback") mosq_test.do_send_receive(sock, publish_packet, publish_packet, "publish") else: - mosq_test.do_send_receive(sock, subscribe_packet, b"", "suback") + try: + mosq_test.do_send_receive(sock, subscribe_packet, b"", "suback") + return 1 + except BrokenPipeError: + pass rc = 0 sock.close() - except mosq_test.TestError: - pass finally: - if start_broker: + if broker: broker.terminate() broker.wait() (stdo, stde) = broker.communicate() if rc: print(stde.decode('utf-8')) - exit(rc) - else: - return rc + return rc def all_tests(start_broker=False): rc = do_test(start_broker, "/"*200, True) # 200 max hierarchy limit if rc: - return rc; + return rc rc = do_test(start_broker, "abc/"*199+"d", True) # 200 max hierarchy limit, longer overall string than 200 if rc: - return rc; + return rc rc = do_test(start_broker, "/"*201, False) # Exceeds 200 max hierarchy limit if rc: - return rc; + return rc rc = do_test(start_broker, "abc/"*201+"d", False) # Exceeds 200 max hierarchy limit, longer overall string than 200 if rc: - return rc; + return rc return 0 if __name__ == '__main__': - all_tests(True) + sys.exit(all_tests(True)) diff --git a/test/broker/02-subscribe-invalid-utf8.py b/test/broker/02-subscribe-invalid-utf8.py index 78b98835..bccf2d3d 100755 --- a/test/broker/02-subscribe-invalid-utf8.py +++ b/test/broker/02-subscribe-invalid-utf8.py @@ -15,46 +15,43 @@ def do_test(start_broker, proto_ver): b[13] = 0 # Topic should never have a 0x0000 subscribe_packet = struct.pack("B"*len(b), *b) - suback_packet = mosq_test.gen_suback(mid, 0, proto_ver=proto_ver) - port = mosq_test.get_port() + broker = None if start_broker: broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port) try: sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=port) if proto_ver == 4: - mosq_test.do_send_receive(sock, subscribe_packet, b"", "suback") + try: + mosq_test.do_send_receive(sock, subscribe_packet, b"", "suback") + except BrokenPipeError: + rc = 0 else: disconnect_packet = mosq_test.gen_disconnect(proto_ver=5, reason_code = mqtt5_rc.MQTT_RC_MALFORMED_PACKET) mosq_test.do_send_receive(sock, subscribe_packet, disconnect_packet, "suback") - - rc = 0 + rc = 0 sock.close() - except mosq_test.TestError: - pass finally: - if start_broker: + if broker: broker.terminate() broker.wait() (stdo, stde) = broker.communicate() if rc: print(stde.decode('utf-8')) print("proto_ver=%d" % (proto_ver)) - exit(rc) - else: - return rc + return rc def all_tests(start_broker=False): rc = do_test(start_broker, proto_ver=4) if rc: - return rc; + return rc rc = do_test(start_broker, proto_ver=5) if rc: - return rc; + return rc return 0 if __name__ == '__main__': - all_tests(True) + sys.exit(all_tests(True)) diff --git a/test/broker/02-subscribe-long-topic.py b/test/broker/02-subscribe-long-topic.py index 70fd8127..b53a178e 100755 --- a/test/broker/02-subscribe-long-topic.py +++ b/test/broker/02-subscribe-long-topic.py @@ -13,46 +13,44 @@ def do_test(start_broker, proto_ver): connack_packet = mosq_test.gen_connack(rc=0, proto_ver=proto_ver) subscribe_packet = mosq_test.gen_subscribe(mid, "/"*65535, 0, proto_ver=proto_ver) - suback_packet = mosq_test.gen_suback(mid, 0, proto_ver=proto_ver) port = mosq_test.get_port() + broker = None if start_broker: broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port) try: sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=port) if proto_ver == 4: - mosq_test.do_send_receive(sock, subscribe_packet, b"", "suback") + try: + mosq_test.do_send_receive(sock, subscribe_packet, b"", "suback") + except BrokenPipeError: + rc = 0 else: disconnect_packet = mosq_test.gen_disconnect(proto_ver=5, reason_code = mqtt5_rc.MQTT_RC_MALFORMED_PACKET) mosq_test.do_send_receive(sock, subscribe_packet, disconnect_packet, "suback") - - rc = 0 + rc = 0 sock.close() - except mosq_test.TestError: - pass finally: - if start_broker: + if broker: broker.terminate() broker.wait() (stdo, stde) = broker.communicate() if rc: print(stde.decode('utf-8')) print("proto_ver=%d" % (proto_ver)) - exit(rc) - else: - return rc + return rc def all_tests(start_broker=False): rc = do_test(start_broker, proto_ver=4) if rc: - return rc; + return rc rc = do_test(start_broker, proto_ver=5) if rc: - return rc; + return rc return 0 if __name__ == '__main__': - all_tests(True) + sys.exit(all_tests(True)) diff --git a/test/broker/03-publish-invalid-utf8.py b/test/broker/03-publish-invalid-utf8.py index e52bc7f8..ef5bc925 100755 --- a/test/broker/03-publish-invalid-utf8.py +++ b/test/broker/03-publish-invalid-utf8.py @@ -15,46 +15,43 @@ def do_test(start_broker, proto_ver): b[11] = 0 # Topic should never have a 0x0000 publish_packet = struct.pack("B"*len(b), *b) - puback_packet = mosq_test.gen_puback(mid, proto_ver=proto_ver) - port = mosq_test.get_port() + broker = None if start_broker: broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port) try: sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=port) if proto_ver == 4: - mosq_test.do_send_receive(sock, publish_packet, b"", "puback") + try: + mosq_test.do_send_receive(sock, publish_packet, b"", "puback") + except BrokenPipeError: + rc = 0 else: disconnect_packet = mosq_test.gen_disconnect(proto_ver=5, reason_code=mqtt5_rc.MQTT_RC_MALFORMED_PACKET) mosq_test.do_send_receive(sock, publish_packet, disconnect_packet, "puback") - - rc = 0 + rc = 0 sock.close() - except mosq_test.TestError: - pass finally: - if start_broker: + if broker: broker.terminate() broker.wait() (stdo, stde) = broker.communicate() if rc: print(stde.decode('utf-8')) print("proto_ver=%d" % (proto_ver)) - exit(rc) - else: - return rc + return rc def all_tests(start_broker=False): rc = do_test(start_broker, proto_ver=4) if rc: - return rc; + return rc rc = do_test(start_broker, proto_ver=5) if rc: - return rc; + return rc return 0 if __name__ == '__main__': - all_tests(True) + sys.exit(all_tests(True)) diff --git a/test/broker/03-publish-long-topic.py b/test/broker/03-publish-long-topic.py index bc6b989c..4f267826 100755 --- a/test/broker/03-publish-long-topic.py +++ b/test/broker/03-publish-long-topic.py @@ -14,46 +14,44 @@ def do_test(start_broker, proto_ver): connack_packet = mosq_test.gen_connack(rc=0, proto_ver=proto_ver) publish_packet = mosq_test.gen_publish("/"*65535, qos=1, mid=mid, payload="message", proto_ver=proto_ver) - puback_packet = mosq_test.gen_puback(mid, proto_ver=proto_ver) port = mosq_test.get_port() + broker = None if start_broker: broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port) try: sock = mosq_test.do_client_connect(connect_packet, connack_packet, port=port) if proto_ver == 4: - mosq_test.do_send_receive(sock, publish_packet, b"", "puback") + try: + mosq_test.do_send_receive(sock, publish_packet, b"", "puback") + except BrokenPipeError: + rc = 0 else: disconnect_packet = mosq_test.gen_disconnect(proto_ver=5, reason_code=mqtt5_rc.MQTT_RC_MALFORMED_PACKET) mosq_test.do_send_receive(sock, publish_packet, disconnect_packet, "puback") - - rc = 0 + rc = 0 sock.close() - except mosq_test.TestError: - pass finally: - if start_broker: + if broker: broker.terminate() broker.wait() (stdo, stde) = broker.communicate() if rc: print(stde.decode('utf-8')) print("proto_ver=%d" % (proto_ver)) - exit(rc) - else: - return rc + return rc def all_tests(start_broker=False): rc = do_test(start_broker, proto_ver=4) if rc: - return rc; + return rc rc = do_test(start_broker, proto_ver=5) if rc: - return rc; + return rc return 0 if __name__ == '__main__': - all_tests(True) + sys.exit(all_tests(True)) diff --git a/test/broker/03-publish-qos2-max-inflight-exceeded.py b/test/broker/03-publish-qos2-max-inflight-exceeded.py index 4433f8a7..ac4a6c2c 100755 --- a/test/broker/03-publish-qos2-max-inflight-exceeded.py +++ b/test/broker/03-publish-qos2-max-inflight-exceeded.py @@ -27,13 +27,14 @@ def do_test(proto_ver): disconnect_packet = mosq_test.gen_disconnect(reason_code=mqtt5_rc.MQTT_RC_RECEIVE_MAXIMUM_EXCEEDED, proto_ver=proto_ver) else: disconnect_packet = b"" - mosq_test.do_send_receive(sock, publish_packet, disconnect_packet, "disconnect") + try: + mosq_test.do_send_receive(sock, publish_packet, disconnect_packet, "disconnect") + except BrokenPipeError: + pass rc = 0 sock.close() - except mosq_test.TestError: - pass finally: broker.terminate() broker.wait() @@ -41,9 +42,15 @@ def do_test(proto_ver): if rc: print(stde.decode('utf-8')) print("proto_ver=%d" % (proto_ver)) - exit(rc) + return rc -do_test(proto_ver=4) -do_test(proto_ver=5) -exit(0) +def all_test(): + rc = do_test(proto_ver=4) + if rc: + return rc + rc = do_test(proto_ver=5) + return rc + +if __name__ == "__main__": + sys.exit(all_test()) diff --git a/test/broker/07-will-delay-invalid-573191.py b/test/broker/07-will-delay-invalid-573191.py index 4c44a174..102c6ae0 100755 --- a/test/broker/07-will-delay-invalid-573191.py +++ b/test/broker/07-will-delay-invalid-573191.py @@ -8,7 +8,6 @@ from mosq_test_helper import * def do_test(): rc = 1 - mid = 1 props = mqtt5_props.gen_uint32_prop(mqtt5_props.PROP_WILL_DELAY_INTERVAL, 3) connect_packet = mosq_test.gen_connect("will-573191-test", proto_ver=5, will_topic="", will_properties=props) connack_packet = b"" @@ -19,6 +18,7 @@ def do_test(): try: sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=30, port=port) sock.close() + except BrokenPipeError: rc = 0 finally: broker.terminate() @@ -26,6 +26,6 @@ def do_test(): (stdo, stde) = broker.communicate() if rc: print(stde.decode('utf-8')) - exit(rc) + return rc -do_test() +sys.exit(do_test()) diff --git a/test/broker/07-will-invalid-utf8.py b/test/broker/07-will-invalid-utf8.py index 7abcd3b4..750a4158 100755 --- a/test/broker/07-will-invalid-utf8.py +++ b/test/broker/07-will-invalid-utf8.py @@ -6,7 +6,6 @@ from mosq_test_helper import * def do_test(start_broker, proto_ver): rc = 1 - mid = 53 connect_packet = mosq_test.gen_connect("will-invalid-utf8", will_topic="will/invalid/utf8", proto_ver=proto_ver) b = list(struct.unpack("B"*len(connect_packet), connect_packet)) @@ -14,36 +13,31 @@ def do_test(start_broker, proto_ver): connect_packet = struct.pack("B"*len(b), *b) port = mosq_test.get_port() + broker = None if start_broker: broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port) try: sock = mosq_test.do_client_connect(connect_packet, b"", timeout=30, port=port) - rc = 0 sock.close() - except mosq_test.TestError: - pass + except BrokenPipeError: + rc = 0 finally: - if start_broker: + if broker: broker.terminate() broker.wait() (stdo, stde) = broker.communicate() if rc: print(stde.decode('utf-8')) print("proto_ver=%d" % (proto_ver)) - exit(rc) - else: - return rc + return rc def all_tests(start_broker=False): rc = do_test(start_broker, proto_ver=4) if rc: - return rc; - rc = do_test(start_broker, proto_ver=5) - if rc: - return rc; - return 0 + return rc + return do_test(start_broker, proto_ver=5) if __name__ == '__main__': - all_tests(True) + sys.exit(all_tests(True)) diff --git a/test/broker/07-will-no-flag.py b/test/broker/07-will-no-flag.py index 8173854a..0464bd38 100755 --- a/test/broker/07-will-no-flag.py +++ b/test/broker/07-will-no-flag.py @@ -16,36 +16,31 @@ def do_test(start_broker, proto_ver): connect_packet = struct.pack("B"*len(bmod), *bmod) port = mosq_test.get_port() + broker = None if start_broker: broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port) try: sock = mosq_test.do_client_connect(connect_packet, b"", port=port) sock.close() + except BrokenPipeError: rc = 0 - except mosq_test.TestError: - pass finally: - if start_broker: + if broker: broker.terminate() broker.wait() (stdo, stde) = broker.communicate() if rc: print(stde.decode('utf-8')) print("proto_ver=%d" % (proto_ver)) - exit(rc) - else: - return rc + return rc def all_tests(start_broker=False): rc = do_test(start_broker, proto_ver=4) if rc: - return rc; - rc = do_test(start_broker, proto_ver=5) - if rc: - return rc; - return 0 + return rc + return do_test(start_broker, proto_ver=5) if __name__ == '__main__': - all_tests(True) + sys.exit(all_tests(True)) diff --git a/test/broker/07-will-null-topic.py b/test/broker/07-will-null-topic.py index 3b1392e7..4dc3b389 100755 --- a/test/broker/07-will-null-topic.py +++ b/test/broker/07-will-null-topic.py @@ -7,43 +7,36 @@ from mosq_test_helper import * def do_test(start_broker, proto_ver): rc = 1 connect_packet = mosq_test.gen_connect("will-null-topic", will_topic="", will_payload=struct.pack("!4sB7s", b"will", 0, b"message"), proto_ver=proto_ver) - connack_packet = mosq_test.gen_connack(rc=2, proto_ver=proto_ver) port = mosq_test.get_port() + broker = None if start_broker: broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port) try: sock = mosq_test.do_client_connect(connect_packet, b"", timeout=30, port=port) - rc = 0 sock.close() - except socket.error as e: - if e.errno == errno.ECONNRESET: - # Connection has been closed by peer, this is the expected behaviour - rc = 0 - except mosq_test.TestError: - pass + except BrokenPipeError: + rc = 0 finally: - if start_broker: + if broker: broker.terminate() broker.wait() (stdo, stde) = broker.communicate() if rc: print(stde.decode('utf-8')) print("proto_ver=%d" % (proto_ver)) - exit(rc) - else: - return rc + return rc def all_tests(start_broker=False): rc = do_test(start_broker, proto_ver=4) if rc: - return rc; + return rc rc = do_test(start_broker, proto_ver=5) if rc: - return rc; + return rc return 0 if __name__ == '__main__': - all_tests(True) + sys.exit(all_tests(True)) diff --git a/test/broker/09-extended-auth-single.py b/test/broker/09-extended-auth-single.py index 55df2a81..fd0d1db3 100755 --- a/test/broker/09-extended-auth-single.py +++ b/test/broker/09-extended-auth-single.py @@ -54,9 +54,15 @@ connack5_packet = mosq_test.gen_connack(rc=0, proto_ver=5, properties=props) broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port) + try: - sock = mosq_test.do_client_connect(connect1_packet, b"", timeout=20, port=port) - sock.close() + sock = None + try: + sock = mosq_test.do_client_connect(connect1_packet, b"", timeout=20, port=port) + sock.close() + rc = 2 + except BrokenPipeError: + pass sock = mosq_test.do_client_connect(connect2_packet, connack2_packet, timeout=20, port=port) sock.close() @@ -71,8 +77,6 @@ try: sock.close() rc = 0 -except mosq_test.TestError: - pass finally: os.remove(conf_file) broker.terminate() @@ -82,5 +86,4 @@ finally: print(stde.decode('utf-8')) -exit(rc) - +sys.exit(rc) diff --git a/test/mosq_test.py b/test/mosq_test.py index c1cb1d0f..c0bb89aa 100644 --- a/test/mosq_test.py +++ b/test/mosq_test.py @@ -119,7 +119,7 @@ def expect_packet(sock, name, expected): while len(packet_recvd) < rlen: data = sock.recv(rlen-len(packet_recvd)) if len(data) == 0: - break + raise BrokenPipeError(f"when reading {name} from {sock.getpeername()}") packet_recvd += data except socket.timeout: pass From 34391080d61d78a7b507f449e0f0f7158df6df97 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Wed, 22 Jun 2022 17:33:39 +0100 Subject: [PATCH 10/20] Add dynsec init by simple file. --- ChangeLog.txt | 2 + plugins/dynamic-security/config.c | 6 +- plugins/dynamic-security/config_init.c | 133 +++++++++++++++----- plugins/dynamic-security/dynamic_security.h | 10 +- plugins/dynamic-security/plugin.c | 10 +- test/broker/14-dynsec-config-init-env.py | 56 +++++++++ test/broker/14-dynsec-config-init-file.py | 57 +++++++++ test/broker/14-dynsec-config-init-random.py | 79 ++++++++++++ test/broker/Makefile | 9 +- test/broker/test.py | 9 +- test/mosq_test.py | 6 +- www/pages/documentation/dynamic-security.md | 51 ++++++-- 12 files changed, 374 insertions(+), 54 deletions(-) create mode 100755 test/broker/14-dynsec-config-init-env.py create mode 100755 test/broker/14-dynsec-config-init-file.py create mode 100755 test/broker/14-dynsec-config-init-random.py diff --git a/ChangeLog.txt b/ChangeLog.txt index 9865d2cc..fb090ad7 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -86,6 +86,8 @@ Plugins / plugin interface: - The dynamic security plugin now supports `%c` and `%u` patterns for substituting client id and username respectively, in all ACLs except for subscribeLiteral and unsubscribeLiteral. +- The dynamic security plugin now supports multiple ways to initialise the + first configuration file. - Add `mosquitto_sub_matches_acl()`, which can match one topic filter (a subscription) against another topic filter (an ACL). - Registration of the MOSQ_EVT_CONTROL plugin event is now handled globally diff --git a/plugins/dynamic-security/config.c b/plugins/dynamic-security/config.c index 64995c44..dd3823a4 100644 --- a/plugins/dynamic-security/config.c +++ b/plugins/dynamic-security/config.c @@ -98,7 +98,7 @@ int dynsec__config_from_json(struct dynsec__data *data, const char *json_str) tree = cJSON_Parse(json_str); if(tree == NULL){ - mosquitto_log_printf(MOSQ_LOG_ERR, "Error loading Dynamic security plugin config: File is not valid JSON.\n"); + mosquitto_log_printf(MOSQ_LOG_ERR, "Error loading Dynamic security plugin config: File is not valid JSON."); return 1; } @@ -128,9 +128,7 @@ int dynsec__config_load(struct dynsec__data *data) fptr = fopen(data->config_file, "rb"); if(fptr == NULL){ /* Attempt to initialise a new config file */ - if(dynsec__config_init(data->config_file) == MOSQ_ERR_SUCCESS){ - mosquitto_log_printf(MOSQ_LOG_INFO, "Dynamic security plugin config not found, generating a default config."); - mosquitto_log_printf(MOSQ_LOG_INFO, " Generated passwords are at %s.pw", data->config_file); + if(dynsec__config_init(data) == MOSQ_ERR_SUCCESS){ /* If it works, try to open the file again */ fptr = fopen(data->config_file, "rb"); } diff --git a/plugins/dynamic-security/config_init.c b/plugins/dynamic-security/config_init.c index c7248463..9a72b8bd 100644 --- a/plugins/dynamic-security/config_init.c +++ b/plugins/dynamic-security/config_init.c @@ -19,6 +19,7 @@ Contributors: #include "config.h" #include +#include #include #include #include @@ -63,7 +64,56 @@ static int add_default_access(cJSON *j_tree) } -static int generate_password(int iterations, char **password, char **password_hash, char **salt) +static int get_password_from_init_file(struct dynsec__data *data, char **pw) +{ + FILE *fptr; + char buf[1024]; + int pos; + + if(data->password_init_file == NULL){ + *pw = NULL; + return MOSQ_ERR_SUCCESS; + } + fptr = fopen(data->password_init_file, "rt"); + if(!fptr){ + mosquitto_log_printf(MOSQ_LOG_ERR, "Error: Unable to get initial password from '%s', file not accessible.", data->password_init_file); + return MOSQ_ERR_INVAL; + } + if(!fgets(buf, sizeof(buf), fptr)){ + fclose(fptr); + mosquitto_log_printf(MOSQ_LOG_ERR, "Error: Unable to get initial password from '%s', file empty.", data->password_init_file); + return MOSQ_ERR_INVAL; + } + fclose(fptr); + + pos = (int)strlen(buf)-1; + while(pos >= 0 && isspace(buf[pos])){ + buf[pos] = '\0'; + pos--; + } + if(strlen(buf) == 0){ + mosquitto_log_printf(MOSQ_LOG_ERR, "Error: Unable to get initial password from '%s', password is empty.", data->password_init_file); + return MOSQ_ERR_INVAL; + } + *pw = strdup(buf); + if(!*pw){ + mosquitto_log_printf(MOSQ_LOG_ERR, "Error: Unable to get initial password from '%s', out of memory.", data->password_init_file); + return MOSQ_ERR_NOMEM; + }else{ + return MOSQ_ERR_SUCCESS; + } +} + + +/* Generate a password for the admin user + * + * Uses passwords from, in order: + * + * * The password defined in the plugin_opt_password_init_file file + * * The contents of the MOSQUITTO_DYNSEC_PASSWORD environment variable + * * Randomly generated passwords for "admin", "user", stored in plain text at '.pw' + */ +static int generate_password(struct dynsec__data *data, int iterations, char **password, char **password_hash, char **salt) { struct mosquitto_pw pw; int i; @@ -75,9 +125,13 @@ static int generate_password(int iterations, char **password, char **password_ha memset(&pw, 0, sizeof(struct mosquitto_pw)); pw.hashtype = pw_sha512_pbkdf2; - pwenv = getenv("MOSQUITTO_DYNSEC_PASSWORD"); - if(pwenv){ - if(strlen(pwenv) < 12){ + if(data->init_mode == dpwim_file){ + if(get_password_from_init_file(data, password)){ + return MOSQ_ERR_INVAL; + } + }else if(data->init_mode == dpwim_env){ + pwenv = getenv("MOSQUITTO_DYNSEC_PASSWORD"); + if(pwenv == NULL || strlen(pwenv) < 12){ mosquitto_log_printf(MOSQ_LOG_ERR, "Error: Not generating dynsec config, MOSQUITTO_DYNSEC_PASSWORD must be at least 12 characters."); return MOSQ_ERR_INVAL; } @@ -140,14 +194,14 @@ static int client_role_add(cJSON *j_roles, const char *rolename) } -static int client_add_admin(FILE *pwfile, cJSON *j_clients) +static int client_add_admin(struct dynsec__data *data, FILE *pwfile, cJSON *j_clients) { cJSON *j_client, *j_roles; char *password = NULL; char *password_hash = NULL; char *salt = NULL; - if(generate_password(10000, &password, &password_hash, &salt)){ + if(generate_password(data, 10000, &password, &password_hash, &salt)){ return MOSQ_ERR_UNKNOWN; } @@ -181,23 +235,25 @@ static int client_add_admin(FILE *pwfile, cJSON *j_clients) return MOSQ_ERR_NOMEM; } - fprintf(pwfile, "admin %s\n", password); + if(data->init_mode == dpwim_random){ + fprintf(pwfile, "admin %s\n", password); + } free(password); return MOSQ_ERR_SUCCESS; } -static int client_add_user(FILE *pwfile, cJSON *j_clients) +static int client_add_user(struct dynsec__data *data, FILE *pwfile, cJSON *j_clients) { cJSON *j_client, *j_roles; char *password = NULL; char *password_hash = NULL; char *salt = NULL; - if(getenv("MOSQUITTO_DYNSEC_PASSWORD")){ + if(data->init_mode != dpwim_random){ return MOSQ_ERR_SUCCESS; } - if(generate_password(10000, &password, &password_hash, &salt)){ + if(generate_password(data, 10000, &password, &password_hash, &salt)){ return MOSQ_ERR_UNKNOWN; } @@ -234,40 +290,42 @@ static int client_add_user(FILE *pwfile, cJSON *j_clients) return MOSQ_ERR_SUCCESS; } -static int add_clients(const char *filename, cJSON *j_tree) +static int add_clients(struct dynsec__data *data, cJSON *j_tree) { cJSON *j_clients; char *pwfile; size_t len; - FILE *fptr; + FILE *fptr = NULL; - len = strlen(filename) + 5; - pwfile = malloc(len); - if(pwfile == NULL){ - return MOSQ_ERR_NOMEM; - } - snprintf(pwfile, len, "%s.pw", filename); - fptr = mosquitto__fopen(pwfile, "wb", true); - free(pwfile); - if(fptr == NULL){ - return MOSQ_ERR_UNKNOWN; + if(data->init_mode == dpwim_random){ + len = strlen(data->config_file) + 5; + pwfile = malloc(len); + if(pwfile == NULL){ + return MOSQ_ERR_NOMEM; + } + snprintf(pwfile, len, "%s.pw", data->config_file); + fptr = mosquitto__fopen(pwfile, "wb", true); + free(pwfile); + if(fptr == NULL){ + return MOSQ_ERR_UNKNOWN; + } } j_clients = cJSON_AddArrayToObject(j_tree, "clients"); if(j_clients == NULL){ - fclose(fptr); + if(fptr) fclose(fptr); return MOSQ_ERR_NOMEM; } - if(client_add_admin(fptr, j_clients) - || client_add_user(fptr, j_clients) + if(client_add_admin(data, fptr, j_clients) + || client_add_user(data, fptr, j_clients) ){ - fclose(fptr); + if(fptr) fclose(fptr); return MOSQ_ERR_NOMEM; } - fclose(fptr); + if(fptr) fclose(fptr); return MOSQ_ERR_SUCCESS; } @@ -416,7 +474,7 @@ static int role_add_topic_observe(cJSON *j_roles) if(cJSON_AddStringToObject(j_role, "rolename", "topic-observe") == NULL || cJSON_AddStringToObject(j_role, "textdescription", - "Read/write access to the full application topic hierarchy.") == NULL + "Read only access to the full application topic hierarchy.") == NULL || (j_acls = cJSON_AddArrayToObject(j_role, "acls")) == NULL ){ @@ -455,19 +513,32 @@ static int add_roles(cJSON *j_tree) } -int dynsec__config_init(const char *filename) +int dynsec__config_init(struct dynsec__data *data) { FILE *fptr; cJSON *j_tree; char *json_str; + mosquitto_log_printf(MOSQ_LOG_INFO, "Dynamic security plugin config not found, generating a default config."); + + if(data->password_init_file){ + mosquitto_log_printf(MOSQ_LOG_INFO, " Using admin password from file '%s'", data->password_init_file); + data->init_mode = dpwim_file; + }else if(getenv("MOSQUITTO_DYNSEC_PASSWORD")){ + mosquitto_log_printf(MOSQ_LOG_INFO, " Using admin password from MOSQUITTO_DYNSEC_PASSWORD environment variable"); + data->init_mode = dpwim_env; + }else{ + mosquitto_log_printf(MOSQ_LOG_INFO, " Generated passwords are at %s.pw", data->config_file); + data->init_mode = dpwim_random; + } + j_tree = cJSON_CreateObject(); if(j_tree == NULL){ return MOSQ_ERR_NOMEM; } if(add_default_access(j_tree) != MOSQ_ERR_SUCCESS - || add_clients(filename, j_tree) != MOSQ_ERR_SUCCESS + || add_clients(data, j_tree) != MOSQ_ERR_SUCCESS || add_groups(j_tree) != MOSQ_ERR_SUCCESS || add_roles(j_tree) != MOSQ_ERR_SUCCESS || cJSON_AddStringToObject(j_tree, "anonymousGroup", "unauthenticated") == NULL @@ -483,7 +554,7 @@ int dynsec__config_init(const char *filename) return MOSQ_ERR_NOMEM; } - fptr = mosquitto__fopen(filename, "wb", true); + fptr = mosquitto__fopen(data->config_file, "wb", true); if(fptr == NULL){ return MOSQ_ERR_UNKNOWN; } diff --git a/plugins/dynamic-security/dynamic_security.h b/plugins/dynamic-security/dynamic_security.h index 02bcf63f..5ff8bc2b 100644 --- a/plugins/dynamic-security/dynamic_security.h +++ b/plugins/dynamic-security/dynamic_security.h @@ -136,14 +136,22 @@ struct dynsec__acl_default_access{ bool unsubscribe; }; +enum dynsec_pw_init_mode{ + dpwim_file = 1, + dpwim_env = 2, + dpwim_random = 3, +}; + struct dynsec__data{ char *config_file; + char *password_init_file; struct dynsec__client *clients; struct dynsec__group *groups; struct dynsec__role *roles; struct dynsec__group *anonymous_group; struct dynsec__kicklist *kicklist; struct dynsec__acl_default_access default_access; + int init_mode; }; /* ################################################################ @@ -152,7 +160,7 @@ struct dynsec__data{ * # * ################################################################ */ -int dynsec__config_init(const char *filename); +int dynsec__config_init(struct dynsec__data *data); void dynsec__config_save(struct dynsec__data *data); int dynsec__config_load(struct dynsec__data *data); char *dynsec__config_to_json(struct dynsec__data *data); diff --git a/plugins/dynamic-security/plugin.c b/plugins/dynamic-security/plugin.c index 216ccbb7..788efacd 100644 --- a/plugins/dynamic-security/plugin.c +++ b/plugins/dynamic-security/plugin.c @@ -50,7 +50,11 @@ int mosquitto_plugin_init(mosquitto_plugin_id_t *identifier, void **user_data, s if(dynsec_data.config_file == NULL){ return MOSQ_ERR_NOMEM; } - break; + }else if(!strcasecmp(options[i].key, "password_init_file")){ + dynsec_data.password_init_file = mosquitto_strdup(options[i].value); + if(dynsec_data.password_init_file == NULL){ + return MOSQ_ERR_NOMEM; + } } } if(dynsec_data.config_file == NULL){ @@ -82,5 +86,9 @@ int mosquitto_plugin_cleanup(void *user_data, struct mosquitto_opt *options, int mosquitto_free(dynsec_data.config_file); dynsec_data.config_file = NULL; + + mosquitto_free(dynsec_data.password_init_file); + dynsec_data.password_init_file = NULL; + return MOSQ_ERR_SUCCESS; } diff --git a/test/broker/14-dynsec-config-init-env.py b/test/broker/14-dynsec-config-init-env.py new file mode 100755 index 00000000..a23b8029 --- /dev/null +++ b/test/broker/14-dynsec-config-init-env.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 + +from mosq_test_helper import * +import json +import shutil + +def write_config(filename, port): + with open(filename, 'w') as f: + f.write("listener %d\n" % (port)) + f.write("allow_anonymous false\n") + f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n") + f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port)) + + +port = mosq_test.get_port() +conf_file = os.path.basename(__file__).replace('.py', '.conf') +write_config(conf_file, port) + +try: + os.mkdir(str(port)) +except FileExistsError: + pass + +rc = 1 +connect_packet = mosq_test.gen_connect("ctrl-test", username="admin", password="adminadminadmin") +connack_packet = mosq_test.gen_connack(rc=0) + +env = os.environ +env["MOSQUITTO_DYNSEC_PASSWORD"] = "adminadminadmin" +broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port, env=env) + +try: + sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=5, port=port) + rc = 0 + sock.close() +except mosq_test.TestError: + pass +finally: + os.remove(conf_file) + try: + os.remove(f"{port}/dynamic-security.json") + except FileNotFoundError: + pass + try: + os.remove(f"{port}/dynamic-security.json.pw") + except FileNotFoundError: + pass + os.rmdir(f"{port}") + broker.terminate() + broker.wait() + (stdo, stde) = broker.communicate() + if rc: + print(stde.decode('utf-8')) + + +exit(rc) diff --git a/test/broker/14-dynsec-config-init-file.py b/test/broker/14-dynsec-config-init-file.py new file mode 100755 index 00000000..aca6a084 --- /dev/null +++ b/test/broker/14-dynsec-config-init-file.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 + +from mosq_test_helper import * +import json +import shutil + +def write_config(filename, port): + with open(filename, 'w') as f: + f.write("listener %d\n" % (port)) + f.write("allow_anonymous false\n") + f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n") + f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port)) + f.write("plugin_opt_password_init_file %d/init\n" % (port)) + + +port = mosq_test.get_port() +conf_file = os.path.basename(__file__).replace('.py', '.conf') +write_config(conf_file, port) + +try: + os.mkdir(str(port)) + with open(f"{port}/init", "w") as f: + f.write("adminadminadmin\n") +except FileExistsError: + pass + +rc = 1 +connect_packet = mosq_test.gen_connect("ctrl-test", username="admin", password="adminadminadmin") +connack_packet = mosq_test.gen_connack(rc=0) + +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=5, port=port) + rc = 0 + sock.close() +except mosq_test.TestError: + pass +finally: + os.remove(conf_file) + try: + os.remove(f"{port}/dynamic-security.json") + except FileNotFoundError: + pass + try: + os.remove(f"{port}/init") + except FileNotFoundError: + pass + os.rmdir(f"{port}") + broker.terminate() + broker.wait() + (stdo, stde) = broker.communicate() + if rc: + print(stde.decode('utf-8')) + + +exit(rc) diff --git a/test/broker/14-dynsec-config-init-random.py b/test/broker/14-dynsec-config-init-random.py new file mode 100755 index 00000000..20e7116a --- /dev/null +++ b/test/broker/14-dynsec-config-init-random.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 + +from mosq_test_helper import * +import json +import shutil + +def write_config(filename, port): + with open(filename, 'w') as f: + f.write("listener %d\n" % (port)) + f.write("allow_anonymous false\n") + f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n") + f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port)) + + +port = mosq_test.get_port() +conf_file = os.path.basename(__file__).replace('.py', '.conf') +write_config(conf_file, port) + +try: + os.mkdir(str(port)) +except FileExistsError: + pass + +rc = 1 +broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port) + +with open(f"{port}/dynamic-security.json.pw", "r") as f: + data = f.readlines() + +admin_pw = data[0].split(" ")[1].strip() +user_pw = data[1].split(" ")[1].strip() + +try: + # Admin user + connect_packet = mosq_test.gen_connect("ctrl-test", username="admin", password=admin_pw) + connack_packet = mosq_test.gen_connack(rc=0) + sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=5, port=port) + + # Subscribe should be allowed + mid = 2 + subscribe_packet = mosq_test.gen_subscribe(mid, "$CONTROL/dynamic-security/#", 1) + suback_packet = mosq_test.gen_suback(mid, 1) + mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "admin suback") + sock.close() + + # Basic user + connect_packet = mosq_test.gen_connect("ctrl-test", username="democlient", password=user_pw) + connack_packet = mosq_test.gen_connack(rc=0) + sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=5, port=port) + + # Subscribe should not be allowed + mid = 2 + subscribe_packet = mosq_test.gen_subscribe(mid, "$CONTROL/dynamic-security/#", 1) + suback_packet = mosq_test.gen_suback(mid, 128) + mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "user suback") + sock.close() + + rc = 0 +except mosq_test.TestError: + pass +finally: + os.remove(conf_file) + try: + os.remove(f"{port}/dynamic-security.json") + except FileNotFoundError: + pass + try: + os.remove(f"{port}/dynamic-security.json.pw") + except FileNotFoundError: + pass + os.rmdir(f"{port}") + broker.terminate() + broker.wait() + (stdo, stde) = broker.communicate() + if rc: + print(stde.decode('utf-8')) + + +exit(rc) diff --git a/test/broker/Makefile b/test/broker/Makefile index c9f3d808..4248c6e2 100644 --- a/test/broker/Makefile +++ b/test/broker/Makefile @@ -239,18 +239,21 @@ ifeq ($(WITH_CJSON),yes) ./14-dynsec-allow-wildcard.py ./14-dynsec-anon-group.py ./14-dynsec-auth.py - ./14-dynsec-client.py ./14-dynsec-client-invalid.py + ./14-dynsec-client.py + ./14-dynsec-config-init-env.py + ./14-dynsec-config-init-file.py + ./14-dynsec-config-init-random.py ./14-dynsec-default-access.py ./14-dynsec-disable-client.py - ./14-dynsec-group.py ./14-dynsec-group-invalid.py + ./14-dynsec-group.py ./14-dynsec-modify-client.py ./14-dynsec-modify-group.py ./14-dynsec-modify-role.py ./14-dynsec-plugin-invalid.py - ./14-dynsec-role.py ./14-dynsec-role-invalid.py + ./14-dynsec-role.py endif endif diff --git a/test/broker/test.py b/test/broker/test.py index 7bdd84ef..d1fe29d8 100755 --- a/test/broker/test.py +++ b/test/broker/test.py @@ -201,18 +201,21 @@ tests = [ (1, './14-dynsec-allow-wildcard.py'), (1, './14-dynsec-anon-group.py'), (1, './14-dynsec-auth.py'), - (1, './14-dynsec-client.py'), (1, './14-dynsec-client-invalid.py'), + (1, './14-dynsec-client.py'), + (1, './14-dynsec-config-init-env.py'), + (1, './14-dynsec-config-init-file.py'), + (1, './14-dynsec-config-init-random.py'), (1, './14-dynsec-default-access.py'), (1, './14-dynsec-disable-client.py'), - (1, './14-dynsec-group.py'), (1, './14-dynsec-group-invalid.py'), + (1, './14-dynsec-group.py'), (1, './14-dynsec-modify-client.py'), (1, './14-dynsec-modify-group.py'), (1, './14-dynsec-modify-role.py'), (1, './14-dynsec-plugin-invalid.py'), - (1, './14-dynsec-role.py'), (1, './14-dynsec-role-invalid.py'), + (1, './14-dynsec-role.py'), #(1, './15-persist-client-msg-in-v5-0.py'), #(1, './15-persist-client-msg-out-queue-v3-1-1.py'), diff --git a/test/mosq_test.py b/test/mosq_test.py index c1cb1d0f..c8cf36df 100644 --- a/test/mosq_test.py +++ b/test/mosq_test.py @@ -19,7 +19,7 @@ class TestError(Exception): def __init__(self, message="Mismatched packets"): self.message = message -def start_broker(filename, cmd=None, port=0, use_conf=False, expect_fail=False, nolog=False, checkhost="localhost"): +def start_broker(filename, cmd=None, port=0, use_conf=False, expect_fail=False, nolog=False, checkhost="localhost", env=None): global vg_index global vg_logfiles @@ -56,9 +56,9 @@ def start_broker(filename, cmd=None, port=0, use_conf=False, expect_fail=False, #print(port) #print(cmd) if nolog == False: - broker = subprocess.Popen(cmd, stderr=subprocess.PIPE) + broker = subprocess.Popen(cmd, stderr=subprocess.PIPE, env=env) else: - broker = subprocess.Popen(cmd, stderr=subprocess.DEVNULL) + broker = subprocess.Popen(cmd, stderr=subprocess.DEVNULL, env=env) for i in range(0, 20): time.sleep(delay) c = None diff --git a/www/pages/documentation/dynamic-security.md b/www/pages/documentation/dynamic-security.md index 43277963..07f86830 100644 --- a/www/pages/documentation/dynamic-security.md +++ b/www/pages/documentation/dynamic-security.md @@ -329,25 +329,60 @@ during normal operation the configuration stays in memory. ### Generating the configuration file - 2.1 onwards -To generate your initial configuration file there are two choices. In version +To generate your initial configuration file there are a few choices. In version 2.0.x, you must use the `mosquitto_ctrl` utility as described below. From version 2.1 onwards, if the configuration file does not exist, the plugin will -attempt to generate a default configuration file with some sensible defaults, -including an admin client that can administer the plugin, a democlient client -that has read/write access to the application topic hierarchy, and a few roles -for different situations. Random passwords will be generated for the two -clients and placed in a file with the same name and location as the -configuration file, with `.pw` appended. For example -`dynamic-security.json.pw`. +attempt to generate a default configuration file with some sensible defaults. The roles created are: +* `broker-admin` - grants access to administer general broker settings * `client` - read/write access to the full application topic hierarchy '#' * `dynsec-admin` - grants access to administer clients/groups/roles +* `super-admin` - grants access to administer any `$CONTROL` APIs * `sys-notify` - allow bridges to publish connection state messages * `sys-observe` - allow read only access to the $SYS/# topic hierarchy * `topic-observe` - allow read only access to the full application topic hierarchy '#' +The groups created are: + +* `unauthenticated` - automatic group that anonymous/unauthenticated clients + are placed in, if anonymous access is allowed. + +The initial users can be generated in three different ways, as described below. + +#### Initialisation file + +Create a text file with a single line. This line will be used as the password +for the `admin` user, which will have access to administer the dynamic security +plugin. + +Set the configuration option to trigger the use of this file: +``` +plugin_opt_password_init_file path/to/init-file +``` + +Once the initial run of the broker has been done, the init file can be deleted. + +This method is well suited to use with e.g. docker secrets inside a container. + +#### Environment variable + +Set the `MOSQUITTO_DYNSEC_PASSWORD` environment variable to a string text and +it will be used as the password for the `admin` user, which will have access to +administer the dynamic security plugin. + +#### Default + +If neither `plugin_opt_password_init_file` nor `MOSQUITTO_DYNSEC_PASSWORD` are +set, then the plugin will generate random passwords and store them in *plain +text* at `.pw`, for example `dynamic-security.json.pw`. +This file should be deleted once the passwords are known. + +Two users will be created, `admin`, which will have access to administer the +dynamic security plugin, and `democlient`, which will have read/write access to +the application topic hierarchy `#`. + ### Generating the configuration file - 2.0 onwards To generate an initial file using the `mosquitto_ctrl` utility: From 436635fda8c8f20963d97ea688002a5e5c836418 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Wed, 22 Jun 2022 22:43:39 +0100 Subject: [PATCH 11/20] Use absolute rather than relative paths when installing. --- lib/CMakeLists.txt | 4 ++-- lib/cpp/CMakeLists.txt | 2 +- src/CMakeLists.txt | 9 +++++++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 606a18a0..fb0362aa 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -171,5 +171,5 @@ if(WITH_STATIC_LIBRARIES) ) endif() -install(FILES ../include/mosquitto.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") -install(FILES ../include/mqtt_protocol.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") +install(FILES ${mosquitto_SOURCE_DIR}/include/mosquitto.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") +install(FILES ${mosquitto_SOURCE_DIR}/include/mqtt_protocol.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") diff --git a/lib/cpp/CMakeLists.txt b/lib/cpp/CMakeLists.txt index 99a2eb28..d249cc57 100644 --- a/lib/cpp/CMakeLists.txt +++ b/lib/cpp/CMakeLists.txt @@ -69,4 +69,4 @@ if(WITH_STATIC_LIBRARIES) ) endif() -install(FILES ../../mosquittopp.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") +install(FILES ${mosquitto_SOURCE_DIR}/include/mosquittopp.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 250bcfd5..f7f9af12 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -186,7 +186,7 @@ endif() if(WITH_WEBSOCKETS) if(WITH_WEBSOCKETS_BUILTIN) add_definitions("-DWITH_WEBSOCKETS=WS_IS_BUILTIN") - set(MOSQ_SRCS ${MOSQ_SRCS} ../deps/picohttpparser/picohttpparser.c) + set(MOSQ_SRCS ${MOSQ_SRCS} ${mosquitto_SOURCE_DIR}/deps/picohttpparser/picohttpparser.c) else() find_package(libwebsockets) add_definitions("-DWITH_WEBSOCKETS=WS_IS_LWS") @@ -271,4 +271,9 @@ endif() install(TARGETS mosquitto RUNTIME DESTINATION "${CMAKE_INSTALL_SBINDIR}" ) -install(FILES ../include/mosquitto_broker.h ../include/mosquitto_plugin.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") +install( + FILES + ${mosquitto_SOURCE_DIR}/include/mosquitto_broker.h + ${mosquitto_SOURCE_DIR}/include/mosquitto_plugin.h + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" +) From 71e1b92564201af76e5749fc97dfa4e0739fe981 Mon Sep 17 00:00:00 2001 From: Norbert Heusser Date: Tue, 10 May 2022 14:38:34 +0200 Subject: [PATCH 12/20] Fixed race condition in test/broker/11-persistent-subscription-no-local.py --- test/broker/11-persistent-subscription-no-local.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/broker/11-persistent-subscription-no-local.py b/test/broker/11-persistent-subscription-no-local.py index d56d39c4..0886e568 100755 --- a/test/broker/11-persistent-subscription-no-local.py +++ b/test/broker/11-persistent-subscription-no-local.py @@ -52,7 +52,7 @@ if os.path.exists('mosquitto-%d.db' % (port)): broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port) -(stdo1, stde1) = ("", "") +(stdo1, stde1) = (None, None) try: sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=20, port=port) mosq_test.do_send_receive(sock, subscribe1_packet, suback1_packet, "suback1") @@ -64,6 +64,9 @@ try: sock.send(puback2a_packet) + # Send a ping and wait for the the response to make sure the puback2a_packet was processed by the broker + mosq_test.do_ping(sock) + broker.terminate() broker.wait() (stdo1, stde1) = broker.communicate() @@ -84,6 +87,9 @@ except mosq_test.TestError: pass finally: os.remove(conf_file) + if rc and stde1: + print(stde1.decode('utf-8')) + broker.terminate() broker.wait() (stdo, stde) = broker.communicate() From 36935a338420d895bfd71416ecd4d669f3c9cd0e Mon Sep 17 00:00:00 2001 From: Kai Buschulte Date: Tue, 21 Jun 2022 10:37:00 +0200 Subject: [PATCH 13/20] Use OpenSSL:SSL cmake target Instead of using the CMAKE_OPENSSL_INCLUDE and CMAKE_OPENSSL_LIBRARY variables the imported target OpenSSL::SSL is used. This is a more modern way of target linking. Signed-off-by: Kai Buschulte --- apps/mosquitto_ctrl/CMakeLists.txt | 3 +-- apps/mosquitto_passwd/CMakeLists.txt | 3 +-- lib/CMakeLists.txt | 6 +++--- src/CMakeLists.txt | 6 +++--- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/apps/mosquitto_ctrl/CMakeLists.txt b/apps/mosquitto_ctrl/CMakeLists.txt index 201d07ac..fef60f26 100644 --- a/apps/mosquitto_ctrl/CMakeLists.txt +++ b/apps/mosquitto_ctrl/CMakeLists.txt @@ -17,7 +17,6 @@ if(WITH_TLS AND CJSON_FOUND) ) target_include_directories(mosquitto_ctrl PRIVATE - "${OPENSSL_INCLUDE_DIR}" "${STDBOOL_H_PATH}" "${STDINT_H_PATH}" "${mosquitto_SOURCE_DIR}" @@ -57,7 +56,7 @@ if(WITH_TLS AND CJSON_FOUND) target_link_libraries(mosquitto_ctrl PRIVATE - ${OPENSSL_LIBRARIES} + OpenSSL::SSL cJSON ) diff --git a/apps/mosquitto_passwd/CMakeLists.txt b/apps/mosquitto_passwd/CMakeLists.txt index 75120186..3d853d64 100644 --- a/apps/mosquitto_passwd/CMakeLists.txt +++ b/apps/mosquitto_passwd/CMakeLists.txt @@ -10,7 +10,6 @@ if(WITH_TLS) ) target_include_directories(mosquitto_passwd PRIVATE - "${OPENSSL_INCLUDE_DIR}" "${STDBOOL_H_PATH}" "${STDINT_H_PATH}" "${mosquitto_SOURCE_DIR}" @@ -22,7 +21,7 @@ if(WITH_TLS) target_link_libraries(mosquitto_passwd PRIVATE - ${OPENSSL_LIBRARIES} + OpenSSL::SSL ) install(TARGETS mosquitto_passwd diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index fb0362aa..9ec540ce 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -58,7 +58,9 @@ if (WITH_THREADING AND WIN32) list(APPEND C_SRC "../common/winthread_mosq.c" "../common/winthread_mosq.h") endif() -set (LIBRARIES ${OPENSSL_LIBRARIES}) +if(WITH_TLS) + set (LIBRARIES OpenSSL::SSL) +endif() if(UNIX AND NOT APPLE AND NOT ANDROID) find_library(LIBRT rt) @@ -104,7 +106,6 @@ endif() target_include_directories(libmosquitto PUBLIC "${mosquitto_SOURCE_DIR}/include" - "${OPENSSL_INCLUDE_DIR}" PRIVATE "${STDBOOL_H_PATH}" "${STDINT_H_PATH}" @@ -152,7 +153,6 @@ if(WITH_STATIC_LIBRARIES) target_link_libraries(libmosquitto_static PRIVATE ${LIBRARIES}) target_include_directories(libmosquitto_static PRIVATE - "${OPENSSL_INCLUDE_DIR}" "${STDBOOL_H_PATH}" "${STDINT_H_PATH}" "${mosquitto_SOURCE_DIR}" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f7f9af12..fb43f09b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -148,7 +148,9 @@ endif() add_definitions (-DWITH_BROKER) -set (MOSQ_LIBS ${MOSQ_LIBS} ${OPENSSL_LIBRARIES}) +if(WITH_TLS) + set (MOSQ_LIBS ${MOSQ_LIBS} OpenSSL::SSL) +endif() # Check for getaddrinfo_a include(CheckLibraryExists) check_library_exists(anl getaddrinfo_a "" HAVE_GETADDRINFO_A) @@ -225,11 +227,9 @@ endif() target_include_directories(mosquitto PUBLIC "${mosquitto_SOURCE_DIR}/include" - "${OPENSSL_INCLUDE_DIR}" PRIVATE "${STDBOOL_H_PATH}" "${STDINT_H_PATH}" - "${mosquitto_SOURCE_DIR}" "${mosquitto_SOURCE_DIR}/common" "${mosquitto_SOURCE_DIR}/lib" "${mosquitto_SOURCE_DIR}/src" From 8b5a86fd52936d2d1b566eb3946f47fabe36e0ec Mon Sep 17 00:00:00 2001 From: Kai Buschulte Date: Tue, 21 Jun 2022 11:20:08 +0200 Subject: [PATCH 14/20] Introduce config-header cmake target This is an interface cmake target which specifies include directories required by the config.h places in the root of the project. This header is a "public" header visible to plugins linking the mosquitto exports. Signed-off-by: Kai Buschulte --- CMakeLists.txt | 17 +++++++++++++++++ plugins/examples/add-properties/CMakeLists.txt | 1 - .../client-lifetime-stats/CMakeLists.txt | 1 - .../examples/message-timestamp/CMakeLists.txt | 1 - .../examples/payload-size-stats/CMakeLists.txt | 1 - .../examples/plugin-event-stats/CMakeLists.txt | 1 - .../examples/print-ip-on-publish/CMakeLists.txt | 1 - plugins/persist-sqlite/CMakeLists.txt | 1 - src/CMakeLists.txt | 6 ++++-- 9 files changed, 21 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b2bd9b29..3a36fe80 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -104,6 +104,23 @@ option(WITH_APPS "Build apps?" ON) option(WITH_PLUGINS "Build plugins?" ON) option(DOCUMENTATION "Build documentation?" ON) +add_library(config-header + INTERFACE + config.h +) +target_include_directories(config-header + INTERFACE + ${mosquitto_SOURCE_DIR} +) + +if(WITH_TLS) + target_include_directories(config-header + INTERFACE + "${OPENSSL_INCLUDE_DIR}" + ) +endif() + + add_subdirectory(lib) if(WITH_CLIENTS) add_subdirectory(client) diff --git a/plugins/examples/add-properties/CMakeLists.txt b/plugins/examples/add-properties/CMakeLists.txt index afc4d4ef..3f6c689b 100644 --- a/plugins/examples/add-properties/CMakeLists.txt +++ b/plugins/examples/add-properties/CMakeLists.txt @@ -7,7 +7,6 @@ add_library(${PLUGIN_NAME} MODULE target_include_directories(${PLUGIN_NAME} PRIVATE "${STDBOOL_H_PATH}" "${STDINT_H_PATH}" - "${mosquitto_SOURCE_DIR}" "${mosquitto_SOURCE_DIR}/include" ) diff --git a/plugins/examples/client-lifetime-stats/CMakeLists.txt b/plugins/examples/client-lifetime-stats/CMakeLists.txt index 6e4fe0fb..bfd43fef 100644 --- a/plugins/examples/client-lifetime-stats/CMakeLists.txt +++ b/plugins/examples/client-lifetime-stats/CMakeLists.txt @@ -7,7 +7,6 @@ add_library(${PLUGIN_NAME} MODULE target_include_directories(${PLUGIN_NAME} PRIVATE "${STDBOOL_H_PATH}" "${STDINT_H_PATH}" - "${mosquitto_SOURCE_DIR}" "${mosquitto_SOURCE_DIR}/deps" "${mosquitto_SOURCE_DIR}/include" ) diff --git a/plugins/examples/message-timestamp/CMakeLists.txt b/plugins/examples/message-timestamp/CMakeLists.txt index 8a674285..ad74d786 100644 --- a/plugins/examples/message-timestamp/CMakeLists.txt +++ b/plugins/examples/message-timestamp/CMakeLists.txt @@ -7,7 +7,6 @@ add_library(${PLUGIN_NAME} MODULE target_include_directories(${PLUGIN_NAME} PRIVATE "${STDBOOL_H_PATH}" "${STDINT_H_PATH}" - "${mosquitto_SOURCE_DIR}" "${mosquitto_SOURCE_DIR}/include" ) diff --git a/plugins/examples/payload-size-stats/CMakeLists.txt b/plugins/examples/payload-size-stats/CMakeLists.txt index f3c62875..32dd35da 100644 --- a/plugins/examples/payload-size-stats/CMakeLists.txt +++ b/plugins/examples/payload-size-stats/CMakeLists.txt @@ -7,7 +7,6 @@ add_library(${PLUGIN_NAME} MODULE target_include_directories(${PLUGIN_NAME} PRIVATE "${STDBOOL_H_PATH}" "${STDINT_H_PATH}" - "${mosquitto_SOURCE_DIR}" "${mosquitto_SOURCE_DIR}/include" ) diff --git a/plugins/examples/plugin-event-stats/CMakeLists.txt b/plugins/examples/plugin-event-stats/CMakeLists.txt index 972994a3..316fde29 100644 --- a/plugins/examples/plugin-event-stats/CMakeLists.txt +++ b/plugins/examples/plugin-event-stats/CMakeLists.txt @@ -7,7 +7,6 @@ add_library(${PLUGIN_NAME} MODULE target_include_directories(${PLUGIN_NAME} PRIVATE "${STDBOOL_H_PATH}" "${STDINT_H_PATH}" - "${mosquitto_SOURCE_DIR}" "${mosquitto_SOURCE_DIR}/include" ) diff --git a/plugins/examples/print-ip-on-publish/CMakeLists.txt b/plugins/examples/print-ip-on-publish/CMakeLists.txt index 1cf4b30d..2b01eb3e 100644 --- a/plugins/examples/print-ip-on-publish/CMakeLists.txt +++ b/plugins/examples/print-ip-on-publish/CMakeLists.txt @@ -7,7 +7,6 @@ add_library(${PLUGIN_NAME} MODULE target_include_directories(${PLUGIN_NAME} PRIVATE "${STDBOOL_H_PATH}" "${STDINT_H_PATH}" - "${mosquitto_SOURCE_DIR}" "${mosquitto_SOURCE_DIR}/include" ) diff --git a/plugins/persist-sqlite/CMakeLists.txt b/plugins/persist-sqlite/CMakeLists.txt index b20b6388..ea3571a9 100644 --- a/plugins/persist-sqlite/CMakeLists.txt +++ b/plugins/persist-sqlite/CMakeLists.txt @@ -2,7 +2,6 @@ if(SQLITE3_FOUND AND CJSON_FOUND) set(CLIENT_INC "${STDBOOL_H_PATH}" "${STDINT_H_PATH}" - "${mosquitto_SOURCE_DIR}" "${mosquitto_SOURCE_DIR}/deps" "${mosquitto_SOURCE_DIR}/include" "${mosquitto_SOURCE_DIR}/src" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fb43f09b..c7cbc959 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -241,8 +241,10 @@ if(WITH_BUNDLED_DEPS) endif() target_link_libraries(mosquitto - PRIVATE - ${MOSQ_LIBS} + PUBLIC + config-header + PRIVATE + ${MOSQ_LIBS} ) if (WITH_THREADING AND NOT WIN32) From 4eadc96bcb8e42c1ed8507565ec5698896a5f756 Mon Sep 17 00:00:00 2001 From: Kai Buschulte Date: Tue, 21 Jun 2022 12:16:19 +0200 Subject: [PATCH 15/20] Add Traceback to failing packet_match This helps finding the caller/cause of the failure Signed-off-by: Kai Buschulte --- test/mosq_test.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/mosq_test.py b/test/mosq_test.py index 675ced8f..ce6486ec 100644 --- a/test/mosq_test.py +++ b/test/mosq_test.py @@ -1,3 +1,4 @@ +import atexit import errno import os import socket @@ -6,6 +7,8 @@ import struct import sys import time +import traceback + import mqtt5_props import __main__ @@ -141,6 +144,7 @@ def packet_matches(name, recvd, expected): print("Expected: "+to_string(expected)) except struct.error: print("Expected (not decoded, len=%d): %s" % (len(expected), expected)) + traceback.print_stack(file=sys.stdout) return False else: From 0fe397603bb897888fc4e332d90cb9061360004a Mon Sep 17 00:00:00 2001 From: Kai Buschulte Date: Tue, 21 Jun 2022 12:16:43 +0200 Subject: [PATCH 16/20] Workaround problem with sqlite3-wal files These files are not removed but empty for some versions of sqlite3 Signed-off-by: Kai Buschulte --- test/broker/sqlite_help.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/broker/sqlite_help.py b/test/broker/sqlite_help.py index 809f3e12..d69f7e72 100755 --- a/test/broker/sqlite_help.py +++ b/test/broker/sqlite_help.py @@ -1,4 +1,5 @@ import os +from pathlib import Path dir_in = 0 dir_out = 1 @@ -40,7 +41,12 @@ def cleanup(port): try: os.rmdir(f"{port}") rc = 0 - except OSError: + except OSError as e: + print(f"ERROR sqlite3 file not removed after shutdown") + if Path(str(port), "mosquitto.sqlite3-wal").stat().st_size == 0: + # some versions of sqlite3 do not remove the wal file + # thus we make sure that the file is at least empty (no pending db transactions) + rc = 0 os.remove(f"{port}/mosquitto.sqlite3-shm") os.remove(f"{port}/mosquitto.sqlite3-wal") os.rmdir(f"{port}") From a883bda9c1be6e583e83c93381a1231a91b66f56 Mon Sep 17 00:00:00 2001 From: Kai Buschulte Date: Tue, 21 Jun 2022 14:05:52 +0200 Subject: [PATCH 17/20] Add CMake test target Use `ctest` or `make test` to run all tests. With this it's also possible to run tests on a Mac. Relative paths used in tests become absolute ones to make tests executable from any build folder. Also fixed race condition in test/broker/11-persistent-subscription-no-local.py Signed-off-by: Kai Buschulte --- CMakeLists.txt | 2 + cmake/FindCUnit.cmake | 36 +++++ src/conf.c | 9 +- test/CMakeLists.txt | 4 + .../01-connect-uname-no-password-denied.py | 2 +- test/broker/01-connect-uname-or-anon.py | 2 +- .../01-connect-uname-password-denied.py | 2 +- ...1-connect-uname-password-success-no-tls.py | 2 +- test/broker/01-connect-unix-socket.py | 2 +- test/broker/06-bridge-b2br-disconnect-qos1.py | 2 +- .../06-bridge-clean-session-csF-lcsF.py | 6 +- .../06-bridge-clean-session-csF-lcsN.py | 2 +- .../06-bridge-clean-session-csF-lcsT.py | 2 +- .../06-bridge-clean-session-csT-lcsF.py | 2 +- .../06-bridge-clean-session-csT-lcsN.py | 2 +- .../06-bridge-clean-session-csT-lcsT.py | 2 +- test/broker/06-bridge-reconnect-local-out.py | 2 +- test/broker/08-ssl-bridge.py | 16 +- test/broker/08-ssl-connect-cert-auth-crl.py | 10 +- ...8-ssl-connect-cert-auth-expired-allowed.py | 8 +- .../08-ssl-connect-cert-auth-expired.py | 8 +- .../08-ssl-connect-cert-auth-revoked.py | 10 +- .../08-ssl-connect-cert-auth-without.py | 8 +- test/broker/08-ssl-connect-cert-auth.py | 8 +- test/broker/08-ssl-connect-identity.py | 8 +- .../broker/08-ssl-connect-no-auth-wrong-ca.py | 8 +- test/broker/08-ssl-connect-no-auth.py | 8 +- test/broker/08-ssl-connect-no-identity.py | 8 +- test/broker/08-ssl-hup-disconnect.py | 8 +- test/broker/08-tls-psk-bridge.py | 8 +- test/broker/08-tls-psk-pub.py | 6 +- test/broker/09-extended-auth-single2.py | 2 +- test/broker/14-dynsec-acl.py | 9 +- test/broker/14-dynsec-allow-wildcard.py | 6 +- test/broker/14-dynsec-anon-group.py | 4 +- test/broker/14-dynsec-auth.py | 4 +- test/broker/14-dynsec-client-invalid.py | 4 +- test/broker/14-dynsec-client.py | 4 +- test/broker/14-dynsec-default-access.py | 4 +- test/broker/14-dynsec-disable-client.py | 4 +- test/broker/14-dynsec-group-invalid.py | 4 +- test/broker/14-dynsec-group.py | 4 +- test/broker/14-dynsec-modify-client.py | 4 +- test/broker/14-dynsec-modify-group.py | 4 +- test/broker/14-dynsec-modify-role.py | 4 +- test/broker/14-dynsec-plugin-invalid.py | 4 +- test/broker/14-dynsec-role-invalid.py | 4 +- test/broker/14-dynsec-role.py | 4 +- ...ient-v5.0.py => 15-persist-client-v5-0.py} | 0 test/broker/15-sqlite-retain-clear-v5-0.py | 5 +- test/broker/16-cmd-args.py | 2 +- test/broker/16-config-parse-errors.py | 20 +-- test/broker/17-control-list-listeners.py | 8 +- test/broker/CMakeLists.txt | 41 +++++ test/broker/c/CMakeLists.txt | 47 ++++++ test/broker/mosq_test_helper.py | 4 + test/broker/msg_sequence_test.py | 5 +- test/client/02-subscribe-argv-errors.py | 2 +- test/client/02-subscribe-filter-out.py | 2 +- .../02-subscribe-format-json-properties.py | 2 +- test/client/02-subscribe-format-json-qos0.py | 2 +- test/client/02-subscribe-format-json-qos1.py | 2 +- .../client/02-subscribe-format-json-retain.py | 2 +- test/client/02-subscribe-format.py | 2 +- test/client/02-subscribe-null.py | 2 +- test/client/02-subscribe-qos1.py | 2 +- test/client/02-subscribe-verbose.py | 2 +- test/client/03-publish-argv-errors.py | 2 +- test/client/03-publish-file-empty.py | 2 +- test/client/03-publish-file.py | 2 +- test/client/03-publish-options-file.py | 2 +- test/client/03-publish-qos0-empty.py | 2 +- test/client/03-publish-qos1-properties.py | 2 +- test/client/03-publish-qos1.py | 2 +- test/client/03-publish-repeat.py | 2 +- test/client/03-publish-socks.py | 2 +- test/client/03-publish-stdin-file.py | 2 +- test/client/03-publish-stdin-line.py | 2 +- test/client/03-publish-url.py | 2 +- test/client/CMakeLists.txt | 19 +++ test/lib/01-con-discon-success-v5.py | 4 +- test/lib/01-con-discon-success.py | 4 +- test/lib/01-con-discon-will-clear.py | 4 +- test/lib/01-con-discon-will-v5.py | 4 +- test/lib/01-con-discon-will.py | 4 +- test/lib/01-keepalive-pingreq.py | 4 +- test/lib/01-no-clean-session.py | 4 +- test/lib/01-pre-connect-callback.py | 4 +- test/lib/01-server-keepalive-pingreq.py | 4 +- test/lib/01-unpwd-set.py | 4 +- test/lib/01-will-set.py | 4 +- test/lib/01-will-unpwd-set.py | 4 +- test/lib/02-subscribe-qos0.py | 4 +- test/lib/02-subscribe-qos1.py | 4 +- test/lib/02-subscribe-qos2.py | 4 +- test/lib/02-unsubscribe-multiple-v5.py | 4 +- test/lib/02-unsubscribe-v5.py | 4 +- test/lib/02-unsubscribe.py | 4 +- .../03-publish-b2c-qos1-unexpected-puback.py | 4 +- test/lib/03-publish-b2c-qos1.py | 4 +- test/lib/03-publish-b2c-qos2-len.py | 4 +- .../03-publish-b2c-qos2-unexpected-pubcomp.py | 4 +- .../03-publish-b2c-qos2-unexpected-pubrel.py | 4 +- test/lib/03-publish-b2c-qos2.py | 4 +- test/lib/03-publish-c2b-qos1-disconnect.py | 4 +- test/lib/03-publish-c2b-qos1-len.py | 4 +- .../03-publish-c2b-qos1-receive-maximum.py | 4 +- test/lib/03-publish-c2b-qos1-timeout.py | 4 +- test/lib/03-publish-c2b-qos2-disconnect.py | 4 +- test/lib/03-publish-c2b-qos2-len.py | 4 +- test/lib/03-publish-c2b-qos2-maximum-qos-0.py | 4 +- test/lib/03-publish-c2b-qos2-maximum-qos-1.py | 4 +- test/lib/03-publish-c2b-qos2-pubrec-error.py | 4 +- .../03-publish-c2b-qos2-receive-maximum-1.py | 4 +- .../03-publish-c2b-qos2-receive-maximum-2.py | 4 +- test/lib/03-publish-c2b-qos2-timeout.py | 4 +- test/lib/03-publish-c2b-qos2.py | 4 +- test/lib/03-publish-qos0-no-payload.py | 4 +- test/lib/03-publish-qos0.py | 4 +- test/lib/03-request-response-correlation.py | 4 +- test/lib/03-request-response.py | 4 +- test/lib/04-retain-qos0.py | 4 +- test/lib/08-ssl-bad-cacert.py | 4 +- test/lib/08-ssl-connect-cert-auth-enc.py | 11 +- test/lib/08-ssl-connect-cert-auth.py | 8 +- test/lib/08-ssl-connect-no-auth.py | 6 +- test/lib/08-ssl-fake-cacert.py | 15 +- test/lib/09-util-topic-tokenise.py | 4 +- test/lib/11-prop-oversize-packet.py | 4 +- test/lib/11-prop-recv-qos0.py | 4 +- test/lib/11-prop-recv-qos1.py | 4 +- test/lib/11-prop-recv-qos2.py | 4 +- test/lib/11-prop-send-content-type.py | 4 +- test/lib/11-prop-send-payload-format.py | 4 +- test/lib/CMakeLists.txt | 24 +++ test/lib/c/08-ssl-connect-cert-auth-enc.c | 12 +- test/lib/c/08-ssl-connect-cert-auth.c | 11 +- test/lib/c/08-ssl-connect-no-auth.c | 5 +- test/lib/c/08-ssl-fake-cacert.c | 10 +- test/lib/c/CMakeLists.txt | 65 ++++++++ test/lib/c/Makefile | 6 +- test/lib/cpp/CMakeLists.txt | 37 +++++ test/lib/mosq_test_helper.py | 6 + test/mosq_test.py | 18 ++- test/path_helper.h | 16 ++ test/random/random_client.py | 5 +- test/unit/CMakeLists.txt | 143 ++++++++++++++++++ test/unit/Makefile | 4 +- test/unit/persist_read_test.c | 105 +++++++++---- test/unit/persist_write_test.c | 55 ++++--- 150 files changed, 906 insertions(+), 327 deletions(-) create mode 100644 cmake/FindCUnit.cmake create mode 100644 test/CMakeLists.txt rename test/broker/{15-persist-client-v5.0.py => 15-persist-client-v5-0.py} (100%) create mode 100644 test/broker/CMakeLists.txt create mode 100644 test/broker/c/CMakeLists.txt create mode 100644 test/client/CMakeLists.txt create mode 100644 test/lib/CMakeLists.txt create mode 100644 test/lib/c/CMakeLists.txt create mode 100644 test/lib/cpp/CMakeLists.txt create mode 100644 test/path_helper.h create mode 100644 test/unit/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a36fe80..77ef82d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -163,3 +163,5 @@ install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libmosquittopp.pc" DESTINATION "${CMA # Testing # ======================================== enable_testing() + +add_subdirectory(test) diff --git a/cmake/FindCUnit.cmake b/cmake/FindCUnit.cmake new file mode 100644 index 00000000..c7a7d8d0 --- /dev/null +++ b/cmake/FindCUnit.cmake @@ -0,0 +1,36 @@ +find_package(PkgConfig) +pkg_check_modules(PC_CUnit QUIET cunit) + +find_path(CUnit_INCLUDE_DIR + NAMES CUnit/CUnit.h + PATHS ${PC_CUnit_INCLUDE_DIRS} +) + +find_library(CUnit_LIBRARY + NAMES cunit + PATHS ${PC_CUnit_LIBRARY_DIRS} +) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(CUnit + FOUND_VAR CUnit_FOUND + REQUIRED_VARS + CUnit_LIBRARY + CUnit_INCLUDE_DIR + VERSION_VAR CUnit_VERSION +) + +if(CUnit_FOUND) + set(CUnit_LIBRARIES ${CUnit_LIBRARY}) + set(CUnit_INCLUDE_DIRS ${CUnit_INCLUDE_DIR}) + set(CUnit_DEFINITIONS ${PC_CUnit_CFLAGS_OTHER}) +endif() + +if(CUnit_FOUND AND NOT TARGET CUnit::CUnit) + add_library(CUnit::CUnit UNKNOWN IMPORTED) + set_target_properties(CUnit::CUnit PROPERTIES + IMPORTED_LOCATION "${CUnit_LIBRARY}" + INTERFACE_COMPILE_OPTIONS "${PC_CUnit_CFLAGS_OTHER}" + INTERFACE_INCLUDE_DIRECTORIES "${CUnit_INCLUDE_DIR}" + ) +endif() diff --git a/src/conf.c b/src/conf.c index 0aa89be5..9e61e115 100644 --- a/src/conf.c +++ b/src/conf.c @@ -1365,12 +1365,12 @@ static int config__read_file_core(struct mosquitto__config *config, bool reload, log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge support not available."); #endif }else if(!strcmp(token, "bridge_tcp_user_timeout")){ -#if defined(WITH_BRIDGE) && defined(WITH_TCP_USER_TIMEOUT) +#ifdef WITH_BRIDGE if(!cur_bridge){ log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge configuration."); return MOSQ_ERR_INVAL; } - +#ifdef TCP_USER_TIMEOUT if(conf__parse_int(&token, "bridge_tcp_user_timeout", &tmp_int, &saveptr)) return MOSQ_ERR_INVAL; if(tmp_int < 0) { log__printf(NULL, MOSQ_LOG_ERR, "Error: invalid TCP user timeout value."); @@ -1378,7 +1378,10 @@ static int config__read_file_core(struct mosquitto__config *config, bool reload, } cur_bridge->tcp_user_timeout = tmp_int; #else - log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge and/or TCP user timeout support not available."); + log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge TCP user timeout support not available."); +#endif +#else + log__printf(NULL, MOSQ_LOG_WARNING, "Warning: Bridge support not available."); #endif }else if(!strcmp(token, "bridge_tls_use_os_certs")){ #if defined(WITH_BRIDGE) && defined(WITH_TLS) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 00000000..c1282179 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,4 @@ +add_subdirectory(broker) +add_subdirectory(client) +add_subdirectory(lib) +add_subdirectory(unit) diff --git a/test/broker/01-connect-uname-no-password-denied.py b/test/broker/01-connect-uname-no-password-denied.py index 62bf67af..b3d7b086 100755 --- a/test/broker/01-connect-uname-no-password-denied.py +++ b/test/broker/01-connect-uname-no-password-denied.py @@ -8,7 +8,7 @@ from mosq_test_helper import * def write_config(filename, port): with open(filename, 'w') as f: f.write("listener %d\n" % (port)) - f.write("password_file %s\n" % (filename.replace('.conf', '.pwfile'))) + f.write("password_file %s/%s\n" % (Path(__file__).resolve().parent, filename.replace('.conf', '.pwfile'))) f.write("allow_anonymous false\n") diff --git a/test/broker/01-connect-uname-or-anon.py b/test/broker/01-connect-uname-or-anon.py index 6db34fb8..5463c074 100755 --- a/test/broker/01-connect-uname-or-anon.py +++ b/test/broker/01-connect-uname-or-anon.py @@ -12,7 +12,7 @@ def write_config(filename, port, allow_anonymous, password_file): else: f.write("allow_anonymous false\n") if password_file: - f.write("password_file %s\n" % (filename.replace('.conf', '.pwfile'))) + f.write("password_file %s/%s\n" % (Path(__file__).resolve().parent, filename.replace('.conf', '.pwfile'))) def do_test(allow_anonymous, password_file, username, expect_success): port = mosq_test.get_port() diff --git a/test/broker/01-connect-uname-password-denied.py b/test/broker/01-connect-uname-password-denied.py index fce66b8f..156335c6 100755 --- a/test/broker/01-connect-uname-password-denied.py +++ b/test/broker/01-connect-uname-password-denied.py @@ -8,7 +8,7 @@ from mosq_test_helper import * def write_config(filename, port): with open(filename, 'w') as f: f.write("listener %d\n" % (port)) - f.write("password_file %s\n" % (filename.replace('.conf', '.pwfile'))) + f.write("password_file %s/%s\n" % (Path(__file__).resolve().parent, filename.replace('.conf', '.pwfile'))) f.write("allow_anonymous false\n") diff --git a/test/broker/01-connect-uname-password-success-no-tls.py b/test/broker/01-connect-uname-password-success-no-tls.py index 4d1b0a14..6b691baf 100755 --- a/test/broker/01-connect-uname-password-success-no-tls.py +++ b/test/broker/01-connect-uname-password-success-no-tls.py @@ -8,7 +8,7 @@ from mosq_test_helper import * def write_config(filename, port): with open(filename, 'w') as f: f.write("listener %d\n" % (port)) - f.write("password_file %s\n" % (filename.replace('.conf', '.pwfile'))) + f.write("password_file %s/%s\n" % (Path(__file__).resolve().parent, filename.replace('.conf', '.pwfile'))) f.write("allow_anonymous false\n") diff --git a/test/broker/01-connect-unix-socket.py b/test/broker/01-connect-unix-socket.py index 799e4fed..bfa8e718 100755 --- a/test/broker/01-connect-unix-socket.py +++ b/test/broker/01-connect-unix-socket.py @@ -7,7 +7,7 @@ from mosq_test_helper import * vg_index = 0 def start_broker(filename): global vg_index - cmd = ['../../src/mosquitto', '-v', '-c', filename] + cmd = [mosq_test.get_build_root() + '/src/mosquitto', '-v', '-c', filename] if os.environ.get('MOSQ_USE_VALGRIND') is not None: logfile = os.path.basename(__file__)+'.'+str(vg_index)+'.vglog' diff --git a/test/broker/06-bridge-b2br-disconnect-qos1.py b/test/broker/06-bridge-b2br-disconnect-qos1.py index 79b2605b..58f5d0ad 100755 --- a/test/broker/06-bridge-b2br-disconnect-qos1.py +++ b/test/broker/06-bridge-b2br-disconnect-qos1.py @@ -83,7 +83,7 @@ def do_test(proto_ver): (bridge, address) = ssock.accept() bridge.settimeout(20) - mosq_test.expect_packet(bridge, "connect", connect_packet) + mosq_test.expect_packet(bridge, "2nd connect", connect_packet) bridge.send(connack_packet) mosq_test.expect_packet(bridge, "2nd subscribe", subscribe2_packet) diff --git a/test/broker/06-bridge-clean-session-csF-lcsF.py b/test/broker/06-bridge-clean-session-csF-lcsF.py index 6db46cfa..97675cc1 100755 --- a/test/broker/06-bridge-clean-session-csF-lcsF.py +++ b/test/broker/06-bridge-clean-session-csF-lcsF.py @@ -1,9 +1,9 @@ #!/usr/bin/env python3 # Test whether a broker handles cleansession and local_cleansession correctly on bridges -from mosq_test_helper import * from collections import namedtuple -(port_a_listen, port_b_listen) = mosq_test.get_port(2) -subprocess.run(['./06-bridge-clean-session-core.py', str(port_a_listen), str(port_b_listen), "False", "False"]) +from mosq_test_helper import * +(port_a_listen, port_b_listen) = mosq_test.get_port(2) +subprocess.run([f'{Path(__file__).resolve().parent}/06-bridge-clean-session-core.py', str(port_a_listen), str(port_b_listen), "False", "False"]) diff --git a/test/broker/06-bridge-clean-session-csF-lcsN.py b/test/broker/06-bridge-clean-session-csF-lcsN.py index aab8255c..72ca1a13 100755 --- a/test/broker/06-bridge-clean-session-csF-lcsN.py +++ b/test/broker/06-bridge-clean-session-csF-lcsN.py @@ -5,5 +5,5 @@ from mosq_test_helper import * from collections import namedtuple (port_a_listen, port_b_listen) = mosq_test.get_port(2) -subprocess.run(['./06-bridge-clean-session-core.py', str(port_a_listen), str(port_b_listen), "False", "None"]) +subprocess.run([f'{Path(__file__).resolve().parent}/06-bridge-clean-session-core.py', str(port_a_listen), str(port_b_listen), "False", "None"]) diff --git a/test/broker/06-bridge-clean-session-csF-lcsT.py b/test/broker/06-bridge-clean-session-csF-lcsT.py index 005434a1..c54f22d4 100755 --- a/test/broker/06-bridge-clean-session-csF-lcsT.py +++ b/test/broker/06-bridge-clean-session-csF-lcsT.py @@ -5,5 +5,5 @@ from mosq_test_helper import * from collections import namedtuple (port_a_listen, port_b_listen) = mosq_test.get_port(2) -subprocess.run(['./06-bridge-clean-session-core.py', str(port_a_listen), str(port_b_listen), "False", "True"]) +subprocess.run([f'{Path(__file__).resolve().parent}/06-bridge-clean-session-core.py', str(port_a_listen), str(port_b_listen), "False", "True"]) diff --git a/test/broker/06-bridge-clean-session-csT-lcsF.py b/test/broker/06-bridge-clean-session-csT-lcsF.py index fe6a810d..d73a7484 100755 --- a/test/broker/06-bridge-clean-session-csT-lcsF.py +++ b/test/broker/06-bridge-clean-session-csT-lcsF.py @@ -5,5 +5,5 @@ from mosq_test_helper import * from collections import namedtuple (port_a_listen, port_b_listen) = mosq_test.get_port(2) -subprocess.run(['./06-bridge-clean-session-core.py', str(port_a_listen), str(port_b_listen), "True", "False"]) +subprocess.run([f'{Path(__file__).resolve().parent}/06-bridge-clean-session-core.py', str(port_a_listen), str(port_b_listen), "True", "False"]) diff --git a/test/broker/06-bridge-clean-session-csT-lcsN.py b/test/broker/06-bridge-clean-session-csT-lcsN.py index 8afa8700..7508bd67 100755 --- a/test/broker/06-bridge-clean-session-csT-lcsN.py +++ b/test/broker/06-bridge-clean-session-csT-lcsN.py @@ -5,5 +5,5 @@ from mosq_test_helper import * from collections import namedtuple (port_a_listen, port_b_listen) = mosq_test.get_port(2) -subprocess.run(['./06-bridge-clean-session-core.py', str(port_a_listen), str(port_b_listen), "True", "None"]) +subprocess.run([f'{Path(__file__).resolve().parent}/06-bridge-clean-session-core.py', str(port_a_listen), str(port_b_listen), "True", "None"]) diff --git a/test/broker/06-bridge-clean-session-csT-lcsT.py b/test/broker/06-bridge-clean-session-csT-lcsT.py index 9e2d8257..516e23f9 100755 --- a/test/broker/06-bridge-clean-session-csT-lcsT.py +++ b/test/broker/06-bridge-clean-session-csT-lcsT.py @@ -5,5 +5,5 @@ from mosq_test_helper import * from collections import namedtuple (port_a_listen, port_b_listen) = mosq_test.get_port(2) -subprocess.run(['./06-bridge-clean-session-core.py', str(port_a_listen), str(port_b_listen), "True", "True"]) +subprocess.run([f'{Path(__file__).resolve().parent}/06-bridge-clean-session-core.py', str(port_a_listen), str(port_b_listen), "True", "True"]) diff --git a/test/broker/06-bridge-reconnect-local-out.py b/test/broker/06-bridge-reconnect-local-out.py index c6cd3ffe..e10d3c10 100755 --- a/test/broker/06-bridge-reconnect-local-out.py +++ b/test/broker/06-bridge-reconnect-local-out.py @@ -48,7 +48,7 @@ def do_test(proto_ver): broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port1, use_conf=False) - local_cmd = ['../../src/mosquitto', '-c', '06-bridge-reconnect-local-out.conf'] + local_cmd = [mosq_test.get_build_root() + '/src/mosquitto', '-c', '06-bridge-reconnect-local-out.conf'] local_broker = mosq_test.start_broker(cmd=local_cmd, filename=os.path.basename(__file__)+'_local1', use_conf=False, port=port2) if os.environ.get('MOSQ_USE_VALGRIND') is not None: time.sleep(5) diff --git a/test/broker/08-ssl-bridge.py b/test/broker/08-ssl-bridge.py index 85f35cd6..ffe6fd43 100755 --- a/test/broker/08-ssl-bridge.py +++ b/test/broker/08-ssl-bridge.py @@ -2,6 +2,9 @@ from mosq_test_helper import * +source_dir = Path(__file__).resolve().parent +ssl_dir = source_dir.parent / "ssl" + def write_config(filename, port1, port2): with open(filename, 'w') as f: f.write("listener %d\n" % (port2)) @@ -13,7 +16,7 @@ def write_config(filename, port1, port2): f.write("notifications false\n") f.write("restart_timeout 2\n") f.write("\n") - f.write("bridge_cafile ../ssl/all-ca.crt\n") + f.write(f"bridge_cafile {ssl_dir}/all-ca.crt\n") f.write("bridge_insecure true\n") (port1, port2) = mosq_test.get_port(2) @@ -33,7 +36,13 @@ publish_packet = mosq_test.gen_publish("bridge/ssl/test", qos=0, payload="messag sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) -ssock = ssl.wrap_socket(sock, ca_certs="../ssl/all-ca.crt", keyfile="../ssl/server.key", certfile="../ssl/server.crt", server_side=True) +ssock = ssl.wrap_socket( + sock, + ca_certs=ssl_dir / "all-ca.crt", + keyfile=ssl_dir / "server.key", + certfile=ssl_dir / "server.crt", + server_side=True +) ssock.settimeout(20) ssock.bind(('', port1)) ssock.listen(5) @@ -50,7 +59,7 @@ try: mosq_test.expect_packet(bridge, "subscribe", subscribe_packet) bridge.send(suback_packet) - pub = subprocess.Popen(['./08-ssl-bridge-helper.py', str(port2)], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + pub = subprocess.Popen([f'{source_dir}/08-ssl-bridge-helper.py', str(port2)], stdout=subprocess.PIPE, stderr=subprocess.PIPE) pub.wait() (stdo, stde) = pub.communicate() @@ -75,4 +84,3 @@ finally: ssock.close() exit(rc) - diff --git a/test/broker/08-ssl-connect-cert-auth-crl.py b/test/broker/08-ssl-connect-cert-auth-crl.py index 8747e9c2..bd82da1f 100755 --- a/test/broker/08-ssl-connect-cert-auth-crl.py +++ b/test/broker/08-ssl-connect-cert-auth-crl.py @@ -12,11 +12,11 @@ def write_config(filename, port1, port2): f.write("allow_anonymous true\n") f.write("listener %d\n" % (port1)) f.write("allow_anonymous true\n") - f.write("cafile ../ssl/all-ca.crt\n") - f.write("certfile ../ssl/server.crt\n") - f.write("keyfile ../ssl/server.key\n") + f.write(f"cafile {ssl_dir}/all-ca.crt\n") + f.write(f"certfile {ssl_dir}/server.crt\n") + f.write(f"keyfile {ssl_dir}/server.key\n") f.write("require_certificate true\n") - f.write("crlfile ../ssl/crl.pem\n") + f.write(f"crlfile {ssl_dir}/crl.pem\n") (port1, port2) = mosq_test.get_port(2) conf_file = os.path.basename(__file__).replace('.py', '.conf') @@ -30,7 +30,7 @@ broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port2, try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - ssock = ssl.wrap_socket(sock, ca_certs="../ssl/test-root-ca.crt", certfile="../ssl/client.crt", keyfile="../ssl/client.key", cert_reqs=ssl.CERT_REQUIRED) + ssock = ssl.wrap_socket(sock, ca_certs=f"{ssl_dir}/test-root-ca.crt", certfile=f"{ssl_dir}/client.crt", keyfile=f"{ssl_dir}/client.key", cert_reqs=ssl.CERT_REQUIRED) ssock.settimeout(20) ssock.connect(("localhost", port1)) diff --git a/test/broker/08-ssl-connect-cert-auth-expired-allowed.py b/test/broker/08-ssl-connect-cert-auth-expired-allowed.py index eba46d4e..c7d89ac0 100755 --- a/test/broker/08-ssl-connect-cert-auth-expired-allowed.py +++ b/test/broker/08-ssl-connect-cert-auth-expired-allowed.py @@ -13,9 +13,9 @@ def write_config(filename, port1, port2): f.write("listener %d\n" % (port2)) f.write("\n") f.write("listener %d\n" % (port1)) - f.write("cafile ../ssl/all-ca.crt\n") - f.write("certfile ../ssl/server.crt\n") - f.write("keyfile ../ssl/server.key\n") + f.write(f"cafile {ssl_dir}/all-ca.crt\n") + f.write(f"certfile {ssl_dir}/server.crt\n") + f.write(f"keyfile {ssl_dir}/server.key\n") f.write("require_certificate true\n") f.write("disable_client_cert_date_checks true\n") f.write("allow_anonymous true\n") @@ -32,7 +32,7 @@ broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port2, try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - ssock = ssl.wrap_socket(sock, ca_certs="../ssl/test-root-ca.crt", certfile="../ssl/client-expired.crt", keyfile="../ssl/client-expired.key", cert_reqs=ssl.CERT_REQUIRED) + ssock = ssl.wrap_socket(sock, ca_certs=f"{ssl_dir}/test-root-ca.crt", certfile=f"{ssl_dir}/client-expired.crt", keyfile=f"{ssl_dir}/client-expired.key", cert_reqs=ssl.CERT_REQUIRED) ssock.settimeout(20) ssock.connect(("localhost", port1)) mosq_test.do_send_receive(ssock, connect_packet, connack_packet, "connack") diff --git a/test/broker/08-ssl-connect-cert-auth-expired.py b/test/broker/08-ssl-connect-cert-auth-expired.py index 7904d1f7..2aa01592 100755 --- a/test/broker/08-ssl-connect-cert-auth-expired.py +++ b/test/broker/08-ssl-connect-cert-auth-expired.py @@ -14,9 +14,9 @@ def write_config(filename, port1, port2): f.write("listener %d\n" % (port2)) f.write("\n") f.write("listener %d\n" % (port1)) - f.write("cafile ../ssl/all-ca.crt\n") - f.write("certfile ../ssl/server.crt\n") - f.write("keyfile ../ssl/server.key\n") + f.write(f"cafile {ssl_dir}/all-ca.crt\n") + f.write(f"certfile {ssl_dir}/server.crt\n") + f.write(f"keyfile {ssl_dir}/server.key\n") f.write("require_certificate true\n") (port1, port2) = mosq_test.get_port(2) @@ -30,7 +30,7 @@ broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port2, try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - ssock = ssl.wrap_socket(sock, ca_certs="../ssl/test-root-ca.crt", certfile="../ssl/client-expired.crt", keyfile="../ssl/client-expired.key", cert_reqs=ssl.CERT_REQUIRED) + ssock = ssl.wrap_socket(sock, ca_certs=f"{ssl_dir}/test-root-ca.crt", certfile=f"{ssl_dir}/client-expired.crt", keyfile=f"{ssl_dir}/client-expired.key", cert_reqs=ssl.CERT_REQUIRED) ssock.settimeout(20) try: ssock.connect(("localhost", port1)) diff --git a/test/broker/08-ssl-connect-cert-auth-revoked.py b/test/broker/08-ssl-connect-cert-auth-revoked.py index 42cdefc0..25edc9f1 100755 --- a/test/broker/08-ssl-connect-cert-auth-revoked.py +++ b/test/broker/08-ssl-connect-cert-auth-revoked.py @@ -12,11 +12,11 @@ def write_config(filename, port1, port2): f.write("allow_anonymous true\n") f.write("listener %d\n" % (port1)) f.write("allow_anonymous true\n") - f.write("cafile ../ssl/all-ca.crt\n") - f.write("certfile ../ssl/server.crt\n") - f.write("keyfile ../ssl/server.key\n") + f.write(f"cafile {ssl_dir}/all-ca.crt\n") + f.write(f"certfile {ssl_dir}/server.crt\n") + f.write(f"keyfile {ssl_dir}/server.key\n") f.write("require_certificate true\n") - f.write("crlfile ../ssl/crl.pem\n") + f.write(f"crlfile {ssl_dir}/crl.pem\n") (port1, port2) = mosq_test.get_port(2) conf_file = os.path.basename(__file__).replace('.py', '.conf') @@ -29,7 +29,7 @@ broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port2, try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - ssock = ssl.wrap_socket(sock, ca_certs="../ssl/test-root-ca.crt", certfile="../ssl/client-revoked.crt", keyfile="../ssl/client-revoked.key", cert_reqs=ssl.CERT_REQUIRED) + ssock = ssl.wrap_socket(sock, ca_certs=f"{ssl_dir}/test-root-ca.crt", certfile=f"{ssl_dir}/client-revoked.crt", keyfile=f"{ssl_dir}/client-revoked.key", cert_reqs=ssl.CERT_REQUIRED) ssock.settimeout(20) try: ssock.connect(("localhost", port1)) diff --git a/test/broker/08-ssl-connect-cert-auth-without.py b/test/broker/08-ssl-connect-cert-auth-without.py index 486beb79..3c378b83 100755 --- a/test/broker/08-ssl-connect-cert-auth-without.py +++ b/test/broker/08-ssl-connect-cert-auth-without.py @@ -12,9 +12,9 @@ def write_config(filename, port1, port2): with open(filename, 'w') as f: f.write("listener %d\n" % (port2)) f.write("listener %d\n" % (port1)) - f.write("cafile ../ssl/all-ca.crt\n") - f.write("certfile ../ssl/server.crt\n") - f.write("keyfile ../ssl/server.key\n") + f.write(f"cafile {ssl_dir}/all-ca.crt\n") + f.write(f"certfile {ssl_dir}/server.crt\n") + f.write(f"keyfile {ssl_dir}/server.key\n") f.write("require_certificate true\n") (port1, port2) = mosq_test.get_port(2) @@ -27,7 +27,7 @@ connect_packet = mosq_test.gen_connect("connect-cert-test") broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port2, use_conf=True) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -ssock = ssl.wrap_socket(sock, ca_certs="../ssl/test-root-ca.crt", cert_reqs=ssl.CERT_REQUIRED) +ssock = ssl.wrap_socket(sock, ca_certs=f"{ssl_dir}/test-root-ca.crt", cert_reqs=ssl.CERT_REQUIRED) ssock.settimeout(20) try: ssock.connect(("localhost", port1)) diff --git a/test/broker/08-ssl-connect-cert-auth.py b/test/broker/08-ssl-connect-cert-auth.py index 752c3d43..8478e6d0 100755 --- a/test/broker/08-ssl-connect-cert-auth.py +++ b/test/broker/08-ssl-connect-cert-auth.py @@ -14,9 +14,9 @@ def write_config(filename, port1, port2): f.write("allow_anonymous true\n") f.write("listener %d\n" % (port1)) f.write("allow_anonymous true\n") - f.write("cafile ../ssl/all-ca.crt\n") - f.write("certfile ../ssl/server.crt\n") - f.write("keyfile ../ssl/server.key\n") + f.write(f"cafile {ssl_dir}/all-ca.crt\n") + f.write(f"certfile {ssl_dir}/server.crt\n") + f.write(f"keyfile {ssl_dir}/server.key\n") f.write("require_certificate true\n") (port1, port2) = mosq_test.get_port(2) @@ -31,7 +31,7 @@ broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port2, try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - ssock = ssl.wrap_socket(sock, ca_certs="../ssl/test-root-ca.crt", certfile="../ssl/client.crt", keyfile="../ssl/client.key", cert_reqs=ssl.CERT_REQUIRED) + ssock = ssl.wrap_socket(sock, ca_certs=f"{ssl_dir}/test-root-ca.crt", certfile=f"{ssl_dir}/client.crt", keyfile=f"{ssl_dir}/client.key", cert_reqs=ssl.CERT_REQUIRED) ssock.settimeout(20) ssock.connect(("localhost", port1)) diff --git a/test/broker/08-ssl-connect-identity.py b/test/broker/08-ssl-connect-identity.py index e0a35c68..4fd6b4de 100755 --- a/test/broker/08-ssl-connect-identity.py +++ b/test/broker/08-ssl-connect-identity.py @@ -13,9 +13,9 @@ def write_config(filename, port1, port2): f.write("listener %d\n" % (port2)) f.write("\n") f.write("listener %d\n" %(port1)) - f.write("cafile ../ssl/all-ca.crt\n") - f.write("certfile ../ssl/server.crt\n") - f.write("keyfile ../ssl/server.key\n") + f.write(f"cafile {ssl_dir}/all-ca.crt\n") + f.write(f"certfile {ssl_dir}/server.crt\n") + f.write(f"keyfile {ssl_dir}/server.key\n") f.write("\n") f.write("use_identity_as_username true\n") f.write("require_certificate true\n") @@ -32,7 +32,7 @@ broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port2, try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - ssock = ssl.wrap_socket(sock, ca_certs="../ssl/test-root-ca.crt", certfile="../ssl/client.crt", keyfile="../ssl/client.key", cert_reqs=ssl.CERT_REQUIRED) + ssock = ssl.wrap_socket(sock, ca_certs=f"{ssl_dir}/test-root-ca.crt", certfile=f"{ssl_dir}/client.crt", keyfile=f"{ssl_dir}/client.key", cert_reqs=ssl.CERT_REQUIRED) ssock.settimeout(20) ssock.connect(("localhost", port1)) diff --git a/test/broker/08-ssl-connect-no-auth-wrong-ca.py b/test/broker/08-ssl-connect-no-auth-wrong-ca.py index 40edecce..1456b5bd 100755 --- a/test/broker/08-ssl-connect-no-auth-wrong-ca.py +++ b/test/broker/08-ssl-connect-no-auth-wrong-ca.py @@ -13,9 +13,9 @@ def write_config(filename, port1, port2): f.write("listener %d\n" % (port2)) f.write("\n") f.write("listener %d\n" % (port1)) - f.write("cafile ../ssl/all-ca.crt\n") - f.write("certfile ../ssl/server.crt\n") - f.write("keyfile ../ssl/server.key\n") + f.write(f"cafile {ssl_dir}/all-ca.crt\n") + f.write(f"certfile {ssl_dir}/server.crt\n") + f.write(f"keyfile {ssl_dir}/server.key\n") (port1, port2) = mosq_test.get_port(2) conf_file = os.path.basename(__file__).replace('.py', '.conf') @@ -28,7 +28,7 @@ connack_packet = mosq_test.gen_connack(rc=0) broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port2, use_conf=True) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -ssock = ssl.wrap_socket(sock, ca_certs="../ssl/test-alt-ca.crt", cert_reqs=ssl.CERT_REQUIRED) +ssock = ssl.wrap_socket(sock, ca_certs=f"{ssl_dir}/test-alt-ca.crt", cert_reqs=ssl.CERT_REQUIRED) ssock.settimeout(20) try: ssock.connect(("localhost", port1)) diff --git a/test/broker/08-ssl-connect-no-auth.py b/test/broker/08-ssl-connect-no-auth.py index d25ccaf2..29591d2f 100755 --- a/test/broker/08-ssl-connect-no-auth.py +++ b/test/broker/08-ssl-connect-no-auth.py @@ -15,9 +15,9 @@ def write_config(filename, port1, port2): f.write("\n") f.write("listener %d\n" % (port1)) f.write("allow_anonymous true\n") - f.write("cafile ../ssl/all-ca.crt\n") - f.write("certfile ../ssl/server.crt\n") - f.write("keyfile ../ssl/server.key\n") + f.write(f"cafile {ssl_dir}/all-ca.crt\n") + f.write(f"certfile {ssl_dir}/server.crt\n") + f.write(f"keyfile {ssl_dir}/server.key\n") (port1, port2) = mosq_test.get_port(2) conf_file = os.path.basename(__file__).replace('.py', '.conf') @@ -31,7 +31,7 @@ broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port2, try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - ssock = ssl.wrap_socket(sock, ca_certs="../ssl/test-root-ca.crt", cert_reqs=ssl.CERT_REQUIRED) + ssock = ssl.wrap_socket(sock, ca_certs=f"{ssl_dir}/test-root-ca.crt", cert_reqs=ssl.CERT_REQUIRED) ssock.settimeout(20) ssock.connect(("localhost", port1)) diff --git a/test/broker/08-ssl-connect-no-identity.py b/test/broker/08-ssl-connect-no-identity.py index 69550b57..43421b95 100755 --- a/test/broker/08-ssl-connect-no-identity.py +++ b/test/broker/08-ssl-connect-no-identity.py @@ -13,9 +13,9 @@ def write_config(filename, port1, port2): f.write("listener %d\n" % (port2)) f.write("\n") f.write("listener %d\n" % (port1)) - f.write("cafile ../ssl/all-ca.crt\n") - f.write("certfile ../ssl/server.crt\n") - f.write("keyfile ../ssl/server.key\n") + f.write(f"cafile {ssl_dir}/all-ca.crt\n") + f.write(f"certfile {ssl_dir}/server.crt\n") + f.write(f"keyfile {ssl_dir}/server.key\n") f.write("\n") f.write("use_identity_as_username true\n") @@ -31,7 +31,7 @@ broker = mosq_test.start_broker(filename=os.path.basename(__file__), port=port2, try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - ssock = ssl.wrap_socket(sock, ca_certs="../ssl/test-root-ca.crt", cert_reqs=ssl.CERT_REQUIRED) + ssock = ssl.wrap_socket(sock, ca_certs=f"{ssl_dir}/test-root-ca.crt", cert_reqs=ssl.CERT_REQUIRED) ssock.settimeout(20) ssock.connect(("localhost", port1)) diff --git a/test/broker/08-ssl-hup-disconnect.py b/test/broker/08-ssl-hup-disconnect.py index 35e27bde..34f3fab4 100755 --- a/test/broker/08-ssl-hup-disconnect.py +++ b/test/broker/08-ssl-hup-disconnect.py @@ -15,9 +15,9 @@ if sys.version < '2.7': def write_config(filename, pw_file, port, option): with open(filename, 'w') as f: f.write("listener %d\n" % (port)) - f.write("cafile ../ssl/all-ca.crt\n") - f.write("certfile ../ssl/server.crt\n") - f.write("keyfile ../ssl/server.key\n") + f.write(f"cafile {ssl_dir}/all-ca.crt\n") + f.write(f"certfile {ssl_dir}/server.crt\n") + f.write(f"keyfile {ssl_dir}/server.key\n") f.write("require_certificate true\n") f.write("%s true\n" % (option)) f.write("password_file %s\n" % (pw_file)) @@ -42,7 +42,7 @@ def do_test(option): try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - ssock = ssl.wrap_socket(sock, ca_certs="../ssl/test-root-ca.crt", certfile="../ssl/client.crt", keyfile="../ssl/client.key", cert_reqs=ssl.CERT_REQUIRED) + ssock = ssl.wrap_socket(sock, ca_certs=f"{ssl_dir}/test-root-ca.crt", certfile=f"{ssl_dir}/client.crt", keyfile=f"{ssl_dir}/client.key", cert_reqs=ssl.CERT_REQUIRED) ssock.settimeout(20) ssock.connect(("localhost", port)) mosq_test.do_send_receive(ssock, connect_packet, connack_packet, "connack") diff --git a/test/broker/08-tls-psk-bridge.py b/test/broker/08-tls-psk-bridge.py index 31d4fd9b..b8748393 100755 --- a/test/broker/08-tls-psk-bridge.py +++ b/test/broker/08-tls-psk-bridge.py @@ -10,7 +10,7 @@ def write_config1(filename, port1, port2): with open(filename, 'w') as f: f.write("allow_anonymous true\n") f.write("\n") - f.write("psk_file 08-tls-psk-bridge.psk\n") + f.write(f"psk_file {str(source_dir/'08-tls-psk-bridge.psk')}\n") f.write("\n") f.write("listener %d\n" % (port1)) f.write("\n") @@ -35,12 +35,12 @@ write_config1(conf_file1, port1, port2) write_config2(conf_file2, port2, port3) env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp rc = 1 @@ -53,7 +53,7 @@ suback_packet = mosq_test.gen_suback(mid, 0) publish_packet = mosq_test.gen_publish(topic="psk/test", payload="message", qos=0) -bridge_cmd = ['../../src/mosquitto', '-c', '08-tls-psk-bridge.conf2'] +bridge_cmd = [mosq_test.get_build_root() + '/src/mosquitto', '-c', '08-tls-psk-bridge.conf2'] broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port1) bridge = mosq_test.start_broker(filename=os.path.basename(__file__)+'_bridge', cmd=bridge_cmd, port=port3) diff --git a/test/broker/08-tls-psk-pub.py b/test/broker/08-tls-psk-pub.py index 395b3386..1c1c563f 100755 --- a/test/broker/08-tls-psk-pub.py +++ b/test/broker/08-tls-psk-pub.py @@ -10,7 +10,7 @@ if sys.version < '2.7': def write_config(filename, port1, port2): with open(filename, 'w') as f: f.write("allow_anonymous true\n") - f.write("psk_file 08-tls-psk-pub.psk\n") + f.write(f"psk_file {str(source_dir/'08-tls-psk-pub.psk')}\n") f.write("\n") f.write("listener %d\n" % (port1)) f.write("psk_hint hint\n") @@ -23,12 +23,12 @@ conf_file = os.path.basename(__file__).replace('.py', '.conf') write_config(conf_file, port1, port2) env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp rc = 1 diff --git a/test/broker/09-extended-auth-single2.py b/test/broker/09-extended-auth-single2.py index 5f850490..69ccb13f 100755 --- a/test/broker/09-extended-auth-single2.py +++ b/test/broker/09-extended-auth-single2.py @@ -54,7 +54,7 @@ def do_test(suffix): connack5_packet = mosq_test.gen_connack(rc=0, proto_ver=5, properties=props) - broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port) + broker = mosq_test.start_broker(filename=conf_file, use_conf=True, port=port) try: sock = mosq_test.do_client_connect(connect1_packet, b"", timeout=20, port=port) diff --git a/test/broker/14-dynsec-acl.py b/test/broker/14-dynsec-acl.py index 902ae9f9..a1cee168 100755 --- a/test/broker/14-dynsec-acl.py +++ b/test/broker/14-dynsec-acl.py @@ -10,7 +10,7 @@ def write_config(filename, port): with open(filename, 'w') as f: f.write("listener %d\n" % (port)) f.write("allow_anonymous false\n") - f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n") + f.write(f"plugin {mosq_test.get_build_root()}/plugins/dynamic-security/mosquitto_dynamic_security.so\n") f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port)) def command_check(sock, command_payload, expected_response): @@ -215,8 +215,9 @@ suback_c_pattern_packet_denied_success = mosq_test.gen_suback(mid, mqtt5_rc.MQTT try: - os.mkdir(str(port)) - shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port)) + if not os.path.exists(str(port)): + os.mkdir(str(port)) + shutil.copyfile(str(Path(__file__).resolve().parent) + "/dynamic-security-init.json", "%d/dynamic-security.json" % (port)) except FileExistsError: pass @@ -400,7 +401,7 @@ finally: os.remove(f"{port}/dynamic-security.json") except FileNotFoundError: pass - os.rmdir(f"{port}") + shutil.rmtree(f"{port}") broker.terminate() broker.wait() (stdo, stde) = broker.communicate() diff --git a/test/broker/14-dynsec-allow-wildcard.py b/test/broker/14-dynsec-allow-wildcard.py index c95b7c91..f5328e9a 100755 --- a/test/broker/14-dynsec-allow-wildcard.py +++ b/test/broker/14-dynsec-allow-wildcard.py @@ -10,7 +10,7 @@ def write_config(filename, port): with open(filename, 'w') as f: f.write("listener %d\n" % (port)) f.write("allow_anonymous false\n") - f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n") + f.write(f"plugin {mosq_test.get_build_root()}/plugins/dynamic-security/mosquitto_dynamic_security.so\n") f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port)) def command_check(sock, command_payload, expected_response): @@ -81,7 +81,7 @@ disconnect_kick_packet = mosq_test.gen_disconnect(reason_code=mqtt5_rc.MQTT_RC_A try: os.mkdir(str(port)) - shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port)) + shutil.copyfile(str(Path(__file__).resolve().parent / "dynamic-security-init.json"), "%d/dynamic-security.json" % (port)) except FileExistsError: pass @@ -128,7 +128,7 @@ finally: os.remove(f"{port}/dynamic-security.json") except FileNotFoundError: pass - os.rmdir(f"{port}") + shutil.rmtree(f"{port}") broker.terminate() broker.wait() (stdo, stde) = broker.communicate() diff --git a/test/broker/14-dynsec-anon-group.py b/test/broker/14-dynsec-anon-group.py index 7a2f76ea..4a0cbc0f 100755 --- a/test/broker/14-dynsec-anon-group.py +++ b/test/broker/14-dynsec-anon-group.py @@ -9,7 +9,7 @@ def write_config(filename, port): with open(filename, 'w') as f: f.write("listener %d\n" % (port)) f.write("allow_anonymous true\n") - f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n") + f.write(f"plugin {mosq_test.get_build_root()}/plugins/dynamic-security/mosquitto_dynamic_security.so\n") f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port)) def command_check(sock, command_payload, expected_response): @@ -95,7 +95,7 @@ disconnect_packet_kick = mosq_test.gen_disconnect(reason_code=mqtt5_rc.MQTT_RC_A try: os.mkdir(str(port)) - shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port)) + shutil.copyfile(str(Path(__file__).resolve().parent / "dynamic-security-init.json"), "%d/dynamic-security.json" % (port)) except FileExistsError: pass diff --git a/test/broker/14-dynsec-auth.py b/test/broker/14-dynsec-auth.py index a1fa5b3d..6a259a92 100755 --- a/test/broker/14-dynsec-auth.py +++ b/test/broker/14-dynsec-auth.py @@ -8,7 +8,7 @@ def write_config(filename, port): with open(filename, 'w') as f: f.write("listener %d\n" % (port)) f.write("allow_anonymous false\n") - f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n") + f.write(f"plugin {mosq_test.get_build_root()}/plugins/dynamic-security/mosquitto_dynamic_security.so\n") f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port)) def command_check(sock, command_payload, expected_response): @@ -112,7 +112,7 @@ connack_packet_without_un = mosq_test.gen_connack(rc=mqtt5_rc.MQTT_RC_NOT_AUTHOR try: os.mkdir(str(port)) - shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port)) + shutil.copyfile(str(Path(__file__).resolve().parent / "dynamic-security-init.json"), "%d/dynamic-security.json" % (port)) except FileExistsError: pass diff --git a/test/broker/14-dynsec-client-invalid.py b/test/broker/14-dynsec-client-invalid.py index 481ac227..3a702b4c 100755 --- a/test/broker/14-dynsec-client-invalid.py +++ b/test/broker/14-dynsec-client-invalid.py @@ -10,7 +10,7 @@ def write_config(filename, port): with open(filename, 'w') as f: f.write("listener %d\n" % (port)) f.write("allow_anonymous true\n") - f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n") + f.write(f"plugin {mosq_test.get_build_root()}/plugins/dynamic-security/mosquitto_dynamic_security.so\n") f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port)) def command_check(sock, command_payload, expected_response, msg=""): @@ -357,7 +357,7 @@ suback_packet = mosq_test.gen_suback(mid, 1) try: os.mkdir(str(port)) - shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port)) + shutil.copyfile(str(Path(__file__).resolve().parent / "dynamic-security-init.json"), "%d/dynamic-security.json" % (port)) except FileExistsError: pass diff --git a/test/broker/14-dynsec-client.py b/test/broker/14-dynsec-client.py index 10d57000..004c882a 100755 --- a/test/broker/14-dynsec-client.py +++ b/test/broker/14-dynsec-client.py @@ -8,7 +8,7 @@ def write_config(filename, port): with open(filename, 'w') as f: f.write("listener %d\n" % (port)) f.write("allow_anonymous true\n") - f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n") + f.write(f"plugin {mosq_test.get_build_root()}/plugins/dynamic-security/mosquitto_dynamic_security.so\n") f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port)) def command_check(sock, command_payload, expected_response): @@ -73,7 +73,7 @@ suback_packet = mosq_test.gen_suback(mid, 1) try: os.mkdir(str(port)) - shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port)) + shutil.copyfile(str(Path(__file__).resolve().parent / "dynamic-security-init.json"), "%d/dynamic-security.json" % (port)) except FileExistsError: pass diff --git a/test/broker/14-dynsec-default-access.py b/test/broker/14-dynsec-default-access.py index 6587d20e..c263794e 100755 --- a/test/broker/14-dynsec-default-access.py +++ b/test/broker/14-dynsec-default-access.py @@ -10,7 +10,7 @@ def write_config(filename, port): with open(filename, 'w') as f: f.write("listener %d\n" % (port)) f.write("allow_anonymous false\n") - f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n") + f.write(f"plugin {mosq_test.get_build_root()}/plugins/dynamic-security/mosquitto_dynamic_security.so\n") f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port)) def command_check(sock, command_payload, expected_response): @@ -123,7 +123,7 @@ publish_packet_recv = mosq_test.gen_publish(topic="topic", qos=0, payload="messa try: os.mkdir(str(port)) - shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port)) + shutil.copyfile(str(Path(__file__).resolve().parent / "dynamic-security-init.json"), "%d/dynamic-security.json" % (port)) except FileExistsError: pass diff --git a/test/broker/14-dynsec-disable-client.py b/test/broker/14-dynsec-disable-client.py index 6071f4d1..5c89a899 100755 --- a/test/broker/14-dynsec-disable-client.py +++ b/test/broker/14-dynsec-disable-client.py @@ -8,7 +8,7 @@ def write_config(filename, port): with open(filename, 'w') as f: f.write("listener %d\n" % (port)) f.write("allow_anonymous true\n") - f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n") + f.write(f"plugin {mosq_test.get_build_root()}/plugins/dynamic-security/mosquitto_dynamic_security.so\n") f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port)) def command_check(sock, command_payload, expected_response): @@ -64,7 +64,7 @@ suback_packet = mosq_test.gen_suback(mid, 1) try: os.mkdir(str(port)) - shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port)) + shutil.copyfile(str(Path(__file__).resolve().parent / "dynamic-security-init.json"), "%d/dynamic-security.json" % (port)) except FileExistsError: pass diff --git a/test/broker/14-dynsec-group-invalid.py b/test/broker/14-dynsec-group-invalid.py index a7945d12..a319f574 100755 --- a/test/broker/14-dynsec-group-invalid.py +++ b/test/broker/14-dynsec-group-invalid.py @@ -10,7 +10,7 @@ def write_config(filename, port): with open(filename, 'w') as f: f.write("listener %d\n" % (port)) f.write("allow_anonymous true\n") - f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n") + f.write(f"plugin {mosq_test.get_build_root()}/plugins/dynamic-security/mosquitto_dynamic_security.so\n") f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port)) def command_check(sock, command_payload, expected_response, msg=""): @@ -333,7 +333,7 @@ suback_packet = mosq_test.gen_suback(mid, 1) try: os.mkdir(str(port)) - shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port)) + shutil.copyfile(str(Path(__file__).resolve().parent / "dynamic-security-init.json"), "%d/dynamic-security.json" % (port)) except FileExistsError: pass diff --git a/test/broker/14-dynsec-group.py b/test/broker/14-dynsec-group.py index 75ac47d7..baeee111 100755 --- a/test/broker/14-dynsec-group.py +++ b/test/broker/14-dynsec-group.py @@ -8,7 +8,7 @@ def write_config(filename, port): with open(filename, 'w') as f: f.write("listener %d\n" % (port)) f.write("allow_anonymous true\n") - f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n") + f.write(f"plugin {mosq_test.get_build_root()}/plugins/dynamic-security/mosquitto_dynamic_security.so\n") f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port)) def command_check(sock, command_payload, expected_response, msg=""): @@ -112,7 +112,7 @@ suback_packet = mosq_test.gen_suback(mid, 1) try: os.mkdir(str(port)) - shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port)) + shutil.copyfile(str(Path(__file__).resolve().parent / "dynamic-security-init.json"), "%d/dynamic-security.json" % (port)) except FileExistsError: pass diff --git a/test/broker/14-dynsec-modify-client.py b/test/broker/14-dynsec-modify-client.py index d57bfdf8..38166d64 100755 --- a/test/broker/14-dynsec-modify-client.py +++ b/test/broker/14-dynsec-modify-client.py @@ -8,7 +8,7 @@ def write_config(filename, port): with open(filename, 'w') as f: f.write("listener %d\n" % (port)) f.write("allow_anonymous true\n") - f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n") + f.write(f"plugin {mosq_test.get_build_root()}/plugins/dynamic-security/mosquitto_dynamic_security.so\n") f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port)) def command_check(sock, command_payload, expected_response, msg=""): @@ -151,7 +151,7 @@ suback_packet = mosq_test.gen_suback(mid, 1) try: os.mkdir(str(port)) - shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port)) + shutil.copyfile(str(Path(__file__).resolve().parent / "dynamic-security-init.json"), "%d/dynamic-security.json" % (port)) except FileExistsError: pass diff --git a/test/broker/14-dynsec-modify-group.py b/test/broker/14-dynsec-modify-group.py index 310b6859..45b0a38d 100755 --- a/test/broker/14-dynsec-modify-group.py +++ b/test/broker/14-dynsec-modify-group.py @@ -8,7 +8,7 @@ def write_config(filename, port): with open(filename, 'w') as f: f.write("listener %d\n" % (port)) f.write("allow_anonymous true\n") - f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n") + f.write(f"plugin {mosq_test.get_build_root()}/plugins/dynamic-security/mosquitto_dynamic_security.so\n") f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port)) def command_check(sock, command_payload, expected_response, msg=""): @@ -134,7 +134,7 @@ suback_packet = mosq_test.gen_suback(mid, 1) try: os.mkdir(str(port)) - shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port)) + shutil.copyfile(str(Path(__file__).resolve().parent / "dynamic-security-init.json"), "%d/dynamic-security.json" % (port)) except FileExistsError: pass diff --git a/test/broker/14-dynsec-modify-role.py b/test/broker/14-dynsec-modify-role.py index 6720e2bc..991feb1e 100755 --- a/test/broker/14-dynsec-modify-role.py +++ b/test/broker/14-dynsec-modify-role.py @@ -8,7 +8,7 @@ def write_config(filename, port): with open(filename, 'w') as f: f.write("listener %d\n" % (port)) f.write("allow_anonymous true\n") - f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n") + f.write(f"plugin {mosq_test.get_build_root()}/plugins/dynamic-security/mosquitto_dynamic_security.so\n") f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port)) def command_check(sock, command_payload, expected_response): @@ -115,7 +115,7 @@ suback_packet = mosq_test.gen_suback(mid, 1) try: os.mkdir(str(port)) - shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port)) + shutil.copyfile(str(Path(__file__).resolve().parent / "dynamic-security-init.json"), "%d/dynamic-security.json" % (port)) except FileExistsError: pass diff --git a/test/broker/14-dynsec-plugin-invalid.py b/test/broker/14-dynsec-plugin-invalid.py index 4775696b..abd4bd8c 100755 --- a/test/broker/14-dynsec-plugin-invalid.py +++ b/test/broker/14-dynsec-plugin-invalid.py @@ -10,7 +10,7 @@ def write_config(filename, port): with open(filename, 'w') as f: f.write("listener %d\n" % (port)) f.write("allow_anonymous true\n") - f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n") + f.write(f"plugin {mosq_test.get_build_root()}/plugins/dynamic-security/mosquitto_dynamic_security.so\n") f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port)) def command_check(sock, command_payload, expected_response, msg=""): @@ -104,7 +104,7 @@ suback_packet = mosq_test.gen_suback(mid, 1) try: os.mkdir(str(port)) - shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port)) + shutil.copyfile(str(Path(__file__).resolve().parent / "dynamic-security-init.json"), "%d/dynamic-security.json" % (port)) except FileExistsError: pass diff --git a/test/broker/14-dynsec-role-invalid.py b/test/broker/14-dynsec-role-invalid.py index f498f0d4..8e240826 100755 --- a/test/broker/14-dynsec-role-invalid.py +++ b/test/broker/14-dynsec-role-invalid.py @@ -10,7 +10,7 @@ def write_config(filename, port): with open(filename, 'w') as f: f.write("listener %d\n" % (port)) f.write("allow_anonymous true\n") - f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n") + f.write(f"plugin {mosq_test.get_build_root()}/plugins/dynamic-security/mosquitto_dynamic_security.so\n") f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port)) def command_check(sock, command_payload, expected_response, msg=""): @@ -238,7 +238,7 @@ suback_packet = mosq_test.gen_suback(mid, 1) try: os.mkdir(str(port)) - shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port)) + shutil.copyfile(str(Path(__file__).resolve().parent / "dynamic-security-init.json"), "%d/dynamic-security.json" % (port)) except FileExistsError: pass diff --git a/test/broker/14-dynsec-role.py b/test/broker/14-dynsec-role.py index 44e3f2ca..11f6e047 100755 --- a/test/broker/14-dynsec-role.py +++ b/test/broker/14-dynsec-role.py @@ -8,7 +8,7 @@ def write_config(filename, port): with open(filename, 'w') as f: f.write("listener %d\n" % (port)) f.write("allow_anonymous true\n") - f.write("plugin ../../plugins/dynamic-security/mosquitto_dynamic_security.so\n") + f.write(f"plugin {mosq_test.get_build_root()}/plugins/dynamic-security/mosquitto_dynamic_security.so\n") f.write("plugin_opt_config_file %d/dynamic-security.json\n" % (port)) def command_check(sock, command_payload, expected_response, msg=""): @@ -180,7 +180,7 @@ suback_packet = mosq_test.gen_suback(mid, 1) try: os.mkdir(str(port)) - shutil.copyfile("dynamic-security-init.json", "%d/dynamic-security.json" % (port)) + shutil.copyfile(str(Path(__file__).resolve().parent / "dynamic-security-init.json"), "%d/dynamic-security.json" % (port)) except FileExistsError: pass diff --git a/test/broker/15-persist-client-v5.0.py b/test/broker/15-persist-client-v5-0.py similarity index 100% rename from test/broker/15-persist-client-v5.0.py rename to test/broker/15-persist-client-v5-0.py diff --git a/test/broker/15-sqlite-retain-clear-v5-0.py b/test/broker/15-sqlite-retain-clear-v5-0.py index 45c46f0c..501600de 100755 --- a/test/broker/15-sqlite-retain-clear-v5-0.py +++ b/test/broker/15-sqlite-retain-clear-v5-0.py @@ -27,6 +27,7 @@ publish1_packet = mosq_test.gen_publish(topic1, qos=qos, payload=payload1, retai publish2_packet = mosq_test.gen_publish(topic2, qos=qos, payload=payload2, retain=True, proto_ver=proto_ver) publish2_clear_packet = mosq_test.gen_publish(topic2, qos=qos, payload="", retain=True, proto_ver=proto_ver) +publish2_clear_echo = mosq_test.gen_publish(topic2, qos=qos, payload="", retain=False, proto_ver=proto_ver) mid = 1 subscribe_packet = mosq_test.gen_subscribe(mid, "#", 0, proto_ver=proto_ver) @@ -63,8 +64,8 @@ try: mosq_test.receive_unordered(sock, publish1_packet, publish2_packet, "publish 1 / 2") mosq_test.do_ping(sock) - # Clear retained - sock.send(publish2_clear_packet) + # Clear retained (and wait for the publish to avoid race condition) + mosq_test.do_send_receive(sock, publish2_clear_packet, publish2_clear_echo, "clear retain flag") # Kill broker broker.terminate() diff --git a/test/broker/16-cmd-args.py b/test/broker/16-cmd-args.py index fb131415..c309bf82 100755 --- a/test/broker/16-cmd-args.py +++ b/test/broker/16-cmd-args.py @@ -8,7 +8,7 @@ vg_index = 0 def start_broker(args): global vg_index - cmd = ['../../src/mosquitto'] + args + cmd = [mosq_test.get_build_root() + '/src/mosquitto'] + args if os.environ.get('MOSQ_USE_VALGRIND') is not None: logfile = os.path.basename(__file__)+'.'+str(vg_index)+'.vglog' diff --git a/test/broker/16-config-parse-errors.py b/test/broker/16-config-parse-errors.py index 4163be22..5cda567b 100755 --- a/test/broker/16-config-parse-errors.py +++ b/test/broker/16-config-parse-errors.py @@ -8,7 +8,7 @@ vg_index = 0 def start_broker(filename): global vg_index - cmd = ['../../src/mosquitto', '-v', '-c', filename] + cmd = [mosq_test.get_build_root() + '/src/mosquitto', '-v', '-c', filename] if os.environ.get('MOSQ_USE_VALGRIND') is not None: logfile = os.path.basename(__file__)+'.'+str(vg_index)+'.vglog' @@ -148,18 +148,18 @@ do_test("sys_interval 65536\n", 3) # Invalid value do_test("listener 1888\ncertfile\n", 3) # empty certfile do_test("listener 1888\nkeyfile\n", 3) # empty keyfile -do_test("listener 1888\ncertfile ./16-config-parse-errors.py\nkeyfile ../ssl/server.key\n", 1) # invalid certfile -do_test("listener 1888\ncertfile ../ssl/server.crt\nkeyfile ./16-config-parse-errors.py\n", 1) # invalid keyfile -do_test("listener 1888\ncertfile ../ssl/server.crt\nkeyfile ../ssl/client.key\n", 1) # mismatched certfile / keyfile +do_test(f"listener 1888\ncertfile {source_dir}/16-config-parse-errors.py\nkeyfile {ssl_dir}/server.key\n", 1) # invalid certfile +do_test(f"listener 1888\ncertfile {ssl_dir}/server.crt\nkeyfile {source_dir}/16-config-parse-errors.py\n", 1) # invalid keyfile +do_test(f"listener 1888\ncertfile {ssl_dir}/server.crt\nkeyfile {ssl_dir}/client.key\n", 1) # mismatched certfile / keyfile -do_test("listener 1888\ncertfile ../ssl/server.crt\nkeyfile ../ssl/server.key\ntls_version invalid\n", 1) # invalid tls_version +do_test(f"listener 1888\ncertfile {ssl_dir}/server.crt\nkeyfile {ssl_dir}/server.key\ntls_version invalid", 1) # invalid tls_version -do_test("listener 1888\ncertfile ../ssl/server.crt\nkeyfile ../ssl/server.key\ncrlfile invalid\n", 1) # missing crl file -do_test("listener 1888\ncertfile ../ssl/server.crt\nkeyfile ../ssl/server.key\ndhparamfile invalid\n", 1) # missing dh param file -do_test("listener 1888\ncertfile ../ssl/server.crt\nkeyfile ../ssl/server.key\ndhparamfile ./16-config-parse-errors.py\n", 1) # invalid dh param file +do_test(f"listener 1888\ncertfile {ssl_dir}/server.crt\nkeyfile {ssl_dir}/server.key\ncrlfile invalid", 1) # missing crl file +do_test(f"listener 1888\ncertfile {ssl_dir}/server.crt\nkeyfile {ssl_dir}/server.key\ndhparamfile invalid", 1) # missing dh param file +do_test(f"listener 1888\ncertfile {ssl_dir}/server.crt\nkeyfile {ssl_dir}/server.key\ndhparamfile {source_dir}/16-config-parse-errors.py", 1) # invalid dh param file -do_test("listener 1888\ncertfile ../ssl/server.crt\nkeyfile ../ssl/server.key\nciphers invalid\n", 1) # invalid ciphers -do_test("listener 1888\ncertfile ../ssl/server.crt\nkeyfile ../ssl/server.key\nciphers_tls1.3 invalid\n", 1) # invalid ciphers_tls1.3 +do_test(f"listener 1888\ncertfile {ssl_dir}/server.crt\nkeyfile {ssl_dir}/server.key\nciphers invalid", 1) # invalid ciphers +do_test(f"listener 1888\ncertfile {ssl_dir}/server.crt\nkeyfile {ssl_dir}/server.key\nciphers_tls1.3 invalid", 1) # invalid ciphers_tls1.3 exit(0) diff --git a/test/broker/17-control-list-listeners.py b/test/broker/17-control-list-listeners.py index 7f3f6fc3..65c8d6fc 100755 --- a/test/broker/17-control-list-listeners.py +++ b/test/broker/17-control-list-listeners.py @@ -16,12 +16,12 @@ def write_config(filename, ports): f.write("protocol websockets\n") f.write("listener %d\n" % (ports[2])) f.write("protocol mqtt\n") - f.write("certfile ../ssl/server.crt\n") - f.write("keyfile ../ssl/server.key\n") + f.write(f"certfile {ssl_dir}/server.crt\n") + f.write(f"keyfile {ssl_dir}/server.key\n") f.write("listener %d\n" % (ports[3])) f.write("protocol websockets\n") - f.write("certfile ../ssl/server.crt\n") - f.write("keyfile ../ssl/server.key\n") + f.write(f"certfile {ssl_dir}/server.crt\n") + f.write(f"keyfile {ssl_dir}/server.key\n") f.write("listener 0 17-list-listeners-mqtt.sock\n") f.write("protocol mqtt\n") f.write("listener 0 17-list-listeners-websockets.sock\n") diff --git a/test/broker/CMakeLists.txt b/test/broker/CMakeLists.txt new file mode 100644 index 00000000..52ca73b1 --- /dev/null +++ b/test/broker/CMakeLists.txt @@ -0,0 +1,41 @@ +add_subdirectory(c) + + +file(GLOB PY_TEST_FILES [0-9][0-9]-*.py) + +list(APPEND PY_TEST_FILES "${CMAKE_CURRENT_SOURCE_DIR}/msg_sequence_test.py") + +set(EXCLUDE_LIST + 01-connect-uname-password-success-no-tls + 03-publish-qos1-queued-bytes + 09-extended-auth-single2 + 15-persist-client-msg-in-v3-1-1 + 15-persist-client-msg-in-v5-0 + 15-persist-client-msg-out-queue-v3-1-1 + 15-persist-client-msg-out-v3-1-1 + 15-persist-client-msg-out-v5-0 + 15-persist-client-v3-1-1 + 15-persist-client-v5-0 + 15-persist-publish-properties-v5-0 + 15-persist-retain-v3-1-1 + 15-persist-retain-v5-0 + 15-persist-subscription-v3-1-1 + 15-persist-subscription-v5-0 + # Not a test + 06-bridge-clean-session-core + 08-ssl-bridge-helper +) + +foreach(PY_TEST_FILE ${PY_TEST_FILES}) + get_filename_component(PY_TEST_NAME ${PY_TEST_FILE} NAME_WE) + if(${PY_TEST_NAME} IN_LIST EXCLUDE_LIST) + continue() + endif() + add_test(NAME broker-${PY_TEST_NAME} + COMMAND ${PY_TEST_FILE} + ) + set_tests_properties(broker-${PY_TEST_NAME} + PROPERTIES + ENVIRONMENT "BUILD_ROOT=${CMAKE_BINARY_DIR}" + ) +endforeach() diff --git a/test/broker/c/CMakeLists.txt b/test/broker/c/CMakeLists.txt new file mode 100644 index 00000000..9fd39b12 --- /dev/null +++ b/test/broker/c/CMakeLists.txt @@ -0,0 +1,47 @@ +set(PLUGINS + auth_plugin_acl + auth_plugin_acl_change + auth_plugin_acl_sub_denied + auth_plugin_context_params + auth_plugin_delayed + auth_plugin_extended_multiple + auth_plugin_extended_reauth + auth_plugin_extended_single + auth_plugin_extended_single2 + auth_plugin_id_change + auth_plugin_msg_params + auth_plugin_publish + auth_plugin_pwd + auth_plugin_v2 + auth_plugin_v3 + auth_plugin_v4 + auth_plugin_v5 + auth_plugin_v5_control + auth_plugin_v5_handle_message + plugin_control +) + +foreach(PLUGIN ${PLUGINS}) + add_library(${PLUGIN} MODULE + ${PLUGIN}.c + ) + set_property(TARGET ${PLUGIN} + PROPERTY PREFIX "" + ) + target_link_libraries(${PLUGIN} PRIVATE mosquitto) +endforeach() + +set(BINARIES + 08-tls-psk-pub + 08-tls-psk-bridge +) + +foreach(BINARY ${BINARIES}) + add_executable(${BINARY} + ${BINARY}.c + ) + set_property(TARGET ${BINARY} + PROPERTY SUFFIX .test + ) + target_link_libraries(${BINARY} PRIVATE libmosquitto) +endforeach() diff --git a/test/broker/mosq_test_helper.py b/test/broker/mosq_test_helper.py index 52c0ed51..ed48a47e 100644 --- a/test/broker/mosq_test_helper.py +++ b/test/broker/mosq_test_helper.py @@ -16,3 +16,7 @@ import struct import subprocess import time import errno +from pathlib import Path + +source_dir = Path(__file__).resolve().parent +ssl_dir = source_dir.parent / "ssl" diff --git a/test/broker/msg_sequence_test.py b/test/broker/msg_sequence_test.py index c06b7cc9..917bb8cc 100755 --- a/test/broker/msg_sequence_test.py +++ b/test/broker/msg_sequence_test.py @@ -127,9 +127,10 @@ class MsgSequence(object): def do_test(hostname, port): + data_path=Path(__file__).resolve().parent/"data" rc = 0 sequences = [] - for (_, _, filenames) in walk("data"): + for (_, _, filenames) in walk(data_path): sequences.extend(filenames) break @@ -140,7 +141,7 @@ def do_test(hostname, port): if seq[-5:] != ".json": continue - with open("data/"+seq, "r") as f: + with open(data_path/seq, "r") as f: test_file = json.load(f) for g in test_file: diff --git a/test/client/02-subscribe-argv-errors.py b/test/client/02-subscribe-argv-errors.py index 1084455e..c9bf6e9e 100755 --- a/test/client/02-subscribe-argv-errors.py +++ b/test/client/02-subscribe-argv-errors.py @@ -10,7 +10,7 @@ def do_test(args, stderr_expected, rc_expected): port = mosq_test.get_port() env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = ['../../client/mosquitto_sub'] + args diff --git a/test/client/02-subscribe-filter-out.py b/test/client/02-subscribe-filter-out.py index c40c288e..d541d999 100755 --- a/test/client/02-subscribe-filter-out.py +++ b/test/client/02-subscribe-filter-out.py @@ -17,7 +17,7 @@ def do_test(proto_ver): V = 'mqttv31' env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = ['../../client/mosquitto_sub', diff --git a/test/client/02-subscribe-format-json-properties.py b/test/client/02-subscribe-format-json-properties.py index 77f3d5e8..20553c8f 100755 --- a/test/client/02-subscribe-format-json-properties.py +++ b/test/client/02-subscribe-format-json-properties.py @@ -18,7 +18,7 @@ def do_test(proto_ver): V = 'mqttv31' env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = ['../../client/mosquitto_sub', diff --git a/test/client/02-subscribe-format-json-qos0.py b/test/client/02-subscribe-format-json-qos0.py index 19529301..6891cb06 100755 --- a/test/client/02-subscribe-format-json-qos0.py +++ b/test/client/02-subscribe-format-json-qos0.py @@ -18,7 +18,7 @@ def do_test(proto_ver): V = 'mqttv31' env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = ['../../client/mosquitto_sub', diff --git a/test/client/02-subscribe-format-json-qos1.py b/test/client/02-subscribe-format-json-qos1.py index d893f7c0..85952ddf 100755 --- a/test/client/02-subscribe-format-json-qos1.py +++ b/test/client/02-subscribe-format-json-qos1.py @@ -18,7 +18,7 @@ def do_test(proto_ver): V = 'mqttv31' env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = ['../../client/mosquitto_sub', diff --git a/test/client/02-subscribe-format-json-retain.py b/test/client/02-subscribe-format-json-retain.py index ce9ff5a8..7562ec66 100755 --- a/test/client/02-subscribe-format-json-retain.py +++ b/test/client/02-subscribe-format-json-retain.py @@ -18,7 +18,7 @@ def do_test(proto_ver): V = 'mqttv31' env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = ['../../client/mosquitto_sub', diff --git a/test/client/02-subscribe-format.py b/test/client/02-subscribe-format.py index 4856111f..7dbdf752 100755 --- a/test/client/02-subscribe-format.py +++ b/test/client/02-subscribe-format.py @@ -17,7 +17,7 @@ def do_test(format_str, expected_output, proto_ver=4): V = 'mqttv31' env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = ['../../client/mosquitto_sub', diff --git a/test/client/02-subscribe-null.py b/test/client/02-subscribe-null.py index 63aba98d..4c47d27c 100755 --- a/test/client/02-subscribe-null.py +++ b/test/client/02-subscribe-null.py @@ -17,7 +17,7 @@ def do_test(proto_ver): V = 'mqttv31' env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = ['../../client/mosquitto_sub', diff --git a/test/client/02-subscribe-qos1.py b/test/client/02-subscribe-qos1.py index 73d81c93..b6c75e3e 100755 --- a/test/client/02-subscribe-qos1.py +++ b/test/client/02-subscribe-qos1.py @@ -17,7 +17,7 @@ def do_test(proto_ver): V = 'mqttv31' env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = ['../../client/mosquitto_sub', diff --git a/test/client/02-subscribe-verbose.py b/test/client/02-subscribe-verbose.py index 4b7aeeae..f9b51775 100755 --- a/test/client/02-subscribe-verbose.py +++ b/test/client/02-subscribe-verbose.py @@ -17,7 +17,7 @@ def do_test(proto_ver): V = 'mqttv31' env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = ['../../client/mosquitto_sub', diff --git a/test/client/03-publish-argv-errors.py b/test/client/03-publish-argv-errors.py index 941eb728..5236522a 100755 --- a/test/client/03-publish-argv-errors.py +++ b/test/client/03-publish-argv-errors.py @@ -10,7 +10,7 @@ def do_test(args, stderr_expected, rc_expected): port = mosq_test.get_port() env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = ['../../client/mosquitto_pub'] + args diff --git a/test/client/03-publish-file-empty.py b/test/client/03-publish-file-empty.py index e7886a00..907c0d17 100755 --- a/test/client/03-publish-file-empty.py +++ b/test/client/03-publish-file-empty.py @@ -23,7 +23,7 @@ def do_test(proto_ver): V = 'mqttv31' env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = ['../../client/mosquitto_pub', diff --git a/test/client/03-publish-file.py b/test/client/03-publish-file.py index 9d30b222..08c6dbef 100755 --- a/test/client/03-publish-file.py +++ b/test/client/03-publish-file.py @@ -24,7 +24,7 @@ def do_test(proto_ver): V = 'mqttv31' env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = ['../../client/mosquitto_pub', diff --git a/test/client/03-publish-options-file.py b/test/client/03-publish-options-file.py index 696c6986..f13ba265 100755 --- a/test/client/03-publish-options-file.py +++ b/test/client/03-publish-options-file.py @@ -26,7 +26,7 @@ def do_test(proto_ver): V = 'mqttv31' env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = ['../../client/mosquitto_pub', diff --git a/test/client/03-publish-qos0-empty.py b/test/client/03-publish-qos0-empty.py index 8670c56f..02c93416 100755 --- a/test/client/03-publish-qos0-empty.py +++ b/test/client/03-publish-qos0-empty.py @@ -17,7 +17,7 @@ def do_test(proto_ver): V = 'mqttv31' env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = ['../../client/mosquitto_pub', diff --git a/test/client/03-publish-qos1-properties.py b/test/client/03-publish-qos1-properties.py index bcfb56ab..1f00255f 100755 --- a/test/client/03-publish-qos1-properties.py +++ b/test/client/03-publish-qos1-properties.py @@ -10,7 +10,7 @@ def do_test(proto_ver): port = mosq_test.get_port() env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } if proto_ver == 5: diff --git a/test/client/03-publish-qos1.py b/test/client/03-publish-qos1.py index 68905205..ec4fc60b 100755 --- a/test/client/03-publish-qos1.py +++ b/test/client/03-publish-qos1.py @@ -17,7 +17,7 @@ def do_test(proto_ver): V = 'mqttv31' env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = ['../../client/mosquitto_pub', diff --git a/test/client/03-publish-repeat.py b/test/client/03-publish-repeat.py index 0c0bba4b..28849204 100755 --- a/test/client/03-publish-repeat.py +++ b/test/client/03-publish-repeat.py @@ -17,7 +17,7 @@ def do_test(proto_ver): V = 'mqttv31' env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = ['../../client/mosquitto_pub', diff --git a/test/client/03-publish-socks.py b/test/client/03-publish-socks.py index 446adeb4..26aaf4e3 100755 --- a/test/client/03-publish-socks.py +++ b/test/client/03-publish-socks.py @@ -29,7 +29,7 @@ def do_test(proto_ver, ipver): V = 'mqttv31' env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = [ diff --git a/test/client/03-publish-stdin-file.py b/test/client/03-publish-stdin-file.py index 046ab1ac..99f46906 100755 --- a/test/client/03-publish-stdin-file.py +++ b/test/client/03-publish-stdin-file.py @@ -17,7 +17,7 @@ def do_test(proto_ver): V = 'mqttv31' env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = ['../../client/mosquitto_pub', diff --git a/test/client/03-publish-stdin-line.py b/test/client/03-publish-stdin-line.py index 5d3fc69e..f9b1372d 100755 --- a/test/client/03-publish-stdin-line.py +++ b/test/client/03-publish-stdin-line.py @@ -17,7 +17,7 @@ def do_test(proto_ver): V = 'mqttv31' env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = ['../../client/mosquitto_pub', diff --git a/test/client/03-publish-url.py b/test/client/03-publish-url.py index 03dbc191..384fc36e 100755 --- a/test/client/03-publish-url.py +++ b/test/client/03-publish-url.py @@ -17,7 +17,7 @@ def do_test(proto_ver): V = 'mqttv31' env = { - 'LD_LIBRARY_PATH':'../../lib', + 'LD_LIBRARY_PATH': mosq_test.get_build_root() + '/lib', 'XDG_CONFIG_HOME':'/tmp/missing' } cmd = ['../../client/mosquitto_pub', diff --git a/test/client/CMakeLists.txt b/test/client/CMakeLists.txt new file mode 100644 index 00000000..0fd8f455 --- /dev/null +++ b/test/client/CMakeLists.txt @@ -0,0 +1,19 @@ +file(GLOB PY_TEST_FILES [0-9][0-9]-*.py) + +set(EXCLUDE_LIST + # none +) + +foreach(PY_TEST_FILE ${PY_TEST_FILES}) + get_filename_component(PY_TEST_NAME ${PY_TEST_FILE} NAME_WE) + if(${PY_TEST_NAME} IN_LIST EXCLUDE_LIST) + continue() + endif() + add_test(NAME client-${PY_TEST_NAME} + COMMAND ${PY_TEST_FILE} + ) + set_tests_properties(client-${PY_TEST_NAME} + PROPERTIES + ENVIRONMENT "BUILD_ROOT=${CMAKE_BINARY_DIR}" + ) +endforeach() diff --git a/test/lib/01-con-discon-success-v5.py b/test/lib/01-con-discon-success-v5.py index b2a681d6..260f285b 100755 --- a/test/lib/01-con-discon-success-v5.py +++ b/test/lib/01-con-discon-success-v5.py @@ -29,12 +29,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) diff --git a/test/lib/01-con-discon-success.py b/test/lib/01-con-discon-success.py index a44cafdf..be91e7f7 100755 --- a/test/lib/01-con-discon-success.py +++ b/test/lib/01-con-discon-success.py @@ -27,12 +27,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) diff --git a/test/lib/01-con-discon-will-clear.py b/test/lib/01-con-discon-will-clear.py index 114aee86..3745c226 100755 --- a/test/lib/01-con-discon-will-clear.py +++ b/test/lib/01-con-discon-will-clear.py @@ -21,12 +21,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) diff --git a/test/lib/01-con-discon-will-v5.py b/test/lib/01-con-discon-will-v5.py index b9601478..5779e780 100755 --- a/test/lib/01-con-discon-will-v5.py +++ b/test/lib/01-con-discon-will-v5.py @@ -22,12 +22,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) diff --git a/test/lib/01-con-discon-will.py b/test/lib/01-con-discon-will.py index a16596ec..f68eddcc 100755 --- a/test/lib/01-con-discon-will.py +++ b/test/lib/01-con-discon-will.py @@ -21,12 +21,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) diff --git a/test/lib/01-keepalive-pingreq.py b/test/lib/01-keepalive-pingreq.py index ccae04e2..051ca7ff 100755 --- a/test/lib/01-keepalive-pingreq.py +++ b/test/lib/01-keepalive-pingreq.py @@ -27,12 +27,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/01-no-clean-session.py b/test/lib/01-no-clean-session.py index a649479d..2f1eee4d 100755 --- a/test/lib/01-no-clean-session.py +++ b/test/lib/01-no-clean-session.py @@ -21,12 +21,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/01-pre-connect-callback.py b/test/lib/01-pre-connect-callback.py index a870fcbb..29d0b77e 100755 --- a/test/lib/01-pre-connect-callback.py +++ b/test/lib/01-pre-connect-callback.py @@ -21,12 +21,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/01-server-keepalive-pingreq.py b/test/lib/01-server-keepalive-pingreq.py index e270b771..c3cd068e 100755 --- a/test/lib/01-server-keepalive-pingreq.py +++ b/test/lib/01-server-keepalive-pingreq.py @@ -27,12 +27,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/01-unpwd-set.py b/test/lib/01-unpwd-set.py index aa00ba47..13614a3c 100755 --- a/test/lib/01-unpwd-set.py +++ b/test/lib/01-unpwd-set.py @@ -21,12 +21,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/01-will-set.py b/test/lib/01-will-set.py index 0c9413ea..3c28db57 100755 --- a/test/lib/01-will-set.py +++ b/test/lib/01-will-set.py @@ -23,12 +23,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/01-will-unpwd-set.py b/test/lib/01-will-unpwd-set.py index d357bf24..052172da 100755 --- a/test/lib/01-will-unpwd-set.py +++ b/test/lib/01-will-unpwd-set.py @@ -25,12 +25,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/02-subscribe-qos0.py b/test/lib/02-subscribe-qos0.py index 94f09c39..c58a9a86 100755 --- a/test/lib/02-subscribe-qos0.py +++ b/test/lib/02-subscribe-qos0.py @@ -35,12 +35,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/02-subscribe-qos1.py b/test/lib/02-subscribe-qos1.py index 836c5df5..08c3eb2b 100755 --- a/test/lib/02-subscribe-qos1.py +++ b/test/lib/02-subscribe-qos1.py @@ -35,12 +35,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/02-subscribe-qos2.py b/test/lib/02-subscribe-qos2.py index 54ebcf37..caa150f8 100755 --- a/test/lib/02-subscribe-qos2.py +++ b/test/lib/02-subscribe-qos2.py @@ -35,12 +35,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/02-unsubscribe-multiple-v5.py b/test/lib/02-unsubscribe-multiple-v5.py index 0c39d60e..83e15378 100755 --- a/test/lib/02-unsubscribe-multiple-v5.py +++ b/test/lib/02-unsubscribe-multiple-v5.py @@ -29,12 +29,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) rc = 1 diff --git a/test/lib/02-unsubscribe-v5.py b/test/lib/02-unsubscribe-v5.py index b79a79f8..c6fd01aa 100755 --- a/test/lib/02-unsubscribe-v5.py +++ b/test/lib/02-unsubscribe-v5.py @@ -24,12 +24,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/02-unsubscribe.py b/test/lib/02-unsubscribe.py index 52cade1f..e31f176b 100755 --- a/test/lib/02-unsubscribe.py +++ b/test/lib/02-unsubscribe.py @@ -25,12 +25,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/03-publish-b2c-qos1-unexpected-puback.py b/test/lib/03-publish-b2c-qos1-unexpected-puback.py index 689e7e46..9aa6138f 100755 --- a/test/lib/03-publish-b2c-qos1-unexpected-puback.py +++ b/test/lib/03-publish-b2c-qos1-unexpected-puback.py @@ -23,12 +23,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/03-publish-b2c-qos1.py b/test/lib/03-publish-b2c-qos1.py index c5841a75..9f6a8d5c 100755 --- a/test/lib/03-publish-b2c-qos1.py +++ b/test/lib/03-publish-b2c-qos1.py @@ -34,12 +34,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/03-publish-b2c-qos2-len.py b/test/lib/03-publish-b2c-qos2-len.py index e7778b04..150a89e6 100755 --- a/test/lib/03-publish-b2c-qos2-len.py +++ b/test/lib/03-publish-b2c-qos2-len.py @@ -29,12 +29,12 @@ def len_test(test, pubrel_packet): client_args = sys.argv[1:] env = dict(os.environ) - env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' + env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' - env['PYTHONPATH'] = '../../lib/python:'+pp + env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) diff --git a/test/lib/03-publish-b2c-qos2-unexpected-pubcomp.py b/test/lib/03-publish-b2c-qos2-unexpected-pubcomp.py index 2e545caa..7075651d 100755 --- a/test/lib/03-publish-b2c-qos2-unexpected-pubcomp.py +++ b/test/lib/03-publish-b2c-qos2-unexpected-pubcomp.py @@ -23,12 +23,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/03-publish-b2c-qos2-unexpected-pubrel.py b/test/lib/03-publish-b2c-qos2-unexpected-pubrel.py index ed5962c6..f4b24a3e 100755 --- a/test/lib/03-publish-b2c-qos2-unexpected-pubrel.py +++ b/test/lib/03-publish-b2c-qos2-unexpected-pubrel.py @@ -30,12 +30,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/03-publish-b2c-qos2.py b/test/lib/03-publish-b2c-qos2.py index e76547a2..6d6c1d9f 100755 --- a/test/lib/03-publish-b2c-qos2.py +++ b/test/lib/03-publish-b2c-qos2.py @@ -41,12 +41,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/03-publish-c2b-qos1-disconnect.py b/test/lib/03-publish-c2b-qos1-disconnect.py index 21357a3c..5dbb621e 100755 --- a/test/lib/03-publish-c2b-qos1-disconnect.py +++ b/test/lib/03-publish-c2b-qos1-disconnect.py @@ -26,12 +26,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) diff --git a/test/lib/03-publish-c2b-qos1-len.py b/test/lib/03-publish-c2b-qos1-len.py index 7ee0d371..71534929 100755 --- a/test/lib/03-publish-c2b-qos1-len.py +++ b/test/lib/03-publish-c2b-qos1-len.py @@ -26,12 +26,12 @@ def len_test(test, puback_packet): client_args = sys.argv[1:] env = dict(os.environ) - env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' + env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' - env['PYTHONPATH'] = '../../lib/python:'+pp + env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) diff --git a/test/lib/03-publish-c2b-qos1-receive-maximum.py b/test/lib/03-publish-c2b-qos1-receive-maximum.py index 7992b53e..5a6a7163 100755 --- a/test/lib/03-publish-c2b-qos1-receive-maximum.py +++ b/test/lib/03-publish-c2b-qos1-receive-maximum.py @@ -50,12 +50,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) diff --git a/test/lib/03-publish-c2b-qos1-timeout.py b/test/lib/03-publish-c2b-qos1-timeout.py index 1974bea9..236e06d5 100755 --- a/test/lib/03-publish-c2b-qos1-timeout.py +++ b/test/lib/03-publish-c2b-qos1-timeout.py @@ -40,12 +40,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/03-publish-c2b-qos2-disconnect.py b/test/lib/03-publish-c2b-qos2-disconnect.py index 4f099a95..427df8ec 100755 --- a/test/lib/03-publish-c2b-qos2-disconnect.py +++ b/test/lib/03-publish-c2b-qos2-disconnect.py @@ -28,12 +28,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/03-publish-c2b-qos2-len.py b/test/lib/03-publish-c2b-qos2-len.py index 68db2b60..60f48bf0 100755 --- a/test/lib/03-publish-c2b-qos2-len.py +++ b/test/lib/03-publish-c2b-qos2-len.py @@ -27,12 +27,12 @@ def len_test(test, pubrec_packet, pubcomp_packet): client_args = sys.argv[1:] env = dict(os.environ) - env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' + env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' - env['PYTHONPATH'] = '../../lib/python:'+pp + env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) diff --git a/test/lib/03-publish-c2b-qos2-maximum-qos-0.py b/test/lib/03-publish-c2b-qos2-maximum-qos-0.py index c7fc518d..97290d78 100755 --- a/test/lib/03-publish-c2b-qos2-maximum-qos-0.py +++ b/test/lib/03-publish-c2b-qos2-maximum-qos-0.py @@ -28,12 +28,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) diff --git a/test/lib/03-publish-c2b-qos2-maximum-qos-1.py b/test/lib/03-publish-c2b-qos2-maximum-qos-1.py index 4c064562..87afd9cf 100755 --- a/test/lib/03-publish-c2b-qos2-maximum-qos-1.py +++ b/test/lib/03-publish-c2b-qos2-maximum-qos-1.py @@ -32,12 +32,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) diff --git a/test/lib/03-publish-c2b-qos2-pubrec-error.py b/test/lib/03-publish-c2b-qos2-pubrec-error.py index 719e681e..76fba344 100755 --- a/test/lib/03-publish-c2b-qos2-pubrec-error.py +++ b/test/lib/03-publish-c2b-qos2-pubrec-error.py @@ -39,12 +39,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) diff --git a/test/lib/03-publish-c2b-qos2-receive-maximum-1.py b/test/lib/03-publish-c2b-qos2-receive-maximum-1.py index babc22d8..1af1dc54 100755 --- a/test/lib/03-publish-c2b-qos2-receive-maximum-1.py +++ b/test/lib/03-publish-c2b-qos2-receive-maximum-1.py @@ -56,12 +56,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) diff --git a/test/lib/03-publish-c2b-qos2-receive-maximum-2.py b/test/lib/03-publish-c2b-qos2-receive-maximum-2.py index acd7d535..e1ccec02 100755 --- a/test/lib/03-publish-c2b-qos2-receive-maximum-2.py +++ b/test/lib/03-publish-c2b-qos2-receive-maximum-2.py @@ -56,12 +56,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) diff --git a/test/lib/03-publish-c2b-qos2-timeout.py b/test/lib/03-publish-c2b-qos2-timeout.py index 148f4278..e65b7123 100755 --- a/test/lib/03-publish-c2b-qos2-timeout.py +++ b/test/lib/03-publish-c2b-qos2-timeout.py @@ -46,12 +46,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/03-publish-c2b-qos2.py b/test/lib/03-publish-c2b-qos2.py index 2ef66612..f98c43c6 100755 --- a/test/lib/03-publish-c2b-qos2.py +++ b/test/lib/03-publish-c2b-qos2.py @@ -46,12 +46,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/03-publish-qos0-no-payload.py b/test/lib/03-publish-qos0-no-payload.py index c3264899..98b10ea6 100755 --- a/test/lib/03-publish-qos0-no-payload.py +++ b/test/lib/03-publish-qos0-no-payload.py @@ -31,12 +31,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/03-publish-qos0.py b/test/lib/03-publish-qos0.py index f0fedc26..2260a233 100755 --- a/test/lib/03-publish-qos0.py +++ b/test/lib/03-publish-qos0.py @@ -31,12 +31,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/03-request-response-correlation.py b/test/lib/03-request-response-correlation.py index aee91997..2ca75d22 100755 --- a/test/lib/03-request-response-correlation.py +++ b/test/lib/03-request-response-correlation.py @@ -41,12 +41,12 @@ sock.bind(('', port)) sock.listen(5) env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client1 = mosq_test.start_client(filename="03-request-response-correlation-1.log", cmd=["c/03-request-response-correlation-1.test"], env=env, port=port) try: diff --git a/test/lib/03-request-response.py b/test/lib/03-request-response.py index 53c16561..617b6a70 100755 --- a/test/lib/03-request-response.py +++ b/test/lib/03-request-response.py @@ -33,12 +33,12 @@ sock.bind(('', port)) sock.listen(5) env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client1 = mosq_test.start_client(filename="03-request-response-1.log", cmd=["c/03-request-response-1.test"], env=env, port=port) try: diff --git a/test/lib/04-retain-qos0.py b/test/lib/04-retain-qos0.py index 632613bb..43a15657 100755 --- a/test/lib/04-retain-qos0.py +++ b/test/lib/04-retain-qos0.py @@ -22,12 +22,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/08-ssl-bad-cacert.py b/test/lib/08-ssl-bad-cacert.py index e223a5fb..009fa94f 100755 --- a/test/lib/08-ssl-bad-cacert.py +++ b/test/lib/08-ssl-bad-cacert.py @@ -10,12 +10,12 @@ rc = 1 client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env) client.wait() diff --git a/test/lib/08-ssl-connect-cert-auth-enc.py b/test/lib/08-ssl-connect-cert-auth-enc.py index 6ff1d9c2..692f521a 100755 --- a/test/lib/08-ssl-connect-cert-auth-enc.py +++ b/test/lib/08-ssl-connect-cert-auth-enc.py @@ -26,8 +26,8 @@ disconnect_packet = mosq_test.gen_disconnect() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) -ssock = ssl.wrap_socket(sock, ca_certs="../ssl/all-ca.crt", - keyfile="../ssl/server.key", certfile="../ssl/server.crt", +ssock = ssl.wrap_socket(sock, ca_certs=f"{ssl_dir}/all-ca.crt", + keyfile=f"{ssl_dir}/server.key", certfile=f"{ssl_dir}/server.crt", server_side=True, cert_reqs=ssl.CERT_REQUIRED) ssock.settimeout(10) ssock.bind(('', port)) @@ -35,12 +35,12 @@ ssock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: @@ -57,5 +57,8 @@ except mosq_test.TestError: finally: client.wait() ssock.close() + if rc: + (stdo, stde) = client.communicate() + print(stde.decode('utf-8')) exit(rc) diff --git a/test/lib/08-ssl-connect-cert-auth.py b/test/lib/08-ssl-connect-cert-auth.py index 2b01cc3d..0ee3cf40 100755 --- a/test/lib/08-ssl-connect-cert-auth.py +++ b/test/lib/08-ssl-connect-cert-auth.py @@ -26,8 +26,8 @@ disconnect_packet = mosq_test.gen_disconnect() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) -ssock = ssl.wrap_socket(sock, ca_certs="../ssl/all-ca.crt", - keyfile="../ssl/server.key", certfile="../ssl/server.crt", +ssock = ssl.wrap_socket(sock, ca_certs=f"{ssl_dir}/all-ca.crt", + keyfile=f"{ssl_dir}/server.key", certfile=f"{ssl_dir}/server.crt", server_side=True, cert_reqs=ssl.CERT_REQUIRED) ssock.settimeout(10) ssock.bind(('', port)) @@ -35,12 +35,12 @@ ssock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/08-ssl-connect-no-auth.py b/test/lib/08-ssl-connect-no-auth.py index 374ab841..6377bc65 100755 --- a/test/lib/08-ssl-connect-no-auth.py +++ b/test/lib/08-ssl-connect-no-auth.py @@ -25,19 +25,19 @@ disconnect_packet = mosq_test.gen_disconnect() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) -ssock = ssl.wrap_socket(sock, ca_certs="../ssl/all-ca.crt", keyfile="../ssl/server.key", certfile="../ssl/server.crt", server_side=True) +ssock = ssl.wrap_socket(sock, ca_certs=f"{ssl_dir}/all-ca.crt", keyfile=f"{ssl_dir}/server.key", certfile=f"{ssl_dir}/server.crt", server_side=True) ssock.settimeout(10) ssock.bind(('', port)) ssock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/08-ssl-fake-cacert.py b/test/lib/08-ssl-fake-cacert.py index 4d8c08f9..9123cb7c 100755 --- a/test/lib/08-ssl-fake-cacert.py +++ b/test/lib/08-ssl-fake-cacert.py @@ -10,21 +10,26 @@ if sys.version < '2.7': sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) -ssock = ssl.wrap_socket(sock, ca_certs="../ssl/all-ca.crt", - keyfile="../ssl/server.key", certfile="../ssl/server.crt", - server_side=True, cert_reqs=ssl.CERT_REQUIRED) +ssock = ssl.wrap_socket( + sock, + ca_certs=f"{ssl_dir}/all-ca.crt", + keyfile=f"{ssl_dir}/server.key", + certfile=f"{ssl_dir}/server.crt", + server_side=True, + cert_reqs=ssl.CERT_REQUIRED +) ssock.settimeout(10) ssock.bind(('', port)) ssock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/09-util-topic-tokenise.py b/test/lib/09-util-topic-tokenise.py index dc9ec36b..38903010 100755 --- a/test/lib/09-util-topic-tokenise.py +++ b/test/lib/09-util-topic-tokenise.py @@ -6,12 +6,12 @@ rc = 1 client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env) client.wait() diff --git a/test/lib/11-prop-oversize-packet.py b/test/lib/11-prop-oversize-packet.py index 5755dcfe..d85a2260 100755 --- a/test/lib/11-prop-oversize-packet.py +++ b/test/lib/11-prop-oversize-packet.py @@ -28,12 +28,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/11-prop-recv-qos0.py b/test/lib/11-prop-recv-qos0.py index dab37a98..a2ba8249 100755 --- a/test/lib/11-prop-recv-qos0.py +++ b/test/lib/11-prop-recv-qos0.py @@ -27,12 +27,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/11-prop-recv-qos1.py b/test/lib/11-prop-recv-qos1.py index f83f3b64..3afa3a60 100755 --- a/test/lib/11-prop-recv-qos1.py +++ b/test/lib/11-prop-recv-qos1.py @@ -30,12 +30,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/11-prop-recv-qos2.py b/test/lib/11-prop-recv-qos2.py index 4e34bc2b..b8186b8a 100755 --- a/test/lib/11-prop-recv-qos2.py +++ b/test/lib/11-prop-recv-qos2.py @@ -32,12 +32,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/11-prop-send-content-type.py b/test/lib/11-prop-send-content-type.py index 6fd8f9b1..3321a161 100755 --- a/test/lib/11-prop-send-content-type.py +++ b/test/lib/11-prop-send-content-type.py @@ -22,12 +22,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/11-prop-send-payload-format.py b/test/lib/11-prop-send-payload-format.py index dd2adb6a..b7e3943d 100755 --- a/test/lib/11-prop-send-payload-format.py +++ b/test/lib/11-prop-send-payload-format.py @@ -32,12 +32,12 @@ sock.listen(5) client_args = sys.argv[1:] env = dict(os.environ) -env['LD_LIBRARY_PATH'] = '../../lib:../../lib/cpp' +env['LD_LIBRARY_PATH'] = mosq_test.get_build_root() + '/lib:' + mosq_test.get_build_root() + '/lib/cpp' try: pp = env['PYTHONPATH'] except KeyError: pp = '' -env['PYTHONPATH'] = '../../lib/python:'+pp +env['PYTHONPATH'] = mosq_test.get_build_root() + '/lib/python:'+pp client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, env=env, port=port) try: diff --git a/test/lib/CMakeLists.txt b/test/lib/CMakeLists.txt new file mode 100644 index 00000000..60be580f --- /dev/null +++ b/test/lib/CMakeLists.txt @@ -0,0 +1,24 @@ +add_subdirectory(c) +add_subdirectory(cpp) + + +file(GLOB PY_TEST_FILES [0-9][0-9]-*.py) + +set(EXCLUDE_LIST + 03-publish-c2b-qos1-timeout + 03-publish-c2b-qos2-timeout +) + +foreach(PY_TEST_FILE ${PY_TEST_FILES}) + get_filename_component(PY_TEST_NAME ${PY_TEST_FILE} NAME_WE) + if(${PY_TEST_NAME} IN_LIST EXCLUDE_LIST) + continue() + endif() + add_test(NAME lib-${PY_TEST_NAME} + COMMAND ${PY_TEST_FILE} c/${PY_TEST_NAME}.test + ) + set_tests_properties(lib-${PY_TEST_NAME} + PROPERTIES + ENVIRONMENT "BUILD_ROOT=${CMAKE_BINARY_DIR}" + ) +endforeach() diff --git a/test/lib/c/08-ssl-connect-cert-auth-enc.c b/test/lib/c/08-ssl-connect-cert-auth-enc.c index 2d98b32c..ebd0521e 100644 --- a/test/lib/c/08-ssl-connect-cert-auth-enc.c +++ b/test/lib/c/08-ssl-connect-cert-auth-enc.c @@ -5,6 +5,8 @@ #include #include +#include "path_helper.h" + static int run = -1; static void on_connect(struct mosquitto *mosq, void *obj, int rc) @@ -54,7 +56,15 @@ int main(int argc, char *argv[]) if(mosq == NULL){ return 1; } - mosquitto_tls_set(mosq, "../ssl/test-root-ca.crt", "../ssl/certs", "../ssl/client-encrypted.crt", "../ssl/client-encrypted.key", password_callback); + char cafile[4096]; + cat_sourcedir_with_relpath(cafile, "/../../ssl/test-root-ca.crt"); + char capath[4096]; + cat_sourcedir_with_relpath(capath, "/../../ssl/certs"); + char certfile[4096]; + cat_sourcedir_with_relpath(certfile, "/../../ssl/client-encrypted.crt"); + char keyfile[4096]; + cat_sourcedir_with_relpath(keyfile, "/../../ssl/client-encrypted.key"); + mosquitto_tls_set(mosq, cafile, capath, certfile, keyfile, password_callback); mosquitto_connect_callback_set(mosq, on_connect); mosquitto_disconnect_callback_set(mosq, on_disconnect); diff --git a/test/lib/c/08-ssl-connect-cert-auth.c b/test/lib/c/08-ssl-connect-cert-auth.c index c9582baa..97043f36 100644 --- a/test/lib/c/08-ssl-connect-cert-auth.c +++ b/test/lib/c/08-ssl-connect-cert-auth.c @@ -1,3 +1,4 @@ +#include "path_helper.h" #include #include #include @@ -42,7 +43,15 @@ int main(int argc, char *argv[]) if(mosq == NULL){ return 1; } - mosquitto_tls_set(mosq, "../ssl/test-root-ca.crt", "../ssl/certs", "../ssl/client.crt", "../ssl/client.key", NULL); + char cafile[4096]; + cat_sourcedir_with_relpath(cafile, "/../../ssl/test-root-ca.crt"); + char capath[4096]; + cat_sourcedir_with_relpath(capath, "/../../ssl/certs"); + char certfile[4096]; + cat_sourcedir_with_relpath(certfile, "/../../ssl/client.crt"); + char keyfile[4096]; + cat_sourcedir_with_relpath(keyfile, "/../../ssl/client.key"); + mosquitto_tls_set(mosq, cafile, capath, certfile, keyfile, NULL); mosquitto_connect_callback_set(mosq, on_connect); mosquitto_disconnect_callback_set(mosq, on_disconnect); diff --git a/test/lib/c/08-ssl-connect-no-auth.c b/test/lib/c/08-ssl-connect-no-auth.c index 3713b4b6..acf4cc13 100644 --- a/test/lib/c/08-ssl-connect-no-auth.c +++ b/test/lib/c/08-ssl-connect-no-auth.c @@ -1,3 +1,4 @@ +#include "path_helper.h" #include #include #include @@ -42,7 +43,9 @@ int main(int argc, char *argv[]) if(mosq == NULL){ return 1; } - mosquitto_tls_set(mosq, "../ssl/all-ca.crt", NULL, NULL, NULL, NULL); + char cafile[4096]; + cat_sourcedir_with_relpath(cafile, "/../../ssl/test-root-ca.crt"); + mosquitto_tls_set(mosq, cafile, NULL, NULL, NULL, NULL); mosquitto_connect_callback_set(mosq, on_connect); mosquitto_disconnect_callback_set(mosq, on_disconnect); diff --git a/test/lib/c/08-ssl-fake-cacert.c b/test/lib/c/08-ssl-fake-cacert.c index 828a2d15..f8ce340b 100644 --- a/test/lib/c/08-ssl-fake-cacert.c +++ b/test/lib/c/08-ssl-fake-cacert.c @@ -4,6 +4,8 @@ #include #include +#include "path_helper.h" + static void on_connect(struct mosquitto *mosq, void *obj, int rc) { (void)mosq; @@ -30,7 +32,13 @@ int main(int argc, char *argv[]) if(mosq == NULL){ return 1; } - mosquitto_tls_set(mosq, "../ssl/test-fake-root-ca.crt", NULL, "../ssl/client.crt", "../ssl/client.key", NULL); + char cafile[4096]; + cat_sourcedir_with_relpath(cafile, "/../../ssl/test-fake-root-ca.crt"); + char certfile[4096]; + cat_sourcedir_with_relpath(certfile, "/../../ssl/client.crt"); + char keyfile[4096]; + cat_sourcedir_with_relpath(keyfile, "/../../ssl/client.key"); + mosquitto_tls_set(mosq, cafile, NULL, certfile, keyfile, NULL); mosquitto_connect_callback_set(mosq, on_connect); rc = mosquitto_connect(mosq, "localhost", port, 60); diff --git a/test/lib/c/CMakeLists.txt b/test/lib/c/CMakeLists.txt new file mode 100644 index 00000000..54f02d03 --- /dev/null +++ b/test/lib/c/CMakeLists.txt @@ -0,0 +1,65 @@ +set(BINARIES + 01-con-discon-success + 01-con-discon-success-v5 + 01-con-discon-will + 01-con-discon-will-v5 + 01-con-discon-will-clear + 01-keepalive-pingreq + 01-no-clean-session + 01-pre-connect-callback + 01-server-keepalive-pingreq + 01-unpwd-set + 01-will-set + 01-will-unpwd-set + 02-subscribe-qos0 + 02-subscribe-qos1-async1 + 02-subscribe-qos1-async2 + 02-subscribe-qos1 + 02-subscribe-qos2 + 02-unsubscribe-multiple-v5 + 02-unsubscribe-v5 + 02-unsubscribe + 03-publish-b2c-qos1-unexpected-puback + 03-publish-b2c-qos1 + 03-publish-b2c-qos2-len + 03-publish-b2c-qos2-unexpected-pubrel + 03-publish-b2c-qos2-unexpected-pubcomp + 03-publish-b2c-qos2 + 03-publish-c2b-qos1-disconnect + 03-publish-c2b-qos1-len + 03-publish-c2b-qos1-receive-maximum + 03-publish-c2b-qos2-disconnect + 03-publish-c2b-qos2-len + 03-publish-c2b-qos2-maximum-qos-0 + 03-publish-c2b-qos2-maximum-qos-1 + 03-publish-c2b-qos2-pubrec-error + 03-publish-c2b-qos2-receive-maximum-1 + 03-publish-c2b-qos2-receive-maximum-2 + 03-publish-c2b-qos2 + 03-publish-qos0-no-payload + 03-publish-qos0 + 03-request-response-1 + 03-request-response-2 + 03-request-response-correlation-1 + 04-retain-qos0 + 08-ssl-bad-cacert + 08-ssl-connect-cert-auth-enc + 08-ssl-connect-cert-auth + 08-ssl-connect-no-auth + 08-ssl-fake-cacert + 09-util-topic-tokenise + 11-prop-oversize-packet + 11-prop-recv-qos0 + 11-prop-recv-qos1 + 11-prop-recv-qos2 + 11-prop-send-payload-format + 11-prop-send-content-type +) + +foreach(BINARY ${BINARIES}) + add_executable(${BINARY} ${BINARY}.c) + target_compile_definitions(${BINARY} PRIVATE TEST_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}") + target_include_directories(${BINARY} PRIVATE ${CMAKE_SOURCE_DIR}/test) + set_property(TARGET ${BINARY} PROPERTY SUFFIX .test) + target_link_libraries(${BINARY} PRIVATE libmosquitto) +endforeach() diff --git a/test/lib/c/Makefile b/test/lib/c/Makefile index 0bbf341b..5c9b3d04 100644 --- a/test/lib/c/Makefile +++ b/test/lib/c/Makefile @@ -1,7 +1,7 @@ R=../../.. .PHONY: all clean reallyclean -CFLAGS=-I${R}/include -Werror +CFLAGS=-I${R}/include -I${R}/test -Werror LIBS=${R}/lib/libmosquitto.so.1 SRC = \ @@ -65,8 +65,8 @@ TESTS = ${SRC:.c=.test} all : ${TESTS} -${TESTS} : %.test: %.c - $(CC) $< -o $@ $(CFLAGS) $(LIBS) +${TESTS} : %.test: %.c ${R}/test/path_helper.h + $(CC) $< -o $@ -D TEST_SOURCE_DIR='"$(realpath .)"' $(CFLAGS) $(LIBS) reallyclean : clean -rm -f *.orig diff --git a/test/lib/cpp/CMakeLists.txt b/test/lib/cpp/CMakeLists.txt new file mode 100644 index 00000000..c9b56bce --- /dev/null +++ b/test/lib/cpp/CMakeLists.txt @@ -0,0 +1,37 @@ +set(BINARIES + 01-con-discon-success + 01-will-set + 01-unpwd-set + 01-will-unpwd-set + 01-no-clean-session + 01-keepalive-pingreq + 02-subscribe-qos0 + 02-subscribe-qos1 + 02-subscribe-qos2 + 02-unsubscribe + 03-publish-qos0 + 03-publish-qos0-no-payload + 03-publish-c2b-qos1-disconnect + 03-publish-c2b-qos2 + 03-publish-c2b-qos2-disconnect + 03-publish-b2c-qos1 + 03-publish-b2c-qos2 + 04-retain-qos0 + 08-ssl-connect-no-auth + 08-ssl-connect-cert-auth + 08-ssl-connect-cert-auth-enc + 08-ssl-bad-cacert + 08-ssl-fake-cacert + 09-util-topic-tokenise +) + +foreach(BINARY ${BINARIES}) + set(TARGET "${BINARY}-cpp") + add_executable(${TARGET} + ${BINARY}.cpp + ) + set_property(TARGET ${TARGET} + PROPERTY SUFFIX .test + ) + target_link_libraries(${TARGET} PRIVATE mosquittopp) +endforeach() diff --git a/test/lib/mosq_test_helper.py b/test/lib/mosq_test_helper.py index 0e861dba..9b3cf8de 100644 --- a/test/lib/mosq_test_helper.py +++ b/test/lib/mosq_test_helper.py @@ -13,3 +13,9 @@ import ssl import struct import subprocess import time + + +from pathlib import Path + +source_dir = Path(__file__).resolve().parent +ssl_dir = source_dir.parent / "ssl" diff --git a/test/mosq_test.py b/test/mosq_test.py index ce6486ec..a44246d0 100644 --- a/test/mosq_test.py +++ b/test/mosq_test.py @@ -13,7 +13,7 @@ import mqtt5_props import __main__ -import atexit +from pathlib import Path vg_index = 1 vg_logfiles = [] @@ -22,23 +22,29 @@ class TestError(Exception): def __init__(self, message="Mismatched packets"): self.message = message +def get_build_root(): + result = os.getenv("BUILD_ROOT") + if result is None: + result = str(Path(__file__).resolve().parents[1]) + return result + def start_broker(filename, cmd=None, port=0, use_conf=False, expect_fail=False, nolog=False, checkhost="localhost", env=None): global vg_index global vg_logfiles delay = 0.1 - if use_conf == True: - cmd = ['../../src/mosquitto', '-v', '-c', filename.replace('.py', '.conf')] + if use_conf: + cmd = [get_build_root() + '/src/mosquitto', '-v', '-c', filename.replace('.py', '.conf')] if port == 0: port = 1888 else: if cmd is None and port != 0: - cmd = ['../../src/mosquitto', '-v', '-p', str(port)] + cmd = [get_build_root() + '/src/mosquitto', '-v', '-p', str(port)] elif cmd is None and port == 0: port = 1888 - cmd = ['../../src/mosquitto', '-v', '-c', filename.replace('.py', '.conf')] + cmd = [get_build_root() + '/src/mosquitto', '-v', '-c', filename.replace('.py', '.conf')] elif cmd is not None and port == 0: port = 1888 @@ -89,7 +95,7 @@ def start_client(filename, cmd, env, port=1888): cmd = ['valgrind', '-q', '--log-file='+filename+'.vglog'] + cmd cmd = cmd + [str(port)] - return subprocess.Popen(cmd, env=env) + return subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE) def pub_helper(port, proto_ver=4): connect_packet = gen_connect("pub-helper", proto_ver=proto_ver) diff --git a/test/path_helper.h b/test/path_helper.h new file mode 100644 index 00000000..3af9b8f7 --- /dev/null +++ b/test/path_helper.h @@ -0,0 +1,16 @@ +#ifndef PATH_HELPER_H +#define PATH_HELPER_H + +#include +#include +#include +#include +#include + +/* returns / written to */ +void cat_sourcedir_with_relpath(char* dest, const char* relpath) { + strcpy(dest,TEST_SOURCE_DIR); + strcat(dest, relpath); +} + +#endif diff --git a/test/random/random_client.py b/test/random/random_client.py index 3838b37f..917fbe8a 100755 --- a/test/random/random_client.py +++ b/test/random/random_client.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +from pathlib import Path import paho.mqtt.client as paho import random import sys @@ -115,7 +116,9 @@ def main(): mqttc.username_pw_set("bad", "bad") if use_tls: - mqttc.tls_set(ca_certs="../ssl/all-ca.crt") + source_dir = Path(__file__).resolve().parent + ssl_dir = source_dir.parent / "ssl" + mqttc.tls_set(ca_certs=f"{ssl_dir}/all-ca.crt") mqttc.connect("localhost", port) mqttc.loop_start() diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt new file mode 100644 index 00000000..b8d9ec92 --- /dev/null +++ b/test/unit/CMakeLists.txt @@ -0,0 +1,143 @@ +find_package(CUnit REQUIRED) + +add_library(common-unit-test-header INTERFACE) +target_include_directories(common-unit-test-header + INTERFACE + "${mosquitto_SOURCE_DIR}/include" + "${mosquitto_SOURCE_DIR}/common" + "${mosquitto_SOURCE_DIR}/deps" + "${mosquitto_SOURCE_DIR}/lib" + "${mosquitto_SOURCE_DIR}/src" + "${mosquitto_SOURCE_DIR}/test" +) +target_link_libraries(common-unit-test-header INTERFACE config-header CUnit::CUnit) + +# unit-broker +add_executable(broker-test + datatype_read.c + datatype_write.c + misc_trim_test.c + property_add.c + property_read.c + property_user_read.c + property_write.c + stubs.c + util_topic_test.c + utf8.c + # main test files + test.c + ../../lib/packet_datatypes.c + ../../lib/packet_mosq.c + ../../lib/property_mosq.c + ../../lib/memory_mosq.c + ../../common/misc_mosq.c + ../../lib/util_mosq.c + ../../lib/util_topic.c + ../../lib/utf8_mosq.c +) + +target_link_libraries(broker-test PRIVATE common-unit-test-header OpenSSL::SSL) +add_test(NAME unit-broker-test COMMAND broker-test) + +# bridge-topic-test +add_library(bridge-topic-obj OBJECT ../../src/bridge_topic.c) +target_compile_definitions(bridge-topic-obj PRIVATE WITH_BRIDGE WITH_BROKER) +target_link_libraries(bridge-topic-obj PUBLIC common-unit-test-header) + +add_executable(bridge-topic-test + bridge_topic_test.c + stubs.c + ../../lib/memory_mosq.c + ../../src/memory_public.c + ../../lib/util_topic.c +) +target_link_libraries(bridge-topic-test PRIVATE bridge-topic-obj common-unit-test-header) +add_test(NAME unit-bridge-topic-test COMMAND bridge-topic-test) + +# keepalive-test +add_executable(keepalive-test + keepalive_test.c + keepalive_stubs.c + ../../lib/memory_mosq.c +) +target_link_libraries(keepalive-test PRIVATE common-unit-test-header) +add_test(NAME unit-keepalive-test COMMAND keepalive-test) + +# persist-read-test +add_library(persistence-read-obj + OBJECT + ../../src/persist_read_v234.c + ../../src/persist_read_v5.c + ../../src/persist_read.c + ../../src/retain.c + ../../src/topic_tok.c +) +target_compile_definitions(persistence-read-obj PRIVATE WITH_PERSISTENCE WITH_BROKER) +target_link_libraries(persistence-read-obj PUBLIC common-unit-test-header OpenSSL::SSL) + +add_executable(persist-read-test + persist_read_test.c + persist_read_stubs.c + ../../common/misc_mosq.c + ../../lib/memory_mosq.c + ../../lib/packet_datatypes.c + ../../lib/property_mosq.c + ../../lib/utf8_mosq.c + ../../lib/util_mosq.c + ../../src/memory_public.c +) +target_compile_definitions(persist-read-test PRIVATE TEST_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}") +target_link_libraries(persist-read-test PRIVATE persistence-read-obj) +add_test(NAME unit-persist-read-test COMMAND persist-read-test) + +# persist-write-test +add_library(persistence-write-obj + OBJECT + ../../src/database.c + ../../src/persist_read_v234.c + ../../src/persist_read_v5.c + ../../src/persist_read.c + ../../src/persist_write_v5.c + ../../src/persist_write.c + ../../src/retain.c + ../../src/subs.c + ../../src/topic_tok.c +) +target_compile_definitions(persistence-write-obj PRIVATE WITH_PERSISTENCE WITH_BROKER) +target_link_libraries(persistence-write-obj PUBLIC common-unit-test-header) + +add_executable(persist-write-test + persist_write_test.c + persist_write_stubs.c + ../../common/misc_mosq.c + ../../lib/memory_mosq.c + ../../lib/packet_datatypes.c + ../../lib/property_mosq.c + ../../lib/utf8_mosq.c + ../../lib/util_mosq.c + ../../src/memory_public.c + ../../lib/packet_mosq.c +) +target_compile_definitions(persist-write-test PRIVATE TEST_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}") +target_link_libraries(persist-write-test PRIVATE persistence-write-obj OpenSSL::SSL) +add_test(NAME unit-persist-write-test COMMAND persist-write-test) + +# subs-test +add_library(persistence-obj + OBJECT + ../../src/database.c + ../../src/subs.c + ../../src/topic_tok.c +) +target_compile_definitions(persistence-obj PRIVATE WITH_PERSISTENCE WITH_BROKER) +target_link_libraries(persistence-obj PUBLIC common-unit-test-header) + +add_executable(subs-test + subs_stubs.c + subs_test.c + ../../lib/memory_mosq.c + ../../src/memory_public.c +) + +target_link_libraries(subs-test PRIVATE common-unit-test-header persistence-obj) +add_test(NAME unit-subs-test COMMAND subs-test) diff --git a/test/unit/Makefile b/test/unit/Makefile index 7367a291..7a9cf05d 100644 --- a/test/unit/Makefile +++ b/test/unit/Makefile @@ -3,12 +3,12 @@ include ${R}/config.mk .PHONY: all check test test-broker test-lib clean coverage -CPPFLAGS:=$(CPPFLAGS) -I${R} -I${R}/include -I${R}/lib -I${R}/src -I${R}/common +CPPFLAGS:=$(CPPFLAGS) -I${R} -I${R}/include -I${R}/lib -I${R}/src -I${R}/common -I${R}/test ifeq ($(WITH_BUNDLED_DEPS),yes) CPPFLAGS:=$(CPPFLAGS) -I${R}/deps endif -CFLAGS:=$(CFLAGS) -coverage -Wall -ggdb +CFLAGS:=$(CFLAGS) -coverage -Wall -ggdb -D TEST_SOURCE_DIR='"$(realpath .)"' LDFLAGS:=$(LDFLAGS) -coverage LDADD:=$(LDADD) -lcunit diff --git a/test/unit/persist_read_test.c b/test/unit/persist_read_test.c index 85bf8043..21cf039d 100644 --- a/test/unit/persist_read_test.c +++ b/test/unit/persist_read_test.c @@ -5,6 +5,7 @@ #include #include +#include "path_helper.h" #define WITH_BROKER #define WITH_PERSISTENCE @@ -44,7 +45,10 @@ static void TEST_empty_file(void) config.persistence = true; - config.persistence_filepath = "files/persist_read/empty.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/empty.test-db"); + config.persistence_filepath = persistence_filepath; + rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); } @@ -61,11 +65,14 @@ static void TEST_corrupt_header(void) config.persistence = true; - config.persistence_filepath = "files/persist_read/corrupt-header-short.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/corrupt-header-short.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, 1); - config.persistence_filepath = "files/persist_read/corrupt-header-long.test-db"; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/corrupt-header-long.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, 1); } @@ -80,7 +87,9 @@ static void TEST_unsupported_version(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/unsupported-version.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/unsupported-version.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, 1); @@ -97,7 +106,9 @@ static void TEST_v3_config_ok(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v3-cfg.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v3-cfg.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -115,7 +126,9 @@ static void TEST_v4_config_ok(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v4-cfg.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v4-cfg.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -133,7 +146,9 @@ static void TEST_v3_config_truncated(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v3-cfg-truncated.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v3-cfg-truncated.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, 1); @@ -151,7 +166,9 @@ static void TEST_v3_config_bad_dbid(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v3-cfg-bad-dbid.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v3-cfg-bad-dbid.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, 1); @@ -169,7 +186,9 @@ static void TEST_v3_bad_chunk(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v3-bad-chunk.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v3-bad-chunk.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -187,7 +206,9 @@ static void TEST_v3_message_store(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v3-message-store.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v3-message-store.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -222,7 +243,9 @@ static void TEST_v3_client(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v3-client.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v3-client.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -248,7 +271,9 @@ static void TEST_v3_client_message(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v3-client-message.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v3-client-message.test-db"); + config.persistence_filepath = persistence_filepath; config.max_inflight_messages = 20; rc = persist__restore(); @@ -299,7 +324,9 @@ static void TEST_v3_retain(void) retain__init(); config.persistence = true; - config.persistence_filepath = "files/persist_read/v3-retain.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v3-retain.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -349,7 +376,9 @@ static void TEST_v3_sub(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v3-sub.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v3-sub.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -377,7 +406,9 @@ static void TEST_v4_message_store(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v4-message-store.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v4-message-store.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -411,7 +442,9 @@ static void TEST_v6_config_ok(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v6-cfg.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v6-cfg.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -429,7 +462,9 @@ static void TEST_v5_config_truncated(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v5-cfg-truncated.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v5-cfg-truncated.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, 1); @@ -447,7 +482,9 @@ static void TEST_v5_bad_chunk(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v5-bad-chunk.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v5-bad-chunk.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -465,7 +502,9 @@ static void TEST_v6_message_store(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v6-message-store.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v6-message-store.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -504,7 +543,9 @@ static void TEST_v6_message_store_props(void) config.listener_count = 1; config.persistence = true; - config.persistence_filepath = "files/persist_read/v6-message-store-props.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v6-message-store-props.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -542,7 +583,9 @@ static void TEST_v5_client(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v5-client.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v5-client.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -574,7 +617,9 @@ static void TEST_v6_client(void) config.listeners = &listener; config.listener_count = 1; config.persistence = true; - config.persistence_filepath = "files/persist_read/v6-client.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v6-client.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -605,7 +650,9 @@ static void TEST_v6_client_message(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v6-client-message.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v6-client-message.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -652,7 +699,9 @@ static void TEST_v6_client_message_props(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v6-client-message-props.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v6-client-message-props.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -698,7 +747,9 @@ static void TEST_v6_retain(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v6-retain.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v6-retain.test-db"); + config.persistence_filepath = persistence_filepath; retain__init(); rc = persist__restore(); @@ -746,7 +797,9 @@ static void TEST_v6_sub(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v6-sub.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v6-sub.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); diff --git a/test/unit/persist_write_test.c b/test/unit/persist_write_test.c index 9596b48b..60879f39 100644 --- a/test/unit/persist_write_test.c +++ b/test/unit/persist_write_test.c @@ -5,6 +5,7 @@ #include #include +#include "path_helper.h" #define WITH_BROKER #define WITH_PERSISTENCE @@ -105,7 +106,9 @@ static void TEST_empty_file(void) config.persistence_filepath = "empty.db"; rc = persist__backup(false); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); - CU_ASSERT_EQUAL(0, file_diff("files/persist_write/empty.test-db", "empty.db")); + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_write/empty.test-db"); + CU_ASSERT_EQUAL(0, file_diff(persistence_filepath, "empty.db")); unlink("empty.db"); } @@ -120,7 +123,9 @@ static void TEST_v6_config_ok(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v6-cfg.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v6-cfg.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -128,7 +133,7 @@ static void TEST_v6_config_ok(void) rc = persist__backup(true); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); - CU_ASSERT_EQUAL(0, file_diff("files/persist_read/v6-cfg.test-db", "v6-cfg.db")); + CU_ASSERT_EQUAL(0, file_diff(persistence_filepath, "v6-cfg.db")); unlink("v6-cfg.db"); } @@ -143,7 +148,9 @@ static void TEST_v6_message_store_no_ref(void) db.config = &config; config.persistence = true; - config.persistence_filepath = "files/persist_read/v6-message-store.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v6-message-store.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -151,7 +158,9 @@ static void TEST_v6_message_store_no_ref(void) rc = persist__backup(true); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); - CU_ASSERT_EQUAL(0, file_diff("files/persist_write/v6-message-store-no-ref.test-db", "v6-message-store-no-ref.db")); + char persistence_filepath_no_ref[4096]; + cat_sourcedir_with_relpath(persistence_filepath_no_ref, "/files/persist_write/v6-message-store-no-ref.test-db"); + CU_ASSERT_EQUAL(0, file_diff(persistence_filepath_no_ref, "v6-message-store-no-ref.db")); unlink("v6-message-store-no-ref.db"); } @@ -172,7 +181,9 @@ static void TEST_v6_message_store_props(void) config.listener_count = 1; config.persistence = true; - config.persistence_filepath = "files/persist_read/v6-message-store-props.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v6-message-store-props.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -180,7 +191,7 @@ static void TEST_v6_message_store_props(void) rc = persist__backup(true); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); - CU_ASSERT_EQUAL(0, file_diff("files/persist_read/v6-message-store-props.test-db", "v6-message-store-props.db")); + CU_ASSERT_EQUAL(0, file_diff(persistence_filepath, "v6-message-store-props.db")); unlink("v6-message-store-props.db"); } @@ -201,7 +212,9 @@ static void TEST_v6_client(void) config.listener_count = 1; config.persistence = true; - config.persistence_filepath = "files/persist_read/v6-client.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v6-client.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -209,7 +222,7 @@ static void TEST_v6_client(void) rc = persist__backup(true); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); - CU_ASSERT_EQUAL(0, file_diff("files/persist_read/v6-client.test-db", "v6-client.db")); + CU_ASSERT_EQUAL(0, file_diff(persistence_filepath, "v6-client.db")); unlink("v6-client.db"); } @@ -230,7 +243,9 @@ static void TEST_v6_client_message(void) config.listener_count = 1; config.persistence = true; - config.persistence_filepath = "files/persist_read/v6-client-message.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v6-client-message.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -238,7 +253,7 @@ static void TEST_v6_client_message(void) rc = persist__backup(true); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); - CU_ASSERT_EQUAL(0, file_diff("files/persist_read/v6-client-message.test-db", "v6-client-message.db")); + CU_ASSERT_EQUAL(0, file_diff(persistence_filepath, "v6-client-message.db")); unlink("v6-client-message.db"); } @@ -259,7 +274,9 @@ static void TEST_v6_client_message_props(void) config.listener_count = 1; config.persistence = true; - config.persistence_filepath = "files/persist_read/v6-client-message-props.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v6-client-message-props.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -275,7 +292,7 @@ static void TEST_v6_client_message_props(void) rc = persist__backup(true); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); - CU_ASSERT_EQUAL(0, file_diff("files/persist_read/v6-client-message-props.test-db", "v6-client-message-props.db")); + CU_ASSERT_EQUAL(0, file_diff(persistence_filepath, "v6-client-message-props.db")); //unlink("v6-client-message-props.db"); } @@ -298,7 +315,9 @@ static void TEST_v6_sub(void) db__open(&config); config.persistence = true; - config.persistence_filepath = "files/persist_read/v6-sub.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_read/v6-sub.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -306,7 +325,7 @@ static void TEST_v6_sub(void) rc = persist__backup(true); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); - CU_ASSERT_EQUAL(0, file_diff("files/persist_read/v6-sub.test-db", "v6-sub.db")); + CU_ASSERT_EQUAL(0, file_diff(persistence_filepath, "v6-sub.db")); unlink("v6-sub.db"); } @@ -325,7 +344,9 @@ static void TEST_v5_full(void) db__open(&config); config.persistence = true; - config.persistence_filepath = "files/persist_write/v5-full.test-db"; + char persistence_filepath[4096]; + cat_sourcedir_with_relpath(persistence_filepath, "/files/persist_write/v5-full.test-db"); + config.persistence_filepath = persistence_filepath; rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); @@ -333,7 +354,7 @@ static void TEST_v5_full(void) rc = persist__backup(true); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); - CU_ASSERT_EQUAL(0, file_diff("files/persist_write/v5-full.test-db", "v5-full.db")); + CU_ASSERT_EQUAL(0, file_diff(persistence_filepath, "v5-full.db")); unlink("v5-full.db"); } #endif From d66702ba37ebb689d259bff80908003544b24812 Mon Sep 17 00:00:00 2001 From: Kai Buschulte Date: Wed, 29 Jun 2022 15:31:19 +0200 Subject: [PATCH 18/20] Fix CMake build for versions <3.19 CMake <3.19 does not support interface targets with sources. For better IDE integrations we still can add the config.h using the `target_sources` command. Signed-off-by: Kai Buschulte --- CMakeLists.txt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 77ef82d1..4ab59cb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -104,10 +104,8 @@ option(WITH_APPS "Build apps?" ON) option(WITH_PLUGINS "Build plugins?" ON) option(DOCUMENTATION "Build documentation?" ON) -add_library(config-header - INTERFACE - config.h -) +add_library(config-header INTERFACE) +target_sources(config-header INTERFACE config.h) target_include_directories(config-header INTERFACE ${mosquitto_SOURCE_DIR} From 5b02490fd28af1413301cec608ab87ab0ab8874e Mon Sep 17 00:00:00 2001 From: Kai Buschulte Date: Wed, 29 Jun 2022 15:42:34 +0200 Subject: [PATCH 19/20] Introduce a CMake WITH_TESTS option To enable or disable tests in the build step and to circumvent the CUnit build dependency. Signed-off-by: Kai Buschulte --- CMakeLists.txt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 77ef82d1..f19a2c1b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ option(WITH_BUNDLED_DEPS "Build with bundled dependencies?" ON) option(WITH_TLS "Include SSL/TLS support?" ON) option(WITH_TLS_PSK "Include TLS-PSK support (requires WITH_TLS)?" ON) option(WITH_EC "Include Elliptic Curve support (requires WITH_TLS)?" ON) +option(WITH_TESTS "Enable tests" ON) if (WITH_TLS) find_package(OpenSSL REQUIRED) add_definitions("-DWITH_TLS") @@ -162,6 +163,7 @@ install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libmosquittopp.pc" DESTINATION "${CMA # ======================================== # Testing # ======================================== -enable_testing() - -add_subdirectory(test) +if(WITH_TESTS) + enable_testing() + add_subdirectory(test) +endif() From 4093e717f979a359e052060125a0fedacfb0da38 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 7 Jul 2022 13:37:33 +0100 Subject: [PATCH 20/20] Minor refactor --- src/handle_connect.c | 214 ++++++++++++++++++++++--------------------- 1 file changed, 110 insertions(+), 104 deletions(-) diff --git a/src/handle_connect.c b/src/handle_connect.c index 676cfff8..18ed4679 100644 --- a/src/handle_connect.c +++ b/src/handle_connect.c @@ -460,6 +460,114 @@ static int check_protocol_version(struct mosquitto__listener *listener, int prot } +#ifdef WITH_TLS +static int get_username_from_cert(struct mosquitto *context) +{ + int i; + X509 *client_cert = NULL; + X509_NAME *name; + X509_NAME_ENTRY *name_entry; + ASN1_STRING *name_asn1 = NULL; + BIO *subject_bio; + char *data_start; + long name_length; + char *subject; + + client_cert = SSL_get_peer_certificate(context->ssl); + if(!client_cert){ + if(context->protocol == mosq_p_mqtt5){ + send__connack(context, 0, MQTT_RC_BAD_USERNAME_OR_PASSWORD, NULL); + }else{ + send__connack(context, 0, CONNACK_REFUSED_BAD_USERNAME_PASSWORD, NULL); + } + return MOSQ_ERR_AUTH; + } + name = X509_get_subject_name(client_cert); + if(!name){ + if(context->protocol == mosq_p_mqtt5){ + send__connack(context, 0, MQTT_RC_BAD_USERNAME_OR_PASSWORD, NULL); + }else{ + send__connack(context, 0, CONNACK_REFUSED_BAD_USERNAME_PASSWORD, NULL); + } + X509_free(client_cert); + return MOSQ_ERR_AUTH; + } + if(context->listener->use_identity_as_username){ /* use_identity_as_username */ + i = X509_NAME_get_index_by_NID(name, NID_commonName, -1); + if(i == -1){ + if(context->protocol == mosq_p_mqtt5){ + send__connack(context, 0, MQTT_RC_BAD_USERNAME_OR_PASSWORD, NULL); + }else{ + send__connack(context, 0, CONNACK_REFUSED_BAD_USERNAME_PASSWORD, NULL); + } + X509_free(client_cert); + return MOSQ_ERR_AUTH; + } + name_entry = X509_NAME_get_entry(name, i); + if(name_entry){ + name_asn1 = X509_NAME_ENTRY_get_data(name_entry); + if (name_asn1 == NULL) { + if(context->protocol == mosq_p_mqtt5){ + send__connack(context, 0, MQTT_RC_BAD_USERNAME_OR_PASSWORD, NULL); + }else{ + send__connack(context, 0, CONNACK_REFUSED_BAD_USERNAME_PASSWORD, NULL); + } + X509_free(client_cert); + return MOSQ_ERR_AUTH; + } +#if OPENSSL_VERSION_NUMBER < 0x10100000L + context->username = mosquitto__strdup((char *) ASN1_STRING_data(name_asn1)); +#else + context->username = mosquitto__strdup((char *) ASN1_STRING_get0_data(name_asn1)); +#endif + if(!context->username){ + if(context->protocol == mosq_p_mqtt5){ + send__connack(context, 0, MQTT_RC_SERVER_UNAVAILABLE, NULL); + }else{ + send__connack(context, 0, CONNACK_REFUSED_SERVER_UNAVAILABLE, NULL); + } + X509_free(client_cert); + return MOSQ_ERR_NOMEM; + } + /* Make sure there isn't an embedded NUL character in the CN */ + if ((size_t)ASN1_STRING_length(name_asn1) != strlen(context->username)) { + if(context->protocol == mosq_p_mqtt5){ + send__connack(context, 0, MQTT_RC_BAD_USERNAME_OR_PASSWORD, NULL); + }else{ + send__connack(context, 0, CONNACK_REFUSED_BAD_USERNAME_PASSWORD, NULL); + } + X509_free(client_cert); + return MOSQ_ERR_AUTH; + } + } + } else { /* use_subject_as_username */ + subject_bio = BIO_new(BIO_s_mem()); + X509_NAME_print_ex(subject_bio, X509_get_subject_name(client_cert), 0, XN_FLAG_RFC2253); + data_start = NULL; + name_length = BIO_get_mem_data(subject_bio, &data_start); + subject = mosquitto__malloc(sizeof(char)*(size_t)(name_length+1)); + if(!subject){ + BIO_free(subject_bio); + X509_free(client_cert); + return MOSQ_ERR_NOMEM; + } + memcpy(subject, data_start, (size_t)name_length); + subject[name_length] = '\0'; + BIO_free(subject_bio); + context->username = subject; + } + if(!context->username){ + X509_free(client_cert); + return MOSQ_ERR_AUTH; + } + X509_free(client_cert); + client_cert = NULL; + + return MOSQ_ERR_SUCCESS; +} +#endif + + int handle__connect(struct mosquitto *context) { char protocol_name[7]; @@ -479,17 +587,6 @@ int handle__connect(struct mosquitto *context) void *auth_data_out = NULL; uint16_t auth_data_out_len = 0; bool allow_zero_length_clientid; -#ifdef WITH_TLS - int i; - X509 *client_cert = NULL; - X509_NAME *name; - X509_NAME_ENTRY *name_entry; - ASN1_STRING *name_asn1 = NULL; - BIO *subject_bio; - char *data_start; - long name_length; - char *subject; -#endif G_CONNECTION_COUNT_INC(); @@ -807,96 +904,8 @@ int handle__connect(struct mosquitto *context) } }else{ #endif /* FINAL_WITH_TLS_PSK */ - client_cert = SSL_get_peer_certificate(context->ssl); - if(!client_cert){ - if(context->protocol == mosq_p_mqtt5){ - send__connack(context, 0, MQTT_RC_BAD_USERNAME_OR_PASSWORD, NULL); - }else{ - send__connack(context, 0, CONNACK_REFUSED_BAD_USERNAME_PASSWORD, NULL); - } - rc = MOSQ_ERR_AUTH; - goto handle_connect_error; - } - name = X509_get_subject_name(client_cert); - if(!name){ - if(context->protocol == mosq_p_mqtt5){ - send__connack(context, 0, MQTT_RC_BAD_USERNAME_OR_PASSWORD, NULL); - }else{ - send__connack(context, 0, CONNACK_REFUSED_BAD_USERNAME_PASSWORD, NULL); - } - rc = MOSQ_ERR_AUTH; - goto handle_connect_error; - } - if (context->listener->use_identity_as_username) { /* use_identity_as_username */ - i = X509_NAME_get_index_by_NID(name, NID_commonName, -1); - if(i == -1){ - if(context->protocol == mosq_p_mqtt5){ - send__connack(context, 0, MQTT_RC_BAD_USERNAME_OR_PASSWORD, NULL); - }else{ - send__connack(context, 0, CONNACK_REFUSED_BAD_USERNAME_PASSWORD, NULL); - } - rc = MOSQ_ERR_AUTH; - goto handle_connect_error; - } - name_entry = X509_NAME_get_entry(name, i); - if(name_entry){ - name_asn1 = X509_NAME_ENTRY_get_data(name_entry); - if (name_asn1 == NULL) { - if(context->protocol == mosq_p_mqtt5){ - send__connack(context, 0, MQTT_RC_BAD_USERNAME_OR_PASSWORD, NULL); - }else{ - send__connack(context, 0, CONNACK_REFUSED_BAD_USERNAME_PASSWORD, NULL); - } - rc = MOSQ_ERR_AUTH; - goto handle_connect_error; - } -#if OPENSSL_VERSION_NUMBER < 0x10100000L - context->username = mosquitto__strdup((char *) ASN1_STRING_data(name_asn1)); -#else - context->username = mosquitto__strdup((char *) ASN1_STRING_get0_data(name_asn1)); -#endif - if(!context->username){ - if(context->protocol == mosq_p_mqtt5){ - send__connack(context, 0, MQTT_RC_SERVER_UNAVAILABLE, NULL); - }else{ - send__connack(context, 0, CONNACK_REFUSED_SERVER_UNAVAILABLE, NULL); - } - rc = MOSQ_ERR_NOMEM; - goto handle_connect_error; - } - /* Make sure there isn't an embedded NUL character in the CN */ - if ((size_t)ASN1_STRING_length(name_asn1) != strlen(context->username)) { - if(context->protocol == mosq_p_mqtt5){ - send__connack(context, 0, MQTT_RC_BAD_USERNAME_OR_PASSWORD, NULL); - }else{ - send__connack(context, 0, CONNACK_REFUSED_BAD_USERNAME_PASSWORD, NULL); - } - rc = MOSQ_ERR_AUTH; - goto handle_connect_error; - } - } - } else { /* use_subject_as_username */ - subject_bio = BIO_new(BIO_s_mem()); - X509_NAME_print_ex(subject_bio, X509_get_subject_name(client_cert), 0, XN_FLAG_RFC2253); - data_start = NULL; - name_length = BIO_get_mem_data(subject_bio, &data_start); - subject = mosquitto__malloc(sizeof(char)*(size_t)(name_length+1)); - if(!subject){ - BIO_free(subject_bio); - rc = MOSQ_ERR_NOMEM; - goto handle_connect_error; - } - memcpy(subject, data_start, (size_t)name_length); - subject[name_length] = '\0'; - BIO_free(subject_bio); - context->username = subject; - } - if(!context->username){ - rc = MOSQ_ERR_AUTH; - goto handle_connect_error; - } - X509_free(client_cert); - client_cert = NULL; + rc = get_username_from_cert(context); + if(rc) goto handle_connect_error; #ifdef FINAL_WITH_TLS_PSK } #endif /* FINAL_WITH_TLS_PSK */ @@ -1022,9 +1031,6 @@ handle_connect_error: mosquitto__FREE(will_struct); } context->will = NULL; -#ifdef WITH_TLS - if(client_cert) X509_free(client_cert); -#endif /* We return an error here which means the client is freed later on. */ context->clean_start = true; context->session_expiry_interval = 0;