Fix a bug in the new PACKET implementation
[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(PACKET *pkt)
65 {
66     if (        PACKET_remaining(pkt) != BUF_LEN
67             || !PACKET_forward(pkt, BUF_LEN - 1)
68             ||  PACKET_remaining(pkt) != 1
69             || !PACKET_forward(pkt, 1)
70             ||  PACKET_remaining(pkt)) {
71         fprintf(stderr, "test_PACKET_remaining() failed\n");
72         return 0;
73     }
74
75     return 1;
76 }
77
78 static int test_PACKET_get_1(PACKET *pkt, size_t start)
79 {
80     unsigned int i;
81
82     if (       !PACKET_goto_bookmark(pkt, start)
83             || !PACKET_get_1(pkt, &i)
84             ||  i != 0x02
85             || !PACKET_forward(pkt, BUF_LEN - 2)
86             || !PACKET_get_1(pkt, &i)
87             ||  i != 0xfe
88             ||  PACKET_get_1(pkt, &i)) {
89         fprintf(stderr, "test_PACKET_get_1() failed\n");
90         return 0;
91     }
92
93     return 1;
94 }
95
96 static int test_PACKET_get_4(PACKET *pkt, size_t start)
97 {
98     unsigned long i;
99
100     if (       !PACKET_goto_bookmark(pkt, start)
101             || !PACKET_get_4(pkt, &i)
102             ||  i != 0x08060402UL
103             || !PACKET_forward(pkt, BUF_LEN - 8)
104             || !PACKET_get_4(pkt, &i)
105             ||  i != 0xfefcfaf8UL
106             ||  PACKET_get_4(pkt, &i)) {
107         fprintf(stderr, "test_PACKET_get_4() failed\n");
108         return 0;
109     }
110
111     return 1;
112 }
113
114 static int test_PACKET_get_net_2(PACKET *pkt, size_t start)
115 {
116     unsigned int i;
117
118     if (       !PACKET_goto_bookmark(pkt, start)
119             || !PACKET_get_net_2(pkt, &i)
120             ||  i != 0x0204
121             || !PACKET_forward(pkt, BUF_LEN - 4)
122             || !PACKET_get_net_2(pkt, &i)
123             ||  i != 0xfcfe
124             ||  PACKET_get_net_2(pkt, &i)) {
125         fprintf(stderr, "test_PACKET_get_net_2() failed\n");
126         return 0;
127     }
128
129     return 1;
130 }
131
132 static int test_PACKET_get_net_3(PACKET *pkt, size_t start)
133 {
134     unsigned long i;
135
136     if (       !PACKET_goto_bookmark(pkt, start)
137             || !PACKET_get_net_3(pkt, &i)
138             ||  i != 0x020406UL
139             || !PACKET_forward(pkt, BUF_LEN - 6)
140             || !PACKET_get_net_3(pkt, &i)
141             ||  i != 0xfafcfeUL
142             ||  PACKET_get_net_3(pkt, &i)) {
143         fprintf(stderr, "i is %ld\n", i);
144         fprintf(stderr, "test_PACKET_get_net_3() failed\n");
145         return 0;
146     }
147
148     return 1;
149 }
150
151 static int test_PACKET_get_net_4(PACKET *pkt, size_t start)
152 {
153     unsigned long i;
154
155     if (       !PACKET_goto_bookmark(pkt, start)
156             || !PACKET_get_net_4(pkt, &i)
157             ||  i != 0x02040608UL
158             || !PACKET_forward(pkt, BUF_LEN - 8)
159             || !PACKET_get_net_4(pkt, &i)
160             ||  i != 0xf8fafcfeUL
161             ||  PACKET_get_net_4(pkt, &i)) {
162         fprintf(stderr, "test_PACKET_get_net_4() failed\n");
163         return 0;
164     }
165
166     return 1;
167 }
168
169 static int test_PACKET_get_sub_packet(PACKET *pkt, size_t start)
170 {
171     PACKET subpkt;
172     unsigned long i;
173
174     if (       !PACKET_goto_bookmark(pkt, start)
175             || !PACKET_get_sub_packet(pkt, &subpkt, 4)
176             || !PACKET_get_net_4(&subpkt, &i)
177             ||  i != 0x02040608UL
178             ||  PACKET_remaining(&subpkt)
179             || !PACKET_forward(pkt, BUF_LEN - 8)
180             || !PACKET_get_sub_packet(pkt, &subpkt, 4)
181             || !PACKET_get_net_4(&subpkt, &i)
182             ||  i != 0xf8fafcfeUL
183             ||  PACKET_remaining(&subpkt)
184             ||  PACKET_get_sub_packet(pkt, &subpkt, 4)) {
185         fprintf(stderr, "test_PACKET_get_sub_packet() failed\n");
186         return 0;
187     }
188
189     return 1;
190 }
191
192 static int test_PACKET_get_bytes(PACKET *pkt, size_t start)
193 {
194     unsigned char *bytes;
195
196     if (       !PACKET_goto_bookmark(pkt, start)
197             || !PACKET_get_bytes(pkt, &bytes, 4)
198             ||  bytes[0] != 2 || bytes[1] != 4
199             ||  bytes[2] != 6 || bytes[3] != 8
200             ||  PACKET_remaining(pkt) != BUF_LEN -4
201             || !PACKET_forward(pkt, BUF_LEN - 8)
202             || !PACKET_get_bytes(pkt, &bytes, 4)
203             ||  bytes[0] != 0xf8 || bytes[1] != 0xfa
204             ||  bytes[2] != 0xfc || bytes[3] != 0xfe
205             ||  PACKET_remaining(pkt)) {
206         fprintf(stderr, "test_PACKET_get_bytes() failed\n");
207         return 0;
208     }
209
210     return 1;
211 }
212
213 static int test_PACKET_copy_bytes(PACKET *pkt, size_t start)
214 {
215     unsigned char bytes[4];
216
217     if (       !PACKET_goto_bookmark(pkt, start)
218             || !PACKET_copy_bytes(pkt, bytes, 4)
219             ||  bytes[0] != 2 || bytes[1] != 4
220             ||  bytes[2] != 6 || bytes[3] != 8
221             ||  PACKET_remaining(pkt) != BUF_LEN - 4
222             || !PACKET_forward(pkt, BUF_LEN - 8)
223             || !PACKET_copy_bytes(pkt, bytes, 4)
224             ||  bytes[0] != 0xf8 || bytes[1] != 0xfa
225             ||  bytes[2] != 0xfc || bytes[3] != 0xfe
226             ||  PACKET_remaining(pkt)) {
227         fprintf(stderr, "test_PACKET_copy_bytes() failed\n");
228         return 0;
229     }
230
231     return 1;
232 }
233
234 static int test_PACKET_move_funcs(PACKET *pkt, size_t start)
235 {
236     unsigned char *byte;
237     size_t bm;
238
239     if (       !PACKET_goto_bookmark(pkt, start)
240             ||  PACKET_back(pkt, 1)
241             || !PACKET_forward(pkt, 1)
242             || !PACKET_get_bytes(pkt, &byte, 1)
243             ||  byte[0] != 4
244             || !PACKET_get_bookmark(pkt, &bm)
245             || !PACKET_forward(pkt, BUF_LEN - 2)
246             ||  PACKET_forward(pkt, 1)
247             || !PACKET_back(pkt, 1)
248             || !PACKET_get_bytes(pkt, &byte, 1)
249             ||  byte[0] != 0xfe
250             || !PACKET_goto_bookmark(pkt, bm)
251             || !PACKET_get_bytes(pkt, &byte, 1)
252             ||  byte[0] != 6) {
253         fprintf(stderr, "test_PACKET_move_funcs() failed\n");
254         return 0;
255     }
256
257     return 1;
258 }
259
260 static int test_PACKET_buf_init()
261 {
262     unsigned char buf[BUF_LEN];
263     size_t len;
264     PACKET pkt;
265
266     /* Also tests PACKET_get_len() */
267     if (       !PACKET_buf_init(&pkt, buf, 4)
268             || !PACKET_length(&pkt, &len)
269             ||  len != 4
270             || !PACKET_buf_init(&pkt, buf, BUF_LEN)
271             || !PACKET_length(&pkt, &len)
272             ||  len != BUF_LEN
273             ||  pkt.end - pkt.start != BUF_LEN
274             ||  pkt.end < pkt.start
275             ||  pkt.curr < pkt.start
276             ||  pkt.curr > pkt.end
277             ||  PACKET_buf_init(&pkt, buf, -1)) {
278         fprintf(stderr, "test_PACKET_buf_init() failed\n");
279         return 0;
280         }
281
282     return 1;
283 }
284
285 int main(int argc, char **argv)
286 {
287     unsigned char buf[BUF_LEN];
288     unsigned int i;
289     size_t start = 0;
290     PACKET pkt;
291
292     for (i=1; i<=BUF_LEN; i++) {
293         buf[i-1] = (i * 2) & 0xff;
294     }
295     i = 0;
296
297     if (       !PACKET_buf_init(&pkt, buf, BUF_LEN)
298             || !PACKET_get_bookmark(&pkt, &start)) {
299         fprintf(stderr, "setup failed\n");
300         return 0;
301     }
302
303     if (       !test_PACKET_buf_init()
304             || !test_PACKET_remaining(&pkt)
305             || !test_PACKET_get_1(&pkt, start)
306             || !test_PACKET_get_4(&pkt, start)
307             || !test_PACKET_get_net_2(&pkt, start)
308             || !test_PACKET_get_net_3(&pkt, start)
309             || !test_PACKET_get_net_4(&pkt, start)
310             || !test_PACKET_get_sub_packet(&pkt, start)
311             || !test_PACKET_get_bytes(&pkt, start)
312             || !test_PACKET_copy_bytes(&pkt, start)
313             || !test_PACKET_move_funcs(&pkt, start)) {
314         return 1;
315     }
316     printf("PASS\n");
317     return 0;
318 }