e9a040e7ae31e69be2ee099b3228580d61d6c8b1
[senf.git] / senf / Packets / 80221Bundle / MIHPacket.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 //     Thorsten Horstmann <tho@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 MIHPacket unit tests */
25
26 //#include "MIHPacket.test.hh"
27 //#include "MIHPacket.test.ih"
28
29 // Custom includes
30 #include "MIHPacket.hh"
31
32 #include "../../Utils/auto_unit_test.hh"
33 #include <boost/test/test_tools.hpp>
34
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37 using namespace senf;
38
39 BOOST_AUTO_UNIT_TEST(MIHPacket_MIHFId)
40 {
41     MIHFId id ( MACAddress::from_string("01:02:03:04:05:06"));
42     BOOST_CHECK_EQUAL( id.type(), MIHFId::MACAddress);
43     BOOST_CHECK_EQUAL( id, MIHFId( MACAddress::from_string("01:02:03:04:05:06")));
44     BOOST_CHECK( id != MIHFId( MACAddress::from_string("01:02:03:04:05:07")));
45     BOOST_CHECK( id != MIHFId( INet4Address::from_string("128.129.130.131")));
46     BOOST_CHECK( id < MIHFId( MACAddress::from_string("01:02:03:04:05:07")));
47     BOOST_CHECK( id < MIHFId( INet4Address::from_string("128.129.130.131")));
48 }
49
50 BOOST_AUTO_UNIT_TEST(MIHPacket_msgId)
51 {
52     MIHPacket mihPacket (MIHPacket::create());
53     mihPacket->sid() = 4;
54     mihPacket->opcode() = 3;
55     mihPacket->aid() = 42;
56     BOOST_CHECK_EQUAL( mihPacket->messageId(), 0x4c2a );        
57 }
58
59 BOOST_AUTO_UNIT_TEST(MIHPacket_create_string)
60 {
61     MIHPacket mihPacket (MIHPacket::create());
62     // set some fields
63     mihPacket->fragmentNr() = 42;
64     mihPacket->transactionId() = 21;
65     mihPacket->src_mihfId().setString( "senf@berlios.de");
66     mihPacket->dst_mihfId().setString( "test");
67     mihPacket.finalizeThis();
68
69     unsigned char data[] = {
70             // MIH header
71             0x10, 0x54, 0x00, 0x00, 0x00, 0x15, 0x00, 0x17,
72             // source MIHF_ID TLV:
73             0x01, 0x0f, // type, length
74             0x73, 0x65, 0x6e, 0x66, 0x40, 0x62, 0x65, 0x72, 0x6c,
75             0x69, 0x6f, 0x73, 0x2e, 0x64, 0x65,  // value
76             // destination MIHF_ID TLV:
77             0x02, 0x04, 0x74, 0x65, 0x73, 0x74
78     };
79     BOOST_CHECK(equal( mihPacket.data().begin(), mihPacket.data().end(), data ));
80     BOOST_CHECK_EQUAL( mihPacket->src_mihfId().asString(), "senf@berlios.de");
81     BOOST_CHECK_EQUAL( mihPacket->dst_mihfId().asString(), "test");
82
83     // the maximum length of a MIHF_ID is 253 octets
84     BOOST_CHECK_THROW( mihPacket->dst_mihfId().setString( std::string(254, 'x')), std::length_error);
85
86     // now expand a MIHF_ID
87     mihPacket->dst_mihfId().maxLengthValue(253);
88     mihPacket->dst_mihfId().setString( std::string(200, 'x'));
89     mihPacket.finalizeThis();
90
91     BOOST_CHECK_EQUAL( mihPacket.size(), unsigned(8 + 17 + 203));
92     BOOST_CHECK_EQUAL( mihPacket->payloadLength(), 17 + 203);
93     BOOST_CHECK_EQUAL( mihPacket->dst_mihfId().length(), 200u);
94     BOOST_CHECK_EQUAL( senf::bytes(mihPacket->dst_mihfId()), 203u);
95
96     std::ostringstream oss (std::ostringstream::out);
97     SENF_CHECK_NO_THROW( mihPacket.dump( oss));
98 }
99
100
101 BOOST_AUTO_UNIT_TEST(MIHPacket_create_mac)
102 {
103     MIHPacket mihPacket (MIHPacket::create());
104     // set some fields
105     mihPacket->fragmentNr() = 42;
106     mihPacket->transactionId() = 21;
107     mihPacket->src_mihfId().setMACAddress( MACAddress::from_string("01:02:03:04:05:06"));
108     mihPacket->dst_mihfId().setMACAddress( MACAddress::from_string("07:08:09:0a:0b:0c"));
109     mihPacket.finalizeThis();
110
111     unsigned char data[] = {
112             // MIH header
113             0x10, 0x54, 0x00, 0x00, 0x00, 0x15, 0x00, 0x1c,
114             // source MIHF_ID TLV:
115             0x01, 0x0c, // type, length
116             0x5c, 0x01, 0x5c, 0x02, 0x5c, 0x03, 0x5c, 0x04, 0x5c, 0x05, 0x5c, 0x06, // value (nai-encoded)
117             // destination MIHF_ID TLV:
118             0x02, 0x0c,  // type, length
119             0x5c, 0x07, 0x5c, 0x08, 0x5c, 0x09, 0x5c, 0x0a, 0x5c, 0x0b, 0x5c, 0x0c  // value (nai-encoded)
120     };
121     BOOST_CHECK(equal( mihPacket.data().begin(), mihPacket.data().end(), data ));
122     BOOST_CHECK_EQUAL(
123             mihPacket->src_mihfId().asMACAddress(),
124             MACAddress::from_string("01:02:03:04:05:06"));
125     BOOST_CHECK_EQUAL(
126             mihPacket->dst_mihfId().asMACAddress(),
127             MACAddress::from_string("07:08:09:0a:0b:0c"));
128 }
129
130
131 BOOST_AUTO_UNIT_TEST(MIHPacket_create_inet4)
132 {
133     MIHPacket mihPacket (MIHPacket::create());
134     // set some fields
135     mihPacket->fragmentNr() = 42;
136     mihPacket->transactionId() = 21;
137     mihPacket->src_mihfId().setINet4Address( INet4Address::from_string("128.129.130.131"));
138     mihPacket->dst_mihfId().setINet4Address( INet4Address::from_string("132.133.134.135"));
139     mihPacket.finalizeThis();
140
141     unsigned char data[] = {
142             // MIH header
143             0x10, 0x54, 0x00, 0x00, 0x00, 0x15, 0x00, 0x14,
144             // source MIHF_ID TLV:
145             0x01, 0x08, // type, length
146             0x5c, 0x80, 0x5c, 0x81, 0x5c, 0x82, 0x5c, 0x83, // value (nai-encoded)
147             // destination MIHF_ID TLV:
148             0x02, 0x08, // type, length
149             0x5c, 0x84, 0x5c, 0x85, 0x5c, 0x86, 0x5c, 0x87  // value (nai-encoded)
150     };
151     BOOST_CHECK(equal( mihPacket.data().begin(), mihPacket.data().end(), data ));
152     BOOST_CHECK_EQUAL(
153             mihPacket->src_mihfId().asINet4Address(),
154             INet4Address::from_string("128.129.130.131"));
155     BOOST_CHECK_EQUAL(
156             mihPacket->dst_mihfId().asINet4Address(),
157             INet4Address::from_string("132.133.134.135"));
158 }
159
160
161 BOOST_AUTO_UNIT_TEST(MIHPacket_create_inet6)
162 {
163     MIHPacket mihPacket (MIHPacket::create());
164     // set some fields
165     mihPacket->fragmentNr() = 42;
166     mihPacket->transactionId() = 21;
167     mihPacket->src_mihfId().setINet6Address( INet6Address::from_string("::ffff:1.2.3.4"));
168     mihPacket->dst_mihfId().setINet6Address( INet6Address::from_string("::ffff:5.6.7.8"));
169     mihPacket.finalizeThis();
170
171     unsigned char data[] = {
172             // MIH header
173             0x10, 0x54, 0x00, 0x00, 0x00, 0x15, 0x00, 0x44,
174             // source MIHF_ID TLV:
175             0x01, 0x20, // type, length
176             // value (nai-encoded):
177             0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00,
178             0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00,
179             0x5c, 0x00, 0x5c, 0x00, 0x5c, 0xff, 0x5c, 0xff,
180             0x5c, 0x01, 0x5c, 0x02, 0x5c, 0x03, 0x5c, 0x04,
181             // destination MIHF_ID TLV:
182             0x02, 0x20, // type, length
183             // value (nai-encoded):
184             0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00,
185             0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00,
186             0x5c, 0x00, 0x5c, 0x00, 0x5c, 0xff, 0x5c, 0xff,
187             0x5c, 0x05, 0x5c, 0x06, 0x5c, 0x07, 0x5c, 0x08
188     };
189     BOOST_CHECK(equal( mihPacket.data().begin(), mihPacket.data().end(), data ));
190     BOOST_CHECK_EQUAL(
191             mihPacket->src_mihfId().asINet6Address(),
192             INet6Address::from_string("::ffff:1.2.3.4"));
193     BOOST_CHECK_EQUAL(
194             mihPacket->dst_mihfId().asINet6Address(),
195             INet6Address::from_string("::ffff:5.6.7.8") );
196 }
197
198
199 BOOST_AUTO_UNIT_TEST(MIHPayload_parse)
200 {
201     unsigned char data[] = {
202             // MIH header
203             0x10, 0x54, 0x00, 0x00, 0x00, 0x15,
204             // variable payload length:
205             0x00, 0x2a,
206             // source MIHF_ID TLV:
207             0x01, 0x0f, // type, length
208             0x73, 0x65, 0x6e, 0x66, 0x40, 0x62, 0x65, 0x72, 0x6c,
209             0x69, 0x6f, 0x73, 0x2e, 0x64, 0x65,  // value ("senf@berlios.de")
210             // destination MIHF_ID TLV:
211             0x02, 0x04, 0x74, 0x65, 0x73, 0x74,  // type, length, value ("test")
212             // MIH Payload (two test tlvs)
213             // first test tlv
214             0x42, // type
215             0x0a, // first bit not set, length=10
216             0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, // value
217             // second test tlv
218             0x43, // type
219             0x05, // first bit not set, length=5
220             0x1a, 0x2b, 0x3c, 0x4d, 0x5e // value
221     };
222
223     MIHPacket mihPacket (MIHPacket::create(data));
224     BOOST_CHECK_EQUAL( mihPacket->payloadLength(), 42u);
225
226     BOOST_REQUIRE( mihPacket.next().is<MIHPayloadPacket>() );
227     MIHPayloadPacket mihPayload (mihPacket.next().as<MIHPayloadPacket>());
228
229     BOOST_CHECK_EQUAL( mihPayload->tlv_list().size(), 2u);
230     MIHPayloadPacketParser::tlv_list_t::container tlv_list_container (mihPayload->tlv_list());
231
232     GenericTLVPacket::Parser tlv1 = *tlv_list_container.begin();
233     BOOST_CHECK_EQUAL( tlv1.type(), 0x42);
234     BOOST_CHECK_EQUAL( tlv1.length(), 0x0au);
235     BOOST_CHECK_EQUAL( tlv1.value().size(), 0x0a);
236
237     GenericTLVPacket::Parser tlv2 = *boost::next(tlv_list_container.begin());
238     BOOST_CHECK_EQUAL( tlv2.type(), 0x43);
239     BOOST_CHECK_EQUAL( tlv2.length(), 0x05u);
240     BOOST_CHECK_EQUAL( tlv2.value().size(), 0x05);
241 }
242
243
244 BOOST_AUTO_UNIT_TEST(MIHPayload_create)
245 {
246     MIHPacket mihPacket (MIHPacket::create());
247     mihPacket->fragmentNr() = 42;
248     mihPacket->transactionId() = 21;
249     mihPacket->src_mihfId().setString( "senf@berlios.de");
250     mihPacket->dst_mihfId().setString( "test");
251
252     MIHPayloadPacket mihPayload (MIHPayloadPacket::createAfter(mihPacket));
253
254     unsigned char tlv1_value[] = {
255             0x1a, 0x2b, 0x3c, 0x4d, 0x5e };
256     GenericTLVPacket tlv2 = (GenericTLVPacket::create());
257     tlv2->type() = 0x43;
258     tlv2->value( tlv1_value);
259     tlv2.finalizeThis();
260     mihPayload->tlv_list().push_front( tlv2);
261
262     unsigned char tlv2_value[] = {
263            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
264     GenericTLVPacket tlv1 (GenericTLVPacket::create());
265     tlv1->type() = 0x42;
266     tlv1->value( tlv2_value);
267     tlv1.finalizeThis();
268     mihPayload->tlv_list().push_front( tlv1);
269
270     mihPacket.finalizeAll();
271
272     unsigned char data[] = {
273             // MIH header
274             0x10, 0x54, 0x00, 0x00, 0x00, 0x15,
275             // variable payload length:
276             0x00, 0x2a,
277             // source MIHF_ID TLV:
278             0x01, 0x0f, // type, length
279             0x73, 0x65, 0x6e, 0x66, 0x40, 0x62, 0x65, 0x72, 0x6c,
280             0x69, 0x6f, 0x73, 0x2e, 0x64, 0x65,  // value ("senf@berlios.de")
281             // destination MIHF_ID TLV:
282             0x02, 0x04, 0x74, 0x65, 0x73, 0x74,  // type, length, value ("test")
283             // MIH Payload (two test tlvs)
284             // first test tlv
285             0x42, // type
286             0x0a, // first bit not set, length=10
287             0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, // value
288             // second test tlv
289             0x43, // type
290             0x05, // first bit not set, length=5
291             0x1a, 0x2b, 0x3c, 0x4d, 0x5e // value
292     };
293
294     BOOST_CHECK(equal( mihPacket.data().begin(), mihPacket.data().end(), data ));
295 }
296
297
298 ///////////////////////////////cc.e////////////////////////////////////////
299 #undef prefix_
300
301 \f
302 // Local Variables:
303 // mode: c++
304 // fill-column: 100
305 // c-file-style: "senf"
306 // indent-tabs-mode: nil
307 // ispell-local-dictionary: "american"
308 // compile-command: "scons -u test"
309 // comment-column: 40
310 // End: