]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/packettest.c
1 /* test/packettest.c */
3 * Written by Matt Caswell for the OpenSSL project.
5 /* ====================================================================
6 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
60 #include "../ssl/packet_locl.h"
64 static int test_PACKET_remaining(unsigned char buf
[BUF_LEN
])
68 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
69 || PACKET_remaining(&pkt
) != BUF_LEN
70 || !PACKET_forward(&pkt
, BUF_LEN
- 1)
71 || PACKET_remaining(&pkt
) != 1
72 || !PACKET_forward(&pkt
, 1)
73 || PACKET_remaining(&pkt
) != 0) {
74 fprintf(stderr
, "test_PACKET_remaining() failed\n");
81 static int test_PACKET_get_1(unsigned char buf
[BUF_LEN
])
86 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
87 || !PACKET_get_1(&pkt
, &i
)
89 || !PACKET_forward(&pkt
, BUF_LEN
- 2)
90 || !PACKET_get_1(&pkt
, &i
)
92 || PACKET_get_1(&pkt
, &i
)) {
93 fprintf(stderr
, "test_PACKET_get_1() failed\n");
100 static int test_PACKET_get_4(unsigned char buf
[BUF_LEN
])
105 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
106 || !PACKET_get_4(&pkt
, &i
)
108 || !PACKET_forward(&pkt
, BUF_LEN
- 8)
109 || !PACKET_get_4(&pkt
, &i
)
111 || PACKET_get_4(&pkt
, &i
)) {
112 fprintf(stderr
, "test_PACKET_get_4() failed\n");
119 static int test_PACKET_get_net_2(unsigned char buf
[BUF_LEN
])
124 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
125 || !PACKET_get_net_2(&pkt
, &i
)
127 || !PACKET_forward(&pkt
, BUF_LEN
- 4)
128 || !PACKET_get_net_2(&pkt
, &i
)
130 || PACKET_get_net_2(&pkt
, &i
)) {
131 fprintf(stderr
, "test_PACKET_get_net_2() failed\n");
138 static int test_PACKET_get_net_3(unsigned char buf
[BUF_LEN
])
143 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
144 || !PACKET_get_net_3(&pkt
, &i
)
146 || !PACKET_forward(&pkt
, BUF_LEN
- 6)
147 || !PACKET_get_net_3(&pkt
, &i
)
149 || PACKET_get_net_3(&pkt
, &i
)) {
150 fprintf(stderr
, "test_PACKET_get_net_3() failed\n");
157 static int test_PACKET_get_net_4(unsigned char buf
[BUF_LEN
])
162 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
163 || !PACKET_get_net_4(&pkt
, &i
)
165 || !PACKET_forward(&pkt
, BUF_LEN
- 8)
166 || !PACKET_get_net_4(&pkt
, &i
)
168 || PACKET_get_net_4(&pkt
, &i
)) {
169 fprintf(stderr
, "test_PACKET_get_net_4() failed\n");
176 static int test_PACKET_get_sub_packet(unsigned char buf
[BUF_LEN
])
181 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
182 || !PACKET_get_sub_packet(&pkt
, &subpkt
, 4)
183 || !PACKET_get_net_4(&subpkt
, &i
)
185 || PACKET_remaining(&subpkt
)
186 || !PACKET_forward(&pkt
, BUF_LEN
- 8)
187 || !PACKET_get_sub_packet(&pkt
, &subpkt
, 4)
188 || !PACKET_get_net_4(&subpkt
, &i
)
190 || PACKET_remaining(&subpkt
)
191 || PACKET_get_sub_packet(&pkt
, &subpkt
, 4)) {
192 fprintf(stderr
, "test_PACKET_get_sub_packet() failed\n");
199 static int test_PACKET_get_bytes(unsigned char buf
[BUF_LEN
])
201 unsigned char *bytes
;
204 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
205 || !PACKET_get_bytes(&pkt
, &bytes
, 4)
206 || bytes
[0] != 2 || bytes
[1] != 4
207 || bytes
[2] != 6 || bytes
[3] != 8
208 || PACKET_remaining(&pkt
) != BUF_LEN
-4
209 || !PACKET_forward(&pkt
, BUF_LEN
- 8)
210 || !PACKET_get_bytes(&pkt
, &bytes
, 4)
211 || bytes
[0] != 0xf8 || bytes
[1] != 0xfa
212 || bytes
[2] != 0xfc || bytes
[3] != 0xfe
213 || PACKET_remaining(&pkt
)) {
214 fprintf(stderr
, "test_PACKET_get_bytes() failed\n");
221 static int test_PACKET_copy_bytes(unsigned char buf
[BUF_LEN
])
223 unsigned char bytes
[4];
226 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
227 || !PACKET_copy_bytes(&pkt
, bytes
, 4)
228 || bytes
[0] != 2 || bytes
[1] != 4
229 || bytes
[2] != 6 || bytes
[3] != 8
230 || PACKET_remaining(&pkt
) != BUF_LEN
- 4
231 || !PACKET_forward(&pkt
, BUF_LEN
- 8)
232 || !PACKET_copy_bytes(&pkt
, bytes
, 4)
233 || bytes
[0] != 0xf8 || bytes
[1] != 0xfa
234 || bytes
[2] != 0xfc || bytes
[3] != 0xfe
235 || PACKET_remaining(&pkt
)) {
236 fprintf(stderr
, "test_PACKET_copy_bytes() failed\n");
243 static int test_PACKET_copy_all(unsigned char buf
[BUF_LEN
])
245 unsigned char tmp
[BUF_LEN
];
249 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
250 || !PACKET_copy_all(&pkt
, tmp
, BUF_LEN
, &len
)
252 || memcmp(buf
, tmp
, BUF_LEN
) != 0
253 || PACKET_remaining(&pkt
) != BUF_LEN
254 || PACKET_copy_all(&pkt
, tmp
, BUF_LEN
- 1, &len
)) {
255 fprintf(stderr
, "test_PACKET_copy_bytes() failed\n");
262 static int test_PACKET_memdup(unsigned char buf
[BUF_LEN
])
264 unsigned char *data
= NULL
;
268 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
269 || !PACKET_memdup(&pkt
, &data
, &len
)
271 || memcmp(data
, PACKET_data(&pkt
), len
)
272 || !PACKET_forward(&pkt
, 10)
273 || !PACKET_memdup(&pkt
, &data
, &len
)
274 || len
!= BUF_LEN
- 10
275 || memcmp(data
, PACKET_data(&pkt
), len
)) {
276 fprintf(stderr
, "test_PACKET_memdup() failed\n");
285 static int test_PACKET_strndup()
287 char buf
[10], buf2
[10];
291 memset(buf
, 'x', 10);
292 memset(buf2
, 'y', 10);
295 if ( !PACKET_buf_init(&pkt
, (unsigned char*)buf
, 10)
296 || !PACKET_strndup(&pkt
, &data
)
297 || strlen(data
) != 10
298 || strncmp(data
, buf
, 10)
299 || !PACKET_buf_init(&pkt
, (unsigned char*)buf2
, 10)
300 || !PACKET_strndup(&pkt
, &data
)
302 || strcmp(data
, buf2
)) {
303 fprintf(stderr
, "test_PACKET_strndup failed\n");
312 static int test_PACKET_forward(unsigned char buf
[BUF_LEN
])
317 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
318 || !PACKET_forward(&pkt
, 1)
319 || !PACKET_get_bytes(&pkt
, &byte
, 1)
321 || !PACKET_forward(&pkt
, BUF_LEN
- 3)
322 || !PACKET_get_bytes(&pkt
, &byte
, 1)
323 || byte
[0] != 0xfe) {
324 fprintf(stderr
, "test_PACKET_forward() failed\n");
331 static int test_PACKET_buf_init()
333 unsigned char buf
[BUF_LEN
];
336 /* Also tests PACKET_remaining() */
337 if ( !PACKET_buf_init(&pkt
, buf
, 4)
338 || PACKET_remaining(&pkt
) != 4
339 || !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
340 || PACKET_remaining(&pkt
) != BUF_LEN
341 || PACKET_buf_init(&pkt
, buf
, -1)) {
342 fprintf(stderr
, "test_PACKET_buf_init() failed\n");
349 static int test_PACKET_null_init()
353 PACKET_null_init(&pkt
);
354 if ( PACKET_remaining(&pkt
) != 0
355 || PACKET_forward(&pkt
, 1)) {
356 fprintf(stderr
, "test_PACKET_null_init() failed\n");
363 static int test_PACKET_equal(unsigned char buf
[BUF_LEN
])
367 if ( !PACKET_buf_init(&pkt
, buf
, 4)
368 || !PACKET_equal(&pkt
, buf
, 4)
369 || PACKET_equal(&pkt
, buf
+ 1, 4)
370 || !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
371 || !PACKET_equal(&pkt
, buf
, BUF_LEN
)
372 || PACKET_equal(&pkt
, buf
, BUF_LEN
- 1)
373 || PACKET_equal(&pkt
, buf
, BUF_LEN
+ 1)
374 || PACKET_equal(&pkt
, buf
, 0)) {
375 fprintf(stderr
, "test_PACKET_equal() failed\n");
382 static int test_PACKET_get_length_prefixed_1()
384 unsigned char buf
[BUF_LEN
];
385 const size_t len
= 16;
387 PACKET pkt
, short_pkt
, subpkt
;
390 for (i
= 1; i
< BUF_LEN
; i
++) {
391 buf
[i
] = (i
* 2) & 0xff;
394 if ( !PACKET_buf_init(&pkt
, buf
, BUF_LEN
)
395 || !PACKET_buf_init(&short_pkt
, buf
, len
)
396 || !PACKET_get_length_prefixed_1(&pkt
, &subpkt
)
397 || PACKET_remaining(&subpkt
) != len
398 || !PACKET_get_net_2(&subpkt
, &i
)
400 || PACKET_get_length_prefixed_1(&short_pkt
, &subpkt
)
401 || PACKET_remaining(&short_pkt
) != len
) {
402 fprintf(stderr
, "test_PACKET_get_length_prefixed_1() failed\n");
409 static int test_PACKET_get_length_prefixed_2()
411 unsigned char buf
[1024];
412 const size_t len
= 516; /* 0x0204 */
414 PACKET pkt
, short_pkt
, subpkt
;
416 for (i
= 1; i
<= 1024; i
++) {
417 buf
[i
-1] = (i
* 2) & 0xff;
420 if ( !PACKET_buf_init(&pkt
, buf
, 1024)
421 || !PACKET_buf_init(&short_pkt
, buf
, len
)
422 || !PACKET_get_length_prefixed_2(&pkt
, &subpkt
)
423 || PACKET_remaining(&subpkt
) != len
424 || !PACKET_get_net_2(&subpkt
, &i
)
426 || PACKET_get_length_prefixed_2(&short_pkt
, &subpkt
)
427 || PACKET_remaining(&short_pkt
) != len
) {
428 fprintf(stderr
, "test_PACKET_get_length_prefixed_2() failed\n");
435 static int test_PACKET_get_length_prefixed_3()
437 unsigned char buf
[1024];
438 const size_t len
= 516; /* 0x000204 */
440 PACKET pkt
, short_pkt
, subpkt
;
442 for (i
= 0; i
< 1024; i
++) {
443 buf
[i
] = (i
* 2) & 0xff;
446 if ( !PACKET_buf_init(&pkt
, buf
, 1024)
447 || !PACKET_buf_init(&short_pkt
, buf
, len
)
448 || !PACKET_get_length_prefixed_3(&pkt
, &subpkt
)
449 || PACKET_remaining(&subpkt
) != len
450 || !PACKET_get_net_2(&subpkt
, &i
)
452 || PACKET_get_length_prefixed_3(&short_pkt
, &subpkt
)
453 || PACKET_remaining(&short_pkt
) != len
) {
454 fprintf(stderr
, "test_PACKET_get_length_prefixed_3() failed\n");
461 int main(int argc
, char **argv
)
463 unsigned char buf
[BUF_LEN
];
466 for (i
=1; i
<=BUF_LEN
; i
++) {
467 buf
[i
-1] = (i
* 2) & 0xff;
471 if ( !test_PACKET_buf_init()
472 || !test_PACKET_null_init()
473 || !test_PACKET_remaining(buf
)
474 || !test_PACKET_equal(buf
)
475 || !test_PACKET_get_1(buf
)
476 || !test_PACKET_get_4(buf
)
477 || !test_PACKET_get_net_2(buf
)
478 || !test_PACKET_get_net_3(buf
)
479 || !test_PACKET_get_net_4(buf
)
480 || !test_PACKET_get_sub_packet(buf
)
481 || !test_PACKET_get_bytes(buf
)
482 || !test_PACKET_copy_bytes(buf
)
483 || !test_PACKET_copy_all(buf
)
484 || !test_PACKET_memdup(buf
)
485 || !test_PACKET_strndup()
486 || !test_PACKET_forward(buf
)
487 || !test_PACKET_get_length_prefixed_1()
488 || !test_PACKET_get_length_prefixed_2()
489 || !test_PACKET_get_length_prefixed_3()) {