switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / DefaultBundle / UDPPacket.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 UDPPacket unit tests */
30
31 //#include "UDPPacket.test.hh"
32 //#include "UDPPacket.test.ih"
33
34 // Custom includes
35 #include "UDPPacket.hh"
36 #include "IPv4Packet.hh"
37 #include "IPv6Packet.hh"
38
39 #include <senf/Utils/auto_unit_test.hh>
40 #include <boost/test/test_tools.hpp>
41
42 #define prefix_
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44
45 SENF_AUTO_UNIT_TEST(udpPacket_parse)
46 {
47
48     unsigned char data[] = {
49             0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
50     };
51
52     senf::UDPPacket p (senf::UDPPacket::create(data));
53
54     BOOST_CHECK_EQUAL( p->source(),            0x0102       );
55     BOOST_CHECK_EQUAL( p->destination(),       0x0304       );
56     BOOST_CHECK_EQUAL( p->length(),            0x0506       );
57     BOOST_CHECK_EQUAL( p->checksum(),          0x0708       );
58
59     std::ostringstream oss (std::ostringstream::out);
60     SENF_CHECK_NO_THROW( p.dump( oss));
61 }
62
63 SENF_AUTO_UNIT_TEST(udpPacket_in_ipv4_create)
64 {
65     unsigned char data[] = {
66             0x45, 0x00, 0x00, 0x26, 0x00, 0x00, 0x40, 0x00,
67             0x40, 0x11, 0x3c, 0xc5, 0x7f, 0x00, 0x00, 0x01,
68             0x7f, 0x00, 0x00, 0x01, 0x5b, 0xa0, 0x30, 0x39,
69             0x00, 0x12, 0xfa, 0x6e, 0x54, 0x45, 0x53, 0x54,
70             0x2d, 0x57, 0x52, 0x49, 0x54, 0x45
71     };
72
73     senf::IPv4Packet ip (senf::IPv4Packet::create());
74     ip->source() = senf::INet4Address::Loopback;
75     ip->destination() = senf::INet4Address::Loopback;
76     ip->df() = true;
77     ip->ttl() = 64;
78
79     senf::UDPPacket udp (senf::UDPPacket::createAfter(ip));
80     udp->source() = 23456;
81     udp->destination() = 12345;
82
83     senf::DataPacket::createAfter(udp, std::string("TEST-WRITE"));
84
85     // validates, since the checksum is 0 and thus ignored !
86     BOOST_CHECK( udp->validateChecksum() );
87
88     ip.finalizeAll();
89     BOOST_CHECK_EQUAL_COLLECTIONS(
90             data, data+sizeof(data), ip.data().begin(), ip.data().end() );
91     BOOST_CHECK( udp->validateChecksum() );
92 }
93
94 SENF_AUTO_UNIT_TEST(udpPacket_in_ipv6_parse)
95 {
96     // captured udp packet generated with mgen send over ipv6
97     unsigned char data[] = {
98             // IPv6 Packet
99             0x60, 0x00, 0x00, 0x00, 0x00, 0x32, 0x11, 0x40,
100             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
101             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
102             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
103             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
104             // UDP Packet
105             0x13, 0x89, 0x13, 0x88, 0x00, 0x32, 0x11, 0x23,
106             // mgen payload
107             0x00, 0x2a, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01,
108             0x00, 0x00, 0x00, 0x0b, 0x49, 0xb5, 0x0a, 0x90,
109             0x00, 0x09, 0x5b, 0x37, 0x13, 0x88, 0x02, 0x10,
110             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
111             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
112             0x00, 0x00
113     };
114
115     senf::IPv6Packet ip (senf::IPv6Packet::create(data));
116     BOOST_CHECK_EQUAL( ip->length(),     50u  );
117     BOOST_CHECK_EQUAL( ip->nextHeader(), 0x11 );
118     BOOST_CHECK_EQUAL( ip->hopLimit(),   64u  );
119     BOOST_CHECK_EQUAL( ip->source().value(),      senf::INet6Address::Loopback );
120     BOOST_CHECK_EQUAL( ip->destination().value(), senf::INet6Address::Loopback );
121
122     std::ostringstream oss (std::ostringstream::out);
123     SENF_CHECK_NO_THROW( ip.dump( oss));
124
125     BOOST_REQUIRE( ip.next().is<senf::UDPPacket>() );
126     senf::UDPPacket udp (ip.next().as<senf::UDPPacket>());
127
128     BOOST_CHECK_EQUAL( udp->source(),      5001u  );
129     BOOST_CHECK_EQUAL( udp->destination(), 5000u  );
130     BOOST_CHECK_EQUAL( udp->length(),      50u    );
131     BOOST_CHECK_EQUAL( udp->checksum(),    0x1123 );
132
133     BOOST_CHECK( udp->validateChecksum() );
134 }
135
136 //-/////////////////////////////////////////////////////////////////////////////////////////////////
137 #undef prefix_
138
139 \f
140 // Local Variables:
141 // mode: c++
142 // fill-column: 100
143 // c-file-style: "senf"
144 // indent-tabs-mode: nil
145 // ispell-local-dictionary: "american"
146 // compile-command: "scons -u test"
147 // comment-column: 40
148 // End: