]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/packettest.c
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_get_1(unsigned char buf
[BUF_LEN
])
85 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
86 || !PACKET_get_1(&pkt
, &i
)
88 || !PACKET_forward(&pkt
, BUF_LEN
- 2)
89 || !PACKET_get_1(&pkt
, &i
)
91 || PACKET_get_1(&pkt
, &i
)) {
92 fprintf(stderr
, "test_PACKET_get_1() failed\n");
99 static int test_PACKET_get_4(unsigned char buf
[BUF_LEN
])
104 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
105 || !PACKET_get_4(&pkt
, &i
)
107 || !PACKET_forward(&pkt
, BUF_LEN
- 8)
108 || !PACKET_get_4(&pkt
, &i
)
110 || PACKET_get_4(&pkt
, &i
)) {
111 fprintf(stderr
, "test_PACKET_get_4() failed\n");
118 static int test_PACKET_get_net_2(unsigned char buf
[BUF_LEN
])
123 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
124 || !PACKET_get_net_2(&pkt
, &i
)
126 || !PACKET_forward(&pkt
, BUF_LEN
- 4)
127 || !PACKET_get_net_2(&pkt
, &i
)
129 || PACKET_get_net_2(&pkt
, &i
)) {
130 fprintf(stderr
, "test_PACKET_get_net_2() failed\n");
137 static int test_PACKET_get_net_3(unsigned char buf
[BUF_LEN
])
142 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
143 || !PACKET_get_net_3(&pkt
, &i
)
145 || !PACKET_forward(&pkt
, BUF_LEN
- 6)
146 || !PACKET_get_net_3(&pkt
, &i
)
148 || PACKET_get_net_3(&pkt
, &i
)) {
149 fprintf(stderr
, "test_PACKET_get_net_3() failed\n");
156 static int test_PACKET_get_net_4(unsigned char buf
[BUF_LEN
])
161 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
162 || !PACKET_get_net_4(&pkt
, &i
)
164 || !PACKET_forward(&pkt
, BUF_LEN
- 8)
165 || !PACKET_get_net_4(&pkt
, &i
)
167 || PACKET_get_net_4(&pkt
, &i
)) {
168 fprintf(stderr
, "test_PACKET_get_net_4() failed\n");
175 static int test_PACKET_get_sub_packet(unsigned char buf
[BUF_LEN
])
180 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
181 || !PACKET_get_sub_packet(&pkt
, &subpkt
, 4)
182 || !PACKET_get_net_4(&subpkt
, &i
)
184 || PACKET_remaining(&subpkt
)
185 || !PACKET_forward(&pkt
, BUF_LEN
- 8)
186 || !PACKET_get_sub_packet(&pkt
, &subpkt
, 4)
187 || !PACKET_get_net_4(&subpkt
, &i
)
189 || PACKET_remaining(&subpkt
)
190 || PACKET_get_sub_packet(&pkt
, &subpkt
, 4)) {
191 fprintf(stderr
, "test_PACKET_get_sub_packet() failed\n");
198 static int test_PACKET_get_bytes(unsigned char buf
[BUF_LEN
])
200 unsigned char *bytes
;
203 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
204 || !PACKET_get_bytes(&pkt
, &bytes
, 4)
205 || bytes
[0] != 2 || bytes
[1] != 4
206 || bytes
[2] != 6 || bytes
[3] != 8
207 || PACKET_remaining(&pkt
) != BUF_LEN
-4
208 || !PACKET_forward(&pkt
, BUF_LEN
- 8)
209 || !PACKET_get_bytes(&pkt
, &bytes
, 4)
210 || bytes
[0] != 0xf8 || bytes
[1] != 0xfa
211 || bytes
[2] != 0xfc || bytes
[3] != 0xfe
212 || PACKET_remaining(&pkt
)) {
213 fprintf(stderr
, "test_PACKET_get_bytes() failed\n");
220 static int test_PACKET_copy_bytes(unsigned char buf
[BUF_LEN
])
222 unsigned char bytes
[4];
225 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
226 || !PACKET_copy_bytes(&pkt
, bytes
, 4)
227 || bytes
[0] != 2 || bytes
[1] != 4
228 || bytes
[2] != 6 || bytes
[3] != 8
229 || PACKET_remaining(&pkt
) != BUF_LEN
- 4
230 || !PACKET_forward(&pkt
, BUF_LEN
- 8)
231 || !PACKET_copy_bytes(&pkt
, bytes
, 4)
232 || bytes
[0] != 0xf8 || bytes
[1] != 0xfa
233 || bytes
[2] != 0xfc || bytes
[3] != 0xfe
234 || PACKET_remaining(&pkt
)) {
235 fprintf(stderr
, "test_PACKET_copy_bytes() failed\n");
242 static int test_PACKET_copy_all(unsigned char buf
[BUF_LEN
])
244 unsigned char tmp
[BUF_LEN
];
248 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
249 || !PACKET_copy_all(&pkt
, tmp
, BUF_LEN
, &len
)
251 || memcmp(buf
, tmp
, BUF_LEN
) != 0
252 || PACKET_remaining(&pkt
) != BUF_LEN
253 || PACKET_copy_all(&pkt
, tmp
, BUF_LEN
- 1, &len
)) {
254 fprintf(stderr
, "test_PACKET_copy_bytes() failed\n");
261 static int test_PACKET_memdup(unsigned char buf
[BUF_LEN
])
263 unsigned char *data
= NULL
;
267 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
268 || !PACKET_memdup(&pkt
, &data
, &len
)
270 || memcmp(data
, PACKET_data(&pkt
), len
)
271 || !PACKET_forward(&pkt
, 10)
272 || !PACKET_memdup(&pkt
, &data
, &len
)
273 || len
!= BUF_LEN
- 10
274 || memcmp(data
, PACKET_data(&pkt
), len
)) {
275 fprintf(stderr
, "test_PACKET_memdup() failed\n");
284 static int test_PACKET_strndup()
286 char buf
[10], buf2
[10];
290 memset(buf
, 'x', 10);
291 memset(buf2
, 'y', 10);
294 if ( !PACKET_buf_init(&pkt
, (unsigned char*)buf
, 10)
295 || !PACKET_strndup(&pkt
, &data
)
296 || strlen(data
) != 10
297 || strncmp(data
, buf
, 10)
298 || !PACKET_buf_init(&pkt
, (unsigned char*)buf2
, 10)
299 || !PACKET_strndup(&pkt
, &data
)
301 || strcmp(data
, buf2
)) {
302 fprintf(stderr
, "test_PACKET_strndup failed\n");
311 static int test_PACKET_forward(unsigned char buf
[BUF_LEN
])
316 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
317 || !PACKET_forward(&pkt
, 1)
318 || !PACKET_get_bytes(&pkt
, &byte
, 1)
320 || !PACKET_forward(&pkt
, BUF_LEN
- 3)
321 || !PACKET_get_bytes(&pkt
, &byte
, 1)
322 || byte
[0] != 0xfe) {
323 fprintf(stderr
, "test_PACKET_forward() failed\n");
330 static int test_PACKET_buf_init()
332 unsigned char buf
[BUF_LEN
];
335 /* Also tests PACKET_remaining() */
336 if ( !PACKET_buf_init(&pkt
, buf
, 4)
337 || PACKET_remaining(&pkt
) != 4
338 || !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
339 || PACKET_remaining(&pkt
) != BUF_LEN
340 || PACKET_buf_init(&pkt
, buf
, -1)) {
341 fprintf(stderr
, "test_PACKET_buf_init() failed\n");
348 static int test_PACKET_null_init()
352 PACKET_null_init(&pkt
);
353 if ( PACKET_remaining(&pkt
) != 0
354 || PACKET_forward(&pkt
, 1)) {
355 fprintf(stderr
, "test_PACKET_null_init() failed\n");
362 static int test_PACKET_equal(unsigned char buf
[BUF_LEN
])
366 if ( !PACKET_buf_init(&pkt
, buf
, 4)
367 || !PACKET_equal(&pkt
, buf
, 4)
368 || PACKET_equal(&pkt
, buf
+ 1, 4)
369 || !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
370 || !PACKET_equal(&pkt
, buf
, BUF_LEN
)
371 || PACKET_equal(&pkt
, buf
, BUF_LEN
- 1)
372 || PACKET_equal(&pkt
, buf
, BUF_LEN
+ 1)
373 || PACKET_equal(&pkt
, buf
, 0)) {
374 fprintf(stderr
, "test_PACKET_equal() failed\n");
381 static int test_PACKET_get_length_prefixed_1()
383 unsigned char buf
[BUF_LEN
];
384 const size_t len
= 16;
386 PACKET pkt
, short_pkt
, subpkt
;
389 for (i
= 1; i
< BUF_LEN
; i
++) {
390 buf
[i
] = (i
* 2) & 0xff;
393 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
394 || !PACKET_buf_init(&short_pkt
, buf
, len
)
395 || !PACKET_get_length_prefixed_1(&pkt
, &subpkt
)
396 || PACKET_remaining(&subpkt
) != len
397 || !PACKET_get_net_2(&subpkt
, &i
)
399 || PACKET_get_length_prefixed_1(&short_pkt
, &subpkt
)
400 || PACKET_remaining(&short_pkt
) != len
) {
401 fprintf(stderr
, "test_PACKET_get_length_prefixed_1() failed\n");
408 static int test_PACKET_get_length_prefixed_2()
410 unsigned char buf
[1024];
411 const size_t len
= 516; /* 0x0204 */
413 PACKET pkt
, short_pkt
, subpkt
;
415 for (i
= 1; i
<= 1024; i
++) {
416 buf
[i
-1] = (i
* 2) & 0xff;
419 if ( !PACKET_buf_init(&pkt
, buf
, 1024)
420 || !PACKET_buf_init(&short_pkt
, buf
, len
)
421 || !PACKET_get_length_prefixed_2(&pkt
, &subpkt
)
422 || PACKET_remaining(&subpkt
) != len
423 || !PACKET_get_net_2(&subpkt
, &i
)
425 || PACKET_get_length_prefixed_2(&short_pkt
, &subpkt
)
426 || PACKET_remaining(&short_pkt
) != len
) {
427 fprintf(stderr
, "test_PACKET_get_length_prefixed_2() failed\n");
434 static int test_PACKET_get_length_prefixed_3()
436 unsigned char buf
[1024];
437 const size_t len
= 516; /* 0x000204 */
439 PACKET pkt
, short_pkt
, subpkt
;
441 for (i
= 0; i
< 1024; i
++) {
442 buf
[i
] = (i
* 2) & 0xff;
445 if ( !PACKET_buf_init(&pkt
, buf
, 1024)
446 || !PACKET_buf_init(&short_pkt
, buf
, len
)
447 || !PACKET_get_length_prefixed_3(&pkt
, &subpkt
)
448 || PACKET_remaining(&subpkt
) != len
449 || !PACKET_get_net_2(&subpkt
, &i
)
451 || PACKET_get_length_prefixed_3(&short_pkt
, &subpkt
)
452 || PACKET_remaining(&short_pkt
) != len
) {
453 fprintf(stderr
, "test_PACKET_get_length_prefixed_3() failed\n");
460 int main(int argc
, char **argv
)
462 unsigned char buf
[BUF_LEN
];
465 for (i
=1; i
<=BUF_LEN
; i
++) {
466 buf
[i
-1] = (i
* 2) & 0xff;
470 if ( !test_PACKET_buf_init()
471 || !test_PACKET_null_init()
472 || !test_PACKET_remaining(buf
)
473 || !test_PACKET_equal(buf
)
474 || !test_PACKET_get_1(buf
)
475 || !test_PACKET_get_4(buf
)
476 || !test_PACKET_get_net_2(buf
)
477 || !test_PACKET_get_net_3(buf
)
478 || !test_PACKET_get_net_4(buf
)
479 || !test_PACKET_get_sub_packet(buf
)
480 || !test_PACKET_get_bytes(buf
)
481 || !test_PACKET_copy_bytes(buf
)
482 || !test_PACKET_copy_all(buf
)
483 || !test_PACKET_memdup(buf
)
484 || !test_PACKET_strndup()
485 || !test_PACKET_forward(buf
)
486 || !test_PACKET_get_length_prefixed_1()
487 || !test_PACKET_get_length_prefixed_2()
488 || !test_PACKET_get_length_prefixed_3()) {