updated unittest
[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 #include "ICMPv6TypePacket.hh"
35 #include "EthernetPacket.hh"
36 #include <senf/Utils/hexdump.hh>
37
38 #include <senf/Utils/auto_unit_test.hh>
39 #include <boost/test/test_tools.hpp>
40
41 #define prefix_
42 ///////////////////////////////cc.p////////////////////////////////////////
43
44 BOOST_AUTO_UNIT_TEST(ipv6Extensions_fragment)
45 {
46     // Just for the fun of it, we test a nice chain: A fragment of a fragmented UDP packet
47
48     unsigned char Fragment_packetData[] = {
49         // IP header
50         0x60, 0x00, 0x00, 0x00,         // IP Version, class, flow label
51         0, 20,                          // payload length
52         44,                             // next header (IPv6 Fragment)
53         32,                             // hop limit
54         0x20, 0x01, 0, 0, 0, 0, 0, 0,   // source ip = 2001::1
55            0, 0, 0, 0, 0, 0, 0, 0x01,
56         0x20, 0x01, 0, 0, 0, 0, 0, 0,   // destination ip = 2001::2
57            0, 0, 0, 0, 0, 0, 0, 0x02,
58         // IPv6 Fragment header
59         17,                             // next header (UDP)
60         0,                              // reserved
61         0x05, 0x00,                     // fragment offset, last fragment
62         0x01, 0x02, 0x03, 0x04,         // id
63         // UDP header
64         0x10, 0x00,                     // source port
65         0x20, 0x00,                     // destination port
66         0, 12,                          // length
67         0x00, 0x00,                     // CRC (no, I won't calculate this one ...)
68         // Payload data
69         0x11, 0x12, 0x13, 0x14
70     };
71
72     senf::IPv6Packet pFragment_packet (senf::IPv6Packet::create(Fragment_packetData));
73
74     BOOST_CHECK_EQUAL( pFragment_packet->version(), 6u );
75     BOOST_CHECK_EQUAL( pFragment_packet->length(), 20u );
76     BOOST_CHECK_EQUAL( pFragment_packet->nextHeader(), 44u );
77     BOOST_CHECK_EQUAL( pFragment_packet->source().value(), 
78             senf::INet6Address::from_string("2001::1") );
79     BOOST_CHECK_EQUAL( pFragment_packet->destination().value(), 
80             senf::INet6Address::from_string("2001::2") );
81
82     std::ostringstream oss (std::ostringstream::out);
83     SENF_CHECK_NO_THROW( pFragment_packet.dump( oss));
84
85     BOOST_CHECK( pFragment_packet.next().is<senf::IPv6FragmentPacket>() );
86
87     senf::IPv6FragmentPacket fFragment_packet (pFragment_packet.next().as<senf::IPv6FragmentPacket>());
88
89     BOOST_CHECK_EQUAL( fFragment_packet->nextHeader(), 17u );
90     BOOST_CHECK_EQUAL( fFragment_packet->fragmentOffset(), 160u );
91     BOOST_CHECK_EQUAL( fFragment_packet->id(), 0x01020304u );
92     BOOST_CHECK( fFragment_packet.next().is<senf::UDPPacket>() );
93
94     senf::UDPPacket uFragment_packet (fFragment_packet.next().as<senf::UDPPacket>());
95
96     BOOST_CHECK_EQUAL( uFragment_packet->source(), 0x1000u );
97     BOOST_CHECK_EQUAL( uFragment_packet->destination(), 0x2000u );
98     BOOST_CHECK_EQUAL( uFragment_packet->length(), 12u );
99     BOOST_CHECK( uFragment_packet.next().is<senf::DataPacket>() );
100
101     senf::DataPacket dFragment_packet (uFragment_packet.next().as<senf::DataPacket>());
102     senf::PacketData::iterator i (uFragment_packet.next().data().begin());
103     BOOST_CHECK_EQUAL( dFragment_packet.size(), 4u );
104     BOOST_CHECK_EQUAL( dFragment_packet.data()[0], 0x11 );
105 }
106
107
108 BOOST_AUTO_UNIT_TEST(ipv6Extensions_routing)
109 {
110     unsigned char Routing_packetData[] = {
111         // IP header
112         0x60, 0x30, 0x00, 0x00, 
113         0x00, 0x10,             // payload Length
114         0x2b,                   // next Header   (43 = Routing Header)
115         0xff,                   // Hop Limit
116         0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
117         0x02, 0x01, 0x02, 0xff, 0xfe, 0x00, 0x02, 0x00, // Src Addr
118         0x35, 0x55, 0x55, 0x55, 0x66, 0x66, 0x66, 0x66,
119         0x77, 0x77, 0x77, 0x77, 0x88, 0x88, 0x88, 0x88, // Dest. Addr
120         // Routing Header
121         0x3a,                   // nextHeader (58)
122         0x00,                   // length ( 0 )
123         0x00,                   // routing type ( 0 )
124         0x00,                   // Left Segments ( 0 )
125         0x00, 0x00, 0x00, 0x00, // reserved
126         // ICMPv6
127         0x01,                   // type: 1 (unreachable )
128         0x00,                   // code: 0 (route unreachable)
129         0xa3, 0xd3,             // checksum (incorrect in wireshark capture file, should be 0xa3c4)
130         0x00, 0x00, 0x00, 0x00
131     };
132     
133     senf::IPv6Packet pRouting_packet (senf::IPv6Packet::create(Routing_packetData));
134
135     BOOST_CHECK_EQUAL( pRouting_packet->version(), 6u );
136     BOOST_CHECK_EQUAL( pRouting_packet->length(), 16u );
137     BOOST_CHECK_EQUAL( pRouting_packet->nextHeader(), 43u );
138     BOOST_CHECK_EQUAL( pRouting_packet->source().value(), 
139             senf::INet6Address::from_string("fe80::201:2ff:fe00:200") );
140     BOOST_CHECK_EQUAL( pRouting_packet->destination().value(), 
141             senf::INet6Address::from_string("3555:5555:6666:6666:7777:7777:8888:8888"));
142     
143     std::ostringstream oss (std::ostringstream::out);
144     SENF_CHECK_NO_THROW( pRouting_packet.dump( oss));
145
146     BOOST_REQUIRE( pRouting_packet.next().is<senf::IPv6RoutingPacket>() );
147
148     senf::IPv6RoutingPacket pRouting_extension (pRouting_packet.next().as<senf::IPv6RoutingPacket>());
149
150     BOOST_CHECK_EQUAL( pRouting_extension->nextHeader(), 58u );
151     BOOST_CHECK_EQUAL( pRouting_extension->headerLength(), 0x00 );
152     BOOST_CHECK_EQUAL( pRouting_extension->routingType(), 0x00 );
153     BOOST_CHECK_EQUAL( pRouting_extension->segmentsLeft(), 0x00);
154     
155     BOOST_CHECK_EQUAL( pRouting_extension->reserved(), 0u);
156
157     BOOST_REQUIRE( pRouting_extension.next().is<senf::ICMPv6Packet>() );
158     senf::ICMPv6Packet pICMPv6 (pRouting_extension.next().as<senf::ICMPv6Packet>());
159     BOOST_CHECK_EQUAL( pICMPv6->type(), 1u);
160     BOOST_CHECK_EQUAL( pICMPv6->code(), 0u);
161     BOOST_CHECK_EQUAL( pICMPv6->checksum(), 0xa3d3);
162 }
163
164
165 BOOST_AUTO_UNIT_TEST(ipv6Extensions_hopByHop_parse)
166 {
167     unsigned char HopByHop_packetData[] = {
168         0x60, 0x00, 0x00, 0x00, // IP version, class, flow label
169         0x00, 0x24,             // payload length
170         0x00,                   // next header: IPv6 hop-by-hop option (0)
171         0x01,                   // hop limit (1)
172         0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
173         0x02, 0x19, 0xb9, 0xff, 0xfe, 0xeb, 0xb2, 0x26, // IPv6 Source address
174         0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     
175         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, // IPv6 Destination address ff02::16
176         // HopByHop option
177         0x3a, // next Header (ICMPv6)
178         0x00, // Length (0 = 8Bytes)
179         // option Header
180         0x05, // option type
181         0x02, // option Length (= 2 byte)
182         0x00, 0x00, //data (zero data here ...)
183         0x02, //option type (2, set for testing purposes only)
184         0x00,  //option Type length (=0, no data field needed here)
185         // ICMPv6
186         0x8f, // type 143
187         0x00, // code 0, should always be 0
188         0x50, 0xcc, // checksum
189     };
190
191     senf::IPv6Packet pHop_packet (senf::IPv6Packet::create(HopByHop_packetData));
192     BOOST_CHECK_EQUAL( pHop_packet->version(), 6u );
193     BOOST_CHECK_EQUAL( pHop_packet->length(), 36u );
194     BOOST_CHECK_EQUAL( pHop_packet->nextHeader(), 0u );
195     BOOST_CHECK_EQUAL( pHop_packet->source().value(), 
196             senf::INet6Address::from_string("fe80::219:b9ff:feeb:b226") );
197     BOOST_CHECK_EQUAL( pHop_packet->destination().value(), senf::INet6Address::from_string("ff02::16") );
198
199     std::ostringstream oss (std::ostringstream::out);
200     SENF_CHECK_NO_THROW( pHop_packet.dump( oss ));
201
202     BOOST_REQUIRE( pHop_packet.next().is<senf::IPv6HopByHopOptionsPacket>() );
203     senf::IPv6HopByHopOptionsPacket pHop_extension (pHop_packet.next().as<senf::IPv6HopByHopOptionsPacket>());
204     BOOST_CHECK_EQUAL( pHop_extension->nextHeader(), 58u );
205     BOOST_CHECK_EQUAL( pHop_extension->headerLength(), 0x00 );
206
207     senf::IPv6HopByHopOptionsPacket::Parser::options_t::container optC(pHop_extension->options() );
208     senf::IPv6HopByHopOptionsPacket::Parser::options_t::container::iterator listIter (optC.begin());
209
210     BOOST_CHECK_EQUAL( listIter->altAction(), 0u);
211     BOOST_CHECK_EQUAL( listIter->changeFlag(), 0u);
212     BOOST_CHECK_EQUAL( listIter->optionType(), 5u);
213     BOOST_CHECK_EQUAL( listIter->optionLength(), 2u);
214     ++listIter;
215     BOOST_CHECK_EQUAL( listIter->altAction(), 0u);
216     BOOST_CHECK_EQUAL( listIter->changeFlag(), 0u);
217     BOOST_CHECK_EQUAL( listIter->optionType(), 2u);
218     BOOST_CHECK_EQUAL( listIter->optionLength(), 0);
219
220     BOOST_REQUIRE( pHop_extension.next().is<senf::ICMPv6Packet>() );
221     senf::ICMPv6Packet pICMPv6  (pHop_extension.next().as<senf::ICMPv6Packet>());
222     BOOST_CHECK_EQUAL( pICMPv6->type(), 0x8f);
223     BOOST_CHECK_EQUAL( pICMPv6->code(), 0u);
224     BOOST_CHECK_EQUAL( pICMPv6->checksum(), 0x50cc);
225 }
226
227 BOOST_AUTO_UNIT_TEST(ipv6Extensions_hopByHop_create)                                                                                                      
228 {   
229     try{
230     std::ostringstream oss (std::ostringstream::out);                                                                                                   
231     unsigned char HopByHop_packetData[] = {
232         //Ethernet
233         0x33 ,0x33 ,0x00 ,0x00 ,0x00 ,0x16 ,    //destination MAC
234         0x00 ,0x19 ,0xb9 ,0xeb ,0xb2 ,0x26 ,    //source MAC
235         0x86 ,0xdd,                             //type (IPv6)
236         //IPv6
237         0x60, 0x00, 0x00, 0x00, //IP version, class, flow label
238         0x00, 0x24,           //payload length
239         0x00,                 //next header: IPv6 hop-by-hop option (0)
240         0x01,                 //hop limit (1)
241         0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     //IPv6 Source address (fe80::219:b9ff:feeb:b226)
242         0x02, 0x19, 0xb9, 0xff, 0xfe, 0xeb, 0xb2, 0x26,
243
244         0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     //IPv6 Destination address ff02::16
245         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16,
246         //HopByHop option
247         0x3a,   //next Header (ICMPv6)
248         0x00,   //Length (0 = 8Bytes)
249
250         //option Header
251         0x05, //option type
252         0x02, //option Length (= 2 byte)
253         0x00, 0x00, //data (zero data here ...)
254         0x01, 0x00, //padding
255         //ICMPv6
256         0x8f, //type 143
257         0x00, //code 0, should always be 0
258         0x50 ,0xcc , //checksum
259
260         //MLDv2
261         0x00 ,0x00 , //reserved, zero by default
262         0x00 ,0x01 , //nr of McAddressRecords
263         0x04 ,  //recordType
264         0x00 ,  //auxiliarydatalength
265         0x00 ,0x00 ,    // number of sources
266         0xff ,0x02 ,0x00 ,0x00, 0x00 ,0x00 ,0x00 ,0x00 ,    //ipv6McAddress ff02::1:ffeb:b226
267         0x00 ,0x00 ,0x00 ,0x01 ,0xff ,0xeb ,0xb2 ,0x26
268     };
269     senf::EthernetPacket eth (senf::EthernetPacket::create() );
270     eth->destination() = senf::MACAddress::from_string( "33:33:00:00:00:16" );
271     eth->source() = senf::MACAddress::from_string( "00:19:b9:eb:b2:26" );
272     eth->type_length() = 0x86dd;
273
274     senf::IPv6Packet ip (senf::IPv6Packet::createAfter(eth));
275     ip->version() = 6u;
276     ip->length() = 12u;
277     ip->nextHeader() = 0u;
278     ip->hopLimit() = 1u;
279     ip->source() = senf::INet6Address::from_string("fe80::219:b9ff:feeb:b226");
280     ip->destination() = senf::INet6Address::from_string("ff02::16");
281
282     senf::IPv6HopByHopOptionsPacket pext (senf::IPv6HopByHopOptionsPacket::createAfter(ip) );
283     pext->nextHeader() = 58u;
284     pext->headerLength() = 0u;
285     {
286         senf::IPv6HopByHopOptionsPacket::Parser::options_t::container optC(pext->options() );
287         {
288             senf::IPv6GenericOptionTLVParser opt ( optC.push_back_space().init<senf::IPv6GenericOptionTLVParser>());
289             opt.altAction() = 0u;
290             opt.changeFlag() = 0u;
291             opt.optionType() = 5u;
292             unsigned char val[] = {0x00, 0x00};
293             opt.setPayload(val);
294         }
295     }
296
297     senf::ICMPv6Packet icmp (senf::ICMPv6Packet::createAfter (pext));
298     icmp->type() = 0x8f;
299     icmp->code() = 0u;
300     icmp->checksum() = 0u;
301     senf::MLDv2ListenerReport mld ( senf::MLDv2ListenerReport::createAfter(icmp) );
302     {
303         senf::MLDv2ListenerReport::Parser::mcastAddrRecords_t::container c (mld->mcastAddrRecords() );
304         c.push_back_space();
305         c.back().recordType() = 4u;
306         senf::INet6Address addr( senf::INet6Address::from_string("ff02::1:ffeb:b226") );
307         c.back().mcAddress() = addr;
308     }
309     eth.finalizeAll();
310     SENF_CHECK_NO_THROW( ip.dump(oss) );
311     SENF_CHECK_EQUAL_COLLECTIONS(
312             HopByHop_packetData, HopByHop_packetData+sizeof(HopByHop_packetData),
313             eth.data().begin(), eth.data().end() );
314     eth.dump(std::cout);
315     senf::hexdump(eth.data().begin(), eth.data().end(), std::cout);
316     std::cout << "\n\n" << std::endl;
317     senf::hexdump(boost::begin(HopByHop_packetData), boost::end(HopByHop_packetData), std::cout);
318     }
319     catch (std::exception & e)
320     {
321         std::cerr << e.what() << std::endl; throw;
322     }
323 }
324
325 ///////////////////////////////cc.e////////////////////////////////////////
326 #undef prefix_
327
328
329 // Local Variables:
330 // mode: c++
331 // fill-column: 100
332 // c-file-style: "senf"
333 // indent-tabs-mode: nil
334 // ispell-local-dictionary: "american"
335 // compile-command: "scons -u test"
336 // comment-column: 40
337 // End: