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