6f13eecea3c408c1d70b63231e0bb52782d7da84
[strongswan.git] / Source / charon / testcases / generator_test.c
1 /**
2 * @file generator_test.c
3 *
4 * @brief Tests for the generator_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 "generator_test.h"
26
27 #include <daemon.h>
28 #include <encoding/generator.h>
29 #include <utils/allocator.h>
30 #include <utils/logger_manager.h>
31 #include <utils/logger.h>
32 #include <encoding/payloads/encodings.h>
33 #include <encoding/payloads/ike_header.h>
34 #include <encoding/payloads/transform_attribute.h>
35 #include <encoding/payloads/transform_substructure.h>
36 #include <encoding/payloads/proposal_substructure.h>
37 #include <encoding/payloads/sa_payload.h>
38 #include <encoding/payloads/ke_payload.h>
39 #include <encoding/payloads/notify_payload.h>
40 #include <encoding/payloads/nonce_payload.h>
41 #include <encoding/payloads/id_payload.h>
42 #include <encoding/payloads/auth_payload.h>
43 #include <encoding/payloads/ts_payload.h>
44
45 /*
46 * Described in Header
47 */
48 void test_generator_with_header_payload(tester_t *tester)
49 {
50 generator_t *generator;
51 ike_header_t *header_data;
52 chunk_t generated_data;
53 logger_t *logger;
54
55 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"header payload");
56
57 header_data = ike_header_create();
58 header_data->set_initiator_spi(header_data,1);
59 header_data->set_responder_spi(header_data,2);
60 ((payload_t *) header_data)->set_next_type((payload_t *) header_data, 3);
61 header_data->set_exchange_type(header_data, 6);
62 header_data->set_initiator_flag(header_data, TRUE);
63 header_data->set_response_flag(header_data, TRUE);
64 header_data->set_message_id(header_data,7);
65
66 generator = generator_create();
67 tester->assert_true(tester,(generator != NULL), "generator create check");
68
69 generator->generate_payload(generator,(payload_t *) header_data);
70
71 generator->write_to_chunk(generator,&generated_data);
72
73 u_int8_t expected_generation[] = {
74 0x01,0x00,0x00,0x00,
75 0x00,0x00,0x00,0x00,
76 0x02,0x00,0x00,0x00,
77 0x00,0x00,0x00,0x00,
78 0x03,0x20,0x06,0x28,
79 0x00,0x00,0x00,0x07,
80 0x00,0x00,0x00,0x1C,
81 };
82
83 logger->log_bytes(logger,RAW,"expected header",expected_generation,sizeof(expected_generation));
84 tester->assert_true(tester,(generated_data.len == sizeof(expected_generation)), "compare generated data length");
85 logger->log_chunk(logger,RAW,"generated header",&generated_data);
86 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data 1");
87 allocator_free_chunk(&generated_data);
88
89 generator->destroy(generator);
90
91 header_data->set_initiator_spi(header_data,0x22000054231234LL);
92 header_data->set_responder_spi(header_data,0x122398);
93 ((payload_t *) header_data)->set_next_type((payload_t *) header_data,0xF3);
94 header_data->set_exchange_type(header_data, 0x12);
95 header_data->set_initiator_flag(header_data, TRUE);
96 header_data->set_response_flag(header_data, TRUE);
97 header_data->set_message_id(header_data,0x33AFF3);
98
99 generator = generator_create();
100 tester->assert_true(tester,(generator != NULL), "generator create check");
101
102 generator->generate_payload(generator,(payload_t *)header_data);
103
104 generator->write_to_chunk(generator,&generated_data);
105
106 u_int8_t expected_generation2[] = {
107 0x34,0x12,0x23,0x54,
108 0x00,0x00,0x22,0x00,
109 0x98,0x23,0x12,0x00,
110 0x00,0x00,0x00,0x00,
111 0xF3,0x20,0x12,0x28,
112 0x00,0x33,0xAF,0xF3,
113 0x00,0x00,0x00,0x1C,
114 };
115
116
117 logger->log_bytes(logger,RAW,"expected header",expected_generation2,sizeof(expected_generation2));
118
119 logger->log_chunk(logger,RAW,"generated header",&generated_data);
120
121 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data 2");
122 allocator_free_chunk(&generated_data);
123
124 header_data->destroy(header_data);
125
126 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
127 generator->destroy(generator);
128 }
129
130 /*
131 * Described in header
132 */
133 void test_generator_with_transform_attribute(tester_t *tester)
134 {
135 generator_t *generator;
136 transform_attribute_t *attribute;
137 chunk_t generated_data;
138 logger_t *logger;
139
140 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"transform_attribute payload");
141
142
143 /* test empty attribute */
144 generator = generator_create();
145 tester->assert_true(tester,(generator != NULL), "generator create check");
146 attribute = transform_attribute_create();
147 generator->generate_payload(generator,(payload_t *)attribute);
148 generator->write_to_chunk(generator,&generated_data);
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 attribute->destroy(attribute);
157 generator->destroy(generator);
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_chunk(attribute,data);
170
171 generator->generate_payload(generator,(payload_t *)attribute);
172 generator->write_to_chunk(generator,&generated_data);
173 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
174
175 u_int8_t expected_generation2[] = {
176 0x80,0x00,0x16,0x88,
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 attribute->destroy(attribute);
182 generator->destroy(generator);
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 attribute->set_value_chunk(attribute,data);
196
197 attribute->set_attribute_type(attribute,456);
198
199
200 generator->generate_payload(generator,(payload_t *)attribute);
201 generator->write_to_chunk(generator,&generated_data);
202 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
203
204 u_int8_t expected_generation3[] = {
205 0x01,0xC8,0x00,0x19,
206 0x64,0x64,0x64,0x64,
207 0x64,0x64,0x64,0x64,
208 0x64,0x64,0x65,0x65,
209 0x65,0x65,0x65,0x65,
210 0x65,0x65,0x65,0x65,
211 0x66,0x66,0x66,0x66,
212 0x66
213 };
214 tester->assert_true(tester,(memcmp(expected_generation3,generated_data.ptr,sizeof(expected_generation3)) == 0), "compare generated data");
215
216 allocator_free_chunk(&generated_data);
217 attribute->destroy(attribute);
218 generator->destroy(generator);
219
220
221 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
222 }
223
224
225
226 /*
227 * Described in header
228 */
229 void test_generator_with_transform_substructure(tester_t *tester)
230 {
231 generator_t *generator;
232 transform_attribute_t *attribute1, *attribute2;
233 transform_substructure_t *transform;
234 chunk_t data;
235 chunk_t generated_data;
236 logger_t *logger;
237
238 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"transform substr.");
239
240 /* create generator */
241 generator = generator_create();
242 tester->assert_true(tester,(generator != NULL), "generator create check");
243
244 /* create attribute 1 */
245 attribute1 = transform_attribute_create();
246 char *stringval = "abcd";
247 data.ptr = (void *) stringval;
248 data.len = 4;
249 attribute1->set_value_chunk(attribute1,data);
250 attribute1->set_attribute_type(attribute1,0);
251 logger->log(logger,CONTROL,"attribute1 created");
252
253 /* create attribute 2 */
254 attribute2 = transform_attribute_create();
255 stringval = "efgh";
256 data.ptr = (void *) stringval;
257 data.len = 4;
258 attribute2->set_value_chunk(attribute2,data);
259 attribute2->set_attribute_type(attribute2,0);
260 logger->log(logger,CONTROL,"attribute2 created");
261
262 /* create transform */
263 transform = transform_substructure_create();
264 tester->assert_true(tester,(transform != NULL), "transform create check");
265 transform->add_transform_attribute(transform,attribute1);
266 transform->add_transform_attribute(transform,attribute2);
267 transform->set_transform_type(transform,5); /* hex 5 */
268 transform->set_transform_id(transform,65000); /* hex FDE8 */
269
270
271 logger->log(logger,CONTROL,"transform created");
272
273 generator->generate_payload(generator,(payload_t *)transform);
274 generator->write_to_chunk(generator,&generated_data);
275 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
276
277 u_int8_t expected_generation3[] = {
278 0x00,0x00,0x00,0x18,
279 0x05,0x00,0xFD,0xE8,
280 0x00,0x00,0x00,0x04,
281 0x61,0x62,0x63,0x64,
282 0x00,0x00,0x00,0x04,
283 0x65,0x66,0x67,0x68,
284 };
285 tester->assert_true(tester,(memcmp(expected_generation3,generated_data.ptr,sizeof(expected_generation3)) == 0), "compare generated data");
286
287 allocator_free_chunk(&generated_data);
288 transform->destroy(transform);
289 generator->destroy(generator);
290
291
292 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
293 }
294
295
296 /*
297 * Described in header
298 */
299 void test_generator_with_proposal_substructure(tester_t *tester)
300 {
301 generator_t *generator;
302 transform_attribute_t *attribute1, *attribute2, *attribute3;
303 transform_substructure_t *transform1, *transform2;
304 proposal_substructure_t *proposal;
305 chunk_t data;
306 chunk_t generated_data;
307 logger_t *logger;
308
309 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"transform substr.");
310
311 /* create generator */
312 generator = generator_create();
313 tester->assert_true(tester,(generator != NULL), "generator create check");
314
315 /* create attribute 1 */
316 attribute1 = transform_attribute_create();
317 char *stringval = "abcd";
318 data.ptr = (void *) stringval;
319 data.len = 4;
320 attribute1->set_value_chunk(attribute1,data);
321 attribute1->set_attribute_type(attribute1,0);
322
323 logger->log(logger,CONTROL,"attribute1 created");
324
325 /* create attribute 2 */
326 attribute2 = transform_attribute_create();
327 stringval = "efgh";
328 data.ptr = (void *) stringval;
329 data.len = 4;
330 attribute2->set_value_chunk(attribute2,data);
331 attribute2->set_attribute_type(attribute2,0);
332 logger->log(logger,CONTROL,"attribute2 created");
333
334 /* create attribute 3 */
335 attribute3 = transform_attribute_create();
336 stringval = "ijkl";
337 data.ptr = (void *) stringval;
338 data.len = 4;
339 attribute3->set_value_chunk(attribute3,data);
340 attribute3->set_attribute_type(attribute3,0);
341 logger->log(logger,CONTROL,"attribute3 created");
342
343 /* create transform 1*/
344 transform1 = transform_substructure_create();
345 tester->assert_true(tester,(transform1 != NULL), "transform create check");
346 transform1->add_transform_attribute(transform1,attribute1);
347 transform1->add_transform_attribute(transform1,attribute2);
348 transform1->set_transform_type(transform1,5); /* hex 5 */
349 transform1->set_transform_id(transform1,65000); /* hex FDE8 */
350
351 /* create transform 2*/
352 transform2 = transform_substructure_create();
353 tester->assert_true(tester,(transform2 != NULL), "transform create check");
354 transform2->add_transform_attribute(transform2,attribute3);
355 transform2->set_transform_type(transform2,3); /* hex 3 */
356 transform2->set_transform_id(transform2,4); /* hex 4 */
357
358 logger->log(logger,CONTROL,"transforms created");
359
360 proposal = proposal_substructure_create();
361 tester->assert_true(tester,(proposal != NULL), "proposal create check");
362
363 stringval = "ABCDEFGH";
364 data.ptr = (void *) stringval;
365 data.len = 8;
366
367 proposal->add_transform_substructure(proposal,transform1);
368 proposal->add_transform_substructure(proposal,transform2);
369 proposal->set_spi(proposal,data);
370 proposal->set_proposal_number(proposal,7);
371 proposal->set_protocol_id(proposal,4);
372
373 generator->generate_payload(generator,(payload_t *)proposal);
374 generator->write_to_chunk(generator,&generated_data);
375 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
376
377 u_int8_t expected_generation[] = {
378 /* proposal header */
379 0x00,0x00,0x00,0x38,
380 0x07,0x04,0x08,0x02,
381 /* SPI */
382 0x41,0x42,0x43,0x44,
383 0x45,0x46,0x47,0x48,
384 /* first transform */
385 0x03,0x00,0x00,0x18,
386 0x05,0x00,0xFD,0xE8,
387 /* first transform attributes */
388 0x00,0x00,0x00,0x04,
389 0x61,0x62,0x63,0x64,
390 0x00,0x00,0x00,0x04,
391 0x65,0x66,0x67,0x68,
392 /* second transform */
393 0x00,0x00,0x00,0x10,
394 0x03,0x00,0x00,0x04,
395 /* second transform attributes */
396 0x00,0x00,0x00,0x04,
397 0x69,0x6A,0x6B,0x6C
398 };
399 logger->log_bytes(logger,RAW,"expected transform",expected_generation,sizeof(expected_generation));
400
401 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
402
403 allocator_free_chunk(&generated_data);
404 proposal->destroy(proposal);
405 generator->destroy(generator);
406
407
408 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
409 }
410
411 /*
412 * Described in header
413 */
414 void test_generator_with_sa_payload(tester_t *tester)
415 {
416 generator_t *generator;
417 transform_attribute_t *attribute1, *attribute2, *attribute3;
418 transform_substructure_t *transform1, *transform2;
419 proposal_substructure_t *proposal1, *proposal2;
420 sa_payload_t *sa_payload;
421 ike_header_t *ike_header;
422
423 chunk_t data;
424 chunk_t generated_data;
425 logger_t *logger;
426
427 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with SA Payload");
428
429 /* create generator */
430 generator = generator_create();
431 tester->assert_true(tester,(generator != NULL), "generator create check");
432
433 /* create attribute 1 */
434 attribute1 = transform_attribute_create();
435 char *stringval = "abcd";
436 data.ptr = (void *) stringval;
437 data.len = 4;
438 attribute1->set_value_chunk(attribute1,data);
439 attribute1->set_attribute_type(attribute1,0);
440 logger->log(logger,CONTROL,"attribute1 created");
441
442 /* create attribute 2 */
443 attribute2 = transform_attribute_create();
444 stringval = "efgh";
445 data.ptr = (void *) stringval;
446 data.len = 4;
447 attribute2->set_value_chunk(attribute2,data);
448 attribute2->set_attribute_type(attribute2,0);
449 logger->log(logger,CONTROL,"attribute2 created");
450
451 /* create attribute 3 */
452 attribute3 = transform_attribute_create();
453 stringval = "ijkl";
454 data.ptr = (void *) stringval;
455 data.len = 4;
456 attribute3->set_value_chunk(attribute3,data);
457 attribute3->set_attribute_type(attribute3,0);
458 logger->log(logger,CONTROL,"attribute3 created");
459
460 /* create transform 1*/
461 transform1 = transform_substructure_create();
462 tester->assert_true(tester,(transform1 != NULL), "transform create check");
463 transform1->add_transform_attribute(transform1,attribute1);
464 transform1->add_transform_attribute(transform1,attribute2);
465 transform1->set_transform_type(transform1,5); /* hex 5 */
466 transform1->set_transform_id(transform1,65000); /* hex FDE8 */
467
468 /* create transform 2*/
469 transform2 = transform_substructure_create();
470 tester->assert_true(tester,(transform2 != NULL), "transform create check");
471 transform2->add_transform_attribute(transform2,attribute3);
472 transform2->set_transform_type(transform2,3); /* hex 3 */
473 transform2->set_transform_id(transform2,4); /* hex 4 */
474
475 logger->log(logger,CONTROL,"transforms created");
476
477 /* create proposal 1 */
478 proposal1 = proposal_substructure_create();
479 tester->assert_true(tester,(proposal1 != NULL), "proposal create check");
480
481 stringval = "ABCDEFGH";
482 data.ptr = (void *) stringval;
483 data.len = 8;
484
485 proposal1->add_transform_substructure(proposal1,transform1);
486 proposal1->add_transform_substructure(proposal1,transform2);
487 proposal1->set_spi(proposal1,data);
488 proposal1->set_proposal_number(proposal1,7);
489 proposal1->set_protocol_id(proposal1,4);
490
491 /* create proposal 2 */
492 proposal2 = proposal_substructure_create();
493 tester->assert_true(tester,(proposal2 != NULL), "proposal create check");
494 proposal2->set_proposal_number(proposal2,7);
495 proposal2->set_protocol_id(proposal2,5);
496
497 /* create sa_payload */
498 sa_payload = sa_payload_create();
499
500 sa_payload->add_proposal_substructure(sa_payload,proposal1);
501 sa_payload->add_proposal_substructure(sa_payload,proposal2);
502
503 ike_header = ike_header_create();
504 ike_header->set_initiator_spi(ike_header,0x22000054231234LL);
505 ike_header->set_responder_spi(ike_header,0x122398);
506 ((payload_t *) ike_header)->set_next_type((payload_t *) ike_header,SECURITY_ASSOCIATION);
507 ike_header->set_exchange_type(ike_header, 0x12);
508 ike_header->set_initiator_flag(ike_header, TRUE);
509 ike_header->set_response_flag(ike_header, TRUE);
510 ike_header->set_message_id(ike_header,0x33AFF3);
511
512 generator->generate_payload(generator,(payload_t *)ike_header);
513 generator->generate_payload(generator,(payload_t *)sa_payload);
514 generator->write_to_chunk(generator,&generated_data);
515 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
516
517 u_int8_t expected_generation[] = {
518 /* sa payload header */
519 0x34,0x12,0x23,0x54,
520 0x00,0x00,0x22,0x00,
521 0x98,0x23,0x12,0x00,
522 0x00,0x00,0x00,0x00,
523 0x21,0x20,0x12,0x28,
524 0x00,0x33,0xAF,0xF3,
525 0x00,0x00,0x00,0x60,
526
527 /* sa payload header */
528 0x00,0x00,0x00,0x44,
529 /* proposal header */
530 0x00,0x00,0x00,0x38,
531 0x07,0x04,0x08,0x02,
532 /* SPI */
533 0x41,0x42,0x43,0x44,
534 0x45,0x46,0x47,0x48,
535 /* first transform */
536 0x03,0x00,0x00,0x18,
537 0x05,0x00,0xFD,0xE8,
538 /* first transform attributes */
539 0x00,0x00,0x00,0x04,
540 0x61,0x62,0x63,0x64,
541 0x00,0x00,0x00,0x04,
542 0x65,0x66,0x67,0x68,
543 /* second transform */
544 0x00,0x00,0x00,0x10,
545 0x03,0x00,0x00,0x04,
546 /* second transform attributes */
547 0x00,0x00,0x00,0x04,
548 0x69,0x6A,0x6B,0x6C,
549 /* proposal header 2*/
550 0x00,0x00,0x00,0x08,
551 0x07,0x05,0x00,0x00,
552
553 };
554
555 logger->log_bytes(logger,RAW,"expected transform",expected_generation,sizeof(expected_generation));
556
557 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
558
559 allocator_free_chunk(&generated_data);
560 ike_header->destroy(ike_header);
561 sa_payload->destroy(sa_payload);
562 generator->destroy(generator);
563
564 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
565
566 }
567
568 /*
569 * Described in header
570 */
571 void test_generator_with_ke_payload(tester_t *tester)
572 {
573 generator_t *generator;
574 ke_payload_t *ke_payload;
575 logger_t *logger;
576 chunk_t generated_data;
577 chunk_t key_exchange_data;
578
579 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with KE Payload");
580
581 /* create generator */
582 generator = generator_create();
583 tester->assert_true(tester,(generator != NULL), "generator create check");
584
585 ke_payload = ke_payload_create();
586
587
588 key_exchange_data.ptr = "test-text";
589 key_exchange_data.len = strlen(key_exchange_data.ptr);
590
591 ke_payload->set_key_exchange_data(ke_payload,key_exchange_data);
592
593 ke_payload->set_dh_group_number(ke_payload,7777);
594
595 generator->generate_payload(generator,(payload_t *)ke_payload);
596 generator->write_to_chunk(generator,&generated_data);
597 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
598
599 u_int8_t expected_generation[] = {
600 /* payload header */
601 0x00,0x00,0x00,0x11,
602 0x1E,0x61,0x00,0x00,
603 /* key exchange data */
604 0x74,0x65,0x73,0x74,
605 0x2D,0x74,0x65,0x78,
606 0x74
607 };
608
609
610 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
611
612 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
613
614 allocator_free_chunk(&generated_data);
615
616 ke_payload->destroy(ke_payload);
617 generator->destroy(generator);
618
619 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
620
621 }
622
623 /*
624 * Described in header
625 */
626 void test_generator_with_notify_payload(tester_t *tester)
627 {
628 generator_t *generator;
629 notify_payload_t *notify_payload;
630 logger_t *logger;
631 chunk_t generated_data;
632 chunk_t spi,notification_data;
633
634 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with Notify Payload");
635
636 /* create generator */
637 generator = generator_create();
638 tester->assert_true(tester,(generator != NULL), "generator create check");
639
640 notify_payload = notify_payload_create();
641
642
643 spi.ptr = "12345";
644 spi.len = strlen(spi.ptr);
645
646 notification_data.ptr = "67890";
647 notification_data.len = strlen(notification_data.ptr);
648
649 notify_payload->set_protocol_id(notify_payload,255);
650 notify_payload->set_notify_message_type(notify_payload,63333); /* Hex F765 */
651 notify_payload->set_spi(notify_payload,spi);
652 notify_payload->set_notification_data(notify_payload,notification_data);
653
654 generator->generate_payload(generator,(payload_t *)notify_payload);
655 generator->write_to_chunk(generator,&generated_data);
656 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
657
658 u_int8_t expected_generation[] = {
659 /* payload header */
660 0x00,0x00,0x00,0x12,
661 0xFF,0x05,0xF7,0x65,
662 /* spi */
663 0x31,0x32,0x33,0x34,
664 0x35,
665 /* notification data */
666 0x36,0x37,0x38,0x39,
667 0x30,
668 };
669
670 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
671
672 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
673
674 allocator_free_chunk(&generated_data);
675
676 notify_payload->destroy(notify_payload);
677 generator->destroy(generator);
678
679 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
680
681 }
682
683 /*
684 * Described in header
685 */
686 void test_generator_with_nonce_payload(tester_t *tester)
687 {
688 generator_t *generator;
689 nonce_payload_t *nonce_payload;
690 logger_t *logger;
691 chunk_t generated_data;
692 chunk_t nonce;
693
694 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with Nonce Payload");
695
696 /* create generator */
697 generator = generator_create();
698 tester->assert_true(tester,(generator != NULL), "generator create check");
699
700 nonce_payload = nonce_payload_create();
701
702
703 nonce.ptr = "1234567890123456";
704 nonce.len = strlen("1234567890123456");
705
706 nonce_payload->set_nonce(nonce_payload,nonce);
707
708 generator->generate_payload(generator,(payload_t *)nonce_payload);
709 generator->write_to_chunk(generator,&generated_data);
710 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
711
712
713 u_int8_t expected_generation[] = {
714 /* payload header */
715 0x00,0x00,0x00,0x14,
716 /* nonce data */
717 0x31,0x32,0x33,0x34,
718 0x35,0x36,0x37,0x38,
719 0x39,0x30,0x31,0x32,
720 0x33,0x34,0x35,0x36
721 };
722
723 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
724
725 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
726
727 allocator_free_chunk(&generated_data);
728
729
730 nonce_payload->destroy(nonce_payload);
731 generator->destroy(generator);
732
733 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
734
735 }
736
737 /*
738 * Described in header.
739 */
740 void test_generator_with_id_payload(tester_t *tester)
741 {
742 generator_t *generator;
743 id_payload_t *id_payload;
744 logger_t *logger;
745 chunk_t generated_data;
746 chunk_t id;
747
748 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with ID Payload");
749
750 /* create generator */
751 generator = generator_create();
752 tester->assert_true(tester,(generator != NULL), "generator create check");
753
754 id_payload = id_payload_create(FALSE);
755
756
757 id.ptr = "123456789012";
758 id.len = strlen(id.ptr);
759
760 id_payload->set_id_type(id_payload,ID_IPV4_ADDR);
761 id_payload->set_data(id_payload,id);
762
763 generator->generate_payload(generator,(payload_t *)id_payload);
764 generator->write_to_chunk(generator,&generated_data);
765 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
766
767
768 u_int8_t expected_generation[] = {
769 /* payload header */
770 0x00,0x00,0x00,0x14,
771 0x01,0x00,0x00,0x00,
772 /* id data */
773 0x31,0x32,0x33,0x34,
774 0x35,0x36,0x37,0x38,
775 0x39,0x30,0x31,0x32,
776 };
777
778 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
779
780 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
781
782 allocator_free_chunk(&generated_data);
783
784 id_payload->destroy(id_payload);
785 generator->destroy(generator);
786
787 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
788 }
789
790 /*
791 * Described in header.
792 */
793 void test_generator_with_auth_payload(tester_t *tester)
794 {
795 generator_t *generator;
796 auth_payload_t *auth_payload;
797 logger_t *logger;
798 chunk_t generated_data;
799 chunk_t auth;
800
801 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with AUTH Payload");
802
803 /* create generator */
804 generator = generator_create();
805 tester->assert_true(tester,(generator != NULL), "generator create check");
806
807 auth_payload = auth_payload_create(FALSE);
808
809
810 auth.ptr = "123456789012";
811 auth.len = strlen(auth.ptr);
812
813 auth_payload->set_auth_method(auth_payload,SHARED_KEY_MESSAGE_INTEGRITY_CODE);
814 auth_payload->set_data(auth_payload,auth);
815
816 generator->generate_payload(generator,(payload_t *)auth_payload);
817 generator->write_to_chunk(generator,&generated_data);
818 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
819
820
821 u_int8_t expected_generation[] = {
822 /* payload header */
823 0x00,0x00,0x00,0x14,
824 0x02,0x00,0x00,0x00,
825 /* auth data */
826 0x31,0x32,0x33,0x34,
827 0x35,0x36,0x37,0x38,
828 0x39,0x30,0x31,0x32,
829 };
830
831 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
832
833 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
834
835 allocator_free_chunk(&generated_data);
836
837 auth_payload->destroy(auth_payload);
838 generator->destroy(generator);
839
840 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
841 }
842
843 /*
844 * Described in header.
845 */
846 void test_generator_with_ts_payload(tester_t *tester)
847 {
848 generator_t *generator;
849 ts_payload_t *ts_payload;
850 traffic_selector_substructure_t *ts1, *ts2;
851 host_t *start_host1, *start_host2, *end_host1, *end_host2;
852 logger_t *logger;
853 chunk_t generated_data;
854
855 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with TS Payload");
856
857 /* create generator */
858 generator = generator_create();
859 tester->assert_true(tester,(generator != NULL), "generator create check");
860
861 ts_payload = ts_payload_create(TRUE);
862
863 /* first traffic selector */
864 ts1 = traffic_selector_substructure_create();
865
866 start_host1 = host_create(AF_INET,"192.168.1.0",500);
867 ts1->set_start_host(ts1,start_host1);
868 start_host1->destroy(start_host1);
869
870 end_host1 = host_create(AF_INET,"192.168.1.255",500);
871 ts1->set_end_host(ts1,end_host1);
872 end_host1->destroy(end_host1);
873
874 ts_payload->add_traffic_selector_substructure(ts_payload,ts1);
875
876 /* second traffic selector */
877
878 ts2 = traffic_selector_substructure_create();
879
880 start_host2 = host_create(AF_INET,"0.0.0.0",0);
881 ts2->set_start_host(ts2,start_host2);
882 ts2->set_protocol_id(ts2,3);
883 start_host2->destroy(start_host2);
884
885 end_host2 = host_create(AF_INET,"255.255.255.255",65535);
886 ts2->set_end_host(ts2,end_host2);
887 end_host2->destroy(end_host2);
888
889 ts_payload->add_traffic_selector_substructure(ts_payload,ts2);
890
891
892 generator->generate_payload(generator,(payload_t *)ts_payload);
893 generator->write_to_chunk(generator,&generated_data);
894 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
895
896
897 u_int8_t expected_generation[] = {
898 /* payload header */
899 0x00,0x00,0x00,0x28,
900 0x02,0x00,0x00,0x00,
901
902 /* traffic selector 1 */
903 0x07,0x00,0x00,0x10,
904 0x01,0xF4,0x01,0xF4,
905 0xC0,0xA8,0x01,0x00,
906 0xC0,0xA8,0x01,0xFF,
907
908 /* traffic selector 2 */
909 0x07,0x03,0x00,0x10,
910 0x00,0x00,0xFF,0xFF,
911 0x00,0x00,0x00,0x00,
912 0xFF,0xFF,0xFF,0xFF,
913 };
914
915 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
916
917 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
918
919 allocator_free_chunk(&generated_data);
920
921 ts_payload->destroy(ts_payload);
922 generator->destroy(generator);
923
924 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
925 }