ReactOS 0.4.16-dev-257-g6aa11ac
test_pbuf.c
Go to the documentation of this file.
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_TCP || !TCP_QUEUE_OOSEQ || !LWIP_WND_SCALE
10#error "This test needs TCP OOSEQ queueing and window scaling enabled"
11#endif
12
13/* Setups/teardown functions */
14
15static void
17{
18 lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
19}
20
21static void
23{
24 lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
25}
26
27
28#define TESTBUFSIZE_1 65535
29#define TESTBUFSIZE_2 65530
30#define TESTBUFSIZE_3 50050
37
38/* Test functions */
39START_TEST(test_pbuf_alloc_zero_pbufs)
40{
41 struct pbuf *p;
43
45 fail_unless(p != NULL);
46 if (p != NULL) {
47 pbuf_free(p);
48 }
49
51 fail_unless(p != NULL);
52 if (p != NULL) {
53 pbuf_free(p);
54 }
55
57 fail_unless(p != NULL);
58 if (p != NULL) {
59 pbuf_free(p);
60 }
61
63 fail_unless(p != NULL);
64 if (p != NULL) {
65 pbuf_free(p);
66 }
67}
68END_TEST
69
71START_TEST(test_pbuf_copy_zero_pbuf)
72{
73 struct pbuf *p1, *p2, *p3;
74 err_t err;
76
77 p1 = pbuf_alloc(PBUF_RAW, 1024, PBUF_RAM);
78 fail_unless(p1 != NULL);
79 fail_unless(p1->ref == 1);
80
82 fail_unless(p2 != NULL);
83 fail_unless(p2->ref == 1);
84 p2->len = p2->tot_len = 0;
85
86 pbuf_cat(p1, p2);
87 fail_unless(p1->ref == 1);
88 fail_unless(p2->ref == 1);
89
91 err = pbuf_copy(p3, p1);
92 fail_unless(err == ERR_VAL);
93
94 pbuf_free(p1);
95 pbuf_free(p3);
96}
97END_TEST
98
100START_TEST(test_pbuf_copy_unmatched_chains)
101{
102 uint16_t i, j;
103 err_t err;
104 struct pbuf *source, *dest, *p;
105 LWIP_UNUSED_ARG(_i);
106
107 source = NULL;
108 /* Build source pbuf from linked 16 byte parts,
109 * with payload bytes containing their offset */
110 for (i = 0; i < 8; i++) {
112 fail_unless(p != NULL);
113 for (j = 0; j < p->len; j++) {
114 ((u8_t*)p->payload)[j] = (u8_t)((i << 4) | j);
115 }
116 if (source) {
117 pbuf_cat(source, p);
118 } else {
119 source = p;
120 }
121 }
122 for (i = 0; i < source->tot_len; i++) {
123 fail_unless(pbuf_get_at(source, i) == i);
124 }
125
126 /* Build dest pbuf from other lengths */
128 fail_unless(dest != NULL);
130 fail_unless(p != NULL);
131 pbuf_cat(dest, p);
133 fail_unless(p != NULL);
134 pbuf_cat(dest, p);
135
136 /* Copy contents and verify data */
138 fail_unless(err == ERR_OK);
139 for (i = 0; i < source->tot_len; i++) {
140 fail_unless(pbuf_get_at(dest, i) == i);
141 }
142
145}
146END_TEST
147
148START_TEST(test_pbuf_copy_partial_pbuf)
149{
150 struct pbuf *a, *b, *dest;
151 char lwip[] = "lwip ";
152 char packet[] = "packet";
153 err_t err;
154 LWIP_UNUSED_ARG(_i);
155
157 fail_unless(a != NULL);
158 a->payload = lwip;
160 fail_unless(b != NULL);
161 b->payload = packet;
162 pbuf_cat(a, b);
164 memset(dest->payload, 0, dest->len);
165 fail_unless(dest != NULL);
166
167 /* Don't copy if data will not fit */
168 err = pbuf_copy_partial_pbuf(dest, a, a->tot_len, 4);
169 fail_unless(err == ERR_ARG);
170 /* Don't copy if length is longer than source */
171 err = pbuf_copy_partial_pbuf(dest, a, a->tot_len + 1, 0);
172 fail_unless(err == ERR_ARG);
173 /* Normal copy */
174 err = pbuf_copy_partial_pbuf(dest, a, a->tot_len, 0);
175 fail_unless(err == ERR_OK);
176 fail_unless(strcmp("lwip packet", (char*)dest->payload) == 0);
177 /* Copy at offset */
178 err = pbuf_copy_partial_pbuf(dest, a, a->tot_len, 1);
179 fail_unless(err == ERR_OK);
180 fail_unless(strcmp("llwip packet", (char*)dest->payload) == 0);
181 /* Copy at offset with shorter length */
183 fail_unless(err == ERR_OK);
184 fail_unless(strcmp("llwip lwip p", (char*)dest->payload) == 0);
185 /* Copy with shorter length */
187 fail_unless(err == ERR_OK);
188 fail_unless(strcmp("lwip lwip p", (char*)dest->payload) == 0);
189
191 pbuf_free(a);
192}
193END_TEST
194
195START_TEST(test_pbuf_split_64k_on_small_pbufs)
196{
197 struct pbuf *p, *rest=NULL;
198 LWIP_UNUSED_ARG(_i);
199
201 pbuf_split_64k(p, &rest);
202 fail_unless(p->tot_len == 1);
203 pbuf_free(p);
204}
205END_TEST
206
207START_TEST(test_pbuf_queueing_bigger_than_64k)
208{
209 int i;
210 err_t err;
211 struct pbuf *p1, *p2, *p3, *rest2=NULL, *rest3=NULL;
212 LWIP_UNUSED_ARG(_i);
213
214 for(i = 0; i < TESTBUFSIZE_1; i++) {
215 testbuf_1[i] = (u8_t)rand();
216 }
217 for(i = 0; i < TESTBUFSIZE_2; i++) {
218 testbuf_2[i] = (u8_t)rand();
219 }
220 for(i = 0; i < TESTBUFSIZE_3; i++) {
221 testbuf_3[i] = (u8_t)rand();
222 }
223
225 fail_unless(p1 != NULL);
227 fail_unless(p2 != NULL);
229 fail_unless(p3 != NULL);
231 fail_unless(err == ERR_OK);
233 fail_unless(err == ERR_OK);
235 fail_unless(err == ERR_OK);
236
237 pbuf_cat(p1, p2);
238 pbuf_cat(p1, p3);
239
240 pbuf_split_64k(p1, &rest2);
241 fail_unless(p1->tot_len == TESTBUFSIZE_1);
242 fail_unless(rest2->tot_len == (u16_t)((TESTBUFSIZE_2+TESTBUFSIZE_3) & 0xFFFF));
243 pbuf_split_64k(rest2, &rest3);
244 fail_unless(rest2->tot_len == TESTBUFSIZE_2);
245 fail_unless(rest3->tot_len == TESTBUFSIZE_3);
246
253
254 pbuf_free(p1);
255 pbuf_free(rest2);
256 pbuf_free(rest3);
257}
258END_TEST
259
260/* Test for bug that writing with pbuf_take_at() did nothing
261 * and returned ERR_OK when writing at beginning of a pbuf
262 * in the chain.
263 */
264START_TEST(test_pbuf_take_at_edge)
265{
266 err_t res;
267 u8_t *out;
268 int i;
269 u8_t testdata[] = { 0x01, 0x08, 0x82, 0x02 };
270 struct pbuf *p = pbuf_alloc(PBUF_RAW, 1024, PBUF_POOL);
271 struct pbuf *q = p->next;
272 LWIP_UNUSED_ARG(_i);
273 /* alloc big enough to get a chain of pbufs */
274 fail_if(p->tot_len == p->len);
275 memset(p->payload, 0, p->len);
276 memset(q->payload, 0, q->len);
277
278 /* copy data to the beginning of first pbuf */
279 res = pbuf_take_at(p, &testdata, sizeof(testdata), 0);
280 fail_unless(res == ERR_OK);
281
282 out = (u8_t*)p->payload;
283 for (i = 0; i < (int)sizeof(testdata); i++) {
284 fail_unless(out[i] == testdata[i],
285 "Bad data at pos %d, was %02X, expected %02X", i, out[i], testdata[i]);
286 }
287
288 /* copy data to the just before end of first pbuf */
289 res = pbuf_take_at(p, &testdata, sizeof(testdata), p->len - 1);
290 fail_unless(res == ERR_OK);
291
292 out = (u8_t*)p->payload;
293 fail_unless(out[p->len - 1] == testdata[0],
294 "Bad data at pos %d, was %02X, expected %02X", p->len - 1, out[p->len - 1], testdata[0]);
295 out = (u8_t*)q->payload;
296 for (i = 1; i < (int)sizeof(testdata); i++) {
297 fail_unless(out[i-1] == testdata[i],
298 "Bad data at pos %d, was %02X, expected %02X", p->len - 1 + i, out[i-1], testdata[i]);
299 }
300
301 /* copy data to the beginning of second pbuf */
302 res = pbuf_take_at(p, &testdata, sizeof(testdata), p->len);
303 fail_unless(res == ERR_OK);
304
305 out = (u8_t*)p->payload;
306 for (i = 0; i < (int)sizeof(testdata); i++) {
307 fail_unless(out[i] == testdata[i],
308 "Bad data at pos %d, was %02X, expected %02X", p->len+i, out[i], testdata[i]);
309 }
310 pbuf_free(p);
311}
312END_TEST
313
314/* Verify pbuf_put_at()/pbuf_get_at() when using
315 * offsets equal to beginning of new pbuf in chain
316 */
317START_TEST(test_pbuf_get_put_at_edge)
318{
319 u8_t *out;
320 u8_t testdata = 0x01;
321 u8_t getdata;
322 struct pbuf *p = pbuf_alloc(PBUF_RAW, 1024, PBUF_POOL);
323 struct pbuf *q = p->next;
324 LWIP_UNUSED_ARG(_i);
325 /* alloc big enough to get a chain of pbufs */
326 fail_if(p->tot_len == p->len);
327 memset(p->payload, 0, p->len);
328 memset(q->payload, 0, q->len);
329
330 /* put byte at the beginning of second pbuf */
331 pbuf_put_at(p, p->len, testdata);
332
333 out = (u8_t*)q->payload;
334 fail_unless(*out == testdata,
335 "Bad data at pos %d, was %02X, expected %02X", p->len, *out, testdata);
336
337 getdata = pbuf_get_at(p, p->len);
338 fail_unless(*out == getdata,
339 "pbuf_get_at() returned bad data at pos %d, was %02X, expected %02X", p->len, getdata, *out);
340 pbuf_free(p);
341}
342END_TEST
343
345Suite *
347{
348 testfunc tests[] = {
349 TESTFUNC(test_pbuf_alloc_zero_pbufs),
350 TESTFUNC(test_pbuf_copy_zero_pbuf),
351 TESTFUNC(test_pbuf_copy_unmatched_chains),
352 TESTFUNC(test_pbuf_copy_partial_pbuf),
353 TESTFUNC(test_pbuf_split_64k_on_small_pbufs),
354 TESTFUNC(test_pbuf_queueing_bigger_than_64k),
355 TESTFUNC(test_pbuf_take_at_edge),
356 TESTFUNC(test_pbuf_get_put_at_edge)
357 };
358 return create_suite("PBUF", tests, sizeof(tests)/sizeof(testfunc), pbuf_setup, pbuf_teardown);
359}
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
unsigned short int uint16_t
Definition: acefiex.h:54
#define START_TEST(x)
Definition: atltest.h:75
#define NULL
Definition: types.h:112
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
GLuint res
Definition: glext.h:9613
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLfloat GLfloat p
Definition: glext.h:8902
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
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
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 GLint GLint j
Definition: glfuncs.h:250
uint8_t u8_t
Definition: arch.h:125
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:373
uint16_t u16_t
Definition: arch.h:127
s8_t err_t
Definition: err.h:96
@ ERR_OK
Definition: err.h:55
@ ERR_VAL
Definition: err.h:67
@ ERR_ARG
Definition: err.h:88
u8_t pbuf_get_at(const struct pbuf *p, u16_t offset)
Definition: pbuf.c:1402
void pbuf_cat(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:855
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:224
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:727
err_t pbuf_copy(struct pbuf *p_to, const struct pbuf *p_from)
Definition: pbuf.c:959
u16_t pbuf_copy_partial(const struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
Definition: pbuf.c:1058
err_t pbuf_take(struct pbuf *buf, const void *dataptr, u16_t len)
Definition: pbuf.c:1227
err_t pbuf_copy_partial_pbuf(struct pbuf *p_to, const struct pbuf *p_from, u16_t copy_len, u16_t offset)
Definition: pbuf.c:986
err_t pbuf_take_at(struct pbuf *buf, const void *dataptr, u16_t len, u16_t offset)
Definition: pbuf.c:1271
void pbuf_put_at(struct pbuf *p, u16_t offset, u8_t data)
Definition: pbuf.c:1442
@ PBUF_ROM
Definition: pbuf.h:156
@ PBUF_RAM
Definition: pbuf.h:152
@ PBUF_REF
Definition: pbuf.h:160
@ PBUF_POOL
Definition: pbuf.h:167
@ PBUF_RAW
Definition: pbuf.h:111
_Check_return_ int __cdecl rand(void)
Definition: rand.c:10
#define a
Definition: ke_i.h:78
#define b
Definition: ke_i.h:79
Suite * create_suite(const char *name, testfunc *tests, size_t num_tests, SFun setup, SFun teardown)
#define SKIP_POOL(x)
Definition: lwip_check.h:48
void lwip_check_ensure_no_alloc(unsigned int skip)
#define TESTFUNC(x)
Definition: lwip_check.h:22
#define for
Definition: utility.h:88
static struct test_info tests[]
static char * dest
Definition: rtl.c:135
#define err(...)
static FILE * out
Definition: regtests2xml.c:44
#define memset(x, y, z)
Definition: compat.h:39
Definition: dhcpd.h:135
Definition: pbuf.h:186
LWIP_PBUF_REF_T ref
Definition: pbuf.h:218
u16_t tot_len
Definition: pbuf.h:200
struct pbuf * next
Definition: pbuf.h:188
u16_t len
Definition: pbuf.h:203
static void pbuf_setup(void)
Definition: test_pbuf.c:16
static u8_t testbuf_1a[TESTBUFSIZE_1]
Definition: test_pbuf.c:32
#define TESTBUFSIZE_3
Definition: test_pbuf.c:30
static u8_t testbuf_2[TESTBUFSIZE_2]
Definition: test_pbuf.c:33
#define TESTBUFSIZE_2
Definition: test_pbuf.c:29
static u8_t testbuf_1[TESTBUFSIZE_1]
Definition: test_pbuf.c:31
static u8_t testbuf_3a[TESTBUFSIZE_3]
Definition: test_pbuf.c:36
static u8_t testbuf_3[TESTBUFSIZE_3]
Definition: test_pbuf.c:35
#define TESTBUFSIZE_1
Definition: test_pbuf.c:28
static u8_t testbuf_2a[TESTBUFSIZE_2]
Definition: test_pbuf.c:34
END_TEST Suite * pbuf_suite(void)
Definition: test_pbuf.c:346
static void pbuf_teardown(void)
Definition: test_pbuf.c:22