- nonce payload added
[strongswan.git] / Source / charon / testcases / parser_test.c
1 /**
2 * @file parser_test.h
3 *
4 *
5 */
6
7 /*
8 * Copyright (C) 2005 Jan Hutter, Martin Willi
9 * Hochschule fuer Technik Rapperswil
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 */
21
22 #include <string.h>
23
24 #include "parser_test.h"
25
26 #include "../generator.h"
27 #include "../parser.h"
28 #include "../utils/allocator.h"
29 #include "../utils/logger_manager.h"
30 #include "../payloads/encodings.h"
31 #include "../payloads/ike_header.h"
32 #include "../payloads/sa_payload.h"
33 #include "../payloads/nonce_payload.h"
34
35
36 extern logger_manager_t *global_logger_manager;
37
38
39 /*
40 * Described in Header
41 */
42 void test_parser_with_header_payload(tester_t *tester)
43 {
44 parser_t *parser;
45 ike_header_t *ike_header;
46 status_t status;
47 chunk_t header_chunk;
48
49 u_int8_t header_bytes[] = {
50 0x00,0x00,0x00,0x00,
51 0x00,0x00,0x00,0x01,
52 0x00,0x00,0x00,0x00,
53 0x00,0x00,0x00,0x02,
54 0x03,0x45,0x06,0x28,
55 0x00,0x00,0x00,0x07,
56 0x00,0x00,0x00,0x1C,
57 };
58 header_chunk.ptr = header_bytes;
59 header_chunk.len = sizeof(header_bytes);
60
61
62 parser = parser_create(header_chunk);
63 tester->assert_true(tester,(parser != NULL), "parser create check");
64 status = parser->parse_payload(parser, HEADER, (payload_t**)&ike_header);
65 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
66 tester->assert_true(tester,(parser->destroy(parser) == SUCCESS), "parser destroy call check");
67
68 if (status != SUCCESS)
69 {
70 return;
71 }
72
73 tester->assert_true(tester,(ike_header->get_initiator_spi(ike_header) == 1),"parsed initiator_spi value");
74 tester->assert_true(tester,(ike_header->get_responder_spi(ike_header) == 2),"parsed responder_spi value");
75 tester->assert_true(tester,(ike_header->payload_interface.get_next_type((payload_t*)ike_header) == 3),"parsed next_payload value");
76 tester->assert_true(tester,(ike_header->get_maj_version(ike_header) == 4),"parsed maj_version value");
77 tester->assert_true(tester,(ike_header->get_min_version(ike_header) == 5),"parsed min_version value");
78 tester->assert_true(tester,(ike_header->get_exchange_type(ike_header) == 6),"parsed exchange_type value");
79 tester->assert_true(tester,(ike_header->get_initiator_flag(ike_header) == TRUE),"parsed flags.initiator value");
80 tester->assert_true(tester,(ike_header->get_version_flag(ike_header) == FALSE),"parsed flags.version value");
81 tester->assert_true(tester,(ike_header->get_response_flag(ike_header) == TRUE),"parsed flags.response value");
82 tester->assert_true(tester,(ike_header->get_message_id(ike_header) == 7),"parsed message_id value");
83 tester->assert_true(tester,(ike_header->payload_interface.get_length((payload_t*)ike_header) == 0x1C),"parsed length value");
84
85 ike_header->destroy(ike_header);
86 }
87
88 /*
89 * Described in Header
90 */
91 void test_parser_with_sa_payload(tester_t *tester)
92 {
93 parser_t *parser;
94 sa_payload_t *sa_payload;
95 status_t status;
96 chunk_t sa_chunk;
97 linked_list_iterator_t *proposals, *transforms, *attributes;
98
99 u_int8_t sa_bytes[] = {
100 0x00,0x80,0x00,0x24, /* payload header*/
101 0x00,0x00,0x00,0x20, /* a proposal */
102 0x01,0x02,0x04,0x05,
103 0x01,0x02,0x03,0x04, /* spi */
104 0x00,0x00,0x00,0x14, /* transform */
105 0x07,0x00,0x00,0x03,
106 0x80,0x01,0x00,0x05, /* attribute without length */
107 0x00,0x03,0x00,0x04, /* attribute with lenngth */
108 0x01,0x02,0x03,0x04
109
110
111 };
112
113 sa_chunk.ptr = sa_bytes;
114 sa_chunk.len = sizeof(sa_bytes);
115
116
117 parser = parser_create(sa_chunk);
118 tester->assert_true(tester,(parser != NULL), "parser create check");
119 status = parser->parse_payload(parser, SECURITY_ASSOCIATION, (payload_t**)&sa_payload);
120 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
121 tester->assert_true(tester,(parser->destroy(parser) == SUCCESS), "parser destroy call check");
122
123 if (status != SUCCESS)
124 {
125 return;
126 }
127
128
129 sa_payload->create_proposal_substructure_iterator(sa_payload, &proposals, TRUE);
130 while (proposals->has_next(proposals))
131 {
132 proposal_substructure_t *proposal;
133 proposals->current(proposals, (void**)&proposal);
134 chunk_t spi;
135 u_int8_t spi_should[] = {0x01, 0x02, 0x03, 0x04};
136
137 tester->assert_true(tester,(proposal->get_proposal_number(proposal) == 1),"proposal number");
138 tester->assert_true(tester,(proposal->get_protocol_id(proposal) == 2),"proposal id");
139 spi = proposal->get_spi(proposal);
140 tester->assert_false(tester,(memcmp(&spi_should, spi.ptr, spi.len)),"proposal spi");
141
142 proposal->create_transform_substructure_iterator(proposal, &transforms, TRUE);
143 while(transforms->has_next(transforms))
144 {
145 transform_substructure_t *transform;
146 int loopi;
147 transforms->current(transforms, (void**)&transform);
148 tester->assert_true(tester,(transform->get_transform_type(transform) == 7),"transform type");
149 tester->assert_true(tester,(transform->get_transform_id(transform) == 3),"transform id");
150 transform->create_transform_attribute_iterator(transform, &attributes, TRUE);
151 loopi = 0;
152 while (attributes->has_next(attributes))
153 {
154 transform_attribute_t *attribute;
155 attributes->current(attributes, (void**)&attribute);
156 if (loopi == 0)
157 {
158 u_int8_t value[] = {0x05, 0x00};
159 chunk_t attribute_value;
160 tester->assert_true(tester,(attribute->get_attribute_type(attribute) == 1),"attribute 1 type");
161 attribute_value = attribute->get_value(attribute);
162 tester->assert_false(tester,(memcmp(&value, attribute_value.ptr, attribute_value.len)),"attribute 1 value");
163 }
164 if (loopi == 1)
165 {
166 u_int8_t value[] = {0x01, 0x02, 0x03, 0x04};
167 chunk_t attribute_value;
168 tester->assert_true(tester,(attribute->get_attribute_type(attribute) == 3),"attribute 2 type");
169 attribute_value = attribute->get_value(attribute);
170 tester->assert_false(tester,(memcmp(&value, attribute_value.ptr, attribute_value.len)),"attribute 2 value");
171 }
172 loopi++;
173 }
174 attributes->destroy(attributes);
175 }
176 transforms->destroy(transforms);
177 }
178 proposals->destroy(proposals);
179
180
181
182 sa_payload->destroy(sa_payload);
183 }
184
185 /*
186 * Described in Header
187 */
188 void test_parser_with_nonce_payload(tester_t *tester)
189 {
190 parser_t *parser;
191 nonce_payload_t *nonce_payload;
192 status_t status;
193 chunk_t nonce_chunk, result;
194
195
196 u_int8_t nonce_bytes[] = {
197 0x00,0x00,0x00,0x14, /* payload header */
198 0x00,0x01,0x02,0x03, /* 16 Byte nonce */
199 0x04,0x05,0x06,0x07,
200 0x08,0x09,0x0A,0x2B,
201 0x0C,0x0D,0x0E,0x0F
202 };
203
204 nonce_chunk.ptr = nonce_bytes;
205 nonce_chunk.len = sizeof(nonce_bytes);
206
207
208 parser = parser_create(nonce_chunk);
209 tester->assert_true(tester,(parser != NULL), "parser create check");
210 status = parser->parse_payload(parser, NONCE, (payload_t**)&nonce_payload);
211 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
212 tester->assert_true(tester,(parser->destroy(parser) == SUCCESS), "parser destroy call check");
213
214 if (status != SUCCESS)
215 {
216 return;
217 }
218
219 nonce_payload->get_nonce(nonce_payload, &result);
220
221 tester->assert_true(tester,(result.len == 16), "parsed nonce lenght");
222 tester->assert_false(tester,(memcmp(nonce_bytes + 4, result.ptr, result.len)), "parsed nonce data");
223
224
225
226
227 nonce_payload->destroy(nonce_payload);
228 }