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 TLVPacket unit tests */
26 //#include "TLVPacket.test.hh"
27 //#include "TLVPacket.test.ih"
30 #include "TLVPacket.hh"
31 #include "../DefaultBundle/EthernetPacket.hh"
33 #include "../../Utils/auto_unit_test.hh"
34 #include <boost/test/test_tools.hpp>
37 ///////////////////////////////cc.p////////////////////////////////////////
43 void check_TLVPacket(GenericTLVPacket &tlvPacket, boost::uint8_t type, boost::uint32_t length)
45 BOOST_CHECK_EQUAL( tlvPacket->type(), type );
46 BOOST_CHECK_EQUAL( tlvPacket->length(), length );
47 BOOST_CHECK_EQUAL( tlvPacket->value().size(), int(length) );
48 std::ostringstream oss (std::ostringstream::out);
49 SENF_CHECK_NO_THROW( tlvPacket.dump( oss));
50 senf::PacketData::iterator dataIterator (tlvPacket->value().begin());
51 for (unsigned i=0; i<length; i++) {
52 BOOST_CHECK_EQUAL( *dataIterator, i );
59 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_parse_packet_with_simple_length)
61 unsigned char data[] = {
63 0x0A, // first bit not set, length=10
64 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value
66 GenericTLVPacket tlvPacket (GenericTLVPacket::create(data));
67 check_TLVPacket( tlvPacket, 0x01, 0x0Au );
71 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_parse_packet_with_extended_length)
73 unsigned char data[] = {
75 0x81, // first and last bit set => one byte length following
76 0x0A, // length (10 bytes value)
77 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value
79 GenericTLVPacket tlvPacket (GenericTLVPacket::create(data));
80 check_TLVPacket( tlvPacket, 0x01, 0x0Au );
84 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_packet_with_simple_length)
86 unsigned char value[] = {
87 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09
89 GenericTLVPacket tlvPacket (GenericTLVPacket::create());
90 tlvPacket->type() = 42u;
91 tlvPacket->value( value);
92 tlvPacket.finalizeThis();
94 check_TLVPacket( tlvPacket, 42u, 0x0Au );
96 unsigned char data[] = {
98 0x0A, // first bit not set, length=10
99 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value
101 BOOST_CHECK( equal( tlvPacket.data().begin(), tlvPacket.data().end(), data ));
105 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_packet_with_extended_length)
107 unsigned char value[255];
108 for (unsigned i=0; i<sizeof(value); i++)
110 GenericTLVPacket tlvPacket (GenericTLVPacket::create());
111 tlvPacket->maxLengthValue( DynamicTLVLengthParser::max_value);
112 tlvPacket->type() = 42u;
113 tlvPacket->value( value);
114 tlvPacket.finalizeThis();
116 check_TLVPacket( tlvPacket, 42u, sizeof(value) );
118 unsigned char data[] = {
120 0x81, // first and last bit set => one byte length following
121 0xff, // length (255 bytes value)
122 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // first bytes of value
125 tlvPacket.data().begin(),
126 boost::next( tlvPacket.data().begin(), sizeof(data)),
131 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_invalid_packet)
133 GenericTLVPacket tlvPacket (GenericTLVPacket::create());
134 tlvPacket->type() = 42u;
135 tlvPacket.finalizeThis();
137 unsigned char value[255];
138 for (unsigned i=0; i<sizeof(value); i++)
141 BOOST_CHECK_THROW( tlvPacket->value( value), TLVLengthException);
142 tlvPacket->maxLengthValue( sizeof(value));
143 tlvPacket->value( value);
144 tlvPacket.finalizeThis();
145 check_TLVPacket( tlvPacket, 42u, sizeof(value) );
151 struct TestMacAddressTLVPacketParser : public BaseTLVPacketParser
153 # include SENF_PARSER()
154 SENF_PARSER_INHERIT ( BaseTLVPacketParser );
155 SENF_PARSER_VECTOR ( value, bytes(length), senf::MACAddressParser );
156 SENF_PARSER_FINALIZE( TestMacAddressTLVPacketParser );
159 struct TestMacAddressTLVPacketType
160 : public PacketTypeBase,
161 public PacketTypeMixin<TestMacAddressTLVPacketType>
163 typedef PacketTypeMixin<TestMacAddressTLVPacketType> mixin;
164 typedef TestMacAddressTLVPacketParser parser;
166 using mixin::nextPacketRange;
168 using mixin::initSize;
170 static void finalize(ConcretePacket<TestMacAddressTLVPacketType> p) {
174 typedef ConcretePacket<TestMacAddressTLVPacketType> TestMacAddressTLVPacket;
177 BOOST_AUTO_UNIT_TEST(TestMacAddressTLVPacket_create)
179 TestMacAddressTLVPacket tlvPacket (TestMacAddressTLVPacket::create());
180 tlvPacket->type() = 42;
181 tlvPacket->value().push_back( senf::MACAddress::from_string("01:23:45:67:89:ab") );
182 tlvPacket->value().push_back( senf::MACAddress::from_string("cd:ef:01:23:45:67") );
183 tlvPacket.finalizeThis();
185 unsigned char data[] = {
188 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67 // value
190 BOOST_CHECK( equal( tlvPacket.data().begin(), tlvPacket.data().end(), data ));
194 ///////////////////////////////cc.e////////////////////////////////////////
201 // c-file-style: "senf"
202 // indent-tabs-mode: nil
203 // ispell-local-dictionary: "american"
204 // compile-command: "scons -u test"
205 // comment-column: 40