- wrote logger_manager
[strongswan.git] / Source / charon / tests / 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 "../globals.h"
26 #include "../allocator.h"
27 #include "../logger_manager.h"
28 #include "generator_test.h"
29 #include "../tester.h"
30 #include "../logger.h"
31 #include "../encodings.h"
32 #include "../generator.h"
33 #include "../encodings/ike_header.h"
34
35 extern payload_info_t *payload_infos[];
36
37 /*
38 * Described in Header
39 */
40 void test_generator_with_unsupported_payload(tester_t *tester)
41 {
42 generator_t *generator;
43 generator_context_t *generator_context;
44 void * data_struct;
45
46 generator = generator_create(payload_infos);
47 tester->assert_true(tester,(generator != NULL), "generator create check");
48
49 generator_context = generator->create_context(generator);
50
51 tester->assert_true(tester,(generator->generate_payload(generator,(payload_type_t) -1,data_struct,generator_context) == NOT_SUPPORTED),"generate_payload call check");
52
53 generator_context->destroy(generator_context);
54
55 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
56 }
57
58 /*
59 * Described in Header
60 */
61 void test_generator_with_header_payload(tester_t *tester)
62 {
63 generator_t *generator;
64 generator_context_t *generator_context;
65 ike_header_t header_data;
66 chunk_t generated_data;
67 status_t status;
68 logger_t *logger;
69
70 global_logger_manager->get_logger(global_logger_manager,TESTER,&logger,"header payload");
71
72 header_data.initiator_spi = 1;
73 header_data.responder_spi = 2;
74 header_data.next_payload = 3;
75 header_data.maj_version = 4;
76 header_data.min_version = 5;
77 header_data.exchange_type = 6;
78 header_data.flags.initiator = TRUE;
79 header_data.flags.version = FALSE;
80 header_data.flags.response = TRUE;
81 header_data.message_id = 7;
82 header_data.length = 8;
83
84 generator = generator_create(payload_infos);
85 tester->assert_true(tester,(generator != NULL), "generator create check");
86
87 generator_context = generator->create_context(generator);
88 tester->assert_true(tester,(generator_context != NULL), "generator_context create check");
89
90 status = generator->generate_payload(generator,HEADER,&header_data,generator_context);
91 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
92
93 tester->assert_true(tester,(generator->write_to_chunk(generator,generator_context,&generated_data) == SUCCESS),"write_to_chunk call check");
94
95 u_int8_t expected_generation[] = {
96 0x00,0x00,0x00,0x00,
97 0x00,0x00,0x00,0x01,
98 0x00,0x00,0x00,0x00,
99 0x00,0x00,0x00,0x02,
100 0x03,0x45,0x06,0x28,
101 0x00,0x00,0x00,0x07,
102 0x00,0x00,0x00,0x08,
103 };
104
105
106 tester->assert_true(tester,(generated_data.len == sizeof(expected_generation)), "compare generated data length");
107 logger->log_chunk(logger,RAW,"generated header",&generated_data);
108 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data 1");
109 allocator_free_chunk(generated_data);
110 generator_context->destroy(generator_context);
111
112
113 header_data.initiator_spi = 0x22000054231234;
114 header_data.responder_spi = 0x122398;
115 header_data.next_payload = 0xF3;
116 header_data.maj_version = 0x2;
117 header_data.min_version = 0x0;
118 header_data.exchange_type = 0x12;
119 header_data.flags.initiator = TRUE;
120 header_data.flags.version = TRUE;
121 header_data.flags.response = TRUE;
122 header_data.message_id = 0x33AFF3;
123 header_data.length = 0xAA11F;
124
125 generator_context = generator->create_context(generator);
126
127 status = generator->generate_payload(generator,HEADER,&header_data,generator_context);
128 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
129
130 tester->assert_true(tester,(generator->write_to_chunk(generator,generator_context,&generated_data) == SUCCESS),"write_to_chunk call check");
131
132 u_int8_t expected_generation2[] = {
133 0x00,0x22,0x00,0x00,
134 0x54,0x23,0x12,0x34,
135 0x00,0x00,0x00,0x00,
136 0x00,0x12,0x23,0x98,
137 0xF3,0x20,0x12,0x38,
138 0x00,0x33,0xAF,0xF3,
139 0x00,0x0A,0xA1,0x1F,
140 };
141
142 logger->log_chunk(logger,RAW,"generated header",&generated_data);
143
144 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data 2");
145 allocator_free_chunk(generated_data);
146
147 generator_context->destroy(generator_context);
148 global_logger_manager->destroy_logger(global_logger_manager,logger);
149 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
150 }