mirror of
https://github.com/lwip-tcpip/lwip.git
synced 2025-02-04 12:39:58 +00:00
added unit test for mqtt (that does not really do something useful yet)
This commit is contained in:
parent
bf01941b23
commit
5cc168c0f1
@ -8,6 +8,7 @@
|
||||
#include "etharp/test_etharp.h"
|
||||
#include "dhcp/test_dhcp.h"
|
||||
#include "mdns/test_mdns.h"
|
||||
#include "mqtt/test_mqtt.h"
|
||||
#include "api/test_sockets.h"
|
||||
|
||||
#include "lwip/init.h"
|
||||
@ -49,6 +50,7 @@ int main(void)
|
||||
etharp_suite,
|
||||
dhcp_suite,
|
||||
mdns_suite,
|
||||
mqtt_suite,
|
||||
sockets_suite
|
||||
};
|
||||
size_t num = sizeof(suites)/sizeof(void*);
|
||||
|
@ -64,4 +64,6 @@
|
||||
/* Minimal changes to opt.h required for etharp unit tests: */
|
||||
#define ETHARP_SUPPORT_STATIC_ENTRIES 1
|
||||
|
||||
#define MEMP_NUM_SYS_TIMEOUT (LWIP_NUM_SYS_TIMEOUT_INTERNAL + 1)
|
||||
|
||||
#endif /* LWIP_HDR_LWIPOPTS_H */
|
||||
|
107
test/unit/mqtt/test_mqtt.c
Normal file
107
test/unit/mqtt/test_mqtt.c
Normal file
@ -0,0 +1,107 @@
|
||||
#include "test_mqtt.h"
|
||||
|
||||
#include "lwip/pbuf.h"
|
||||
#include "lwip/apps/mqtt.h"
|
||||
#include "lwip/apps/mqtt_priv.h"
|
||||
#include "lwip/netif.h"
|
||||
|
||||
const ip_addr_t test_mqtt_local_ip = IPADDR4_INIT_BYTES(192, 168, 1, 1);
|
||||
const ip_addr_t test_mqtt_remote_ip = IPADDR4_INIT_BYTES(192, 168, 1, 2);
|
||||
const ip_addr_t test_mqtt_netmask = IPADDR4_INIT_BYTES(255, 255, 255, 0);
|
||||
|
||||
static err_t test_mqtt_netif_output(struct netif *netif, struct pbuf *p,
|
||||
const ip4_addr_t *ipaddr)
|
||||
{
|
||||
LWIP_UNUSED_ARG(netif);
|
||||
LWIP_UNUSED_ARG(ipaddr);
|
||||
LWIP_UNUSED_ARG(p);
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
static void
|
||||
test_mqtt_init_netif(struct netif *netif, const ip_addr_t *ip_addr, const ip_addr_t *netmask)
|
||||
{
|
||||
struct netif *n;
|
||||
memset(netif, 0, sizeof(struct netif));
|
||||
netif->output = test_mqtt_netif_output;
|
||||
netif->flags |= NETIF_FLAG_UP | NETIF_FLAG_LINK_UP;
|
||||
ip_addr_copy_from_ip4(netif->netmask, *ip_2_ip4(netmask));
|
||||
ip_addr_copy_from_ip4(netif->ip_addr, *ip_2_ip4(ip_addr));
|
||||
for (n = netif_list; n != NULL; n = n->next) {
|
||||
if (n == netif) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
netif->next = NULL;
|
||||
netif_list = netif;
|
||||
}
|
||||
|
||||
/* Setups/teardown functions */
|
||||
static struct netif *old_netif_list;
|
||||
static struct netif *old_netif_default;
|
||||
|
||||
static void
|
||||
mqtt_setup(void)
|
||||
{
|
||||
old_netif_list = netif_list;
|
||||
old_netif_default = netif_default;
|
||||
netif_list = NULL;
|
||||
netif_default = NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
mqtt_teardown(void)
|
||||
{
|
||||
netif_list = NULL;
|
||||
netif_default = NULL;
|
||||
/* restore netif_list for next tests (e.g. loopif) */
|
||||
netif_list = old_netif_list;
|
||||
netif_default = old_netif_default;
|
||||
}
|
||||
|
||||
static void test_mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status)
|
||||
{
|
||||
LWIP_UNUSED_ARG(client);
|
||||
LWIP_UNUSED_ARG(arg);
|
||||
LWIP_UNUSED_ARG(status);
|
||||
}
|
||||
|
||||
START_TEST(basic_connect)
|
||||
{
|
||||
mqtt_client_t* client;
|
||||
struct netif netif;
|
||||
err_t err;
|
||||
struct mqtt_connect_client_info_t client_info = {
|
||||
"dumm",
|
||||
NULL, NULL,
|
||||
10,
|
||||
NULL, NULL, 0, 0
|
||||
};
|
||||
struct pbuf *p;
|
||||
unsigned char rxbuf[] = {0x20, 0x02, 0x00, 0x00};
|
||||
LWIP_UNUSED_ARG(_i);
|
||||
|
||||
test_mqtt_init_netif(&netif, &test_mqtt_local_ip, &test_mqtt_netmask);
|
||||
|
||||
client = mqtt_client_new();
|
||||
fail_unless(client != NULL);
|
||||
err = mqtt_client_connect(client, &test_mqtt_remote_ip, 1234, test_mqtt_connection_cb, NULL, &client_info);
|
||||
fail_unless(err == ERR_OK);
|
||||
|
||||
client->conn->connected(client->conn->callback_arg, client->conn, ERR_OK);
|
||||
p = pbuf_alloc(PBUF_RAW, sizeof(rxbuf), PBUF_REF);
|
||||
fail_unless(p != NULL);
|
||||
p->payload = rxbuf;
|
||||
if (client->conn->recv(client->conn->callback_arg, client->conn, p, ERR_OK) != ERR_OK) {
|
||||
pbuf_free(p);
|
||||
}
|
||||
}
|
||||
END_TEST
|
||||
|
||||
Suite* mqtt_suite(void)
|
||||
{
|
||||
testfunc tests[] = {
|
||||
TESTFUNC(basic_connect),
|
||||
};
|
||||
return create_suite("MQTT", tests, sizeof(tests)/sizeof(testfunc), mqtt_setup, mqtt_teardown);
|
||||
}
|
8
test/unit/mqtt/test_mqtt.h
Normal file
8
test/unit/mqtt/test_mqtt.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef LWIP_HDR_TEST_MQTT_H__
|
||||
#define LWIP_HDR_TEST_MQTT_H__
|
||||
|
||||
#include "../lwip_check.h"
|
||||
|
||||
Suite* mqtt_suite(void);
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user