unit tests: added test_netif (checking ext_callbacks only, for now)

This commit is contained in:
goldsimon 2018-01-12 22:27:15 +01:00
parent 452c6a5378
commit fa75ffed9d
5 changed files with 203 additions and 0 deletions

View File

@ -35,6 +35,7 @@ TESTFILES=$(TESTDIR)/lwip_unittests.c \
$(TESTDIR)/arch/sys_arch.c \
$(TESTDIR)/core/test_def.c \
$(TESTDIR)/core/test_mem.c \
$(TESTDIR)/core/test_netif.c \
$(TESTDIR)/core/test_pbuf.c \
$(TESTDIR)/core/test_timers.c \
$(TESTDIR)/dhcp/test_dhcp.c \

189
test/unit/core/test_netif.c Normal file
View File

@ -0,0 +1,189 @@
#include "test_netif.h"
#include "lwip/netif.h"
#include "lwip/stats.h"
#include "lwip/etharp.h"
#include "netif/ethernet.h"
#if !LWIP_NETIF_EXT_STATUS_CALLBACK
#error "This tests needs LWIP_NETIF_EXT_STATUS_CALLBACK enabled"
#endif
struct netif net_test;
/* Setups/teardown functions */
static void
netif_setup(void)
{
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
static void
netif_teardown(void)
{
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
/* test helper functions */
static err_t
testif_tx_func(struct netif *netif, struct pbuf *p)
{
LWIP_UNUSED_ARG(netif);
LWIP_UNUSED_ARG(p);
return ERR_OK;
}
static err_t
testif_init(struct netif *netif)
{
netif->name[0] = 'c';
netif->name[1] = 'h';
netif->output = etharp_output;
netif->linkoutput = testif_tx_func;
netif->mtu = 1500;
netif->hwaddr_len = 6;
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6;
netif->hwaddr[0] = 0x02;
netif->hwaddr[1] = 0x03;
netif->hwaddr[2] = 0x04;
netif->hwaddr[3] = 0x05;
netif->hwaddr[4] = 0x06;
netif->hwaddr[5] = 0x07;
return ERR_OK;
}
#define MAX_NSC_REASON_IDX 10
static int ext_cb_counters[MAX_NSC_REASON_IDX];
static netif_nsc_reason_t reasons;
static netif_ext_callback_args_t *expected_args;
static int dummy_active;
static void
test_netif_ext_callback_dummy(struct netif* netif, netif_nsc_reason_t reason, const netif_ext_callback_args_t* args)
{
LWIP_UNUSED_ARG(netif);
LWIP_UNUSED_ARG(reason);
LWIP_UNUSED_ARG(args);
fail_unless(dummy_active);
}
static void
test_netif_ext_callback(struct netif* netif, netif_nsc_reason_t reason, const netif_ext_callback_args_t* args)
{
int i;
u32_t reason_flags = (u32_t)reason;
u32_t mask;
reasons = reasons | reason;
fail_unless(netif == &net_test);
fail_unless(reason != 0);
fail_unless((((u32_t)reason) & ~((1U << MAX_NSC_REASON_IDX) - 1U)) == 0);
LWIP_UNUSED_ARG(args);
for (i = 0, mask = 1U; i < MAX_NSC_REASON_IDX; i++, mask <<= 1) {
if (reason_flags & mask) {
ext_cb_counters[i]++;
}
}
if (expected_args != NULL) {
fail_unless(memcmp(expected_args, args, sizeof(netif_ext_callback_args_t)) == 0);
}
}
static void
test_netif_ext_callback_assert_flag_count(netif_nsc_reason_t reason, int expected_count)
{
int i;
u32_t reason_flags = (u32_t)reason;
u32_t mask;
for (i = 0, mask = 1U; i < MAX_NSC_REASON_IDX; i++, mask <<= 1) {
if (reason_flags & mask) {
fail_unless(ext_cb_counters[i] == expected_count);
}
}
}
static void
test_netif_ext_callback_reset(void)
{
memset(ext_cb_counters, 0, sizeof(ext_cb_counters));
reasons = 0;
}
/* Test functions */
NETIF_DECLARE_EXT_CALLBACK(netif_callback_1)
NETIF_DECLARE_EXT_CALLBACK(netif_callback_2)
NETIF_DECLARE_EXT_CALLBACK(netif_callback_3)
START_TEST(test_netif_extcallbacks)
{
ip4_addr_t addr;
ip4_addr_t netmask;
ip4_addr_t gw;
LWIP_UNUSED_ARG(_i);
IP4_ADDR(&addr, 0, 0, 0, 0);
IP4_ADDR(&netmask, 0, 0, 0, 0);
IP4_ADDR(&gw, 0, 0, 0, 0);
netif_add_ext_callback(&netif_callback_3, test_netif_ext_callback_dummy);
netif_add_ext_callback(&netif_callback_2, test_netif_ext_callback);
netif_add_ext_callback(&netif_callback_1, test_netif_ext_callback_dummy);
dummy_active = 1;
reasons = 0;
netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);
fail_unless(reasons == LWIP_NSC_NETIF_ADDED);
test_netif_ext_callback_assert_flag_count(LWIP_NSC_NETIF_ADDED, 1);
test_netif_ext_callback_reset();
netif_set_link_up(&net_test);
fail_unless(reasons == LWIP_NSC_LINK_CHANGED);
test_netif_ext_callback_assert_flag_count(LWIP_NSC_LINK_CHANGED, 1);
test_netif_ext_callback_reset();
netif_set_up(&net_test);
fail_unless(reasons == LWIP_NSC_STATUS_CHANGED);
test_netif_ext_callback_assert_flag_count(LWIP_NSC_STATUS_CHANGED, 1);
test_netif_ext_callback_reset();
IP4_ADDR(&addr, 1, 2, 3, 4);
netif_set_ipaddr(&net_test, &addr);
fail_unless(reasons == LWIP_NSC_IPV4_ADDRESS_CHANGED);
test_netif_ext_callback_assert_flag_count(LWIP_NSC_IPV4_ADDRESS_CHANGED, 1);
test_netif_ext_callback_reset();
netif_remove(&net_test);
fail_unless(reasons == (LWIP_NSC_NETIF_REMOVED | LWIP_NSC_STATUS_CHANGED));
test_netif_ext_callback_assert_flag_count(LWIP_NSC_NETIF_REMOVED, 1);
test_netif_ext_callback_assert_flag_count(LWIP_NSC_STATUS_CHANGED, 1);
test_netif_ext_callback_reset();
netif_remove_ext_callback(&netif_callback_2);
netif_remove_ext_callback(&netif_callback_3);
netif_remove_ext_callback(&netif_callback_1);
dummy_active = 0;
}
END_TEST
/** Create the suite including all tests for this module */
Suite *
netif_suite(void)
{
testfunc tests[] = {
TESTFUNC(test_netif_extcallbacks)
};
return create_suite("NETIF", tests, sizeof(tests)/sizeof(testfunc), netif_setup, netif_teardown);
}

View File

@ -0,0 +1,8 @@
#ifndef LWIP_HDR_TEST_NETIF_H
#define LWIP_HDR_TEST_NETIF_H
#include "../lwip_check.h"
Suite *netif_suite(void);
#endif

View File

@ -6,6 +6,7 @@
#include "tcp/test_tcp_oos.h"
#include "core/test_def.h"
#include "core/test_mem.h"
#include "core/test_netif.h"
#include "core/test_pbuf.h"
#include "core/test_timers.h"
#include "etharp/test_etharp.h"
@ -66,6 +67,7 @@ int main(void)
tcp_oos_suite,
def_suite,
mem_suite,
netif_suite,
pbuf_suite,
timers_suite,
etharp_suite,

View File

@ -72,6 +72,9 @@
/* MIB2 stats are required to check IPv4 reassembly results */
#define MIB2_STATS 1
/* netif tests want to test this, so enable: */
#define LWIP_NETIF_EXT_STATUS_CALLBACK 1
/* Check lwip_stats.mem.illegal instead of asserting */
#define LWIP_MEM_ILLEGAL_FREE(msg) /* to nothing */