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.
25 //#include "TLVPacket.test.hh"
26 //#include "TLVPacket.test.ih"
29 #include "TLVPacket.hh"
30 #include "../DefaultBundle/EthernetPacket.hh"
32 #include "../../Utils/auto_unit_test.hh"
33 #include <boost/test/test_tools.hpp>
36 ///////////////////////////////cc.p////////////////////////////////////////
42 void check_TLVPacket(GenericTLVPacket &tlvPacket, boost::uint8_t type, boost::uint32_t length)
44 BOOST_CHECK_EQUAL( tlvPacket->type(), type );
45 BOOST_CHECK_EQUAL( tlvPacket->length(), length );
46 BOOST_CHECK_EQUAL( tlvPacket->value().size(), int(length) );
47 std::ostringstream oss (std::ostringstream::out);
48 SENF_CHECK_NO_THROW( tlvPacket.dump( oss));
49 senf::PacketData::iterator dataIterator (tlvPacket->value().begin());
50 for (unsigned i=0; i<length; i++) {
51 BOOST_CHECK_EQUAL( *dataIterator, i );
58 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_parse_packet_with_simple_length)
60 unsigned char data[] = {
62 0x0A, // first bit not set, length=10
63 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value
65 GenericTLVPacket tlvPacket (GenericTLVPacket::create(data));
66 check_TLVPacket( tlvPacket, 0x01, 0x0Au );
70 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_parse_packet_with_extended_length)
72 unsigned char data[] = {
74 0x81, // first and last bit set => one byte length following
75 0x0A, // length (10 bytes value)
76 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value
78 GenericTLVPacket tlvPacket (GenericTLVPacket::create(data));
79 check_TLVPacket( tlvPacket, 0x01, 0x0Au );
83 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_packet_with_simple_length)
85 unsigned char value[] = {
86 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09
88 GenericTLVPacket tlvPacket (GenericTLVPacket::create());
89 tlvPacket->type() = 42u;
90 tlvPacket->value( value);
91 tlvPacket.finalizeThis();
93 check_TLVPacket( tlvPacket, 42u, 0x0Au );
95 unsigned char data[] = {
97 0x0A, // first bit not set, length=10
98 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value
100 BOOST_CHECK( equal( tlvPacket.data().begin(), tlvPacket.data().end(), data ));
104 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_packet_with_extended_length)
106 unsigned char value[255];
107 for (unsigned i=0; i<sizeof(value); i++)
109 GenericTLVPacket tlvPacket (GenericTLVPacket::create());
110 tlvPacket->maxLengthValue( DynamicTLVLengthParser::max_value);
111 tlvPacket->type() = 42u;
112 tlvPacket->value( value);
113 tlvPacket.finalizeThis();
115 check_TLVPacket( tlvPacket, 42u, sizeof(value) );
117 unsigned char data[] = {
119 0x81, // first and last bit set => one byte length following
120 0xff, // length (255 bytes value)
121 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // first bytes of value
124 tlvPacket.data().begin(),
125 boost::next( tlvPacket.data().begin(), sizeof(data)),
130 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_invalid_packet)
132 GenericTLVPacket tlvPacket (GenericTLVPacket::create());
133 tlvPacket->type() = 42u;
134 tlvPacket.finalizeThis();
136 unsigned char value[255];
137 for (unsigned i=0; i<sizeof(value); i++)
140 BOOST_CHECK_THROW( tlvPacket->value( value), TLVLengthException);
141 tlvPacket->maxLengthValue( sizeof(value));
142 tlvPacket->value( value);
143 tlvPacket.finalizeThis();
144 check_TLVPacket( tlvPacket, 42u, sizeof(value) );
150 struct TestMacAddressTLVPacketParser : public BaseTLVPacketParser
152 # include SENF_PARSER()
153 SENF_PARSER_INHERIT ( BaseTLVPacketParser );
154 SENF_PARSER_VECTOR ( value, bytes(length), senf::MACAddressParser );
155 SENF_PARSER_FINALIZE( TestMacAddressTLVPacketParser );
158 struct TestMacAddressTLVPacketType
159 : public PacketTypeBase,
160 public PacketTypeMixin<TestMacAddressTLVPacketType>
162 typedef PacketTypeMixin<TestMacAddressTLVPacketType> mixin;
163 typedef TestMacAddressTLVPacketParser parser;
165 using mixin::nextPacketRange;
167 using mixin::initSize;
169 static void finalize(ConcretePacket<TestMacAddressTLVPacketType> p) {
173 typedef ConcretePacket<TestMacAddressTLVPacketType> TestMacAddressTLVPacket;
176 BOOST_AUTO_UNIT_TEST(TestMacAddressTLVPacket_create)
178 TestMacAddressTLVPacket tlvPacket (TestMacAddressTLVPacket::create());
179 tlvPacket->type() = 42;
180 tlvPacket->value().push_back( senf::MACAddress::from_string("01:23:45:67:89:ab") );
181 tlvPacket->value().push_back( senf::MACAddress::from_string("cd:ef:01:23:45:67") );
182 tlvPacket.finalizeThis();
184 unsigned char data[] = {
187 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67 // value
189 BOOST_CHECK( equal( tlvPacket.data().begin(), tlvPacket.data().end(), data ));
193 ///////////////////////////////cc.e////////////////////////////////////////
200 // c-file-style: "senf"
201 // indent-tabs-mode: nil
202 // ispell-local-dictionary: "american"
203 // compile-command: "scons -u test"
204 // comment-column: 40