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