1 /* test/packettest.c */
3 * Written by Matt Caswell for the OpenSSL project.
5 /* ====================================================================
6 * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
60 #include "../ssl/packet_locl.h"
64 static int test_PACKET_remaining(PACKET *pkt)
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");
78 static int test_PACKET_get_1(PACKET *pkt, size_t start)
82 if ( !PACKET_goto_bookmark(pkt, start)
83 || !PACKET_get_1(pkt, &i)
85 || !PACKET_forward(pkt, BUF_LEN - 2)
86 || !PACKET_get_1(pkt, &i)
88 || PACKET_get_1(pkt, &i)) {
89 fprintf(stderr, "test_PACKET_get_1() failed\n");
96 static int test_PACKET_get_4(PACKET *pkt, size_t start)
100 if ( !PACKET_goto_bookmark(pkt, start)
101 || !PACKET_get_4(pkt, &i)
103 || !PACKET_forward(pkt, BUF_LEN - 8)
104 || !PACKET_get_4(pkt, &i)
106 || PACKET_get_4(pkt, &i)) {
107 fprintf(stderr, "test_PACKET_get_4() failed\n");
114 static int test_PACKET_get_net_2(PACKET *pkt, size_t start)
118 if ( !PACKET_goto_bookmark(pkt, start)
119 || !PACKET_get_net_2(pkt, &i)
121 || !PACKET_forward(pkt, BUF_LEN - 4)
122 || !PACKET_get_net_2(pkt, &i)
124 || PACKET_get_net_2(pkt, &i)) {
125 fprintf(stderr, "test_PACKET_get_net_2() failed\n");
132 static int test_PACKET_get_net_3(PACKET *pkt, size_t start)
136 if ( !PACKET_goto_bookmark(pkt, start)
137 || !PACKET_get_net_3(pkt, &i)
139 || !PACKET_forward(pkt, BUF_LEN - 6)
140 || !PACKET_get_net_3(pkt, &i)
142 || PACKET_get_net_3(pkt, &i)) {
143 fprintf(stderr, "test_PACKET_get_net_3() failed\n");
150 static int test_PACKET_get_net_4(PACKET *pkt, size_t start)
154 if ( !PACKET_goto_bookmark(pkt, start)
155 || !PACKET_get_net_4(pkt, &i)
157 || !PACKET_forward(pkt, BUF_LEN - 8)
158 || !PACKET_get_net_4(pkt, &i)
160 || PACKET_get_net_4(pkt, &i)) {
161 fprintf(stderr, "test_PACKET_get_net_4() failed\n");
168 static int test_PACKET_get_sub_packet(PACKET *pkt, size_t start)
173 if ( !PACKET_goto_bookmark(pkt, start)
174 || !PACKET_get_sub_packet(pkt, &subpkt, 4)
175 || !PACKET_get_net_4(&subpkt, &i)
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)
182 || PACKET_remaining(&subpkt)
183 || PACKET_get_sub_packet(pkt, &subpkt, 4)) {
184 fprintf(stderr, "test_PACKET_get_sub_packet() failed\n");
191 static int test_PACKET_get_bytes(PACKET *pkt, size_t start)
193 unsigned char *bytes;
195 if ( !PACKET_goto_bookmark(pkt, start)
196 || !PACKET_get_bytes(pkt, &bytes, 4)
197 || bytes[0] != 1 || bytes[1] != 2
198 || bytes[2] != 3 || bytes[3] != 4
199 || PACKET_remaining(pkt) != BUF_LEN -4
200 || !PACKET_forward(pkt, BUF_LEN - 8)
201 || !PACKET_get_bytes(pkt, &bytes, 4)
202 || bytes[0] != 0xfc || bytes[1] != 0xfd
203 || bytes[2] != 0xfe || bytes[3] != 0xff
204 || PACKET_remaining(pkt)) {
205 fprintf(stderr, "test_PACKET_get_bytes() failed\n");
212 static int test_PACKET_copy_bytes(PACKET *pkt, size_t start)
214 unsigned char bytes[4];
216 if ( !PACKET_goto_bookmark(pkt, start)
217 || !PACKET_copy_bytes(pkt, bytes, 4)
218 || bytes[0] != 1 || bytes[1] != 2
219 || bytes[2] != 3 || bytes[3] != 4
220 || PACKET_remaining(pkt) != BUF_LEN - 4
221 || !PACKET_forward(pkt, BUF_LEN - 8)
222 || !PACKET_copy_bytes(pkt, bytes, 4)
223 || bytes[0] != 0xfc || bytes[1] != 0xfd
224 || bytes[2] != 0xfe || bytes[3] != 0xff
225 || PACKET_remaining(pkt)) {
226 fprintf(stderr, "test_PACKET_copy_bytes() failed\n");
233 static int test_PACKET_move_funcs(PACKET *pkt, size_t start)
238 if ( !PACKET_goto_bookmark(pkt, start)
239 || PACKET_back(pkt, 1)
240 || !PACKET_forward(pkt, 1)
241 || !PACKET_get_bytes(pkt, &byte, 1)
243 || !PACKET_get_bookmark(pkt, &bm)
244 || !PACKET_forward(pkt, BUF_LEN - 2)
245 || PACKET_forward(pkt, 1)
246 || !PACKET_back(pkt, 1)
247 || !PACKET_get_bytes(pkt, &byte, 1)
249 || !PACKET_goto_bookmark(pkt, bm)
250 || !PACKET_get_bytes(pkt, &byte, 1)
252 fprintf(stderr, "test_PACKET_move_funcs() failed\n");
259 static int test_PACKET_buf_init()
261 unsigned char buf[BUF_LEN];
265 /* Also tests PACKET_get_len() */
266 if ( !PACKET_buf_init(&pkt, buf, 4)
267 || !PACKET_length(&pkt, &len)
269 || !PACKET_buf_init(&pkt, buf, BUF_LEN)
270 || !PACKET_length(&pkt, &len)
272 || pkt.end - pkt.start != BUF_LEN
273 || pkt.end < pkt.start
274 || pkt.curr < pkt.start
275 || pkt.curr > pkt.end
276 || PACKET_buf_init(&pkt, buf, -1)) {
277 fprintf(stderr, "test_PACKET_buf_init() failed\n");
284 int main(int argc, char **argv)
286 unsigned char buf[BUF_LEN];
291 for (i=1; i<=BUF_LEN; i++) {
296 if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
297 || !PACKET_get_bookmark(&pkt, &start)) {
298 fprintf(stderr, "setup failed\n");
302 if ( !test_PACKET_buf_init()
303 || !test_PACKET_remaining(&pkt)
304 || !test_PACKET_get_1(&pkt, start)
305 || !test_PACKET_get_4(&pkt, start)
306 || !test_PACKET_get_net_2(&pkt, start)
307 || !test_PACKET_get_net_3(&pkt, start)
308 || !test_PACKET_get_net_4(&pkt, start)
309 || !test_PACKET_get_sub_packet(&pkt, start)
310 || !test_PACKET_get_bytes(&pkt, start)
311 || !test_PACKET_copy_bytes(&pkt, start)
312 || !test_PACKET_move_funcs(&pkt, start)) {