ReactOS 0.4.16-dev-602-ge302bac
test_etharp.c
Go to the documentation of this file.
1#include "test_etharp.h"
2
3#include "lwip/udp.h"
4#include "lwip/etharp.h"
5#include "lwip/inet.h"
6#include "netif/ethernet.h"
7#include "lwip/stats.h"
8#include "lwip/prot/iana.h"
9
10#if !LWIP_STATS || !UDP_STATS || !MEMP_STATS || !ETHARP_STATS
11#error "This tests needs UDP-, MEMP- and ETHARP-statistics enabled"
12#endif
13#if !ETHARP_SUPPORT_STATIC_ENTRIES
14#error "This test needs ETHARP_SUPPORT_STATIC_ENTRIES enabled"
15#endif
16
17static struct netif test_netif;
18static ip4_addr_t test_ipaddr, test_netmask, test_gw;
19struct eth_addr test_ethaddr = {{1,1,1,1,1,1}};
20struct eth_addr test_ethaddr2 = {{1,1,1,1,1,2}};
21struct eth_addr test_ethaddr3 = {{1,1,1,1,1,3}};
22struct eth_addr test_ethaddr4 = {{1,1,1,1,1,4}};
23static int linkoutput_ctr;
24
25/* Helper functions */
26static void
28{
29 int i;
30 /* call etharp_tmr often enough to have all entries cleaned */
31 for(i = 0; i < 0xff; i++) {
32 etharp_tmr();
33 }
34}
35
36static err_t
38{
39 fail_unless(netif == &test_netif);
40 fail_unless(p != NULL);
42 return ERR_OK;
43}
44
45static err_t
47{
48 fail_unless(netif != NULL);
50 netif->output = etharp_output;
51 netif->mtu = 1500;
54 return ERR_OK;
55}
56
57static void
59{
60 IP4_ADDR(&test_gw, 192,168,0,1);
61 IP4_ADDR(&test_ipaddr, 192,168,0,1);
62 IP4_ADDR(&test_netmask, 255,255,0,0);
63
64 fail_unless(netif_default == NULL);
68}
69
70static void
72{
73 fail_unless(netif_default == &test_netif);
75}
76
77static void
78create_arp_response(ip4_addr_t *adr)
79{
80 int k;
81 struct eth_hdr *ethhdr;
82 struct etharp_hdr *etharphdr;
83 struct pbuf *p = pbuf_alloc(PBUF_RAW, sizeof(struct eth_hdr) + sizeof(struct etharp_hdr), PBUF_RAM);
84 if(p == NULL) {
85 FAIL_RET();
86 }
87 ethhdr = (struct eth_hdr*)p->payload;
88 etharphdr = (struct etharp_hdr*)(ethhdr + 1);
89
90 ethhdr->dest = test_ethaddr;
91 ethhdr->src = test_ethaddr2;
92 ethhdr->type = htons(ETHTYPE_ARP);
93
94 etharphdr->hwtype = htons(LWIP_IANA_HWTYPE_ETHERNET);
95 etharphdr->proto = htons(ETHTYPE_IP);
96 etharphdr->hwlen = ETHARP_HWADDR_LEN;
97 etharphdr->protolen = sizeof(ip4_addr_t);
98 etharphdr->opcode = htons(ARP_REPLY);
99
100 SMEMCPY(&etharphdr->sipaddr, adr, sizeof(ip4_addr_t));
101 SMEMCPY(&etharphdr->dipaddr, &test_ipaddr, sizeof(ip4_addr_t));
102
103 k = 6;
104 while(k > 0) {
105 k--;
106 /* Write the ARP MAC-Addresses */
107 etharphdr->shwaddr.addr[k] = test_ethaddr2.addr[k];
108 etharphdr->dhwaddr.addr[k] = test_ethaddr.addr[k];
109 /* Write the Ethernet MAC-Addresses */
110 ethhdr->dest.addr[k] = test_ethaddr.addr[k];
111 ethhdr->src.addr[k] = test_ethaddr2.addr[k];
112 }
113
114 ethernet_input(p, &test_netif);
115}
116
117/* Setups/teardown functions */
118
119static void
121{
124 lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
125}
126
127static void
129{
132 lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
133}
134
135
136/* Test functions */
137
138START_TEST(test_etharp_table)
139{
140#if ETHARP_SUPPORT_STATIC_ENTRIES
141 err_t err;
142#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
143 ssize_t idx;
144 const ip4_addr_t *unused_ipaddr;
145 struct eth_addr *unused_ethaddr;
146 struct udp_pcb* pcb;
147 LWIP_UNUSED_ARG(_i);
148
149 if (netif_default != &test_netif) {
150 fail("This test needs a default netif");
151 }
152
153 linkoutput_ctr = 0;
154
155 pcb = udp_new();
156 fail_unless(pcb != NULL);
157 if (pcb != NULL) {
158 ip4_addr_t adrs[ARP_TABLE_SIZE + 2];
159 int i;
160 for(i = 0; i < ARP_TABLE_SIZE + 2; i++) {
161 IP4_ADDR(&adrs[i], 192,168,0,i+2);
162 }
163 /* fill ARP-table with dynamic entries */
164 for(i = 0; i < ARP_TABLE_SIZE; i++) {
165 struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, 10, PBUF_RAM);
166 fail_unless(p != NULL);
167 if (p != NULL) {
168 err_t err2;
170 ip_addr_copy_from_ip4(dst, adrs[i]);
171 err2 = udp_sendto(pcb, p, &dst, 123);
172 fail_unless(err2 == ERR_OK);
173 /* etharp request sent? */
174 fail_unless(linkoutput_ctr == (2*i) + 1);
175 pbuf_free(p);
176
177 /* create an ARP response */
178 create_arp_response(&adrs[i]);
179 /* queued UDP packet sent? */
180 fail_unless(linkoutput_ctr == (2*i) + 2);
181
182 idx = etharp_find_addr(NULL, &adrs[i], &unused_ethaddr, &unused_ipaddr);
183 fail_unless(idx == i);
184 etharp_tmr();
185 }
186 }
187 linkoutput_ctr = 0;
188#if ETHARP_SUPPORT_STATIC_ENTRIES
189 /* create one static entry */
190 err = etharp_add_static_entry(&adrs[ARP_TABLE_SIZE], &test_ethaddr3);
191 fail_unless(err == ERR_OK);
192 idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
193 fail_unless(idx == 0);
194 fail_unless(linkoutput_ctr == 0);
195#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
196
197 linkoutput_ctr = 0;
198 /* fill ARP-table with dynamic entries */
199 for(i = 0; i < ARP_TABLE_SIZE; i++) {
200 struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, 10, PBUF_RAM);
201 fail_unless(p != NULL);
202 if (p != NULL) {
203 err_t err2;
205 ip_addr_copy_from_ip4(dst, adrs[i]);
206 err2 = udp_sendto(pcb, p, &dst, 123);
207 fail_unless(err2 == ERR_OK);
208 /* etharp request sent? */
209 fail_unless(linkoutput_ctr == (2*i) + 1);
210 pbuf_free(p);
211
212 /* create an ARP response */
213 create_arp_response(&adrs[i]);
214 /* queued UDP packet sent? */
215 fail_unless(linkoutput_ctr == (2*i) + 2);
216
217 idx = etharp_find_addr(NULL, &adrs[i], &unused_ethaddr, &unused_ipaddr);
218 if (i < ARP_TABLE_SIZE - 1) {
219 fail_unless(idx == i+1);
220 } else {
221 /* the last entry must not overwrite the static entry! */
222 fail_unless(idx == 1);
223 }
224 etharp_tmr();
225 }
226 }
227#if ETHARP_SUPPORT_STATIC_ENTRIES
228 /* create a second static entry */
229 err = etharp_add_static_entry(&adrs[ARP_TABLE_SIZE+1], &test_ethaddr4);
230 fail_unless(err == ERR_OK);
231 idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
232 fail_unless(idx == 0);
233 idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE+1], &unused_ethaddr, &unused_ipaddr);
234 fail_unless(idx == 2);
235 /* and remove it again */
236 err = etharp_remove_static_entry(&adrs[ARP_TABLE_SIZE+1]);
237 fail_unless(err == ERR_OK);
238 idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
239 fail_unless(idx == 0);
240 idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE+1], &unused_ethaddr, &unused_ipaddr);
241 fail_unless(idx == -1);
242#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
243
244 /* check that static entries don't time out */
246 idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
247 fail_unless(idx == 0);
248
249#if ETHARP_SUPPORT_STATIC_ENTRIES
250 /* remove the first static entry */
251 err = etharp_remove_static_entry(&adrs[ARP_TABLE_SIZE]);
252 fail_unless(err == ERR_OK);
253 idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
254 fail_unless(idx == -1);
255 idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE+1], &unused_ethaddr, &unused_ipaddr);
256 fail_unless(idx == -1);
257#endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
258
259 udp_remove(pcb);
260 }
261}
262END_TEST
263
264
266Suite *
268{
269 testfunc tests[] = {
270 TESTFUNC(test_etharp_table)
271 };
272 return create_suite("ETHARP", tests, sizeof(tests)/sizeof(testfunc), etharp_setup, etharp_teardown);
273}
#define START_TEST(x)
Definition: atltest.h:75
#define NULL
Definition: types.h:112
unsigned int idx
Definition: utils.c:41
GLenum GLenum dst
Definition: glext.h:6340
GLfloat GLfloat p
Definition: glext.h:8902
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:373
@ LWIP_IANA_HWTYPE_ETHERNET
Definition: iana.h:54
@ ETHTYPE_ARP
Definition: ieee.h:56
@ ETHTYPE_IP
Definition: ieee.h:54
s8_t err_t
Definition: err.h:96
@ ERR_OK
Definition: err.h:55
#define ARP_TABLE_SIZE
Definition: opt.h:629
#define SMEMCPY(dst, src, len)
Definition: opt.h:145
#define NETIF_FLAG_LINK_UP
Definition: netif.h:93
#define NETIF_FLAG_ETHARP
Definition: netif.h:97
#define NETIF_FLAG_BROADCAST
Definition: netif.h:87
void netif_remove(struct netif *netif)
Definition: netif.c:764
struct netif * netif_add(struct netif *netif, void *state, netif_init_fn init, netif_input_fn input)
Definition: netif.c:287
void netif_set_default(struct netif *netif)
Definition: netif.c:849
void netif_set_up(struct netif *netif)
Definition: netif.c:871
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:224
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:727
@ PBUF_RAM
Definition: pbuf.h:152
@ PBUF_RAW
Definition: pbuf.h:111
@ PBUF_TRANSPORT
Definition: pbuf.h:93
ip6_addr_t ip_addr_t
Definition: ip_addr.h:344
#define ETHARP_HWADDR_LEN
Definition: etharp.h:48
@ ARP_REPLY
Definition: etharp.h:107
Suite * create_suite(const char *name, testfunc *tests, size_t num_tests, SFun setup, SFun teardown)
#define FAIL_RET()
Definition: lwip_check.h:10
#define SKIP_POOL(x)
Definition: lwip_check.h:48
void lwip_check_ensure_no_alloc(unsigned int skip)
#define TESTFUNC(x)
Definition: lwip_check.h:22
#define htons(x)
Definition: module.h:215
static struct test_info tests[]
int k
Definition: mpi.c:3369
struct netif * netif_default
Definition: netif.c:115
#define err(...)
int ssize_t
Definition: rosdhcp.h:48
Definition: netif.h:269
u8_t flags
Definition: netif.h:354
u16_t mtu
Definition: netif.h:344
netif_linkoutput_fn linkoutput
Definition: netif.h:308
u8_t hwaddr_len
Definition: netif.h:352
Definition: pbuf.h:186
struct eth_addr test_ethaddr2
Definition: test_etharp.c:20
END_TEST Suite * etharp_suite(void)
Definition: test_etharp.c:267
static ip4_addr_t test_netmask
Definition: test_etharp.c:18
struct eth_addr test_ethaddr3
Definition: test_etharp.c:21
static void etharp_remove_all(void)
Definition: test_etharp.c:27
static err_t default_netif_linkoutput(struct netif *netif, struct pbuf *p)
Definition: test_etharp.c:37
static ip4_addr_t test_gw
Definition: test_etharp.c:18
static void etharp_setup(void)
Definition: test_etharp.c:120
static struct netif test_netif
Definition: test_etharp.c:17
static err_t default_netif_init(struct netif *netif)
Definition: test_etharp.c:46
struct eth_addr test_ethaddr
Definition: test_etharp.c:19
static void default_netif_add(void)
Definition: test_etharp.c:58
static void create_arp_response(ip4_addr_t *adr)
Definition: test_etharp.c:78
static ip4_addr_t test_ipaddr
Definition: test_etharp.c:18
static void default_netif_remove(void)
Definition: test_etharp.c:71
static int linkoutput_ctr
Definition: test_etharp.c:23
static void etharp_teardown(void)
Definition: test_etharp.c:128
struct eth_addr test_ethaddr4
Definition: test_etharp.c:22