test_mqtt.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. *
  6. * This test code is in the Public Domain (or CC0 licensed, at your option.)
  7. *
  8. * Unless required by applicable law or agreed to in writing, this
  9. * software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  10. * CONDITIONS OF ANY KIND, either express or implied.
  11. */
  12. #include <sys/time.h>
  13. #include "freertos/FreeRTOS.h"
  14. #include "freertos/event_groups.h"
  15. #include "unity.h"
  16. #include "test_utils.h"
  17. #include "memory_checks.h"
  18. #include "mqtt_client.h"
  19. #include "nvs_flash.h"
  20. #include "esp_ota_ops.h"
  21. #include "sdkconfig.h"
  22. #include "test_mqtt_client_broker.h"
  23. #include "test_mqtt_connection.h"
  24. #include "esp_mac.h"
  25. static void test_leak_setup(const char * file, long line)
  26. {
  27. uint8_t mac[6];
  28. struct timeval te;
  29. gettimeofday(&te, NULL); // get current time
  30. esp_read_mac(mac, ESP_MAC_WIFI_STA);
  31. printf("%s:%ld: time=%jd.%lds, mac:" MACSTR "\n", file, line, (intmax_t)te.tv_sec, te.tv_usec, MAC2STR(mac));
  32. test_utils_record_free_mem();
  33. }
  34. TEST_CASE("mqtt init with invalid url", "[mqtt][leaks=0]")
  35. {
  36. test_leak_setup(__FILE__, __LINE__);
  37. const esp_mqtt_client_config_t mqtt_cfg = {
  38. .broker.address.uri = "INVALID",
  39. };
  40. esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
  41. TEST_ASSERT_EQUAL(NULL, client );
  42. }
  43. TEST_CASE("mqtt init and deinit", "[mqtt][leaks=0]")
  44. {
  45. test_leak_setup(__FILE__, __LINE__);
  46. const esp_mqtt_client_config_t mqtt_cfg = {
  47. // no connection takes place, but the uri has to be valid for init() to succeed
  48. .broker.address.uri = "mqtts://localhost:8883",
  49. };
  50. esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
  51. TEST_ASSERT_NOT_EQUAL(NULL, client );
  52. esp_mqtt_client_destroy(client);
  53. }
  54. static const char* this_bin_addr(void)
  55. {
  56. spi_flash_mmap_handle_t out_handle;
  57. const void *binary_address;
  58. const esp_partition_t* partition = esp_ota_get_running_partition();
  59. esp_partition_mmap(partition, 0, partition->size, SPI_FLASH_MMAP_DATA, &binary_address, &out_handle);
  60. return binary_address;
  61. }
  62. TEST_CASE("mqtt enqueue and destroy outbox", "[mqtt][leaks=0]")
  63. {
  64. const char * bin_addr = this_bin_addr();
  65. test_leak_setup(__FILE__, __LINE__);
  66. const int messages = 20;
  67. const int size = 2000;
  68. const esp_mqtt_client_config_t mqtt_cfg = {
  69. // no connection takes place, but the uri has to be valid for init() to succeed
  70. .broker.address.uri = "mqtts://localhost:8883",
  71. };
  72. esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
  73. TEST_ASSERT_NOT_EQUAL(NULL, client );
  74. int bytes_before = esp_get_free_heap_size();
  75. for (int i=0; i<messages; ++i) {
  76. esp_mqtt_client_publish(client, "test", bin_addr, size, 1, 0);
  77. }
  78. int bytes_after = esp_get_free_heap_size();
  79. // check that outbox allocated all messages on heap
  80. TEST_ASSERT_GREATER_OR_EQUAL(messages*size, bytes_before - bytes_after);
  81. esp_mqtt_client_destroy(client);
  82. }
  83. #if SOC_EMAC_SUPPORTED
  84. /**
  85. * This test cases uses ethernet kit, so build and use it only if EMAC supported
  86. */
  87. TEST_CASE("mqtt broker tests", "[mqtt][test_env=UT_T2_Ethernet]")
  88. {
  89. test_case_uses_tcpip();
  90. connect_test_fixture_setup();
  91. RUN_MQTT_BROKER_TEST(mqtt_connect_disconnect);
  92. RUN_MQTT_BROKER_TEST(mqtt_subscribe_publish);
  93. RUN_MQTT_BROKER_TEST(mqtt_lwt_clean_disconnect);
  94. RUN_MQTT_BROKER_TEST(mqtt_subscribe_payload);
  95. connect_test_fixture_teardown();
  96. }
  97. #endif // SOC_EMAC_SUPPORTED