Skip to content

Commit

Permalink
Start of tests for persistence reading.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Mar 14, 2019
1 parent b635673 commit 9411d94
Show file tree
Hide file tree
Showing 20 changed files with 606 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ test/lib/cpp/*.test

test/unit/coverage.info
test/unit/mosq_test
test/unit/out
test/unit/persist_read_test
test/unit/out/

www/cache/
__pycache__
4 changes: 1 addition & 3 deletions src/database.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ int db__open(struct mosquitto__config *config, struct mosquitto_db *db)
db->unpwd = NULL;

#ifdef WITH_PERSISTENCE
if(config->persistence && config->persistence_filepath){
if(persist__restore(db)) return 1;
}
if(persist__restore(db)) return 1;
#endif

return MOSQ_ERR_SUCCESS;
Expand Down
5 changes: 4 additions & 1 deletion src/persist_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,10 @@ int persist__restore(struct mosquitto_db *db)

assert(db);
assert(db->config);
assert(db->config->persistence_filepath);

if(!db->config->persistence || db->config->persistence_filepath == NULL){
return MOSQ_ERR_SUCCESS;
}

db->msg_store_load = NULL;

Expand Down
32 changes: 28 additions & 4 deletions test/unit/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
include ../../config.mk

.PHONY: all test clean coverage
.PHONY: all test test-broker test-lib clean coverage

CFLAGS=-I../.. -I../../lib -coverage -Wall -ggdb
CFLAGS=-I../.. -I../../lib -I../../src -coverage -Wall -ggdb
TEST_LDFLAGS=-lcunit -coverage

TEST_OBJS = test.o \
Expand All @@ -22,31 +22,55 @@ LIB_OBJS = memory_mosq.o \
util_topic.o \
utf8_mosq.o

PERSIST_READ_TEST_OBJS = \
persist_read_test.o \
persist_read_stubs.o

PERSIST_READ_OBJS = \
memory_mosq.o \
persist_read.o \
util_mosq.o

all : test

mosq_test : ${TEST_OBJS} ${LIB_OBJS}
$(CROSS_COMPILE)$(CC) -o $@ $^ ${TEST_LDFLAGS}

persist_read_test : ${PERSIST_READ_TEST_OBJS} ${PERSIST_READ_OBJS}
$(CROSS_COMPILE)$(CC) -o $@ $^ ${TEST_LDFLAGS}

memory_mosq.o : ../../lib/memory_mosq.c
$(CROSS_COMPILE)$(CC) $(CFLAGS) -c -o $@ $^

packet_datatypes.o : ../../lib/packet_datatypes.c
$(CROSS_COMPILE)$(CC) $(CFLAGS) -c -o $@ $^

persist_read.o : ../../src/persist_read.c
$(CROSS_COMPILE)$(CC) $(CFLAGS) -DWITH_BROKER -DWITH_PERSISTENCE -c -o $@ $^

property_mosq.o : ../../lib/property_mosq.c
$(CROSS_COMPILE)$(CC) $(CFLAGS) -c -o $@ $^

util_mosq.o : ../../lib/util_mosq.c
$(CROSS_COMPILE)$(CC) $(CFLAGS) -c -o $@ $^

util_topic.o : ../../lib/util_topic.c
$(CROSS_COMPILE)$(CC) $(CFLAGS) -c -o $@ $^

utf8_mosq.o : ../../lib/utf8_mosq.c
$(CROSS_COMPILE)$(CC) $(CFLAGS) -c -o $@ $^

test : mosq_test
test-lib : mosq_test
./mosq_test

test-broker : persist_read_test
./persist_read_test

test : test-broker test-lib

clean :
-rm -rf mosq_test *.o *.gcda *.gcno coverage.info out/
-rm -rf mosq_test persist_read_test
-rm -rf *.o *.gcda *.gcno coverage.info out/

coverage :
lcov --capture --directory . --output-file coverage.info
Expand Down
1 change: 1 addition & 0 deletions test/unit/files/persist_read/corrupt-header-long.test-db
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
corruptcorruptcorruptcorruptcorruptcorrupt
1 change: 1 addition & 0 deletions test/unit/files/persist_read/corrupt-header-short.test-db
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
corrupt
Empty file.
Binary file not shown.
Binary file added test/unit/files/persist_read/v3-bad-chunk.test-db
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added test/unit/files/persist_read/v3-cfg.test-db
Binary file not shown.
Binary file not shown.
Binary file added test/unit/files/persist_read/v3-client.test-db
Binary file not shown.
Binary file not shown.
Binary file added test/unit/files/persist_read/v3-retain.test-db
Binary file not shown.
Binary file added test/unit/files/persist_read/v3-sub.test-db
Binary file not shown.
Binary file not shown.
133 changes: 133 additions & 0 deletions test/unit/persist_read_stubs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include <time.h>

#define WITH_BROKER

#include <logging_mosq.h>
#include <memory_mosq.h>
#include <mosquitto_broker_internal.h>
#include <net_mosq.h>
#include <send_mosq.h>
#include <time_mosq.h>

extern uint64_t last_retained;
extern char *last_sub;
extern int last_qos;

struct mosquitto *context__init(struct mosquitto_db *db, mosq_sock_t sock)
{
return mosquitto__calloc(1, sizeof(struct mosquitto));
}

int db__message_store(struct mosquitto_db *db, const struct mosquitto *source, uint16_t source_mid, char *topic, int qos, uint32_t payloadlen, mosquitto__payload_uhpa *payload, int retain, struct mosquitto_msg_store **stored, uint32_t message_expiry_interval, mosquitto_property *properties, dbid_t store_id)
{
struct mosquitto_msg_store *temp = NULL;
int rc = MOSQ_ERR_SUCCESS;

temp = mosquitto__calloc(1, sizeof(struct mosquitto_msg_store));
if(!temp){
rc = MOSQ_ERR_NOMEM;
goto error;
}

if(source && source->id){
temp->source_id = mosquitto__strdup(source->id);
}else{
temp->source_id = mosquitto__strdup("");
}
if(!temp->source_id){
rc = MOSQ_ERR_NOMEM;
goto error;
}

if(source && source->username){
temp->source_username = mosquitto__strdup(source->username);
if(!temp->source_username){
rc = MOSQ_ERR_NOMEM;
goto error;
}
}
if(source){
temp->source_listener = source->listener;
}
temp->source_mid = source_mid;
temp->mid = 0;
temp->qos = qos;
temp->retain = retain;
temp->topic = topic;
topic = NULL;
temp->payloadlen = payloadlen;
temp->properties = properties;
if(payloadlen){
UHPA_MOVE(temp->payload, *payload, payloadlen);
}else{
temp->payload.ptr = NULL;
}
if(message_expiry_interval > 0){
temp->message_expiry_time = time(NULL) + message_expiry_interval;
}else{
temp->message_expiry_time = 0;
}

temp->dest_ids = NULL;
temp->dest_id_count = 0;
db->msg_store_count++;
db->msg_store_bytes += payloadlen;
(*stored) = temp;

if(!store_id){
temp->db_id = ++db->last_db_id;
}else{
temp->db_id = store_id;
}

db->msg_store = temp;

return MOSQ_ERR_SUCCESS;
error:
mosquitto__free(topic);
if(temp){
mosquitto__free(temp->source_id);
mosquitto__free(temp->source_username);
mosquitto__free(temp->topic);
mosquitto__free(temp);
}
UHPA_FREE(*payload, payloadlen);
return rc;
}

int log__printf(struct mosquitto *mosq, int priority, const char *fmt, ...)
{
return 0;
}

time_t mosquitto_time(void)
{
return 123;
}

int net__socket_close(struct mosquitto_db *db, struct mosquitto *mosq)
{
return MOSQ_ERR_SUCCESS;
}

int send__pingreq(struct mosquitto *mosq)
{
return MOSQ_ERR_SUCCESS;
}

int sub__add(struct mosquitto_db *db, struct mosquitto *context, const char *sub, int qos, uint32_t identifier, int options, struct mosquitto__subhier **root)
{
last_sub = strdup(sub);
last_qos = qos;

return MOSQ_ERR_SUCCESS;
}

int sub__messages_queue(struct mosquitto_db *db, const char *source_id, const char *topic, int qos, int retain, struct mosquitto_msg_store **stored)
{
if(retain){
last_retained = (*stored)->db_id;
}
return MOSQ_ERR_SUCCESS;
}

Loading

0 comments on commit 9411d94

Please sign in to comment.