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