]>
Commit | Line | Data |
---|---|---|
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 | ||
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) | |
bc6616a4 | 70 | || PACKET_remaining(pkt) != 0) { |
6fc2ef20 MC |
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) | |
44128847 | 84 | || i != 0x02 |
6fc2ef20 MC |
85 | || !PACKET_forward(pkt, BUF_LEN - 2) |
86 | || !PACKET_get_1(pkt, &i) | |
44128847 | 87 | || i != 0xfe |
6fc2ef20 MC |
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) | |
44128847 | 102 | || i != 0x08060402UL |
6fc2ef20 MC |
103 | || !PACKET_forward(pkt, BUF_LEN - 8) |
104 | || !PACKET_get_4(pkt, &i) | |
44128847 | 105 | || i != 0xfefcfaf8UL |
6fc2ef20 MC |
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) | |
44128847 | 120 | || i != 0x0204 |
6fc2ef20 MC |
121 | || !PACKET_forward(pkt, BUF_LEN - 4) |
122 | || !PACKET_get_net_2(pkt, &i) | |
44128847 | 123 | || i != 0xfcfe |
6fc2ef20 MC |
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 | { | |
04fe876b | 134 | unsigned long i; |
6fc2ef20 MC |
135 | |
136 | if ( !PACKET_goto_bookmark(pkt, start) | |
137 | || !PACKET_get_net_3(pkt, &i) | |
44128847 | 138 | || i != 0x020406UL |
6fc2ef20 MC |
139 | || !PACKET_forward(pkt, BUF_LEN - 6) |
140 | || !PACKET_get_net_3(pkt, &i) | |
44128847 | 141 | || i != 0xfafcfeUL |
6fc2ef20 MC |
142 | || PACKET_get_net_3(pkt, &i)) { |
143 | fprintf(stderr, "test_PACKET_get_net_3() failed\n"); | |
144 | return 0; | |
145 | } | |
146 | ||
147 | return 1; | |
148 | } | |
149 | ||
150 | static int test_PACKET_get_net_4(PACKET *pkt, size_t start) | |
151 | { | |
152 | unsigned long i; | |
153 | ||
154 | if ( !PACKET_goto_bookmark(pkt, start) | |
155 | || !PACKET_get_net_4(pkt, &i) | |
44128847 | 156 | || i != 0x02040608UL |
6fc2ef20 MC |
157 | || !PACKET_forward(pkt, BUF_LEN - 8) |
158 | || !PACKET_get_net_4(pkt, &i) | |
44128847 | 159 | || i != 0xf8fafcfeUL |
6fc2ef20 MC |
160 | || PACKET_get_net_4(pkt, &i)) { |
161 | fprintf(stderr, "test_PACKET_get_net_4() failed\n"); | |
162 | return 0; | |
163 | } | |
164 | ||
165 | return 1; | |
166 | } | |
167 | ||
168 | static int test_PACKET_get_sub_packet(PACKET *pkt, size_t start) | |
169 | { | |
170 | PACKET subpkt; | |
171 | unsigned long i; | |
172 | ||
173 | if ( !PACKET_goto_bookmark(pkt, start) | |
174 | || !PACKET_get_sub_packet(pkt, &subpkt, 4) | |
175 | || !PACKET_get_net_4(&subpkt, &i) | |
44128847 | 176 | || i != 0x02040608UL |
6fc2ef20 MC |
177 | || PACKET_remaining(&subpkt) |
178 | || !PACKET_forward(pkt, BUF_LEN - 8) | |
179 | || !PACKET_get_sub_packet(pkt, &subpkt, 4) | |
180 | || !PACKET_get_net_4(&subpkt, &i) | |
44128847 | 181 | || i != 0xf8fafcfeUL |
6fc2ef20 MC |
182 | || PACKET_remaining(&subpkt) |
183 | || PACKET_get_sub_packet(pkt, &subpkt, 4)) { | |
184 | fprintf(stderr, "test_PACKET_get_sub_packet() failed\n"); | |
185 | return 0; | |
186 | } | |
187 | ||
188 | return 1; | |
189 | } | |
190 | ||
191 | static int test_PACKET_get_bytes(PACKET *pkt, size_t start) | |
192 | { | |
193 | unsigned char *bytes; | |
194 | ||
195 | if ( !PACKET_goto_bookmark(pkt, start) | |
196 | || !PACKET_get_bytes(pkt, &bytes, 4) | |
44128847 MC |
197 | || bytes[0] != 2 || bytes[1] != 4 |
198 | || bytes[2] != 6 || bytes[3] != 8 | |
6fc2ef20 MC |
199 | || PACKET_remaining(pkt) != BUF_LEN -4 |
200 | || !PACKET_forward(pkt, BUF_LEN - 8) | |
201 | || !PACKET_get_bytes(pkt, &bytes, 4) | |
44128847 MC |
202 | || bytes[0] != 0xf8 || bytes[1] != 0xfa |
203 | || bytes[2] != 0xfc || bytes[3] != 0xfe | |
6fc2ef20 MC |
204 | || PACKET_remaining(pkt)) { |
205 | fprintf(stderr, "test_PACKET_get_bytes() failed\n"); | |
206 | return 0; | |
207 | } | |
208 | ||
209 | return 1; | |
210 | } | |
211 | ||
212 | static int test_PACKET_copy_bytes(PACKET *pkt, size_t start) | |
213 | { | |
214 | unsigned char bytes[4]; | |
215 | ||
216 | if ( !PACKET_goto_bookmark(pkt, start) | |
217 | || !PACKET_copy_bytes(pkt, bytes, 4) | |
44128847 MC |
218 | || bytes[0] != 2 || bytes[1] != 4 |
219 | || bytes[2] != 6 || bytes[3] != 8 | |
6fc2ef20 MC |
220 | || PACKET_remaining(pkt) != BUF_LEN - 4 |
221 | || !PACKET_forward(pkt, BUF_LEN - 8) | |
222 | || !PACKET_copy_bytes(pkt, bytes, 4) | |
44128847 MC |
223 | || bytes[0] != 0xf8 || bytes[1] != 0xfa |
224 | || bytes[2] != 0xfc || bytes[3] != 0xfe | |
6fc2ef20 MC |
225 | || PACKET_remaining(pkt)) { |
226 | fprintf(stderr, "test_PACKET_copy_bytes() failed\n"); | |
227 | return 0; | |
228 | } | |
229 | ||
230 | return 1; | |
231 | } | |
232 | ||
6d41fc80 EK |
233 | static int test_PACKET_memdup(PACKET *pkt, size_t start) |
234 | { | |
235 | unsigned char *data = NULL; | |
236 | size_t len; | |
237 | if ( !PACKET_goto_bookmark(pkt, start) | |
238 | || !PACKET_memdup(pkt, &data, &len) | |
239 | || len != BUF_LEN | |
240 | || memcmp(data, PACKET_data(pkt), len) | |
241 | || !PACKET_forward(pkt, 10) | |
242 | || !PACKET_memdup(pkt, &data, &len) | |
243 | || len != BUF_LEN - 10 | |
244 | || memcmp(data, PACKET_data(pkt), len) | |
245 | || !PACKET_back(pkt, 1) | |
246 | || !PACKET_memdup(pkt, &data, &len) | |
247 | || len != BUF_LEN - 9 | |
248 | || memcmp(data, PACKET_data(pkt), len)) { | |
249 | fprintf(stderr, "test_PACKET_memdup() failed\n"); | |
250 | OPENSSL_free(data); | |
251 | return 0; | |
252 | } | |
253 | ||
254 | OPENSSL_free(data); | |
255 | return 1; | |
256 | } | |
257 | ||
258 | static int test_PACKET_strndup() | |
259 | { | |
260 | char buf[10], buf2[10]; | |
261 | memset(buf, 'x', 10); | |
262 | memset(buf2, 'y', 10); | |
263 | buf2[5] = '\0'; | |
264 | char *data = NULL; | |
265 | PACKET pkt; | |
266 | ||
267 | if ( !PACKET_buf_init(&pkt, (unsigned char*)buf, 10) | |
268 | || !PACKET_strndup(&pkt, &data) | |
269 | || strlen(data) != 10 | |
270 | || strncmp(data, buf, 10) | |
271 | || !PACKET_buf_init(&pkt, (unsigned char*)buf2, 10) | |
272 | || !PACKET_strndup(&pkt, &data) | |
273 | || strlen(data) != 5 | |
274 | || strcmp(data, buf2)) { | |
275 | fprintf(stderr, "test_PACKET_strndup failed\n"); | |
276 | OPENSSL_free(data); | |
277 | return 0; | |
278 | } | |
279 | ||
280 | OPENSSL_free(data); | |
281 | return 1; | |
282 | } | |
283 | ||
6fc2ef20 MC |
284 | static int test_PACKET_move_funcs(PACKET *pkt, size_t start) |
285 | { | |
286 | unsigned char *byte; | |
287 | size_t bm; | |
288 | ||
289 | if ( !PACKET_goto_bookmark(pkt, start) | |
290 | || PACKET_back(pkt, 1) | |
291 | || !PACKET_forward(pkt, 1) | |
292 | || !PACKET_get_bytes(pkt, &byte, 1) | |
44128847 | 293 | || byte[0] != 4 |
6fc2ef20 MC |
294 | || !PACKET_get_bookmark(pkt, &bm) |
295 | || !PACKET_forward(pkt, BUF_LEN - 2) | |
296 | || PACKET_forward(pkt, 1) | |
297 | || !PACKET_back(pkt, 1) | |
298 | || !PACKET_get_bytes(pkt, &byte, 1) | |
44128847 | 299 | || byte[0] != 0xfe |
6fc2ef20 MC |
300 | || !PACKET_goto_bookmark(pkt, bm) |
301 | || !PACKET_get_bytes(pkt, &byte, 1) | |
44128847 | 302 | || byte[0] != 6) { |
6fc2ef20 MC |
303 | fprintf(stderr, "test_PACKET_move_funcs() failed\n"); |
304 | return 0; | |
305 | } | |
306 | ||
307 | return 1; | |
308 | } | |
309 | ||
310 | static int test_PACKET_buf_init() | |
311 | { | |
312 | unsigned char buf[BUF_LEN]; | |
313 | size_t len; | |
314 | PACKET pkt; | |
315 | ||
316 | /* Also tests PACKET_get_len() */ | |
317 | if ( !PACKET_buf_init(&pkt, buf, 4) | |
318 | || !PACKET_length(&pkt, &len) | |
319 | || len != 4 | |
320 | || !PACKET_buf_init(&pkt, buf, BUF_LEN) | |
321 | || !PACKET_length(&pkt, &len) | |
322 | || len != BUF_LEN | |
323 | || pkt.end - pkt.start != BUF_LEN | |
324 | || pkt.end < pkt.start | |
325 | || pkt.curr < pkt.start | |
326 | || pkt.curr > pkt.end | |
327 | || PACKET_buf_init(&pkt, buf, -1)) { | |
328 | fprintf(stderr, "test_PACKET_buf_init() failed\n"); | |
329 | return 0; | |
330 | } | |
331 | ||
332 | return 1; | |
333 | } | |
334 | ||
ec30e856 EK |
335 | static int test_PACKET_get_length_prefixed_1() |
336 | { | |
337 | unsigned char buf[BUF_LEN]; | |
338 | const size_t len = 16; | |
339 | unsigned int i; | |
340 | PACKET pkt, short_pkt, subpkt; | |
341 | ||
342 | buf[0] = len; | |
343 | for (i = 1; i < BUF_LEN; i++) { | |
344 | buf[i] = (i * 2) & 0xff; | |
345 | } | |
346 | ||
347 | if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) | |
348 | || !PACKET_buf_init(&short_pkt, buf, len) | |
349 | || !PACKET_get_length_prefixed_1(&pkt, &subpkt) | |
350 | || PACKET_remaining(&subpkt) != len | |
351 | || !PACKET_get_net_2(&subpkt, &i) | |
352 | || i != 0x0204 | |
353 | || PACKET_get_length_prefixed_1(&short_pkt, &subpkt) | |
354 | || PACKET_remaining(&short_pkt) != len) { | |
355 | fprintf(stderr, "test_PACKET_get_length_prefixed_1() failed\n"); | |
356 | return 0; | |
357 | } | |
358 | ||
359 | return 1; | |
360 | } | |
361 | ||
362 | static int test_PACKET_get_length_prefixed_2() | |
363 | { | |
364 | unsigned char buf[1024]; | |
365 | const size_t len = 516; /* 0x0204 */ | |
366 | unsigned int i; | |
367 | PACKET pkt, short_pkt, subpkt; | |
368 | ||
369 | for (i = 1; i <= 1024; i++) { | |
370 | buf[i-1] = (i * 2) & 0xff; | |
371 | } | |
372 | ||
373 | if ( !PACKET_buf_init(&pkt, buf, 1024) | |
374 | || !PACKET_buf_init(&short_pkt, buf, len) | |
375 | || !PACKET_get_length_prefixed_2(&pkt, &subpkt) | |
376 | || PACKET_remaining(&subpkt) != len | |
377 | || !PACKET_get_net_2(&subpkt, &i) | |
378 | || i != 0x0608 | |
379 | || PACKET_get_length_prefixed_2(&short_pkt, &subpkt) | |
380 | || PACKET_remaining(&short_pkt) != len) { | |
381 | fprintf(stderr, "test_PACKET_get_length_prefixed_2() failed\n"); | |
382 | return 0; | |
383 | } | |
384 | ||
385 | return 1; | |
386 | } | |
387 | ||
388 | static int test_PACKET_get_length_prefixed_3() | |
389 | { | |
390 | unsigned char buf[1024]; | |
391 | const size_t len = 516; /* 0x000204 */ | |
392 | unsigned int i; | |
393 | PACKET pkt, short_pkt, subpkt; | |
394 | ||
395 | for (i = 0; i < 1024; i++) { | |
396 | buf[i] = (i * 2) & 0xff; | |
397 | } | |
398 | ||
399 | if ( !PACKET_buf_init(&pkt, buf, 1024) | |
400 | || !PACKET_buf_init(&short_pkt, buf, len) | |
401 | || !PACKET_get_length_prefixed_3(&pkt, &subpkt) | |
402 | || PACKET_remaining(&subpkt) != len | |
403 | || !PACKET_get_net_2(&subpkt, &i) | |
404 | || i != 0x0608 | |
405 | || PACKET_get_length_prefixed_3(&short_pkt, &subpkt) | |
406 | || PACKET_remaining(&short_pkt) != len) { | |
407 | fprintf(stderr, "test_PACKET_get_length_prefixed_3() failed\n"); | |
408 | return 0; | |
409 | } | |
410 | ||
411 | return 1; | |
412 | } | |
413 | ||
6fc2ef20 MC |
414 | int main(int argc, char **argv) |
415 | { | |
416 | unsigned char buf[BUF_LEN]; | |
417 | unsigned int i; | |
418 | size_t start = 0; | |
419 | PACKET pkt; | |
420 | ||
421 | for (i=1; i<=BUF_LEN; i++) { | |
44128847 | 422 | buf[i-1] = (i * 2) & 0xff; |
6fc2ef20 MC |
423 | } |
424 | i = 0; | |
425 | ||
426 | if ( !PACKET_buf_init(&pkt, buf, BUF_LEN) | |
427 | || !PACKET_get_bookmark(&pkt, &start)) { | |
428 | fprintf(stderr, "setup failed\n"); | |
429 | return 0; | |
430 | } | |
431 | ||
432 | if ( !test_PACKET_buf_init() | |
433 | || !test_PACKET_remaining(&pkt) | |
434 | || !test_PACKET_get_1(&pkt, start) | |
435 | || !test_PACKET_get_4(&pkt, start) | |
436 | || !test_PACKET_get_net_2(&pkt, start) | |
437 | || !test_PACKET_get_net_3(&pkt, start) | |
438 | || !test_PACKET_get_net_4(&pkt, start) | |
439 | || !test_PACKET_get_sub_packet(&pkt, start) | |
440 | || !test_PACKET_get_bytes(&pkt, start) | |
441 | || !test_PACKET_copy_bytes(&pkt, start) | |
6d41fc80 EK |
442 | || !test_PACKET_memdup(&pkt, start) |
443 | || !test_PACKET_strndup() | |
ec30e856 EK |
444 | || !test_PACKET_move_funcs(&pkt, start) |
445 | || !test_PACKET_get_length_prefixed_1() | |
446 | || !test_PACKET_get_length_prefixed_2() | |
447 | || !test_PACKET_get_length_prefixed_3()) { | |
6fc2ef20 MC |
448 | return 1; |
449 | } | |
450 | printf("PASS\n"); | |
451 | return 0; | |
452 | } |