- fixed bug in memory reallocation
[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 "../encodings.h"
29 #include "../generator.h"
30 #include "../encodings/ike_header.h"
31
32 extern payload_info_t *payload_infos[];
33
34 /*
35 * Described in Header
36 */
37 void test_generator_with_unsupported_payload(tester_t *tester)
38 {
39 generator_t *generator;
40 void * data_struct;
41 chunk_t generated_data;
42
43 generator = generator_create(payload_infos);
44 tester->assert_true(tester,(generator != NULL), "generator create check");
45 tester->assert_true(tester,(generator->generate_payload(generator,(payload_type_t) -1,data_struct,&generated_data) == NOT_SUPPORTED),"generate_payload call check");
46
47 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
48 }
49
50 /*
51 * Described in Header
52 */
53 void test_generator_with_header_payload(tester_t *tester)
54 {
55 generator_t *generator;
56 ike_header_t header_data;
57 chunk_t generated_data;
58 status_t status;
59
60 header_data.initiator_spi = 1;
61 header_data.responder_spi = 2;
62 header_data.next_payload = 3;
63 header_data.maj_version = 4;
64 header_data.min_version = 5;
65 header_data.exchange_type = 6;
66 header_data.flags.initiator = TRUE;
67 header_data.flags.version = FALSE;
68 header_data.flags.response = TRUE;
69 header_data.message_id = 7;
70 header_data.length = 8;
71
72 generator = generator_create(payload_infos);
73 tester->assert_true(tester,(generator != NULL), "generator create check");
74
75 status = generator->generate_payload(generator,HEADER,&header_data,&generated_data);
76 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
77
78 u_int8_t expected_generation[] = {
79 0x00,0x00,0x00,0x00,
80 0x00,0x00,0x00,0x01,
81 0x00,0x00,0x00,0x00,
82 0x00,0x00,0x00,0x02,
83 0x03,0x45,0x06,0x28,
84 0x00,0x00,0x00,0x07,
85 0x00,0x00,0x00,0x08,
86 };
87
88 tester->assert_true(tester,(generated_data.len == sizeof(expected_generation)), "compare generated data length");
89
90 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
91 allocator_free_chunk(generated_data);
92
93 header_data.initiator_spi = 0x22000054231234;
94 header_data.responder_spi = 0x122398;
95 header_data.next_payload = 0xF3;
96 header_data.maj_version = 0x2;
97 header_data.min_version = 0x0;
98 header_data.exchange_type = 0x12;
99 header_data.flags.initiator = FALSE;
100 header_data.flags.version = FALSE;
101 header_data.flags.response = FALSE;
102 header_data.message_id = 0x33AFF3;
103 header_data.length = 0xAA11F;
104
105 status = generator->generate_payload(generator,HEADER,&header_data,&generated_data);
106 tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
107
108 u_int8_t expected_generation2[] = {
109 0x00,0x22,0x00,0x00,
110 0x54,0x23,0x12,0x34,
111 0x00,0x00,0x00,0x00,
112 0x00,0x12,0x23,0x98,
113 0xF3,0x20,0x12,0x00,
114 0x00,0x33,0xAF,0xF3,
115 0x00,0x0A,0xA1,0x1F,
116 };
117
118 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
119 allocator_free_chunk(generated_data);
120
121 tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
122 }