1 #include "test_etharp.h"
2
3 #include "lwip/udp.h"
4 #include "netif/etharp.h"
5 #include "lwip/stats.h"
6
7 #if !LWIP_STATS || !UDP_STATS || !MEMP_STATS || !ETHARP_STATS
8 #error "This tests needs UDP-, MEMP- and ETHARP-statistics enabled"
9 #endif
10 #if !ETHARP_SUPPORT_STATIC_ENTRIES
11 #error "This test needs ETHARP_SUPPORT_STATIC_ENTRIES enabled"
12 #endif
13
14 static struct netif test_netif;
15 static ip_addr_t test_ipaddr, test_netmask, test_gw;
16 struct eth_addr test_ethaddr = {1,1,1,1,1,1};
17 struct eth_addr test_ethaddr2 = {1,1,1,1,1,2};
18 struct eth_addr test_ethaddr3 = {1,1,1,1,1,3};
19 struct eth_addr test_ethaddr4 = {1,1,1,1,1,4};
20 static int linkoutput_ctr;
21
22 /* Helper functions */
23 static void
etharp_remove_all(void)24 etharp_remove_all(void)
25 {
26 int i;
27 /* call etharp_tmr often enough to have all entries cleaned */
28 for(i = 0; i < 0xff; i++) {
29 etharp_tmr();
30 }
31 }
32
33 static err_t
default_netif_linkoutput(struct netif * netif,struct pbuf * p)34 default_netif_linkoutput(struct netif *netif, struct pbuf *p)
35 {
36 fail_unless(netif == &test_netif);
37 fail_unless(p != NULL);
38 linkoutput_ctr++;
39 return ERR_OK;
40 }
41
42 static err_t
default_netif_init(struct netif * netif)43 default_netif_init(struct netif *netif)
44 {
45 fail_unless(netif != NULL);
46 netif->linkoutput = default_netif_linkoutput;
47 netif->output = etharp_output;
48 netif->mtu = 1500;
49 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
50 netif->hwaddr_len = ETHARP_HWADDR_LEN;
51 return ERR_OK;
52 }
53
54 static void
default_netif_add(void)55 default_netif_add(void)
56 {
57 IP4_ADDR(&test_gw, 192,168,0,1);
58 IP4_ADDR(&test_ipaddr, 192,168,0,1);
59 IP4_ADDR(&test_netmask, 255,255,0,0);
60
61 fail_unless(netif_default == NULL);
62 netif_set_default(netif_add(&test_netif, &test_ipaddr, &test_netmask,
63 &test_gw, NULL, default_netif_init, NULL));
64 netif_set_up(&test_netif);
65 }
66
67 static void
default_netif_remove(void)68 default_netif_remove(void)
69 {
70 fail_unless(netif_default == &test_netif);
71 netif_remove(&test_netif);
72 }
73
74 static void
create_arp_response(ip_addr_t * adr)75 create_arp_response(ip_addr_t *adr)
76 {
77 int k;
78 struct eth_hdr *ethhdr;
79 struct etharp_hdr *etharphdr;
80 struct pbuf *p = pbuf_alloc(PBUF_RAW, sizeof(struct eth_hdr) + sizeof(struct etharp_hdr), PBUF_RAM);
81 if(p == NULL) {
82 FAIL_RET();
83 }
84 ethhdr = (struct eth_hdr*)p->payload;
85 etharphdr = (struct etharp_hdr*)(ethhdr + 1);
86
87 ethhdr->dest = test_ethaddr;
88 ethhdr->src = test_ethaddr2;
89 ethhdr->type = htons(ETHTYPE_ARP);
90
91 etharphdr->hwtype = htons(/*HWTYPE_ETHERNET*/ 1);
92 etharphdr->proto = htons(ETHTYPE_IP);
93 etharphdr->hwlen = ETHARP_HWADDR_LEN;
94 etharphdr->protolen = sizeof(ip_addr_t);
95 etharphdr->opcode = htons(ARP_REPLY);
96
97 SMEMCPY(ðarphdr->sipaddr, adr, sizeof(ip_addr_t));
98 SMEMCPY(ðarphdr->dipaddr, &test_ipaddr, sizeof(ip_addr_t));
99
100 k = 6;
101 while(k > 0) {
102 k--;
103 /* Write the ARP MAC-Addresses */
104 etharphdr->shwaddr.addr[k] = test_ethaddr2.addr[k];
105 etharphdr->dhwaddr.addr[k] = test_ethaddr.addr[k];
106 /* Write the Ethernet MAC-Addresses */
107 ethhdr->dest.addr[k] = test_ethaddr.addr[k];
108 ethhdr->src.addr[k] = test_ethaddr2.addr[k];
109 }
110
111 ethernet_input(p, &test_netif);
112 }
113
114 /* Setups/teardown functions */
115
116 static void
etharp_setup(void)117 etharp_setup(void)
118 {
119 etharp_remove_all();
120 default_netif_add();
121 }
122
123 static void
etharp_teardown(void)124 etharp_teardown(void)
125 {
126 etharp_remove_all();
127 default_netif_remove();
128 }
129
130
131 /* Test functions */
132
START_TEST(test_etharp_table)133 START_TEST(test_etharp_table)
134 {
135 #if ETHARP_SUPPORT_STATIC_ENTRIES
136 err_t err;
137 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
138 s8_t idx;
139 ip_addr_t *unused_ipaddr;
140 struct eth_addr *unused_ethaddr;
141 struct udp_pcb* pcb;
142 LWIP_UNUSED_ARG(_i);
143
144 if (netif_default != &test_netif) {
145 fail("This test needs a default netif");
146 }
147
148 linkoutput_ctr = 0;
149
150 pcb = udp_new();
151 fail_unless(pcb != NULL);
152 if (pcb != NULL) {
153 ip_addr_t adrs[ARP_TABLE_SIZE + 2];
154 int i;
155 for(i = 0; i < ARP_TABLE_SIZE + 2; i++) {
156 IP4_ADDR(&adrs[i], 192,168,0,i+2);
157 }
158 /* fill ARP-table with dynamic entries */
159 for(i = 0; i < ARP_TABLE_SIZE; i++) {
160 struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, 10, PBUF_RAM);
161 fail_unless(p != NULL);
162 if (p != NULL) {
163 err_t err = udp_sendto(pcb, p, &adrs[i], 123);
164 fail_unless(err == ERR_OK);
165 /* etharp request sent? */
166 fail_unless(linkoutput_ctr == (2*i) + 1);
167 pbuf_free(p);
168
169 /* create an ARP response */
170 create_arp_response(&adrs[i]);
171 /* queued UDP packet sent? */
172 fail_unless(linkoutput_ctr == (2*i) + 2);
173
174 idx = etharp_find_addr(NULL, &adrs[i], &unused_ethaddr, &unused_ipaddr);
175 fail_unless(idx == i);
176 etharp_tmr();
177 }
178 }
179 linkoutput_ctr = 0;
180 #if ETHARP_SUPPORT_STATIC_ENTRIES
181 /* create one static entry */
182 err = etharp_add_static_entry(&adrs[ARP_TABLE_SIZE], &test_ethaddr3);
183 fail_unless(err == ERR_OK);
184 idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
185 fail_unless(idx == 0);
186 fail_unless(linkoutput_ctr == 0);
187 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
188
189 linkoutput_ctr = 0;
190 /* fill ARP-table with dynamic entries */
191 for(i = 0; i < ARP_TABLE_SIZE; i++) {
192 struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, 10, PBUF_RAM);
193 fail_unless(p != NULL);
194 if (p != NULL) {
195 err_t err = udp_sendto(pcb, p, &adrs[i], 123);
196 fail_unless(err == ERR_OK);
197 /* etharp request sent? */
198 fail_unless(linkoutput_ctr == (2*i) + 1);
199 pbuf_free(p);
200
201 /* create an ARP response */
202 create_arp_response(&adrs[i]);
203 /* queued UDP packet sent? */
204 fail_unless(linkoutput_ctr == (2*i) + 2);
205
206 idx = etharp_find_addr(NULL, &adrs[i], &unused_ethaddr, &unused_ipaddr);
207 if (i < ARP_TABLE_SIZE - 1) {
208 fail_unless(idx == i+1);
209 } else {
210 /* the last entry must not overwrite the static entry! */
211 fail_unless(idx == 1);
212 }
213 etharp_tmr();
214 }
215 }
216 #if ETHARP_SUPPORT_STATIC_ENTRIES
217 /* create a second static entry */
218 err = etharp_add_static_entry(&adrs[ARP_TABLE_SIZE+1], &test_ethaddr4);
219 fail_unless(err == ERR_OK);
220 idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
221 fail_unless(idx == 0);
222 idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE+1], &unused_ethaddr, &unused_ipaddr);
223 fail_unless(idx == 2);
224 /* and remove it again */
225 err = etharp_remove_static_entry(&adrs[ARP_TABLE_SIZE+1]);
226 fail_unless(err == ERR_OK);
227 idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
228 fail_unless(idx == 0);
229 idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE+1], &unused_ethaddr, &unused_ipaddr);
230 fail_unless(idx == -1);
231 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
232
233 /* check that static entries don't time out */
234 etharp_remove_all();
235 idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
236 fail_unless(idx == 0);
237
238 #if ETHARP_SUPPORT_STATIC_ENTRIES
239 /* remove the first static entry */
240 err = etharp_remove_static_entry(&adrs[ARP_TABLE_SIZE]);
241 fail_unless(err == ERR_OK);
242 idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
243 fail_unless(idx == -1);
244 idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE+1], &unused_ethaddr, &unused_ipaddr);
245 fail_unless(idx == -1);
246 #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
247
248 udp_remove(pcb);
249 }
250 }
251 END_TEST
252
253
254 /** Create the suite including all tests for this module */
255 Suite *
etharp_suite(void)256 etharp_suite(void)
257 {
258 TFun tests[] = {
259 test_etharp_table
260 };
261 return create_suite("ETHARP", tests, sizeof(tests)/sizeof(TFun), etharp_setup, etharp_teardown);
262 }
263