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