ssl3_get_client_hello: rearrange logic
[openssl.git] / test / packettest.c
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
64 static int test_PACKET_remaining(unsigned char buf[BUF_LEN])
65 {
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) {
74         fprintf(stderr, "test_PACKET_remaining() failed\n");
75         return 0;
76     }
77
78     return 1;
79 }
80
81 static int test_PACKET_get_1(unsigned char buf[BUF_LEN])
82 {
83     unsigned int i;
84     PACKET pkt;
85
86     if (       !PACKET_buf_init(&pkt, buf, BUF_LEN)
87             || !PACKET_get_1(&pkt, &i)
88             ||  i != 0x02
89             || !PACKET_forward(&pkt, BUF_LEN - 2)
90             || !PACKET_get_1(&pkt, &i)
91             ||  i != 0xfe
92             ||  PACKET_get_1(&pkt, &i)) {
93         fprintf(stderr, "test_PACKET_get_1() failed\n");
94         return 0;
95     }
96
97     return 1;
98 }
99
100 static int test_PACKET_get_4(unsigned char buf[BUF_LEN])
101 {
102     unsigned long i;
103     PACKET pkt;
104
105     if (       !PACKET_buf_init(&pkt, buf, BUF_LEN)
106             || !PACKET_get_4(&pkt, &i)
107             ||  i != 0x08060402UL
108             || !PACKET_forward(&pkt, BUF_LEN - 8)
109             || !PACKET_get_4(&pkt, &i)
110             ||  i != 0xfefcfaf8UL
111             ||  PACKET_get_4(&pkt, &i)) {
112         fprintf(stderr, "test_PACKET_get_4() failed\n");
113         return 0;
114     }
115
116     return 1;
117 }
118
119 static int test_PACKET_get_net_2(unsigned char buf[BUF_LEN])
120 {
121     unsigned int i;
122     PACKET pkt;
123
124     if (       !PACKET_buf_init(&pkt, buf, BUF_LEN)
125             || !PACKET_get_net_2(&pkt, &i)
126             ||  i != 0x0204
127             || !PACKET_forward(&pkt, BUF_LEN - 4)
128             || !PACKET_get_net_2(&pkt, &i)
129             ||  i != 0xfcfe
130             ||  PACKET_get_net_2(&pkt, &i)) {
131         fprintf(stderr, "test_PACKET_get_net_2() failed\n");
132         return 0;
133     }
134
135     return 1;
136 }
137
138 static int test_PACKET_get_net_3(unsigned char buf[BUF_LEN])
139 {
140     unsigned long i;
141     PACKET pkt;
142
143     if (       !PACKET_buf_init(&pkt, buf, BUF_LEN)
144             || !PACKET_get_net_3(&pkt, &i)
145             ||  i != 0x020406UL
146             || !PACKET_forward(&pkt, BUF_LEN - 6)
147             || !PACKET_get_net_3(&pkt, &i)
148             ||  i != 0xfafcfeUL
149             ||  PACKET_get_net_3(&pkt, &i)) {
150         fprintf(stderr, "test_PACKET_get_net_3() failed\n");
151         return 0;
152     }
153
154     return 1;
155 }
156
157 static int test_PACKET_get_net_4(unsigned char buf[BUF_LEN])
158 {
159     unsigned long i;
160     PACKET pkt;
161
162     if (       !PACKET_buf_init(&pkt, buf, BUF_LEN)
163             || !PACKET_get_net_4(&pkt, &i)
164             ||  i != 0x02040608UL
165             || !PACKET_forward(&pkt, BUF_LEN - 8)
166             || !PACKET_get_net_4(&pkt, &i)
167             ||  i != 0xf8fafcfeUL
168             ||  PACKET_get_net_4(&pkt, &i)) {
169         fprintf(stderr, "test_PACKET_get_net_4() failed\n");
170         return 0;
171     }
172
173     return 1;
174 }
175
176 static int test_PACKET_get_sub_packet(unsigned char buf[BUF_LEN])
177 {
178     PACKET pkt, subpkt;
179     unsigned long i;
180
181     if (       !PACKET_buf_init(&pkt, buf, BUF_LEN)
182             || !PACKET_get_sub_packet(&pkt, &subpkt, 4)
183             || !PACKET_get_net_4(&subpkt, &i)
184             ||  i != 0x02040608UL
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)
189             ||  i != 0xf8fafcfeUL
190             ||  PACKET_remaining(&subpkt)
191             ||  PACKET_get_sub_packet(&pkt, &subpkt, 4)) {
192         fprintf(stderr, "test_PACKET_get_sub_packet() failed\n");
193         return 0;
194     }
195
196     return 1;
197 }
198
199 static int test_PACKET_get_bytes(unsigned char buf[BUF_LEN])
200 {
201     unsigned char *bytes;
202     PACKET pkt;
203
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");
215         return 0;
216     }
217
218     return 1;
219 }
220
221 static int test_PACKET_copy_bytes(unsigned char buf[BUF_LEN])
222 {
223     unsigned char bytes[4];
224     PACKET pkt;
225
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");
237         return 0;
238     }
239
240     return 1;
241 }
242
243 static int test_PACKET_memdup(unsigned char buf[BUF_LEN])
244 {
245     unsigned char *data = NULL;
246     size_t len;
247     PACKET pkt;
248
249     if (       !PACKET_buf_init(&pkt, buf, BUF_LEN)
250             || !PACKET_memdup(&pkt, &data, &len)
251             ||  len != BUF_LEN
252             ||  memcmp(data, PACKET_data(&pkt), len)
253             || !PACKET_forward(&pkt, 10)
254             || !PACKET_memdup(&pkt, &data, &len)
255             ||  len != BUF_LEN - 10
256             ||  memcmp(data, PACKET_data(&pkt), len)) {
257         fprintf(stderr, "test_PACKET_memdup() failed\n");
258         OPENSSL_free(data);
259         return 0;
260     }
261
262     OPENSSL_free(data);
263     return 1;
264 }
265
266 static int test_PACKET_strndup()
267 {
268     char buf[10], buf2[10];
269     char *data = NULL;
270     PACKET pkt;
271
272     memset(buf, 'x', 10);
273     memset(buf2, 'y', 10);
274     buf2[5] = '\0';
275
276     if (       !PACKET_buf_init(&pkt, (unsigned char*)buf, 10)
277             || !PACKET_strndup(&pkt, &data)
278             ||  strlen(data) != 10
279             ||  strncmp(data, buf, 10)
280             || !PACKET_buf_init(&pkt, (unsigned char*)buf2, 10)
281             || !PACKET_strndup(&pkt, &data)
282             ||  strlen(data) != 5
283             ||  strcmp(data, buf2)) {
284         fprintf(stderr, "test_PACKET_strndup failed\n");
285         OPENSSL_free(data);
286         return 0;
287     }
288
289     OPENSSL_free(data);
290     return 1;
291 }
292
293 static int test_PACKET_forward(unsigned char buf[BUF_LEN])
294 {
295     unsigned char *byte;
296     PACKET pkt;
297
298     if (       !PACKET_buf_init(&pkt, buf, BUF_LEN)
299             || !PACKET_forward(&pkt, 1)
300             || !PACKET_get_bytes(&pkt, &byte, 1)
301             ||  byte[0] != 4
302             || !PACKET_forward(&pkt, BUF_LEN - 3)
303             || !PACKET_get_bytes(&pkt, &byte, 1)
304             ||  byte[0] != 0xfe) {
305         fprintf(stderr, "test_PACKET_forward() failed\n");
306         return 0;
307     }
308
309     return 1;
310 }
311
312 static int test_PACKET_buf_init()
313 {
314     unsigned char buf[BUF_LEN];
315     PACKET pkt;
316
317     /* Also tests PACKET_get_len() */
318     if (       !PACKET_buf_init(&pkt, buf, 4)
319             ||  PACKET_remaining(&pkt) != 4
320             || !PACKET_buf_init(&pkt, buf, BUF_LEN)
321             ||  PACKET_remaining(&pkt) != BUF_LEN
322             ||  PACKET_buf_init(&pkt, buf, -1)) {
323         fprintf(stderr, "test_PACKET_buf_init() failed\n");
324         return 0;
325         }
326
327     return 1;
328 }
329
330 static int test_PACKET_null_init()
331 {
332     PACKET pkt;
333
334     PACKET_null_init(&pkt);
335     /* Also tests PACKET_get_len() */
336     if (       PACKET_remaining(&pkt) != 0
337             || PACKET_forward(&pkt, 1)) {
338         fprintf(stderr, "test_PACKET_null_init() failed\n");
339         return 0;
340         }
341
342     return 1;
343 }
344
345 static int test_PACKET_get_length_prefixed_1()
346 {
347     unsigned char buf[BUF_LEN];
348     const size_t len = 16;
349     unsigned int i;
350     PACKET pkt, short_pkt, subpkt;
351
352     buf[0] = len;
353     for (i = 1; i < BUF_LEN; i++) {
354         buf[i] = (i * 2) & 0xff;
355     }
356
357     if (       !PACKET_buf_init(&pkt, buf, BUF_LEN)
358             || !PACKET_buf_init(&short_pkt, buf, len)
359             || !PACKET_get_length_prefixed_1(&pkt, &subpkt)
360             ||  PACKET_remaining(&subpkt) != len
361             || !PACKET_get_net_2(&subpkt, &i)
362             ||  i != 0x0204
363             ||  PACKET_get_length_prefixed_1(&short_pkt, &subpkt)
364             ||  PACKET_remaining(&short_pkt) != len) {
365         fprintf(stderr, "test_PACKET_get_length_prefixed_1() failed\n");
366         return 0;
367     }
368
369     return 1;
370 }
371
372 static int test_PACKET_get_length_prefixed_2()
373 {
374     unsigned char buf[1024];
375     const size_t len = 516;  /* 0x0204 */
376     unsigned int i;
377     PACKET pkt, short_pkt, subpkt;
378
379     for (i = 1; i <= 1024; i++) {
380         buf[i-1] = (i * 2) & 0xff;
381     }
382
383     if (       !PACKET_buf_init(&pkt, buf, 1024)
384             || !PACKET_buf_init(&short_pkt, buf, len)
385             || !PACKET_get_length_prefixed_2(&pkt, &subpkt)
386             ||  PACKET_remaining(&subpkt) != len
387             || !PACKET_get_net_2(&subpkt, &i)
388             ||  i != 0x0608
389             ||  PACKET_get_length_prefixed_2(&short_pkt, &subpkt)
390             ||  PACKET_remaining(&short_pkt) != len) {
391         fprintf(stderr, "test_PACKET_get_length_prefixed_2() failed\n");
392         return 0;
393     }
394
395     return 1;
396 }
397
398 static int test_PACKET_get_length_prefixed_3()
399 {
400     unsigned char buf[1024];
401     const size_t len = 516;  /* 0x000204 */
402     unsigned int i;
403     PACKET pkt, short_pkt, subpkt;
404
405     for (i = 0; i < 1024; i++) {
406         buf[i] = (i * 2) & 0xff;
407     }
408
409     if (       !PACKET_buf_init(&pkt, buf, 1024)
410             || !PACKET_buf_init(&short_pkt, buf, len)
411             || !PACKET_get_length_prefixed_3(&pkt, &subpkt)
412             ||  PACKET_remaining(&subpkt) != len
413             || !PACKET_get_net_2(&subpkt, &i)
414             ||  i != 0x0608
415             ||  PACKET_get_length_prefixed_3(&short_pkt, &subpkt)
416             ||  PACKET_remaining(&short_pkt) != len) {
417         fprintf(stderr, "test_PACKET_get_length_prefixed_3() failed\n");
418         return 0;
419     }
420
421     return 1;
422 }
423
424 int main(int argc, char **argv)
425 {
426     unsigned char buf[BUF_LEN];
427     unsigned int i;
428
429     for (i=1; i<=BUF_LEN; i++) {
430         buf[i-1] = (i * 2) & 0xff;
431     }
432     i = 0;
433
434     if (       !test_PACKET_buf_init()
435             || !test_PACKET_null_init()
436             || !test_PACKET_remaining(buf)
437             || !test_PACKET_get_1(buf)
438             || !test_PACKET_get_4(buf)
439             || !test_PACKET_get_net_2(buf)
440             || !test_PACKET_get_net_3(buf)
441             || !test_PACKET_get_net_4(buf)
442             || !test_PACKET_get_sub_packet(buf)
443             || !test_PACKET_get_bytes(buf)
444             || !test_PACKET_copy_bytes(buf)
445             || !test_PACKET_memdup(buf)
446             || !test_PACKET_strndup()
447             || !test_PACKET_forward(buf)
448             || !test_PACKET_get_length_prefixed_1()
449             || !test_PACKET_get_length_prefixed_2()
450             || !test_PACKET_get_length_prefixed_3()) {
451         return 1;
452     }
453     printf("PASS\n");
454     return 0;
455 }