Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers test_mqtt.c Source File

test_mqtt.c

00001 #include "test_mqtt.h"
00002 
00003 #include "lwip/pbuf.h"
00004 #include "lwip/apps/mqtt.h"
00005 #include "lwip/apps/mqtt_priv.h"
00006 #include "lwip/netif.h"
00007 
00008 const ip_addr_t test_mqtt_local_ip = IPADDR4_INIT_BYTES(192, 168, 1, 1);
00009 const ip_addr_t test_mqtt_remote_ip = IPADDR4_INIT_BYTES(192, 168, 1, 2);
00010 const ip_addr_t test_mqtt_netmask = IPADDR4_INIT_BYTES(255, 255, 255, 0);
00011 
00012 static err_t test_mqtt_netif_output(struct netif *netif, struct pbuf *p,
00013        const ip4_addr_t *ipaddr)
00014 {
00015   LWIP_UNUSED_ARG(netif);
00016   LWIP_UNUSED_ARG(ipaddr);
00017   LWIP_UNUSED_ARG(p);
00018   return ERR_OK;
00019 }
00020 
00021 static void
00022 test_mqtt_init_netif(struct netif *netif, const ip_addr_t *ip_addr, const ip_addr_t *netmask)
00023 {
00024   struct netif *n;
00025   memset(netif, 0, sizeof(struct netif));
00026   netif->output = test_mqtt_netif_output;
00027   netif->flags |= NETIF_FLAG_UP | NETIF_FLAG_LINK_UP;
00028   ip_addr_copy_from_ip4(netif->netmask, *ip_2_ip4(netmask));
00029   ip_addr_copy_from_ip4(netif->ip_addr, *ip_2_ip4(ip_addr));
00030   for (n = netif_list; n != NULL; n = n->next) {
00031     if (n == netif) {
00032       return;
00033     }
00034   }
00035   netif->next = NULL;
00036   netif_list = netif;
00037 }
00038 
00039 /* Setups/teardown functions */
00040 static struct netif *old_netif_list;
00041 static struct netif *old_netif_default;
00042 
00043 static void
00044 mqtt_setup(void)
00045 {
00046   old_netif_list = netif_list;
00047   old_netif_default = netif_default;
00048   netif_list = NULL;
00049   netif_default = NULL;
00050   lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
00051 }
00052 
00053 static void
00054 mqtt_teardown(void)
00055 {
00056   netif_list = NULL;
00057   netif_default = NULL;
00058   /* restore netif_list for next tests (e.g. loopif) */
00059   netif_list = old_netif_list;
00060   netif_default = old_netif_default;
00061   lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
00062 }
00063 
00064 static void test_mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status)
00065 {
00066   LWIP_UNUSED_ARG(client);
00067   LWIP_UNUSED_ARG(arg);
00068   LWIP_UNUSED_ARG(status);
00069 }
00070 
00071 START_TEST(basic_connect)
00072 {
00073   mqtt_client_t* client;
00074   struct netif netif;
00075   err_t err;
00076   struct mqtt_connect_client_info_t client_info = {
00077     "dumm",
00078     NULL, NULL,
00079     10,
00080     NULL, NULL, 0, 0
00081   };
00082   struct pbuf *p;
00083   unsigned char rxbuf[] = {0x20, 0x02, 0x00, 0x00};
00084   LWIP_UNUSED_ARG(_i);
00085 
00086   test_mqtt_init_netif(&netif, &test_mqtt_local_ip, &test_mqtt_netmask);
00087 
00088   client = mqtt_client_new();
00089   fail_unless(client != NULL);
00090   err = mqtt_client_connect(client, &test_mqtt_remote_ip, 1234, test_mqtt_connection_cb, NULL, &client_info);
00091   fail_unless(err == ERR_OK);
00092 
00093   client->conn->connected(client->conn->callback_arg, client->conn, ERR_OK);
00094   p = pbuf_alloc(PBUF_RAW, sizeof(rxbuf), PBUF_REF);
00095   fail_unless(p != NULL);
00096   p->payload = rxbuf;
00097   /* since we hack the rx path, we have to hack the rx window, too: */
00098   client->conn->rcv_wnd -= p->tot_len;
00099   if (client->conn->recv(client->conn->callback_arg, client->conn, p, ERR_OK) != ERR_OK) {
00100     pbuf_free(p);
00101   }
00102 
00103   mqtt_disconnect(client);
00104   /* fixme: mqtt_client_fre() is missing... */
00105   mem_free(client);
00106 }
00107 END_TEST
00108 
00109 Suite* mqtt_suite(void)
00110 {
00111   testfunc tests[] = {
00112     TESTFUNC(basic_connect),
00113   };
00114   return create_suite("MQTT", tests, sizeof(tests)/sizeof(testfunc), mqtt_setup, mqtt_teardown);
00115 }