Utils: Add SENF_CHECK_EQUAL_COLLECTIONS() unit-test for better char range support
[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 //
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the
21 // Free Software Foundation, Inc.,
22 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23
24 /** \file
25     \brief IPv6Extensions unit tests */
26
27 //#include "IPv6Extensions.test.hh"
28 //#include "IPv6Extensions.test.ih"
29
30 // Custom includes
31 #include "IPv6Extensions.hh"
32 #include "IPv6Packet.hh"
33 #include "UDPPacket.hh"
34 #include "ICMPv6Packet.hh"
35 #include <senf/Utils/hexdump.hh>
36
37 #include <senf/Utils/auto_unit_test.hh>
38 #include <boost/test/test_tools.hpp>
39
40 #define prefix_
41 ///////////////////////////////cc.p////////////////////////////////////////
42
43 BOOST_AUTO_UNIT_TEST(ipv6Extensions_fragment)
44 {
45     // Just for the fun of it, we test a nice chain: A fragment of a fragmented UDP packet
46
47     unsigned char Fragment_packetData[] = {
48         // IP header
49         0x60, 0x00, 0x00, 0x00,         // IP Version, class, flow label
50         0, 20,                          // payload length
51         44,                             // next header (IPv6 Fragment)
52         32,                             // hop limit
53         0x20, 0x01, 0, 0, 0, 0, 0, 0,   // source ip = 2001::1
54            0, 0, 0, 0, 0, 0, 0, 0x01,
55         0x20, 0x01, 0, 0, 0, 0, 0, 0,   // destination ip = 2001::2
56            0, 0, 0, 0, 0, 0, 0, 0x02,
57         // IPv6 Fragment header
58         17,                             // next header (UDP)
59         0,                              // reserved
60         0x05, 0x00,                     // fragment offset, last fragment
61         0x01, 0x02, 0x03, 0x04,         // id
62         // UDP header
63         0x10, 0x00,                     // source port
64         0x20, 0x00,                     // destination port
65         0, 12,                          // length
66         0x00, 0x00,                     // CRC (no, I won't calculate this one ...)
67         // Payload data
68         0x11, 0x12, 0x13, 0x14
69     };
70
71     senf::IPv6Packet pFragment_packet (senf::IPv6Packet::create(Fragment_packetData));
72
73     BOOST_CHECK_EQUAL( pFragment_packet->version(), 6u );
74     BOOST_CHECK_EQUAL( pFragment_packet->length(), 20u );
75     BOOST_CHECK_EQUAL( pFragment_packet->nextHeader(), 44u );
76     BOOST_CHECK_EQUAL( pFragment_packet->source().value(), 
77             senf::INet6Address::from_string("2001::1") );
78     BOOST_CHECK_EQUAL( pFragment_packet->destination().value(), 
79             senf::INet6Address::from_string("2001::2") );
80
81     std::ostringstream oss (std::ostringstream::out);
82     SENF_CHECK_NO_THROW( pFragment_packet.dump( oss));
83
84     BOOST_CHECK( pFragment_packet.next().is<senf::IPv6FragmentPacket>() );
85
86     senf::IPv6FragmentPacket fFragment_packet (pFragment_packet.next().as<senf::IPv6FragmentPacket>());
87
88     BOOST_CHECK_EQUAL( fFragment_packet->nextHeader(), 17u );
89     BOOST_CHECK_EQUAL( fFragment_packet->fragmentOffset(), 160u );
90     BOOST_CHECK_EQUAL( fFragment_packet->id(), 0x01020304u );
91     BOOST_CHECK( fFragment_packet.next().is<senf::UDPPacket>() );
92
93     senf::UDPPacket uFragment_packet (fFragment_packet.next().as<senf::UDPPacket>());
94
95     BOOST_CHECK_EQUAL( uFragment_packet->source(), 0x1000u );
96     BOOST_CHECK_EQUAL( uFragment_packet->destination(), 0x2000u );
97     BOOST_CHECK_EQUAL( uFragment_packet->length(), 12u );
98     BOOST_CHECK( uFragment_packet.next().is<senf::DataPacket>() );
99
100     senf::DataPacket dFragment_packet (uFragment_packet.next().as<senf::DataPacket>());
101     senf::PacketData::iterator i (uFragment_packet.next().data().begin());
102     BOOST_CHECK_EQUAL( dFragment_packet.size(), 4u );
103     BOOST_CHECK_EQUAL( dFragment_packet.data()[0], 0x11 );
104 }
105
106
107 BOOST_AUTO_UNIT_TEST(ipv6Extensions_routing)
108 {
109     unsigned char Routing_packetData[] = {
110         // IP header
111         0x60, 0x30, 0x00, 0x00, 
112         0x00, 0x10,             // payload Length
113         0x2b,                   // next Header   (43 = Routing Header)
114         0xff,                   // Hop Limit
115         0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
116         0x02, 0x01, 0x02, 0xff, 0xfe, 0x00, 0x02, 0x00, // Src Addr
117         0x35, 0x55, 0x55, 0x55, 0x66, 0x66, 0x66, 0x66,
118         0x77, 0x77, 0x77, 0x77, 0x88, 0x88, 0x88, 0x88, // Dest. Addr
119         // Routing Header
120         0x3a,                   // nextHeader (58)
121         0x00,                   // length ( 0 )
122         0x00,                   // routing type ( 0 )
123         0x00,                   // Left Segments ( 0 )
124         0x00, 0x00, 0x00, 0x00, // reserved
125         // ICMPv6
126         0x01,                   // type: 1 (unreachable )
127         0x00,                   // code: 0 (route unreachable)
128         0xa3, 0xd3,             // checksum (incorrect in wireshark capture file, should be 0xa3c4)
129         0x00, 0x00, 0x00, 0x00
130     };
131     
132     senf::IPv6Packet pRouting_packet (senf::IPv6Packet::create(Routing_packetData));
133
134     BOOST_CHECK_EQUAL( pRouting_packet->version(), 6u );
135     BOOST_CHECK_EQUAL( pRouting_packet->length(), 16u );
136     BOOST_CHECK_EQUAL( pRouting_packet->nextHeader(), 43u );
137     BOOST_CHECK_EQUAL( pRouting_packet->source().value(), 
138             senf::INet6Address::from_string("fe80::201:2ff:fe00:200") );
139     BOOST_CHECK_EQUAL( pRouting_packet->destination().value(), 
140             senf::INet6Address::from_string("3555:5555:6666:6666:7777:7777:8888:8888"));
141     
142     std::ostringstream oss (std::ostringstream::out);
143     SENF_CHECK_NO_THROW( pRouting_packet.dump( oss));
144
145     BOOST_REQUIRE( pRouting_packet.next().is<senf::IPv6RoutingPacket>() );
146
147     senf::IPv6RoutingPacket pRouting_extension (pRouting_packet.next().as<senf::IPv6RoutingPacket>());
148
149     BOOST_CHECK_EQUAL( pRouting_extension->nextHeader(), 58u );
150     BOOST_CHECK_EQUAL( pRouting_extension->headerLength(), 0x00 );
151     BOOST_CHECK_EQUAL( pRouting_extension->routingType(), 0x00 );
152     BOOST_CHECK_EQUAL( pRouting_extension->segmentsLeft(), 0x00);
153     
154     BOOST_CHECK_EQUAL( pRouting_extension->reserved(), 0u);
155
156     BOOST_REQUIRE( pRouting_extension.next().is<senf::ICMPv6Packet>() );
157     senf::ICMPv6Packet pICMPv6 (pRouting_extension.next().as<senf::ICMPv6Packet>());
158     BOOST_CHECK_EQUAL( pICMPv6->type(), 1u);
159     BOOST_CHECK_EQUAL( pICMPv6->code(), 0u);
160     BOOST_CHECK_EQUAL( pICMPv6->checksum(), 0xa3d3);
161 }
162
163
164 BOOST_AUTO_UNIT_TEST(ipv6Extensions_hopByHop_parse)
165 {
166     unsigned char HopByHop_packetData[] = {
167         0x60, 0x00, 0x00, 0x00, // IP version, class, flow label
168         0x00, 0x24,             // payload length
169         0x00,                   // next header: IPv6 hop-by-hop option (0)
170         0x01,                   // hop limit (1)
171         0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
172         0x02, 0x19, 0xb9, 0xff, 0xfe, 0xeb, 0xb2, 0x26, // IPv6 Source address
173         0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     
174         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, // IPv6 Destination address ff02::16
175         // HopByHop option
176         0x3a, // next Header (ICMPv6)
177         0x00, // Length (0 = 8Bytes)
178         // option Header
179         0x05, // option type
180         0x02, // option Length (= 2 byte)
181         0x00, 0x00, //data (zero data here ...)
182         0x02, //option type (2, set for testing purposes only)
183         0x00,  //option Type length (=0, no data field needed here)
184         // ICMPv6
185         0x8f, // type 143
186         0x00, // code 0, should always be 0
187         0x50, 0xcc, // checksum
188     };
189
190     senf::IPv6Packet pHop_packet (senf::IPv6Packet::create(HopByHop_packetData));
191     BOOST_CHECK_EQUAL( pHop_packet->version(), 6u );
192     BOOST_CHECK_EQUAL( pHop_packet->length(), 36u );
193     BOOST_CHECK_EQUAL( pHop_packet->nextHeader(), 0u );
194     BOOST_CHECK_EQUAL( pHop_packet->source().value(), 
195             senf::INet6Address::from_string("fe80::219:b9ff:feeb:b226") );
196     BOOST_CHECK_EQUAL( pHop_packet->destination().value(), senf::INet6Address::from_string("ff02::16") );
197
198     std::ostringstream oss (std::ostringstream::out);
199     SENF_CHECK_NO_THROW( pHop_packet.dump( oss ));
200
201     BOOST_REQUIRE( pHop_packet.next().is<senf::IPv6HopByHopOptionsPacket>() );
202     senf::IPv6HopByHopOptionsPacket pHop_extension (
203             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     std::ostringstream oss (std::ostringstream::out);                                                                                                   
230     unsigned char HopByHop_packetData[] = {
231             0x60, 0x00, 0x00, 0x00, //IP version, class, flow label                                                                                       
232             0x00, 0x0c,           //payload length                                                                                                        
233             0x00,                 //next header: IPv6 hop-by-hop option (0)                                                                               
234             0x01,                 //hop limit (1)                                                                                                         
235             0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     //IPv6 Source address (fe80::219:b9ff:feeb:b226)                                          
236             0x02, 0x19, 0xb9, 0xff, 0xfe, 0xeb, 0xb2, 0x26,
237
238             0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     //IPv6 Destination address ff02::16
239             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16,
240             //HopByHop option
241             0x3a,   //next Header (ICMPv6)
242             0x00,   //Length (0 = 8Bytes)
243
244             //option Header
245             0x05, //option type
246             0x02, //option Length (= 2 byte)
247             0x00, 0x00, //data (zero data here ...)
248
249             0x02, //option type (2, set for testing purposes only)
250             0x00,  //option Type length (=0, no data field needed here)
251
252             //ICMPv6
253             0x8f, //type 143
254             0x00, //code 0, should always be 0
255             0xca, 0xdf, //checksum
256     };
257     senf::IPv6Packet ip (senf::IPv6Packet::create());
258     ip->version() = 6u;
259     ip->length() = 12u;
260     ip->nextHeader() = 0u;
261     ip->hopLimit() = 1u;
262     ip->source() = senf::INet6Address::from_string("fe80::219:b9ff:feeb:b226");
263     ip->destination() = senf::INet6Address::from_string("ff02::16");
264     senf::IPv6HopByHopOptionsPacket pext (senf::IPv6HopByHopOptionsPacket::createAfter(ip) );
265     pext->nextHeader() = 58u;
266     pext->headerLength() = 0u;
267     {
268         senf::IPv6HopByHopOptionsPacket::Parser::options_t::container optC(pext->options() );
269         
270         optC.push_back_space();
271         senf::IPv6GenericOptionTLVParser opt = optC.back().init<senf::IPv6GenericOptionTLVParser>();
272         opt.altAction() = 0u;
273         opt.changeFlag() = 0u;
274         opt.optionType() = 5u;
275         unsigned char val[] = {0x00, 0x00};
276         opt.setPayload(val);
277         
278         optC.push_back_space();
279         opt = optC.back().init<senf::IPv6GenericOptionTLVParser>();
280         opt.altAction() = 0u;
281         opt.changeFlag() = 0u;
282         opt.optionType() = 2u;
283     }
284     senf::ICMPv6Packet icmp (senf::ICMPv6Packet::createAfter (pext));
285     icmp->type() = 0x8f;
286     icmp->code() = 0u;
287     ip.finalizeAll();
288     SENF_CHECK_NO_THROW( ip.dump(oss) );
289     SENF_CHECK_EQUAL_COLLECTIONS(
290         HopByHop_packetData, HopByHop_packetData+sizeof(HopByHop_packetData),
291         ip.data().begin(), ip.data().end() );
292 }
293
294 ///////////////////////////////cc.e////////////////////////////////////////
295 #undef prefix_
296
297
298 // Local Variables:
299 // mode: c++
300 // fill-column: 100
301 // c-file-style: "senf"
302 // indent-tabs-mode: nil
303 // ispell-local-dictionary: "american"
304 // compile-command: "scons -u test"
305 // comment-column: 40
306 // End: