test_mqtt.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "test_mqtt.h"
  2. #include "lwip/pbuf.h"
  3. #include "lwip/apps/mqtt.h"
  4. #include "lwip/apps/mqtt_priv.h"
  5. #include "lwip/netif.h"
  6. const ip_addr_t test_mqtt_local_ip = IPADDR4_INIT_BYTES(192, 168, 1, 1);
  7. const ip_addr_t test_mqtt_remote_ip = IPADDR4_INIT_BYTES(192, 168, 1, 2);
  8. const ip_addr_t test_mqtt_netmask = IPADDR4_INIT_BYTES(255, 255, 255, 0);
  9. static err_t test_mqtt_netif_output(struct netif *netif, struct pbuf *p,
  10. const ip4_addr_t *ipaddr)
  11. {
  12. LWIP_UNUSED_ARG(netif);
  13. LWIP_UNUSED_ARG(ipaddr);
  14. LWIP_UNUSED_ARG(p);
  15. return ERR_OK;
  16. }
  17. static void
  18. test_mqtt_init_netif(struct netif *netif, const ip_addr_t *ip_addr, const ip_addr_t *netmask)
  19. {
  20. struct netif *n;
  21. memset(netif, 0, sizeof(struct netif));
  22. netif->output = test_mqtt_netif_output;
  23. netif->flags |= NETIF_FLAG_UP | NETIF_FLAG_LINK_UP;
  24. ip_addr_copy_from_ip4(netif->netmask, *ip_2_ip4(netmask));
  25. ip_addr_copy_from_ip4(netif->ip_addr, *ip_2_ip4(ip_addr));
  26. for (n = netif_list; n != NULL; n = n->next) {
  27. if (n == netif) {
  28. return;
  29. }
  30. }
  31. netif->next = NULL;
  32. netif_list = netif;
  33. }
  34. /* Setups/teardown functions */
  35. static struct netif *old_netif_list;
  36. static struct netif *old_netif_default;
  37. static void
  38. mqtt_setup(void)
  39. {
  40. old_netif_list = netif_list;
  41. old_netif_default = netif_default;
  42. netif_list = NULL;
  43. netif_default = NULL;
  44. lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
  45. }
  46. static void
  47. mqtt_teardown(void)
  48. {
  49. netif_list = NULL;
  50. netif_default = NULL;
  51. /* restore netif_list for next tests (e.g. loopif) */
  52. netif_list = old_netif_list;
  53. netif_default = old_netif_default;
  54. lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
  55. }
  56. static void test_mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status)
  57. {
  58. LWIP_UNUSED_ARG(client);
  59. LWIP_UNUSED_ARG(arg);
  60. LWIP_UNUSED_ARG(status);
  61. }
  62. START_TEST(basic_connect)
  63. {
  64. mqtt_client_t* client;
  65. struct netif netif;
  66. err_t err;
  67. struct mqtt_connect_client_info_t client_info = {
  68. "dumm",
  69. NULL, NULL,
  70. 10,
  71. NULL, NULL, 0, 0
  72. };
  73. struct pbuf *p;
  74. unsigned char rxbuf[] = {0x20, 0x02, 0x00, 0x00};
  75. LWIP_UNUSED_ARG(_i);
  76. test_mqtt_init_netif(&netif, &test_mqtt_local_ip, &test_mqtt_netmask);
  77. client = mqtt_client_new();
  78. fail_unless(client != NULL);
  79. err = mqtt_client_connect(client, &test_mqtt_remote_ip, 1234, test_mqtt_connection_cb, NULL, &client_info);
  80. fail_unless(err == ERR_OK);
  81. client->conn->connected(client->conn->callback_arg, client->conn, ERR_OK);
  82. p = pbuf_alloc(PBUF_RAW, sizeof(rxbuf), PBUF_REF);
  83. fail_unless(p != NULL);
  84. p->payload = rxbuf;
  85. /* since we hack the rx path, we have to hack the rx window, too: */
  86. client->conn->rcv_wnd -= p->tot_len;
  87. if (client->conn->recv(client->conn->callback_arg, client->conn, p, ERR_OK) != ERR_OK) {
  88. pbuf_free(p);
  89. }
  90. mqtt_disconnect(client);
  91. /* fixme: mqtt_client_fre() is missing... */
  92. mem_free(client);
  93. }
  94. END_TEST
  95. Suite* mqtt_suite(void)
  96. {
  97. testfunc tests[] = {
  98. TESTFUNC(basic_connect),
  99. };
  100. return create_suite("MQTT", tests, sizeof(tests)/sizeof(testfunc), mqtt_setup, mqtt_teardown);
  101. }