Start of persistence writing tests.

This commit is contained in:
Roger A. Light 2019-03-19 14:25:40 +00:00
parent 54a35ed0ed
commit 7a53b28080
12 changed files with 371 additions and 1014 deletions

1
.gitignore vendored
View File

@ -63,6 +63,7 @@ test/lib/cpp/*.test
test/unit/coverage.info
test/unit/mosq_test
test/unit/persist_read_test
test/unit/persist_write_test
test/unit/out/
www/cache/

File diff suppressed because it is too large Load Diff

View File

@ -375,6 +375,8 @@ int persist__backup(struct mosquitto_db *db, bool shutdown)
int len;
if(!db || !db->config || !db->config->persistence_filepath) return MOSQ_ERR_INVAL;
if(db->config->persistence == false) return MOSQ_ERR_SUCCESS;
log__printf(NULL, MOSQ_LOG_INFO, "Saving in-memory database to %s.", db->config->persistence_filepath);
len = strlen(db->config->persistence_filepath)+5;
@ -408,6 +410,7 @@ int persist__backup(struct mosquitto_db *db, bool shutdown)
*/
rc = unlink(outfile);
if (rc != 0) {
rc = 0;
if (errno != ENOENT) {
log__printf(NULL, MOSQ_LOG_INFO, "Error saving in-memory database, unable to remove %s.", outfile);
goto error;

View File

@ -32,14 +32,35 @@ PERSIST_READ_OBJS = \
persist_read_v234.o \
util_mosq.o
PERSIST_WRITE_TEST_OBJS = \
persist_write_test.o \
persist_write_stubs.o
PERSIST_WRITE_OBJS = \
database.o \
memory_mosq.o \
persist_read.o \
persist_read_v234.o \
persist_write.o \
subs.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}
persist_write_test : ${PERSIST_WRITE_TEST_OBJS} ${PERSIST_WRITE_OBJS}
$(CROSS_COMPILE)$(CC) -o $@ $^ ${TEST_LDFLAGS}
database.o : ../../src/database.c
$(CROSS_COMPILE)$(CC) $(CFLAGS) -DWITH_BROKER -DWITH_PERSISTENCE -c -o $@ $^
memory_mosq.o : ../../lib/memory_mosq.c
$(CROSS_COMPILE)$(CC) $(CFLAGS) -c -o $@ $^
@ -52,9 +73,15 @@ persist_read.o : ../../src/persist_read.c
persist_read_v234.o : ../../src/persist_read_v234.c
$(CROSS_COMPILE)$(CC) $(CFLAGS) -DWITH_BROKER -DWITH_PERSISTENCE -c -o $@ $^
persist_write.o : ../../src/persist_write.c
$(CROSS_COMPILE)$(CC) $(CFLAGS) -DWITH_BROKER -DWITH_PERSISTENCE -c -o $@ $^
property_mosq.o : ../../lib/property_mosq.c
$(CROSS_COMPILE)$(CC) $(CFLAGS) -c -o $@ $^
subs.o : ../../src/subs.c
$(CROSS_COMPILE)$(CC) $(CFLAGS) -DWITH_BROKER -DWITH_PERSISTENCE -c -o $@ $^
util_mosq.o : ../../lib/util_mosq.c
$(CROSS_COMPILE)$(CC) $(CFLAGS) -c -o $@ $^
@ -67,13 +94,14 @@ utf8_mosq.o : ../../lib/utf8_mosq.c
test-lib : mosq_test
./mosq_test
test-broker : persist_read_test
test-broker : persist_read_test persist_write_test
./persist_read_test
./persist_write_test
test : test-broker test-lib
clean :
-rm -rf mosq_test persist_read_test
-rm -rf mosq_test persist_read_test persist_write_test
-rm -rf *.o *.gcda *.gcno coverage.info out/
coverage :

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -107,6 +107,25 @@ static void TEST_v3_config_ok(void)
}
static void TEST_v4_config_ok(void)
{
struct mosquitto_db db;
struct mosquitto__config config;
int rc;
memset(&db, 0, sizeof(struct mosquitto_db));
memset(&config, 0, sizeof(struct mosquitto__config));
db.config = &config;
config.persistence = true;
config.persistence_filepath = "files/persist_read/v4-cfg.test-db";
rc = persist__restore(&db);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
CU_ASSERT_EQUAL(db.last_db_id, 0x7856341200000000);
}
static void TEST_v3_config_truncated(void)
{
struct mosquitto_db db;
@ -402,6 +421,7 @@ int init_persist_read_tests(void)
|| !CU_add_test(test_suite, "v3 client message", TEST_v3_client_message)
|| !CU_add_test(test_suite, "v3 retain", TEST_v3_retain)
|| !CU_add_test(test_suite, "v3 sub", TEST_v3_sub)
|| !CU_add_test(test_suite, "v4 config ok", TEST_v4_config_ok)
|| !CU_add_test(test_suite, "v4 message store", TEST_v4_message_store)
){
@ -414,6 +434,7 @@ int init_persist_read_tests(void)
int main(int argc, char *argv[])
{
int fails;
if(CU_initialize_registry() != CUE_SUCCESS){
printf("Error initializing CUnit registry.\n");
@ -430,7 +451,8 @@ int main(int argc, char *argv[])
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
fails = CU_get_number_of_failures();
CU_cleanup_registry();
return 0;
return (int)fails;
}

View File

@ -0,0 +1,80 @@
#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 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 mosquitto_property_add_varint(mosquitto_property **proplist, int identifier, uint32_t value)
{
return MOSQ_ERR_SUCCESS;
}
void mosquitto_property_free_all(mosquitto_property **properties)
{
}
int mosquitto_acl_check(struct mosquitto_db *db, struct mosquitto *context, const char *topic, long payloadlen, void* payload, int qos, bool retain, int access)
{
return MOSQ_ERR_SUCCESS;
}
int acl__find_acls(struct mosquitto_db *db, struct mosquitto *context)
{
return MOSQ_ERR_SUCCESS;
}
int send__publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint32_t payloadlen, const void *payload, int qos, bool retain, bool dup, const mosquitto_property *cmsg_props, const mosquitto_property *store_props, uint32_t expiry_interval)
{
return MOSQ_ERR_SUCCESS;
}
int send__pubcomp(struct mosquitto *mosq, uint16_t mid)
{
return MOSQ_ERR_SUCCESS;
}
int send__pubrec(struct mosquitto *mosq, uint16_t mid, uint8_t reason_code)
{
return MOSQ_ERR_SUCCESS;
}
int send__pubrel(struct mosquitto *mosq, uint16_t mid)
{
return MOSQ_ERR_SUCCESS;
}

View File

@ -0,0 +1,231 @@
/* Tests for persistence.
*
* FIXME - these need to be aggressive about finding failures, at the moment
* they are just confirming that good behaviour works. */
#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>
#define WITH_BROKER
#define WITH_PERSISTENCE
#include "mosquitto_broker_internal.h"
#include "persist.h"
uint64_t last_retained;
char *last_sub = NULL;
int last_qos;
/* read entire file into memory */
static int file_read(const char *filename, uint8_t **data, size_t *len)
{
FILE *fptr;
size_t rc;
fptr = fopen(filename, "rb");
if(!fptr) return 1;
fseek(fptr, 0, SEEK_END);
*len = ftell(fptr);
*data = malloc(*len);
if(!(*data)){
fclose(fptr);
return 1;
}
fseek(fptr, 0, SEEK_SET);
rc = fread(*data, 1, *len, fptr);
fclose(fptr);
if(rc == *len){
return 0;
}else{
*len = 0;
free(*data);
return 1;
}
}
/* Crude file diff, only for small files */
static int file_diff(const char *one, const char *two)
{
size_t len1, len2;
uint8_t *data1 = NULL, *data2 = NULL;
int rc = 1;
if(file_read(one, &data1, &len1)){
return 1;
}
if(file_read(two, &data2, &len2)){
free(data1);
return 1;
}
if(len1 == len2){
rc = memcmp(data1, data2, len1);
}
free(data1);
free(data2);
return rc;
}
static void TEST_persistence_disabled(void)
{
struct mosquitto_db db;
struct mosquitto__config config;
int rc;
memset(&db, 0, sizeof(struct mosquitto_db));
memset(&config, 0, sizeof(struct mosquitto__config));
db.config = &config;
rc = persist__backup(&db, false);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_INVAL);
config.persistence_filepath = "disabled.db";
rc = persist__backup(&db, false);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
}
static void TEST_empty_file(void)
{
struct mosquitto_db db;
struct mosquitto__config config;
int rc;
memset(&db, 0, sizeof(struct mosquitto_db));
memset(&config, 0, sizeof(struct mosquitto__config));
db.config = &config;
config.persistence = true;
config.persistence_filepath = "empty.db";
rc = persist__backup(&db, false);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
CU_ASSERT_EQUAL(0, file_diff("files/persist_write/empty.test-db", "empty.db"));
unlink("empty.db");
}
static void TEST_v4_config_ok(void)
{
struct mosquitto_db db;
struct mosquitto__config config;
int rc;
memset(&db, 0, sizeof(struct mosquitto_db));
memset(&config, 0, sizeof(struct mosquitto__config));
db.config = &config;
config.persistence = true;
config.persistence_filepath = "files/persist_read/v4-cfg.test-db";
rc = persist__restore(&db);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
config.persistence_filepath = "v4-cfg.db";
rc = persist__backup(&db, true);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
CU_ASSERT_EQUAL(0, file_diff("files/persist_read/v4-cfg.test-db", "v4-cfg.db"));
unlink("v4-cfg.db");
}
static void TEST_v4_message_store_no_ref(void)
{
struct mosquitto_db db;
struct mosquitto__config config;
int rc;
memset(&db, 0, sizeof(struct mosquitto_db));
memset(&config, 0, sizeof(struct mosquitto__config));
db.config = &config;
config.persistence = true;
config.persistence_filepath = "files/persist_read/v4-message-store.test-db";
rc = persist__restore(&db);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
config.persistence_filepath = "v4-message-store-no-ref.db";
rc = persist__backup(&db, true);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
CU_ASSERT_EQUAL(0, file_diff("files/persist_write/v4-message-store-no-ref.test-db", "v4-message-store-no-ref.db"));
unlink("v4-message-store-no-ref.db");
}
#if 0
NOT WORKING
static void TEST_v4_full(void)
{
struct mosquitto_db db;
struct mosquitto__config config;
int rc;
memset(&db, 0, sizeof(struct mosquitto_db));
memset(&config, 0, sizeof(struct mosquitto__config));
db.config = &config;
db__open(&config, &db);
config.persistence = true;
config.persistence_filepath = "files/persist_write/v4-full.test-db";
rc = persist__restore(&db);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
config.persistence_filepath = "v4-full.db";
rc = persist__backup(&db, true);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
CU_ASSERT_EQUAL(0, file_diff("files/persist_write/v4-full.test-db", "v4-full.db"));
unlink("v4-full.db");
}
#endif
/* ========================================================================
* TEST SUITE SETUP
* ======================================================================== */
int main(int argc, char *argv[])
{
CU_pSuite test_suite = NULL;
unsigned int fails;
if(CU_initialize_registry() != CUE_SUCCESS){
printf("Error initializing CUnit registry.\n");
return 1;
}
test_suite = CU_add_suite("Persist write", NULL, NULL);
if(!test_suite){
printf("Error adding CUnit persist write test suite.\n");
CU_cleanup_registry();
return 1;
}
if(0
|| !CU_add_test(test_suite, "Persistence disabled", TEST_persistence_disabled)
|| !CU_add_test(test_suite, "Empty file", TEST_empty_file)
|| !CU_add_test(test_suite, "v4 config ok", TEST_v4_config_ok)
|| !CU_add_test(test_suite, "v4 message store (message has no refs)", TEST_v4_message_store_no_ref)
//|| !CU_add_test(test_suite, "v4 full", TEST_v4_full)
){
printf("Error adding persist CUnit tests.\n");
CU_cleanup_registry();
return 1;
}
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
fails = CU_get_number_of_failures();
CU_cleanup_registry();
return (int)fails;
}

View File

@ -15,6 +15,7 @@ int init_util_topic_tests(void);
int main(int argc, char *argv[])
{
unsigned int fails;
if(CU_initialize_registry() != CUE_SUCCESS){
printf("Error initializing CUnit registry.\n");
@ -38,8 +39,9 @@ int main(int argc, char *argv[])
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
fails = CU_get_number_of_failures();
CU_cleanup_registry();
return 0;
return (int)fails;
}