switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / 80221Bundle / MIHPacket.test.cc
index 444f576..08d3605 100644 (file)
@@ -2,23 +2,28 @@
 //
 // Copyright (C) 2009
 // Fraunhofer Institute for Open Communication Systems (FOKUS)
-// Competence Center NETwork research (NET), St. Augustin, GERMANY
-//     Thorsten Horstmann <tho@berlios.de>
 //
-// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation; either version 2 of the License, or
-// (at your option) any later version.
+// The contents of this file are subject to the Fraunhofer FOKUS Public License
+// Version 1.0 (the "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at 
+// http://senf.berlios.de/license.html
 //
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
+// The Fraunhofer FOKUS Public License Version 1.0 is based on, 
+// but modifies the Mozilla Public License Version 1.1.
+// See the full license text for the amendments.
 //
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the
-// Free Software Foundation, Inc.,
-// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+// Software distributed under the License is distributed on an "AS IS" basis, 
+// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
+// for the specific language governing rights and limitations under the License.
+//
+// The Original Code is Fraunhofer FOKUS code.
+//
+// The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
+// (registered association), Hansastraße 27 c, 80686 Munich, Germany.
+// All Rights Reserved.
+//
+// Contributor(s):
+//   Thorsten Horstmann <tho@berlios.de>
 
 /** \file
     \brief MIHPacket unit tests */
 #include <boost/test/test_tools.hpp>
 
 #define prefix_
-///////////////////////////////cc.p////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 using namespace senf;
 
-BOOST_AUTO_UNIT_TEST(MIHPacket_MIHFId)
-{
-    MIHFId id ( MACAddress::from_string("01:02:03:04:05:06"));
-    BOOST_CHECK_EQUAL( id.type(), MIHFId::MACAddress);
-    BOOST_CHECK_EQUAL( id, MIHFId( MACAddress::from_string("01:02:03:04:05:06")));
-    BOOST_CHECK( id != MIHFId( MACAddress::from_string("01:02:03:04:05:07")));
-    BOOST_CHECK( id != MIHFId( INet4Address::from_string("128.129.130.131")));
-    BOOST_CHECK( id < MIHFId( MACAddress::from_string("01:02:03:04:05:07")));
-    BOOST_CHECK( id < MIHFId( INet4Address::from_string("128.129.130.131")));
-}
-
-BOOST_AUTO_UNIT_TEST(MIHPacket_msgId)
+SENF_AUTO_UNIT_TEST(MIHPacket_msgId)
 {
     MIHPacket mihPacket (MIHPacket::create());
     mihPacket->sid() = 4;
     mihPacket->opcode() = 3;
     mihPacket->aid() = 42;
-    BOOST_CHECK_EQUAL( mihPacket->messageId(), 0x4c2a );        
+    BOOST_CHECK_EQUAL( mihPacket->messageId(), 0x4c2a );
 }
 
-BOOST_AUTO_UNIT_TEST(MIHPacket_create_eth)
+SENF_AUTO_UNIT_TEST(MIHPacket_create_eth)
 {
     EthernetPacket eth (EthernetPacket::create());
     MIHPacket mihPacket (MIHPacket::createAfter(eth));
@@ -67,7 +61,7 @@ BOOST_AUTO_UNIT_TEST(MIHPacket_create_eth)
     SENF_CHECK_NO_THROW( eth.dump( oss));
 }
 
-BOOST_AUTO_UNIT_TEST(MIHPacket_create_string)
+SENF_AUTO_UNIT_TEST(MIHPacket_create_string)
 {
     MIHPacket mihPacket (MIHPacket::create());
     // set some fields
@@ -79,37 +73,57 @@ BOOST_AUTO_UNIT_TEST(MIHPacket_create_string)
 
     unsigned char data[] = {
             // MIH header
-            0x10, 0x54, 0x00, 0x00, 0x00, 0x15, 0x00, 0x17,
+            0x10, 0x54, 0x00, 0x00, 0x00, 0x15, 0x00, 0x19,
             // source MIHF_ID TLV:
-            0x01, 0x0f, // type, length
+            0x01, 0x10, // type, length
+            0x0f,  // value-length
             0x73, 0x65, 0x6e, 0x66, 0x40, 0x62, 0x65, 0x72, 0x6c,
             0x69, 0x6f, 0x73, 0x2e, 0x64, 0x65,  // value
             // destination MIHF_ID TLV:
-            0x02, 0x04, 0x74, 0x65, 0x73, 0x74
+            0x02, 0x05, 0x04, 0x74, 0x65, 0x73, 0x74
     };
-    BOOST_CHECK(equal( mihPacket.data().begin(), mihPacket.data().end(), data ));
+    SENF_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data),
+            mihPacket.data().begin(), mihPacket.data().end() );
     BOOST_CHECK_EQUAL( mihPacket->src_mihfId().valueAsString(), "senf@berlios.de");
     BOOST_CHECK_EQUAL( mihPacket->dst_mihfId().valueAsString(), "test");
 
     // the maximum length of a MIHF_ID is 253 octets
     BOOST_CHECK_THROW( mihPacket->dst_mihfId().value( std::string(254, 'x')), std::length_error);
-
     // now expand a MIHF_ID
-    mihPacket->dst_mihfId().maxLengthValue(253);
-    mihPacket->dst_mihfId().value( std::string(200, 'x'));
+    // first the tricky one: when setting the maximum id length to 128 the TLV length field
+    // is set to 129 and therefore expanded to 2 bytes; the id-length field is still 1 byte long
+    mihPacket->dst_mihfId().maxIdLength(128);
+    mihPacket->dst_mihfId().value( std::string(128, 'x'));
     mihPacket.finalizeThis();
-
-    BOOST_CHECK_EQUAL( mihPacket.size(), unsigned(8 + 17 + 203));
-    BOOST_CHECK_EQUAL( mihPacket->payloadLength(), 17 + 203);
-    BOOST_CHECK_EQUAL( mihPacket->dst_mihfId().length(), 200u);
-    BOOST_CHECK_EQUAL( senf::bytes(mihPacket->dst_mihfId()), 203u);
+    // packet size is now MIH header (8 bytes) + src MIHIFId TLV (18 bytes) +
+    // dst MIHIFId TLV (1 byte type + 2 byte TLV length + 1 byte id length + 128 id value)
+    BOOST_CHECK_EQUAL( mihPacket.size(), unsigned(8 + 18 + 1+2+1+128));
+    BOOST_CHECK_EQUAL( mihPacket->payloadLength(), 18 + 1+2+1+128);
+    BOOST_CHECK_EQUAL( mihPacket->dst_mihfId().length(), 1+128);
+    BOOST_CHECK_EQUAL( senf::bytes(mihPacket->dst_mihfId()), 1+2+1+128);
+    // now we extend the dst id to 129 bytes; than we have 2 bytes tlv length and 2 bytes
+    // id-length field
+    mihPacket->dst_mihfId().maxIdLength(129);
+    mihPacket->dst_mihfId().value( std::string(129, 'x'));
+    mihPacket.finalizeThis();
+    // packet size is now MIH header (8 bytes) + src MIHIFId TLV (18 bytes) +
+    // dst MIHIFId TLV (1 byte type + 2 byte TLV length + 2 byte id length + 128 id value)
+    BOOST_CHECK_EQUAL( mihPacket.size(), unsigned(8 + 18 + 1+2+2+129));
+    BOOST_CHECK_EQUAL( mihPacket->payloadLength(), 18 + 1+2+2+129);
+    BOOST_CHECK_EQUAL( mihPacket->dst_mihfId().length(), 2+129);
+    BOOST_CHECK_EQUAL( senf::bytes(mihPacket->dst_mihfId()), 1+2+2+129);
+    // finally we shrink to dst id field
+    mihPacket->dst_mihfId().value( "test");
+    mihPacket.finalizeThis();
+    SENF_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data),
+            mihPacket.data().begin(), mihPacket.data().end() );
 
     std::ostringstream oss (std::ostringstream::out);
     SENF_CHECK_NO_THROW( mihPacket.dump( oss));
 }
 
 
-BOOST_AUTO_UNIT_TEST(MIHPacket_create_mac)
+SENF_AUTO_UNIT_TEST(MIHPacket_create_mac)
 {
     MACAddress srcMac ( MACAddress::from_string("01:02:03:04:05:06"));
     MACAddress dstMac ( MACAddress::from_string("07:08:09:0a:0b:0c"));
@@ -123,12 +137,14 @@ BOOST_AUTO_UNIT_TEST(MIHPacket_create_mac)
 
     unsigned char data[] = {
             // MIH header
-            0x10, 0x54, 0x00, 0x00, 0x00, 0x15, 0x00, 0x1c,
+            0x10, 0x54, 0x00, 0x00, 0x00, 0x15, 0x00, 0x1e,
             // source MIHF_ID TLV:
-            0x01, 0x0c, // type, length
+            0x01, 0x0d, // type, length
+            0x0c,       // value-length
             0x5c, 0x01, 0x5c, 0x02, 0x5c, 0x03, 0x5c, 0x04, 0x5c, 0x05, 0x5c, 0x06, // value (nai-encoded)
             // destination MIHF_ID TLV:
-            0x02, 0x0c,  // type, length
+            0x02, 0x0d, // type, length
+            0x0c,       // value-length
             0x5c, 0x07, 0x5c, 0x08, 0x5c, 0x09, 0x5c, 0x0a, 0x5c, 0x0b, 0x5c, 0x0c  // value (nai-encoded)
     };
     SENF_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data),
@@ -140,7 +156,7 @@ BOOST_AUTO_UNIT_TEST(MIHPacket_create_mac)
 }
 
 
-BOOST_AUTO_UNIT_TEST(MIHPacket_create_inet4)
+SENF_AUTO_UNIT_TEST(MIHPacket_create_inet4)
 {
     MIHPacket mihPacket (MIHPacket::create());
     // set some fields
@@ -152,12 +168,14 @@ BOOST_AUTO_UNIT_TEST(MIHPacket_create_inet4)
 
     unsigned char data[] = {
             // MIH header
-            0x10, 0x54, 0x00, 0x00, 0x00, 0x15, 0x00, 0x14,
+            0x10, 0x54, 0x00, 0x00, 0x00, 0x15, 0x00, 0x16,
             // source MIHF_ID TLV:
-            0x01, 0x08, // type, length
+            0x01, 0x09, // type, length
+            0x08,       // value-length
             0x5c, 0x80, 0x5c, 0x81, 0x5c, 0x82, 0x5c, 0x83, // value (nai-encoded)
             // destination MIHF_ID TLV:
-            0x02, 0x08, // type, length
+            0x02, 0x09, // type, length
+            0x08,       // value-length
             0x5c, 0x84, 0x5c, 0x85, 0x5c, 0x86, 0x5c, 0x87  // value (nai-encoded)
     };
     SENF_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data),
@@ -171,7 +189,7 @@ BOOST_AUTO_UNIT_TEST(MIHPacket_create_inet4)
 }
 
 
-BOOST_AUTO_UNIT_TEST(MIHPacket_create_inet6)
+SENF_AUTO_UNIT_TEST(MIHPacket_create_inet6)
 {
     MIHPacket mihPacket (MIHPacket::create());
     // set some fields
@@ -183,16 +201,18 @@ BOOST_AUTO_UNIT_TEST(MIHPacket_create_inet6)
 
     unsigned char data[] = {
             // MIH header
-            0x10, 0x54, 0x00, 0x00, 0x00, 0x15, 0x00, 0x44,
+            0x10, 0x54, 0x00, 0x00, 0x00, 0x15, 0x00, 0x46,
             // source MIHF_ID TLV:
-            0x01, 0x20, // type, length
+            0x01, 0x21, // type, length
+            0x20,       // value-length
             // value (nai-encoded):
             0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00,
             0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00,
             0x5c, 0x00, 0x5c, 0x00, 0x5c, 0xff, 0x5c, 0xff,
             0x5c, 0x01, 0x5c, 0x02, 0x5c, 0x03, 0x5c, 0x04,
             // destination MIHF_ID TLV:
-            0x02, 0x20, // type, length
+            0x02, 0x21, // type, length
+            0x20,       // value-length
             // value (nai-encoded):
             0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00,
             0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00, 0x5c, 0x00,
@@ -210,7 +230,7 @@ BOOST_AUTO_UNIT_TEST(MIHPacket_create_inet6)
 }
 
 
-BOOST_AUTO_UNIT_TEST(MIHPayload_parse)
+SENF_AUTO_UNIT_TEST(MIHPayload_parse)
 {
     unsigned char data[] = {
             // MIH header
@@ -218,11 +238,12 @@ BOOST_AUTO_UNIT_TEST(MIHPayload_parse)
             // variable payload length:
             0x00, 0x29,
             // source MIHF_ID TLV:
-            0x01, 0x0f, // type, length
+            0x01, 0x10, // type, length
+            0x0f,  // value-length
             0x73, 0x65, 0x6e, 0x66, 0x40, 0x62, 0x65, 0x72, 0x6c,
-            0x69, 0x6f, 0x73, 0x2e, 0x64, 0x65,  // value ("senf@berlios.de")
+            0x69, 0x6f, 0x73, 0x2e, 0x64, 0x65,  // value
             // destination MIHF_ID TLV:
-            0x02, 0x04, 0x74, 0x65, 0x73, 0x74,  // type, length, value ("test")
+            0x02, 0x05, 0x04, 0x74, 0x65, 0x73, 0x74,
             // MIH Payload (two test tlvs)
             // first test tlv
             0x42, // type
@@ -253,13 +274,13 @@ BOOST_AUTO_UNIT_TEST(MIHPayload_parse)
     BOOST_CHECK_EQUAL( tlv2.type(), 0x0c);
     BOOST_CHECK_EQUAL( tlv2.length(), 0x04u);
     BOOST_CHECK_EQUAL( tlv2.value().size(), 0x04);
-    
+
     std::ostringstream oss (std::ostringstream::out);
     SENF_CHECK_NO_THROW( mihPayload.dump( oss));
 }
 
 
-BOOST_AUTO_UNIT_TEST(MIHPayload_create)
+SENF_AUTO_UNIT_TEST(MIHPayload_create)
 {
     MIHPacket mihPacket (MIHPacket::create());
     mihPacket->fragmentNr() = 42;
@@ -270,7 +291,7 @@ BOOST_AUTO_UNIT_TEST(MIHPayload_create)
     MIHGenericPayloadPacket mihPayload (MIHGenericPayloadPacket::createAfter(mihPacket));
     MIHGenericPayloadPacket::Parser::tlvList_t::container tlvListContainer (
             mihPayload->tlvList() );
-    
+
     unsigned char tlv1_value[] = {
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
     MIHGenericTLVParser tlv1 ( tlvListContainer.push_back_space());
@@ -280,7 +301,7 @@ BOOST_AUTO_UNIT_TEST(MIHPayload_create)
     unsigned char tlv2_value[] = {
             0x1a, 0x2b, 0x3c, 0x4d };
     MIHGenericTLVParser tlv2 ( tlvListContainer.push_back_space());
-    tlv2.type() = 0x0c;
+    tlv2.type() = 0x43;
     tlv2.value( tlv2_value);
 
     mihPacket.finalizeAll();
@@ -289,29 +310,47 @@ BOOST_AUTO_UNIT_TEST(MIHPayload_create)
             // MIH header
             0x10, 0x54, 0x00, 0x00, 0x00, 0x15,
             // variable payload length:
-            0x00, 0x29,
+            0x00, 0x2b,
             // source MIHF_ID TLV:
-            0x01, 0x0f, // type, length
+            0x01, 0x10, // type, length
+            0x0f,  // value-length
             0x73, 0x65, 0x6e, 0x66, 0x40, 0x62, 0x65, 0x72, 0x6c,
-            0x69, 0x6f, 0x73, 0x2e, 0x64, 0x65,  // value ("senf@berlios.de")
+            0x69, 0x6f, 0x73, 0x2e, 0x64, 0x65,  // value
             // destination MIHF_ID TLV:
-            0x02, 0x04, 0x74, 0x65, 0x73, 0x74,  // type, length, value ("test")
+            0x02, 0x05, 0x04, 0x74, 0x65, 0x73, 0x74,
             // MIH Payload (two test tlvs)
             // first test tlv
             0x42, // type
             0x0a, // first bit not set, length=10
             0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, // value
             // second test tlv
-            0x0c, // type
+            0x43, // type
             0x04, // first bit not set, length=4
             0x1a, 0x2b, 0x3c, 0x4d // value
     };
     SENF_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data),
-            mihPacket.data().begin(), mihPacket.data().end() );    
+            mihPacket.data().begin(), mihPacket.data().end() );
 }
 
+SENF_AUTO_UNIT_TEST(Test_MIHFIdTLV)
+{
+    unsigned char data[] = {
+            // MIH header
+            0x10, 0x54, 0x00, 0x00, 0x00, 0x15, 0x00, 0x06,
+            // source MIHF_ID TLV:
+            0x01, 0x01, // type, length
+            0x00,       // value-length
+            // destination MIHF_ID TLV:
+            0x02, 0x01, // type, length
+            0x00        // value-length
+    };
+
+    MIHPacket mihPacket (MIHPacket::create(data));
+    BOOST_CHECK( mihPacket->src_mihfId().valueEquals( MIHFId::Multicast) );
+    BOOST_CHECK( mihPacket->dst_mihfId().valueEquals( MIHFId::Multicast) );
+}
 
-///////////////////////////////cc.e////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 #undef prefix_
 
 \f