From 5be3f7b5d41d5950aa5d598ca7c6afae79087a9d Mon Sep 17 00:00:00 2001 From: David Cermak Date: Sun, 19 Jan 2020 21:24:28 +0100 Subject: [PATCH] mqtt: add basic set of unit tests --- components/mqtt/esp-mqtt | 2 +- components/mqtt/test/CMakeLists.txt | 2 + components/mqtt/test/component.mk | 4 ++ components/mqtt/test/test_mqtt.c | 70 +++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 components/mqtt/test/CMakeLists.txt create mode 100644 components/mqtt/test/component.mk create mode 100644 components/mqtt/test/test_mqtt.c diff --git a/components/mqtt/esp-mqtt b/components/mqtt/esp-mqtt index 86fc8b7..9e20c7a 160000 --- a/components/mqtt/esp-mqtt +++ b/components/mqtt/esp-mqtt @@ -1 +1 @@ -Subproject commit 86fc8b7584f7f3aebf422843d84a26655e485fbe +Subproject commit 9e20c7ae3d6951cab3ed8dc66a0467da5bf37f16 diff --git a/components/mqtt/test/CMakeLists.txt b/components/mqtt/test/CMakeLists.txt new file mode 100644 index 0000000..867c584 --- /dev/null +++ b/components/mqtt/test/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRC_DIRS "." + PRIV_REQUIRES unity test_utils mqtt nvs_flash app_update) \ No newline at end of file diff --git a/components/mqtt/test/component.mk b/components/mqtt/test/component.mk new file mode 100644 index 0000000..5be8734 --- /dev/null +++ b/components/mqtt/test/component.mk @@ -0,0 +1,4 @@ +# +#Component Makefile +# +COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive \ No newline at end of file diff --git a/components/mqtt/test/test_mqtt.c b/components/mqtt/test/test_mqtt.c new file mode 100644 index 0000000..12cd177 --- /dev/null +++ b/components/mqtt/test/test_mqtt.c @@ -0,0 +1,70 @@ +#include "test_utils.h" +#include "mqtt_client.h" +#include "unity.h" +#include +#include "nvs_flash.h" +#include "esp_ota_ops.h" + +static void test_leak_setup(const char * file, long line) +{ + uint8_t mac[6]; + struct timeval te; + gettimeofday(&te, NULL); // get current time + esp_read_mac(mac, ESP_MAC_WIFI_STA); + printf("%s:%ld: time=%ld.%lds, mac:" MACSTR "\n", file, line, te.tv_sec, te.tv_usec, MAC2STR(mac)); + unity_reset_leak_checks(); +} + +static const char* this_bin_addr(void) +{ + spi_flash_mmap_handle_t out_handle; + const void *binary_address; + const esp_partition_t* partition = esp_ota_get_running_partition(); + esp_partition_mmap(partition, 0, partition->size, SPI_FLASH_MMAP_DATA, &binary_address, &out_handle); + return binary_address; +} + +TEST_CASE("mqtt init with invalid url", "[mqtt][leaks=0]") +{ + test_leak_setup(__FILE__, __LINE__); + const esp_mqtt_client_config_t mqtt_cfg = { + .uri = "INVALID", + }; + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + TEST_ASSERT_EQUAL(NULL, client ); +} + +TEST_CASE("mqtt init and deinit", "[mqtt][leaks=0]") +{ + test_leak_setup(__FILE__, __LINE__); + const esp_mqtt_client_config_t mqtt_cfg = { + // no connection takes place, but the uri has to be valid for init() to succeed + .uri = "mqtts://localhost:8883", + }; + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + TEST_ASSERT_NOT_EQUAL(NULL, client ); + esp_mqtt_client_destroy(client); +} + +TEST_CASE("mqtt enqueue and destroy outbox", "[mqtt][leaks=0]") +{ + const char * bin_addr = this_bin_addr(); + test_leak_setup(__FILE__, __LINE__); + const int messages = 20; + const int size = 2000; + const esp_mqtt_client_config_t mqtt_cfg = { + // no connection takes place, but the uri has to be valid for init() to succeed + .uri = "mqtts://localhost:8883", + }; + esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); + TEST_ASSERT_NOT_EQUAL(NULL, client ); + int bytes_before = esp_get_free_heap_size(); + for (int i=0; i