4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Dennis Goslar <dennis.goslar@inf.hochschule-bonn-rhein-sieg.de>
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.
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.
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.
24 \brief TCPPacket unit tests */
26 //#include "TCPPacket.test.hh"
27 //#include "TCPPacket.test.ih"
30 #include "TCPPacket.hh"
31 #include "IPv4Packet.hh"
32 #include "IPv6Packet.hh"
34 #include <senf/Utils/auto_unit_test.hh>
35 #include <boost/test/test_tools.hpp>
38 ///////////////////////////////cc.p////////////////////////////////////////
40 BOOST_AUTO_UNIT_TEST(tcpPacket_parse)
43 unsigned char data[] = {
44 0x80, 0xA6, 0x00, 0x50, 0x2E, 0x93, 0x31, 0xB8, 0x66, 0xF9, 0xB7,
45 0xA1, 0x80, 0x18, 0x00, 0x2E, 0xCF, 0x65, 0x00, 0x00
48 senf::TCPPacket p (senf::TCPPacket::create(data));
50 BOOST_CHECK_EQUAL( p->source(), 0x80A6u );
51 BOOST_CHECK_EQUAL( p->destination(), 0x0050u );
52 BOOST_CHECK_EQUAL( p->sequencenumber(), 0x2E9331B8u );
53 BOOST_CHECK_EQUAL( p->acknowledgmentnumber(), 0x66F9B7A1u );
54 BOOST_CHECK_EQUAL( p->dataoffset(), 0x8u );
55 BOOST_CHECK_EQUAL( p->urgf(), 0u );
56 BOOST_CHECK_EQUAL( p->ackf(), 1u );
57 BOOST_CHECK_EQUAL( p->pshf(), 1u );
58 BOOST_CHECK_EQUAL( p->rstf(), 0u );
59 BOOST_CHECK_EQUAL( p->synf(), 0u );
60 BOOST_CHECK_EQUAL( p->finf(), 0u );
61 BOOST_CHECK_EQUAL( p->window(), 0x002Eu );
62 BOOST_CHECK_EQUAL( p->checksum(), 0xCF65u );
63 BOOST_CHECK_EQUAL( p->urgentpointer(), 0x0000u );
65 std::ostringstream oss (std::ostringstream::out);
66 SENF_CHECK_NO_THROW( p.dump( oss));
69 BOOST_AUTO_UNIT_TEST(tcpPacket_in_ipv4_parse)
71 unsigned char data[] = {
73 0x45, 0x00, 0x00, 0xb2, 0x0f, 0x4d, 0x40, 0x00, 0x0f9, 0x06, 0xa0,
74 0xa1, 0xc1, 0x63, 0x90, 0x55, 0x0a, 0x14, 0x75, 0x8a,
76 //20 Byte TCP-Header + 12 Byte Options
77 0x00, 0x50, 0xc3, 0xbf, 0xe2, 0xf7, 0x49, 0xf3, 0x3e, 0x4f, 0x8a,
78 0xbe, 0x80, 0x18, 0x13, 0x24, 0x2e, 0x33, 0x00, 0x00, 0x01, 0x01,
79 0x08, 0x0a, 0x0f, 0xf1, 0xa7, 0x6b, 0x00, 0x59, 0xf4, 0x75,
82 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x20, 0x33, 0x30,
83 0x34, 0x20, 0x4e, 0x6f, 0x74, 0x20, 0x4d, 0x6f, 0x64, 0x69, 0x66,
84 0x69, 0x65, 0x64, 0x0d, 0x0a, 0x44, 0x61, 0x74, 0x65, 0x3a, 0x20,
85 0x54, 0x68, 0x75, 0x2c, 0x20, 0x32, 0x33, 0x20, 0x4a, 0x75, 0x6c,
86 0x20, 0x32, 0x30, 0x30, 0x39, 0x20, 0x31, 0x35, 0x3a, 0x31, 0x35,
87 0x3a, 0x31, 0x39, 0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a, 0x53, 0x65,
88 0x72, 0x76, 0x65, 0x72, 0x3a, 0x20, 0x41, 0x70, 0x61, 0x63, 0x68,
89 0x65, 0x2f, 0x32, 0x2e, 0x32, 0x2e, 0x31, 0x31, 0x0d, 0x0a, 0x43,
90 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20,
91 0x63, 0x6c, 0x6f, 0x73, 0x65, 0x0d, 0x0a, 0x56, 0x61, 0x72, 0x79,
92 0x3a, 0x20, 0x55, 0x73, 0x65, 0x72, 0x2d, 0x41, 0x67, 0x65, 0x6e,
93 0x74, 0x0d, 0x0a, 0x0d, 0x0a
96 senf::IPv4Packet ip (senf::IPv4Packet::create(data));
97 senf::TCPPacket tcp (ip.next<senf::TCPPacket>());
99 BOOST_CHECK( tcp->validateChecksum() );
103 BOOST_AUTO_UNIT_TEST(tcpPacket_in_ipv6_parse)
105 unsigned char data[] = {
107 0x60, 0x00, 0x00, 0x00, 0x00, 0xa5, 0x06, 0x39, 0x20, 0x01, 0x48,
108 0x60, 0xa0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
109 0x00, 0x68, 0x20, 0x01, 0x06, 0x38, 0x04, 0x08, 0x02, 0x01, 0xef,
110 0xdc, 0x71, 0x52, 0x17, 0x2c, 0x44, 0x3e,
112 // 20 Byte TCP-Header without Options
113 0x00, 0x50, 0xac, 0x3e, 0x82, 0x99, 0x60, 0xa6, 0xa4, 0xfa, 0x32,
114 0x83, 0x50, 0x18, 0x19, 0x20, 0xd7, 0xcd, 0x00, 0x00,
117 0x1a, 0x01, 0x52, 0x0e, 0x26, 0x6f, 0xac, 0xd9, 0x78, 0x0c, 0x3c,
118 0x69, 0x3c, 0xa6, 0x49, 0xc6, 0x85, 0xc2, 0xa9, 0x72, 0x75, 0x9a,
119 0xef, 0x63, 0x46, 0xa3, 0xd4, 0x11, 0xda, 0x04, 0x95, 0x55,0x7b,
120 0xb5, 0x6c, 0xcb, 0x52, 0xfb, 0xa0, 0x7a, 0x03, 0xd5, 0x98, 0x75,
121 0x8b, 0x53, 0xfc, 0x23, 0x21, 0x01, 0xc5, 0x06, 0x66, 0x6c, 0x0d,
122 0x06, 0x1d, 0x69, 0x2f, 0xec, 0xb7, 0x7d, 0xe3, 0xd6, 0x71, 0x23,
123 0xaa, 0x58, 0x3b, 0x80, 0x58, 0x5b, 0x10, 0xa1, 0xa8, 0x8f, 0x59,
124 0x3d, 0x3d, 0x80, 0xd6, 0x2e, 0x0d, 0x21, 0x33, 0x2b, 0x47, 0x5b,
125 0xb2, 0xd0, 0xde, 0x19, 0xaf, 0x03, 0x2a, 0x33,0x86, 0x57, 0x8e,
126 0x07, 0xf9, 0x6e, 0xde, 0x68, 0xe1, 0xd8, 0x63, 0x92, 0x18, 0x1a,
127 0xa9, 0xc0, 0xe5, 0x94, 0xba, 0x1d, 0x97, 0xed, 0x7e, 0x2b, 0xa8,
128 0xda, 0x4a, 0x96, 0x0c, 0xe5, 0x78, 0xb9, 0xec, 0x80, 0xf8, 0x5c,
129 0x94, 0xf9, 0xd3, 0x49, 0x79, 0x4a, 0x4a, 0x98, 0xb1, 0x13, 0x73,
133 senf::IPv6Packet ip (senf::IPv6Packet::create(data));
134 senf::TCPPacket tcp (ip.next<senf::TCPPacket>());
136 BOOST_CHECK( tcp->validateChecksum() );
139 BOOST_AUTO_UNIT_TEST(tcpPacket_in_ipv6_create)
141 unsigned char data[] = {
143 0x60, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x06, 0x40,
144 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
145 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
146 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
147 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
149 // TCP-Header + TCP-Payload
150 0x5b, 0xa0, 0x30, 0x39, 0x00, 0x00, 0x00, 0x00,
151 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00,
152 0x18, 0x80, 0x00, 0x00, 0x54, 0x45, 0x53, 0x54,
153 0x2d, 0x57, 0x52, 0x49, 0x54, 0x45
156 senf::IPv6Packet ip (senf::IPv6Packet::create());
157 ip->source() = senf::INet6Address::Loopback;
158 ip->destination() = senf::INet6Address::Loopback;
161 senf::TCPPacket tcp (senf::TCPPacket::createAfter(ip));
162 tcp->source() = 23456;
163 tcp->destination() = 12345;
165 senf::DataPacket::createAfter(tcp, std::string("TEST-WRITE"));
167 // validates, since the checksum is 0 and thus ignored !
168 BOOST_CHECK( tcp->validateChecksum() );
172 BOOST_CHECK_EQUAL_COLLECTIONS(
173 data, data+sizeof(data), ip.data().begin(), ip.data().end() );
174 BOOST_CHECK( tcp->validateChecksum() );
177 ///////////////////////////////cc.e////////////////////////////////////////
184 // c-file-style: "senf"
185 // indent-tabs-mode: nil
186 // ispell-local-dictionary: "american"
187 // compile-command: "scons -u test"
188 // comment-column: 40