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