- memory allocation checks removed
[strongswan.git] / Source / charon / testcases / parser_test.c
1 /**
2 * @file parser_test.h
3 *
4 *
5 */
6
7 /*
8 * Copyright (C) 2005 Jan Hutter, Martin Willi
9 * Hochschule fuer Technik Rapperswil
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 */
21
22 #include <string.h>
23
24 #include "parser_test.h"
25
26 #include <utils/allocator.h>
27 #include <utils/logger_manager.h>
28 #include <encoding/generator.h>
29 #include <encoding/parser.h>
30 #include <encoding/payloads/encodings.h>
31 #include <encoding/payloads/ike_header.h>
32 #include <encoding/payloads/sa_payload.h>
33 #include <encoding/payloads/nonce_payload.h>
34 #include <encoding/payloads/ke_payload.h>
35 #include <encoding/payloads/notify_payload.h>
36
37
38 extern logger_manager_t *global_logger_manager;
39
40
41 /*
42 * Described in Header
43 */
44 void test_parser_with_header_payload(tester_t *tester)
45 {
46 parser_t *parser;
47 ike_header_t *ike_header;
48 status_t status;
49 chunk_t header_chunk;
50
51 u_int8_t header_bytes[] = {
52 0x01,0x00,0x00,0x00,
53 0x00,0x00,0x00,0x00,
54 0x02,0x00,0x00,0x00,
55 0x00,0x00,0x00,0x00,
56 0x03,0x45,0x06,0x28,
57 0x00,0x00,0x00,0x07,
58 0x00,0x00,0x00,0x1C,
59 };
60 header_chunk.ptr = header_bytes;
61 header_chunk.len = sizeof(header_bytes);
62
63
64 parser = parser_create(header_chunk);
65 tester->assert_true(tester,(parser != NULL), "parser create check");
66 status = parser->parse_payload(parser, HEADER, (payload_t**)&ike_header);
67 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
68 parser->destroy(parser);
69
70 if (status != SUCCESS)
71 {
72 return;
73 }
74
75 tester->assert_true(tester,(ike_header->get_initiator_spi(ike_header) == 1),"parsed initiator_spi value");
76 tester->assert_true(tester,(ike_header->get_responder_spi(ike_header) == 2),"parsed responder_spi value");
77 tester->assert_true(tester,(ike_header->payload_interface.get_next_type((payload_t*)ike_header) == 3),"parsed next_payload value");
78 tester->assert_true(tester,(ike_header->get_maj_version(ike_header) == 4),"parsed maj_version value");
79 tester->assert_true(tester,(ike_header->get_min_version(ike_header) == 5),"parsed min_version value");
80 tester->assert_true(tester,(ike_header->get_exchange_type(ike_header) == 6),"parsed exchange_type value");
81 tester->assert_true(tester,(ike_header->get_initiator_flag(ike_header) == TRUE),"parsed flags.initiator value");
82 tester->assert_true(tester,(ike_header->get_version_flag(ike_header) == FALSE),"parsed flags.version value");
83 tester->assert_true(tester,(ike_header->get_response_flag(ike_header) == TRUE),"parsed flags.response value");
84 tester->assert_true(tester,(ike_header->get_message_id(ike_header) == 7),"parsed message_id value");
85 tester->assert_true(tester,(ike_header->payload_interface.get_length((payload_t*)ike_header) == 0x1C),"parsed length value");
86
87 ike_header->destroy(ike_header);
88 }
89
90 /*
91 * Described in Header
92 */
93 void test_parser_with_sa_payload(tester_t *tester)
94 {
95 parser_t *parser;
96 sa_payload_t *sa_payload;
97 status_t status;
98 chunk_t sa_chunk;
99 iterator_t *proposals, *transforms, *attributes;
100
101 u_int8_t sa_bytes[] = {
102 0x00,0x80,0x00,0x24, /* payload header*/
103 0x00,0x00,0x00,0x20, /* a proposal */
104 0x01,0x02,0x04,0x05,
105 0x01,0x02,0x03,0x04, /* spi */
106 0x00,0x00,0x00,0x14, /* transform */
107 0x07,0x00,0x00,0x03,
108 0x80,0x01,0x00,0x05, /* attribute without length */
109 0x00,0x03,0x00,0x04, /* attribute with lenngth */
110 0x01,0x02,0x03,0x04
111
112
113 };
114
115 sa_chunk.ptr = sa_bytes;
116 sa_chunk.len = sizeof(sa_bytes);
117
118
119 parser = parser_create(sa_chunk);
120 tester->assert_true(tester,(parser != NULL), "parser create check");
121 status = parser->parse_payload(parser, SECURITY_ASSOCIATION, (payload_t**)&sa_payload);
122 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
123 parser->destroy(parser);
124
125 if (status != SUCCESS)
126 {
127 return;
128 }
129
130
131 sa_payload->create_proposal_substructure_iterator(sa_payload, &proposals, TRUE);
132 while (proposals->has_next(proposals))
133 {
134 proposal_substructure_t *proposal;
135 proposals->current(proposals, (void**)&proposal);
136 chunk_t spi;
137 u_int8_t spi_should[] = {0x01, 0x02, 0x03, 0x04};
138
139 tester->assert_true(tester,(proposal->get_proposal_number(proposal) == 1),"proposal number");
140 tester->assert_true(tester,(proposal->get_protocol_id(proposal) == 2),"proposal id");
141 spi = proposal->get_spi(proposal);
142 tester->assert_false(tester,(memcmp(&spi_should, spi.ptr, spi.len)),"proposal spi");
143
144 proposal->create_transform_substructure_iterator(proposal, &transforms, TRUE);
145 while(transforms->has_next(transforms))
146 {
147 transform_substructure_t *transform;
148 int loopi;
149 transforms->current(transforms, (void**)&transform);
150 tester->assert_true(tester,(transform->get_transform_type(transform) == 7),"transform type");
151 tester->assert_true(tester,(transform->get_transform_id(transform) == 3),"transform id");
152 transform->create_transform_attribute_iterator(transform, &attributes, TRUE);
153 loopi = 0;
154 while (attributes->has_next(attributes))
155 {
156 transform_attribute_t *attribute;
157 attributes->current(attributes, (void**)&attribute);
158 if (loopi == 0)
159 {
160 u_int8_t value[] = {0x05, 0x00};
161 chunk_t attribute_value;
162 tester->assert_true(tester,(attribute->get_attribute_type(attribute) == 1),"attribute 1 type");
163 attribute_value = attribute->get_value_chunk(attribute);
164 tester->assert_false(tester,(memcmp(&value, attribute_value.ptr, attribute_value.len)),"attribute 1 value");
165 }
166 if (loopi == 1)
167 {
168 u_int8_t value[] = {0x01, 0x02, 0x03, 0x04};
169 chunk_t attribute_value;
170 tester->assert_true(tester,(attribute->get_attribute_type(attribute) == 3),"attribute 2 type");
171 attribute_value = attribute->get_value_chunk(attribute);
172 tester->assert_false(tester,(memcmp(&value, attribute_value.ptr, attribute_value.len)),"attribute 2 value");
173 }
174 loopi++;
175 }
176 attributes->destroy(attributes);
177 }
178 transforms->destroy(transforms);
179 }
180 proposals->destroy(proposals);
181
182
183
184 sa_payload->destroy(sa_payload);
185 }
186
187 /*
188 * Described in Header
189 */
190 void test_parser_with_nonce_payload(tester_t *tester)
191 {
192 parser_t *parser;
193 nonce_payload_t *nonce_payload;
194 status_t status;
195 chunk_t nonce_chunk, result;
196
197 u_int8_t nonce_bytes[] = {
198 0x00,0x00,0x00,0x14, /* payload header */
199 0x00,0x01,0x02,0x03, /* 16 Byte nonce */
200 0x04,0x05,0x06,0x07,
201 0x08,0x09,0x0A,0x2B,
202 0x0C,0x0D,0x0E,0x0F
203 };
204
205 nonce_chunk.ptr = nonce_bytes;
206 nonce_chunk.len = sizeof(nonce_bytes);
207
208 parser = parser_create(nonce_chunk);
209 tester->assert_true(tester,(parser != NULL), "parser create check");
210 status = parser->parse_payload(parser, NONCE, (payload_t**)&nonce_payload);
211 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
212 parser->destroy(parser);
213
214 if (status != SUCCESS)
215 {
216 return;
217 }
218 nonce_payload->get_nonce(nonce_payload, &result);
219 tester->assert_true(tester,(result.len == 16), "parsed nonce lenght");
220 tester->assert_false(tester,(memcmp(nonce_bytes + 4, result.ptr, result.len)), "parsed nonce data");
221 nonce_payload->destroy(nonce_payload);
222 allocator_free_chunk(&result);
223 }
224
225 /*
226 * Described in Header
227 */
228 void test_parser_with_ke_payload(tester_t *tester)
229 {
230 parser_t *parser;
231 ke_payload_t *ke_payload;
232 status_t status;
233 chunk_t ke_chunk, result;
234
235 u_int8_t ke_bytes[] = {
236 0x00,0x00,0x00,0x18, /* payload header */
237 0x00,0x03,0x00,0x00, /* dh group 3 */
238 0x01,0x02,0x03,0x03, /* 16 Byte dh data */
239 0x04,0x05,0x06,0x07,
240 0x08,0x09,0x0A,0x2B,
241 0x0C,0x0D,0x0E,0x0F
242 };
243
244 ke_chunk.ptr = ke_bytes;
245 ke_chunk.len = sizeof(ke_bytes);
246
247 parser = parser_create(ke_chunk);
248 tester->assert_true(tester,(parser != NULL), "parser create check");
249 status = parser->parse_payload(parser, KEY_EXCHANGE, (payload_t**)&ke_payload);
250 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
251 parser->destroy(parser);
252
253 if (status != SUCCESS)
254 {
255 return;
256 }
257 tester->assert_true(tester,(ke_payload->get_dh_group_number(ke_payload) == 3), "DH group");
258 result = ke_payload->get_key_exchange_data(ke_payload);
259 tester->assert_true(tester,(result.len == 16), "parsed key lenght");
260 tester->assert_false(tester,(memcmp(ke_bytes + 8, result.ptr, result.len)), "parsed key data");
261 ke_payload->destroy(ke_payload);
262 }
263
264
265 /*
266 * Described in Header
267 */
268 void test_parser_with_notify_payload(tester_t *tester)
269 {
270 parser_t *parser;
271 notify_payload_t *notify_payload;
272 status_t status;
273 chunk_t notify_chunk, result;
274
275 u_int8_t notify_bytes[] = {
276 0x00,0x00,0x00,0x1C, /* payload header */
277 0x03,0x04,0x00,0x01,
278 0x01,0x02,0x03,0x03, /* spi */
279 0x04,0x05,0x06,0x07, /* noti dati */
280 0x08,0x09,0x0A,0x2B,
281 0x0C,0x0D,0x0E,0x0F,
282 0x0C,0x0D,0x0E,0x0F
283 };
284
285 notify_chunk.ptr = notify_bytes;
286 notify_chunk.len = sizeof(notify_bytes);
287
288 parser = parser_create(notify_chunk);
289 tester->assert_true(tester,(parser != NULL), "parser create check");
290 status = parser->parse_payload(parser, NOTIFY, (payload_t**)&notify_payload);
291 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
292 parser->destroy(parser);
293
294 if (status != SUCCESS)
295 {
296 return;
297 }
298 tester->assert_true(tester,(notify_payload->get_protocol_id(notify_payload) == 3), "Protocol id");
299 tester->assert_true(tester,(notify_payload->get_notify_message_type(notify_payload) == 1), "notify message type");
300
301 result = notify_payload->get_spi(notify_payload);
302 tester->assert_false(tester,(memcmp(notify_bytes + 8, result.ptr, result.len)), "parsed spi");
303
304 result = notify_payload->get_notification_data(notify_payload);
305 tester->assert_false(tester,(memcmp(notify_bytes + 12, result.ptr, result.len)), "parsed notification data");
306
307 notify_payload->destroy(notify_payload);
308 }