added FormFeed character for emacs to end of files
[senf.git] / senf / Packets / DefaultBundle / ICMPv6Packet.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Philipp Batroff <pug@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 /** \file
24     \brief ICMPv6Packet unit tests */
25
26 // Custom includes
27 #include <sstream>
28 #include "ICMPv6Packet.hh"
29 #include "ICMPv6TypePacket.hh"
30
31 #include <senf/Utils/auto_unit_test.hh>
32 #include <boost/test/test_tools.hpp>
33
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 BOOST_AUTO_UNIT_TEST(ICMPv6Packet_packet)
37 {
38     unsigned char dataListenerReport[] = {
39             0x8f, 0x00, 0x8d, 0x54, 0x00, 0x00, 0x00, 0x01,
40             0x04, 0x00, 0x00, 0x00, 0xff, 0x15, 0x00, 0x00,
41             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42             0x00, 0x00, 0x00, 0x16
43     };
44
45     senf::ICMPv6Packet pListenerReport ( senf::ICMPv6Packet::create(dataListenerReport) );
46
47     BOOST_CHECK_EQUAL( pListenerReport->type(),     0x8f   );
48     BOOST_CHECK_EQUAL( pListenerReport->code(),     0x00   );
49     BOOST_CHECK_EQUAL( pListenerReport->checksum(), 0x8d54 );
50     BOOST_CHECK( pListenerReport.next() );
51     BOOST_CHECK( pListenerReport.next().is<senf::MLDv2ListenerReport>() );
52     BOOST_CHECK_EQUAL( pListenerReport.next().size(), 24u );
53
54     std::ostringstream oss (std::ostringstream::out);
55     SENF_CHECK_NO_THROW( pListenerReport.dump( oss));
56
57     unsigned char dataListenerQuery[] = {
58         0x82, 0x00, 0xf7, 0xd6, 0x27, 0x10, 0x00, 0x00, 
59         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61         0x02, 0x7d, 0x00, 0x00
62     };
63     senf::ICMPv6Packet pListenerQuery ( senf::ICMPv6Packet::create(dataListenerQuery) );
64     BOOST_CHECK_EQUAL( pListenerQuery->type(),     0x82   );
65     BOOST_CHECK_EQUAL( pListenerQuery->code(),     0x00   );
66     BOOST_CHECK_EQUAL( pListenerQuery->checksum(), 0xf7d6 );
67     BOOST_CHECK( pListenerQuery.next() );
68     BOOST_CHECK( pListenerQuery.next().is<senf::MLDv2ListenerQuery>() );
69     BOOST_CHECK_EQUAL( pListenerQuery.next().size(), 24u );
70
71     SENF_CHECK_NO_THROW( pListenerQuery.dump( oss));
72     
73     unsigned char dataEchoRequest[] = {
74         0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x07
75     };
76     senf::ICMPv6Packet pEchoRequest ( senf::ICMPv6Packet::create(dataEchoRequest) );
77     BOOST_CHECK_EQUAL( pEchoRequest->type(),     0x80   );
78     BOOST_CHECK_EQUAL( pEchoRequest->code(),     0x00   );
79     BOOST_CHECK_EQUAL( pEchoRequest->checksum(), 0x0000 );
80     BOOST_CHECK( pEchoRequest.next() );
81     BOOST_CHECK( pEchoRequest.next().is<senf::ICMPv6EchoRequest>() );
82     BOOST_CHECK_EQUAL( pEchoRequest.next().size(), 4u );
83
84     SENF_CHECK_NO_THROW( pEchoRequest.dump( oss));
85     
86     unsigned char dataEchoReply[] = {
87         0x81, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x4d
88     };
89     
90     senf::ICMPv6Packet pEchoReply ( senf::ICMPv6Packet::create(dataEchoReply) );
91     BOOST_CHECK_EQUAL( pEchoReply->type(),     0x81   );
92     BOOST_CHECK_EQUAL( pEchoReply->code(),     0x00   );
93     BOOST_CHECK_EQUAL( pEchoReply->checksum(), 0x0000 );
94     BOOST_CHECK( pEchoReply.next() );
95     BOOST_CHECK( pEchoReply.next().is<senf::ICMPv6EchoReply>() );
96     BOOST_CHECK_EQUAL( pEchoReply.next().size(), 4u );
97
98     SENF_CHECK_NO_THROW( pEchoReply.dump( oss));
99     
100     
101     unsigned char dataErrDestUnreachable[] = {
102         0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 
103     };
104     
105     senf::ICMPv6Packet pErrDestUnreachable ( senf::ICMPv6Packet::create(dataErrDestUnreachable) );
106     BOOST_CHECK_EQUAL( pErrDestUnreachable->type(),     0x01   );
107     BOOST_CHECK_EQUAL( pErrDestUnreachable->code(),     0x00   );
108     BOOST_CHECK_EQUAL( pErrDestUnreachable->checksum(), 0x0000 );
109     BOOST_CHECK( pErrDestUnreachable.next() );
110     BOOST_CHECK( pErrDestUnreachable.next().is<senf::ICMPv6ErrDestUnreachable>() );
111     BOOST_CHECK_EQUAL( pErrDestUnreachable.next().size(), 4u );
112
113     SENF_CHECK_NO_THROW( pErrDestUnreachable.dump( oss));
114     
115     
116     unsigned char dataErrTooBig[] = {
117         0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0xd8 
118     };
119     
120     senf::ICMPv6Packet pErrTooBig ( senf::ICMPv6Packet::create(dataErrTooBig) );
121     BOOST_CHECK_EQUAL( pErrTooBig->type(),     0x02   );
122     BOOST_CHECK_EQUAL( pErrTooBig->code(),     0x00   );
123     BOOST_CHECK_EQUAL( pErrTooBig->checksum(), 0x0000 );
124     BOOST_CHECK( pErrTooBig.next() );
125     BOOST_CHECK( pErrTooBig.next().is<senf::ICMPv6ErrTooBig>() );
126     BOOST_CHECK_EQUAL( pErrTooBig.next().size(), 4u );
127
128     SENF_CHECK_NO_THROW( pErrTooBig.dump( oss));
129     
130     
131     unsigned char dataErrTimeExceeded[] = {
132         0x03, 0x63, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
133     };
134     
135     senf::ICMPv6Packet pErrTimeExceeded ( senf::ICMPv6Packet::create(dataErrTimeExceeded) );
136     BOOST_CHECK_EQUAL( pErrTimeExceeded->type(),     0x03   );
137     BOOST_CHECK_EQUAL( pErrTimeExceeded->code(),     0x63   );
138     BOOST_CHECK_EQUAL( pErrTimeExceeded->checksum(), 0x0000 );
139     BOOST_CHECK( pErrTimeExceeded.next() );
140     BOOST_CHECK( pErrTimeExceeded.next().is<senf::ICMPv6ErrTimeExceeded>() );
141     BOOST_CHECK_EQUAL( pErrTimeExceeded.next().size(), 4u );
142
143     SENF_CHECK_NO_THROW( pErrTimeExceeded.dump( oss));
144     
145     
146     unsigned char dataErrParamProblem[] = {
147         0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
148     };
149     
150     senf::ICMPv6Packet pErrParamProblem ( senf::ICMPv6Packet::create(dataErrParamProblem) );
151     BOOST_CHECK_EQUAL( pErrParamProblem->type(),     0x04   );
152     BOOST_CHECK_EQUAL( pErrParamProblem->code(),     0x01   );
153     BOOST_CHECK_EQUAL( pErrParamProblem->checksum(), 0x0000 );
154     BOOST_CHECK( pErrParamProblem.next() );
155     BOOST_CHECK( pErrParamProblem.next().is<senf::ICMPv6ErrParamProblem>() );
156     BOOST_CHECK_EQUAL( pErrParamProblem.next().size(), 4u );
157
158     SENF_CHECK_NO_THROW( pErrParamProblem.dump( oss));
159     
160 }
161
162 BOOST_AUTO_UNIT_TEST(ICMPv6Packet_create)
163 {
164     std::ostringstream oss (std::ostringstream::out);
165     
166     unsigned char ping[] = { 0x60, 0x00, 0x00, 0x00, 0x00, 0x40, 0x3a, 0x40,
167                              0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
168                              0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
169                              0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
170                              0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
171                              0x80, 0x00, 0xda, 0xe0, 0x9f, 0x7e, 0x00, 0x09,
172                              
173                              0xb7, 0x3c, 0xbb, 0x4a, 0x9d, 0x90, 0x0a, 0x00, //payload
174                              0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
175                              0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
176                              0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
177                              0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
178                              0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
179                              0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
180     };
181
182     senf::IPv6Packet ip (senf::IPv6Packet::create());
183     ip->hopLimit() = 64;
184     ip->source() = senf::INet6Address::Loopback;
185     ip->destination() = senf::INet6Address::Loopback;
186
187     senf::ICMPv6Packet icmp (senf::ICMPv6Packet::createAfter(ip));
188     icmp->code() = 0;
189     
190     senf::ICMPv6EchoRequest ereq (senf::ICMPv6EchoRequest::createAfter(icmp));
191     ereq->identifier() = 0x9f7e;
192     ereq->seqNr() = 9;
193
194     senf::DataPacket data (
195         senf::DataPacket::createAfter(ereq, std::make_pair(ping+48, ping+sizeof(ping))));
196
197     ip.finalizeAll();
198     
199     SENF_CHECK_NO_THROW (ip.dump( oss ));
200
201     std::string dump (
202         "Internet protocol Version 6:\n"
203         "  version                 : 6\n"
204         "  traffic class           : 0x00\n"
205         "  flow label              : 0x00000\n"
206         "  payload length          : 64\n"
207         "  next header             : 58\n"
208         "  hop limit               : 64\n"
209         "  source                  : ::1\n"
210         "  destination             : ::1\n"
211         "ICMPv6 protocol:\n"
212         "  type                    : 128\n"
213         "  code                    : 0\n"
214         "  checksum                : 0xdae0\n"
215         "ICMPv6 Echo Request:\n"
216         "  Identifier              : 40830\n"
217         "  SequenceNumber          : 9\n"
218         "Payload data (56 bytes)\n"
219         );
220
221     {
222         std::stringstream ss;
223         ip.dump(ss);
224         BOOST_CHECK_EQUAL( ss.str(), dump );
225     }
226
227     SENF_CHECK_EQUAL_COLLECTIONS( ip.data().begin(), ip.data().end(),
228                                   ping, ping+sizeof(ping) );
229
230     senf::IPv6Packet orig (senf::IPv6Packet::create(ping));
231
232     {
233         std::stringstream ss;
234         orig.dump(ss);
235         BOOST_CHECK_EQUAL( ss.str(), dump );
236     }
237 }
238
239 ///////////////////////////////cc.e////////////////////////////////////////
240
241 \f
242 // Local Variables:
243 // mode: c++
244 // fill-column: 100
245 // c-file-style: "senf"
246 // indent-tabs-mode: nil
247 // ispell-local-dictionary: "american"
248 // compile-command: "scons -u test"
249 // comment-column: 40
250 // End: