- moved ike_header fields to private
[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
34
35 extern logger_manager_t *global_logger_manager;
36
37
38 /*
39 * Described in Header
40 */
41 void test_parser_with_header_payload(tester_t *tester)
42 {
43 parser_t *parser;
44 ike_header_t *ike_header;
45 status_t status;
46 chunk_t header_chunk;
47
48 u_int8_t header_bytes[] = {
49 0x00,0x00,0x00,0x00,
50 0x00,0x00,0x00,0x01,
51 0x00,0x00,0x00,0x00,
52 0x00,0x00,0x00,0x02,
53 0x03,0x45,0x06,0x28,
54 0x00,0x00,0x00,0x07,
55 0x00,0x00,0x00,0x1C,
56 };
57 header_chunk.ptr = header_bytes;
58 header_chunk.len = sizeof(header_bytes);
59
60
61 parser = parser_create(header_chunk);
62 tester->assert_true(tester,(parser != NULL), "parser create check");
63 status = parser->parse_payload(parser, HEADER, (payload_t**)&ike_header);
64 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
65 tester->assert_true(tester,(parser->destroy(parser) == SUCCESS), "parser destroy call check");
66
67 if (status != SUCCESS)
68 {
69 return;
70 }
71
72 tester->assert_true(tester,(ike_header->get_initiator_spi(ike_header) == 1),"parsed initiator_spi value");
73 tester->assert_true(tester,(ike_header->get_responder_spi(ike_header) == 2),"parsed responder_spi value");
74 tester->assert_true(tester,(ike_header->payload_interface.get_next_type((payload_t*)ike_header) == 3),"parsed next_payload value");
75 tester->assert_true(tester,(ike_header->get_maj_version(ike_header) == 4),"parsed maj_version value");
76 tester->assert_true(tester,(ike_header->get_min_version(ike_header) == 5),"parsed min_version value");
77 tester->assert_true(tester,(ike_header->get_exchange_type(ike_header) == 6),"parsed exchange_type value");
78 tester->assert_true(tester,(ike_header->get_initiator_flag(ike_header) == TRUE),"parsed flags.initiator value");
79 tester->assert_true(tester,(ike_header->get_version_flag(ike_header) == FALSE),"parsed flags.version value");
80 tester->assert_true(tester,(ike_header->get_response_flag(ike_header) == TRUE),"parsed flags.response value");
81 tester->assert_true(tester,(ike_header->get_message_id(ike_header) == 7),"parsed message_id value");
82 tester->assert_true(tester,(ike_header->payload_interface.get_length((payload_t*)ike_header) == 0x1C),"parsed length value");
83
84 ike_header->destroy(ike_header);
85 }
86
87 /*
88 * Described in Header
89 */
90 void test_parser_with_sa_payload(tester_t *tester)
91 {
92 parser_t *parser;
93 sa_payload_t *sa_payload;
94 status_t status;
95 chunk_t sa_chunk;
96
97 u_int8_t sa_bytes[] = {
98 0x00,0x80,0x00,0x24, /* payload header*/
99 0x00,0x00,0x00,0x20, /* a proposal */
100 0x01,0x02,0x04,0x05,
101 0x01,0x02,0x03,0x04, /* spi */
102 0x00,0x00,0x00,0x14, /* transform */
103 0x02,0x00,0x00,0x03,
104 0x80,0x01,0x00,0x05, /* attribute without length */
105 0x00,0x01,0x00,0x04, /* attribute with lenngth */
106 0x01,0x02,0x03,0x04
107
108
109 };
110
111 sa_chunk.ptr = sa_bytes;
112 sa_chunk.len = sizeof(sa_bytes);
113
114
115 parser = parser_create(sa_chunk);
116 tester->assert_true(tester,(parser != NULL), "parser create check");
117 status = parser->parse_payload(parser, SECURITY_ASSOCIATION, (payload_t**)&sa_payload);
118 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
119 tester->assert_true(tester,(parser->destroy(parser) == SUCCESS), "parser destroy call check");
120
121 if (status != SUCCESS)
122 {
123 return;
124 }
125
126
127 sa_payload->destroy(sa_payload);
128 }