2 * Written by Matt Caswell for the OpenSSL project.
4 /* ====================================================================
5 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * openssl-core@openssl.org.
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
59 #include "../ssl/packet_locl.h"
63 static int test_PACKET_remaining(unsigned char buf[BUF_LEN])
67 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
68 || PACKET_remaining(&pkt) != BUF_LEN
69 || !PACKET_forward(&pkt, BUF_LEN - 1)
70 || PACKET_remaining(&pkt) != 1
71 || !PACKET_forward(&pkt, 1)
72 || PACKET_remaining(&pkt) != 0) {
73 fprintf(stderr, "test_PACKET_remaining() failed\n");
80 static int test_PACKET_end(unsigned char buf[BUF_LEN])
84 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
85 || PACKET_remaining(&pkt) != BUF_LEN
86 || PACKET_end(&pkt) != buf + BUF_LEN
87 || !PACKET_forward(&pkt, BUF_LEN - 1)
88 || PACKET_end(&pkt) != buf + BUF_LEN
89 || !PACKET_forward(&pkt, 1)
90 || PACKET_end(&pkt) != buf + BUF_LEN) {
91 fprintf(stderr, "test_PACKET_end() failed\n");
98 static int test_PACKET_get_1(unsigned char buf[BUF_LEN])
103 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
104 || !PACKET_get_1(&pkt, &i)
106 || !PACKET_forward(&pkt, BUF_LEN - 2)
107 || !PACKET_get_1(&pkt, &i)
109 || PACKET_get_1(&pkt, &i)) {
110 fprintf(stderr, "test_PACKET_get_1() failed\n");
117 static int test_PACKET_get_4(unsigned char buf[BUF_LEN])
122 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
123 || !PACKET_get_4(&pkt, &i)
125 || !PACKET_forward(&pkt, BUF_LEN - 8)
126 || !PACKET_get_4(&pkt, &i)
128 || PACKET_get_4(&pkt, &i)) {
129 fprintf(stderr, "test_PACKET_get_4() failed\n");
136 static int test_PACKET_get_net_2(unsigned char buf[BUF_LEN])
141 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
142 || !PACKET_get_net_2(&pkt, &i)
144 || !PACKET_forward(&pkt, BUF_LEN - 4)
145 || !PACKET_get_net_2(&pkt, &i)
147 || PACKET_get_net_2(&pkt, &i)) {
148 fprintf(stderr, "test_PACKET_get_net_2() failed\n");
155 static int test_PACKET_get_net_3(unsigned char buf[BUF_LEN])
160 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
161 || !PACKET_get_net_3(&pkt, &i)
163 || !PACKET_forward(&pkt, BUF_LEN - 6)
164 || !PACKET_get_net_3(&pkt, &i)
166 || PACKET_get_net_3(&pkt, &i)) {
167 fprintf(stderr, "test_PACKET_get_net_3() failed\n");
174 static int test_PACKET_get_net_4(unsigned char buf[BUF_LEN])
179 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
180 || !PACKET_get_net_4(&pkt, &i)
182 || !PACKET_forward(&pkt, BUF_LEN - 8)
183 || !PACKET_get_net_4(&pkt, &i)
185 || PACKET_get_net_4(&pkt, &i)) {
186 fprintf(stderr, "test_PACKET_get_net_4() failed\n");
193 static int test_PACKET_get_sub_packet(unsigned char buf[BUF_LEN])
198 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
199 || !PACKET_get_sub_packet(&pkt, &subpkt, 4)
200 || !PACKET_get_net_4(&subpkt, &i)
202 || PACKET_remaining(&subpkt)
203 || !PACKET_forward(&pkt, BUF_LEN - 8)
204 || !PACKET_get_sub_packet(&pkt, &subpkt, 4)
205 || !PACKET_get_net_4(&subpkt, &i)
207 || PACKET_remaining(&subpkt)
208 || PACKET_get_sub_packet(&pkt, &subpkt, 4)) {
209 fprintf(stderr, "test_PACKET_get_sub_packet() failed\n");
216 static int test_PACKET_get_bytes(unsigned char buf[BUF_LEN])
218 const unsigned char *bytes;
221 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
222 || !PACKET_get_bytes(&pkt, &bytes, 4)
223 || bytes[0] != 2 || bytes[1] != 4
224 || bytes[2] != 6 || bytes[3] != 8
225 || PACKET_remaining(&pkt) != BUF_LEN -4
226 || !PACKET_forward(&pkt, BUF_LEN - 8)
227 || !PACKET_get_bytes(&pkt, &bytes, 4)
228 || bytes[0] != 0xf8 || bytes[1] != 0xfa
229 || bytes[2] != 0xfc || bytes[3] != 0xfe
230 || PACKET_remaining(&pkt)) {
231 fprintf(stderr, "test_PACKET_get_bytes() failed\n");
238 static int test_PACKET_copy_bytes(unsigned char buf[BUF_LEN])
240 unsigned char bytes[4];
243 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
244 || !PACKET_copy_bytes(&pkt, bytes, 4)
245 || bytes[0] != 2 || bytes[1] != 4
246 || bytes[2] != 6 || bytes[3] != 8
247 || PACKET_remaining(&pkt) != BUF_LEN - 4
248 || !PACKET_forward(&pkt, BUF_LEN - 8)
249 || !PACKET_copy_bytes(&pkt, bytes, 4)
250 || bytes[0] != 0xf8 || bytes[1] != 0xfa
251 || bytes[2] != 0xfc || bytes[3] != 0xfe
252 || PACKET_remaining(&pkt)) {
253 fprintf(stderr, "test_PACKET_copy_bytes() failed\n");
260 static int test_PACKET_copy_all(unsigned char buf[BUF_LEN])
262 unsigned char tmp[BUF_LEN];
266 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
267 || !PACKET_copy_all(&pkt, tmp, BUF_LEN, &len)
269 || memcmp(buf, tmp, BUF_LEN) != 0
270 || PACKET_remaining(&pkt) != BUF_LEN
271 || PACKET_copy_all(&pkt, tmp, BUF_LEN - 1, &len)) {
272 fprintf(stderr, "test_PACKET_copy_bytes() failed\n");
279 static int test_PACKET_memdup(unsigned char buf[BUF_LEN])
281 unsigned char *data = NULL;
285 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
286 || !PACKET_memdup(&pkt, &data, &len)
288 || memcmp(data, PACKET_data(&pkt), len)
289 || !PACKET_forward(&pkt, 10)
290 || !PACKET_memdup(&pkt, &data, &len)
291 || len != BUF_LEN - 10
292 || memcmp(data, PACKET_data(&pkt), len)) {
293 fprintf(stderr, "test_PACKET_memdup() failed\n");
302 static int test_PACKET_strndup()
304 char buf[10], buf2[10];
308 memset(buf, 'x', 10);
309 memset(buf2, 'y', 10);
312 if ( !PACKET_buf_init(&pkt, (unsigned char*)buf, 10)
313 || !PACKET_strndup(&pkt, &data)
314 || strlen(data) != 10
315 || strncmp(data, buf, 10)
316 || !PACKET_buf_init(&pkt, (unsigned char*)buf2, 10)
317 || !PACKET_strndup(&pkt, &data)
319 || strcmp(data, buf2)) {
320 fprintf(stderr, "test_PACKET_strndup failed\n");
329 static int test_PACKET_contains_zero_byte()
331 char buf[10], buf2[10];
334 memset(buf, 'x', 10);
335 memset(buf2, 'y', 10);
338 if ( !PACKET_buf_init(&pkt, (unsigned char*)buf, 10)
339 || PACKET_contains_zero_byte(&pkt)
340 || !PACKET_buf_init(&pkt, (unsigned char*)buf2, 10)
341 || !PACKET_contains_zero_byte(&pkt)) {
342 fprintf(stderr, "test_PACKET_contains_zero_byte failed\n");
349 static int test_PACKET_forward(unsigned char buf[BUF_LEN])
351 const unsigned char *byte;
354 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
355 || !PACKET_forward(&pkt, 1)
356 || !PACKET_get_bytes(&pkt, &byte, 1)
358 || !PACKET_forward(&pkt, BUF_LEN - 3)
359 || !PACKET_get_bytes(&pkt, &byte, 1)
360 || byte[0] != 0xfe) {
361 fprintf(stderr, "test_PACKET_forward() failed\n");
368 static int test_PACKET_buf_init()
370 unsigned char buf[BUF_LEN];
373 /* Also tests PACKET_remaining() */
374 if ( !PACKET_buf_init(&pkt, buf, 4)
375 || PACKET_remaining(&pkt) != 4
376 || !PACKET_buf_init(&pkt, buf, BUF_LEN)
377 || PACKET_remaining(&pkt) != BUF_LEN
378 || PACKET_buf_init(&pkt, buf, -1)) {
379 fprintf(stderr, "test_PACKET_buf_init() failed\n");
386 static int test_PACKET_null_init()
390 PACKET_null_init(&pkt);
391 if ( PACKET_remaining(&pkt) != 0
392 || PACKET_forward(&pkt, 1)) {
393 fprintf(stderr, "test_PACKET_null_init() failed\n");
400 static int test_PACKET_equal(unsigned char buf[BUF_LEN])
404 if ( !PACKET_buf_init(&pkt, buf, 4)
405 || !PACKET_equal(&pkt, buf, 4)
406 || PACKET_equal(&pkt, buf + 1, 4)
407 || !PACKET_buf_init(&pkt, buf, BUF_LEN)
408 || !PACKET_equal(&pkt, buf, BUF_LEN)
409 || PACKET_equal(&pkt, buf, BUF_LEN - 1)
410 || PACKET_equal(&pkt, buf, BUF_LEN + 1)
411 || PACKET_equal(&pkt, buf, 0)) {
412 fprintf(stderr, "test_PACKET_equal() failed\n");
419 static int test_PACKET_get_length_prefixed_1()
421 unsigned char buf[BUF_LEN];
422 const size_t len = 16;
424 PACKET pkt, short_pkt, subpkt;
427 for (i = 1; i < BUF_LEN; i++) {
428 buf[i] = (i * 2) & 0xff;
431 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
432 || !PACKET_buf_init(&short_pkt, buf, len)
433 || !PACKET_get_length_prefixed_1(&pkt, &subpkt)
434 || PACKET_remaining(&subpkt) != len
435 || !PACKET_get_net_2(&subpkt, &i)
437 || PACKET_get_length_prefixed_1(&short_pkt, &subpkt)
438 || PACKET_remaining(&short_pkt) != len) {
439 fprintf(stderr, "test_PACKET_get_length_prefixed_1() failed\n");
446 static int test_PACKET_get_length_prefixed_2()
448 unsigned char buf[1024];
449 const size_t len = 516; /* 0x0204 */
451 PACKET pkt, short_pkt, subpkt;
453 for (i = 1; i <= 1024; i++) {
454 buf[i-1] = (i * 2) & 0xff;
457 if ( !PACKET_buf_init(&pkt, buf, 1024)
458 || !PACKET_buf_init(&short_pkt, buf, len)
459 || !PACKET_get_length_prefixed_2(&pkt, &subpkt)
460 || PACKET_remaining(&subpkt) != len
461 || !PACKET_get_net_2(&subpkt, &i)
463 || PACKET_get_length_prefixed_2(&short_pkt, &subpkt)
464 || PACKET_remaining(&short_pkt) != len) {
465 fprintf(stderr, "test_PACKET_get_length_prefixed_2() failed\n");
472 static int test_PACKET_get_length_prefixed_3()
474 unsigned char buf[1024];
475 const size_t len = 516; /* 0x000204 */
477 PACKET pkt, short_pkt, subpkt;
479 for (i = 0; i < 1024; i++) {
480 buf[i] = (i * 2) & 0xff;
483 if ( !PACKET_buf_init(&pkt, buf, 1024)
484 || !PACKET_buf_init(&short_pkt, buf, len)
485 || !PACKET_get_length_prefixed_3(&pkt, &subpkt)
486 || PACKET_remaining(&subpkt) != len
487 || !PACKET_get_net_2(&subpkt, &i)
489 || PACKET_get_length_prefixed_3(&short_pkt, &subpkt)
490 || PACKET_remaining(&short_pkt) != len) {
491 fprintf(stderr, "test_PACKET_get_length_prefixed_3() failed\n");
498 static int test_PACKET_as_length_prefixed_1()
500 unsigned char buf[BUF_LEN];
501 const size_t len = 16;
503 PACKET pkt, exact_pkt, subpkt;
506 for (i = 1; i < BUF_LEN; i++) {
507 buf[i] = (i * 2) & 0xff;
510 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
511 || !PACKET_buf_init(&exact_pkt, buf, len + 1)
512 || PACKET_as_length_prefixed_1(&pkt, &subpkt)
513 || PACKET_remaining(&pkt) != BUF_LEN
514 || !PACKET_as_length_prefixed_1(&exact_pkt, &subpkt)
515 || PACKET_remaining(&exact_pkt) != 0
516 || PACKET_remaining(&subpkt) != len) {
517 fprintf(stderr, "test_PACKET_as_length_prefixed_1() failed\n");
524 static int test_PACKET_as_length_prefixed_2()
526 unsigned char buf[1024];
527 const size_t len = 516; /* 0x0204 */
529 PACKET pkt, exact_pkt, subpkt;
531 for (i = 1; i <= 1024; i++) {
532 buf[i-1] = (i * 2) & 0xff;
535 if ( !PACKET_buf_init(&pkt, buf, 1024)
536 || !PACKET_buf_init(&exact_pkt, buf, len + 2)
537 || PACKET_as_length_prefixed_2(&pkt, &subpkt)
538 || PACKET_remaining(&pkt) != 1024
539 || !PACKET_as_length_prefixed_2(&exact_pkt, &subpkt)
540 || PACKET_remaining(&exact_pkt) != 0
541 || PACKET_remaining(&subpkt) != len) {
542 fprintf(stderr, "test_PACKET_as_length_prefixed_2() failed\n");
549 int main(int argc, char **argv)
551 unsigned char buf[BUF_LEN];
554 for (i=1; i<=BUF_LEN; i++) {
555 buf[i-1] = (i * 2) & 0xff;
559 if ( !test_PACKET_buf_init()
560 || !test_PACKET_null_init()
561 || !test_PACKET_remaining(buf)
562 || !test_PACKET_end(buf)
563 || !test_PACKET_equal(buf)
564 || !test_PACKET_get_1(buf)
565 || !test_PACKET_get_4(buf)
566 || !test_PACKET_get_net_2(buf)
567 || !test_PACKET_get_net_3(buf)
568 || !test_PACKET_get_net_4(buf)
569 || !test_PACKET_get_sub_packet(buf)
570 || !test_PACKET_get_bytes(buf)
571 || !test_PACKET_copy_bytes(buf)
572 || !test_PACKET_copy_all(buf)
573 || !test_PACKET_memdup(buf)
574 || !test_PACKET_strndup()
575 || !test_PACKET_contains_zero_byte()
576 || !test_PACKET_forward(buf)
577 || !test_PACKET_get_length_prefixed_1()
578 || !test_PACKET_get_length_prefixed_2()
579 || !test_PACKET_get_length_prefixed_3()
580 || !test_PACKET_as_length_prefixed_1()
581 || !test_PACKET_as_length_prefixed_2()) {