generator now has its generator_context which is used to generate more
[strongswan.git] / Source / charon / tests / 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 "../allocator.h"
26 #include "generator_test.h"
27 #include "../tester.h"
28 #include "../logger.h"
29 #include "../encodings.h"
30 #include "../generator.h"
31 #include "../encodings/ike_header.h"
32
33 extern payload_info_t *payload_infos[];
34
35 extern logger_t *global_logger;
36 /*
37 * Described in Header
38 */
39 void test_generator_with_unsupported_payload(tester_t *tester)
40 {
41 generator_t *generator;
42 generator_context_t *generator_context;
43 void * data_struct;
44 chunk_t generated_data;
45
46 generator = generator_create(payload_infos);
47 tester->assert_true(tester,(generator != NULL), "generator create check");
48
49 generator_context = generator->create_context(generator);
50
51 tester->assert_true(tester,(generator->generate_payload(generator,(payload_type_t) -1,data_struct,generator_context) == NOT_SUPPORTED),"generate_payload call check");
52
53 generator_context->destroy(generator_context);
54
55 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
56 }
57
58 /*
59 * Described in Header
60 */
61 void test_generator_with_header_payload(tester_t *tester)
62 {
63 generator_t *generator;
64 generator_context_t *generator_context;
65 ike_header_t header_data;
66 chunk_t generated_data;
67 status_t status;
68
69 header_data.initiator_spi = 1;
70 header_data.responder_spi = 2;
71 header_data.next_payload = 3;
72 header_data.maj_version = 4;
73 header_data.min_version = 5;
74 header_data.exchange_type = 6;
75 header_data.flags.initiator = TRUE;
76 header_data.flags.version = FALSE;
77 header_data.flags.response = TRUE;
78 header_data.message_id = 7;
79 header_data.length = 8;
80
81 generator = generator_create(payload_infos);
82 tester->assert_true(tester,(generator != NULL), "generator create check");
83
84 generator_context = generator->create_context(generator);
85 tester->assert_true(tester,(generator_context != NULL), "generator_context create check");
86
87 status = generator->generate_payload(generator,HEADER,&header_data,generator_context);
88 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
89
90 tester->assert_true(tester,(generator->write_to_chunk(generator,generator_context,&generated_data) == SUCCESS),"write_to_chunk call check");
91
92 u_int8_t expected_generation[] = {
93 0x00,0x00,0x00,0x00,
94 0x00,0x00,0x00,0x01,
95 0x00,0x00,0x00,0x00,
96 0x00,0x00,0x00,0x02,
97 0x03,0x45,0x06,0x28,
98 0x00,0x00,0x00,0x07,
99 0x00,0x00,0x00,0x08,
100 };
101
102
103 tester->assert_true(tester,(generated_data.len == sizeof(expected_generation)), "compare generated data length");
104
105 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data 1");
106 allocator_free_chunk(generated_data);
107 generator_context->destroy(generator_context);
108
109
110 header_data.initiator_spi = 0x22000054231234;
111 header_data.responder_spi = 0x122398;
112 header_data.next_payload = 0xF3;
113 header_data.maj_version = 0x2;
114 header_data.min_version = 0x0;
115 header_data.exchange_type = 0x12;
116 header_data.flags.initiator = TRUE;
117 header_data.flags.version = TRUE;
118 header_data.flags.response = TRUE;
119 header_data.message_id = 0x33AFF3;
120 header_data.length = 0xAA11F;
121
122 generator_context = generator->create_context(generator);
123
124 status = generator->generate_payload(generator,HEADER,&header_data,generator_context);
125 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
126
127 tester->assert_true(tester,(generator->write_to_chunk(generator,generator_context,&generated_data) == SUCCESS),"write_to_chunk call check");
128
129 u_int8_t expected_generation2[] = {
130 0x00,0x22,0x00,0x00,
131 0x54,0x23,0x12,0x34,
132 0x00,0x00,0x00,0x00,
133 0x00,0x12,0x23,0x98,
134 0xF3,0x20,0x12,0x38,
135 0x00,0x33,0xAF,0xF3,
136 0x00,0x0A,0xA1,0x1F,
137 };
138
139 global_logger->log_chunk(global_logger,CONTROL,"generated header",&generated_data);
140
141 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data 2");
142 allocator_free_chunk(generated_data);
143
144 generator_context->destroy(generator_context);
145 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
146 }