From 028adb2444fc6e62ee975615aa5690bee2a0a890 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Tue, 26 Mar 2024 12:19:02 +0000 Subject: [PATCH] Test leak fixes --- src/bridge_topic.c | 18 +++ src/mosquitto_broker_internal.h | 1 + test/lib/c/fuzzish.c | 6 +- test/unit/broker/Makefile | 3 +- test/unit/broker/bridge_topic_test.c | 4 + test/unit/broker/persist_read_stubs.c | 218 ++++++++++++-------------- test/unit/broker/persist_read_test.c | 30 ++++ test/unit/broker/persist_write_test.c | 34 ++++ 8 files changed, 197 insertions(+), 117 deletions(-) diff --git a/src/bridge_topic.c b/src/bridge_topic.c index 0391cdefc2..a1608f590a 100644 --- a/src/bridge_topic.c +++ b/src/bridge_topic.c @@ -132,6 +132,24 @@ static struct mosquitto__bridge_topic *bridge__find_topic(struct mosquitto__brid } +void bridge__cleanup_topics(struct mosquitto__bridge *bridge) +{ + struct mosquitto__bridge_topic *topic, *topic_tmp; + + if(!bridge) return; + + LL_FOREACH_SAFE(bridge->topics, topic, topic_tmp){ + LL_DELETE(bridge->topics, topic); + mosquitto_free(topic->local_prefix); + mosquitto_free(topic->remote_prefix); + mosquitto_free(topic->local_topic); + mosquitto_free(topic->remote_topic); + mosquitto_free(topic->topic); + mosquitto_free(topic); + } +} + + /* topic [[[out | in | both] qos-level] local-prefix remote-prefix] */ int bridge__add_topic(struct mosquitto__bridge *bridge, const char *topic, enum mosquitto__bridge_direction direction, uint8_t qos, const char *local_prefix, const char *remote_prefix) { diff --git a/src/mosquitto_broker_internal.h b/src/mosquitto_broker_internal.h index 22c8cfe8b9..f4ad5be029 100644 --- a/src/mosquitto_broker_internal.h +++ b/src/mosquitto_broker_internal.h @@ -797,6 +797,7 @@ int bridge__on_connect(struct mosquitto *context); void bridge_check(void); int bridge__register_local_connections(void); int bridge__add_topic(struct mosquitto__bridge *bridge, const char *topic, enum mosquitto__bridge_direction direction, uint8_t qos, const char *local_prefix, const char *remote_prefix); +void bridge__cleanup_topics(struct mosquitto__bridge *bridge); int bridge__remap_topic_in(struct mosquitto *context, char **topic); #endif diff --git a/test/lib/c/fuzzish.c b/test/lib/c/fuzzish.c index 8091352a22..0f5a0e254b 100644 --- a/test/lib/c/fuzzish.c +++ b/test/lib/c/fuzzish.c @@ -257,7 +257,6 @@ static void on_message_v5(struct mosquitto *mosq, void *obj, const struct mosqui static void on_subscribe(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos) { UNUSED(mosq); - UNUSED(obj); UNUSED(mid); int tot = 0; @@ -269,12 +268,13 @@ static void on_subscribe(struct mosquitto *mosq, void *obj, int mid, int qos_cou for(int i=0; i #include #include +#ifndef WITH_SYS_TREE +# define WITH_SYS_TREE +#endif +#include extern char *last_sub; extern int last_qos; @@ -24,73 +28,6 @@ struct mosquitto *context__init(void) return m; } -void db__msg_store_free(struct mosquitto__base_msg *store) -{ - int i; - - mosquitto_free(store->data.source_id); - mosquitto_free(store->data.source_username); - if(store->dest_ids){ - for(i=0; idest_id_count; i++){ - mosquitto_free(store->dest_ids[i]); - } - mosquitto_free(store->dest_ids); - } - mosquitto_free(store->data.topic); - mosquitto_property_free_all(&store->data.properties); - mosquitto_free(store->data.payload); - mosquitto_free(store); -} - -int db__message_store(const struct mosquitto *source, struct mosquitto__base_msg *stored, uint32_t *message_expiry_interval, enum mosquitto_msg_origin origin) -{ - int rc = MOSQ_ERR_SUCCESS; - - UNUSED(origin); - - if(source && source->id){ - stored->data.source_id = mosquitto_strdup(source->id); - }else{ - stored->data.source_id = mosquitto_strdup(""); - } - if(!stored->data.source_id){ - rc = MOSQ_ERR_NOMEM; - goto error; - } - - if(source && source->username){ - stored->data.source_username = mosquitto_strdup(source->username); - if(!stored->data.source_username){ - rc = MOSQ_ERR_NOMEM; - goto error; - } - } - if(source){ - stored->source_listener = source->listener; - } - if(message_expiry_interval){ - stored->data.expiry_time = time(NULL) + (*message_expiry_interval); - }else{ - stored->data.expiry_time = 0; - } - - stored->dest_ids = NULL; - stored->dest_id_count = 0; - db.msg_store_count++; - db.msg_store_bytes += stored->data.payloadlen; - - if(!stored->data.store_id){ - stored->data.store_id = ++db.last_db_id; - } - - HASH_ADD(hh, db.msg_store, data.store_id, sizeof(stored->data.store_id), stored); - - return MOSQ_ERR_SUCCESS; -error: - db__msg_store_free(stored); - return rc; -} - int log__printf(struct mosquitto *mosq, unsigned int priority, const char *fmt, ...) { UNUSED(mosq); @@ -159,41 +96,6 @@ int sub__add(struct mosquitto *context, const struct mosquitto_subscription *sub return MOSQ_ERR_SUCCESS; } -int db__message_insert_incoming(struct mosquitto *context, uint64_t cmsg_id, struct mosquitto__base_msg *msg, bool persist) -{ - UNUSED(context); - UNUSED(cmsg_id); - UNUSED(msg); - UNUSED(persist); - - return MOSQ_ERR_SUCCESS; -} - -int db__message_insert_outgoing(struct mosquitto *context, uint64_t cmsg_id, uint16_t mid, uint8_t qos, bool retain, struct mosquitto__base_msg *stored, uint32_t subscription_identifier, bool update, bool persist) -{ - UNUSED(context); - UNUSED(cmsg_id); - UNUSED(mid); - UNUSED(qos); - UNUSED(retain); - UNUSED(stored); - UNUSED(subscription_identifier); - UNUSED(update); - UNUSED(persist); - - return MOSQ_ERR_SUCCESS; -} - -void db__msg_store_ref_dec(struct mosquitto__base_msg **store) -{ - UNUSED(store); -} - -void db__msg_store_ref_inc(struct mosquitto__base_msg *store) -{ - store->ref_count++; -} - void callback__on_disconnect(struct mosquitto *mosq, int rc, const mosquitto_property *props) { UNUSED(mosq); @@ -201,18 +103,6 @@ void callback__on_disconnect(struct mosquitto *mosq, int rc, const mosquitto_pro UNUSED(props); } -void db__msg_add_to_inflight_stats(struct mosquitto_msg_data *msg_data, struct mosquitto__client_msg *msg) -{ - UNUSED(msg_data); - UNUSED(msg); -} - -void db__msg_add_to_queued_stats(struct mosquitto_msg_data *msg_data, struct mosquitto__client_msg *msg) -{ - UNUSED(msg_data); - UNUSED(msg); -} - void context__add_to_by_id(struct mosquitto *context) { if(context->in_by_id == false){ @@ -261,3 +151,103 @@ void mosquitto_log_printf(int level, const char *fmt, ...) UNUSED(level); UNUSED(fmt); } +struct mosquitto__subhier *sub__add_hier_entry(struct mosquitto__subhier *parent, struct mosquitto__subhier **sibling, const char *topic, uint16_t len) +{ + UNUSED(parent); + UNUSED(sibling); + UNUSED(topic); + UNUSED(len); + + return NULL; +} +void plugin_persist__handle_client_msg_add(struct mosquitto *context, const struct mosquitto__client_msg *cmsg) +{ + UNUSED(context); + UNUSED(cmsg); +} +void plugin_persist__handle_client_msg_delete(struct mosquitto *context, const struct mosquitto__client_msg *cmsg) +{ + UNUSED(context); + UNUSED(cmsg); +} +void plugin_persist__handle_client_msg_update(struct mosquitto *context, const struct mosquitto__client_msg *cmsg) +{ + UNUSED(context); + UNUSED(cmsg); +} +void plugin_persist__handle_client_msg_clear(struct mosquitto *context, uint8_t direction) +{ + UNUSED(context); + UNUSED(direction); +} +void plugin_persist__handle_base_msg_delete(struct mosquitto__base_msg *msg) +{ + UNUSED(msg); +} +void plugin_persist__handle_subscription_delete(struct mosquitto *context, char *sub) +{ + UNUSED(context); + UNUSED(sub); +} +int sub__messages_queue(const char *source_id, const char *topic, uint8_t qos, int retain, struct mosquitto__base_msg **base_msg) +{ + UNUSED(source_id); + UNUSED(topic); + UNUSED(qos); + UNUSED(retain); + *base_msg = NULL; + return 0; +} +void metrics__int_inc(enum mosq_metric_type m, int64_t value) +{ + UNUSED(m); UNUSED(value); +} +void metrics__int_dec(enum mosq_metric_type m, int64_t value) +{ + UNUSED(m); UNUSED(value); +} +int send__publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint32_t payloadlen, const void *payload, uint8_t qos, bool retain, bool dup, uint32_t subscription_identifier, const mosquitto_property *store_props, uint32_t expiry_interval) +{ + UNUSED(mosq); + UNUSED(mid); + UNUSED(topic); + UNUSED(payloadlen); + UNUSED(payload); + UNUSED(qos); + UNUSED(retain); + UNUSED(dup); + UNUSED(subscription_identifier); + UNUSED(store_props); + UNUSED(expiry_interval); + + return MOSQ_ERR_SUCCESS; +} + +int send__pubcomp(struct mosquitto *mosq, uint16_t mid, const mosquitto_property *properties) +{ + UNUSED(mosq); + UNUSED(mid); + UNUSED(properties); + + return MOSQ_ERR_SUCCESS; +} + +int send__pubrec(struct mosquitto *mosq, uint16_t mid, uint8_t reason_code, const mosquitto_property *properties) +{ + UNUSED(mosq); + UNUSED(mid); + UNUSED(reason_code); + UNUSED(properties); + + return MOSQ_ERR_SUCCESS; +} + +int send__pubrel(struct mosquitto *mosq, uint16_t mid, const mosquitto_property *properties) +{ + UNUSED(mosq); + UNUSED(mid); + UNUSED(properties); + + return MOSQ_ERR_SUCCESS; +} + diff --git a/test/unit/broker/persist_read_test.c b/test/unit/broker/persist_read_test.c index a17699f2e8..a6f7c31773 100644 --- a/test/unit/broker/persist_read_test.c +++ b/test/unit/broker/persist_read_test.c @@ -18,6 +18,20 @@ uint32_t last_identifier; struct mosquitto_db db; +static void test_cleanup(void) +{ + struct mosquitto *ctxt, *ctxt_tmp; + + HASH_ITER(hh_id, db.contexts_by_id, ctxt, ctxt_tmp){ + HASH_DELETE(hh_id, db.contexts_by_id, ctxt); + mosquitto_free(ctxt->username); + mosquitto_free(ctxt->id); + db__messages_delete(ctxt, true); + mosquitto_free(ctxt); + } + db__close(); +} + static void TEST_persistence_disabled(void) { struct mosquitto__config config; @@ -29,6 +43,7 @@ static void TEST_persistence_disabled(void) rc = persist__restore(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); + test_cleanup(); } @@ -48,6 +63,7 @@ static void TEST_empty_file(void) config.persistence_filepath = persistence_filepath; rc = persist__restore(); + test_cleanup(); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); } @@ -228,6 +244,7 @@ static void TEST_v3_message_store(void) CU_ASSERT_NSTRING_EQUAL(db.msg_store->data.payload, "payload", 7); } } + test_cleanup(); } static void TEST_v3_client(void) @@ -256,6 +273,7 @@ static void TEST_v3_client(void) CU_ASSERT_PTR_NULL(context->msgs_out.inflight); CU_ASSERT_EQUAL(context->last_mid, 0x5287); } + test_cleanup(); } static void TEST_v3_client_message(void) @@ -309,6 +327,7 @@ static void TEST_v3_client_message(void) CU_ASSERT_EQUAL(context->msgs_out.inflight->data.subscription_identifier, 0); } } + test_cleanup(); } static void TEST_v3_retain(void) @@ -358,6 +377,7 @@ static void TEST_v3_retain(void) } } } + test_cleanup(); } static void TEST_v3_sub(void) @@ -392,6 +412,7 @@ static void TEST_v3_sub(void) } CU_ASSERT_EQUAL(last_qos, 1); } + test_cleanup(); } static void TEST_v4_message_store(void) @@ -428,6 +449,7 @@ static void TEST_v4_message_store(void) CU_ASSERT_NSTRING_EQUAL(db.msg_store->data.payload, "payload", 7); } } + test_cleanup(); } static void TEST_v6_config_ok(void) @@ -522,6 +544,7 @@ static void TEST_v6_message_store(void) } CU_ASSERT_PTR_NULL(db.msg_store->data.properties); } + test_cleanup(); } @@ -568,6 +591,7 @@ static void TEST_v6_message_store_props(void) } CU_ASSERT_PTR_NOT_NULL(db.msg_store->source_listener); } + test_cleanup(); } static void TEST_v5_client(void) @@ -596,6 +620,7 @@ static void TEST_v5_client(void) CU_ASSERT_PTR_NULL(context->msgs_out.inflight); CU_ASSERT_EQUAL(context->last_mid, 0x5287); } + test_cleanup(); } static void TEST_v6_client(void) @@ -635,6 +660,7 @@ static void TEST_v6_client(void) CU_ASSERT_STRING_EQUAL(context->username, "usrname"); } } + test_cleanup(); } static void TEST_v6_client_message(void) @@ -684,6 +710,7 @@ static void TEST_v6_client_message(void) CU_ASSERT_EQUAL(context->msgs_out.inflight->data.subscription_identifier, 0); } } + test_cleanup(); } static void TEST_v6_client_message_props(void) @@ -733,6 +760,7 @@ static void TEST_v6_client_message_props(void) CU_ASSERT_EQUAL(context->msgs_out.inflight->data.subscription_identifier, 1); } } + test_cleanup(); } static void TEST_v6_retain(void) @@ -779,6 +807,7 @@ static void TEST_v6_retain(void) } } } + test_cleanup(); } static void TEST_v6_sub(void) @@ -814,6 +843,7 @@ static void TEST_v6_sub(void) CU_ASSERT_EQUAL(last_qos, 1); CU_ASSERT_EQUAL(last_identifier, 0x7623); } + test_cleanup(); } /* ======================================================================== diff --git a/test/unit/broker/persist_write_test.c b/test/unit/broker/persist_write_test.c index 2fe93f9891..9557303f84 100644 --- a/test/unit/broker/persist_write_test.c +++ b/test/unit/broker/persist_write_test.c @@ -16,6 +16,22 @@ int last_qos; struct mosquitto_db db; +static void test_cleanup(void) +{ + struct mosquitto *ctxt, *ctxt_tmp; + + HASH_ITER(hh_id, db.contexts_by_id, ctxt, ctxt_tmp){ + HASH_DELETE(hh_id, db.contexts_by_id, ctxt); + mosquitto_free(ctxt->username); + mosquitto_free(ctxt->id); + db__messages_delete(ctxt, true); + sub__clean_session(ctxt); + mosquitto_free(ctxt); + } + + db__close(); +} + /* read entire file into memory */ static int file_read(const char *filename, uint8_t **data, size_t *len) { @@ -92,6 +108,8 @@ static void TEST_persistence_disabled(void) config.persistence_filepath = "disabled.db"; rc = persist__backup(false); CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); + + test_cleanup(); } @@ -113,6 +131,8 @@ static void TEST_empty_file(void) 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"); + + test_cleanup(); } @@ -138,6 +158,8 @@ static void TEST_v6_config_ok(void) CU_ASSERT_EQUAL(0, file_diff(persistence_filepath, "v6-cfg.db")); unlink("v6-cfg.db"); + + test_cleanup(); } @@ -165,6 +187,8 @@ static void TEST_v6_message_store_no_ref(void) 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"); + + test_cleanup(); } @@ -196,6 +220,8 @@ static void TEST_v6_message_store_props(void) CU_ASSERT_EQUAL(0, file_diff(persistence_filepath, "v6-message-store-props.db")); unlink("v6-message-store-props.db"); + + test_cleanup(); } @@ -227,6 +253,8 @@ static void TEST_v6_client(void) CU_ASSERT_EQUAL(0, file_diff(persistence_filepath, "v6-client.db")); unlink("v6-client.db"); + + test_cleanup(); } @@ -258,6 +286,8 @@ static void TEST_v6_client_message(void) CU_ASSERT_EQUAL(0, file_diff(persistence_filepath, "v6-client-message.db")); unlink("v6-client-message.db"); + + test_cleanup(); } @@ -297,6 +327,8 @@ static void TEST_v6_client_message_props(void) CU_ASSERT_EQUAL(0, file_diff(persistence_filepath, "v6-client-message-props.db")); //unlink("v6-client-message-props.db"); + + test_cleanup(); } @@ -330,6 +362,8 @@ static void TEST_v6_sub(void) CU_ASSERT_EQUAL(0, file_diff(persistence_filepath, "v6-sub.db")); unlink("v6-sub.db"); + + test_cleanup(); }