- test of header payload rewritten
[strongswan.git] / Source / charon / testcases / generator_test.c
1 /**
2 * @file generator.h
3 *
4 * @brief Tests to test the Generator class generator_t
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 "generator_test.h"
26
27 #include "../globals.h"
28 #include "../generator.h"
29 #include "../utils/allocator.h"
30 #include "../utils/logger_manager.h"
31 #include "../utils/logger.h"
32 #include "../payloads/encodings.h"
33 #include "../payloads/ike_header.h"
34 #include "../payloads/transform_attribute.h"
35 #include "../payloads/transform_substructure.h"
36 #include "../payloads/proposal_substructure.h"
37
38 /*
39 * Described in Header
40 */
41 void test_generator_with_header_payload(tester_t *tester)
42 {
43 generator_t *generator;
44 ike_header_t *header_data;
45 chunk_t generated_data;
46 status_t status;
47 logger_t *logger;
48
49 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"header payload");
50
51 header_data = ike_header_create();
52 header_data->set_initiator_spi(header_data,1);
53 header_data->set_responder_spi(header_data,2);
54 header_data->set_next_payload(header_data, 3);
55 header_data->set_exchange_type(header_data, 6);
56 header_data->set_initiator_flag(header_data, TRUE);
57 header_data->set_response_flag(header_data, TRUE);
58 header_data->set_message_id(header_data,7);
59
60 generator = generator_create();
61 tester->assert_true(tester,(generator != NULL), "generator create check");
62
63 status = generator->generate_payload(generator,(payload_t *) header_data);
64 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
65
66 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
67
68 u_int8_t expected_generation[] = {
69 0x00,0x00,0x00,0x00,
70 0x00,0x00,0x00,0x01,
71 0x00,0x00,0x00,0x00,
72 0x00,0x00,0x00,0x02,
73 0x03,0x20,0x06,0x28,
74 0x00,0x00,0x00,0x07,
75 0x00,0x00,0x00,0x1C,
76 };
77
78 logger->log_bytes(logger,RAW,"expected header",expected_generation,sizeof(expected_generation));
79 tester->assert_true(tester,(generated_data.len == sizeof(expected_generation)), "compare generated data length");
80 logger->log_chunk(logger,RAW,"generated header",&generated_data);
81 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data 1");
82 allocator_free_chunk(generated_data);
83
84 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
85
86 header_data->set_initiator_spi(header_data,0x22000054231234LL);
87 header_data->set_responder_spi(header_data,0x122398);
88 header_data->set_next_payload(header_data, 0xF3);
89 header_data->set_exchange_type(header_data, 0x12);
90 header_data->set_initiator_flag(header_data, TRUE);
91 header_data->set_response_flag(header_data, TRUE);
92 header_data->set_message_id(header_data,0x33AFF3);
93
94 generator = generator_create();
95 tester->assert_true(tester,(generator != NULL), "generator create check");
96
97 status = generator->generate_payload(generator,(payload_t *)header_data);
98 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
99
100 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
101
102 u_int8_t expected_generation2[] = {
103 0x00,0x22,0x00,0x00,
104 0x54,0x23,0x12,0x34,
105 0x00,0x00,0x00,0x00,
106 0x00,0x12,0x23,0x98,
107 0xF3,0x20,0x12,0x28,
108 0x00,0x33,0xAF,0xF3,
109 0x00,0x00,0x00,0x1C,
110 };
111
112 logger->log_bytes(logger,RAW,"expected header",expected_generation2,sizeof(expected_generation2));
113
114 logger->log_chunk(logger,RAW,"generated header",&generated_data);
115
116 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data 2");
117 allocator_free_chunk(generated_data);
118
119 header_data->destroy(header_data);
120
121 global_logger_manager->destroy_logger(global_logger_manager,logger);
122 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
123 }
124
125 /*
126 * Described in header
127 */
128 void test_generator_with_transform_attribute(tester_t *tester)
129 {
130 generator_t *generator;
131 transform_attribute_t *attribute;
132 status_t status;
133 chunk_t generated_data;
134 logger_t *logger;
135
136 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"transform_attribute payload");
137
138
139 /* test empty attribute */
140 generator = generator_create();
141 tester->assert_true(tester,(generator != NULL), "generator create check");
142 attribute = transform_attribute_create();
143 status = generator->generate_payload(generator,(payload_t *)attribute);
144 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
145 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
146 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
147
148 u_int8_t expected_generation[] = {
149 0x80,0x00,0x00,0x00,
150 };
151 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
152 allocator_free_chunk(generated_data);
153 tester->assert_true(tester,(attribute->destroy(attribute) == SUCCESS), "attribute destroy call check");
154 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
155
156 /* test attribute with 2 byte data */
157 generator = generator_create();
158 tester->assert_true(tester,(generator != NULL), "generator create check");
159
160 attribute = transform_attribute_create();
161 u_int16_t dataval = 5768;
162 chunk_t data;
163 data.ptr = (void *) &dataval;
164 data.len = 2;
165
166 attribute->set_value(attribute,data);
167
168 status = generator->generate_payload(generator,(payload_t *)attribute);
169 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
170 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
171 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
172
173 u_int8_t expected_generation2[] = {
174 0x80,0x00,0x88,0x16,
175 };
176 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data");
177
178 allocator_free_chunk(generated_data);
179 tester->assert_true(tester,(attribute->destroy(attribute) == SUCCESS), "attribute destroy call check");
180 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
181
182
183
184 /* test attribute with 25 byte data */
185 generator = generator_create();
186 tester->assert_true(tester,(generator != NULL), "generator create check");
187
188 attribute = transform_attribute_create();
189 char *stringval = "ddddddddddeeeeeeeeeefffff";
190 data.ptr = (void *) stringval;
191 data.len = 25;
192
193 status = attribute->set_value(attribute,data);
194 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
195
196 status = attribute->set_attribute_type(attribute,456);
197 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
198
199
200 status = generator->generate_payload(generator,(payload_t *)attribute);
201 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
202 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
203 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
204
205 u_int8_t expected_generation3[] = {
206 0x01,0xC8,0x00,0x19,
207 0x64,0x64,0x64,0x64,
208 0x64,0x64,0x64,0x64,
209 0x64,0x64,0x65,0x65,
210 0x65,0x65,0x65,0x65,
211 0x65,0x65,0x65,0x65,
212 0x66,0x66,0x66,0x66,
213 0x66
214 };
215 tester->assert_true(tester,(memcmp(expected_generation3,generated_data.ptr,sizeof(expected_generation3)) == 0), "compare generated data");
216
217 allocator_free_chunk(generated_data);
218 tester->assert_true(tester,(attribute->destroy(attribute) == SUCCESS), "attribute destroy call check");
219 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
220
221
222 global_logger_manager->destroy_logger(global_logger_manager,logger);
223 }
224
225
226
227 /*
228 * Described in header
229 */
230 void test_generator_with_transform_substructure(tester_t *tester)
231 {
232 generator_t *generator;
233 transform_attribute_t *attribute1, *attribute2;
234 transform_substructure_t *transform;
235 chunk_t data;
236 status_t status;
237 chunk_t generated_data;
238 logger_t *logger;
239
240 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"transform substr.");
241
242 /* create generator */
243 generator = generator_create();
244 tester->assert_true(tester,(generator != NULL), "generator create check");
245
246 /* create attribute 1 */
247 attribute1 = transform_attribute_create();
248 char *stringval = "abcd";
249 data.ptr = (void *) stringval;
250 data.len = 4;
251 status = attribute1->set_value(attribute1,data);
252 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
253 status = attribute1->set_attribute_type(attribute1,0);
254 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
255 logger->log(logger,CONTROL,"attribute1 created");
256
257 /* create attribute 2 */
258 attribute2 = transform_attribute_create();
259 stringval = "efgh";
260 data.ptr = (void *) stringval;
261 data.len = 4;
262 status = attribute2->set_value(attribute2,data);
263 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
264 status = attribute2->set_attribute_type(attribute2,0);
265 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
266 logger->log(logger,CONTROL,"attribute2 created");
267
268 /* create transform */
269 transform = transform_substructure_create();
270 tester->assert_true(tester,(transform != NULL), "transform create check");
271 status = transform->add_transform_attribute(transform,attribute1);
272 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
273 status = transform->add_transform_attribute(transform,attribute2);
274 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
275 status = transform->set_transform_type(transform,5); /* hex 5 */
276 tester->assert_true(tester,(status == SUCCESS),"set_transform_type call check");
277 status = transform->set_transform_id(transform,65000); /* hex FDE8 */
278 tester->assert_true(tester,(status == SUCCESS),"set_transform_id call check");
279
280
281 logger->log(logger,CONTROL,"transform created");
282
283 status = generator->generate_payload(generator,(payload_t *)transform);
284 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
285 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
286 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
287
288 u_int8_t expected_generation3[] = {
289 0x00,0x00,0x00,0x18,
290 0x05,0x00,0xFD,0xE8,
291 0x00,0x00,0x00,0x04,
292 0x61,0x62,0x63,0x64,
293 0x00,0x00,0x00,0x04,
294 0x65,0x66,0x67,0x68,
295 };
296 tester->assert_true(tester,(memcmp(expected_generation3,generated_data.ptr,sizeof(expected_generation3)) == 0), "compare generated data");
297
298 allocator_free_chunk(generated_data);
299 tester->assert_true(tester,(transform->destroy(transform) == SUCCESS), "transform destroy call check");
300 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
301
302
303 global_logger_manager->destroy_logger(global_logger_manager,logger);
304 }
305
306
307 /*
308 * Described in header
309 */
310 void test_generator_with_proposal_substructure(tester_t *tester)
311 {
312 generator_t *generator;
313 transform_attribute_t *attribute1, *attribute2, *attribute3;
314 transform_substructure_t *transform1, *transform2;
315 proposal_substructure_t *proposal;
316 chunk_t data;
317 status_t status;
318 chunk_t generated_data;
319 logger_t *logger;
320
321 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"transform substr.");
322
323 /* create generator */
324 generator = generator_create();
325 tester->assert_true(tester,(generator != NULL), "generator create check");
326
327 /* create attribute 1 */
328 attribute1 = transform_attribute_create();
329 char *stringval = "abcd";
330 data.ptr = (void *) stringval;
331 data.len = 4;
332 status = attribute1->set_value(attribute1,data);
333 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
334 status = attribute1->set_attribute_type(attribute1,0);
335 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
336 logger->log(logger,CONTROL,"attribute1 created");
337
338 /* create attribute 2 */
339 attribute2 = transform_attribute_create();
340 stringval = "efgh";
341 data.ptr = (void *) stringval;
342 data.len = 4;
343 status = attribute2->set_value(attribute2,data);
344 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
345 status = attribute2->set_attribute_type(attribute2,0);
346 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
347 logger->log(logger,CONTROL,"attribute2 created");
348
349 /* create attribute 3 */
350 attribute3 = transform_attribute_create();
351 stringval = "ijkl";
352 data.ptr = (void *) stringval;
353 data.len = 4;
354 status = attribute3->set_value(attribute3,data);
355 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
356 status = attribute3->set_attribute_type(attribute3,0);
357 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
358 logger->log(logger,CONTROL,"attribute3 created");
359
360 /* create transform 1*/
361 transform1 = transform_substructure_create();
362 tester->assert_true(tester,(transform1 != NULL), "transform create check");
363 status = transform1->add_transform_attribute(transform1,attribute1);
364 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
365 status = transform1->add_transform_attribute(transform1,attribute2);
366 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
367 status = transform1->set_transform_type(transform1,5); /* hex 5 */
368 tester->assert_true(tester,(status == SUCCESS),"set_transform_type call check");
369 status = transform1->set_transform_id(transform1,65000); /* hex FDE8 */
370 tester->assert_true(tester,(status == SUCCESS),"set_transform_id call check");
371
372 /* create transform 2*/
373 transform2 = transform_substructure_create();
374 tester->assert_true(tester,(transform2 != NULL), "transform create check");
375 status = transform2->add_transform_attribute(transform2,attribute3);
376 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
377 status = transform2->set_transform_type(transform2,3); /* hex 3 */
378 tester->assert_true(tester,(status == SUCCESS),"set_transform_type call check");
379 status = transform2->set_transform_id(transform2,4); /* hex 4 */
380 tester->assert_true(tester,(status == SUCCESS),"set_transform_id call check");
381
382 logger->log(logger,CONTROL,"transforms created");
383
384 proposal = proposal_substructure_create();
385 tester->assert_true(tester,(proposal != NULL), "proposal create check");
386
387 stringval = "ABCDEFGH";
388 data.ptr = (void *) stringval;
389 data.len = 8;
390
391 status = proposal->add_transform_substructure(proposal,transform1);
392 tester->assert_true(tester,(status == SUCCESS),"add_transform_substructure call check");
393 status = proposal->add_transform_substructure(proposal,transform2);
394 tester->assert_true(tester,(status == SUCCESS),"add_transform_substructure call check");
395 status = proposal->set_spi(proposal,data);
396 tester->assert_true(tester,(status == SUCCESS),"set_spi call check");
397 status = proposal->set_proposal_number(proposal,7);
398 tester->assert_true(tester,(status == SUCCESS),"set_proposal_number call check");
399 status = proposal->set_protocol_id(proposal,4);
400 tester->assert_true(tester,(status == SUCCESS),"set_protocol_id call check");
401
402
403 status = generator->generate_payload(generator,(payload_t *)proposal);
404 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
405 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
406 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
407
408 u_int8_t expected_generation[] = {
409 /* proposal header */
410 0x00,0x00,0x00,0x38,
411 0x07,0x04,0x08,0x02,
412 /* SPI */
413 0x41,0x42,0x43,0x44,
414 0x45,0x46,0x47,0x48,
415 /* first transform */
416 0x03,0x00,0x00,0x18,
417 0x05,0x00,0xFD,0xE8,
418 /* first transform attributes */
419 0x00,0x00,0x00,0x04,
420 0x61,0x62,0x63,0x64,
421 0x00,0x00,0x00,0x04,
422 0x65,0x66,0x67,0x68,
423 /* second transform */
424 0x00,0x00,0x00,0x10,
425 0x03,0x00,0x00,0x04,
426 /* second transform attributes */
427 0x00,0x00,0x00,0x04,
428 0x69,0x6A,0x6B,0x6C
429 };
430 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
431
432 allocator_free_chunk(generated_data);
433 tester->assert_true(tester,(proposal->destroy(proposal) == SUCCESS), "proposal destroy call check");
434 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
435
436
437 global_logger_manager->destroy_logger(global_logger_manager,logger);
438 }
439