diff --git a/test/unit/api/test_sockets.c b/test/unit/api/test_sockets.c index a4362142..4972f28a 100644 --- a/test/unit/api/test_sockets.c +++ b/test/unit/api/test_sockets.c @@ -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 diff --git a/test/unit/core/test_mem.c b/test/unit/core/test_mem.c index 66b4877e..c362758d 100644 --- a/test/unit/core/test_mem.c +++ b/test/unit/core/test_mem.c @@ -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)); } diff --git a/test/unit/core/test_pbuf.c b/test/unit/core/test_pbuf.c index 8ce0e9d3..86b90b29 100644 --- a/test/unit/core/test_pbuf.c +++ b/test/unit/core/test_pbuf.c @@ -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)); } diff --git a/test/unit/dhcp/test_dhcp.c b/test/unit/dhcp/test_dhcp.c index 47aaa084..1b0c8843 100644 --- a/test/unit/dhcp/test_dhcp.c +++ b/test/unit/dhcp/test_dhcp.c @@ -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 diff --git a/test/unit/etharp/test_etharp.c b/test/unit/etharp/test_etharp.c index 59d73b9b..67b02d0b 100644 --- a/test/unit/etharp/test_etharp.c +++ b/test/unit/etharp/test_etharp.c @@ -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)); } diff --git a/test/unit/ip4/test_ip4.c b/test/unit/ip4/test_ip4.c index e0e82d8d..a5a7fd93 100644 --- a/test/unit/ip4/test_ip4.c +++ b/test/unit/ip4/test_ip4.c @@ -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)); } diff --git a/test/unit/lwip_check.h b/test/unit/lwip_check.h index 0c218d1d..73468ddf 100644 --- a/test/unit/lwip_check.h +++ b/test/unit/lwip_check.h @@ -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 */ diff --git a/test/unit/lwip_unittests.c b/test/unit/lwip_unittests.c index dd53a204..f0dce30d 100644 --- a/test/unit/lwip_unittests.c +++ b/test/unit/lwip_unittests.c @@ -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 diff --git a/test/unit/mqtt/test_mqtt.c b/test/unit/mqtt/test_mqtt.c index 36e24adf..32f08e67 100644 --- a/test/unit/mqtt/test_mqtt.c +++ b/test/unit/mqtt/test_mqtt.c @@ -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 diff --git a/test/unit/tcp/test_tcp.c b/test/unit/tcp/test_tcp.c index 3edde8b6..9db2369b 100644 --- a/test/unit/tcp/test_tcp.c +++ b/test/unit/tcp/test_tcp.c @@ -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)); } diff --git a/test/unit/tcp/test_tcp_oos.c b/test/unit/tcp/test_tcp_oos.c index 2f87833f..a190e7dc 100644 --- a/test/unit/tcp/test_tcp_oos.c +++ b/test/unit/tcp/test_tcp_oos.c @@ -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)); } diff --git a/test/unit/udp/test_udp.c b/test/unit/udp/test_udp.c index 147822f4..4ad693e9 100644 --- a/test/unit/udp/test_udp.c +++ b/test/unit/udp/test_udp.c @@ -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)); }