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