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(), 0x8917);
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().value( "senf@berlios.de");
77 mihPacket->dst_mihfId().value( "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().valueAsString(), "senf@berlios.de");
92 BOOST_CHECK_EQUAL( mihPacket->dst_mihfId().valueAsString(), "test");
94 // the maximum length of a MIHF_ID is 253 octets
95 BOOST_CHECK_THROW( mihPacket->dst_mihfId().value( std::string(254, 'x')), std::length_error);
97 // now expand a MIHF_ID
98 mihPacket->dst_mihfId().maxLengthValue(253);
99 mihPacket->dst_mihfId().value( 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 MACAddress srcMac ( MACAddress::from_string("01:02:03:04:05:06"));
115 MACAddress dstMac ( MACAddress::from_string("07:08:09:0a:0b:0c"));
116 MIHPacket mihPacket (MIHPacket::create());
118 mihPacket->fragmentNr() = 42;
119 mihPacket->transactionId() = 21;
120 mihPacket->src_mihfId().value( srcMac);
121 mihPacket->dst_mihfId().value( dstMac);
122 mihPacket.finalizeThis();
124 unsigned char data[] = {
126 0x10, 0x54, 0x00, 0x00, 0x00, 0x15, 0x00, 0x1c,
127 // source MIHF_ID TLV:
128 0x01, 0x0c, // type, length
129 0x5c, 0x01, 0x5c, 0x02, 0x5c, 0x03, 0x5c, 0x04, 0x5c, 0x05, 0x5c, 0x06, // value (nai-encoded)
130 // destination MIHF_ID TLV:
131 0x02, 0x0c, // type, length
132 0x5c, 0x07, 0x5c, 0x08, 0x5c, 0x09, 0x5c, 0x0a, 0x5c, 0x0b, 0x5c, 0x0c // value (nai-encoded)
134 SENF_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data),
135 mihPacket.data().begin(), mihPacket.data().end() );
136 BOOST_CHECK_EQUAL( mihPacket->src_mihfId().valueAsMACAddress(), srcMac);
137 BOOST_CHECK_EQUAL( mihPacket->dst_mihfId().valueAsMACAddress(), dstMac);
138 BOOST_CHECK_EQUAL( mihPacket->dst_mihfId().valueAs( MIHFId::MACAddress), MIHFId(dstMac) );
139 BOOST_CHECK( mihPacket->src_mihfId().valueEquals(srcMac));
143 BOOST_AUTO_UNIT_TEST(MIHPacket_create_inet4)
145 MIHPacket mihPacket (MIHPacket::create());
147 mihPacket->fragmentNr() = 42;
148 mihPacket->transactionId() = 21;
149 mihPacket->src_mihfId().value( INet4Address::from_string("128.129.130.131"));
150 mihPacket->dst_mihfId().value( INet4Address::from_string("132.133.134.135"));
151 mihPacket.finalizeThis();
153 unsigned char data[] = {
155 0x10, 0x54, 0x00, 0x00, 0x00, 0x15, 0x00, 0x14,
156 // source MIHF_ID TLV:
157 0x01, 0x08, // type, length
158 0x5c, 0x80, 0x5c, 0x81, 0x5c, 0x82, 0x5c, 0x83, // value (nai-encoded)
159 // destination MIHF_ID TLV:
160 0x02, 0x08, // type, length
161 0x5c, 0x84, 0x5c, 0x85, 0x5c, 0x86, 0x5c, 0x87 // value (nai-encoded)
163 SENF_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data),
164 mihPacket.data().begin(), mihPacket.data().end() );
166 mihPacket->src_mihfId().valueAsINet4Address(),
167 INet4Address::from_string("128.129.130.131"));
169 mihPacket->dst_mihfId().valueAsINet4Address(),
170 INet4Address::from_string("132.133.134.135"));
174 BOOST_AUTO_UNIT_TEST(MIHPacket_create_inet6)
176 MIHPacket mihPacket (MIHPacket::create());
178 mihPacket->fragmentNr() = 42;
179 mihPacket->transactionId() = 21;
180 mihPacket->src_mihfId().value( INet6Address::from_string("::ffff:1.2.3.4"));
181 mihPacket->dst_mihfId().value( INet6Address::from_string("::ffff:5.6.7.8"));
182 mihPacket.finalizeThis();
184 unsigned char data[] = {
186 0x10, 0x54, 0x00, 0x00, 0x00, 0x15, 0x00, 0x44,
187 // source MIHF_ID TLV:
188 0x01, 0x20, // type, length
189 // value (nai-encoded):
190 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00,
191 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00,
192 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0xff, 0x5c, 0xff,
193 0x5c, 0x01, 0x5c, 0x02, 0x5c, 0x03, 0x5c, 0x04,
194 // destination MIHF_ID TLV:
195 0x02, 0x20, // type, length
196 // value (nai-encoded):
197 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00,
198 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00,
199 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0xff, 0x5c, 0xff,
200 0x5c, 0x05, 0x5c, 0x06, 0x5c, 0x07, 0x5c, 0x08
202 SENF_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data),
203 mihPacket.data().begin(), mihPacket.data().end() );
205 mihPacket->src_mihfId().valueAsINet6Address(),
206 INet6Address::from_string("::ffff:1.2.3.4"));
208 mihPacket->dst_mihfId().valueAsINet6Address(),
209 INet6Address::from_string("::ffff:5.6.7.8") );
213 BOOST_AUTO_UNIT_TEST(MIHPayload_parse)
215 unsigned char data[] = {
217 0x10, 0x54, 0x00, 0x00, 0x00, 0x15,
218 // variable payload length:
220 // source MIHF_ID TLV:
221 0x01, 0x0f, // type, length
222 0x73, 0x65, 0x6e, 0x66, 0x40, 0x62, 0x65, 0x72, 0x6c,
223 0x69, 0x6f, 0x73, 0x2e, 0x64, 0x65, // value ("senf@berlios.de")
224 // destination MIHF_ID TLV:
225 0x02, 0x04, 0x74, 0x65, 0x73, 0x74, // type, length, value ("test")
226 // MIH Payload (two test tlvs)
229 0x0a, // first bit not set, length=10
230 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, // value
233 0x04, // first bit not set, length=4
234 0x1a, 0x2b, 0x3c, 0x4d // value
237 MIHPacket mihPacket (MIHPacket::create(data));
238 BOOST_CHECK_EQUAL( mihPacket->payloadLength(), 41u);
240 BOOST_REQUIRE( mihPacket.next().is<MIHGenericPayloadPacket>() );
241 MIHGenericPayloadPacket mihPayload (mihPacket.next().as<MIHGenericPayloadPacket>());
243 BOOST_CHECK_EQUAL( mihPayload->tlvList().size(), 2u);
244 MIHGenericPayloadPacket::Parser::tlvList_t::container tlvListContainer (
245 mihPayload->tlvList());
247 MIHGenericTLVParser tlv1 = *tlvListContainer.begin();
248 BOOST_CHECK_EQUAL( tlv1.type(), 0x42);
249 BOOST_CHECK_EQUAL( tlv1.length(), 0x0au);
250 BOOST_CHECK_EQUAL( tlv1.value().size(), 0x0a);
252 MIHGenericTLVParser tlv2 = *boost::next(tlvListContainer.begin());
253 BOOST_CHECK_EQUAL( tlv2.type(), 0x0c);
254 BOOST_CHECK_EQUAL( tlv2.length(), 0x04u);
255 BOOST_CHECK_EQUAL( tlv2.value().size(), 0x04);
257 std::ostringstream oss (std::ostringstream::out);
258 SENF_CHECK_NO_THROW( mihPayload.dump( oss));
262 BOOST_AUTO_UNIT_TEST(MIHPayload_create)
264 MIHPacket mihPacket (MIHPacket::create());
265 mihPacket->fragmentNr() = 42;
266 mihPacket->transactionId() = 21;
267 mihPacket->src_mihfId().value( "senf@berlios.de");
268 mihPacket->dst_mihfId().value( "test");
270 MIHGenericPayloadPacket mihPayload (MIHGenericPayloadPacket::createAfter(mihPacket));
271 MIHGenericPayloadPacket::Parser::tlvList_t::container tlvListContainer (
272 mihPayload->tlvList() );
274 unsigned char tlv1_value[] = {
275 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
276 MIHGenericTLVParser tlv1 ( tlvListContainer.push_back_space());
278 tlv1.value( tlv1_value);
280 unsigned char tlv2_value[] = {
281 0x1a, 0x2b, 0x3c, 0x4d };
282 MIHGenericTLVParser tlv2 ( tlvListContainer.push_back_space());
284 tlv2.value( tlv2_value);
286 mihPacket.finalizeAll();
288 unsigned char data[] = {
290 0x10, 0x54, 0x00, 0x00, 0x00, 0x15,
291 // variable payload length:
293 // source MIHF_ID TLV:
294 0x01, 0x0f, // type, length
295 0x73, 0x65, 0x6e, 0x66, 0x40, 0x62, 0x65, 0x72, 0x6c,
296 0x69, 0x6f, 0x73, 0x2e, 0x64, 0x65, // value ("senf@berlios.de")
297 // destination MIHF_ID TLV:
298 0x02, 0x04, 0x74, 0x65, 0x73, 0x74, // type, length, value ("test")
299 // MIH Payload (two test tlvs)
302 0x0a, // first bit not set, length=10
303 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, // value
306 0x04, // first bit not set, length=4
307 0x1a, 0x2b, 0x3c, 0x4d // value
309 SENF_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data),
310 mihPacket.data().begin(), mihPacket.data().end() );
314 ///////////////////////////////cc.e////////////////////////////////////////
321 // c-file-style: "senf"
322 // indent-tabs-mode: nil
323 // ispell-local-dictionary: "american"
324 // compile-command: "scons -u test"
325 // comment-column: 40