4dacbf30f7b2154ab880ad6be2f5dd2adf9f4da6
[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 <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
42 /*
43 * Described in Header
44 */
45 void test_generator_with_header_payload(tester_t *tester)
46 {
47 generator_t *generator;
48 ike_header_t *header_data;
49 chunk_t generated_data;
50 status_t status;
51 logger_t *logger;
52
53 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"header payload");
54
55 header_data = ike_header_create();
56 header_data->set_initiator_spi(header_data,1);
57 header_data->set_responder_spi(header_data,2);
58 ((payload_t *) header_data)->set_next_type((payload_t *) header_data, 3);
59 header_data->set_exchange_type(header_data, 6);
60 header_data->set_initiator_flag(header_data, TRUE);
61 header_data->set_response_flag(header_data, TRUE);
62 header_data->set_message_id(header_data,7);
63
64 generator = generator_create();
65 tester->assert_true(tester,(generator != NULL), "generator create check");
66
67 generator->generate_payload(generator,(payload_t *) header_data);
68
69 generator->write_to_chunk(generator,&generated_data);
70
71 u_int8_t expected_generation[] = {
72 0x01,0x00,0x00,0x00,
73 0x00,0x00,0x00,0x00,
74 0x02,0x00,0x00,0x00,
75 0x00,0x00,0x00,0x00,
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 generator->destroy(generator);
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 generator->generate_payload(generator,(payload_t *)header_data);
101 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
102
103 generator->write_to_chunk(generator,&generated_data);
104
105 u_int8_t expected_generation2[] = {
106 0x34,0x12,0x23,0x54,
107 0x00,0x00,0x22,0x00,
108 0x98,0x23,0x12,0x00,
109 0x00,0x00,0x00,0x00,
110 0xF3,0x20,0x12,0x28,
111 0x00,0x33,0xAF,0xF3,
112 0x00,0x00,0x00,0x1C,
113 };
114
115
116 logger->log_bytes(logger,RAW,"expected header",expected_generation2,sizeof(expected_generation2));
117
118 logger->log_chunk(logger,RAW,"generated header",&generated_data);
119
120 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data 2");
121 allocator_free_chunk(&generated_data);
122
123 header_data->destroy(header_data);
124
125 global_logger_manager->destroy_logger(global_logger_manager,logger);
126 generator->destroy(generator);
127 }
128
129 /*
130 * Described in header
131 */
132 void test_generator_with_transform_attribute(tester_t *tester)
133 {
134 generator_t *generator;
135 transform_attribute_t *attribute;
136 status_t status;
137 chunk_t generated_data;
138 logger_t *logger;
139
140 logger = global_logger_manager->create_logger(global_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 tester->assert_true(tester,(attribute->destroy(attribute) == SUCCESS), "attribute destroy call check");
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 tester->assert_true(tester,(attribute->destroy(attribute) == SUCCESS), "attribute destroy call check");
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 status = attribute->set_value_chunk(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 generator->generate_payload(generator,(payload_t *)attribute);
203 generator->write_to_chunk(generator,&generated_data);
204 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
205
206 u_int8_t expected_generation3[] = {
207 0x01,0xC8,0x00,0x19,
208 0x64,0x64,0x64,0x64,
209 0x64,0x64,0x64,0x64,
210 0x64,0x64,0x65,0x65,
211 0x65,0x65,0x65,0x65,
212 0x65,0x65,0x65,0x65,
213 0x66,0x66,0x66,0x66,
214 0x66
215 };
216 tester->assert_true(tester,(memcmp(expected_generation3,generated_data.ptr,sizeof(expected_generation3)) == 0), "compare generated data");
217
218 allocator_free_chunk(&generated_data);
219 tester->assert_true(tester,(attribute->destroy(attribute) == SUCCESS), "attribute destroy call check");
220 generator->destroy(generator);
221
222
223 global_logger_manager->destroy_logger(global_logger_manager,logger);
224 }
225
226
227
228 /*
229 * Described in header
230 */
231 void test_generator_with_transform_substructure(tester_t *tester)
232 {
233 generator_t *generator;
234 transform_attribute_t *attribute1, *attribute2;
235 transform_substructure_t *transform;
236 chunk_t data;
237 status_t status;
238 chunk_t generated_data;
239 logger_t *logger;
240
241 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"transform substr.");
242
243 /* create generator */
244 generator = generator_create();
245 tester->assert_true(tester,(generator != NULL), "generator create check");
246
247 /* create attribute 1 */
248 attribute1 = transform_attribute_create();
249 char *stringval = "abcd";
250 data.ptr = (void *) stringval;
251 data.len = 4;
252 status = attribute1->set_value_chunk(attribute1,data);
253 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
254 status = attribute1->set_attribute_type(attribute1,0);
255 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
256 logger->log(logger,CONTROL,"attribute1 created");
257
258 /* create attribute 2 */
259 attribute2 = transform_attribute_create();
260 stringval = "efgh";
261 data.ptr = (void *) stringval;
262 data.len = 4;
263 status = attribute2->set_value_chunk(attribute2,data);
264 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
265 status = attribute2->set_attribute_type(attribute2,0);
266 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
267 logger->log(logger,CONTROL,"attribute2 created");
268
269 /* create transform */
270 transform = transform_substructure_create();
271 tester->assert_true(tester,(transform != NULL), "transform create check");
272 status = transform->add_transform_attribute(transform,attribute1);
273 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
274 status = transform->add_transform_attribute(transform,attribute2);
275 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
276 status = transform->set_transform_type(transform,5); /* hex 5 */
277 tester->assert_true(tester,(status == SUCCESS),"set_transform_type call check");
278 status = transform->set_transform_id(transform,65000); /* hex FDE8 */
279 tester->assert_true(tester,(status == SUCCESS),"set_transform_id call check");
280
281
282 logger->log(logger,CONTROL,"transform created");
283
284 generator->generate_payload(generator,(payload_t *)transform);
285 generator->write_to_chunk(generator,&generated_data);
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 generator->destroy(generator);
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_chunk(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_chunk(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_chunk(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 generator->generate_payload(generator,(payload_t *)proposal);
404 generator->write_to_chunk(generator,&generated_data);
405 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
406
407 u_int8_t expected_generation[] = {
408 /* proposal header */
409 0x00,0x00,0x00,0x38,
410 0x07,0x04,0x08,0x02,
411 /* SPI */
412 0x41,0x42,0x43,0x44,
413 0x45,0x46,0x47,0x48,
414 /* first transform */
415 0x03,0x00,0x00,0x18,
416 0x05,0x00,0xFD,0xE8,
417 /* first transform attributes */
418 0x00,0x00,0x00,0x04,
419 0x61,0x62,0x63,0x64,
420 0x00,0x00,0x00,0x04,
421 0x65,0x66,0x67,0x68,
422 /* second transform */
423 0x00,0x00,0x00,0x10,
424 0x03,0x00,0x00,0x04,
425 /* second transform attributes */
426 0x00,0x00,0x00,0x04,
427 0x69,0x6A,0x6B,0x6C
428 };
429 logger->log_bytes(logger,RAW,"expected transform",expected_generation,sizeof(expected_generation));
430
431 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
432
433 allocator_free_chunk(&generated_data);
434 tester->assert_true(tester,(proposal->destroy(proposal) == SUCCESS), "proposal destroy call check");
435 generator->destroy(generator);
436
437
438 global_logger_manager->destroy_logger(global_logger_manager,logger);
439 }
440
441 /*
442 * Described in header
443 */
444 void test_generator_with_sa_payload(tester_t *tester)
445 {
446 generator_t *generator;
447 transform_attribute_t *attribute1, *attribute2, *attribute3;
448 transform_substructure_t *transform1, *transform2;
449 proposal_substructure_t *proposal1, *proposal2;
450 sa_payload_t *sa_payload;
451 ike_header_t *ike_header;
452
453 chunk_t data;
454 status_t status;
455 chunk_t generated_data;
456 logger_t *logger;
457
458 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"Message with SA Payload");
459
460 /* create generator */
461 generator = generator_create();
462 tester->assert_true(tester,(generator != NULL), "generator create check");
463
464 /* create attribute 1 */
465 attribute1 = transform_attribute_create();
466 char *stringval = "abcd";
467 data.ptr = (void *) stringval;
468 data.len = 4;
469 status = attribute1->set_value_chunk(attribute1,data);
470 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
471 status = attribute1->set_attribute_type(attribute1,0);
472 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
473 logger->log(logger,CONTROL,"attribute1 created");
474
475 /* create attribute 2 */
476 attribute2 = transform_attribute_create();
477 stringval = "efgh";
478 data.ptr = (void *) stringval;
479 data.len = 4;
480 status = attribute2->set_value_chunk(attribute2,data);
481 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
482 status = attribute2->set_attribute_type(attribute2,0);
483 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
484 logger->log(logger,CONTROL,"attribute2 created");
485
486 /* create attribute 3 */
487 attribute3 = transform_attribute_create();
488 stringval = "ijkl";
489 data.ptr = (void *) stringval;
490 data.len = 4;
491 status = attribute3->set_value_chunk(attribute3,data);
492 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
493 status = attribute3->set_attribute_type(attribute3,0);
494 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
495 logger->log(logger,CONTROL,"attribute3 created");
496
497 /* create transform 1*/
498 transform1 = transform_substructure_create();
499 tester->assert_true(tester,(transform1 != NULL), "transform create check");
500 status = transform1->add_transform_attribute(transform1,attribute1);
501 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
502 status = transform1->add_transform_attribute(transform1,attribute2);
503 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
504 status = transform1->set_transform_type(transform1,5); /* hex 5 */
505 tester->assert_true(tester,(status == SUCCESS),"set_transform_type call check");
506 status = transform1->set_transform_id(transform1,65000); /* hex FDE8 */
507 tester->assert_true(tester,(status == SUCCESS),"set_transform_id call check");
508
509 /* create transform 2*/
510 transform2 = transform_substructure_create();
511 tester->assert_true(tester,(transform2 != NULL), "transform create check");
512 status = transform2->add_transform_attribute(transform2,attribute3);
513 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
514 status = transform2->set_transform_type(transform2,3); /* hex 3 */
515 tester->assert_true(tester,(status == SUCCESS),"set_transform_type call check");
516 status = transform2->set_transform_id(transform2,4); /* hex 4 */
517 tester->assert_true(tester,(status == SUCCESS),"set_transform_id call check");
518
519 logger->log(logger,CONTROL,"transforms created");
520
521 /* create proposal 1 */
522 proposal1 = proposal_substructure_create();
523 tester->assert_true(tester,(proposal1 != NULL), "proposal create check");
524
525 stringval = "ABCDEFGH";
526 data.ptr = (void *) stringval;
527 data.len = 8;
528
529 status = proposal1->add_transform_substructure(proposal1,transform1);
530 tester->assert_true(tester,(status == SUCCESS),"add_transform_substructure call check");
531 status = proposal1->add_transform_substructure(proposal1,transform2);
532 tester->assert_true(tester,(status == SUCCESS),"add_transform_substructure call check");
533 status = proposal1->set_spi(proposal1,data);
534 tester->assert_true(tester,(status == SUCCESS),"set_spi call check");
535 status = proposal1->set_proposal_number(proposal1,7);
536 tester->assert_true(tester,(status == SUCCESS),"set_proposal_number call check");
537 status = proposal1->set_protocol_id(proposal1,4);
538 tester->assert_true(tester,(status == SUCCESS),"set_protocol_id call check");
539
540 /* create proposal 2 */
541 proposal2 = proposal_substructure_create();
542 tester->assert_true(tester,(proposal2 != NULL), "proposal create check");
543 status = proposal2->set_proposal_number(proposal2,7);
544 tester->assert_true(tester,(status == SUCCESS),"set_proposal_number call check");
545 status = proposal2->set_protocol_id(proposal2,5);
546 tester->assert_true(tester,(status == SUCCESS),"set_protocol_id call check");
547
548 /* create sa_payload */
549 sa_payload = sa_payload_create();
550
551 sa_payload->add_proposal_substructure(sa_payload,proposal1);
552 sa_payload->add_proposal_substructure(sa_payload,proposal2);
553
554 ike_header = ike_header_create();
555 ike_header->set_initiator_spi(ike_header,0x22000054231234LL);
556 ike_header->set_responder_spi(ike_header,0x122398);
557 ((payload_t *) ike_header)->set_next_type((payload_t *) ike_header,SECURITY_ASSOCIATION);
558 ike_header->set_exchange_type(ike_header, 0x12);
559 ike_header->set_initiator_flag(ike_header, TRUE);
560 ike_header->set_response_flag(ike_header, TRUE);
561 ike_header->set_message_id(ike_header,0x33AFF3);
562
563 generator->generate_payload(generator,(payload_t *)ike_header);
564 generator->generate_payload(generator,(payload_t *)sa_payload);
565 generator->write_to_chunk(generator,&generated_data);
566 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
567
568 u_int8_t expected_generation[] = {
569 /* sa payload header */
570 0x34,0x12,0x23,0x54,
571 0x00,0x00,0x22,0x00,
572 0x98,0x23,0x12,0x00,
573 0x00,0x00,0x00,0x00,
574 0x21,0x20,0x12,0x28,
575 0x00,0x33,0xAF,0xF3,
576 0x00,0x00,0x00,0x60,
577
578 /* sa payload header */
579 0x00,0x00,0x00,0x44,
580 /* proposal header */
581 0x00,0x00,0x00,0x38,
582 0x07,0x04,0x08,0x02,
583 /* SPI */
584 0x41,0x42,0x43,0x44,
585 0x45,0x46,0x47,0x48,
586 /* first transform */
587 0x03,0x00,0x00,0x18,
588 0x05,0x00,0xFD,0xE8,
589 /* first transform attributes */
590 0x00,0x00,0x00,0x04,
591 0x61,0x62,0x63,0x64,
592 0x00,0x00,0x00,0x04,
593 0x65,0x66,0x67,0x68,
594 /* second transform */
595 0x00,0x00,0x00,0x10,
596 0x03,0x00,0x00,0x04,
597 /* second transform attributes */
598 0x00,0x00,0x00,0x04,
599 0x69,0x6A,0x6B,0x6C,
600 /* proposal header 2*/
601 0x00,0x00,0x00,0x08,
602 0x07,0x05,0x00,0x00,
603
604 };
605
606 logger->log_bytes(logger,RAW,"expected transform",expected_generation,sizeof(expected_generation));
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 generator->destroy(generator);
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 chunk_t generated_data;
628 chunk_t key_exchange_data;
629
630 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"Message with KE Payload");
631
632 /* create generator */
633 generator = generator_create();
634 tester->assert_true(tester,(generator != NULL), "generator create check");
635
636 ke_payload = ke_payload_create();
637
638
639 key_exchange_data.ptr = "test-text";
640 key_exchange_data.len = strlen(key_exchange_data.ptr);
641
642 ke_payload->set_key_exchange_data(ke_payload,key_exchange_data);
643
644 ke_payload->set_dh_group_number(ke_payload,7777);
645
646 generator->generate_payload(generator,(payload_t *)ke_payload);
647 generator->write_to_chunk(generator,&generated_data);
648 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
649
650 u_int8_t expected_generation[] = {
651 /* payload header */
652 0x00,0x00,0x00,0x11,
653 0x1E,0x61,0x00,0x00,
654 /* key exchange data */
655 0x74,0x65,0x73,0x74,
656 0x2D,0x74,0x65,0x78,
657 0x74
658 };
659
660
661 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
662
663 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
664
665 allocator_free_chunk(&generated_data);
666
667 tester->assert_true(tester,(ke_payload->destroy(ke_payload) == SUCCESS), "sa_payload destroy call check");
668 generator->destroy(generator);
669
670 global_logger_manager->destroy_logger(global_logger_manager,logger);
671
672 }
673
674 /*
675 * Described in header
676 */
677 void test_generator_with_notify_payload(tester_t *tester)
678 {
679 generator_t *generator;
680 notify_payload_t *notify_payload;
681 logger_t *logger;
682 chunk_t generated_data;
683 chunk_t spi,notification_data;
684
685 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"Message with Notify Payload");
686
687 /* create generator */
688 generator = generator_create();
689 tester->assert_true(tester,(generator != NULL), "generator create check");
690
691 notify_payload = notify_payload_create();
692
693
694 spi.ptr = "12345";
695 spi.len = strlen(spi.ptr);
696
697 notification_data.ptr = "67890";
698 notification_data.len = strlen(notification_data.ptr);
699
700 notify_payload->set_protocol_id(notify_payload,255);
701 notify_payload->set_notify_message_type(notify_payload,63333); /* Hex F765 */
702 notify_payload->set_spi(notify_payload,spi);
703 notify_payload->set_notification_data(notify_payload,notification_data);
704
705 generator->generate_payload(generator,(payload_t *)notify_payload);
706 generator->write_to_chunk(generator,&generated_data);
707 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
708
709 u_int8_t expected_generation[] = {
710 /* payload header */
711 0x00,0x00,0x00,0x12,
712 0xFF,0x05,0xF7,0x65,
713 /* spi */
714 0x31,0x32,0x33,0x34,
715 0x35,
716 /* notification data */
717 0x36,0x37,0x38,0x39,
718 0x30,
719 };
720
721 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
722
723 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
724
725 allocator_free_chunk(&generated_data);
726
727 tester->assert_true(tester,(notify_payload->destroy(notify_payload) == SUCCESS), "notify_payload destroy call check");
728 generator->destroy(generator);
729
730 global_logger_manager->destroy_logger(global_logger_manager,logger);
731
732 }
733
734 /*
735 * Described in header
736 */
737 void test_generator_with_nonce_payload(tester_t *tester)
738 {
739 generator_t *generator;
740 nonce_payload_t *nonce_payload;
741 logger_t *logger;
742 chunk_t generated_data;
743 chunk_t nonce;
744
745 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"Message with Nonce Payload");
746
747 /* create generator */
748 generator = generator_create();
749 tester->assert_true(tester,(generator != NULL), "generator create check");
750
751 nonce_payload = nonce_payload_create();
752
753
754 nonce.ptr = "1234567890123456";
755 nonce.len = strlen("1234567890123456");
756
757 nonce_payload->set_nonce(nonce_payload,nonce);
758
759 generator->generate_payload(generator,(payload_t *)nonce_payload);
760 generator->write_to_chunk(generator,&generated_data);
761 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
762
763
764 u_int8_t expected_generation[] = {
765 /* payload header */
766 0x00,0x00,0x00,0x14,
767 /* nonce data */
768 0x31,0x32,0x33,0x34,
769 0x35,0x36,0x37,0x38,
770 0x39,0x30,0x31,0x32,
771 0x33,0x34,0x35,0x36
772 };
773
774 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
775
776 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
777
778 allocator_free_chunk(&generated_data);
779
780
781 tester->assert_true(tester,(nonce_payload->destroy(nonce_payload) == SUCCESS), "notify_payload destroy call check");
782 generator->destroy(generator);
783
784 global_logger_manager->destroy_logger(global_logger_manager,logger);
785
786 }
787