2010-03-20 11:45:25 +00:00
|
|
|
#include "test_etharp.h"
|
|
|
|
|
|
|
|
#include "lwip/udp.h"
|
2016-08-19 07:03:57 +00:00
|
|
|
#include "lwip/etharp.h"
|
2019-08-10 09:31:24 +00:00
|
|
|
#include "lwip/inet.h"
|
2016-08-19 07:03:57 +00:00
|
|
|
#include "netif/ethernet.h"
|
2010-03-20 11:45:25 +00:00
|
|
|
#include "lwip/stats.h"
|
2017-08-07 19:06:19 +00:00
|
|
|
#include "lwip/prot/iana.h"
|
2010-03-20 11:45:25 +00:00
|
|
|
|
|
|
|
#if !LWIP_STATS || !UDP_STATS || !MEMP_STATS || !ETHARP_STATS
|
|
|
|
#error "This tests needs UDP-, MEMP- and ETHARP-statistics enabled"
|
|
|
|
#endif
|
|
|
|
#if !ETHARP_SUPPORT_STATIC_ENTRIES
|
|
|
|
#error "This test needs ETHARP_SUPPORT_STATIC_ENTRIES enabled"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static struct netif test_netif;
|
2015-04-10 06:51:06 +00:00
|
|
|
static ip4_addr_t test_ipaddr, test_netmask, test_gw;
|
2015-05-28 12:08:06 +00:00
|
|
|
struct eth_addr test_ethaddr = {{1,1,1,1,1,1}};
|
|
|
|
struct eth_addr test_ethaddr2 = {{1,1,1,1,1,2}};
|
|
|
|
struct eth_addr test_ethaddr3 = {{1,1,1,1,1,3}};
|
|
|
|
struct eth_addr test_ethaddr4 = {{1,1,1,1,1,4}};
|
2010-03-20 11:45:25 +00:00
|
|
|
static int linkoutput_ctr;
|
|
|
|
|
|
|
|
/* Helper functions */
|
|
|
|
static void
|
|
|
|
etharp_remove_all(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
/* call etharp_tmr often enough to have all entries cleaned */
|
|
|
|
for(i = 0; i < 0xff; i++) {
|
|
|
|
etharp_tmr();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static err_t
|
|
|
|
default_netif_linkoutput(struct netif *netif, struct pbuf *p)
|
|
|
|
{
|
|
|
|
fail_unless(netif == &test_netif);
|
|
|
|
fail_unless(p != NULL);
|
|
|
|
linkoutput_ctr++;
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static err_t
|
|
|
|
default_netif_init(struct netif *netif)
|
|
|
|
{
|
|
|
|
fail_unless(netif != NULL);
|
|
|
|
netif->linkoutput = default_netif_linkoutput;
|
|
|
|
netif->output = etharp_output;
|
|
|
|
netif->mtu = 1500;
|
|
|
|
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
|
|
|
|
netif->hwaddr_len = ETHARP_HWADDR_LEN;
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
default_netif_add(void)
|
|
|
|
{
|
|
|
|
IP4_ADDR(&test_gw, 192,168,0,1);
|
|
|
|
IP4_ADDR(&test_ipaddr, 192,168,0,1);
|
|
|
|
IP4_ADDR(&test_netmask, 255,255,0,0);
|
|
|
|
|
|
|
|
fail_unless(netif_default == NULL);
|
|
|
|
netif_set_default(netif_add(&test_netif, &test_ipaddr, &test_netmask,
|
2018-06-13 08:46:44 +00:00
|
|
|
&test_gw, NULL, default_netif_init, NULL));
|
2010-03-20 11:45:25 +00:00
|
|
|
netif_set_up(&test_netif);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
default_netif_remove(void)
|
|
|
|
{
|
|
|
|
fail_unless(netif_default == &test_netif);
|
|
|
|
netif_remove(&test_netif);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-04-10 06:51:06 +00:00
|
|
|
create_arp_response(ip4_addr_t *adr)
|
2010-03-20 11:45:25 +00:00
|
|
|
{
|
|
|
|
int k;
|
|
|
|
struct eth_hdr *ethhdr;
|
|
|
|
struct etharp_hdr *etharphdr;
|
|
|
|
struct pbuf *p = pbuf_alloc(PBUF_RAW, sizeof(struct eth_hdr) + sizeof(struct etharp_hdr), PBUF_RAM);
|
|
|
|
if(p == NULL) {
|
|
|
|
FAIL_RET();
|
|
|
|
}
|
|
|
|
ethhdr = (struct eth_hdr*)p->payload;
|
|
|
|
etharphdr = (struct etharp_hdr*)(ethhdr + 1);
|
|
|
|
|
|
|
|
ethhdr->dest = test_ethaddr;
|
|
|
|
ethhdr->src = test_ethaddr2;
|
|
|
|
ethhdr->type = htons(ETHTYPE_ARP);
|
|
|
|
|
2017-08-08 10:20:56 +00:00
|
|
|
etharphdr->hwtype = htons(LWIP_IANA_HWTYPE_ETHERNET);
|
2010-03-20 11:45:25 +00:00
|
|
|
etharphdr->proto = htons(ETHTYPE_IP);
|
2011-03-30 18:47:20 +00:00
|
|
|
etharphdr->hwlen = ETHARP_HWADDR_LEN;
|
2015-04-10 06:51:06 +00:00
|
|
|
etharphdr->protolen = sizeof(ip4_addr_t);
|
2010-03-20 11:45:25 +00:00
|
|
|
etharphdr->opcode = htons(ARP_REPLY);
|
|
|
|
|
2015-04-10 06:51:06 +00:00
|
|
|
SMEMCPY(ðarphdr->sipaddr, adr, sizeof(ip4_addr_t));
|
|
|
|
SMEMCPY(ðarphdr->dipaddr, &test_ipaddr, sizeof(ip4_addr_t));
|
2010-03-20 11:45:25 +00:00
|
|
|
|
|
|
|
k = 6;
|
|
|
|
while(k > 0) {
|
|
|
|
k--;
|
|
|
|
/* Write the ARP MAC-Addresses */
|
|
|
|
etharphdr->shwaddr.addr[k] = test_ethaddr2.addr[k];
|
|
|
|
etharphdr->dhwaddr.addr[k] = test_ethaddr.addr[k];
|
|
|
|
/* Write the Ethernet MAC-Addresses */
|
|
|
|
ethhdr->dest.addr[k] = test_ethaddr.addr[k];
|
|
|
|
ethhdr->src.addr[k] = test_ethaddr2.addr[k];
|
|
|
|
}
|
|
|
|
|
|
|
|
ethernet_input(p, &test_netif);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setups/teardown functions */
|
|
|
|
|
|
|
|
static void
|
|
|
|
etharp_setup(void)
|
|
|
|
{
|
|
|
|
etharp_remove_all();
|
|
|
|
default_netif_add();
|
2017-08-03 20:25:33 +00:00
|
|
|
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
|
2010-03-20 11:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
etharp_teardown(void)
|
|
|
|
{
|
|
|
|
etharp_remove_all();
|
|
|
|
default_netif_remove();
|
2017-08-03 20:25:33 +00:00
|
|
|
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
|
2010-03-20 11:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Test functions */
|
|
|
|
|
|
|
|
START_TEST(test_etharp_table)
|
|
|
|
{
|
2010-03-26 13:40:31 +00:00
|
|
|
#if ETHARP_SUPPORT_STATIC_ENTRIES
|
2010-03-20 11:45:25 +00:00
|
|
|
err_t err;
|
2010-03-26 13:40:31 +00:00
|
|
|
#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
|
2018-06-08 18:03:51 +00:00
|
|
|
ssize_t idx;
|
2015-09-27 20:59:15 +00:00
|
|
|
const ip4_addr_t *unused_ipaddr;
|
2010-03-20 11:45:25 +00:00
|
|
|
struct eth_addr *unused_ethaddr;
|
|
|
|
struct udp_pcb* pcb;
|
|
|
|
LWIP_UNUSED_ARG(_i);
|
|
|
|
|
|
|
|
if (netif_default != &test_netif) {
|
|
|
|
fail("This test needs a default netif");
|
|
|
|
}
|
|
|
|
|
|
|
|
linkoutput_ctr = 0;
|
|
|
|
|
|
|
|
pcb = udp_new();
|
|
|
|
fail_unless(pcb != NULL);
|
|
|
|
if (pcb != NULL) {
|
2015-04-10 06:51:06 +00:00
|
|
|
ip4_addr_t adrs[ARP_TABLE_SIZE + 2];
|
2010-03-20 11:45:25 +00:00
|
|
|
int i;
|
|
|
|
for(i = 0; i < ARP_TABLE_SIZE + 2; i++) {
|
|
|
|
IP4_ADDR(&adrs[i], 192,168,0,i+2);
|
|
|
|
}
|
|
|
|
/* fill ARP-table with dynamic entries */
|
|
|
|
for(i = 0; i < ARP_TABLE_SIZE; i++) {
|
|
|
|
struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, 10, PBUF_RAM);
|
|
|
|
fail_unless(p != NULL);
|
|
|
|
if (p != NULL) {
|
2016-06-23 20:30:37 +00:00
|
|
|
err_t err2;
|
2015-04-10 06:51:06 +00:00
|
|
|
ip_addr_t dst;
|
|
|
|
ip_addr_copy_from_ip4(dst, adrs[i]);
|
2016-06-23 20:30:37 +00:00
|
|
|
err2 = udp_sendto(pcb, p, &dst, 123);
|
|
|
|
fail_unless(err2 == ERR_OK);
|
2010-03-20 11:45:25 +00:00
|
|
|
/* etharp request sent? */
|
|
|
|
fail_unless(linkoutput_ctr == (2*i) + 1);
|
|
|
|
pbuf_free(p);
|
|
|
|
|
|
|
|
/* create an ARP response */
|
|
|
|
create_arp_response(&adrs[i]);
|
|
|
|
/* queued UDP packet sent? */
|
|
|
|
fail_unless(linkoutput_ctr == (2*i) + 2);
|
|
|
|
|
|
|
|
idx = etharp_find_addr(NULL, &adrs[i], &unused_ethaddr, &unused_ipaddr);
|
|
|
|
fail_unless(idx == i);
|
|
|
|
etharp_tmr();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
linkoutput_ctr = 0;
|
2010-03-26 13:40:31 +00:00
|
|
|
#if ETHARP_SUPPORT_STATIC_ENTRIES
|
2010-03-20 11:45:25 +00:00
|
|
|
/* create one static entry */
|
|
|
|
err = etharp_add_static_entry(&adrs[ARP_TABLE_SIZE], &test_ethaddr3);
|
|
|
|
fail_unless(err == ERR_OK);
|
|
|
|
idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
|
|
|
|
fail_unless(idx == 0);
|
|
|
|
fail_unless(linkoutput_ctr == 0);
|
2010-03-26 13:40:31 +00:00
|
|
|
#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
|
2010-03-20 11:45:25 +00:00
|
|
|
|
|
|
|
linkoutput_ctr = 0;
|
|
|
|
/* fill ARP-table with dynamic entries */
|
|
|
|
for(i = 0; i < ARP_TABLE_SIZE; i++) {
|
|
|
|
struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, 10, PBUF_RAM);
|
|
|
|
fail_unless(p != NULL);
|
|
|
|
if (p != NULL) {
|
2016-06-23 20:30:37 +00:00
|
|
|
err_t err2;
|
2015-04-10 06:51:06 +00:00
|
|
|
ip_addr_t dst;
|
|
|
|
ip_addr_copy_from_ip4(dst, adrs[i]);
|
2016-06-23 20:30:37 +00:00
|
|
|
err2 = udp_sendto(pcb, p, &dst, 123);
|
|
|
|
fail_unless(err2 == ERR_OK);
|
2010-03-20 11:45:25 +00:00
|
|
|
/* etharp request sent? */
|
|
|
|
fail_unless(linkoutput_ctr == (2*i) + 1);
|
|
|
|
pbuf_free(p);
|
|
|
|
|
|
|
|
/* create an ARP response */
|
|
|
|
create_arp_response(&adrs[i]);
|
|
|
|
/* queued UDP packet sent? */
|
|
|
|
fail_unless(linkoutput_ctr == (2*i) + 2);
|
|
|
|
|
|
|
|
idx = etharp_find_addr(NULL, &adrs[i], &unused_ethaddr, &unused_ipaddr);
|
|
|
|
if (i < ARP_TABLE_SIZE - 1) {
|
|
|
|
fail_unless(idx == i+1);
|
|
|
|
} else {
|
|
|
|
/* the last entry must not overwrite the static entry! */
|
|
|
|
fail_unless(idx == 1);
|
|
|
|
}
|
|
|
|
etharp_tmr();
|
|
|
|
}
|
|
|
|
}
|
2010-03-26 13:40:31 +00:00
|
|
|
#if ETHARP_SUPPORT_STATIC_ENTRIES
|
2010-03-20 11:45:25 +00:00
|
|
|
/* create a second static entry */
|
|
|
|
err = etharp_add_static_entry(&adrs[ARP_TABLE_SIZE+1], &test_ethaddr4);
|
|
|
|
fail_unless(err == ERR_OK);
|
|
|
|
idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
|
|
|
|
fail_unless(idx == 0);
|
|
|
|
idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE+1], &unused_ethaddr, &unused_ipaddr);
|
|
|
|
fail_unless(idx == 2);
|
|
|
|
/* and remove it again */
|
|
|
|
err = etharp_remove_static_entry(&adrs[ARP_TABLE_SIZE+1]);
|
|
|
|
fail_unless(err == ERR_OK);
|
|
|
|
idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
|
|
|
|
fail_unless(idx == 0);
|
|
|
|
idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE+1], &unused_ethaddr, &unused_ipaddr);
|
|
|
|
fail_unless(idx == -1);
|
2010-03-26 13:40:31 +00:00
|
|
|
#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
|
2010-03-20 11:45:25 +00:00
|
|
|
|
|
|
|
/* check that static entries don't time out */
|
|
|
|
etharp_remove_all();
|
|
|
|
idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
|
|
|
|
fail_unless(idx == 0);
|
|
|
|
|
2010-03-26 13:40:31 +00:00
|
|
|
#if ETHARP_SUPPORT_STATIC_ENTRIES
|
2010-03-20 11:45:25 +00:00
|
|
|
/* remove the first static entry */
|
|
|
|
err = etharp_remove_static_entry(&adrs[ARP_TABLE_SIZE]);
|
|
|
|
fail_unless(err == ERR_OK);
|
|
|
|
idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
|
|
|
|
fail_unless(idx == -1);
|
|
|
|
idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE+1], &unused_ethaddr, &unused_ipaddr);
|
|
|
|
fail_unless(idx == -1);
|
2010-03-26 13:40:31 +00:00
|
|
|
#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
|
2010-03-20 11:45:25 +00:00
|
|
|
|
|
|
|
udp_remove(pcb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
|
|
|
|
/** Create the suite including all tests for this module */
|
|
|
|
Suite *
|
|
|
|
etharp_suite(void)
|
|
|
|
{
|
2014-02-12 20:52:21 +00:00
|
|
|
testfunc tests[] = {
|
|
|
|
TESTFUNC(test_etharp_table)
|
2010-03-20 11:45:25 +00:00
|
|
|
};
|
2014-02-12 20:52:21 +00:00
|
|
|
return create_suite("ETHARP", tests, sizeof(tests)/sizeof(testfunc), etharp_setup, etharp_teardown);
|
2010-03-20 11:45:25 +00:00
|
|
|
}
|