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.
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.
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.
24 \brief IPv6Extensions unit tests */
26 //#include "IPv6Extensions.test.hh"
27 //#include "IPv6Extensions.test.ih"
30 #include "IPv6Extensions.hh"
31 #include "IPv6Packet.hh"
32 #include "UDPPacket.hh"
33 #include "ICMPv6Packet.hh"
34 #include <senf/Utils/hexdump.hh>
36 #include <senf/Utils/auto_unit_test.hh>
37 #include <boost/test/test_tools.hpp>
40 ///////////////////////////////cc.p////////////////////////////////////////
42 BOOST_AUTO_UNIT_TEST(ipv6Extensions_fragment)
44 // Just for the fun of it, we test a nice chain: A fragment of a fragmented UDP packet
46 unsigned char Fragment_packetData[] = {
48 0x60, 0x00, 0x00, 0x00, // IP Version, class, flow label
49 0, 20, // payload length
50 44, // next header (IPv6 Fragment)
52 0x20, 0x01, 0, 0, 0, 0, 0, 0, // source ip = 2001::1
53 0, 0, 0, 0, 0, 0, 0, 0x01,
54 0x20, 0x01, 0, 0, 0, 0, 0, 0, // destination ip = 2001::2
55 0, 0, 0, 0, 0, 0, 0, 0x02,
56 // IPv6 Fragment header
57 17, // next header (UDP)
59 0x05, 0x00, // fragment offset, last fragment
60 0x01, 0x02, 0x03, 0x04, // id
62 0x10, 0x00, // source port
63 0x20, 0x00, // destination port
65 0x00, 0x00, // CRC (no, I won't calculate this one ...)
67 0x11, 0x12, 0x13, 0x14
70 senf::IPv6Packet pFragment_packet (senf::IPv6Packet::create(Fragment_packetData));
72 BOOST_CHECK_EQUAL( pFragment_packet->version(), 6u );
73 BOOST_CHECK_EQUAL( pFragment_packet->length(), 20u );
74 BOOST_CHECK_EQUAL( pFragment_packet->nextHeader(), 44u );
75 BOOST_CHECK_EQUAL( pFragment_packet->source().value(),
76 senf::INet6Address::from_string("2001::1") );
77 BOOST_CHECK_EQUAL( pFragment_packet->destination().value(),
78 senf::INet6Address::from_string("2001::2") );
80 std::ostringstream oss (std::ostringstream::out);
81 SENF_CHECK_NO_THROW( pFragment_packet.dump( oss));
83 BOOST_CHECK( pFragment_packet.next().is<senf::IPv6Fragment>() );
85 senf::IPv6Fragment fFragment_packet (pFragment_packet.next().as<senf::IPv6Fragment>());
87 BOOST_CHECK_EQUAL( fFragment_packet->nextHeader(), 17u );
88 BOOST_CHECK_EQUAL( fFragment_packet->fragmentOffset(), 160u );
89 BOOST_CHECK_EQUAL( fFragment_packet->id(), 0x01020304u );
90 BOOST_CHECK( fFragment_packet.next().is<senf::UDPPacket>() );
92 senf::UDPPacket uFragment_packet (fFragment_packet.next().as<senf::UDPPacket>());
94 BOOST_CHECK_EQUAL( uFragment_packet->source(), 0x1000u );
95 BOOST_CHECK_EQUAL( uFragment_packet->destination(), 0x2000u );
96 BOOST_CHECK_EQUAL( uFragment_packet->length(), 12u );
97 BOOST_CHECK( uFragment_packet.next().is<senf::DataPacket>() );
99 senf::DataPacket dFragment_packet (uFragment_packet.next().as<senf::DataPacket>());
100 senf::PacketData::iterator i (uFragment_packet.next().data().begin());
101 BOOST_CHECK_EQUAL( dFragment_packet.size(), 4u );
102 BOOST_CHECK_EQUAL( dFragment_packet.data()[0], 0x11 );
106 BOOST_AUTO_UNIT_TEST(ipv6Extensions_routing)
108 unsigned char Routing_packetData[] = {
110 0x60, 0x30, 0x00, 0x00,
111 0x00, 0x10, // payload Length
112 0x2b, // next Header (43 = Routing Header)
114 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
115 0x02, 0x01, 0x02, 0xff, 0xfe, 0x00, 0x02, 0x00, // Src Addr
116 0x35, 0x55, 0x55, 0x55, 0x66, 0x66, 0x66, 0x66,
117 0x77, 0x77, 0x77, 0x77, 0x88, 0x88, 0x88, 0x88, // Dest. Addr
119 0x3a, // nextHeader (58)
120 0x00, // length ( 0 )
121 0x00, // routing type ( 0 )
122 0x00, // Left Segments ( 0 )
123 0x00, 0x00, 0x00, 0x00, // reserved
125 0x01, // type: 1 (unreachable )
126 0x00, // code: 0 (route unreachable)
127 0xa3, 0xd3, // checksum (incorrect in wireshark capture file, should be 0xa3c4)
128 0x00, 0x00, 0x00, 0x00
131 senf::IPv6Packet pRouting_packet (senf::IPv6Packet::create(Routing_packetData));
133 BOOST_CHECK_EQUAL( pRouting_packet->version(), 6u );
134 BOOST_CHECK_EQUAL( pRouting_packet->length(), 16u );
135 BOOST_CHECK_EQUAL( pRouting_packet->nextHeader(), 43u );
136 BOOST_CHECK_EQUAL( pRouting_packet->source().value(),
137 senf::INet6Address::from_string("fe80::201:2ff:fe00:200") );
138 BOOST_CHECK_EQUAL( pRouting_packet->destination().value(),
139 senf::INet6Address::from_string("3555:5555:6666:6666:7777:7777:8888:8888"));
141 std::ostringstream oss (std::ostringstream::out);
142 SENF_CHECK_NO_THROW( pRouting_packet.dump( oss));
144 BOOST_REQUIRE( pRouting_packet.next().is<senf::IPv6Routing>() );
146 senf::IPv6Routing pRouting_extension (pRouting_packet.next().as<senf::IPv6Routing>());
148 BOOST_CHECK_EQUAL( pRouting_extension->nextHeader(), 58u );
149 BOOST_CHECK_EQUAL( pRouting_extension->headerLength(), 0x00 );
150 BOOST_CHECK_EQUAL( pRouting_extension->routingType(), 0x00 );
151 BOOST_CHECK_EQUAL( pRouting_extension->segmentsLeft(), 0x00);
153 BOOST_CHECK_EQUAL( pRouting_extension->reserved(), 0u);
155 BOOST_REQUIRE( pRouting_extension.next().is<senf::ICMPv6Packet>() );
156 senf::ICMPv6Packet pICMPv6 (pRouting_extension.next().as<senf::ICMPv6Packet>());
157 BOOST_CHECK_EQUAL( pICMPv6->type(), 1u);
158 BOOST_CHECK_EQUAL( pICMPv6->code(), 0u);
159 BOOST_CHECK_EQUAL( pICMPv6->checksum(), 0xa3d3);
163 BOOST_AUTO_UNIT_TEST(ipv6Extensions_hopByHop_parse)
165 unsigned char HopByHop_packetData[] = {
166 0x60, 0x00, 0x00, 0x00, // IP version, class, flow label
167 0x00, 0x24, // payload length
168 0x00, // next header: IPv6 hop-by-hop option (0)
169 0x01, // hop limit (1)
170 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
171 0x02, 0x19, 0xb9, 0xff, 0xfe, 0xeb, 0xb2, 0x26, // IPv6 Source address
172 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
173 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, // IPv6 Destination address ff02::16
175 0x3a, // next Header (ICMPv6)
176 0x00, // Length (0 = 8Bytes)
179 0x02, // option Length (= 2 byte)
180 0x00, 0x00, //data (zero data here ...)
181 0x02, //option type (2, set for testing purposes only)
182 0x00, //option Type length (=0, no data field needed here)
185 0x00, // code 0, should always be 0
186 0x50, 0xcc, // checksum
189 senf::IPv6Packet pHop_packet (senf::IPv6Packet::create(HopByHop_packetData));
190 BOOST_CHECK_EQUAL( pHop_packet->version(), 6u );
191 BOOST_CHECK_EQUAL( pHop_packet->length(), 36u );
192 BOOST_CHECK_EQUAL( pHop_packet->nextHeader(), 0u );
193 BOOST_CHECK_EQUAL( pHop_packet->source().value(),
194 senf::INet6Address::from_string("fe80::219:b9ff:feeb:b226") );
195 BOOST_CHECK_EQUAL( pHop_packet->destination().value(), senf::INet6Address::from_string("ff02::16") );
197 std::ostringstream oss (std::ostringstream::out);
198 SENF_CHECK_NO_THROW( pHop_packet.dump( oss ));
200 BOOST_REQUIRE( pHop_packet.next().is<senf::IPv6HopByHop>() );
201 senf::IPv6HopByHop pHop_extension (pHop_packet.next().as<senf::IPv6HopByHop>());
202 BOOST_CHECK_EQUAL( pHop_extension->nextHeader(), 58u );
203 BOOST_CHECK_EQUAL( pHop_extension->headerLength(), 0x00 );
205 senf::IPv6HopByHop::Parser::options_t::container optC(pHop_extension->options() );
206 senf::IPv6HopByHop::Parser::options_t::container::iterator listIter (optC.begin());
208 BOOST_CHECK_EQUAL( listIter->altAction(), 0u);
209 BOOST_CHECK_EQUAL( listIter->changeFlag(), 0u);
210 BOOST_CHECK_EQUAL( listIter->optionType(), 5u);
211 BOOST_CHECK_EQUAL( listIter->optionLength(), 2u);
213 BOOST_CHECK_EQUAL( listIter->altAction(), 0u);
214 BOOST_CHECK_EQUAL( listIter->changeFlag(), 0u);
215 BOOST_CHECK_EQUAL( listIter->optionType(), 2u);
216 BOOST_CHECK_EQUAL( listIter->optionLength(), 0);
218 BOOST_REQUIRE( pHop_extension.next().is<senf::ICMPv6Packet>() );
219 senf::ICMPv6Packet pICMPv6 (pHop_extension.next().as<senf::ICMPv6Packet>());
220 BOOST_CHECK_EQUAL( pICMPv6->type(), 0x8f);
221 BOOST_CHECK_EQUAL( pICMPv6->code(), 0u);
222 BOOST_CHECK_EQUAL( pICMPv6->checksum(), 0x50cc);
225 BOOST_AUTO_UNIT_TEST(ipv6Extensions_hopByHop_create)
227 std::ostringstream oss (std::ostringstream::out);
228 unsigned char HopByHop_packetData[] = {
229 0x60, 0x00, 0x00, 0x00, //IP version, class, flow label
230 0x00, 0x0c, //payload length
231 0x00, //next header: IPv6 hop-by-hop option (0)
232 0x01, //hop limit (1)
233 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //IPv6 Source address (fe80::219:b9ff:feeb:b226)
234 0x02, 0x19, 0xb9, 0xff, 0xfe, 0xeb, 0xb2, 0x26,
236 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //IPv6 Destination address ff02::16
237 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16,
239 0x3a, //next Header (ICMPv6)
240 0x00, //Length (0 = 8Bytes)
244 0x02, //option Length (= 2 byte)
245 0x00, 0x00, //data (zero data here ...)
247 0x02, //option type (2, set for testing purposes only)
248 0x00, //option Type length (=0, no data field needed here)
252 0x00, //code 0, should always be 0
253 0xca, 0xdf, //checksum
255 senf::IPv6Packet ip (senf::IPv6Packet::create());
258 ip->nextHeader() = 0u;
260 ip->source() = senf::INet6Address::from_string("fe80::219:b9ff:feeb:b226");
261 ip->destination() = senf::INet6Address::from_string("ff02::16");
262 senf::IPv6HopByHop pext (senf::IPv6HopByHop::createAfter(ip) );
263 pext->nextHeader() = 58u;
264 pext->headerLength() = 0u;
266 senf::IPv6HopByHop::Parser::options_t::container optC(pext->options() );
268 optC.push_back_space();
269 senf::GenericOptTypeTLVParser opt = optC.back().init<senf::GenericOptTypeTLVParser>();
270 opt.altAction() = 0u;
271 opt.changeFlag() = 0u;
272 opt.optionType() = 5u;
273 unsigned char val[] = {0x00, 0x00};
276 optC.push_back_space();
277 opt = optC.back().init<senf::GenericOptTypeTLVParser>();
278 opt.altAction() = 0u;
279 opt.changeFlag() = 0u;
280 opt.optionType() = 2u;
282 senf::ICMPv6Packet icmp (senf::ICMPv6Packet::createAfter (pext));
286 SENF_CHECK_NO_THROW( ip.dump(oss) );
287 BOOST_CHECK_EQUAL_COLLECTIONS(
288 HopByHop_packetData, HopByHop_packetData+sizeof(HopByHop_packetData),
289 ip.data().begin(), ip.data().end() );
292 ///////////////////////////////cc.e////////////////////////////////////////
299 // c-file-style: "senf"
300 // indent-tabs-mode: nil
301 // ispell-local-dictionary: "american"
302 // compile-command: "scons -u test"
303 // comment-column: 40