xref: /nrf52832-nimble/rt-thread/components/net/lwip-2.1.0/test/unit/mqtt/test_mqtt.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
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 
8 const ip_addr_t test_mqtt_local_ip = IPADDR4_INIT_BYTES(192, 168, 1, 1);
9 const ip_addr_t test_mqtt_remote_ip = IPADDR4_INIT_BYTES(192, 168, 1, 2);
10 const ip_addr_t test_mqtt_netmask = IPADDR4_INIT_BYTES(255, 255, 255, 0);
11 
12 static 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 
21 static void
22 test_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 */
40 static struct netif *old_netif_list;
41 static struct netif *old_netif_default;
42 
43 static void
44 mqtt_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 
53 static void
54 mqtt_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 
64 static 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 
71 START_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   if (client->conn->recv(client->conn->callback_arg, client->conn, p, ERR_OK) != ERR_OK) {
98     pbuf_free(p);
99   }
100 
101   mqtt_disconnect(client);
102   /* fixme: mqtt_client_fre() is missing... */
103   mem_free(client);
104 }
105 END_TEST
106 
107 Suite* mqtt_suite(void)
108 {
109   testfunc tests[] = {
110     TESTFUNC(basic_connect),
111   };
112   return create_suite("MQTT", tests, sizeof(tests)/sizeof(testfunc), mqtt_setup, mqtt_teardown);
113 }
114