packettest.c revision 1.1 1 1.1 christos /*
2 1.1 christos * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
3 1.1 christos *
4 1.1 christos * Licensed under the OpenSSL license (the "License"). You may not use
5 1.1 christos * this file except in compliance with the License. You can obtain a copy
6 1.1 christos * in the file LICENSE in the source distribution or at
7 1.1 christos * https://www.openssl.org/source/license.html
8 1.1 christos */
9 1.1 christos
10 1.1 christos #include "../ssl/packet_locl.h"
11 1.1 christos
12 1.1 christos #define BUF_LEN 255
13 1.1 christos
14 1.1 christos static int test_PACKET_remaining(unsigned char buf[BUF_LEN])
15 1.1 christos {
16 1.1 christos PACKET pkt;
17 1.1 christos
18 1.1 christos if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
19 1.1 christos || PACKET_remaining(&pkt) != BUF_LEN
20 1.1 christos || !PACKET_forward(&pkt, BUF_LEN - 1)
21 1.1 christos || PACKET_remaining(&pkt) != 1
22 1.1 christos || !PACKET_forward(&pkt, 1)
23 1.1 christos || PACKET_remaining(&pkt) != 0) {
24 1.1 christos fprintf(stderr, "test_PACKET_remaining() failed\n");
25 1.1 christos return 0;
26 1.1 christos }
27 1.1 christos
28 1.1 christos return 1;
29 1.1 christos }
30 1.1 christos
31 1.1 christos static int test_PACKET_end(unsigned char buf[BUF_LEN])
32 1.1 christos {
33 1.1 christos PACKET pkt;
34 1.1 christos
35 1.1 christos if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
36 1.1 christos || PACKET_remaining(&pkt) != BUF_LEN
37 1.1 christos || PACKET_end(&pkt) != buf + BUF_LEN
38 1.1 christos || !PACKET_forward(&pkt, BUF_LEN - 1)
39 1.1 christos || PACKET_end(&pkt) != buf + BUF_LEN
40 1.1 christos || !PACKET_forward(&pkt, 1)
41 1.1 christos || PACKET_end(&pkt) != buf + BUF_LEN) {
42 1.1 christos fprintf(stderr, "test_PACKET_end() failed\n");
43 1.1 christos return 0;
44 1.1 christos }
45 1.1 christos
46 1.1 christos return 1;
47 1.1 christos }
48 1.1 christos
49 1.1 christos static int test_PACKET_get_1(unsigned char buf[BUF_LEN])
50 1.1 christos {
51 1.1 christos unsigned int i;
52 1.1 christos PACKET pkt;
53 1.1 christos
54 1.1 christos if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
55 1.1 christos || !PACKET_get_1(&pkt, &i)
56 1.1 christos || i != 0x02
57 1.1 christos || !PACKET_forward(&pkt, BUF_LEN - 2)
58 1.1 christos || !PACKET_get_1(&pkt, &i)
59 1.1 christos || i != 0xfe
60 1.1 christos || PACKET_get_1(&pkt, &i)) {
61 1.1 christos fprintf(stderr, "test_PACKET_get_1() failed\n");
62 1.1 christos return 0;
63 1.1 christos }
64 1.1 christos
65 1.1 christos return 1;
66 1.1 christos }
67 1.1 christos
68 1.1 christos static int test_PACKET_get_4(unsigned char buf[BUF_LEN])
69 1.1 christos {
70 1.1 christos unsigned long i;
71 1.1 christos PACKET pkt;
72 1.1 christos
73 1.1 christos if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
74 1.1 christos || !PACKET_get_4(&pkt, &i)
75 1.1 christos || i != 0x08060402UL
76 1.1 christos || !PACKET_forward(&pkt, BUF_LEN - 8)
77 1.1 christos || !PACKET_get_4(&pkt, &i)
78 1.1 christos || i != 0xfefcfaf8UL
79 1.1 christos || PACKET_get_4(&pkt, &i)) {
80 1.1 christos fprintf(stderr, "test_PACKET_get_4() failed\n");
81 1.1 christos return 0;
82 1.1 christos }
83 1.1 christos
84 1.1 christos return 1;
85 1.1 christos }
86 1.1 christos
87 1.1 christos static int test_PACKET_get_net_2(unsigned char buf[BUF_LEN])
88 1.1 christos {
89 1.1 christos unsigned int i;
90 1.1 christos PACKET pkt;
91 1.1 christos
92 1.1 christos if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
93 1.1 christos || !PACKET_get_net_2(&pkt, &i)
94 1.1 christos || i != 0x0204
95 1.1 christos || !PACKET_forward(&pkt, BUF_LEN - 4)
96 1.1 christos || !PACKET_get_net_2(&pkt, &i)
97 1.1 christos || i != 0xfcfe
98 1.1 christos || PACKET_get_net_2(&pkt, &i)) {
99 1.1 christos fprintf(stderr, "test_PACKET_get_net_2() failed\n");
100 1.1 christos return 0;
101 1.1 christos }
102 1.1 christos
103 1.1 christos return 1;
104 1.1 christos }
105 1.1 christos
106 1.1 christos static int test_PACKET_get_net_3(unsigned char buf[BUF_LEN])
107 1.1 christos {
108 1.1 christos unsigned long i;
109 1.1 christos PACKET pkt;
110 1.1 christos
111 1.1 christos if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
112 1.1 christos || !PACKET_get_net_3(&pkt, &i)
113 1.1 christos || i != 0x020406UL
114 1.1 christos || !PACKET_forward(&pkt, BUF_LEN - 6)
115 1.1 christos || !PACKET_get_net_3(&pkt, &i)
116 1.1 christos || i != 0xfafcfeUL
117 1.1 christos || PACKET_get_net_3(&pkt, &i)) {
118 1.1 christos fprintf(stderr, "test_PACKET_get_net_3() failed\n");
119 1.1 christos return 0;
120 1.1 christos }
121 1.1 christos
122 1.1 christos return 1;
123 1.1 christos }
124 1.1 christos
125 1.1 christos static int test_PACKET_get_net_4(unsigned char buf[BUF_LEN])
126 1.1 christos {
127 1.1 christos unsigned long i;
128 1.1 christos PACKET pkt;
129 1.1 christos
130 1.1 christos if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
131 1.1 christos || !PACKET_get_net_4(&pkt, &i)
132 1.1 christos || i != 0x02040608UL
133 1.1 christos || !PACKET_forward(&pkt, BUF_LEN - 8)
134 1.1 christos || !PACKET_get_net_4(&pkt, &i)
135 1.1 christos || i != 0xf8fafcfeUL
136 1.1 christos || PACKET_get_net_4(&pkt, &i)) {
137 1.1 christos fprintf(stderr, "test_PACKET_get_net_4() failed\n");
138 1.1 christos return 0;
139 1.1 christos }
140 1.1 christos
141 1.1 christos return 1;
142 1.1 christos }
143 1.1 christos
144 1.1 christos static int test_PACKET_get_sub_packet(unsigned char buf[BUF_LEN])
145 1.1 christos {
146 1.1 christos PACKET pkt, subpkt;
147 1.1 christos unsigned long i;
148 1.1 christos
149 1.1 christos if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
150 1.1 christos || !PACKET_get_sub_packet(&pkt, &subpkt, 4)
151 1.1 christos || !PACKET_get_net_4(&subpkt, &i)
152 1.1 christos || i != 0x02040608UL
153 1.1 christos || PACKET_remaining(&subpkt)
154 1.1 christos || !PACKET_forward(&pkt, BUF_LEN - 8)
155 1.1 christos || !PACKET_get_sub_packet(&pkt, &subpkt, 4)
156 1.1 christos || !PACKET_get_net_4(&subpkt, &i)
157 1.1 christos || i != 0xf8fafcfeUL
158 1.1 christos || PACKET_remaining(&subpkt)
159 1.1 christos || PACKET_get_sub_packet(&pkt, &subpkt, 4)) {
160 1.1 christos fprintf(stderr, "test_PACKET_get_sub_packet() failed\n");
161 1.1 christos return 0;
162 1.1 christos }
163 1.1 christos
164 1.1 christos return 1;
165 1.1 christos }
166 1.1 christos
167 1.1 christos static int test_PACKET_get_bytes(unsigned char buf[BUF_LEN])
168 1.1 christos {
169 1.1 christos const unsigned char *bytes;
170 1.1 christos PACKET pkt;
171 1.1 christos
172 1.1 christos if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
173 1.1 christos || !PACKET_get_bytes(&pkt, &bytes, 4)
174 1.1 christos || bytes[0] != 2 || bytes[1] != 4
175 1.1 christos || bytes[2] != 6 || bytes[3] != 8
176 1.1 christos || PACKET_remaining(&pkt) != BUF_LEN -4
177 1.1 christos || !PACKET_forward(&pkt, BUF_LEN - 8)
178 1.1 christos || !PACKET_get_bytes(&pkt, &bytes, 4)
179 1.1 christos || bytes[0] != 0xf8 || bytes[1] != 0xfa
180 1.1 christos || bytes[2] != 0xfc || bytes[3] != 0xfe
181 1.1 christos || PACKET_remaining(&pkt)) {
182 1.1 christos fprintf(stderr, "test_PACKET_get_bytes() failed\n");
183 1.1 christos return 0;
184 1.1 christos }
185 1.1 christos
186 1.1 christos return 1;
187 1.1 christos }
188 1.1 christos
189 1.1 christos static int test_PACKET_copy_bytes(unsigned char buf[BUF_LEN])
190 1.1 christos {
191 1.1 christos unsigned char bytes[4];
192 1.1 christos PACKET pkt;
193 1.1 christos
194 1.1 christos if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
195 1.1 christos || !PACKET_copy_bytes(&pkt, bytes, 4)
196 1.1 christos || bytes[0] != 2 || bytes[1] != 4
197 1.1 christos || bytes[2] != 6 || bytes[3] != 8
198 1.1 christos || PACKET_remaining(&pkt) != BUF_LEN - 4
199 1.1 christos || !PACKET_forward(&pkt, BUF_LEN - 8)
200 1.1 christos || !PACKET_copy_bytes(&pkt, bytes, 4)
201 1.1 christos || bytes[0] != 0xf8 || bytes[1] != 0xfa
202 1.1 christos || bytes[2] != 0xfc || bytes[3] != 0xfe
203 1.1 christos || PACKET_remaining(&pkt)) {
204 1.1 christos fprintf(stderr, "test_PACKET_copy_bytes() failed\n");
205 1.1 christos return 0;
206 1.1 christos }
207 1.1 christos
208 1.1 christos return 1;
209 1.1 christos }
210 1.1 christos
211 1.1 christos static int test_PACKET_copy_all(unsigned char buf[BUF_LEN])
212 1.1 christos {
213 1.1 christos unsigned char tmp[BUF_LEN];
214 1.1 christos PACKET pkt;
215 1.1 christos size_t len;
216 1.1 christos
217 1.1 christos if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
218 1.1 christos || !PACKET_copy_all(&pkt, tmp, BUF_LEN, &len)
219 1.1 christos || len != BUF_LEN
220 1.1 christos || memcmp(buf, tmp, BUF_LEN) != 0
221 1.1 christos || PACKET_remaining(&pkt) != BUF_LEN
222 1.1 christos || PACKET_copy_all(&pkt, tmp, BUF_LEN - 1, &len)) {
223 1.1 christos fprintf(stderr, "test_PACKET_copy_bytes() failed\n");
224 1.1 christos return 0;
225 1.1 christos }
226 1.1 christos
227 1.1 christos return 1;
228 1.1 christos }
229 1.1 christos
230 1.1 christos static int test_PACKET_memdup(unsigned char buf[BUF_LEN])
231 1.1 christos {
232 1.1 christos unsigned char *data = NULL;
233 1.1 christos size_t len;
234 1.1 christos PACKET pkt;
235 1.1 christos
236 1.1 christos if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
237 1.1 christos || !PACKET_memdup(&pkt, &data, &len)
238 1.1 christos || len != BUF_LEN
239 1.1 christos || memcmp(data, PACKET_data(&pkt), len)
240 1.1 christos || !PACKET_forward(&pkt, 10)
241 1.1 christos || !PACKET_memdup(&pkt, &data, &len)
242 1.1 christos || len != BUF_LEN - 10
243 1.1 christos || memcmp(data, PACKET_data(&pkt), len)) {
244 1.1 christos fprintf(stderr, "test_PACKET_memdup() failed\n");
245 1.1 christos OPENSSL_free(data);
246 1.1 christos return 0;
247 1.1 christos }
248 1.1 christos
249 1.1 christos OPENSSL_free(data);
250 1.1 christos return 1;
251 1.1 christos }
252 1.1 christos
253 1.1 christos static int test_PACKET_strndup()
254 1.1 christos {
255 1.1 christos char buf[10], buf2[10];
256 1.1 christos char *data = NULL;
257 1.1 christos PACKET pkt;
258 1.1 christos
259 1.1 christos memset(buf, 'x', 10);
260 1.1 christos memset(buf2, 'y', 10);
261 1.1 christos buf2[5] = '\0';
262 1.1 christos
263 1.1 christos if ( !PACKET_buf_init(&pkt, (unsigned char*)buf, 10)
264 1.1 christos || !PACKET_strndup(&pkt, &data)
265 1.1 christos || strlen(data) != 10
266 1.1 christos || strncmp(data, buf, 10)
267 1.1 christos || !PACKET_buf_init(&pkt, (unsigned char*)buf2, 10)
268 1.1 christos || !PACKET_strndup(&pkt, &data)
269 1.1 christos || strlen(data) != 5
270 1.1 christos || strcmp(data, buf2)) {
271 1.1 christos fprintf(stderr, "test_PACKET_strndup failed\n");
272 1.1 christos OPENSSL_free(data);
273 1.1 christos return 0;
274 1.1 christos }
275 1.1 christos
276 1.1 christos OPENSSL_free(data);
277 1.1 christos return 1;
278 1.1 christos }
279 1.1 christos
280 1.1 christos static int test_PACKET_contains_zero_byte()
281 1.1 christos {
282 1.1 christos char buf[10], buf2[10];
283 1.1 christos PACKET pkt;
284 1.1 christos
285 1.1 christos memset(buf, 'x', 10);
286 1.1 christos memset(buf2, 'y', 10);
287 1.1 christos buf2[5] = '\0';
288 1.1 christos
289 1.1 christos if ( !PACKET_buf_init(&pkt, (unsigned char*)buf, 10)
290 1.1 christos || PACKET_contains_zero_byte(&pkt)
291 1.1 christos || !PACKET_buf_init(&pkt, (unsigned char*)buf2, 10)
292 1.1 christos || !PACKET_contains_zero_byte(&pkt)) {
293 1.1 christos fprintf(stderr, "test_PACKET_contains_zero_byte failed\n");
294 1.1 christos return 0;
295 1.1 christos }
296 1.1 christos
297 1.1 christos return 1;
298 1.1 christos }
299 1.1 christos
300 1.1 christos static int test_PACKET_forward(unsigned char buf[BUF_LEN])
301 1.1 christos {
302 1.1 christos const unsigned char *byte;
303 1.1 christos PACKET pkt;
304 1.1 christos
305 1.1 christos if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
306 1.1 christos || !PACKET_forward(&pkt, 1)
307 1.1 christos || !PACKET_get_bytes(&pkt, &byte, 1)
308 1.1 christos || byte[0] != 4
309 1.1 christos || !PACKET_forward(&pkt, BUF_LEN - 3)
310 1.1 christos || !PACKET_get_bytes(&pkt, &byte, 1)
311 1.1 christos || byte[0] != 0xfe) {
312 1.1 christos fprintf(stderr, "test_PACKET_forward() failed\n");
313 1.1 christos return 0;
314 1.1 christos }
315 1.1 christos
316 1.1 christos return 1;
317 1.1 christos }
318 1.1 christos
319 1.1 christos static int test_PACKET_buf_init()
320 1.1 christos {
321 1.1 christos unsigned char buf[BUF_LEN];
322 1.1 christos PACKET pkt;
323 1.1 christos
324 1.1 christos /* Also tests PACKET_remaining() */
325 1.1 christos if ( !PACKET_buf_init(&pkt, buf, 4)
326 1.1 christos || PACKET_remaining(&pkt) != 4
327 1.1 christos || !PACKET_buf_init(&pkt, buf, BUF_LEN)
328 1.1 christos || PACKET_remaining(&pkt) != BUF_LEN
329 1.1 christos || PACKET_buf_init(&pkt, buf, -1)) {
330 1.1 christos fprintf(stderr, "test_PACKET_buf_init() failed\n");
331 1.1 christos return 0;
332 1.1 christos }
333 1.1 christos
334 1.1 christos return 1;
335 1.1 christos }
336 1.1 christos
337 1.1 christos static int test_PACKET_null_init()
338 1.1 christos {
339 1.1 christos PACKET pkt;
340 1.1 christos
341 1.1 christos PACKET_null_init(&pkt);
342 1.1 christos if ( PACKET_remaining(&pkt) != 0
343 1.1 christos || PACKET_forward(&pkt, 1)) {
344 1.1 christos fprintf(stderr, "test_PACKET_null_init() failed\n");
345 1.1 christos return 0;
346 1.1 christos }
347 1.1 christos
348 1.1 christos return 1;
349 1.1 christos }
350 1.1 christos
351 1.1 christos static int test_PACKET_equal(unsigned char buf[BUF_LEN])
352 1.1 christos {
353 1.1 christos PACKET pkt;
354 1.1 christos
355 1.1 christos if ( !PACKET_buf_init(&pkt, buf, 4)
356 1.1 christos || !PACKET_equal(&pkt, buf, 4)
357 1.1 christos || PACKET_equal(&pkt, buf + 1, 4)
358 1.1 christos || !PACKET_buf_init(&pkt, buf, BUF_LEN)
359 1.1 christos || !PACKET_equal(&pkt, buf, BUF_LEN)
360 1.1 christos || PACKET_equal(&pkt, buf, BUF_LEN - 1)
361 1.1 christos || PACKET_equal(&pkt, buf, BUF_LEN + 1)
362 1.1 christos || PACKET_equal(&pkt, buf, 0)) {
363 1.1 christos fprintf(stderr, "test_PACKET_equal() failed\n");
364 1.1 christos return 0;
365 1.1 christos }
366 1.1 christos
367 1.1 christos return 1;
368 1.1 christos }
369 1.1 christos
370 1.1 christos static int test_PACKET_get_length_prefixed_1()
371 1.1 christos {
372 1.1 christos unsigned char buf[BUF_LEN];
373 1.1 christos const size_t len = 16;
374 1.1 christos unsigned int i;
375 1.1 christos PACKET pkt, short_pkt, subpkt;
376 1.1 christos
377 1.1 christos buf[0] = len;
378 1.1 christos for (i = 1; i < BUF_LEN; i++) {
379 1.1 christos buf[i] = (i * 2) & 0xff;
380 1.1 christos }
381 1.1 christos
382 1.1 christos if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
383 1.1 christos || !PACKET_buf_init(&short_pkt, buf, len)
384 1.1 christos || !PACKET_get_length_prefixed_1(&pkt, &subpkt)
385 1.1 christos || PACKET_remaining(&subpkt) != len
386 1.1 christos || !PACKET_get_net_2(&subpkt, &i)
387 1.1 christos || i != 0x0204
388 1.1 christos || PACKET_get_length_prefixed_1(&short_pkt, &subpkt)
389 1.1 christos || PACKET_remaining(&short_pkt) != len) {
390 1.1 christos fprintf(stderr, "test_PACKET_get_length_prefixed_1() failed\n");
391 1.1 christos return 0;
392 1.1 christos }
393 1.1 christos
394 1.1 christos return 1;
395 1.1 christos }
396 1.1 christos
397 1.1 christos static int test_PACKET_get_length_prefixed_2()
398 1.1 christos {
399 1.1 christos unsigned char buf[1024];
400 1.1 christos const size_t len = 516; /* 0x0204 */
401 1.1 christos unsigned int i;
402 1.1 christos PACKET pkt, short_pkt, subpkt;
403 1.1 christos
404 1.1 christos for (i = 1; i <= 1024; i++) {
405 1.1 christos buf[i-1] = (i * 2) & 0xff;
406 1.1 christos }
407 1.1 christos
408 1.1 christos if ( !PACKET_buf_init(&pkt, buf, 1024)
409 1.1 christos || !PACKET_buf_init(&short_pkt, buf, len)
410 1.1 christos || !PACKET_get_length_prefixed_2(&pkt, &subpkt)
411 1.1 christos || PACKET_remaining(&subpkt) != len
412 1.1 christos || !PACKET_get_net_2(&subpkt, &i)
413 1.1 christos || i != 0x0608
414 1.1 christos || PACKET_get_length_prefixed_2(&short_pkt, &subpkt)
415 1.1 christos || PACKET_remaining(&short_pkt) != len) {
416 1.1 christos fprintf(stderr, "test_PACKET_get_length_prefixed_2() failed\n");
417 1.1 christos return 0;
418 1.1 christos }
419 1.1 christos
420 1.1 christos return 1;
421 1.1 christos }
422 1.1 christos
423 1.1 christos static int test_PACKET_get_length_prefixed_3()
424 1.1 christos {
425 1.1 christos unsigned char buf[1024];
426 1.1 christos const size_t len = 516; /* 0x000204 */
427 1.1 christos unsigned int i;
428 1.1 christos PACKET pkt, short_pkt, subpkt;
429 1.1 christos
430 1.1 christos for (i = 0; i < 1024; i++) {
431 1.1 christos buf[i] = (i * 2) & 0xff;
432 1.1 christos }
433 1.1 christos
434 1.1 christos if ( !PACKET_buf_init(&pkt, buf, 1024)
435 1.1 christos || !PACKET_buf_init(&short_pkt, buf, len)
436 1.1 christos || !PACKET_get_length_prefixed_3(&pkt, &subpkt)
437 1.1 christos || PACKET_remaining(&subpkt) != len
438 1.1 christos || !PACKET_get_net_2(&subpkt, &i)
439 1.1 christos || i != 0x0608
440 1.1 christos || PACKET_get_length_prefixed_3(&short_pkt, &subpkt)
441 1.1 christos || PACKET_remaining(&short_pkt) != len) {
442 1.1 christos fprintf(stderr, "test_PACKET_get_length_prefixed_3() failed\n");
443 1.1 christos return 0;
444 1.1 christos }
445 1.1 christos
446 1.1 christos return 1;
447 1.1 christos }
448 1.1 christos
449 1.1 christos static int test_PACKET_as_length_prefixed_1()
450 1.1 christos {
451 1.1 christos unsigned char buf[BUF_LEN];
452 1.1 christos const size_t len = 16;
453 1.1 christos unsigned int i;
454 1.1 christos PACKET pkt, exact_pkt, subpkt;
455 1.1 christos
456 1.1 christos buf[0] = len;
457 1.1 christos for (i = 1; i < BUF_LEN; i++) {
458 1.1 christos buf[i] = (i * 2) & 0xff;
459 1.1 christos }
460 1.1 christos
461 1.1 christos if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
462 1.1 christos || !PACKET_buf_init(&exact_pkt, buf, len + 1)
463 1.1 christos || PACKET_as_length_prefixed_1(&pkt, &subpkt)
464 1.1 christos || PACKET_remaining(&pkt) != BUF_LEN
465 1.1 christos || !PACKET_as_length_prefixed_1(&exact_pkt, &subpkt)
466 1.1 christos || PACKET_remaining(&exact_pkt) != 0
467 1.1 christos || PACKET_remaining(&subpkt) != len) {
468 1.1 christos fprintf(stderr, "test_PACKET_as_length_prefixed_1() failed\n");
469 1.1 christos return 0;
470 1.1 christos }
471 1.1 christos
472 1.1 christos return 1;
473 1.1 christos }
474 1.1 christos
475 1.1 christos static int test_PACKET_as_length_prefixed_2()
476 1.1 christos {
477 1.1 christos unsigned char buf[1024];
478 1.1 christos const size_t len = 516; /* 0x0204 */
479 1.1 christos unsigned int i;
480 1.1 christos PACKET pkt, exact_pkt, subpkt;
481 1.1 christos
482 1.1 christos for (i = 1; i <= 1024; i++) {
483 1.1 christos buf[i-1] = (i * 2) & 0xff;
484 1.1 christos }
485 1.1 christos
486 1.1 christos if ( !PACKET_buf_init(&pkt, buf, 1024)
487 1.1 christos || !PACKET_buf_init(&exact_pkt, buf, len + 2)
488 1.1 christos || PACKET_as_length_prefixed_2(&pkt, &subpkt)
489 1.1 christos || PACKET_remaining(&pkt) != 1024
490 1.1 christos || !PACKET_as_length_prefixed_2(&exact_pkt, &subpkt)
491 1.1 christos || PACKET_remaining(&exact_pkt) != 0
492 1.1 christos || PACKET_remaining(&subpkt) != len) {
493 1.1 christos fprintf(stderr, "test_PACKET_as_length_prefixed_2() failed\n");
494 1.1 christos return 0;
495 1.1 christos }
496 1.1 christos
497 1.1 christos return 1;
498 1.1 christos }
499 1.1 christos
500 1.1 christos int main(int argc, char **argv)
501 1.1 christos {
502 1.1 christos unsigned char buf[BUF_LEN];
503 1.1 christos unsigned int i;
504 1.1 christos
505 1.1 christos for (i=1; i<=BUF_LEN; i++) {
506 1.1 christos buf[i-1] = (i * 2) & 0xff;
507 1.1 christos }
508 1.1 christos i = 0;
509 1.1 christos
510 1.1 christos if ( !test_PACKET_buf_init()
511 1.1 christos || !test_PACKET_null_init()
512 1.1 christos || !test_PACKET_remaining(buf)
513 1.1 christos || !test_PACKET_end(buf)
514 1.1 christos || !test_PACKET_equal(buf)
515 1.1 christos || !test_PACKET_get_1(buf)
516 1.1 christos || !test_PACKET_get_4(buf)
517 1.1 christos || !test_PACKET_get_net_2(buf)
518 1.1 christos || !test_PACKET_get_net_3(buf)
519 1.1 christos || !test_PACKET_get_net_4(buf)
520 1.1 christos || !test_PACKET_get_sub_packet(buf)
521 1.1 christos || !test_PACKET_get_bytes(buf)
522 1.1 christos || !test_PACKET_copy_bytes(buf)
523 1.1 christos || !test_PACKET_copy_all(buf)
524 1.1 christos || !test_PACKET_memdup(buf)
525 1.1 christos || !test_PACKET_strndup()
526 1.1 christos || !test_PACKET_contains_zero_byte()
527 1.1 christos || !test_PACKET_forward(buf)
528 1.1 christos || !test_PACKET_get_length_prefixed_1()
529 1.1 christos || !test_PACKET_get_length_prefixed_2()
530 1.1 christos || !test_PACKET_get_length_prefixed_3()
531 1.1 christos || !test_PACKET_as_length_prefixed_1()
532 1.1 christos || !test_PACKET_as_length_prefixed_2()) {
533 1.1 christos return 1;
534 1.1 christos }
535 1.1 christos printf("PASS\n");
536 1.1 christos return 0;
537 1.1 christos }
538