Ensure that unit tests leave the stack in a clean state

This commit is contained in:
goldsimon 2017-08-03 22:25:33 +02:00
parent 6fa5d02435
commit 4cec20230e
12 changed files with 77 additions and 1 deletions

View File

@ -7,6 +7,7 @@
#include "lwip/stats.h"
#include "lwip/tcpip.h"
#include "lwip/priv/tcp_priv.h"
static int
@ -32,12 +33,23 @@ test_sockets_get_used_count(void)
static void
sockets_setup(void)
{
/* expect full free heap */
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
static void
sockets_teardown(void)
{
fail_unless(test_sockets_get_used_count() == 0);
/* poll until all memory is released... */
tcpip_thread_poll_one();
while (tcp_tw_pcbs) {
tcp_abort(tcp_tw_pcbs);
tcpip_thread_poll_one();
}
tcpip_thread_poll_one();
/* ensure full free heap */
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
#ifndef NUM_SOCKETS

View File

@ -15,11 +15,13 @@
static void
mem_setup(void)
{
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
static void
mem_teardown(void)
{
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}

View File

@ -18,11 +18,13 @@
static void
pbuf_setup(void)
{
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
static void
pbuf_teardown(void)
{
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}

View File

@ -121,7 +121,8 @@ static enum tcase {
TEST_LWIP_DHCP_NAK,
TEST_LWIP_DHCP_RELAY,
TEST_LWIP_DHCP_NAK_NO_ENDMARKER,
TEST_LWIP_DHCP_INVALID_OVERLOAD
TEST_LWIP_DHCP_INVALID_OVERLOAD,
TEST_NONE
} tcase;
static int debug = 0;
@ -188,10 +189,12 @@ static err_t testif_init(struct netif *netif)
static void dhcp_setup(void)
{
txpacket = 0;
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
static void dhcp_teardown(void)
{
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
static void check_pkt(struct pbuf *p, u32_t pos, const u8_t *mem, u32_t len)
@ -490,6 +493,9 @@ START_TEST(test_dhcp)
fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t)));
fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t)));
tcase = TEST_NONE;
dhcp_stop(&net_test);
dhcp_cleanup(&net_test);
netif_remove(&net_test);
}
END_TEST
@ -552,6 +558,9 @@ START_TEST(test_dhcp_nak)
fail_unless(txpacket == 4); /* DHCP nak sent */
tcase = TEST_NONE;
dhcp_stop(&net_test);
dhcp_cleanup(&net_test);
netif_remove(&net_test);
}
END_TEST
@ -796,6 +805,9 @@ START_TEST(test_dhcp_relayed)
fail_unless(txpacket == 7, "txpacket = %d", txpacket);
tcase = TEST_NONE;
dhcp_stop(&net_test);
dhcp_cleanup(&net_test);
netif_remove(&net_test);
}
@ -899,6 +911,9 @@ START_TEST(test_dhcp_nak_no_endmarker)
/* NAK should put us in another state for a while, no other way detecting it */
fail_unless(netif_dhcp_data(&net_test)->state != DHCP_STATE_REQUESTING);
tcase = TEST_NONE;
dhcp_stop(&net_test);
dhcp_cleanup(&net_test);
netif_remove(&net_test);
}
END_TEST
@ -1005,6 +1020,9 @@ START_TEST(test_dhcp_invalid_overload)
fail_unless(txpacket == 2); /* No more sent */
xid = htonl(netif_dhcp_data(&net_test)->xid); /* xid updated */
tcase = TEST_NONE;
dhcp_stop(&net_test);
dhcp_cleanup(&net_test);
netif_remove(&net_test);
}
END_TEST

View File

@ -119,6 +119,7 @@ etharp_setup(void)
{
etharp_remove_all();
default_netif_add();
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
static void
@ -126,6 +127,7 @@ etharp_teardown(void)
{
etharp_remove_all();
default_netif_remove();
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}

View File

@ -6,6 +6,8 @@
#include "lwip/prot/ip.h"
#include "lwip/prot/ip4.h"
#include "lwip/tcpip.h"
#if !LWIP_IPV4 || !IP_REASSEMBLY || !MIB2_STATS || !IPFRAG_STATS
#error "This tests needs LWIP_IPV4, IP_REASSEMBLY; MIB2- and IPFRAG-statistics enabled"
#endif
@ -55,6 +57,7 @@ create_ip4_input_fragment(u16_t ip_id, u16_t start, u16_t len, int last)
static void
ip4_setup(void)
{
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
static void
@ -65,6 +68,9 @@ ip4_teardown(void)
netif_list->loop_first = NULL;
}
netif_list->loop_last = NULL;
/* poll until all memory is released... */
tcpip_thread_poll_one();
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}

View File

@ -34,4 +34,9 @@ Suite* create_suite(const char* name, testfunc *tests, size_t num_tests, SFun se
int lwip_unittests_run(void)
#endif
/* helper functions */
#define SKIP_POOL(x) (1 << x)
#define SKIP_HEAP (1 << MEMP_MAX)
void lwip_check_ensure_no_alloc(uint32_t skip);
#endif /* LWIP_HDR_LWIP_CHECK_H */

View File

@ -33,6 +33,23 @@ Suite* create_suite(const char* name, testfunc *tests, size_t num_tests, SFun se
return s;
}
void lwip_check_ensure_no_alloc(uint32_t skip)
{
int i;
uint32_t mask;
fail_unless(skip != 0);
if (!(skip & SKIP_HEAP)) {
fail_unless(lwip_stats.mem.used == 0);
}
for (i = 0, mask = 1; i < MEMP_MAX; i++, mask <<= 1) {
if (!(skip & mask)) {
fail_unless(lwip_stats.memp[i]->used == 0);
}
}
}
#ifdef LWIP_UNITTESTS_LIB
int lwip_unittests_run(void)
#else

View File

@ -47,6 +47,7 @@ mqtt_setup(void)
old_netif_default = netif_default;
netif_list = NULL;
netif_default = NULL;
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
static void
@ -57,6 +58,7 @@ mqtt_teardown(void)
/* restore netif_list for next tests (e.g. loopif) */
netif_list = old_netif_list;
netif_default = old_netif_default;
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
static void test_mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status)
@ -95,6 +97,10 @@ START_TEST(basic_connect)
if (client->conn->recv(client->conn->callback_arg, client->conn, p, ERR_OK) != ERR_OK) {
pbuf_free(p);
}
mqtt_disconnect(client);
/* fixme: mqtt_client_fre() is missing... */
mem_free(client);
}
END_TEST

View File

@ -58,6 +58,7 @@ tcp_setup(void)
test_tcp_timer = 0;
tcp_remove_all();
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
static void
@ -69,6 +70,7 @@ tcp_teardown(void)
/* restore netif_list for next tests (e.g. loopif) */
netif_list = old_netif_list;
netif_default = old_netif_default;
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}

View File

@ -129,6 +129,7 @@ tcp_oos_setup(void)
netif_list = NULL;
netif_default = NULL;
tcp_remove_all();
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
static void
@ -140,6 +141,7 @@ tcp_oos_teardown(void)
/* restore netif_list for next tests (e.g. loopif) */
netif_list = old_netif_list;
netif_default = old_netif_default;
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}

View File

@ -28,12 +28,14 @@ static void
udp_setup(void)
{
udp_remove_all();
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
static void
udp_teardown(void)
{
udp_remove_all();
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}