1 #include "test_ip4.h"
2
3 #include "lwip/ip4.h"
4 #include "lwip/inet_chksum.h"
5 #include "lwip/stats.h"
6 #include "lwip/prot/ip.h"
7 #include "lwip/prot/ip4.h"
8
9 #include "lwip/tcpip.h"
10
11 #if !LWIP_IPV4 || !IP_REASSEMBLY || !MIB2_STATS || !IPFRAG_STATS
12 #error "This tests needs LWIP_IPV4, IP_REASSEMBLY; MIB2- and IPFRAG-statistics enabled"
13 #endif
14
15 /* Helper functions */
16 static void
create_ip4_input_fragment(u16_t ip_id,u16_t start,u16_t len,int last)17 create_ip4_input_fragment(u16_t ip_id, u16_t start, u16_t len, int last)
18 {
19 struct pbuf *p;
20 struct netif *input_netif = netif_list; /* just use any netif */
21 fail_unless((start & 7) == 0);
22 fail_unless(((len & 7) == 0) || last);
23 fail_unless(input_netif != NULL);
24
25 p = pbuf_alloc(PBUF_RAW, len + sizeof(struct ip_hdr), PBUF_RAM);
26 fail_unless(p != NULL);
27 if (p != NULL) {
28 err_t err;
29 struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
30 IPH_VHL_SET(iphdr, 4, sizeof(struct ip_hdr) / 4);
31 IPH_TOS_SET(iphdr, 0);
32 IPH_LEN_SET(iphdr, lwip_htons(p->tot_len));
33 IPH_ID_SET(iphdr, lwip_htons(ip_id));
34 if (last) {
35 IPH_OFFSET_SET(iphdr, lwip_htons(start / 8));
36 } else {
37 IPH_OFFSET_SET(iphdr, lwip_htons((start / 8) | IP_MF));
38 }
39 IPH_TTL_SET(iphdr, 5);
40 IPH_PROTO_SET(iphdr, IP_PROTO_UDP);
41 IPH_CHKSUM_SET(iphdr, 0);
42 ip4_addr_copy(iphdr->src, *netif_ip4_addr(input_netif));
43 iphdr->src.addr = lwip_htonl(lwip_htonl(iphdr->src.addr) + 1);
44 ip4_addr_copy(iphdr->dest, *netif_ip4_addr(input_netif));
45 IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, sizeof(struct ip_hdr)));
46
47 err = ip4_input(p, input_netif);
48 if (err != ERR_OK) {
49 pbuf_free(p);
50 }
51 fail_unless(err == ERR_OK);
52 }
53 }
54
55 /* Setups/teardown functions */
56
57 static void
ip4_setup(void)58 ip4_setup(void)
59 {
60 lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
61 }
62
63 static void
ip4_teardown(void)64 ip4_teardown(void)
65 {
66 if (netif_list->loop_first != NULL) {
67 pbuf_free(netif_list->loop_first);
68 netif_list->loop_first = NULL;
69 }
70 netif_list->loop_last = NULL;
71 /* poll until all memory is released... */
72 tcpip_thread_poll_one();
73 lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
74 }
75
76
77 /* Test functions */
78
START_TEST(test_ip4_reass)79 START_TEST(test_ip4_reass)
80 {
81 const u16_t ip_id = 128;
82 LWIP_UNUSED_ARG(_i);
83
84 memset(&lwip_stats.mib2, 0, sizeof(lwip_stats.mib2));
85
86 create_ip4_input_fragment(ip_id, 8*200, 200, 1);
87 fail_unless(lwip_stats.ip_frag.recv == 1);
88 fail_unless(lwip_stats.ip_frag.err == 0);
89 fail_unless(lwip_stats.ip_frag.memerr == 0);
90 fail_unless(lwip_stats.ip_frag.drop == 0);
91 fail_unless(lwip_stats.mib2.ipreasmoks == 0);
92
93 create_ip4_input_fragment(ip_id, 0*200, 200, 0);
94 fail_unless(lwip_stats.ip_frag.recv == 2);
95 fail_unless(lwip_stats.ip_frag.err == 0);
96 fail_unless(lwip_stats.ip_frag.memerr == 0);
97 fail_unless(lwip_stats.ip_frag.drop == 0);
98 fail_unless(lwip_stats.mib2.ipreasmoks == 0);
99
100 create_ip4_input_fragment(ip_id, 1*200, 200, 0);
101 fail_unless(lwip_stats.ip_frag.recv == 3);
102 fail_unless(lwip_stats.ip_frag.err == 0);
103 fail_unless(lwip_stats.ip_frag.memerr == 0);
104 fail_unless(lwip_stats.ip_frag.drop == 0);
105 fail_unless(lwip_stats.mib2.ipreasmoks == 0);
106
107 create_ip4_input_fragment(ip_id, 2*200, 200, 0);
108 fail_unless(lwip_stats.ip_frag.recv == 4);
109 fail_unless(lwip_stats.ip_frag.err == 0);
110 fail_unless(lwip_stats.ip_frag.memerr == 0);
111 fail_unless(lwip_stats.ip_frag.drop == 0);
112 fail_unless(lwip_stats.mib2.ipreasmoks == 0);
113
114 create_ip4_input_fragment(ip_id, 3*200, 200, 0);
115 fail_unless(lwip_stats.ip_frag.recv == 5);
116 fail_unless(lwip_stats.ip_frag.err == 0);
117 fail_unless(lwip_stats.ip_frag.memerr == 0);
118 fail_unless(lwip_stats.ip_frag.drop == 0);
119 fail_unless(lwip_stats.mib2.ipreasmoks == 0);
120
121 create_ip4_input_fragment(ip_id, 4*200, 200, 0);
122 fail_unless(lwip_stats.ip_frag.recv == 6);
123 fail_unless(lwip_stats.ip_frag.err == 0);
124 fail_unless(lwip_stats.ip_frag.memerr == 0);
125 fail_unless(lwip_stats.ip_frag.drop == 0);
126 fail_unless(lwip_stats.mib2.ipreasmoks == 0);
127
128 create_ip4_input_fragment(ip_id, 7*200, 200, 0);
129 fail_unless(lwip_stats.ip_frag.recv == 7);
130 fail_unless(lwip_stats.ip_frag.err == 0);
131 fail_unless(lwip_stats.ip_frag.memerr == 0);
132 fail_unless(lwip_stats.ip_frag.drop == 0);
133 fail_unless(lwip_stats.mib2.ipreasmoks == 0);
134
135 create_ip4_input_fragment(ip_id, 6*200, 200, 0);
136 fail_unless(lwip_stats.ip_frag.recv == 8);
137 fail_unless(lwip_stats.ip_frag.err == 0);
138 fail_unless(lwip_stats.ip_frag.memerr == 0);
139 fail_unless(lwip_stats.ip_frag.drop == 0);
140 fail_unless(lwip_stats.mib2.ipreasmoks == 0);
141
142 create_ip4_input_fragment(ip_id, 5*200, 200, 0);
143 fail_unless(lwip_stats.ip_frag.recv == 9);
144 fail_unless(lwip_stats.ip_frag.err == 0);
145 fail_unless(lwip_stats.ip_frag.memerr == 0);
146 fail_unless(lwip_stats.ip_frag.drop == 0);
147 fail_unless(lwip_stats.mib2.ipreasmoks == 1);
148 }
149 END_TEST
150
151
152 /** Create the suite including all tests for this module */
153 Suite *
ip4_suite(void)154 ip4_suite(void)
155 {
156 testfunc tests[] = {
157 TESTFUNC(test_ip4_reass),
158 };
159 return create_suite("IPv4", tests, sizeof(tests)/sizeof(testfunc), ip4_setup, ip4_teardown);
160 }
161