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