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