Skip to content

Commit 028adb2

Browse files
committed
Test leak fixes
1 parent 4da5081 commit 028adb2

File tree

8 files changed

+197
-117
lines changed

8 files changed

+197
-117
lines changed

src/bridge_topic.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,24 @@ static struct mosquitto__bridge_topic *bridge__find_topic(struct mosquitto__brid
132132
}
133133

134134

135+
void bridge__cleanup_topics(struct mosquitto__bridge *bridge)
136+
{
137+
struct mosquitto__bridge_topic *topic, *topic_tmp;
138+
139+
if(!bridge) return;
140+
141+
LL_FOREACH_SAFE(bridge->topics, topic, topic_tmp){
142+
LL_DELETE(bridge->topics, topic);
143+
mosquitto_free(topic->local_prefix);
144+
mosquitto_free(topic->remote_prefix);
145+
mosquitto_free(topic->local_topic);
146+
mosquitto_free(topic->remote_topic);
147+
mosquitto_free(topic->topic);
148+
mosquitto_free(topic);
149+
}
150+
}
151+
152+
135153
/* topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix] */
136154
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)
137155
{

src/mosquitto_broker_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,7 @@ int bridge__on_connect(struct mosquitto *context);
797797
void bridge_check(void);
798798
int bridge__register_local_connections(void);
799799
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);
800+
void bridge__cleanup_topics(struct mosquitto__bridge *bridge);
800801
int bridge__remap_topic_in(struct mosquitto *context, char **topic);
801802
#endif
802803

test/lib/c/fuzzish.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ static void on_message_v5(struct mosquitto *mosq, void *obj, const struct mosqui
257257
static void on_subscribe(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos)
258258
{
259259
UNUSED(mosq);
260-
UNUSED(obj);
261260
UNUSED(mid);
262261

263262
int tot = 0;
@@ -269,12 +268,13 @@ static void on_subscribe(struct mosquitto *mosq, void *obj, int mid, int qos_cou
269268
for(int i=0; i<qos_count; i++){
270269
tot += granted_qos[i];
271270
}
271+
272+
(void)tot;
272273
}
273274

274275
static void on_subscribe_v5(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos, const mosquitto_property *props)
275276
{
276277
UNUSED(mosq);
277-
UNUSED(obj);
278278
UNUSED(mid);
279279

280280
int tot = 0;
@@ -287,6 +287,8 @@ static void on_subscribe_v5(struct mosquitto *mosq, void *obj, int mid, int qos_
287287
tot += granted_qos[i];
288288
}
289289
prop_test(props);
290+
291+
(void)tot;
290292
}
291293

292294
static void on_unsubscribe(struct mosquitto *mosq, void *obj, int mid)

test/unit/broker/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ include ${R}/make/broker.mk
44

55
.PHONY: all check test test-compile clean coverage
66

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

3535
PERSIST_READ_OBJS = \
36+
${R}/src/database.o \
3637
${R}/src/packet_datatypes.o \
3738
${R}/src/persist_read.o \
3839
${R}/src/persist_read_v234.o \

test/unit/broker/bridge_topic_test.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ static void map_valid_helper(const char *topic, const char *local_prefix, const
3131
CU_ASSERT_STRING_EQUAL(map_topic, expected);
3232
free(map_topic);
3333
}
34+
35+
bridge__cleanup_topics(&bridge);
3436
}
3537

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

4749
rc = bridge__add_topic(&bridge, topic, bd_in, 0, local_prefix, remote_prefix);
4850
CU_ASSERT_NOT_EQUAL(rc, 0);
51+
52+
bridge__cleanup_topics(&bridge);
4953
}
5054

5155

test/unit/broker/persist_read_stubs.c

Lines changed: 104 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include <net_mosq.h>
66
#include <send_mosq.h>
77
#include <callbacks.h>
8+
#ifndef WITH_SYS_TREE
9+
# define WITH_SYS_TREE
10+
#endif
11+
#include <sys_tree.h>
812

913
extern char *last_sub;
1014
extern int last_qos;
@@ -24,73 +28,6 @@ struct mosquitto *context__init(void)
2428
return m;
2529
}
2630

27-
void db__msg_store_free(struct mosquitto__base_msg *store)
28-
{
29-
int i;
30-
31-
mosquitto_free(store->data.source_id);
32-
mosquitto_free(store->data.source_username);
33-
if(store->dest_ids){
34-
for(i=0; i<store->dest_id_count; i++){
35-
mosquitto_free(store->dest_ids[i]);
36-
}
37-
mosquitto_free(store->dest_ids);
38-
}
39-
mosquitto_free(store->data.topic);
40-
mosquitto_property_free_all(&store->data.properties);
41-
mosquitto_free(store->data.payload);
42-
mosquitto_free(store);
43-
}
44-
45-
int db__message_store(const struct mosquitto *source, struct mosquitto__base_msg *stored, uint32_t *message_expiry_interval, enum mosquitto_msg_origin origin)
46-
{
47-
int rc = MOSQ_ERR_SUCCESS;
48-
49-
UNUSED(origin);
50-
51-
if(source && source->id){
52-
stored->data.source_id = mosquitto_strdup(source->id);
53-
}else{
54-
stored->data.source_id = mosquitto_strdup("");
55-
}
56-
if(!stored->data.source_id){
57-
rc = MOSQ_ERR_NOMEM;
58-
goto error;
59-
}
60-
61-
if(source && source->username){
62-
stored->data.source_username = mosquitto_strdup(source->username);
63-
if(!stored->data.source_username){
64-
rc = MOSQ_ERR_NOMEM;
65-
goto error;
66-
}
67-
}
68-
if(source){
69-
stored->source_listener = source->listener;
70-
}
71-
if(message_expiry_interval){
72-
stored->data.expiry_time = time(NULL) + (*message_expiry_interval);
73-
}else{
74-
stored->data.expiry_time = 0;
75-
}
76-
77-
stored->dest_ids = NULL;
78-
stored->dest_id_count = 0;
79-
db.msg_store_count++;
80-
db.msg_store_bytes += stored->data.payloadlen;
81-
82-
if(!stored->data.store_id){
83-
stored->data.store_id = ++db.last_db_id;
84-
}
85-
86-
HASH_ADD(hh, db.msg_store, data.store_id, sizeof(stored->data.store_id), stored);
87-
88-
return MOSQ_ERR_SUCCESS;
89-
error:
90-
db__msg_store_free(stored);
91-
return rc;
92-
}
93-
9431
int log__printf(struct mosquitto *mosq, unsigned int priority, const char *fmt, ...)
9532
{
9633
UNUSED(mosq);
@@ -159,60 +96,13 @@ int sub__add(struct mosquitto *context, const struct mosquitto_subscription *sub
15996
return MOSQ_ERR_SUCCESS;
16097
}
16198

162-
int db__message_insert_incoming(struct mosquitto *context, uint64_t cmsg_id, struct mosquitto__base_msg *msg, bool persist)
163-
{
164-
UNUSED(context);
165-
UNUSED(cmsg_id);
166-
UNUSED(msg);
167-
UNUSED(persist);
168-
169-
return MOSQ_ERR_SUCCESS;
170-
}
171-
172-
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)
173-
{
174-
UNUSED(context);
175-
UNUSED(cmsg_id);
176-
UNUSED(mid);
177-
UNUSED(qos);
178-
UNUSED(retain);
179-
UNUSED(stored);
180-
UNUSED(subscription_identifier);
181-
UNUSED(update);
182-
UNUSED(persist);
183-
184-
return MOSQ_ERR_SUCCESS;
185-
}
186-
187-
void db__msg_store_ref_dec(struct mosquitto__base_msg **store)
188-
{
189-
UNUSED(store);
190-
}
191-
192-
void db__msg_store_ref_inc(struct mosquitto__base_msg *store)
193-
{
194-
store->ref_count++;
195-
}
196-
19799
void callback__on_disconnect(struct mosquitto *mosq, int rc, const mosquitto_property *props)
198100
{
199101
UNUSED(mosq);
200102
UNUSED(rc);
201103
UNUSED(props);
202104
}
203105

204-
void db__msg_add_to_inflight_stats(struct mosquitto_msg_data *msg_data, struct mosquitto__client_msg *msg)
205-
{
206-
UNUSED(msg_data);
207-
UNUSED(msg);
208-
}
209-
210-
void db__msg_add_to_queued_stats(struct mosquitto_msg_data *msg_data, struct mosquitto__client_msg *msg)
211-
{
212-
UNUSED(msg_data);
213-
UNUSED(msg);
214-
}
215-
216106
void context__add_to_by_id(struct mosquitto *context)
217107
{
218108
if(context->in_by_id == false){
@@ -261,3 +151,103 @@ void mosquitto_log_printf(int level, const char *fmt, ...)
261151
UNUSED(level);
262152
UNUSED(fmt);
263153
}
154+
struct mosquitto__subhier *sub__add_hier_entry(struct mosquitto__subhier *parent, struct mosquitto__subhier **sibling, const char *topic, uint16_t len)
155+
{
156+
UNUSED(parent);
157+
UNUSED(sibling);
158+
UNUSED(topic);
159+
UNUSED(len);
160+
161+
return NULL;
162+
}
163+
void plugin_persist__handle_client_msg_add(struct mosquitto *context, const struct mosquitto__client_msg *cmsg)
164+
{
165+
UNUSED(context);
166+
UNUSED(cmsg);
167+
}
168+
void plugin_persist__handle_client_msg_delete(struct mosquitto *context, const struct mosquitto__client_msg *cmsg)
169+
{
170+
UNUSED(context);
171+
UNUSED(cmsg);
172+
}
173+
void plugin_persist__handle_client_msg_update(struct mosquitto *context, const struct mosquitto__client_msg *cmsg)
174+
{
175+
UNUSED(context);
176+
UNUSED(cmsg);
177+
}
178+
void plugin_persist__handle_client_msg_clear(struct mosquitto *context, uint8_t direction)
179+
{
180+
UNUSED(context);
181+
UNUSED(direction);
182+
}
183+
void plugin_persist__handle_base_msg_delete(struct mosquitto__base_msg *msg)
184+
{
185+
UNUSED(msg);
186+
}
187+
void plugin_persist__handle_subscription_delete(struct mosquitto *context, char *sub)
188+
{
189+
UNUSED(context);
190+
UNUSED(sub);
191+
}
192+
int sub__messages_queue(const char *source_id, const char *topic, uint8_t qos, int retain, struct mosquitto__base_msg **base_msg)
193+
{
194+
UNUSED(source_id);
195+
UNUSED(topic);
196+
UNUSED(qos);
197+
UNUSED(retain);
198+
*base_msg = NULL;
199+
return 0;
200+
}
201+
void metrics__int_inc(enum mosq_metric_type m, int64_t value)
202+
{
203+
UNUSED(m); UNUSED(value);
204+
}
205+
void metrics__int_dec(enum mosq_metric_type m, int64_t value)
206+
{
207+
UNUSED(m); UNUSED(value);
208+
}
209+
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)
210+
{
211+
UNUSED(mosq);
212+
UNUSED(mid);
213+
UNUSED(topic);
214+
UNUSED(payloadlen);
215+
UNUSED(payload);
216+
UNUSED(qos);
217+
UNUSED(retain);
218+
UNUSED(dup);
219+
UNUSED(subscription_identifier);
220+
UNUSED(store_props);
221+
UNUSED(expiry_interval);
222+
223+
return MOSQ_ERR_SUCCESS;
224+
}
225+
226+
int send__pubcomp(struct mosquitto *mosq, uint16_t mid, const mosquitto_property *properties)
227+
{
228+
UNUSED(mosq);
229+
UNUSED(mid);
230+
UNUSED(properties);
231+
232+
return MOSQ_ERR_SUCCESS;
233+
}
234+
235+
int send__pubrec(struct mosquitto *mosq, uint16_t mid, uint8_t reason_code, const mosquitto_property *properties)
236+
{
237+
UNUSED(mosq);
238+
UNUSED(mid);
239+
UNUSED(reason_code);
240+
UNUSED(properties);
241+
242+
return MOSQ_ERR_SUCCESS;
243+
}
244+
245+
int send__pubrel(struct mosquitto *mosq, uint16_t mid, const mosquitto_property *properties)
246+
{
247+
UNUSED(mosq);
248+
UNUSED(mid);
249+
UNUSED(properties);
250+
251+
return MOSQ_ERR_SUCCESS;
252+
}
253+

0 commit comments

Comments
 (0)