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