4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Thorsten Horstmann <tho@berlios.de>
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.
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.
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.
24 \brief MIHPacket unit tests */
26 //#include "MIHPacket.test.hh"
27 //#include "MIHPacket.test.ih"
30 #include "MIHPacket.hh"
31 #include <senf/Packets/DefaultBundle/EthernetPacket.hh>
33 #include <senf/Utils/auto_unit_test.hh>
34 #include <boost/test/test_tools.hpp>
37 ///////////////////////////////cc.p////////////////////////////////////////
40 BOOST_AUTO_UNIT_TEST(MIHPacket_MIHFId)
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")));
51 BOOST_AUTO_UNIT_TEST(MIHPacket_msgId)
53 MIHPacket mihPacket (MIHPacket::create());
55 mihPacket->opcode() = 3;
56 mihPacket->aid() = 42;
57 BOOST_CHECK_EQUAL( mihPacket->messageId(), 0x4c2a );
60 BOOST_AUTO_UNIT_TEST(MIHPacket_create_eth)
62 EthernetPacket eth (EthernetPacket::create());
63 MIHPacket mihPacket (MIHPacket::createAfter(eth));
65 BOOST_CHECK_EQUAL( eth->type_length(), 0x86dd);
66 std::ostringstream oss (std::ostringstream::out);
67 SENF_CHECK_NO_THROW( eth.dump( oss));
70 BOOST_AUTO_UNIT_TEST(MIHPacket_create_string)
72 MIHPacket mihPacket (MIHPacket::create());
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();
80 unsigned char data[] = {
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
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");
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);
97 // now expand a MIHF_ID
98 mihPacket->dst_mihfId().maxLengthValue(253);
99 mihPacket->dst_mihfId().setString( std::string(200, 'x'));
100 mihPacket.finalizeThis();
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);
107 std::ostringstream oss (std::ostringstream::out);
108 SENF_CHECK_NO_THROW( mihPacket.dump( oss));
112 BOOST_AUTO_UNIT_TEST(MIHPacket_create_mac)
114 MIHPacket mihPacket (MIHPacket::create());
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();
122 unsigned char data[] = {
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)
132 BOOST_CHECK(equal( mihPacket.data().begin(), mihPacket.data().end(), data ));
134 mihPacket->src_mihfId().asMACAddress(),
135 MACAddress::from_string("01:02:03:04:05:06"));
137 mihPacket->dst_mihfId().asMACAddress(),
138 MACAddress::from_string("07:08:09:0a:0b:0c"));
142 BOOST_AUTO_UNIT_TEST(MIHPacket_create_inet4)
144 MIHPacket mihPacket (MIHPacket::create());
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();
152 unsigned char data[] = {
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)
162 BOOST_CHECK(equal( mihPacket.data().begin(), mihPacket.data().end(), data ));
164 mihPacket->src_mihfId().asINet4Address(),
165 INet4Address::from_string("128.129.130.131"));
167 mihPacket->dst_mihfId().asINet4Address(),
168 INet4Address::from_string("132.133.134.135"));
172 BOOST_AUTO_UNIT_TEST(MIHPacket_create_inet6)
174 MIHPacket mihPacket (MIHPacket::create());
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();
182 unsigned char data[] = {
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
200 BOOST_CHECK(equal( mihPacket.data().begin(), mihPacket.data().end(), data ));
202 mihPacket->src_mihfId().asINet6Address(),
203 INet6Address::from_string("::ffff:1.2.3.4"));
205 mihPacket->dst_mihfId().asINet6Address(),
206 INet6Address::from_string("::ffff:5.6.7.8") );
210 BOOST_AUTO_UNIT_TEST(MIHPayload_parse)
212 unsigned char data[] = {
214 0x10, 0x54, 0x00, 0x00, 0x00, 0x15,
215 // variable payload length:
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)
226 0x0a, // first bit not set, length=10
227 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, // value
230 0x05, // first bit not set, length=5
231 0x1a, 0x2b, 0x3c, 0x4d, 0x5e // value
234 MIHPacket mihPacket (MIHPacket::create(data));
235 BOOST_CHECK_EQUAL( mihPacket->payloadLength(), 42u);
237 BOOST_REQUIRE( mihPacket.next().is<MIHPayloadPacket>() );
238 MIHPayloadPacket mihPayload (mihPacket.next().as<MIHPayloadPacket>());
240 BOOST_CHECK_EQUAL( mihPayload->tlv_list().size(), 2u);
241 MIHPayloadPacketParser::tlv_list_t::container tlv_list_container (mihPayload->tlv_list());
243 GenericTLVPacket::Parser tlv1 = *tlv_list_container.begin();
244 BOOST_CHECK_EQUAL( tlv1.type(), 0x42);
245 BOOST_CHECK_EQUAL( tlv1.length(), 0x0au);
246 BOOST_CHECK_EQUAL( tlv1.value().size(), 0x0a);
248 GenericTLVPacket::Parser tlv2 = *boost::next(tlv_list_container.begin());
249 BOOST_CHECK_EQUAL( tlv2.type(), 0x43);
250 BOOST_CHECK_EQUAL( tlv2.length(), 0x05u);
251 BOOST_CHECK_EQUAL( tlv2.value().size(), 0x05);
255 BOOST_AUTO_UNIT_TEST(MIHPayload_create)
257 MIHPacket mihPacket (MIHPacket::create());
258 mihPacket->fragmentNr() = 42;
259 mihPacket->transactionId() = 21;
260 mihPacket->src_mihfId().setString( "senf@berlios.de");
261 mihPacket->dst_mihfId().setString( "test");
263 MIHPayloadPacket mihPayload (MIHPayloadPacket::createAfter(mihPacket));
265 unsigned char tlv1_value[] = {
266 0x1a, 0x2b, 0x3c, 0x4d, 0x5e };
267 GenericTLVPacket tlv2 = (GenericTLVPacket::create());
269 tlv2->value( tlv1_value);
271 mihPayload->tlv_list().push_front( tlv2);
273 unsigned char tlv2_value[] = {
274 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
275 GenericTLVPacket tlv1 (GenericTLVPacket::create());
277 tlv1->value( tlv2_value);
279 mihPayload->tlv_list().push_front( tlv1);
281 mihPacket.finalizeAll();
283 unsigned char data[] = {
285 0x10, 0x54, 0x00, 0x00, 0x00, 0x15,
286 // variable payload length:
288 // source MIHF_ID TLV:
289 0x01, 0x0f, // type, length
290 0x73, 0x65, 0x6e, 0x66, 0x40, 0x62, 0x65, 0x72, 0x6c,
291 0x69, 0x6f, 0x73, 0x2e, 0x64, 0x65, // value ("senf@berlios.de")
292 // destination MIHF_ID TLV:
293 0x02, 0x04, 0x74, 0x65, 0x73, 0x74, // type, length, value ("test")
294 // MIH Payload (two test tlvs)
297 0x0a, // first bit not set, length=10
298 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, // value
301 0x05, // first bit not set, length=5
302 0x1a, 0x2b, 0x3c, 0x4d, 0x5e // value
305 BOOST_CHECK(equal( mihPacket.data().begin(), mihPacket.data().end(), data ));
309 ///////////////////////////////cc.e////////////////////////////////////////
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