BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 #include "test_pbuf.h"
2  
3 #include "lwip/pbuf.h"
4 #include "lwip/stats.h"
5  
6 #if !LWIP_STATS || !MEM_STATS ||!MEMP_STATS
7 #error "This tests needs MEM- and MEMP-statistics enabled"
8 #endif
9 #if LWIP_DNS
10 #error "This test needs DNS turned off (as it mallocs on init)"
11 #endif
12 #if !LWIP_TCP || !TCP_QUEUE_OOSEQ || !LWIP_WND_SCALE
13 #error "This test needs TCP OOSEQ queueing and window scaling enabled"
14 #endif
15  
16 /* Setups/teardown functions */
17  
18 static void
19 pbuf_setup(void)
20 {
21 lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
22 }
23  
24 static void
25 pbuf_teardown(void)
26 {
27 lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
28 }
29  
30  
31 #define TESTBUFSIZE_1 65535
32 #define TESTBUFSIZE_2 65530
33 #define TESTBUFSIZE_3 50050
34 static u8_t testbuf_1[TESTBUFSIZE_1];
35 static u8_t testbuf_1a[TESTBUFSIZE_1];
36 static u8_t testbuf_2[TESTBUFSIZE_2];
37 static u8_t testbuf_2a[TESTBUFSIZE_2];
38 static u8_t testbuf_3[TESTBUFSIZE_3];
39 static u8_t testbuf_3a[TESTBUFSIZE_3];
40  
41 /* Test functions */
42 START_TEST(test_pbuf_alloc_zero_pbufs)
43 {
44 struct pbuf *p;
45 LWIP_UNUSED_ARG(_i);
46  
47 p = pbuf_alloc(PBUF_RAW, 0, PBUF_ROM);
48 fail_unless(p != NULL);
49 if (p != NULL) {
50 pbuf_free(p);
51 }
52  
53 p = pbuf_alloc(PBUF_RAW, 0, PBUF_RAM);
54 fail_unless(p != NULL);
55 if (p != NULL) {
56 pbuf_free(p);
57 }
58  
59 p = pbuf_alloc(PBUF_RAW, 0, PBUF_REF);
60 fail_unless(p != NULL);
61 if (p != NULL) {
62 pbuf_free(p);
63 }
64  
65 p = pbuf_alloc(PBUF_RAW, 0, PBUF_POOL);
66 fail_unless(p != NULL);
67 if (p != NULL) {
68 pbuf_free(p);
69 }
70 }
71 END_TEST
72  
73 /** Call pbuf_copy on a pbuf with zero length */
74 START_TEST(test_pbuf_copy_zero_pbuf)
75 {
76 struct pbuf *p1, *p2, *p3;
77 err_t err;
78 LWIP_UNUSED_ARG(_i);
79  
80 fail_unless(lwip_stats.mem.used == 0);
81 fail_unless(MEMP_STATS_GET(used, MEMP_PBUF_POOL) == 0);
82  
83 p1 = pbuf_alloc(PBUF_RAW, 1024, PBUF_RAM);
84 fail_unless(p1 != NULL);
85 fail_unless(p1->ref == 1);
86  
87 p2 = pbuf_alloc(PBUF_RAW, 2, PBUF_POOL);
88 fail_unless(p2 != NULL);
89 fail_unless(p2->ref == 1);
90 p2->len = p2->tot_len = 0;
91  
92 pbuf_cat(p1, p2);
93 fail_unless(p1->ref == 1);
94 fail_unless(p2->ref == 1);
95  
96 p3 = pbuf_alloc(PBUF_RAW, p1->tot_len, PBUF_POOL);
97 err = pbuf_copy(p3, p1);
98 fail_unless(err == ERR_VAL);
99  
100 pbuf_free(p1);
101 pbuf_free(p3);
102 fail_unless(lwip_stats.mem.used == 0);
103  
104 fail_unless(lwip_stats.mem.used == 0);
105 fail_unless(MEMP_STATS_GET(used, MEMP_PBUF_POOL) == 0);
106 }
107 END_TEST
108  
109 START_TEST(test_pbuf_split_64k_on_small_pbufs)
110 {
111 struct pbuf *p, *rest=NULL;
112 LWIP_UNUSED_ARG(_i);
113  
114 p = pbuf_alloc(PBUF_RAW, 1, PBUF_POOL);
115 pbuf_split_64k(p, &rest);
116 fail_unless(p->tot_len == 1);
117 pbuf_free(p);
118 }
119 END_TEST
120  
121 START_TEST(test_pbuf_queueing_bigger_than_64k)
122 {
123 int i;
124 err_t err;
125 struct pbuf *p1, *p2, *p3, *rest2=NULL, *rest3=NULL;
126 LWIP_UNUSED_ARG(_i);
127  
128 for(i = 0; i < TESTBUFSIZE_1; i++) {
129 testbuf_1[i] = (u8_t)rand();
130 }
131 for(i = 0; i < TESTBUFSIZE_2; i++) {
132 testbuf_2[i] = (u8_t)rand();
133 }
134 for(i = 0; i < TESTBUFSIZE_3; i++) {
135 testbuf_3[i] = (u8_t)rand();
136 }
137  
138 p1 = pbuf_alloc(PBUF_RAW, TESTBUFSIZE_1, PBUF_POOL);
139 fail_unless(p1 != NULL);
140 p2 = pbuf_alloc(PBUF_RAW, TESTBUFSIZE_2, PBUF_POOL);
141 fail_unless(p2 != NULL);
142 p3 = pbuf_alloc(PBUF_RAW, TESTBUFSIZE_3, PBUF_POOL);
143 fail_unless(p3 != NULL);
144 err = pbuf_take(p1, testbuf_1, TESTBUFSIZE_1);
145 fail_unless(err == ERR_OK);
146 err = pbuf_take(p2, testbuf_2, TESTBUFSIZE_2);
147 fail_unless(err == ERR_OK);
148 err = pbuf_take(p3, testbuf_3, TESTBUFSIZE_3);
149 fail_unless(err == ERR_OK);
150  
151 pbuf_cat(p1, p2);
152 pbuf_cat(p1, p3);
153  
154 pbuf_split_64k(p1, &rest2);
155 fail_unless(p1->tot_len == TESTBUFSIZE_1);
156 fail_unless(rest2->tot_len == (u16_t)((TESTBUFSIZE_2+TESTBUFSIZE_3) & 0xFFFF));
157 pbuf_split_64k(rest2, &rest3);
158 fail_unless(rest2->tot_len == TESTBUFSIZE_2);
159 fail_unless(rest3->tot_len == TESTBUFSIZE_3);
160  
161 pbuf_copy_partial(p1, testbuf_1a, TESTBUFSIZE_1, 0);
162 pbuf_copy_partial(rest2, testbuf_2a, TESTBUFSIZE_2, 0);
163 pbuf_copy_partial(rest3, testbuf_3a, TESTBUFSIZE_3, 0);
164 for(i = 0; i < TESTBUFSIZE_1; i++)
165 fail_unless(testbuf_1[i] == testbuf_1a[i]);
166 for(i = 0; i < TESTBUFSIZE_2; i++)
167 fail_unless(testbuf_2[i] == testbuf_2a[i]);
168 for(i = 0; i < TESTBUFSIZE_3; i++)
169 fail_unless(testbuf_3[i] == testbuf_3a[i]);
170  
171 pbuf_free(p1);
172 pbuf_free(rest2);
173 pbuf_free(rest3);
174 }
175 END_TEST
176  
177 /* Test for bug that writing with pbuf_take_at() did nothing
178 * and returned ERR_OK when writing at beginning of a pbuf
179 * in the chain.
180 */
181 START_TEST(test_pbuf_take_at_edge)
182 {
183 err_t res;
184 u8_t *out;
185 int i;
186 u8_t testdata[] = { 0x01, 0x08, 0x82, 0x02 };
187 struct pbuf *p = pbuf_alloc(PBUF_RAW, 1024, PBUF_POOL);
188 struct pbuf *q = p->next;
189 LWIP_UNUSED_ARG(_i);
190 /* alloc big enough to get a chain of pbufs */
191 fail_if(p->tot_len == p->len);
192 memset(p->payload, 0, p->len);
193 memset(q->payload, 0, q->len);
194  
195 /* copy data to the beginning of first pbuf */
196 res = pbuf_take_at(p, &testdata, sizeof(testdata), 0);
197 fail_unless(res == ERR_OK);
198  
199 out = (u8_t*)p->payload;
200 for (i = 0; i < (int)sizeof(testdata); i++) {
201 fail_unless(out[i] == testdata[i],
202 "Bad data at pos %d, was %02X, expected %02X", i, out[i], testdata[i]);
203 }
204  
205 /* copy data to the just before end of first pbuf */
206 res = pbuf_take_at(p, &testdata, sizeof(testdata), p->len - 1);
207 fail_unless(res == ERR_OK);
208  
209 out = (u8_t*)p->payload;
210 fail_unless(out[p->len - 1] == testdata[0],
211 "Bad data at pos %d, was %02X, expected %02X", p->len - 1, out[p->len - 1], testdata[0]);
212 out = (u8_t*)q->payload;
213 for (i = 1; i < (int)sizeof(testdata); i++) {
214 fail_unless(out[i-1] == testdata[i],
215 "Bad data at pos %d, was %02X, expected %02X", p->len - 1 + i, out[i-1], testdata[i]);
216 }
217  
218 /* copy data to the beginning of second pbuf */
219 res = pbuf_take_at(p, &testdata, sizeof(testdata), p->len);
220 fail_unless(res == ERR_OK);
221  
222 out = (u8_t*)p->payload;
223 for (i = 0; i < (int)sizeof(testdata); i++) {
224 fail_unless(out[i] == testdata[i],
225 "Bad data at pos %d, was %02X, expected %02X", p->len+i, out[i], testdata[i]);
226 }
227 pbuf_free(p);
228 }
229 END_TEST
230  
231 /* Verify pbuf_put_at()/pbuf_get_at() when using
232 * offsets equal to beginning of new pbuf in chain
233 */
234 START_TEST(test_pbuf_get_put_at_edge)
235 {
236 u8_t *out;
237 u8_t testdata = 0x01;
238 u8_t getdata;
239 struct pbuf *p = pbuf_alloc(PBUF_RAW, 1024, PBUF_POOL);
240 struct pbuf *q = p->next;
241 LWIP_UNUSED_ARG(_i);
242 /* alloc big enough to get a chain of pbufs */
243 fail_if(p->tot_len == p->len);
244 memset(p->payload, 0, p->len);
245 memset(q->payload, 0, q->len);
246  
247 /* put byte at the beginning of second pbuf */
248 pbuf_put_at(p, p->len, testdata);
249  
250 out = (u8_t*)q->payload;
251 fail_unless(*out == testdata,
252 "Bad data at pos %d, was %02X, expected %02X", p->len, *out, testdata);
253  
254 getdata = pbuf_get_at(p, p->len);
255 fail_unless(*out == getdata,
256 "pbuf_get_at() returned bad data at pos %d, was %02X, expected %02X", p->len, getdata, *out);
257 pbuf_free(p);
258 }
259 END_TEST
260  
261 /** Create the suite including all tests for this module */
262 Suite *
263 pbuf_suite(void)
264 {
265 testfunc tests[] = {
266 TESTFUNC(test_pbuf_alloc_zero_pbufs),
267 TESTFUNC(test_pbuf_copy_zero_pbuf),
268 TESTFUNC(test_pbuf_split_64k_on_small_pbufs),
269 TESTFUNC(test_pbuf_queueing_bigger_than_64k),
270 TESTFUNC(test_pbuf_take_at_edge),
271 TESTFUNC(test_pbuf_get_put_at_edge)
272 };
273 return create_suite("PBUF", tests, sizeof(tests)/sizeof(testfunc), pbuf_setup, pbuf_teardown);
274 }