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
17 static struct netif test_netif;
18 static ip4_addr_t test_ipaddr, test_netmask, test_gw;
19 struct eth_addr test_ethaddr = {{1,1,1,1,1,1}};
20 struct eth_addr test_ethaddr2 = {{1,1,1,1,1,2}};
21 struct eth_addr test_ethaddr3 = {{1,1,1,1,1,3}};
22 struct eth_addr test_ethaddr4 = {{1,1,1,1,1,4}};
23 static int linkoutput_ctr;
24
25 /* Helper functions */
26 static void
etharp_remove_all(void)27 etharp_remove_all(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
36 static err_t
default_netif_linkoutput(struct netif * netif,struct pbuf * p)37 default_netif_linkoutput(struct netif *netif, struct pbuf *p)
38 {
39 fail_unless(netif == &test_netif);
40 fail_unless(p != NULL);
41 linkoutput_ctr++;
42 return ERR_OK;
43 }
44
45 static err_t
default_netif_init(struct netif * netif)46 default_netif_init(struct netif *netif)
47 {
48 fail_unless(netif != NULL);
49 netif->linkoutput = default_netif_linkoutput;
50 netif->output = etharp_output;
51 netif->mtu = 1500;
52 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
53 netif->hwaddr_len = ETHARP_HWADDR_LEN;
54 return ERR_OK;
55 }
56
57 static void
default_netif_add(void)58 default_netif_add(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);
65 netif_set_default(netif_add(&test_netif, &test_ipaddr, &test_netmask,
66 &test_gw, NULL, default_netif_init, NULL));
67 netif_set_up(&test_netif);
68 }
69
70 static void
default_netif_remove(void)71 default_netif_remove(void)
72 {
73 fail_unless(netif_default == &test_netif);
74 netif_remove(&test_netif);
75 }
76
77 static void
create_arp_response(ip4_addr_t * adr)78 create_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(ðarphdr->sipaddr, adr, sizeof(ip4_addr_t));
101 SMEMCPY(ðarphdr->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
119 static void
etharp_setup(void)120 etharp_setup(void)
121 {
122 etharp_remove_all();
123 default_netif_add();
124 lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
125 }
126
127 static void
etharp_teardown(void)128 etharp_teardown(void)
129 {
130 etharp_remove_all();
131 default_netif_remove();
132 lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
133 }
134
135
136 /* Test functions */
137
START_TEST(test_etharp_table)138 START_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;
169 ip_addr_t dst;
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;
204 ip_addr_t dst;
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 */
245 etharp_remove_all();
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 }
262 END_TEST
263
264
265 /** Create the suite including all tests for this module */
266 Suite *
etharp_suite(void)267 etharp_suite(void)
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 }
274