2447f5b01fdeed65516a94f243ff2a2aae39c0c5
[senf.git] / senf / Packets / DefaultBundle / IPv4Packet.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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 IPv4Packet unit tests */
25
26 //#include "IPv4Packet.test.hh"
27 //#include "IPv4Packet.test.ih"
28
29 // Custom includes
30 #include "IPv4Packet.hh"
31 #include "UDPPacket.hh"
32
33 #include <senf/Utils/auto_unit_test.hh>
34 #include <boost/test/test_tools.hpp>
35
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 using namespace senf;
40
41 SENF_AUTO_UNIT_TEST(ipV4Packet_parse)
42 {
43     unsigned char data[] = {
44             0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
45             0x09, 0x0A, 0x0B, 0x0C, 0x11, 0x12, 0x13, 0x14,
46             0x15, 0x16, 0x17, 0x18
47     };
48
49     senf::IPv4Packet p (senf::IPv4Packet::create(data));
50
51     BOOST_CHECK_EQUAL( p->version(),     0x00u       );
52     BOOST_CHECK_EQUAL( p->ihl(),         0x01u       );
53     BOOST_CHECK_EQUAL( p->tos(),         0x02u       );
54     BOOST_CHECK_EQUAL( p->length(),      0x0304u     );
55     BOOST_CHECK_EQUAL( p->identifier(),  0x0506u     );
56     BOOST_CHECK_EQUAL( p->df(),          0           );
57     BOOST_CHECK_EQUAL( p->mf(),          0           );
58     BOOST_CHECK_EQUAL( p->frag(),        0x0708u     );
59     BOOST_CHECK_EQUAL( p->ttl(),         0x09u       );
60     BOOST_CHECK_EQUAL( p->protocol(),    0x0Au       );
61     BOOST_CHECK_EQUAL( p->checksum(),    0x0B0Cu     );
62     BOOST_CHECK_EQUAL( p->source().value(), senf::INet4Address(0x11121314u) );
63     BOOST_CHECK_EQUAL( p->destination().value(), senf::INet4Address(0x15161718u) );
64
65     std::ostringstream oss (std::ostringstream::out);
66     SENF_CHECK_NO_THROW( p.dump( oss));
67 }
68
69 SENF_AUTO_UNIT_TEST(ipV4Packet_create)
70 {
71     senf::IPv4Packet ip (senf::IPv4Packet::create());
72
73     BOOST_CHECK_EQUAL( ip->version(), 4u );
74     BOOST_CHECK_EQUAL( ip->ihl(), 5u );
75     BOOST_CHECK_EQUAL( ip.size(), 20u );
76
77     senf::UDPPacket udp (senf::UDPPacket::createAfter(ip));
78
79     BOOST_CHECK( ! ip->validateChecksum() );
80
81     ip.finalizeAll();
82     BOOST_CHECK_EQUAL( ip->length(), 28u );
83     BOOST_CHECK_EQUAL( ip->protocol(), 17u );
84     BOOST_CHECK_EQUAL( ip->checksum(), 0xbad2 );
85
86     // Check, that the checksum field is correctly skipped
87     ip.finalizeAll();
88     BOOST_CHECK_EQUAL( ip->checksum(), 0xbad2 );
89
90     BOOST_CHECK( ip->validateChecksum() );
91 }
92
93 ///////////////////////////////cc.e////////////////////////////////////////
94 #undef prefix_
95
96 \f
97 // Local Variables:
98 // mode: c++
99 // fill-column: 100
100 // c-file-style: "senf"
101 // indent-tabs-mode: nil
102 // ispell-local-dictionary: "american"
103 // compile-command: "scons -u test"
104 // comment-column: 40
105 // End: