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