constify PACKET
[openssl.git] / test / packettest.c
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
63 static int test_PACKET_remaining(unsigned char buf[BUF_LEN])
64 {
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) {
73         fprintf(stderr, "test_PACKET_remaining() failed\n");
74         return 0;
75     }
76
77     return 1;
78 }
79
80 static int test_PACKET_get_1(unsigned char buf[BUF_LEN])
81 {
82     unsigned int i;
83     PACKET pkt;
84
85     if (       !PACKET_buf_init(&pkt, buf, BUF_LEN)
86             || !PACKET_get_1(&pkt, &i)
87             ||  i != 0x02
88             || !PACKET_forward(&pkt, BUF_LEN - 2)
89             || !PACKET_get_1(&pkt, &i)
90             ||  i != 0xfe
91             ||  PACKET_get_1(&pkt, &i)) {
92         fprintf(stderr, "test_PACKET_get_1() failed\n");
93         return 0;
94     }
95
96     return 1;
97 }
98
99 static int test_PACKET_get_4(unsigned char buf[BUF_LEN])
100 {
101     unsigned long i;
102     PACKET pkt;
103
104     if (       !PACKET_buf_init(&pkt, buf, BUF_LEN)
105             || !PACKET_get_4(&pkt, &i)
106             ||  i != 0x08060402UL
107             || !PACKET_forward(&pkt, BUF_LEN - 8)
108             || !PACKET_get_4(&pkt, &i)
109             ||  i != 0xfefcfaf8UL
110             ||  PACKET_get_4(&pkt, &i)) {
111         fprintf(stderr, "test_PACKET_get_4() failed\n");
112         return 0;
113     }
114
115     return 1;
116 }
117
118 static int test_PACKET_get_net_2(unsigned char buf[BUF_LEN])
119 {
120     unsigned int i;
121     PACKET pkt;
122
123     if (       !PACKET_buf_init(&pkt, buf, BUF_LEN)
124             || !PACKET_get_net_2(&pkt, &i)
125             ||  i != 0x0204
126             || !PACKET_forward(&pkt, BUF_LEN - 4)
127             || !PACKET_get_net_2(&pkt, &i)
128             ||  i != 0xfcfe
129             ||  PACKET_get_net_2(&pkt, &i)) {
130         fprintf(stderr, "test_PACKET_get_net_2() failed\n");
131         return 0;
132     }
133
134     return 1;
135 }
136
137 static int test_PACKET_get_net_3(unsigned char buf[BUF_LEN])
138 {
139     unsigned long i;
140     PACKET pkt;
141
142     if (       !PACKET_buf_init(&pkt, buf, BUF_LEN)
143             || !PACKET_get_net_3(&pkt, &i)
144             ||  i != 0x020406UL
145             || !PACKET_forward(&pkt, BUF_LEN - 6)
146             || !PACKET_get_net_3(&pkt, &i)
147             ||  i != 0xfafcfeUL
148             ||  PACKET_get_net_3(&pkt, &i)) {
149         fprintf(stderr, "test_PACKET_get_net_3() failed\n");
150         return 0;
151     }
152
153     return 1;
154 }
155
156 static int test_PACKET_get_net_4(unsigned char buf[BUF_LEN])
157 {
158     unsigned long i;
159     PACKET pkt;
160
161     if (       !PACKET_buf_init(&pkt, buf, BUF_LEN)
162             || !PACKET_get_net_4(&pkt, &i)
163             ||  i != 0x02040608UL
164             || !PACKET_forward(&pkt, BUF_LEN - 8)
165             || !PACKET_get_net_4(&pkt, &i)
166             ||  i != 0xf8fafcfeUL
167             ||  PACKET_get_net_4(&pkt, &i)) {
168         fprintf(stderr, "test_PACKET_get_net_4() failed\n");
169         return 0;
170     }
171
172     return 1;
173 }
174
175 static int test_PACKET_get_sub_packet(unsigned char buf[BUF_LEN])
176 {
177     PACKET pkt, subpkt;
178     unsigned long i;
179
180     if (       !PACKET_buf_init(&pkt, buf, BUF_LEN)
181             || !PACKET_get_sub_packet(&pkt, &subpkt, 4)
182             || !PACKET_get_net_4(&subpkt, &i)
183             ||  i != 0x02040608UL
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)
188             ||  i != 0xf8fafcfeUL
189             ||  PACKET_remaining(&subpkt)
190             ||  PACKET_get_sub_packet(&pkt, &subpkt, 4)) {
191         fprintf(stderr, "test_PACKET_get_sub_packet() failed\n");
192         return 0;
193     }
194
195     return 1;
196 }
197
198 static int test_PACKET_get_bytes(unsigned char buf[BUF_LEN])
199 {
200     const unsigned char *bytes;
201     PACKET pkt;
202
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");
214         return 0;
215     }
216
217     return 1;
218 }
219
220 static int test_PACKET_copy_bytes(unsigned char buf[BUF_LEN])
221 {
222     unsigned char bytes[4];
223     PACKET pkt;
224
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");
236         return 0;
237     }
238
239     return 1;
240 }
241
242 static int test_PACKET_copy_all(unsigned char buf[BUF_LEN])
243 {
244     unsigned char tmp[BUF_LEN];
245     PACKET pkt;
246     size_t len;
247
248     if (       !PACKET_buf_init(&pkt, buf, BUF_LEN)
249                || !PACKET_copy_all(&pkt, tmp, BUF_LEN, &len)
250                || len != BUF_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");
255         return 0;
256     }
257
258     return 1;
259 }
260
261 static int test_PACKET_memdup(unsigned char buf[BUF_LEN])
262 {
263     unsigned char *data = NULL;
264     size_t len;
265     PACKET pkt;
266
267     if (       !PACKET_buf_init(&pkt, buf, BUF_LEN)
268             || !PACKET_memdup(&pkt, &data, &len)
269             ||  len != BUF_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");
276         OPENSSL_free(data);
277         return 0;
278     }
279
280     OPENSSL_free(data);
281     return 1;
282 }
283
284 static int test_PACKET_strndup()
285 {
286     char buf[10], buf2[10];
287     char *data = NULL;
288     PACKET pkt;
289
290     memset(buf, 'x', 10);
291     memset(buf2, 'y', 10);
292     buf2[5] = '\0';
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
311 static int test_PACKET_forward(unsigned char buf[BUF_LEN])
312 {
313     const unsigned char *byte;
314     PACKET pkt;
315
316     if (       !PACKET_buf_init(&pkt, buf, BUF_LEN)
317             || !PACKET_forward(&pkt, 1)
318             || !PACKET_get_bytes(&pkt, &byte, 1)
319             ||  byte[0] != 4
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");
324         return 0;
325     }
326
327     return 1;
328 }
329
330 static int test_PACKET_buf_init()
331 {
332     unsigned char buf[BUF_LEN];
333     PACKET pkt;
334
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");
342         return 0;
343         }
344
345     return 1;
346 }
347
348 static int test_PACKET_null_init()
349 {
350     PACKET pkt;
351
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");
356         return 0;
357         }
358
359     return 1;
360 }
361
362 static 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
381 static 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
408 static 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
434 static 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
460 int main(int argc, char **argv)
461 {
462     unsigned char buf[BUF_LEN];
463     unsigned int i;
464
465     for (i=1; i<=BUF_LEN; i++) {
466         buf[i-1] = (i * 2) & 0xff;
467     }
468     i = 0;
469
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()) {
489         return 1;
490     }
491     printf("PASS\n");
492     return 0;
493 }