Packets: added tests for dump methods
[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
32 #include "../../Utils/auto_unit_test.hh"
33 #include <boost/test/test_tools.hpp>
34
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 BOOST_AUTO_UNIT_TEST(udpPacket_packet)
39 {
40
41     unsigned char data[] = { 0x01, 0x02, 0x03, 0x04,
42                              0x05, 0x06, 0x07, 0x08
43                            };
44
45     senf::UDPPacket p (senf::UDPPacket::create(data));
46
47     BOOST_CHECK_EQUAL( p->source(),            0x0102       );
48     BOOST_CHECK_EQUAL( p->destination(),       0x0304       );
49     BOOST_CHECK_EQUAL( p->length(),            0x0506       );
50     BOOST_CHECK_EQUAL( p->checksum(),          0x0708       );
51
52     std::ostringstream oss (std::ostringstream::out);
53     SENF_CHECK_NO_THROW( p.dump( oss));
54 }
55
56 BOOST_AUTO_UNIT_TEST(udpPacket_create)
57 {
58     unsigned char data[] = { 0x45, 0x00, 0x00, 0x26, 0x00, 0x00, 0x40, 0x00,
59                              0x40, 0x11, 0x3c, 0xc5, 0x7f, 0x00, 0x00, 0x01,
60                              0x7f, 0x00, 0x00, 0x01, 0x5b, 0xa0, 0x30, 0x39,
61                              0x00, 0x12, 0xfa, 0x6e, 0x54, 0x45, 0x53, 0x54,
62                              0x2d, 0x57, 0x52, 0x49, 0x54, 0x45 };
63
64     senf::IPv4Packet ip (senf::IPv4Packet::create());
65     ip->source() = senf::INet4Address::Loopback;
66     ip->destination() = senf::INet4Address::Loopback;
67     ip->df() = true;
68     ip->ttl() = 64;
69
70     senf::UDPPacket udp (senf::UDPPacket::createAfter(ip));
71     udp->source() = 23456;
72     udp->destination() = 12345;
73
74     senf::DataPacket::createAfter(udp,std::string("TEST-WRITE"));
75
76     // validates, since the checksum is 0 and thus ignored !
77     BOOST_CHECK( udp->validateChecksum() );
78
79     ip.finalizeAll();
80     BOOST_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data),
81                                    ip.data().begin(), ip.data().end() );
82     BOOST_CHECK( udp->validateChecksum() );
83 }
84
85
86 ///////////////////////////////cc.e////////////////////////////////////////
87 #undef prefix_
88
89
90 // Local Variables:
91 // mode: c++
92 // fill-column: 100
93 // c-file-style: "senf"
94 // indent-tabs-mode: nil
95 // ispell-local-dictionary: "american"
96 // compile-command: "scons -u test"
97 // comment-column: 40
98 // End: