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