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