2 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "../ssl/packet_locl.h"
14 static int test_PACKET_remaining(unsigned char buf[BUF_LEN])
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) {
24 fprintf(stderr, "test_PACKET_remaining() failed\n");
31 static int test_PACKET_end(unsigned char buf[BUF_LEN])
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");
49 static int test_PACKET_get_1(unsigned char buf[BUF_LEN])
54 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
55 || !PACKET_get_1(&pkt, &i)
57 || !PACKET_forward(&pkt, BUF_LEN - 2)
58 || !PACKET_get_1(&pkt, &i)
60 || PACKET_get_1(&pkt, &i)) {
61 fprintf(stderr, "test_PACKET_get_1() failed\n");
68 static int test_PACKET_get_4(unsigned char buf[BUF_LEN])
73 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
74 || !PACKET_get_4(&pkt, &i)
76 || !PACKET_forward(&pkt, BUF_LEN - 8)
77 || !PACKET_get_4(&pkt, &i)
79 || PACKET_get_4(&pkt, &i)) {
80 fprintf(stderr, "test_PACKET_get_4() failed\n");
87 static int test_PACKET_get_net_2(unsigned char buf[BUF_LEN])
92 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
93 || !PACKET_get_net_2(&pkt, &i)
95 || !PACKET_forward(&pkt, BUF_LEN - 4)
96 || !PACKET_get_net_2(&pkt, &i)
98 || PACKET_get_net_2(&pkt, &i)) {
99 fprintf(stderr, "test_PACKET_get_net_2() failed\n");
106 static int test_PACKET_get_net_3(unsigned char buf[BUF_LEN])
111 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
112 || !PACKET_get_net_3(&pkt, &i)
114 || !PACKET_forward(&pkt, BUF_LEN - 6)
115 || !PACKET_get_net_3(&pkt, &i)
117 || PACKET_get_net_3(&pkt, &i)) {
118 fprintf(stderr, "test_PACKET_get_net_3() failed\n");
125 static int test_PACKET_get_net_4(unsigned char buf[BUF_LEN])
130 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
131 || !PACKET_get_net_4(&pkt, &i)
133 || !PACKET_forward(&pkt, BUF_LEN - 8)
134 || !PACKET_get_net_4(&pkt, &i)
136 || PACKET_get_net_4(&pkt, &i)) {
137 fprintf(stderr, "test_PACKET_get_net_4() failed\n");
144 static int test_PACKET_get_sub_packet(unsigned char buf[BUF_LEN])
149 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
150 || !PACKET_get_sub_packet(&pkt, &subpkt, 4)
151 || !PACKET_get_net_4(&subpkt, &i)
153 || PACKET_remaining(&subpkt)
154 || !PACKET_forward(&pkt, BUF_LEN - 8)
155 || !PACKET_get_sub_packet(&pkt, &subpkt, 4)
156 || !PACKET_get_net_4(&subpkt, &i)
158 || PACKET_remaining(&subpkt)
159 || PACKET_get_sub_packet(&pkt, &subpkt, 4)) {
160 fprintf(stderr, "test_PACKET_get_sub_packet() failed\n");
167 static int test_PACKET_get_bytes(unsigned char buf[BUF_LEN])
169 const unsigned char *bytes;
172 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
173 || !PACKET_get_bytes(&pkt, &bytes, 4)
174 || bytes[0] != 2 || bytes[1] != 4
175 || bytes[2] != 6 || bytes[3] != 8
176 || PACKET_remaining(&pkt) != BUF_LEN -4
177 || !PACKET_forward(&pkt, BUF_LEN - 8)
178 || !PACKET_get_bytes(&pkt, &bytes, 4)
179 || bytes[0] != 0xf8 || bytes[1] != 0xfa
180 || bytes[2] != 0xfc || bytes[3] != 0xfe
181 || PACKET_remaining(&pkt)) {
182 fprintf(stderr, "test_PACKET_get_bytes() failed\n");
189 static int test_PACKET_copy_bytes(unsigned char buf[BUF_LEN])
191 unsigned char bytes[4];
194 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
195 || !PACKET_copy_bytes(&pkt, bytes, 4)
196 || bytes[0] != 2 || bytes[1] != 4
197 || bytes[2] != 6 || bytes[3] != 8
198 || PACKET_remaining(&pkt) != BUF_LEN - 4
199 || !PACKET_forward(&pkt, BUF_LEN - 8)
200 || !PACKET_copy_bytes(&pkt, bytes, 4)
201 || bytes[0] != 0xf8 || bytes[1] != 0xfa
202 || bytes[2] != 0xfc || bytes[3] != 0xfe
203 || PACKET_remaining(&pkt)) {
204 fprintf(stderr, "test_PACKET_copy_bytes() failed\n");
211 static int test_PACKET_copy_all(unsigned char buf[BUF_LEN])
213 unsigned char tmp[BUF_LEN];
217 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
218 || !PACKET_copy_all(&pkt, tmp, BUF_LEN, &len)
220 || memcmp(buf, tmp, BUF_LEN) != 0
221 || PACKET_remaining(&pkt) != BUF_LEN
222 || PACKET_copy_all(&pkt, tmp, BUF_LEN - 1, &len)) {
223 fprintf(stderr, "test_PACKET_copy_bytes() failed\n");
230 static int test_PACKET_memdup(unsigned char buf[BUF_LEN])
232 unsigned char *data = NULL;
236 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
237 || !PACKET_memdup(&pkt, &data, &len)
239 || memcmp(data, PACKET_data(&pkt), len)
240 || !PACKET_forward(&pkt, 10)
241 || !PACKET_memdup(&pkt, &data, &len)
242 || len != BUF_LEN - 10
243 || memcmp(data, PACKET_data(&pkt), len)) {
244 fprintf(stderr, "test_PACKET_memdup() failed\n");
253 static int test_PACKET_strndup()
255 char buf[10], buf2[10];
259 memset(buf, 'x', 10);
260 memset(buf2, 'y', 10);
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)
270 || strcmp(data, buf2)) {
271 fprintf(stderr, "test_PACKET_strndup failed\n");
280 static int test_PACKET_contains_zero_byte()
282 char buf[10], buf2[10];
285 memset(buf, 'x', 10);
286 memset(buf2, 'y', 10);
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");
300 static int test_PACKET_forward(unsigned char buf[BUF_LEN])
302 const unsigned char *byte;
305 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
306 || !PACKET_forward(&pkt, 1)
307 || !PACKET_get_bytes(&pkt, &byte, 1)
309 || !PACKET_forward(&pkt, BUF_LEN - 3)
310 || !PACKET_get_bytes(&pkt, &byte, 1)
311 || byte[0] != 0xfe) {
312 fprintf(stderr, "test_PACKET_forward() failed\n");
319 static int test_PACKET_buf_init()
321 unsigned char buf[BUF_LEN];
324 /* Also tests PACKET_remaining() */
325 if ( !PACKET_buf_init(&pkt, buf, 4)
326 || PACKET_remaining(&pkt) != 4
327 || !PACKET_buf_init(&pkt, buf, BUF_LEN)
328 || PACKET_remaining(&pkt) != BUF_LEN
329 || PACKET_buf_init(&pkt, buf, -1)) {
330 fprintf(stderr, "test_PACKET_buf_init() failed\n");
337 static int test_PACKET_null_init()
341 PACKET_null_init(&pkt);
342 if ( PACKET_remaining(&pkt) != 0
343 || PACKET_forward(&pkt, 1)) {
344 fprintf(stderr, "test_PACKET_null_init() failed\n");
351 static int test_PACKET_equal(unsigned char buf[BUF_LEN])
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");
370 static int test_PACKET_get_length_prefixed_1()
372 unsigned char buf[BUF_LEN];
373 const size_t len = 16;
375 PACKET pkt, short_pkt, subpkt;
378 for (i = 1; i < BUF_LEN; i++) {
379 buf[i] = (i * 2) & 0xff;
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)
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");
397 static int test_PACKET_get_length_prefixed_2()
399 unsigned char buf[1024];
400 const size_t len = 516; /* 0x0204 */
402 PACKET pkt, short_pkt, subpkt;
404 for (i = 1; i <= 1024; i++) {
405 buf[i-1] = (i * 2) & 0xff;
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)
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");
423 static int test_PACKET_get_length_prefixed_3()
425 unsigned char buf[1024];
426 const size_t len = 516; /* 0x000204 */
428 PACKET pkt, short_pkt, subpkt;
430 for (i = 0; i < 1024; i++) {
431 buf[i] = (i * 2) & 0xff;
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)
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");
449 static int test_PACKET_as_length_prefixed_1()
451 unsigned char buf[BUF_LEN];
452 const size_t len = 16;
454 PACKET pkt, exact_pkt, subpkt;
457 for (i = 1; i < BUF_LEN; i++) {
458 buf[i] = (i * 2) & 0xff;
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");
475 static int test_PACKET_as_length_prefixed_2()
477 unsigned char buf[1024];
478 const size_t len = 516; /* 0x0204 */
480 PACKET pkt, exact_pkt, subpkt;
482 for (i = 1; i <= 1024; i++) {
483 buf[i-1] = (i * 2) & 0xff;
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");
500 int main(int argc, char **argv)
502 unsigned char buf[BUF_LEN];
505 for (i=1; i<=BUF_LEN; i++) {
506 buf[i-1] = (i * 2) & 0xff;
510 if ( !test_PACKET_buf_init()
511 || !test_PACKET_null_init()
512 || !test_PACKET_remaining(buf)
513 || !test_PACKET_end(buf)
514 || !test_PACKET_equal(buf)
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)
523 || !test_PACKET_copy_all(buf)
524 || !test_PACKET_memdup(buf)
525 || !test_PACKET_strndup()
526 || !test_PACKET_contains_zero_byte()
527 || !test_PACKET_forward(buf)
528 || !test_PACKET_get_length_prefixed_1()
529 || !test_PACKET_get_length_prefixed_2()
530 || !test_PACKET_get_length_prefixed_3()
531 || !test_PACKET_as_length_prefixed_1()
532 || !test_PACKET_as_length_prefixed_2()) {