Fixed whitespace in all files (no tabs)
[senf.git] / Packets / IpV6Extensions.test.cc
1 // Copyright (C) 2007
2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 //     Stefan Bund <g0dil@berlios.de>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 /** \file
22     \brief IpV6Extensions.test unit tests */
23
24 //#include "IpV6Extensions.test.hh"
25 //#include "IpV6Extensions.test.ih"
26
27 // Custom includes
28 #include "IpV6Extensions.hh"
29 #include "IpV6Packet.hh"
30 #include "UDPPacket.hh"
31 #include "DataPacket.hh"
32 #include "Socket/INetAddressing.hh"
33
34 #include <boost/test/auto_unit_test.hpp>
35 #include <boost/test/test_tools.hpp>
36
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 using namespace senf;
41
42 BOOST_AUTO_UNIT_TEST(ipv6Extension_Fragment_parser)
43 {
44     unsigned char data[] = { 59, 0, 0x10, 0x20,
45                              0x01, 0x02, 0x03, 0x04 };
46
47     typedef unsigned char * iterator;
48     Parse_IpV6Extension_Fragment<iterator> p (data);
49
50     BOOST_CHECK_EQUAL( unsigned(p.nextHeader()), 59u );
51     BOOST_CHECK_EQUAL( unsigned(p.fragmentOffset()), 0x1020u >> 3 );
52     BOOST_CHECK ( ! p.moreFragments() );
53     BOOST_CHECK_EQUAL( unsigned(p.id()), 0x01020304u );
54 }
55
56 BOOST_AUTO_UNIT_TEST(ipv6Extension_Fragment_packet)
57 {
58     // Just for the fun of it, we test a nice chain: A fragment of a fragmented UDP packet
59
60     unsigned char data[] = {
61         // IP header
62         0x60, 0x00, 0x00, 0x00,         // IP Version, class, flow label
63         0, 20,                          // payload length
64         44,                             // next header (IPv6 Fragment)
65         32,                             // hop limit
66         0x20, 0x01, 0, 0, 0, 0, 0, 0,   // source ip = 2001::1
67            0, 0, 0, 0, 0, 0, 0, 0x01,
68         0x20, 0x01, 0, 0, 0, 0, 0, 0,   // destination ip = 2001::2
69            0, 0, 0, 0, 0, 0, 0, 0x02,
70         // IPv6 Fragment header
71         17,                             // next header (UDP)
72         0,                              // reserved
73         0x05, 0x00,                     // fragment offset, last fragment
74         0x01, 0x02, 0x03, 0x04,         // id
75         // UDP header
76         0x10, 0x00,                     // source port
77         0x20, 0x00,                     // destination port
78         0, 12,                          // length
79         0x00, 0x00,                     // CRC (no, I won't calculate this one ...)
80         // Payload data
81         0x11, 0x12, 0x13, 0x14
82     };
83
84     IpV6Packet::ptr p (Packet::create<IpV6Packet>(data, data + sizeof(data)));
85
86     BOOST_CHECK_EQUAL( p->version(), 6u );
87     BOOST_CHECK_EQUAL( p->length(), 20u );
88     BOOST_CHECK_EQUAL( p->nextHeader(), 44u );
89     BOOST_CHECK_EQUAL( INet6Address(p->source().range()), "2001::1" );
90     BOOST_CHECK_EQUAL( INet6Address(p->destination().range()), "2001::2" );
91     BOOST_CHECK( p->next()->is<IpV6Extension_Fragment>() );
92
93     IpV6Extension_Fragment::ptr f (p->next()->as<IpV6Extension_Fragment>());
94
95     BOOST_CHECK_EQUAL( f->nextHeader(), 17u );
96     BOOST_CHECK_EQUAL( f->fragmentOffset(), 160u );
97     BOOST_CHECK_EQUAL( f->id(), 0x01020304u );
98     BOOST_CHECK( f->next()->is<UDPPacket>() );
99
100     UDPPacket::ptr u (f->next()->as<UDPPacket>());
101
102     BOOST_CHECK_EQUAL( u->source(), 0x1000u );
103     BOOST_CHECK_EQUAL( u->destination(), 0x2000u );
104     BOOST_CHECK_EQUAL( u->length(), 12u );
105     BOOST_CHECK( u->next()->is<DataPacket>() );
106
107     Packet::iterator i (u->next()->begin());
108     BOOST_CHECK_EQUAL( Parse_UInt32<Packet::iterator>(i).value(), 0x11121314u );
109 }
110
111 ///////////////////////////////cc.e////////////////////////////////////////
112 #undef prefix_
113
114 \f
115 // Local Variables:
116 // mode: c++
117 // fill-column: 100
118 // c-file-style: "senf"
119 // indent-tabs-mode: nil
120 // ispell-local-dictionary: "american"
121 // End: