038dc26d570cc60639b27b9bb375c453953f8bd3
[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
36 /*
37 * Described in Header
38 */
39 void test_generator_with_header_payload(tester_t *tester)
40 {
41 generator_t *generator;
42 ike_header_t *header_data;
43 chunk_t generated_data;
44 status_t status;
45 logger_t *logger;
46
47 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"header payload");
48
49 header_data = ike_header_create();
50
51 header_data->initiator_spi = 1;
52 header_data->responder_spi = 2;
53 header_data->next_payload = 3;
54 header_data->maj_version = 4;
55 header_data->min_version = 5;
56 header_data->exchange_type = 6;
57 header_data->flags.initiator = TRUE;
58 header_data->flags.version = FALSE;
59 header_data->flags.response = TRUE;
60 header_data->message_id = 7;
61 header_data->length = 8;
62
63 generator = generator_create();
64 tester->assert_true(tester,(generator != NULL), "generator create check");
65
66 status = generator->generate_payload(generator,(payload_t *) header_data);
67 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
68
69 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
70
71 u_int8_t expected_generation[] = {
72 0x00,0x00,0x00,0x00,
73 0x00,0x00,0x00,0x01,
74 0x00,0x00,0x00,0x00,
75 0x00,0x00,0x00,0x02,
76 0x03,0x45,0x06,0x28,
77 0x00,0x00,0x00,0x07,
78 0x00,0x00,0x00,0x08,
79 };
80
81
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 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
88
89 header_data->initiator_spi = 0x22000054231234;
90 header_data->responder_spi = 0x122398;
91 header_data->next_payload = 0xF3;
92 header_data->maj_version = 0x2;
93 header_data->min_version = 0x0;
94 header_data->exchange_type = 0x12;
95 header_data->flags.initiator = TRUE;
96 header_data->flags.version = TRUE;
97 header_data->flags.response = TRUE;
98 header_data->message_id = 0x33AFF3;
99 header_data->length = 0xAA11F;
100
101 generator = generator_create();
102 tester->assert_true(tester,(generator != NULL), "generator create check");
103
104 status = generator->generate_payload(generator,(payload_t *)header_data);
105 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
106
107 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
108
109 u_int8_t expected_generation2[] = {
110 0x00,0x22,0x00,0x00,
111 0x54,0x23,0x12,0x34,
112 0x00,0x00,0x00,0x00,
113 0x00,0x12,0x23,0x98,
114 0xF3,0x20,0x12,0x38,
115 0x00,0x33,0xAF,0xF3,
116 0x00,0x0A,0xA1,0x1F,
117 };
118
119 logger->log_chunk(logger,RAW,"generated header",&generated_data);
120
121 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data 2");
122 allocator_free_chunk(generated_data);
123
124 header_data->destroy(header_data);
125
126 global_logger_manager->destroy_logger(global_logger_manager,logger);
127 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
128 }
129
130 /*
131 * Described in header
132 */
133 void test_generator_with_transform_attribute(tester_t *tester)
134 {
135 generator_t *generator;
136 transform_attribute_t *attribute;
137 status_t status;
138 chunk_t generated_data;
139 logger_t *logger;
140
141 logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"header payload");
142
143
144 /* test empty attribute */
145 generator = generator_create();
146 tester->assert_true(tester,(generator != NULL), "generator create check");
147 attribute = transform_attribute_create();
148 status = generator->generate_payload(generator,(payload_t *)attribute);
149 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
150 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
151 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
152
153 u_int8_t expected_generation[] = {
154 0x80,0x00,0x00,0x00,
155 };
156 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
157 allocator_free_chunk(generated_data);
158 tester->assert_true(tester,(attribute->destroy(attribute) == SUCCESS), "attribute destroy call check");
159 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
160
161 /* test attribute with 2 byte data */
162 generator = generator_create();
163 tester->assert_true(tester,(generator != NULL), "generator create check");
164
165 attribute = transform_attribute_create();
166 u_int16_t dataval = 5768;
167 chunk_t data;
168 data.ptr = (void *) &dataval;
169 data.len = 2;
170
171 attribute->set_value(attribute,data);
172
173 status = generator->generate_payload(generator,(payload_t *)attribute);
174 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
175 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
176 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
177
178 u_int8_t expected_generation2[] = {
179 0x80,0x00,0x88,0x16,
180 };
181 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data");
182
183 allocator_free_chunk(generated_data);
184 tester->assert_true(tester,(attribute->destroy(attribute) == SUCCESS), "attribute destroy call check");
185 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
186
187
188
189 /* test attribute with 25 byte data */
190 generator = generator_create();
191 tester->assert_true(tester,(generator != NULL), "generator create check");
192
193 attribute = transform_attribute_create();
194 char *stringval = "ddddddddddeeeeeeeeeefffff";
195 data.ptr = (void *) stringval;
196 data.len = 25;
197
198 status = attribute->set_value(attribute,data);
199 tester->assert_true(tester,(status == SUCCESS),"set_value call check");
200
201 status = attribute->set_attribute_type(attribute,456);
202 tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
203
204
205 status = generator->generate_payload(generator,(payload_t *)attribute);
206 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
207 tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
208 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
209
210 u_int8_t expected_generation3[] = {
211 0x01,0xC8,0x00,0x19,
212 0x64,0x64,0x64,0x64,
213 0x64,0x64,0x64,0x64,
214 0x64,0x64,0x65,0x65,
215 0x65,0x65,0x65,0x65,
216 0x65,0x65,0x65,0x65,
217 0x66,0x66,0x66,0x66,
218 0x66
219 };
220 tester->assert_true(tester,(memcmp(expected_generation3,generated_data.ptr,sizeof(expected_generation3)) == 0), "compare generated data");
221
222 allocator_free_chunk(generated_data);
223 tester->assert_true(tester,(attribute->destroy(attribute) == SUCCESS), "attribute destroy call check");
224 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
225
226
227 global_logger_manager->destroy_logger(global_logger_manager,logger);
228 }