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