moved IPv6Extension option-list to defaultbundle
[senf.git] / senf / Packets / DefaultBundle / IPv6Extensions.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //     Philipp Batroff <philipp.batroff@fokus.fraunhofer.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief IPv6Extensions unit tests */
25
26 //#include "IPv6Extensions.test.hh"
27 //#include "IPv6Extensions.test.ih"
28
29 // Custom includes
30 #include "IPv6Extensions.hh"
31 #include "IPv6Packet.hh"
32 #include "UDPPacket.hh"
33 #include "ICMPv6Packet.hh"
34
35 #include <senf/Utils/auto_unit_test.hh>
36 #include <boost/test/test_tools.hpp>
37
38 #define prefix_
39 ///////////////////////////////cc.p////////////////////////////////////////
40
41 BOOST_AUTO_UNIT_TEST(ipv6Extensions_fragment)
42 {
43     // Just for the fun of it, we test a nice chain: A fragment of a fragmented UDP packet
44
45     unsigned char Fragment_packetData[] = {
46         // IP header
47         0x60, 0x00, 0x00, 0x00,         // IP Version, class, flow label
48         0, 20,                          // payload length
49         44,                             // next header (IPv6 Fragment)
50         32,                             // hop limit
51         0x20, 0x01, 0, 0, 0, 0, 0, 0,   // source ip = 2001::1
52            0, 0, 0, 0, 0, 0, 0, 0x01,
53         0x20, 0x01, 0, 0, 0, 0, 0, 0,   // destination ip = 2001::2
54            0, 0, 0, 0, 0, 0, 0, 0x02,
55         // IPv6 Fragment header
56         17,                             // next header (UDP)
57         0,                              // reserved
58         0x05, 0x00,                     // fragment offset, last fragment
59         0x01, 0x02, 0x03, 0x04,         // id
60         // UDP header
61         0x10, 0x00,                     // source port
62         0x20, 0x00,                     // destination port
63         0, 12,                          // length
64         0x00, 0x00,                     // CRC (no, I won't calculate this one ...)
65         // Payload data
66         0x11, 0x12, 0x13, 0x14
67     };
68
69     senf::IPv6Packet pFragment_packet (senf::IPv6Packet::create(Fragment_packetData));
70
71     BOOST_CHECK_EQUAL( pFragment_packet->version(), 6u );
72     BOOST_CHECK_EQUAL( pFragment_packet->length(), 20u );
73     BOOST_CHECK_EQUAL( pFragment_packet->nextHeader(), 44u );
74     BOOST_CHECK_EQUAL( pFragment_packet->source().value(), 
75             senf::INet6Address::from_string("2001::1") );
76     BOOST_CHECK_EQUAL( pFragment_packet->destination().value(), 
77             senf::INet6Address::from_string("2001::2") );
78
79     std::ostringstream oss (std::ostringstream::out);
80     SENF_CHECK_NO_THROW( pFragment_packet.dump( oss));
81
82     BOOST_CHECK( pFragment_packet.next().is<senf::IPv6Extension_Fragment>() );
83
84     senf::IPv6Extension_Fragment fFragment_packet (pFragment_packet.next().as<senf::IPv6Extension_Fragment>());
85
86     BOOST_CHECK_EQUAL( fFragment_packet->nextHeader(), 17u );
87     BOOST_CHECK_EQUAL( fFragment_packet->fragmentOffset(), 160u );
88     BOOST_CHECK_EQUAL( fFragment_packet->id(), 0x01020304u );
89     BOOST_CHECK( fFragment_packet.next().is<senf::UDPPacket>() );
90
91     senf::UDPPacket uFragment_packet (fFragment_packet.next().as<senf::UDPPacket>());
92
93     BOOST_CHECK_EQUAL( uFragment_packet->source(), 0x1000u );
94     BOOST_CHECK_EQUAL( uFragment_packet->destination(), 0x2000u );
95     BOOST_CHECK_EQUAL( uFragment_packet->length(), 12u );
96     BOOST_CHECK( uFragment_packet.next().is<senf::DataPacket>() );
97
98     senf::DataPacket dFragment_packet (uFragment_packet.next().as<senf::DataPacket>());
99     senf::PacketData::iterator i (uFragment_packet.next().data().begin());
100     BOOST_CHECK_EQUAL( dFragment_packet.size(), 4u );
101     BOOST_CHECK_EQUAL( dFragment_packet.data()[0], 0x11 );
102 }
103
104
105 BOOST_AUTO_UNIT_TEST(ipv6Extensions_routing)
106 {
107     unsigned char Routing_packetData[] = {
108         // IP header
109         0x60, 0x30, 0x00, 0x00, 
110         0x00, 0x10,             // payload Length
111         0x2b,                   // next Header   (43 = Routing Header)
112         0xff,                   // Hop Limit
113         0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
114         0x02, 0x01, 0x02, 0xff, 0xfe, 0x00, 0x02, 0x00, // Src Addr
115         0x35, 0x55, 0x55, 0x55, 0x66, 0x66, 0x66, 0x66,
116         0x77, 0x77, 0x77, 0x77, 0x88, 0x88, 0x88, 0x88, // Dest. Addr
117         // Routing Header
118         0x3a,                   // nextHeader (58)
119         0x00,                   // length ( 0 )
120         0x00,                   // routing type ( 0 )
121         0x00,                   // Left Segments ( 0 )
122         0x00, 0x00, 0x00, 0x00, // reserved
123         // ICMPv6
124         0x01,                   // type: 1 (unreachable )
125         0x00,                   // code: 0 (route unreachable)
126         0xa3, 0xd3,             // checksum (incorrect in wireshark capture file, should be 0xa3c4)
127         0x00, 0x00, 0x00, 0x00
128     };
129     
130     senf::IPv6Packet pRouting_packet (senf::IPv6Packet::create(Routing_packetData));
131
132     BOOST_CHECK_EQUAL( pRouting_packet->version(), 6u );
133     BOOST_CHECK_EQUAL( pRouting_packet->length(), 16u );
134     BOOST_CHECK_EQUAL( pRouting_packet->nextHeader(), 43u );
135     BOOST_CHECK_EQUAL( pRouting_packet->source().value(), 
136             senf::INet6Address::from_string("fe80::201:2ff:fe00:200") );
137     BOOST_CHECK_EQUAL( pRouting_packet->destination().value(), 
138             senf::INet6Address::from_string("3555:5555:6666:6666:7777:7777:8888:8888"));
139     
140     std::ostringstream oss (std::ostringstream::out);
141     SENF_CHECK_NO_THROW( pRouting_packet.dump( oss));
142
143     BOOST_REQUIRE( pRouting_packet.next().is<senf::IPv6Extension_Routing>() );
144
145     senf::IPv6Extension_Routing pRouting_extension (pRouting_packet.next().as<senf::IPv6Extension_Routing>());
146
147     BOOST_CHECK_EQUAL( pRouting_extension->nextHeader(), 58u );
148     BOOST_CHECK_EQUAL( pRouting_extension->headerLength(), 0x00 );
149     BOOST_CHECK_EQUAL( pRouting_extension->routingType(), 0x00 );
150     BOOST_CHECK_EQUAL( pRouting_extension->segmentsLeft(), 0x00);
151     
152     BOOST_CHECK_EQUAL( pRouting_extension->reserved(), 0u);
153
154     BOOST_REQUIRE( pRouting_extension.next().is<senf::ICMPv6Packet>() );
155     senf::ICMPv6Packet pICMPv6 (pRouting_extension.next().as<senf::ICMPv6Packet>());
156     BOOST_CHECK_EQUAL( pICMPv6->type(), 1u);
157     BOOST_CHECK_EQUAL( pICMPv6->code(), 0u);
158     BOOST_CHECK_EQUAL( pICMPv6->checksum(), 0xa3d3);
159 }
160
161
162 BOOST_AUTO_UNIT_TEST(ipv6Extensions_hopByHop_parse)
163 {
164     unsigned char HopByHop_packetData[] = {
165         0x60, 0x00, 0x00, 0x00, // IP version, class, flow label
166         0x00, 0x24,             // payload length
167         0x00,                   // next header: IPv6 hop-by-hop option (0)
168         0x01,                   // hop limit (1)
169         0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
170         0x02, 0x19, 0xb9, 0xff, 0xfe, 0xeb, 0xb2, 0x26, // IPv6 Source address
171         0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     
172         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, // IPv6 Destination address ff02::16
173         // HopByHop option
174         0x3a, // next Header (ICMPv6)
175         0x00, // Length (0 = 8Bytes)
176         // option Header
177         0x05, // option type
178         0x02, // option Length (= 2 byte)
179         0x00, 0x00, //data (zero data here ...)
180         0x02, //option type (2, set for testing purposes only)
181         0x00,  //option Type length (=0, no data field needed here)
182         // ICMPv6
183         0x8f, // type 143
184         0x00, // code 0, should always be 0
185         0x50, 0xcc, // checksum
186     };
187
188     senf::IPv6Packet pHop_packet (senf::IPv6Packet::create(HopByHop_packetData));
189     BOOST_CHECK_EQUAL( pHop_packet->version(), 6u );
190     BOOST_CHECK_EQUAL( pHop_packet->length(), 36u );
191     BOOST_CHECK_EQUAL( pHop_packet->nextHeader(), 0u );
192     BOOST_CHECK_EQUAL( pHop_packet->source().value(), 
193             senf::INet6Address::from_string("fe80::219:b9ff:feeb:b226") );
194     BOOST_CHECK_EQUAL( pHop_packet->destination().value(), senf::INet6Address::from_string("ff02::16") );
195
196     std::ostringstream oss (std::ostringstream::out);
197     SENF_CHECK_NO_THROW( pHop_packet.dump( oss ));
198
199     BOOST_REQUIRE( pHop_packet.next().is<senf::IPv6Extension_HopByHop>() );
200     senf::IPv6Extension_HopByHop pHop_extension (pHop_packet.next().as<senf::IPv6Extension_HopByHop>());
201     BOOST_CHECK_EQUAL( pHop_extension->nextHeader(), 58u );
202     BOOST_CHECK_EQUAL( pHop_extension->headerLength(), 0x00 );
203
204     senf::IPv6Extension_HopByHop::Parser::options_t::container optC(pHop_extension->options() );
205     senf::IPv6Extension_HopByHop::Parser::options_t::container::iterator listIter (optC.begin());
206
207     BOOST_CHECK_EQUAL( listIter->altAction(), 0u);
208     BOOST_CHECK_EQUAL( listIter->changeFlag(), 0u);
209     BOOST_CHECK_EQUAL( listIter->optionType(), 5u);
210     BOOST_CHECK_EQUAL( listIter->optionLength(), 2u);
211     ++listIter;
212     BOOST_CHECK_EQUAL( listIter->altAction(), 0u);
213     BOOST_CHECK_EQUAL( listIter->changeFlag(), 0u);
214     BOOST_CHECK_EQUAL( listIter->optionType(), 2u);
215     BOOST_CHECK_EQUAL( listIter->optionLength(), 0);
216
217     BOOST_REQUIRE( pHop_extension.next().is<senf::ICMPv6Packet>() );
218     senf::ICMPv6Packet pICMPv6  (pHop_extension.next().as<senf::ICMPv6Packet>());
219     BOOST_CHECK_EQUAL( pICMPv6->type(), 0x8f);
220     BOOST_CHECK_EQUAL( pICMPv6->code(), 0u);
221     BOOST_CHECK_EQUAL( pICMPv6->checksum(), 0x50cc);
222 }
223
224
225 ///////////////////////////////cc.e////////////////////////////////////////
226 #undef prefix_
227
228
229 // Local Variables:
230 // mode: c++
231 // fill-column: 100
232 // c-file-style: "senf"
233 // indent-tabs-mode: nil
234 // ispell-local-dictionary: "american"
235 // compile-command: "scons -u test"
236 // comment-column: 40
237 // End: