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 <senf/Packets.hh>
32 #include "../../Utils/auto_unit_test.hh"
33 #include <boost/test/test_tools.hpp>
36 ///////////////////////////////cc.p////////////////////////////////////////
41 void check_TLVPacket(GenericTLVPacket &tlvPacket, boost::uint8_t type, boost::uint32_t length)
43 BOOST_CHECK_EQUAL( tlvPacket->type(), type );
44 BOOST_CHECK_EQUAL( tlvPacket->length(), length );
45 BOOST_CHECK_EQUAL( tlvPacket->value().size(), length );
46 senf::PacketData::iterator dataIterator (tlvPacket->value().begin());
47 for (unsigned i=0; i<length; i++) {
48 BOOST_CHECK_EQUAL( *dataIterator, i );
54 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_parse_packet_with_simple_length)
56 unsigned char data[] = {
58 0x0A, // first bit not set, length=10
59 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value
61 GenericTLVPacket tlvPacket (GenericTLVPacket::create(data));
62 check_TLVPacket( tlvPacket, 0x01, 0x0Au );
66 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_parse_packet_with_extended_length)
68 unsigned char data[] = {
70 0x81, // first and last bit set => one byte length following
71 0x0A, // length (10 bytes value)
72 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value
74 GenericTLVPacket tlvPacket (GenericTLVPacket::create(data));
75 check_TLVPacket( tlvPacket, 0x01, 0x0Au );
79 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_packet_with_simple_length)
81 unsigned char value[] = {
82 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09
84 GenericTLVPacket tlvPacket (GenericTLVPacket::create());
85 tlvPacket->type() = 42u;
86 tlvPacket->value( value);
87 tlvPacket.finalizeThis();
89 check_TLVPacket( tlvPacket, 42u, 0x0Au );
91 unsigned char data[] = {
93 0x0A, // first bit not set, length=10
94 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value
96 BOOST_CHECK( equal( tlvPacket.data().begin(), tlvPacket.data().end(), data ));
100 BOOST_AUTO_UNIT_TEST(TLVPacket_create_packet_with_extended_length)
102 GenericTLVPacket tlvPacket (GenericTLVPacket::create());
103 tlvPacket->type() = 42u;
104 for (uint8_t i=0; i<129; i++)
105 tlvPacket->value().push_back( i);
106 tlvPacket.finalizeAll();
108 check_TLVPacket( tlvPacket, 42u, 129u );
112 BOOST_AUTO_UNIT_TEST(TLVPacket_create_invalid_packet)
118 BOOST_AUTO_UNIT_TEST(TLVFixPacket_static)
120 // check static values:
121 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt8Parser> > TestTLVPacket8;
122 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt24Parser> > TestTLVPacket24;
124 BOOST_CHECK_EQUAL( TestTLVPacket8::type::initSize(), 4+1u );
125 BOOST_CHECK_EQUAL( TestTLVPacket24::type::initSize(), 4+3u );
129 template <class TLVFixPacketType>
130 void test_TLVFixPacket_parsing(unsigned lengthParser_size)
132 std::vector<char> data;
133 data.push_back(0x01); data.push_back(0x23); data.push_back(0x45); data.push_back(0x67); // type
134 data.insert(data.end(), lengthParser_size-1, 0x00);
135 data.push_back(0x0A); // length
136 for( int i=0; i < 10; i++ ) {
137 data.push_back(i); // payload
139 TLVFixPacketType tlvPacket (TLVFixPacketType::create(
140 boost::make_iterator_range(data.begin(), data.end())));
141 check_TLVPacket( tlvPacket, 0x01234567u, 0x0Au );
144 BOOST_AUTO_UNIT_TEST(TLVFixPacket_parse_packet)
146 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt8Parser> > TestTLVPacket8;
147 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt16Parser> > TestTLVPacket16;
148 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt24Parser> > TestTLVPacket24;
149 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt32Parser> > TestTLVPacket32;
151 test_TLVFixPacket_parsing<TestTLVPacket8>( UInt8Parser::fixed_bytes);
152 test_TLVFixPacket_parsing<TestTLVPacket16>( UInt16Parser::fixed_bytes);
153 test_TLVFixPacket_parsing<TestTLVPacket24>( UInt24Parser::fixed_bytes);
154 test_TLVFixPacket_parsing<TestTLVPacket32>( UInt32Parser::fixed_bytes);
158 template <class TLVFixPacketType>
159 void test_TLVFixPacket_creating()
161 std::string payload ("Hello, world!");
162 TLVFixPacketType tlvPacket (TLVFixPacketType::create());
163 tlvPacket->type() = 42u;
164 DataPacket::createAfter( tlvPacket, payload );
165 tlvPacket.finalizeAll();
167 BOOST_CHECK_EQUAL( tlvPacket->type(), 42u);
168 BOOST_CHECK_EQUAL( tlvPacket->length(), 13u);
170 PacketData & tlvPacket_value (tlvPacket.next().data());
171 BOOST_CHECK( equal( tlvPacket_value.begin(), tlvPacket_value.end(), payload.begin() ));
174 BOOST_AUTO_UNIT_TEST(TLVFixPacket_create_packet)
176 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt8Parser> > TestTLVPacket8;
177 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt16Parser> > TestTLVPacket16;
178 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt24Parser> > TestTLVPacket24;
179 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt32Parser> > TestTLVPacket32;
181 test_TLVFixPacket_creating<TestTLVPacket8>();
182 test_TLVFixPacket_creating<TestTLVPacket16>();
183 test_TLVFixPacket_creating<TestTLVPacket24>();
184 test_TLVFixPacket_creating<TestTLVPacket32>();
188 template <class TLVFixPacketType>
189 void test_invalid_TLVFixPacket_creating(boost::uint32_t max_value)
191 TLVFixPacketType tlvPacket (TLVFixPacketType::create());
192 tlvPacket->type() = 42u;
193 DataPacket payload (DataPacket::createAfter( tlvPacket, max_value+1));
194 //DataPacket::createAfter( payload, 1); // this is one byte to much.
195 BOOST_CHECK_THROW( tlvPacket.finalizeAll(), UnsuportedTLVPacketException);
198 BOOST_AUTO_UNIT_TEST(TLVFixPacket_create_invalid_packet)
200 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt8Parser> > TestTLVPacket8;
201 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt16Parser> > TestTLVPacket16;
202 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt24Parser> > TestTLVPacket24;
204 test_invalid_TLVFixPacket_creating<TestTLVPacket8> ( UInt8Parser::max_value);
205 test_invalid_TLVFixPacket_creating<TestTLVPacket16>( UInt16Parser::max_value);
206 test_invalid_TLVFixPacket_creating<TestTLVPacket24>( UInt24Parser::max_value);
211 ///////////////////////////////cc.e////////////////////////////////////////
218 // c-file-style: "senf"
219 // indent-tabs-mode: nil
220 // ispell-local-dictionary: "american"
221 // compile-command: "scons -u test"
222 // comment-column: 40