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