- wrote payload sa_payload
[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 #include "../payloads/sa_payload.h"
38 #include "../payloads/ke_payload.h"
39
40 /*
41 * Described in Header
42 */
43 void test_generator_with_header_payload(tester_t *tester)
44 {
45 generator_t *generator;
46 ike_header_t *header_data;
47 chunk_t generated_data;
48 status_t status;
49 logger_t *logger;
50
51 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"header payload");
52
53 header_data = ike_header_create();
54 header_data->set_initiator_spi(header_data,1);
55 header_data->set_responder_spi(header_data,2);
56 ((payload_t *) header_data)->set_next_type((payload_t *) header_data, 3);
57 header_data->set_exchange_type(header_data, 6);
58 header_data->set_initiator_flag(header_data, TRUE);
59 header_data->set_response_flag(header_data, TRUE);
60 header_data->set_message_id(header_data,7);
61
62 generator = generator_create();
63 tester->assert_true(tester,(generator != NULL), "generator create check");
64
65 status = generator->generate_payload(generator,(payload_t *) header_data);
66 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
67
68 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
69
70 u_int8_t expected_generation[] = {
71 0x00,0x00,0x00,0x00,
72 0x00,0x00,0x00,0x01,
73 0x00,0x00,0x00,0x00,
74 0x00,0x00,0x00,0x02,
75 0x03,0x20,0x06,0x28,
76 0x00,0x00,0x00,0x07,
77 0x00,0x00,0x00,0x1C,
78 };
79
80 logger->log_bytes(logger,RAW,"expected header",expected_generation,sizeof(expected_generation));
81 tester->assert_true(tester,(generated_data.len == sizeof(expected_generation)), "compare generated data length");
82 logger->log_chunk(logger,RAW,"generated header",&generated_data);
83 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data 1");
84 allocator_free_chunk(generated_data);
85
86 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
87
88 header_data->set_initiator_spi(header_data,0x22000054231234LL);
89 header_data->set_responder_spi(header_data,0x122398);
90 ((payload_t *) header_data)->set_next_type((payload_t *) header_data,0xF3);
91 header_data->set_exchange_type(header_data, 0x12);
92 header_data->set_initiator_flag(header_data, TRUE);
93 header_data->set_response_flag(header_data, TRUE);
94 header_data->set_message_id(header_data,0x33AFF3);
95
96 generator = generator_create();
97 tester->assert_true(tester,(generator != NULL), "generator create check");
98
99 status = generator->generate_payload(generator,(payload_t *)header_data);
100 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
101
102 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
103
104 u_int8_t expected_generation2[] = {
105 0x00,0x22,0x00,0x00,
106 0x54,0x23,0x12,0x34,
107 0x00,0x00,0x00,0x00,
108 0x00,0x12,0x23,0x98,
109 0xF3,0x20,0x12,0x28,
110 0x00,0x33,0xAF,0xF3,
111 0x00,0x00,0x00,0x1C,
112 };
113
114 logger->log_bytes(logger,RAW,"expected header",expected_generation2,sizeof(expected_generation2));
115
116 logger->log_chunk(logger,RAW,"generated header",&generated_data);
117
118 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data 2");
119 allocator_free_chunk(generated_data);
120
121 header_data->destroy(header_data);
122
123 global_logger_manager->destroy_logger(global_logger_manager,logger);
124 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
125 }
126
127 /*
128 * Described in header
129 */
130 void test_generator_with_transform_attribute(tester_t *tester)
131 {
132 generator_t *generator;
133 transform_attribute_t *attribute;
134 status_t status;
135 chunk_t generated_data;
136 logger_t *logger;
137
138 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"transform_attribute payload");
139
140
141 /* test empty attribute */
142 generator = generator_create();
143 tester->assert_true(tester,(generator != NULL), "generator create check");
144 attribute = transform_attribute_create();
145 status = generator->generate_payload(generator,(payload_t *)attribute);
146 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
147 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
148 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
149
150 u_int8_t expected_generation[] = {
151 0x80,0x00,0x00,0x00,
152 };
153 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
154 allocator_free_chunk(generated_data);
155 tester->assert_true(tester,(attribute->destroy(attribute) == SUCCESS), "attribute destroy call check");
156 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
157
158 /* test attribute with 2 byte data */
159 generator = generator_create();
160 tester->assert_true(tester,(generator != NULL), "generator create check");
161
162 attribute = transform_attribute_create();
163 u_int16_t dataval = 5768;
164 chunk_t data;
165 data.ptr = (void *) &dataval;
166 data.len = 2;
167
168 attribute->set_value(attribute,data);
169
170 status = generator->generate_payload(generator,(payload_t *)attribute);
171 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
172 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
173 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
174
175 u_int8_t expected_generation2[] = {
176 0x80,0x00,0x88,0x16,
177 };
178 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data");
179
180 allocator_free_chunk(generated_data);
181 tester->assert_true(tester,(attribute->destroy(attribute) == SUCCESS), "attribute destroy call check");
182 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
183
184
185
186 /* test attribute with 25 byte data */
187 generator = generator_create();
188 tester->assert_true(tester,(generator != NULL), "generator create check");
189
190 attribute = transform_attribute_create();
191 char *stringval = "ddddddddddeeeeeeeeeefffff";
192 data.ptr = (void *) stringval;
193 data.len = 25;
194
195 status = attribute->set_value(attribute,data);
196 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
197
198 status = attribute->set_attribute_type(attribute,456);
199 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
200
201
202 status = generator->generate_payload(generator,(payload_t *)attribute);
203 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
204 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
205 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
206
207 u_int8_t expected_generation3[] = {
208 0x01,0xC8,0x00,0x19,
209 0x64,0x64,0x64,0x64,
210 0x64,0x64,0x64,0x64,
211 0x64,0x64,0x65,0x65,
212 0x65,0x65,0x65,0x65,
213 0x65,0x65,0x65,0x65,
214 0x66,0x66,0x66,0x66,
215 0x66
216 };
217 tester->assert_true(tester,(memcmp(expected_generation3,generated_data.ptr,sizeof(expected_generation3)) == 0), "compare generated data");
218
219 allocator_free_chunk(generated_data);
220 tester->assert_true(tester,(attribute->destroy(attribute) == SUCCESS), "attribute destroy call check");
221 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
222
223
224 global_logger_manager->destroy_logger(global_logger_manager,logger);
225 }
226
227
228
229 /*
230 * Described in header
231 */
232 void test_generator_with_transform_substructure(tester_t *tester)
233 {
234 generator_t *generator;
235 transform_attribute_t *attribute1, *attribute2;
236 transform_substructure_t *transform;
237 chunk_t data;
238 status_t status;
239 chunk_t generated_data;
240 logger_t *logger;
241
242 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"transform substr.");
243
244 /* create generator */
245 generator = generator_create();
246 tester->assert_true(tester,(generator != NULL), "generator create check");
247
248 /* create attribute 1 */
249 attribute1 = transform_attribute_create();
250 char *stringval = "abcd";
251 data.ptr = (void *) stringval;
252 data.len = 4;
253 status = attribute1->set_value(attribute1,data);
254 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
255 status = attribute1->set_attribute_type(attribute1,0);
256 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
257 logger->log(logger,CONTROL,"attribute1 created");
258
259 /* create attribute 2 */
260 attribute2 = transform_attribute_create();
261 stringval = "efgh";
262 data.ptr = (void *) stringval;
263 data.len = 4;
264 status = attribute2->set_value(attribute2,data);
265 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
266 status = attribute2->set_attribute_type(attribute2,0);
267 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
268 logger->log(logger,CONTROL,"attribute2 created");
269
270 /* create transform */
271 transform = transform_substructure_create();
272 tester->assert_true(tester,(transform != NULL), "transform create check");
273 status = transform->add_transform_attribute(transform,attribute1);
274 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
275 status = transform->add_transform_attribute(transform,attribute2);
276 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
277 status = transform->set_transform_type(transform,5); /* hex 5 */
278 tester->assert_true(tester,(status == SUCCESS),"set_transform_type call check");
279 status = transform->set_transform_id(transform,65000); /* hex FDE8 */
280 tester->assert_true(tester,(status == SUCCESS),"set_transform_id call check");
281
282
283 logger->log(logger,CONTROL,"transform created");
284
285 status = generator->generate_payload(generator,(payload_t *)transform);
286 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
287 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
288 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
289
290 u_int8_t expected_generation3[] = {
291 0x00,0x00,0x00,0x18,
292 0x05,0x00,0xFD,0xE8,
293 0x00,0x00,0x00,0x04,
294 0x61,0x62,0x63,0x64,
295 0x00,0x00,0x00,0x04,
296 0x65,0x66,0x67,0x68,
297 };
298 tester->assert_true(tester,(memcmp(expected_generation3,generated_data.ptr,sizeof(expected_generation3)) == 0), "compare generated data");
299
300 allocator_free_chunk(generated_data);
301 tester->assert_true(tester,(transform->destroy(transform) == SUCCESS), "transform destroy call check");
302 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
303
304
305 global_logger_manager->destroy_logger(global_logger_manager,logger);
306 }
307
308
309 /*
310 * Described in header
311 */
312 void test_generator_with_proposal_substructure(tester_t *tester)
313 {
314 generator_t *generator;
315 transform_attribute_t *attribute1, *attribute2, *attribute3;
316 transform_substructure_t *transform1, *transform2;
317 proposal_substructure_t *proposal;
318 chunk_t data;
319 status_t status;
320 chunk_t generated_data;
321 logger_t *logger;
322
323 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"transform substr.");
324
325 /* create generator */
326 generator = generator_create();
327 tester->assert_true(tester,(generator != NULL), "generator create check");
328
329 /* create attribute 1 */
330 attribute1 = transform_attribute_create();
331 char *stringval = "abcd";
332 data.ptr = (void *) stringval;
333 data.len = 4;
334 status = attribute1->set_value(attribute1,data);
335 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
336 status = attribute1->set_attribute_type(attribute1,0);
337 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
338 logger->log(logger,CONTROL,"attribute1 created");
339
340 /* create attribute 2 */
341 attribute2 = transform_attribute_create();
342 stringval = "efgh";
343 data.ptr = (void *) stringval;
344 data.len = 4;
345 status = attribute2->set_value(attribute2,data);
346 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
347 status = attribute2->set_attribute_type(attribute2,0);
348 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
349 logger->log(logger,CONTROL,"attribute2 created");
350
351 /* create attribute 3 */
352 attribute3 = transform_attribute_create();
353 stringval = "ijkl";
354 data.ptr = (void *) stringval;
355 data.len = 4;
356 status = attribute3->set_value(attribute3,data);
357 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
358 status = attribute3->set_attribute_type(attribute3,0);
359 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
360 logger->log(logger,CONTROL,"attribute3 created");
361
362 /* create transform 1*/
363 transform1 = transform_substructure_create();
364 tester->assert_true(tester,(transform1 != NULL), "transform create check");
365 status = transform1->add_transform_attribute(transform1,attribute1);
366 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
367 status = transform1->add_transform_attribute(transform1,attribute2);
368 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
369 status = transform1->set_transform_type(transform1,5); /* hex 5 */
370 tester->assert_true(tester,(status == SUCCESS),"set_transform_type call check");
371 status = transform1->set_transform_id(transform1,65000); /* hex FDE8 */
372 tester->assert_true(tester,(status == SUCCESS),"set_transform_id call check");
373
374 /* create transform 2*/
375 transform2 = transform_substructure_create();
376 tester->assert_true(tester,(transform2 != NULL), "transform create check");
377 status = transform2->add_transform_attribute(transform2,attribute3);
378 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
379 status = transform2->set_transform_type(transform2,3); /* hex 3 */
380 tester->assert_true(tester,(status == SUCCESS),"set_transform_type call check");
381 status = transform2->set_transform_id(transform2,4); /* hex 4 */
382 tester->assert_true(tester,(status == SUCCESS),"set_transform_id call check");
383
384 logger->log(logger,CONTROL,"transforms created");
385
386 proposal = proposal_substructure_create();
387 tester->assert_true(tester,(proposal != NULL), "proposal create check");
388
389 stringval = "ABCDEFGH";
390 data.ptr = (void *) stringval;
391 data.len = 8;
392
393 status = proposal->add_transform_substructure(proposal,transform1);
394 tester->assert_true(tester,(status == SUCCESS),"add_transform_substructure call check");
395 status = proposal->add_transform_substructure(proposal,transform2);
396 tester->assert_true(tester,(status == SUCCESS),"add_transform_substructure call check");
397 status = proposal->set_spi(proposal,data);
398 tester->assert_true(tester,(status == SUCCESS),"set_spi call check");
399 status = proposal->set_proposal_number(proposal,7);
400 tester->assert_true(tester,(status == SUCCESS),"set_proposal_number call check");
401 status = proposal->set_protocol_id(proposal,4);
402 tester->assert_true(tester,(status == SUCCESS),"set_protocol_id call check");
403
404
405 status = generator->generate_payload(generator,(payload_t *)proposal);
406 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
407 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
408 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
409
410 u_int8_t expected_generation[] = {
411 /* proposal header */
412 0x00,0x00,0x00,0x38,
413 0x07,0x04,0x08,0x02,
414 /* SPI */
415 0x41,0x42,0x43,0x44,
416 0x45,0x46,0x47,0x48,
417 /* first transform */
418 0x03,0x00,0x00,0x18,
419 0x05,0x00,0xFD,0xE8,
420 /* first transform attributes */
421 0x00,0x00,0x00,0x04,
422 0x61,0x62,0x63,0x64,
423 0x00,0x00,0x00,0x04,
424 0x65,0x66,0x67,0x68,
425 /* second transform */
426 0x00,0x00,0x00,0x10,
427 0x03,0x00,0x00,0x04,
428 /* second transform attributes */
429 0x00,0x00,0x00,0x04,
430 0x69,0x6A,0x6B,0x6C
431 };
432 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
433
434 allocator_free_chunk(generated_data);
435 tester->assert_true(tester,(proposal->destroy(proposal) == SUCCESS), "proposal destroy call check");
436 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
437
438
439 global_logger_manager->destroy_logger(global_logger_manager,logger);
440 }
441
442 /*
443 * Described in header
444 */
445 void test_generator_with_sa_payload(tester_t *tester)
446 {
447 generator_t *generator;
448 transform_attribute_t *attribute1, *attribute2, *attribute3;
449 transform_substructure_t *transform1, *transform2;
450 proposal_substructure_t *proposal1, *proposal2;
451 sa_payload_t *sa_payload;
452 ike_header_t *ike_header;
453
454 chunk_t data;
455 status_t status;
456 chunk_t generated_data;
457 logger_t *logger;
458
459 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"Message with SA Payload");
460
461 /* create generator */
462 generator = generator_create();
463 tester->assert_true(tester,(generator != NULL), "generator create check");
464
465 /* create attribute 1 */
466 attribute1 = transform_attribute_create();
467 char *stringval = "abcd";
468 data.ptr = (void *) stringval;
469 data.len = 4;
470 status = attribute1->set_value(attribute1,data);
471 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
472 status = attribute1->set_attribute_type(attribute1,0);
473 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
474 logger->log(logger,CONTROL,"attribute1 created");
475
476 /* create attribute 2 */
477 attribute2 = transform_attribute_create();
478 stringval = "efgh";
479 data.ptr = (void *) stringval;
480 data.len = 4;
481 status = attribute2->set_value(attribute2,data);
482 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
483 status = attribute2->set_attribute_type(attribute2,0);
484 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
485 logger->log(logger,CONTROL,"attribute2 created");
486
487 /* create attribute 3 */
488 attribute3 = transform_attribute_create();
489 stringval = "ijkl";
490 data.ptr = (void *) stringval;
491 data.len = 4;
492 status = attribute3->set_value(attribute3,data);
493 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
494 status = attribute3->set_attribute_type(attribute3,0);
495 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
496 logger->log(logger,CONTROL,"attribute3 created");
497
498 /* create transform 1*/
499 transform1 = transform_substructure_create();
500 tester->assert_true(tester,(transform1 != NULL), "transform create check");
501 status = transform1->add_transform_attribute(transform1,attribute1);
502 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
503 status = transform1->add_transform_attribute(transform1,attribute2);
504 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
505 status = transform1->set_transform_type(transform1,5); /* hex 5 */
506 tester->assert_true(tester,(status == SUCCESS),"set_transform_type call check");
507 status = transform1->set_transform_id(transform1,65000); /* hex FDE8 */
508 tester->assert_true(tester,(status == SUCCESS),"set_transform_id call check");
509
510 /* create transform 2*/
511 transform2 = transform_substructure_create();
512 tester->assert_true(tester,(transform2 != NULL), "transform create check");
513 status = transform2->add_transform_attribute(transform2,attribute3);
514 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
515 status = transform2->set_transform_type(transform2,3); /* hex 3 */
516 tester->assert_true(tester,(status == SUCCESS),"set_transform_type call check");
517 status = transform2->set_transform_id(transform2,4); /* hex 4 */
518 tester->assert_true(tester,(status == SUCCESS),"set_transform_id call check");
519
520 logger->log(logger,CONTROL,"transforms created");
521
522 /* create proposal 1 */
523 proposal1 = proposal_substructure_create();
524 tester->assert_true(tester,(proposal1 != NULL), "proposal create check");
525
526 stringval = "ABCDEFGH";
527 data.ptr = (void *) stringval;
528 data.len = 8;
529
530 status = proposal1->add_transform_substructure(proposal1,transform1);
531 tester->assert_true(tester,(status == SUCCESS),"add_transform_substructure call check");
532 status = proposal1->add_transform_substructure(proposal1,transform2);
533 tester->assert_true(tester,(status == SUCCESS),"add_transform_substructure call check");
534 status = proposal1->set_spi(proposal1,data);
535 tester->assert_true(tester,(status == SUCCESS),"set_spi call check");
536 status = proposal1->set_proposal_number(proposal1,7);
537 tester->assert_true(tester,(status == SUCCESS),"set_proposal_number call check");
538 status = proposal1->set_protocol_id(proposal1,4);
539 tester->assert_true(tester,(status == SUCCESS),"set_protocol_id call check");
540
541 /* create proposal 2 */
542 proposal2 = proposal_substructure_create();
543 tester->assert_true(tester,(proposal2 != NULL), "proposal create check");
544 status = proposal2->set_proposal_number(proposal2,7);
545 tester->assert_true(tester,(status == SUCCESS),"set_proposal_number call check");
546 status = proposal2->set_protocol_id(proposal2,5);
547 tester->assert_true(tester,(status == SUCCESS),"set_protocol_id call check");
548
549 /* create sa_payload */
550 sa_payload = sa_payload_create();
551
552 sa_payload->add_proposal_substructure(sa_payload,proposal1);
553 sa_payload->add_proposal_substructure(sa_payload,proposal2);
554
555 ike_header = ike_header_create();
556 ike_header->set_initiator_spi(ike_header,0x22000054231234LL);
557 ike_header->set_responder_spi(ike_header,0x122398);
558 ((payload_t *) ike_header)->set_next_type((payload_t *) ike_header,SECURITY_ASSOCIATION);
559 ike_header->set_exchange_type(ike_header, 0x12);
560 ike_header->set_initiator_flag(ike_header, TRUE);
561 ike_header->set_response_flag(ike_header, TRUE);
562 ike_header->set_message_id(ike_header,0x33AFF3);
563
564 status = generator->generate_payload(generator,(payload_t *)ike_header);
565 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
566 status = generator->generate_payload(generator,(payload_t *)sa_payload);
567 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
568 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
569 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
570
571 u_int8_t expected_generation[] = {
572 /* sa payload header */
573 0x00,0x22,0x00,0x00,
574 0x54,0x23,0x12,0x34,
575 0x00,0x00,0x00,0x00,
576 0x00,0x12,0x23,0x98,
577 0x21,0x20,0x12,0x28,
578 0x00,0x33,0xAF,0xF3,
579 0x00,0x00,0x00,0x60,
580
581 /* sa payload header */
582 0x00,0x00,0x00,0x44,
583 /* proposal header */
584 0x00,0x00,0x00,0x38,
585 0x07,0x04,0x08,0x02,
586 /* SPI */
587 0x41,0x42,0x43,0x44,
588 0x45,0x46,0x47,0x48,
589 /* first transform */
590 0x03,0x00,0x00,0x18,
591 0x05,0x00,0xFD,0xE8,
592 /* first transform attributes */
593 0x00,0x00,0x00,0x04,
594 0x61,0x62,0x63,0x64,
595 0x00,0x00,0x00,0x04,
596 0x65,0x66,0x67,0x68,
597 /* second transform */
598 0x00,0x00,0x00,0x10,
599 0x03,0x00,0x00,0x04,
600 /* second transform attributes */
601 0x00,0x00,0x00,0x04,
602 0x69,0x6A,0x6B,0x6C,
603 /* proposal header 2*/
604 0x00,0x00,0x00,0x08,
605 0x07,0x05,0x00,0x00,
606
607 };
608 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
609
610 allocator_free_chunk(generated_data);
611 tester->assert_true(tester,(ike_header->destroy(ike_header) == SUCCESS), "ike_header destroy call check");
612 tester->assert_true(tester,(sa_payload->destroy(sa_payload) == SUCCESS), "sa_payload destroy call check");
613 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
614
615 global_logger_manager->destroy_logger(global_logger_manager,logger);
616
617 }
618
619 /*
620 * Described in header
621 */
622 void test_generator_with_ke_payload(tester_t *tester)
623 {
624 generator_t *generator;
625 ke_payload_t *ke_payload;
626 logger_t *logger;
627 status_t status;
628 chunk_t generated_data;
629 chunk_t key_exchange_data;
630
631 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"Message with KE Payload");
632
633 /* create generator */
634 generator = generator_create();
635 tester->assert_true(tester,(generator != NULL), "generator create check");
636
637 ke_payload = ke_payload_create();
638
639
640 key_exchange_data.ptr = "test-text";
641 key_exchange_data.len = strlen(key_exchange_data.ptr);
642
643 ke_payload->set_key_exchange_data(ke_payload,key_exchange_data);
644
645 ke_payload->set_dh_group_number(ke_payload,7777);
646
647 status = generator->generate_payload(generator,(payload_t *)ke_payload);
648 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
649 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
650 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
651
652 u_int8_t expected_generation[] = {
653 /* payload header */
654 0x00,0x00,0x00,0x11,
655 0x1E,0x61,0x00,0x00,
656 /* key exchange data */
657 0x74,0x65,0x73,0x74,
658 0x2D,0x74,0x65,0x78,
659 0x74
660 };
661
662
663 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
664
665 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
666
667 allocator_free_chunk(generated_data);
668
669 tester->assert_true(tester,(ke_payload->destroy(ke_payload) == SUCCESS), "sa_payload destroy call check");
670 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
671
672 global_logger_manager->destroy_logger(global_logger_manager,logger);
673
674 }
675