4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief UDPPacket unit tests */
31 //#include "UDPPacket.test.hh"
32 //#include "UDPPacket.test.ih"
35 #include "UDPPacket.hh"
36 #include "IPv4Packet.hh"
37 #include "IPv6Packet.hh"
39 #include <senf/Utils/auto_unit_test.hh>
40 #include <boost/test/test_tools.hpp>
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45 SENF_AUTO_UNIT_TEST(udpPacket_parse)
48 unsigned char data[] = {
49 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
52 senf::UDPPacket p (senf::UDPPacket::create(data));
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 );
59 std::ostringstream oss (std::ostringstream::out);
60 SENF_CHECK_NO_THROW( p.dump( oss));
63 SENF_AUTO_UNIT_TEST(udpPacket_in_ipv4_create)
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
73 senf::IPv4Packet ip (senf::IPv4Packet::create());
74 ip->source() = senf::INet4Address::Loopback;
75 ip->destination() = senf::INet4Address::Loopback;
79 senf::UDPPacket udp (senf::UDPPacket::createAfter(ip));
80 udp->source() = 23456;
81 udp->destination() = 12345;
83 senf::DataPacket::createAfter(udp, std::string("TEST-WRITE"));
85 // validates, since the checksum is 0 and thus ignored !
86 BOOST_CHECK( udp->validateChecksum() );
89 BOOST_CHECK_EQUAL_COLLECTIONS(
90 data, data+sizeof(data), ip.data().begin(), ip.data().end() );
91 BOOST_CHECK( udp->validateChecksum() );
94 SENF_AUTO_UNIT_TEST(udpPacket_in_ipv6_parse)
96 // captured udp packet generated with mgen send over ipv6
97 unsigned char data[] = {
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,
105 0x13, 0x89, 0x13, 0x88, 0x00, 0x32, 0x11, 0x23,
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,
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 );
122 std::ostringstream oss (std::ostringstream::out);
123 SENF_CHECK_NO_THROW( ip.dump( oss));
125 BOOST_REQUIRE( ip.next().is<senf::UDPPacket>() );
126 senf::UDPPacket udp (ip.next().as<senf::UDPPacket>());
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 );
133 BOOST_CHECK( udp->validateChecksum() );
136 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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