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