Skip to content

Commit

Permalink
Test leak fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Mar 26, 2024
1 parent 4da5081 commit 028adb2
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 117 deletions.
18 changes: 18 additions & 0 deletions src/bridge_topic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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)
{
Expand Down
1 change: 1 addition & 0 deletions src/mosquitto_broker_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions test/lib/c/fuzzish.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -269,12 +268,13 @@ static void on_subscribe(struct mosquitto *mosq, void *obj, int mid, int qos_cou
for(int i=0; i<qos_count; i++){
tot += granted_qos[i];
}

(void)tot;
}

static void on_subscribe_v5(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos, const mosquitto_property *props)
{
UNUSED(mosq);
UNUSED(obj);
UNUSED(mid);

int tot = 0;
Expand All @@ -287,6 +287,8 @@ static void on_subscribe_v5(struct mosquitto *mosq, void *obj, int mid, int qos_
tot += granted_qos[i];
}
prop_test(props);

(void)tot;
}

static void on_unsubscribe(struct mosquitto *mosq, void *obj, int mid)
Expand Down
3 changes: 2 additions & 1 deletion test/unit/broker/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include ${R}/make/broker.mk

.PHONY: all check test test-compile clean coverage

LOCAL_CFLAGS+=-coverage
LOCAL_CFLAGS+=-coverage -ggdb
LOCAL_CPPFLAGS+=-DWITH_BROKER -I${R}/src -I${R}/test -I${R}/lib -DTEST_SOURCE_DIR='"$(realpath .)"' -I${R}/lib -I${R}/libcommon
LOCAL_LDFLAGS+=-coverage
LOCAL_LDADD+=-lcunit ${LIBMOSQ_COMMON}
Expand Down Expand Up @@ -33,6 +33,7 @@ PERSIST_READ_TEST_OBJS = \
persist_read_stubs.o

PERSIST_READ_OBJS = \
${R}/src/database.o \
${R}/src/packet_datatypes.o \
${R}/src/persist_read.o \
${R}/src/persist_read_v234.o \
Expand Down
4 changes: 4 additions & 0 deletions test/unit/broker/bridge_topic_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ static void map_valid_helper(const char *topic, const char *local_prefix, const
CU_ASSERT_STRING_EQUAL(map_topic, expected);
free(map_topic);
}

bridge__cleanup_topics(&bridge);
}

static void map_invalid_helper(const char *topic, const char *local_prefix, const char *remote_prefix)
Expand All @@ -46,6 +48,8 @@ static void map_invalid_helper(const char *topic, const char *local_prefix, cons

rc = bridge__add_topic(&bridge, topic, bd_in, 0, local_prefix, remote_prefix);
CU_ASSERT_NOT_EQUAL(rc, 0);

bridge__cleanup_topics(&bridge);
}


Expand Down
218 changes: 104 additions & 114 deletions test/unit/broker/persist_read_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include <net_mosq.h>
#include <send_mosq.h>
#include <callbacks.h>
#ifndef WITH_SYS_TREE
# define WITH_SYS_TREE
#endif
#include <sys_tree.h>

extern char *last_sub;
extern int last_qos;
Expand All @@ -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; i<store->dest_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);
Expand Down Expand Up @@ -159,60 +96,13 @@ 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);
UNUSED(rc);
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){
Expand Down Expand Up @@ -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;
}

0 comments on commit 028adb2

Please sign in to comment.