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>
37 ///////////////////////////////cc.p////////////////////////////////////////
42 void check_TLVPacket(GenericTLVPacket &tlvPacket, boost::uint32_t type, boost::uint32_t length)
44 BOOST_CHECK_EQUAL( tlvPacket->type(), type );
45 BOOST_CHECK_EQUAL( tlvPacket->length(), length );
47 BOOST_CHECK_EQUAL( tlvPacket->value().size(), length);
48 for (int i=0, j=tlvPacket->value().size(); i<j; i++)
49 BOOST_CHECK_EQUAL( tlvPacket->value()[i], i );
53 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_static)
55 // check static values:
56 // number of bytes to allocate for a new GenericTLVPacket should be 2
57 BOOST_CHECK_EQUAL( GenericTLVPacket::type::initSize(), 2u );
61 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_parse_packet_with_simple_length)
63 unsigned char data[] = {
65 0x0A, // first bit not set, length=10
66 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value
68 GenericTLVPacket tlvPacket (GenericTLVPacket::create(data));
69 check_TLVPacket( tlvPacket, 0x01, 0x0Au );
72 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_parse_packet_with_extended_length)
74 unsigned char data[] = {
76 0x81, // first and last bit set => one byte length following
77 0x0A, // length (10 bytes value)
78 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value
80 GenericTLVPacket tlvPacket (GenericTLVPacket::create(data));
81 check_TLVPacket( tlvPacket, 0x01, 0x0Au );
85 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_packet_with_simple_length)
87 GenericTLVPacket tlvPacket (GenericTLVPacket::create());
88 tlvPacket->type() = 42u;
89 for (uint8_t i=0; i<10; i++)
90 tlvPacket->value().push_back( i);
91 tlvPacket.finalizeAll();
93 check_TLVPacket( tlvPacket, 42u, 0x0Au );
97 BOOST_AUTO_UNIT_TEST(TLVPacket_create_packet_with_extended_length)
99 GenericTLVPacket tlvPacket (GenericTLVPacket::create());
100 tlvPacket->type() = 42u;
101 for (uint8_t i=0; i<129; i++)
102 tlvPacket->value().push_back( i);
103 tlvPacket.finalizeAll();
105 check_TLVPacket( tlvPacket, 42u, 129u );
109 BOOST_AUTO_UNIT_TEST(TLVPacket_create_invalid_packet)
115 BOOST_AUTO_UNIT_TEST(TLVFixPacket_static)
117 // check static values:
118 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt8Parser> > TestTLVPacket8;
119 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt24Parser> > TestTLVPacket24;
121 BOOST_CHECK_EQUAL( TestTLVPacket8::type::initSize(), 4+1u );
122 BOOST_CHECK_EQUAL( TestTLVPacket24::type::initSize(), 4+3u );
126 template <class TLVFixPacketType>
127 void test_TLVFixPacket_parsing(unsigned lengthParser_size)
129 std::vector<char> data;
130 data.push_back(0x01); data.push_back(0x23); data.push_back(0x45); data.push_back(0x67); // type
131 data.insert(data.end(), lengthParser_size-1, 0x00);
132 data.push_back(0x0A); // length
133 for( int i=0; i < 10; i++ ) {
134 data.push_back(i); // payload
136 TLVFixPacketType tlvPacket (TLVFixPacketType::create(
137 boost::make_iterator_range(data.begin(), data.end())));
138 check_TLVPacket( tlvPacket, 0x01234567u, 0x0Au );
141 BOOST_AUTO_UNIT_TEST(TLVFixPacket_parse_packet)
143 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt8Parser> > TestTLVPacket8;
144 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt16Parser> > TestTLVPacket16;
145 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt24Parser> > TestTLVPacket24;
146 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt32Parser> > TestTLVPacket32;
148 test_TLVFixPacket_parsing<TestTLVPacket8>( UInt8Parser::fixed_bytes);
149 test_TLVFixPacket_parsing<TestTLVPacket16>( UInt16Parser::fixed_bytes);
150 test_TLVFixPacket_parsing<TestTLVPacket24>( UInt24Parser::fixed_bytes);
151 test_TLVFixPacket_parsing<TestTLVPacket32>( UInt32Parser::fixed_bytes);
155 template <class TLVFixPacketType>
156 void test_TLVFixPacket_creating()
158 std::string payload ("Hello, world!");
159 TLVFixPacketType tlvPacket (TLVFixPacketType::create());
160 tlvPacket->type() = 42u;
161 DataPacket::createAfter( tlvPacket, payload );
162 tlvPacket.finalizeAll();
164 BOOST_CHECK_EQUAL( tlvPacket->type(), 42u);
165 BOOST_CHECK_EQUAL( tlvPacket->length(), 13u);
167 PacketData & tlvPacket_value (tlvPacket.next().data());
168 BOOST_CHECK( equal( tlvPacket_value.begin(), tlvPacket_value.end(), payload.begin() ));
171 BOOST_AUTO_UNIT_TEST(TLVFixPacket_create_packet)
173 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt8Parser> > TestTLVPacket8;
174 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt16Parser> > TestTLVPacket16;
175 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt24Parser> > TestTLVPacket24;
176 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt32Parser> > TestTLVPacket32;
178 test_TLVFixPacket_creating<TestTLVPacket8>();
179 test_TLVFixPacket_creating<TestTLVPacket16>();
180 test_TLVFixPacket_creating<TestTLVPacket24>();
181 test_TLVFixPacket_creating<TestTLVPacket32>();
185 template <class TLVFixPacketType>
186 void test_invalid_TLVFixPacket_creating(boost::uint32_t max_value)
188 TLVFixPacketType tlvPacket (TLVFixPacketType::create());
189 tlvPacket->type() = 42u;
190 DataPacket payload (DataPacket::createAfter( tlvPacket, max_value+1));
191 //DataPacket::createAfter( payload, 1); // this is one byte to much.
192 BOOST_CHECK_THROW( tlvPacket.finalizeAll(), UnsuportedTLVPacketException);
195 BOOST_AUTO_UNIT_TEST(TLVFixPacket_create_invalid_packet)
197 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt8Parser> > TestTLVPacket8;
198 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt16Parser> > TestTLVPacket16;
199 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt24Parser> > TestTLVPacket24;
201 test_invalid_TLVFixPacket_creating<TestTLVPacket8> ( UInt8Parser::max_value);
202 test_invalid_TLVFixPacket_creating<TestTLVPacket16>( UInt16Parser::max_value);
203 test_invalid_TLVFixPacket_creating<TestTLVPacket24>( UInt24Parser::max_value);
209 ///////////////////////////////cc.e////////////////////////////////////////
216 // c-file-style: "senf"
217 // indent-tabs-mode: nil
218 // ispell-local-dictionary: "american"
219 // compile-command: "scons -u test"
220 // comment-column: 40