]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/packettest.c
Remove /* foo.c */ comments
[thirdparty/openssl.git] / test / packettest.c
CommitLineData
6fc2ef20
MC
1/*
2 * Written by Matt Caswell for the OpenSSL project.
3 */
4/* ====================================================================
5 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
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
17 * distribution.
18 *
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/)"
23 *
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.
28 *
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.
32 *
33 * 6. Redistributions of any form whatsoever must retain the following
34 * acknowledgment:
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
37 *
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 * ====================================================================
51 *
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).
55 *
56 */
57
58
59#include "../ssl/packet_locl.h"
60
61#define BUF_LEN 255
62
4bd16463 63static int test_PACKET_remaining(unsigned char buf[BUF_LEN])
6fc2ef20 64{
4bd16463
EK
65 PACKET pkt;
66
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) {
6fc2ef20
MC
73 fprintf(stderr, "test_PACKET_remaining() failed\n");
74 return 0;
75 }
76
77 return 1;
78}
79
4bd16463 80static int test_PACKET_get_1(unsigned char buf[BUF_LEN])
6fc2ef20
MC
81{
82 unsigned int i;
4bd16463 83 PACKET pkt;
6fc2ef20 84
4bd16463
EK
85 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
86 || !PACKET_get_1(&pkt, &i)
44128847 87 || i != 0x02
4bd16463
EK
88 || !PACKET_forward(&pkt, BUF_LEN - 2)
89 || !PACKET_get_1(&pkt, &i)
44128847 90 || i != 0xfe
4bd16463 91 || PACKET_get_1(&pkt, &i)) {
6fc2ef20
MC
92 fprintf(stderr, "test_PACKET_get_1() failed\n");
93 return 0;
94 }
95
96 return 1;
97}
98
4bd16463 99static int test_PACKET_get_4(unsigned char buf[BUF_LEN])
6fc2ef20
MC
100{
101 unsigned long i;
4bd16463 102 PACKET pkt;
6fc2ef20 103
4bd16463
EK
104 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
105 || !PACKET_get_4(&pkt, &i)
44128847 106 || i != 0x08060402UL
4bd16463
EK
107 || !PACKET_forward(&pkt, BUF_LEN - 8)
108 || !PACKET_get_4(&pkt, &i)
44128847 109 || i != 0xfefcfaf8UL
4bd16463 110 || PACKET_get_4(&pkt, &i)) {
6fc2ef20
MC
111 fprintf(stderr, "test_PACKET_get_4() failed\n");
112 return 0;
113 }
114
115 return 1;
116}
117
4bd16463 118static int test_PACKET_get_net_2(unsigned char buf[BUF_LEN])
6fc2ef20
MC
119{
120 unsigned int i;
4bd16463 121 PACKET pkt;
6fc2ef20 122
4bd16463
EK
123 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
124 || !PACKET_get_net_2(&pkt, &i)
44128847 125 || i != 0x0204
4bd16463
EK
126 || !PACKET_forward(&pkt, BUF_LEN - 4)
127 || !PACKET_get_net_2(&pkt, &i)
44128847 128 || i != 0xfcfe
4bd16463 129 || PACKET_get_net_2(&pkt, &i)) {
6fc2ef20
MC
130 fprintf(stderr, "test_PACKET_get_net_2() failed\n");
131 return 0;
132 }
133
134 return 1;
135}
136
4bd16463 137static int test_PACKET_get_net_3(unsigned char buf[BUF_LEN])
6fc2ef20 138{
04fe876b 139 unsigned long i;
4bd16463 140 PACKET pkt;
6fc2ef20 141
4bd16463
EK
142 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
143 || !PACKET_get_net_3(&pkt, &i)
44128847 144 || i != 0x020406UL
4bd16463
EK
145 || !PACKET_forward(&pkt, BUF_LEN - 6)
146 || !PACKET_get_net_3(&pkt, &i)
44128847 147 || i != 0xfafcfeUL
4bd16463 148 || PACKET_get_net_3(&pkt, &i)) {
6fc2ef20
MC
149 fprintf(stderr, "test_PACKET_get_net_3() failed\n");
150 return 0;
151 }
152
153 return 1;
154}
155
4bd16463 156static int test_PACKET_get_net_4(unsigned char buf[BUF_LEN])
6fc2ef20
MC
157{
158 unsigned long i;
4bd16463 159 PACKET pkt;
6fc2ef20 160
4bd16463
EK
161 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
162 || !PACKET_get_net_4(&pkt, &i)
44128847 163 || i != 0x02040608UL
4bd16463
EK
164 || !PACKET_forward(&pkt, BUF_LEN - 8)
165 || !PACKET_get_net_4(&pkt, &i)
44128847 166 || i != 0xf8fafcfeUL
4bd16463 167 || PACKET_get_net_4(&pkt, &i)) {
6fc2ef20
MC
168 fprintf(stderr, "test_PACKET_get_net_4() failed\n");
169 return 0;
170 }
171
172 return 1;
173}
174
4bd16463 175static int test_PACKET_get_sub_packet(unsigned char buf[BUF_LEN])
6fc2ef20 176{
4bd16463 177 PACKET pkt, subpkt;
6fc2ef20
MC
178 unsigned long i;
179
4bd16463
EK
180 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
181 || !PACKET_get_sub_packet(&pkt, &subpkt, 4)
6fc2ef20 182 || !PACKET_get_net_4(&subpkt, &i)
44128847 183 || i != 0x02040608UL
6fc2ef20 184 || PACKET_remaining(&subpkt)
4bd16463
EK
185 || !PACKET_forward(&pkt, BUF_LEN - 8)
186 || !PACKET_get_sub_packet(&pkt, &subpkt, 4)
6fc2ef20 187 || !PACKET_get_net_4(&subpkt, &i)
44128847 188 || i != 0xf8fafcfeUL
6fc2ef20 189 || PACKET_remaining(&subpkt)
4bd16463 190 || PACKET_get_sub_packet(&pkt, &subpkt, 4)) {
6fc2ef20
MC
191 fprintf(stderr, "test_PACKET_get_sub_packet() failed\n");
192 return 0;
193 }
194
195 return 1;
196}
197
4bd16463 198static int test_PACKET_get_bytes(unsigned char buf[BUF_LEN])
6fc2ef20
MC
199{
200 unsigned char *bytes;
4bd16463 201 PACKET pkt;
6fc2ef20 202
4bd16463
EK
203 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
204 || !PACKET_get_bytes(&pkt, &bytes, 4)
44128847
MC
205 || bytes[0] != 2 || bytes[1] != 4
206 || bytes[2] != 6 || bytes[3] != 8
4bd16463
EK
207 || PACKET_remaining(&pkt) != BUF_LEN -4
208 || !PACKET_forward(&pkt, BUF_LEN - 8)
209 || !PACKET_get_bytes(&pkt, &bytes, 4)
44128847
MC
210 || bytes[0] != 0xf8 || bytes[1] != 0xfa
211 || bytes[2] != 0xfc || bytes[3] != 0xfe
4bd16463 212 || PACKET_remaining(&pkt)) {
6fc2ef20
MC
213 fprintf(stderr, "test_PACKET_get_bytes() failed\n");
214 return 0;
215 }
216
217 return 1;
218}
219
4bd16463 220static int test_PACKET_copy_bytes(unsigned char buf[BUF_LEN])
6fc2ef20
MC
221{
222 unsigned char bytes[4];
4bd16463 223 PACKET pkt;
6fc2ef20 224
4bd16463
EK
225 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
226 || !PACKET_copy_bytes(&pkt, bytes, 4)
44128847
MC
227 || bytes[0] != 2 || bytes[1] != 4
228 || bytes[2] != 6 || bytes[3] != 8
4bd16463
EK
229 || PACKET_remaining(&pkt) != BUF_LEN - 4
230 || !PACKET_forward(&pkt, BUF_LEN - 8)
231 || !PACKET_copy_bytes(&pkt, bytes, 4)
44128847
MC
232 || bytes[0] != 0xf8 || bytes[1] != 0xfa
233 || bytes[2] != 0xfc || bytes[3] != 0xfe
4bd16463 234 || PACKET_remaining(&pkt)) {
6fc2ef20
MC
235 fprintf(stderr, "test_PACKET_copy_bytes() failed\n");
236 return 0;
237 }
238
239 return 1;
240}
241
67202973
EK
242static int test_PACKET_copy_all(unsigned char buf[BUF_LEN])
243{
2d284623 244 unsigned char tmp[BUF_LEN];
67202973
EK
245 PACKET pkt;
246 size_t len;
247
248 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
2d284623 249 || !PACKET_copy_all(&pkt, tmp, BUF_LEN, &len)
67202973 250 || len != BUF_LEN
2d284623 251 || memcmp(buf, tmp, BUF_LEN) != 0
67202973 252 || PACKET_remaining(&pkt) != BUF_LEN
2d284623 253 || PACKET_copy_all(&pkt, tmp, BUF_LEN - 1, &len)) {
67202973
EK
254 fprintf(stderr, "test_PACKET_copy_bytes() failed\n");
255 return 0;
256 }
257
258 return 1;
259}
260
4bd16463 261static int test_PACKET_memdup(unsigned char buf[BUF_LEN])
6d41fc80
EK
262{
263 unsigned char *data = NULL;
264 size_t len;
4bd16463
EK
265 PACKET pkt;
266
267 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
268 || !PACKET_memdup(&pkt, &data, &len)
6d41fc80 269 || len != BUF_LEN
4bd16463
EK
270 || memcmp(data, PACKET_data(&pkt), len)
271 || !PACKET_forward(&pkt, 10)
272 || !PACKET_memdup(&pkt, &data, &len)
6d41fc80 273 || len != BUF_LEN - 10
88f84eb2 274 || memcmp(data, PACKET_data(&pkt), len)) {
6d41fc80
EK
275 fprintf(stderr, "test_PACKET_memdup() failed\n");
276 OPENSSL_free(data);
277 return 0;
278 }
279
280 OPENSSL_free(data);
281 return 1;
282}
283
284static int test_PACKET_strndup()
285{
286 char buf[10], buf2[10];
2dcac136
DSH
287 char *data = NULL;
288 PACKET pkt;
289
6d41fc80
EK
290 memset(buf, 'x', 10);
291 memset(buf2, 'y', 10);
292 buf2[5] = '\0';
6d41fc80
EK
293
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)
300 || strlen(data) != 5
301 || strcmp(data, buf2)) {
302 fprintf(stderr, "test_PACKET_strndup failed\n");
303 OPENSSL_free(data);
304 return 0;
305 }
306
307 OPENSSL_free(data);
308 return 1;
309}
310
88f84eb2 311static int test_PACKET_forward(unsigned char buf[BUF_LEN])
6fc2ef20
MC
312{
313 unsigned char *byte;
4bd16463 314 PACKET pkt;
6fc2ef20 315
4bd16463 316 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
4bd16463
EK
317 || !PACKET_forward(&pkt, 1)
318 || !PACKET_get_bytes(&pkt, &byte, 1)
44128847 319 || byte[0] != 4
88f84eb2 320 || !PACKET_forward(&pkt, BUF_LEN - 3)
4bd16463
EK
321 || !PACKET_get_bytes(&pkt, &byte, 1)
322 || byte[0] != 0xfe) {
88f84eb2 323 fprintf(stderr, "test_PACKET_forward() failed\n");
6fc2ef20
MC
324 return 0;
325 }
326
327 return 1;
328}
329
330static int test_PACKET_buf_init()
331{
332 unsigned char buf[BUF_LEN];
6fc2ef20
MC
333 PACKET pkt;
334
67202973 335 /* Also tests PACKET_remaining() */
6fc2ef20 336 if ( !PACKET_buf_init(&pkt, buf, 4)
6a12a574 337 || PACKET_remaining(&pkt) != 4
6fc2ef20 338 || !PACKET_buf_init(&pkt, buf, BUF_LEN)
6a12a574 339 || PACKET_remaining(&pkt) != BUF_LEN
6fc2ef20
MC
340 || PACKET_buf_init(&pkt, buf, -1)) {
341 fprintf(stderr, "test_PACKET_buf_init() failed\n");
342 return 0;
343 }
344
345 return 1;
346}
347
b3e2272c
EK
348static int test_PACKET_null_init()
349{
350 PACKET pkt;
351
352 PACKET_null_init(&pkt);
b3e2272c
EK
353 if ( PACKET_remaining(&pkt) != 0
354 || PACKET_forward(&pkt, 1)) {
355 fprintf(stderr, "test_PACKET_null_init() failed\n");
356 return 0;
357 }
358
359 return 1;
360}
361
31011544
EK
362static int test_PACKET_equal(unsigned char buf[BUF_LEN])
363{
364 PACKET pkt;
365
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");
375 return 0;
376 }
377
378 return 1;
379}
380
ec30e856
EK
381static int test_PACKET_get_length_prefixed_1()
382{
383 unsigned char buf[BUF_LEN];
384 const size_t len = 16;
385 unsigned int i;
386 PACKET pkt, short_pkt, subpkt;
387
388 buf[0] = len;
389 for (i = 1; i < BUF_LEN; i++) {
390 buf[i] = (i * 2) & 0xff;
391 }
392
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)
398 || i != 0x0204
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");
402 return 0;
403 }
404
405 return 1;
406}
407
408static int test_PACKET_get_length_prefixed_2()
409{
410 unsigned char buf[1024];
411 const size_t len = 516; /* 0x0204 */
412 unsigned int i;
413 PACKET pkt, short_pkt, subpkt;
414
415 for (i = 1; i <= 1024; i++) {
416 buf[i-1] = (i * 2) & 0xff;
417 }
418
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)
424 || i != 0x0608
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");
428 return 0;
429 }
430
431 return 1;
432}
433
434static int test_PACKET_get_length_prefixed_3()
435{
436 unsigned char buf[1024];
437 const size_t len = 516; /* 0x000204 */
438 unsigned int i;
439 PACKET pkt, short_pkt, subpkt;
440
441 for (i = 0; i < 1024; i++) {
442 buf[i] = (i * 2) & 0xff;
443 }
444
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)
450 || i != 0x0608
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");
454 return 0;
455 }
456
457 return 1;
458}
459
6fc2ef20
MC
460int main(int argc, char **argv)
461{
462 unsigned char buf[BUF_LEN];
463 unsigned int i;
6fc2ef20
MC
464
465 for (i=1; i<=BUF_LEN; i++) {
44128847 466 buf[i-1] = (i * 2) & 0xff;
6fc2ef20
MC
467 }
468 i = 0;
469
6fc2ef20 470 if ( !test_PACKET_buf_init()
b3e2272c 471 || !test_PACKET_null_init()
4bd16463 472 || !test_PACKET_remaining(buf)
31011544 473 || !test_PACKET_equal(buf)
4bd16463
EK
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)
67202973 482 || !test_PACKET_copy_all(buf)
4bd16463 483 || !test_PACKET_memdup(buf)
6d41fc80 484 || !test_PACKET_strndup()
88f84eb2 485 || !test_PACKET_forward(buf)
ec30e856
EK
486 || !test_PACKET_get_length_prefixed_1()
487 || !test_PACKET_get_length_prefixed_2()
488 || !test_PACKET_get_length_prefixed_3()) {
6fc2ef20
MC
489 return 1;
490 }
491 printf("PASS\n");
492 return 0;
493}