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