- needs update to getter and setter of ike_header
[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 #include "../payloads/proposal_substructure.h"
37
38 /*
39 * Described in Header
40 */
41 void test_generator_with_header_payload(tester_t *tester)
42 {
43 generator_t *generator;
44 ike_header_t *header_data;
45 chunk_t generated_data;
46 status_t status;
47 logger_t *logger;
48
49 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"header payload");
50
51 header_data = ike_header_create();
52 /*
53 header_data->initiator_spi = 1;
54 header_data->responder_spi = 2;
55 header_data->next_payload = 3;
56 header_data->maj_version = 4;
57 header_data->min_version = 5;
58 header_data->exchange_type = 6;
59 header_data->flags.initiator = TRUE;
60 header_data->flags.version = FALSE;
61 header_data->flags.response = TRUE;
62 header_data->message_id = 7;
63 header_data->length = 8;
64 */
65 generator = generator_create();
66 tester->assert_true(tester,(generator != NULL), "generator create check");
67
68 status = generator->generate_payload(generator,(payload_t *) header_data);
69 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
70
71 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
72
73 u_int8_t expected_generation[] = {
74 0x00,0x00,0x00,0x00,
75 0x00,0x00,0x00,0x01,
76 0x00,0x00,0x00,0x00,
77 0x00,0x00,0x00,0x02,
78 0x03,0x45,0x06,0x28,
79 0x00,0x00,0x00,0x07,
80 0x00,0x00,0x00,0x08,
81 };
82
83
84 tester->assert_true(tester,(generated_data.len == sizeof(expected_generation)), "compare generated data length");
85 logger->log_chunk(logger,RAW,"generated header",&generated_data);
86 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data 1");
87 allocator_free_chunk(generated_data);
88
89 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
90 /*
91 header_data->initiator_spi = 0x22000054231234LL;
92 header_data->responder_spi = 0x122398;
93 header_data->next_payload = 0xF3;
94 header_data->maj_version = 0x2;
95 header_data->min_version = 0x0;
96 header_data->exchange_type = 0x12;
97 header_data->flags.initiator = TRUE;
98 header_data->flags.version = TRUE;
99 header_data->flags.response = TRUE;
100 header_data->message_id = 0x33AFF3;
101 header_data->length = 0xAA11F;
102 */
103 generator = generator_create();
104 tester->assert_true(tester,(generator != NULL), "generator create check");
105
106 status = generator->generate_payload(generator,(payload_t *)header_data);
107 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
108
109 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
110
111 u_int8_t expected_generation2[] = {
112 0x00,0x22,0x00,0x00,
113 0x54,0x23,0x12,0x34,
114 0x00,0x00,0x00,0x00,
115 0x00,0x12,0x23,0x98,
116 0xF3,0x20,0x12,0x38,
117 0x00,0x33,0xAF,0xF3,
118 0x00,0x0A,0xA1,0x1F,
119 };
120
121 logger->log_chunk(logger,RAW,"generated header",&generated_data);
122
123 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data 2");
124 allocator_free_chunk(generated_data);
125
126 header_data->destroy(header_data);
127
128 global_logger_manager->destroy_logger(global_logger_manager,logger);
129 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
130 }
131
132 /*
133 * Described in header
134 */
135 void test_generator_with_transform_attribute(tester_t *tester)
136 {
137 generator_t *generator;
138 transform_attribute_t *attribute;
139 status_t status;
140 chunk_t generated_data;
141 logger_t *logger;
142
143 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"transform_attribute payload");
144
145
146 /* test empty attribute */
147 generator = generator_create();
148 tester->assert_true(tester,(generator != NULL), "generator create check");
149 attribute = transform_attribute_create();
150 status = generator->generate_payload(generator,(payload_t *)attribute);
151 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
152 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
153 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
154
155 u_int8_t expected_generation[] = {
156 0x80,0x00,0x00,0x00,
157 };
158 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
159 allocator_free_chunk(generated_data);
160 tester->assert_true(tester,(attribute->destroy(attribute) == SUCCESS), "attribute destroy call check");
161 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
162
163 /* test attribute with 2 byte data */
164 generator = generator_create();
165 tester->assert_true(tester,(generator != NULL), "generator create check");
166
167 attribute = transform_attribute_create();
168 u_int16_t dataval = 5768;
169 chunk_t data;
170 data.ptr = (void *) &dataval;
171 data.len = 2;
172
173 attribute->set_value(attribute,data);
174
175 status = generator->generate_payload(generator,(payload_t *)attribute);
176 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
177 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
178 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
179
180 u_int8_t expected_generation2[] = {
181 0x80,0x00,0x88,0x16,
182 };
183 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data");
184
185 allocator_free_chunk(generated_data);
186 tester->assert_true(tester,(attribute->destroy(attribute) == SUCCESS), "attribute destroy call check");
187 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
188
189
190
191 /* test attribute with 25 byte data */
192 generator = generator_create();
193 tester->assert_true(tester,(generator != NULL), "generator create check");
194
195 attribute = transform_attribute_create();
196 char *stringval = "ddddddddddeeeeeeeeeefffff";
197 data.ptr = (void *) stringval;
198 data.len = 25;
199
200 status = attribute->set_value(attribute,data);
201 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
202
203 status = attribute->set_attribute_type(attribute,456);
204 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
205
206
207 status = generator->generate_payload(generator,(payload_t *)attribute);
208 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
209 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
210 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
211
212 u_int8_t expected_generation3[] = {
213 0x01,0xC8,0x00,0x19,
214 0x64,0x64,0x64,0x64,
215 0x64,0x64,0x64,0x64,
216 0x64,0x64,0x65,0x65,
217 0x65,0x65,0x65,0x65,
218 0x65,0x65,0x65,0x65,
219 0x66,0x66,0x66,0x66,
220 0x66
221 };
222 tester->assert_true(tester,(memcmp(expected_generation3,generated_data.ptr,sizeof(expected_generation3)) == 0), "compare generated data");
223
224 allocator_free_chunk(generated_data);
225 tester->assert_true(tester,(attribute->destroy(attribute) == SUCCESS), "attribute destroy call check");
226 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
227
228
229 global_logger_manager->destroy_logger(global_logger_manager,logger);
230 }
231
232
233
234 /*
235 * Described in header
236 */
237 void test_generator_with_transform_substructure(tester_t *tester)
238 {
239 generator_t *generator;
240 transform_attribute_t *attribute1, *attribute2;
241 transform_substructure_t *transform;
242 chunk_t data;
243 status_t status;
244 chunk_t generated_data;
245 logger_t *logger;
246
247 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"transform substr.");
248
249 /* create generator */
250 generator = generator_create();
251 tester->assert_true(tester,(generator != NULL), "generator create check");
252
253 /* create attribute 1 */
254 attribute1 = transform_attribute_create();
255 char *stringval = "abcd";
256 data.ptr = (void *) stringval;
257 data.len = 4;
258 status = attribute1->set_value(attribute1,data);
259 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
260 status = attribute1->set_attribute_type(attribute1,0);
261 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
262 logger->log(logger,CONTROL,"attribute1 created");
263
264 /* create attribute 2 */
265 attribute2 = transform_attribute_create();
266 stringval = "efgh";
267 data.ptr = (void *) stringval;
268 data.len = 4;
269 status = attribute2->set_value(attribute2,data);
270 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
271 status = attribute2->set_attribute_type(attribute2,0);
272 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
273 logger->log(logger,CONTROL,"attribute2 created");
274
275 /* create transform */
276 transform = transform_substructure_create();
277 tester->assert_true(tester,(transform != NULL), "transform create check");
278 status = transform->add_transform_attribute(transform,attribute1);
279 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
280 status = transform->add_transform_attribute(transform,attribute2);
281 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
282 status = transform->set_transform_type(transform,5); /* hex 5 */
283 tester->assert_true(tester,(status == SUCCESS),"set_transform_type call check");
284 status = transform->set_transform_id(transform,65000); /* hex FDE8 */
285 tester->assert_true(tester,(status == SUCCESS),"set_transform_id call check");
286
287
288 logger->log(logger,CONTROL,"transform created");
289
290 status = generator->generate_payload(generator,(payload_t *)transform);
291 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
292 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
293 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
294
295 u_int8_t expected_generation3[] = {
296 0x00,0x00,0x00,0x18,
297 0x05,0x00,0xFD,0xE8,
298 0x00,0x00,0x00,0x04,
299 0x61,0x62,0x63,0x64,
300 0x00,0x00,0x00,0x04,
301 0x65,0x66,0x67,0x68,
302 };
303 tester->assert_true(tester,(memcmp(expected_generation3,generated_data.ptr,sizeof(expected_generation3)) == 0), "compare generated data");
304
305 allocator_free_chunk(generated_data);
306 tester->assert_true(tester,(transform->destroy(transform) == SUCCESS), "transform destroy call check");
307 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
308
309
310 global_logger_manager->destroy_logger(global_logger_manager,logger);
311 }
312
313
314 /*
315 * Described in header
316 */
317 void test_generator_with_proposal_substructure(tester_t *tester)
318 {
319 generator_t *generator;
320 transform_attribute_t *attribute1, *attribute2, *attribute3;
321 transform_substructure_t *transform1, *transform2;
322 proposal_substructure_t *proposal;
323 chunk_t data;
324 status_t status;
325 chunk_t generated_data;
326 logger_t *logger;
327
328 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"transform substr.");
329
330 /* create generator */
331 generator = generator_create();
332 tester->assert_true(tester,(generator != NULL), "generator create check");
333
334 /* create attribute 1 */
335 attribute1 = transform_attribute_create();
336 char *stringval = "abcd";
337 data.ptr = (void *) stringval;
338 data.len = 4;
339 status = attribute1->set_value(attribute1,data);
340 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
341 status = attribute1->set_attribute_type(attribute1,0);
342 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
343 logger->log(logger,CONTROL,"attribute1 created");
344
345 /* create attribute 2 */
346 attribute2 = transform_attribute_create();
347 stringval = "efgh";
348 data.ptr = (void *) stringval;
349 data.len = 4;
350 status = attribute2->set_value(attribute2,data);
351 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
352 status = attribute2->set_attribute_type(attribute2,0);
353 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
354 logger->log(logger,CONTROL,"attribute2 created");
355
356 /* create attribute 3 */
357 attribute3 = transform_attribute_create();
358 stringval = "ijkl";
359 data.ptr = (void *) stringval;
360 data.len = 4;
361 status = attribute3->set_value(attribute3,data);
362 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
363 status = attribute3->set_attribute_type(attribute3,0);
364 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
365 logger->log(logger,CONTROL,"attribute3 created");
366
367 /* create transform 1*/
368 transform1 = transform_substructure_create();
369 tester->assert_true(tester,(transform1 != NULL), "transform create check");
370 status = transform1->add_transform_attribute(transform1,attribute1);
371 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
372 status = transform1->add_transform_attribute(transform1,attribute2);
373 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
374 status = transform1->set_transform_type(transform1,5); /* hex 5 */
375 tester->assert_true(tester,(status == SUCCESS),"set_transform_type call check");
376 status = transform1->set_transform_id(transform1,65000); /* hex FDE8 */
377 tester->assert_true(tester,(status == SUCCESS),"set_transform_id call check");
378
379 /* create transform 2*/
380 transform2 = transform_substructure_create();
381 tester->assert_true(tester,(transform2 != NULL), "transform create check");
382 status = transform2->add_transform_attribute(transform2,attribute3);
383 tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
384 status = transform2->set_transform_type(transform2,3); /* hex 3 */
385 tester->assert_true(tester,(status == SUCCESS),"set_transform_type call check");
386 status = transform2->set_transform_id(transform2,4); /* hex 4 */
387 tester->assert_true(tester,(status == SUCCESS),"set_transform_id call check");
388
389 logger->log(logger,CONTROL,"transforms created");
390
391 proposal = proposal_substructure_create();
392 tester->assert_true(tester,(proposal != NULL), "proposal create check");
393
394 stringval = "ABCDEFGH";
395 data.ptr = (void *) stringval;
396 data.len = 8;
397
398 status = proposal->add_transform_substructure(proposal,transform1);
399 tester->assert_true(tester,(status == SUCCESS),"add_transform_substructure call check");
400 status = proposal->add_transform_substructure(proposal,transform2);
401 tester->assert_true(tester,(status == SUCCESS),"add_transform_substructure call check");
402 status = proposal->set_spi(proposal,data);
403 tester->assert_true(tester,(status == SUCCESS),"set_spi call check");
404 status = proposal->set_proposal_number(proposal,7);
405 tester->assert_true(tester,(status == SUCCESS),"set_proposal_number call check");
406 status = proposal->set_protocol_id(proposal,4);
407 tester->assert_true(tester,(status == SUCCESS),"set_protocol_id call check");
408
409
410 status = generator->generate_payload(generator,(payload_t *)proposal);
411 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
412 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
413 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
414
415 u_int8_t expected_generation[] = {
416 /* proposal header */
417 0x00,0x00,0x00,0x38,
418 0x07,0x04,0x08,0x02,
419 /* SPI */
420 0x41,0x42,0x43,0x44,
421 0x45,0x46,0x47,0x48,
422 /* first transform */
423 0x03,0x00,0x00,0x18,
424 0x05,0x00,0xFD,0xE8,
425 /* first transform attributes */
426 0x00,0x00,0x00,0x04,
427 0x61,0x62,0x63,0x64,
428 0x00,0x00,0x00,0x04,
429 0x65,0x66,0x67,0x68,
430 /* second transform */
431 0x00,0x00,0x00,0x10,
432 0x03,0x00,0x00,0x04,
433 /* second transform attributes */
434 0x00,0x00,0x00,0x04,
435 0x69,0x6A,0x6B,0x6C
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