1195972f6Sopenharmony_ci#include "test_mqtt.h" 2195972f6Sopenharmony_ci 3195972f6Sopenharmony_ci#include "lwip/pbuf.h" 4195972f6Sopenharmony_ci#include "lwip/apps/mqtt.h" 5195972f6Sopenharmony_ci#include "lwip/apps/mqtt_priv.h" 6195972f6Sopenharmony_ci#include "lwip/netif.h" 7195972f6Sopenharmony_ci 8195972f6Sopenharmony_ciconst ip_addr_t test_mqtt_local_ip = IPADDR4_INIT_BYTES(192, 168, 1, 1); 9195972f6Sopenharmony_ciconst ip_addr_t test_mqtt_remote_ip = IPADDR4_INIT_BYTES(192, 168, 1, 2); 10195972f6Sopenharmony_ciconst ip_addr_t test_mqtt_netmask = IPADDR4_INIT_BYTES(255, 255, 255, 0); 11195972f6Sopenharmony_ci 12195972f6Sopenharmony_cistatic err_t test_mqtt_netif_output(struct netif *netif, struct pbuf *p, 13195972f6Sopenharmony_ci const ip4_addr_t *ipaddr) 14195972f6Sopenharmony_ci{ 15195972f6Sopenharmony_ci LWIP_UNUSED_ARG(netif); 16195972f6Sopenharmony_ci LWIP_UNUSED_ARG(ipaddr); 17195972f6Sopenharmony_ci LWIP_UNUSED_ARG(p); 18195972f6Sopenharmony_ci return ERR_OK; 19195972f6Sopenharmony_ci} 20195972f6Sopenharmony_ci 21195972f6Sopenharmony_cistatic void 22195972f6Sopenharmony_citest_mqtt_init_netif(struct netif *netif, const ip_addr_t *ip_addr, const ip_addr_t *netmask) 23195972f6Sopenharmony_ci{ 24195972f6Sopenharmony_ci struct netif *n; 25195972f6Sopenharmony_ci memset(netif, 0, sizeof(struct netif)); 26195972f6Sopenharmony_ci netif->output = test_mqtt_netif_output; 27195972f6Sopenharmony_ci netif->flags |= NETIF_FLAG_UP | NETIF_FLAG_LINK_UP; 28195972f6Sopenharmony_ci ip_addr_copy_from_ip4(netif->netmask, *ip_2_ip4(netmask)); 29195972f6Sopenharmony_ci ip_addr_copy_from_ip4(netif->ip_addr, *ip_2_ip4(ip_addr)); 30195972f6Sopenharmony_ci for (n = netif_list; n != NULL; n = n->next) { 31195972f6Sopenharmony_ci if (n == netif) { 32195972f6Sopenharmony_ci return; 33195972f6Sopenharmony_ci } 34195972f6Sopenharmony_ci } 35195972f6Sopenharmony_ci netif->next = NULL; 36195972f6Sopenharmony_ci netif_list = netif; 37195972f6Sopenharmony_ci} 38195972f6Sopenharmony_ci 39195972f6Sopenharmony_ci/* Setups/teardown functions */ 40195972f6Sopenharmony_cistatic struct netif *old_netif_list; 41195972f6Sopenharmony_cistatic struct netif *old_netif_default; 42195972f6Sopenharmony_ci 43195972f6Sopenharmony_cistatic void 44195972f6Sopenharmony_cimqtt_setup(void) 45195972f6Sopenharmony_ci{ 46195972f6Sopenharmony_ci old_netif_list = netif_list; 47195972f6Sopenharmony_ci old_netif_default = netif_default; 48195972f6Sopenharmony_ci netif_list = NULL; 49195972f6Sopenharmony_ci netif_default = NULL; 50195972f6Sopenharmony_ci lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT)); 51195972f6Sopenharmony_ci} 52195972f6Sopenharmony_ci 53195972f6Sopenharmony_cistatic void 54195972f6Sopenharmony_cimqtt_teardown(void) 55195972f6Sopenharmony_ci{ 56195972f6Sopenharmony_ci netif_list = NULL; 57195972f6Sopenharmony_ci netif_default = NULL; 58195972f6Sopenharmony_ci /* restore netif_list for next tests (e.g. loopif) */ 59195972f6Sopenharmony_ci netif_list = old_netif_list; 60195972f6Sopenharmony_ci netif_default = old_netif_default; 61195972f6Sopenharmony_ci lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT)); 62195972f6Sopenharmony_ci} 63195972f6Sopenharmony_ci 64195972f6Sopenharmony_cistatic void test_mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) 65195972f6Sopenharmony_ci{ 66195972f6Sopenharmony_ci LWIP_UNUSED_ARG(client); 67195972f6Sopenharmony_ci LWIP_UNUSED_ARG(arg); 68195972f6Sopenharmony_ci LWIP_UNUSED_ARG(status); 69195972f6Sopenharmony_ci} 70195972f6Sopenharmony_ci 71195972f6Sopenharmony_ciSTART_TEST(basic_connect) 72195972f6Sopenharmony_ci{ 73195972f6Sopenharmony_ci mqtt_client_t* client; 74195972f6Sopenharmony_ci struct netif netif; 75195972f6Sopenharmony_ci err_t err; 76195972f6Sopenharmony_ci struct mqtt_connect_client_info_t client_info = { 77195972f6Sopenharmony_ci "dumm", 78195972f6Sopenharmony_ci NULL, NULL, 79195972f6Sopenharmony_ci 10, 80195972f6Sopenharmony_ci NULL, NULL, 0, 0 81195972f6Sopenharmony_ci }; 82195972f6Sopenharmony_ci struct pbuf *p; 83195972f6Sopenharmony_ci unsigned char rxbuf[] = {0x20, 0x02, 0x00, 0x00}; 84195972f6Sopenharmony_ci LWIP_UNUSED_ARG(_i); 85195972f6Sopenharmony_ci 86195972f6Sopenharmony_ci test_mqtt_init_netif(&netif, &test_mqtt_local_ip, &test_mqtt_netmask); 87195972f6Sopenharmony_ci 88195972f6Sopenharmony_ci client = mqtt_client_new(); 89195972f6Sopenharmony_ci fail_unless(client != NULL); 90195972f6Sopenharmony_ci err = mqtt_client_connect(client, &test_mqtt_remote_ip, 1234, test_mqtt_connection_cb, NULL, &client_info); 91195972f6Sopenharmony_ci fail_unless(err == ERR_OK); 92195972f6Sopenharmony_ci 93195972f6Sopenharmony_ci client->conn->connected(client->conn->callback_arg, client->conn, ERR_OK); 94195972f6Sopenharmony_ci p = pbuf_alloc(PBUF_RAW, sizeof(rxbuf), PBUF_REF); 95195972f6Sopenharmony_ci fail_unless(p != NULL); 96195972f6Sopenharmony_ci p->payload = rxbuf; 97195972f6Sopenharmony_ci /* since we hack the rx path, we have to hack the rx window, too: */ 98195972f6Sopenharmony_ci client->conn->rcv_wnd -= p->tot_len; 99195972f6Sopenharmony_ci if (client->conn->recv(client->conn->callback_arg, client->conn, p, ERR_OK) != ERR_OK) { 100195972f6Sopenharmony_ci pbuf_free(p); 101195972f6Sopenharmony_ci } 102195972f6Sopenharmony_ci 103195972f6Sopenharmony_ci mqtt_disconnect(client); 104195972f6Sopenharmony_ci /* fixme: mqtt_client_fre() is missing... */ 105195972f6Sopenharmony_ci mem_free(client); 106195972f6Sopenharmony_ci} 107195972f6Sopenharmony_ciEND_TEST 108195972f6Sopenharmony_ci 109195972f6Sopenharmony_ciSuite* mqtt_suite(void) 110195972f6Sopenharmony_ci{ 111195972f6Sopenharmony_ci testfunc tests[] = { 112195972f6Sopenharmony_ci TESTFUNC(basic_connect), 113195972f6Sopenharmony_ci }; 114195972f6Sopenharmony_ci return create_suite("MQTT", tests, sizeof(tests)/sizeof(testfunc), mqtt_setup, mqtt_teardown); 115195972f6Sopenharmony_ci} 116