switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / DefaultBundle / IPv4Packet.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief IPv4Packet unit tests */
30
31 //#include "IPv4Packet.test.hh"
32 //#include "IPv4Packet.test.ih"
33
34 // Custom includes
35 #include "IPv4Packet.hh"
36 #include "UDPPacket.hh"
37
38 #include <senf/Utils/auto_unit_test.hh>
39 #include <boost/test/test_tools.hpp>
40
41 #define prefix_
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43
44 using namespace senf;
45
46 SENF_AUTO_UNIT_TEST(ipV4Packet_parse)
47 {
48     unsigned char data[] = {
49             0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
50             0x09, 0x0A, 0x0B, 0x0C, 0x11, 0x12, 0x13, 0x14,
51             0x15, 0x16, 0x17, 0x18
52     };
53
54     senf::IPv4Packet p (senf::IPv4Packet::create(data));
55
56     BOOST_CHECK_EQUAL( p->version(),     0x00u       );
57     BOOST_CHECK_EQUAL( p->ihl(),         0x01u       );
58     BOOST_CHECK_EQUAL( p->tos(),         0x02u       );
59     BOOST_CHECK_EQUAL( p->length(),      0x0304u     );
60     BOOST_CHECK_EQUAL( p->identifier(),  0x0506u     );
61     BOOST_CHECK_EQUAL( p->df(),          0           );
62     BOOST_CHECK_EQUAL( p->mf(),          0           );
63     BOOST_CHECK_EQUAL( p->frag(),        0x0708u     );
64     BOOST_CHECK_EQUAL( p->ttl(),         0x09u       );
65     BOOST_CHECK_EQUAL( p->protocol(),    0x0Au       );
66     BOOST_CHECK_EQUAL( p->checksum(),    0x0B0Cu     );
67     BOOST_CHECK_EQUAL( p->source().value(), senf::INet4Address(0x11121314u) );
68     BOOST_CHECK_EQUAL( p->destination().value(), senf::INet4Address(0x15161718u) );
69
70     std::ostringstream oss (std::ostringstream::out);
71     SENF_CHECK_NO_THROW( p.dump( oss));
72 }
73
74 SENF_AUTO_UNIT_TEST(ipV4Packet_create)
75 {
76     senf::IPv4Packet ip (senf::IPv4Packet::create());
77
78     BOOST_CHECK_EQUAL( ip->version(), 4u );
79     BOOST_CHECK_EQUAL( ip->ihl(), 5u );
80     BOOST_CHECK_EQUAL( ip.size(), 20u );
81
82     senf::UDPPacket udp (senf::UDPPacket::createAfter(ip));
83
84     BOOST_CHECK( ! ip->validateChecksum() );
85
86     ip.finalizeAll();
87     BOOST_CHECK_EQUAL( ip->length(), 28u );
88     BOOST_CHECK_EQUAL( ip->protocol(), 17u );
89     BOOST_CHECK_EQUAL( ip->checksum(), 0xbad2 );
90
91     // Check, that the checksum field is correctly skipped
92     ip.finalizeAll();
93     BOOST_CHECK_EQUAL( ip->checksum(), 0xbad2 );
94
95     BOOST_CHECK( ip->validateChecksum() );
96 }
97
98 //-/////////////////////////////////////////////////////////////////////////////////////////////////
99 #undef prefix_
100
101 \f
102 // Local Variables:
103 // mode: c++
104 // fill-column: 100
105 // c-file-style: "senf"
106 // indent-tabs-mode: nil
107 // ispell-local-dictionary: "american"
108 // compile-command: "scons -u test"
109 // comment-column: 40
110 // End: