ReactOS 0.4.15-dev-7907-g95bf896
test_etharp.c
Go to the documentation of this file.
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
14static struct netif test_netif;
16struct eth_addr test_ethaddr = {1,1,1,1,1,1};
17struct eth_addr test_ethaddr2 = {1,1,1,1,1,2};
18struct eth_addr test_ethaddr3 = {1,1,1,1,1,3};
19struct eth_addr test_ethaddr4 = {1,1,1,1,1,4};
20static int linkoutput_ctr;
21
22/* Helper functions */
23static 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
33static err_t
35{
36 fail_unless(netif == &test_netif);
37 fail_unless(p != NULL);
39 return ERR_OK;
40}
41
42static err_t
44{
45 fail_unless(netif != NULL);
47 netif->output = etharp_output;
48 netif->mtu = 1500;
50 netif->hwaddr_len = ETHARP_HWADDR_LEN;
51 return ERR_OK;
52}
53
54static 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);
65}
66
67static void
69{
70 fail_unless(netif_default == &test_netif);
72}
73
74static void
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(&etharphdr->sipaddr, adr, sizeof(ip_addr_t));
98 SMEMCPY(&etharphdr->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
116static void
118{
121}
122
123static void
125{
128}
129
130
131/* Test functions */
132
133START_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 */
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}
251END_TEST
252
253
255Suite *
257{
258 TFun tests[] = {
259 test_etharp_table
260 };
261 return create_suite("ETHARP", tests, sizeof(tests)/sizeof(TFun), etharp_setup, etharp_teardown);
262}
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:73
#define START_TEST(x)
Definition: atltest.h:75
#define NULL
Definition: types.h:112
unsigned int idx
Definition: utils.c:41
signed char s8_t
Definition: cc.h:28
#define ERR_OK
Definition: err.h:52
s8_t err_t
Definition: err.h:47
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 IP4_ADDR(ipaddr, a, b, c, d)
Definition: ip_addr.h:139
typedefPACK_STRUCT_END struct ip_addr ip_addr_t
Definition: ip_addr.h:64
static Suite * create_suite(const char *name, TFun *tests, size_t num_tests, SFun setup, SFun teardown)
Definition: lwip_check.h:20
#define FAIL_RET()
Definition: lwip_check.h:10
#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:76
struct netif * netif_add(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input)
Definition: netif.c:139
void netif_remove(struct netif *netif)
Definition: netif.c:235
void netif_set_default(struct netif *netif)
Definition: netif.c:430
void netif_set_up(struct netif *netif)
Definition: netif.c:453
#define NETIF_FLAG_LINK_UP
Definition: netif.h:84
#define NETIF_FLAG_ETHARP
Definition: netif.h:88
#define NETIF_FLAG_BROADCAST
Definition: netif.h:72
#define SMEMCPY(dst, src, len)
Definition: opt.h:92
#define ARP_TABLE_SIZE
Definition: opt.h:433
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:207
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:618
@ PBUF_RAM
Definition: pbuf.h:58
@ PBUF_RAW
Definition: pbuf.h:54
@ PBUF_TRANSPORT
Definition: pbuf.h:51
#define err(...)
Definition: netif.h:136
u8_t flags
Definition: netif.h:192
netif_output_fn output
Definition: netif.h:151
u16_t mtu
Definition: netif.h:186
netif_linkoutput_fn linkoutput
Definition: netif.h:155
u8_t hwaddr_len
Definition: netif.h:188
Definition: pbuf.h:79
struct eth_addr test_ethaddr2
Definition: test_etharp.c:17
END_TEST Suite * etharp_suite(void)
Definition: test_etharp.c:256
struct eth_addr test_ethaddr3
Definition: test_etharp.c:18
static void etharp_remove_all(void)
Definition: test_etharp.c:24
static err_t default_netif_linkoutput(struct netif *netif, struct pbuf *p)
Definition: test_etharp.c:34
static void etharp_setup(void)
Definition: test_etharp.c:117
static ip_addr_t test_netmask
Definition: test_etharp.c:15
static struct netif test_netif
Definition: test_etharp.c:14
static err_t default_netif_init(struct netif *netif)
Definition: test_etharp.c:43
static ip_addr_t test_ipaddr
Definition: test_etharp.c:15
static ip_addr_t test_gw
Definition: test_etharp.c:15
struct eth_addr test_ethaddr
Definition: test_etharp.c:16
static void default_netif_add(void)
Definition: test_etharp.c:55
static void default_netif_remove(void)
Definition: test_etharp.c:68
static int linkoutput_ctr
Definition: test_etharp.c:20
static void etharp_teardown(void)
Definition: test_etharp.c:124
static void create_arp_response(ip_addr_t *adr)
Definition: test_etharp.c:75
struct eth_addr test_ethaddr4
Definition: test_etharp.c:19