- transform_substructure fully implemented
[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
37 /*
38 * Described in Header
39 */
40 void test_generator_with_header_payload(tester_t *tester)
41 {
42 generator_t *generator;
43 ike_header_t *header_data;
44 chunk_t generated_data;
45 status_t status;
46 logger_t *logger;
47
48 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"header payload");
49
50 header_data = ike_header_create();
51
52 header_data->initiator_spi = 1;
53 header_data->responder_spi = 2;
54 header_data->next_payload = 3;
55 header_data->maj_version = 4;
56 header_data->min_version = 5;
57 header_data->exchange_type = 6;
58 header_data->flags.initiator = TRUE;
59 header_data->flags.version = FALSE;
60 header_data->flags.response = TRUE;
61 header_data->message_id = 7;
62 header_data->length = 8;
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,0x45,0x06,0x28,
78 0x00,0x00,0x00,0x07,
79 0x00,0x00,0x00,0x08,
80 };
81
82
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->initiator_spi = 0x22000054231234;
91 header_data->responder_spi = 0x122398;
92 header_data->next_payload = 0xF3;
93 header_data->maj_version = 0x2;
94 header_data->min_version = 0x0;
95 header_data->exchange_type = 0x12;
96 header_data->flags.initiator = TRUE;
97 header_data->flags.version = TRUE;
98 header_data->flags.response = TRUE;
99 header_data->message_id = 0x33AFF3;
100 header_data->length = 0xAA11F;
101
102 generator = generator_create();
103 tester->assert_true(tester,(generator != NULL), "generator create check");
104
105 status = generator->generate_payload(generator,(payload_t *)header_data);
106 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
107
108 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
109
110 u_int8_t expected_generation2[] = {
111 0x00,0x22,0x00,0x00,
112 0x54,0x23,0x12,0x34,
113 0x00,0x00,0x00,0x00,
114 0x00,0x12,0x23,0x98,
115 0xF3,0x20,0x12,0x38,
116 0x00,0x33,0xAF,0xF3,
117 0x00,0x0A,0xA1,0x1F,
118 };
119
120 logger->log_chunk(logger,RAW,"generated header",&generated_data);
121
122 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data 2");
123 allocator_free_chunk(generated_data);
124
125 header_data->destroy(header_data);
126
127 global_logger_manager->destroy_logger(global_logger_manager,logger);
128 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
129 }
130
131 /*
132 * Described in header
133 */
134 void test_generator_with_transform_attribute(tester_t *tester)
135 {
136 generator_t *generator;
137 transform_attribute_t *attribute;
138 status_t status;
139 chunk_t generated_data;
140 logger_t *logger;
141
142 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"transform_attribute payload");
143
144
145 /* test empty attribute */
146 generator = generator_create();
147 tester->assert_true(tester,(generator != NULL), "generator create check");
148 attribute = transform_attribute_create();
149 status = generator->generate_payload(generator,(payload_t *)attribute);
150 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
151 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
152 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
153
154 u_int8_t expected_generation[] = {
155 0x80,0x00,0x00,0x00,
156 };
157 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
158 allocator_free_chunk(generated_data);
159 tester->assert_true(tester,(attribute->destroy(attribute) == SUCCESS), "attribute destroy call check");
160 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
161
162 /* test attribute with 2 byte data */
163 generator = generator_create();
164 tester->assert_true(tester,(generator != NULL), "generator create check");
165
166 attribute = transform_attribute_create();
167 u_int16_t dataval = 5768;
168 chunk_t data;
169 data.ptr = (void *) &dataval;
170 data.len = 2;
171
172 attribute->set_value(attribute,data);
173
174 status = generator->generate_payload(generator,(payload_t *)attribute);
175 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
176 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
177 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
178
179 u_int8_t expected_generation2[] = {
180 0x80,0x00,0x88,0x16,
181 };
182 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data");
183
184 allocator_free_chunk(generated_data);
185 tester->assert_true(tester,(attribute->destroy(attribute) == SUCCESS), "attribute destroy call check");
186 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
187
188
189
190 /* test attribute with 25 byte data */
191 generator = generator_create();
192 tester->assert_true(tester,(generator != NULL), "generator create check");
193
194 attribute = transform_attribute_create();
195 char *stringval = "ddddddddddeeeeeeeeeefffff";
196 data.ptr = (void *) stringval;
197 data.len = 25;
198
199 status = attribute->set_value(attribute,data);
200 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
201
202 status = attribute->set_attribute_type(attribute,456);
203 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
204
205
206 status = generator->generate_payload(generator,(payload_t *)attribute);
207 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
208 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
209 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
210
211 u_int8_t expected_generation3[] = {
212 0x01,0xC8,0x00,0x19,
213 0x64,0x64,0x64,0x64,
214 0x64,0x64,0x64,0x64,
215 0x64,0x64,0x65,0x65,
216 0x65,0x65,0x65,0x65,
217 0x65,0x65,0x65,0x65,
218 0x66,0x66,0x66,0x66,
219 0x66
220 };
221 tester->assert_true(tester,(memcmp(expected_generation3,generated_data.ptr,sizeof(expected_generation3)) == 0), "compare generated data");
222
223 allocator_free_chunk(generated_data);
224 tester->assert_true(tester,(attribute->destroy(attribute) == SUCCESS), "attribute destroy call check");
225 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
226
227
228 global_logger_manager->destroy_logger(global_logger_manager,logger);
229 }
230
231
232
233 /*
234 * Described in header
235 */
236 void test_generator_with_transform_substructure(tester_t *tester)
237 {
238 generator_t *generator;
239 transform_attribute_t *attribute1, *attribute2;
240 transform_substructure_t *transform;
241 chunk_t data;
242 status_t status;
243 chunk_t generated_data;
244 logger_t *logger;
245
246 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"transform substr.");
247
248 /* create generator */
249 generator = generator_create();
250 tester->assert_true(tester,(generator != NULL), "generator create check");
251
252 /* create attribute 1 */
253 attribute1 = transform_attribute_create();
254 char *stringval = "abcd";
255 data.ptr = (void *) stringval;
256 data.len = 4;
257 status = attribute1->set_value(attribute1,data);
258 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
259 status = attribute1->set_attribute_type(attribute1,0);
260 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
261 logger->log(logger,CONTROL,"attribute1 created");
262
263 /* create attribute 2 */
264 attribute2 = transform_attribute_create();
265 stringval = "efgh";
266 data.ptr = (void *) stringval;
267 data.len = 4;
268 status = attribute2->set_value(attribute2,data);
269 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
270 status = attribute2->set_attribute_type(attribute2,0);
271 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
272 logger->log(logger,CONTROL,"attribute2 created");
273
274 /* create transform */
275 transform = transform_substructure_create();
276 tester->assert_true(tester,(transform != NULL), "transform create check");
277 status = transform->add_transform_attribute(transform,attribute1);
278 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
279 status = transform->add_transform_attribute(transform,attribute2);
280 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
281 status = transform->set_transform_type(transform,5); /* hex 5 */
282 tester->assert_true(tester,(status == SUCCESS),"set_transform_type call check");
283 status = transform->set_transform_id(transform,65000); /* hex FDE8 */
284 tester->assert_true(tester,(status == SUCCESS),"set_transform_id call check");
285
286
287 logger->log(logger,CONTROL,"transform created");
288
289 status = generator->generate_payload(generator,(payload_t *)transform);
290 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
291 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
292 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
293
294 u_int8_t expected_generation3[] = {
295 0x00,0x00,0x00,0x18,
296 0x05,0x00,0xFD,0xE8,
297 0x00,0x00,0x00,0x04,
298 0x61,0x62,0x63,0x64,
299 0x00,0x00,0x00,0x04,
300 0x65,0x66,0x67,0x68,
301 };
302 tester->assert_true(tester,(memcmp(expected_generation3,generated_data.ptr,sizeof(expected_generation3)) == 0), "compare generated data");
303
304 allocator_free_chunk(generated_data);
305 tester->assert_true(tester,(transform->destroy(transform) == SUCCESS), "transform destroy call check");
306 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
307
308
309 global_logger_manager->destroy_logger(global_logger_manager,logger);
310 }