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