a85c35b0a45bdbc725034db71d03f5fed05aab64
[senf.git] / senf / Packets / DefaultBundle / TCPPacket.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
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>
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 /** \file
24     \brief TCPPacket unit tests */
25
26 //#include "TCPPacket.test.hh"
27 //#include "TCPPacket.test.ih"
28
29 // Custom includes
30 #include "TCPPacket.hh"
31 #include "IPv4Packet.hh"
32 #include "IPv6Packet.hh"
33
34 #include <senf/Utils/auto_unit_test.hh>
35 #include <boost/test/test_tools.hpp>
36
37 #define prefix_
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39
40 SENF_AUTO_UNIT_TEST(tcpPacket_parse)
41 {
42     unsigned char data[] = {
43             0x80, 0xA6, 0x00, 0x50, 0x2E, 0x93, 0x31, 0xB8, 0x66, 0xF9, 0xB7,
44             0xA1, 0x80, 0x18, 0x00, 0x2E, 0xCF, 0x65, 0x00, 0x00
45     };
46
47     senf::TCPPacket p (senf::TCPPacket::create(data));
48
49     BOOST_CHECK_EQUAL( p->source(),               0x80A6u       );
50     BOOST_CHECK_EQUAL( p->destination(),          0x0050u       );
51     BOOST_CHECK_EQUAL( p->sequencenumber(),       0x2E9331B8u   );
52     BOOST_CHECK_EQUAL( p->acknowledgmentnumber(), 0x66F9B7A1u   );
53     BOOST_CHECK_EQUAL( p->dataoffset(),           0x8u          );
54     BOOST_CHECK_EQUAL( p->urgf(),                 0u            );
55     BOOST_CHECK_EQUAL( p->ackf(),                 1u            );
56     BOOST_CHECK_EQUAL( p->pshf(),                 1u            );
57     BOOST_CHECK_EQUAL( p->rstf(),                 0u            );
58     BOOST_CHECK_EQUAL( p->synf(),                 0u            );
59     BOOST_CHECK_EQUAL( p->finf(),                 0u            );
60     BOOST_CHECK_EQUAL( p->window(),               0x002Eu       );
61     BOOST_CHECK_EQUAL( p->checksum(),             0xCF65u       );
62     BOOST_CHECK_EQUAL( p->urgentpointer(),        0x0000u       );
63
64     std::ostringstream oss (std::ostringstream::out);
65     SENF_CHECK_NO_THROW( p.dump( oss));
66 }
67
68 SENF_AUTO_UNIT_TEST(tcpPacket_in_ipv4_parse)
69 {
70     unsigned char data[] = {
71             //20 Byte IPv4-Header
72             0x45, 0x00, 0x00, 0xb2, 0x0f, 0x4d, 0x40, 0x00, 0x0f9, 0x06, 0xa0,
73             0xa1, 0xc1, 0x63, 0x90, 0x55, 0x0a, 0x14, 0x75, 0x8a,
74
75             //20 Byte TCP-Header + 12 Byte Options
76             0x00, 0x50, 0xc3, 0xbf, 0xe2, 0xf7, 0x49, 0xf3, 0x3e, 0x4f, 0x8a,
77             0xbe, 0x80, 0x18, 0x13, 0x24, 0x2e, 0x33, 0x00, 0x00, 0x01, 0x01,
78             0x08, 0x0a, 0x0f, 0xf1, 0xa7, 0x6b, 0x00, 0x59, 0xf4, 0x75,
79
80             //TCP-Payload
81             0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x20, 0x33, 0x30,
82             0x34, 0x20, 0x4e, 0x6f, 0x74, 0x20, 0x4d, 0x6f, 0x64, 0x69, 0x66,
83             0x69, 0x65, 0x64, 0x0d, 0x0a, 0x44, 0x61, 0x74, 0x65, 0x3a, 0x20,
84             0x54, 0x68, 0x75, 0x2c, 0x20, 0x32, 0x33, 0x20, 0x4a, 0x75, 0x6c,
85             0x20, 0x32, 0x30, 0x30, 0x39, 0x20, 0x31, 0x35, 0x3a, 0x31, 0x35,
86             0x3a, 0x31, 0x39, 0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a, 0x53, 0x65,
87             0x72, 0x76, 0x65, 0x72, 0x3a, 0x20, 0x41, 0x70, 0x61, 0x63, 0x68,
88             0x65, 0x2f, 0x32, 0x2e, 0x32, 0x2e, 0x31, 0x31, 0x0d, 0x0a, 0x43,
89             0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20,
90             0x63, 0x6c, 0x6f, 0x73, 0x65, 0x0d, 0x0a, 0x56, 0x61, 0x72, 0x79,
91             0x3a, 0x20, 0x55, 0x73, 0x65, 0x72, 0x2d, 0x41, 0x67, 0x65, 0x6e,
92             0x74, 0x0d, 0x0a, 0x0d, 0x0a
93     };
94
95     senf::IPv4Packet ip (senf::IPv4Packet::create(data));
96     senf::TCPPacket tcp (ip.next<senf::TCPPacket>());
97
98     BOOST_CHECK( tcp->validateChecksum() );
99 }
100
101
102 SENF_AUTO_UNIT_TEST(tcpPacket_in_ipv6_parse)
103 {
104     unsigned char data[] = {
105             // IPv6-Header
106             0x60, 0x00, 0x00, 0x00, 0x00, 0xa5, 0x06, 0x39, 0x20, 0x01, 0x48,
107             0x60, 0xa0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
108             0x00, 0x68, 0x20, 0x01, 0x06, 0x38, 0x04, 0x08, 0x02, 0x01, 0xef,
109             0xdc, 0x71, 0x52, 0x17, 0x2c, 0x44, 0x3e,
110
111             // 20 Byte TCP-Header without Options
112             0x00, 0x50, 0xac, 0x3e, 0x82, 0x99, 0x60, 0xa6, 0xa4, 0xfa, 0x32,
113             0x83, 0x50, 0x18, 0x19, 0x20, 0xd7, 0xcd, 0x00, 0x00,
114
115             // TCP-Payload
116             0x1a, 0x01, 0x52, 0x0e, 0x26, 0x6f, 0xac, 0xd9, 0x78, 0x0c, 0x3c,
117             0x69, 0x3c, 0xa6, 0x49, 0xc6, 0x85, 0xc2, 0xa9, 0x72, 0x75, 0x9a,
118             0xef, 0x63, 0x46, 0xa3, 0xd4, 0x11, 0xda, 0x04, 0x95, 0x55,0x7b,
119             0xb5, 0x6c, 0xcb, 0x52, 0xfb, 0xa0, 0x7a, 0x03, 0xd5, 0x98, 0x75,
120             0x8b, 0x53, 0xfc, 0x23, 0x21, 0x01, 0xc5, 0x06, 0x66, 0x6c, 0x0d,
121             0x06, 0x1d, 0x69, 0x2f, 0xec, 0xb7, 0x7d, 0xe3, 0xd6, 0x71, 0x23,
122             0xaa, 0x58, 0x3b, 0x80, 0x58, 0x5b, 0x10, 0xa1, 0xa8, 0x8f, 0x59,
123             0x3d, 0x3d, 0x80, 0xd6, 0x2e, 0x0d, 0x21, 0x33, 0x2b, 0x47, 0x5b,
124             0xb2, 0xd0, 0xde, 0x19, 0xaf, 0x03, 0x2a, 0x33,0x86, 0x57, 0x8e,
125             0x07, 0xf9, 0x6e, 0xde, 0x68, 0xe1, 0xd8, 0x63, 0x92, 0x18, 0x1a,
126             0xa9, 0xc0, 0xe5, 0x94, 0xba, 0x1d, 0x97, 0xed, 0x7e, 0x2b, 0xa8,
127             0xda, 0x4a, 0x96, 0x0c, 0xe5, 0x78, 0xb9, 0xec, 0x80, 0xf8, 0x5c,
128             0x94, 0xf9, 0xd3, 0x49, 0x79, 0x4a, 0x4a, 0x98, 0xb1, 0x13, 0x73,
129             0x50, 0x6c
130     };
131
132     senf::IPv6Packet ip (senf::IPv6Packet::create(data));
133     senf::TCPPacket tcp (ip.next<senf::TCPPacket>());
134
135     BOOST_CHECK( tcp->validateChecksum() );
136 }
137
138 SENF_AUTO_UNIT_TEST(tcpPacket_in_ipv6_create)
139 {
140     unsigned char data[] = {
141             // IPv6-Header
142             0x60, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x06, 0x40,
143             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
144             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
145             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
146             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
147
148             // TCP-Header + TCP-Payload
149             0x5b, 0xa0, 0x30, 0x39, 0x00, 0x00, 0x00, 0x00,
150             0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00,
151             0x18, 0x80, 0x00, 0x00, 0x54, 0x45, 0x53, 0x54,
152             0x2d, 0x57, 0x52, 0x49, 0x54, 0x45
153     };
154
155     senf::IPv6Packet ip (senf::IPv6Packet::create());
156     ip->source() = senf::INet6Address::Loopback;
157     ip->destination() = senf::INet6Address::Loopback;
158     ip->hopLimit() = 64;
159
160     senf::TCPPacket tcp (senf::TCPPacket::createAfter(ip));
161     tcp->source() = 23456;
162     tcp->destination() = 12345;
163
164     senf::DataPacket::createAfter(tcp, std::string("TEST-WRITE"));
165
166     // validates, since the checksum is 0 and thus ignored !
167     BOOST_CHECK( tcp->validateChecksum() );
168
169     ip.finalizeAll();
170
171     BOOST_CHECK_EQUAL_COLLECTIONS(
172             data, data+sizeof(data), ip.data().begin(), ip.data().end() );
173     BOOST_CHECK( tcp->validateChecksum() );
174 }
175
176 //-/////////////////////////////////////////////////////////////////////////////////////////////////
177 #undef prefix_
178
179 \f
180 // Local Variables:
181 // mode: c++
182 // fill-column: 100
183 // c-file-style: "senf"
184 // indent-tabs-mode: nil
185 // ispell-local-dictionary: "american"
186 // compile-command: "scons -u test"
187 // comment-column: 40
188 // End: