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