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 template <class TLVPacketType>
43 void check_TLVPacket(TLVPacketType tlvPacket, boost::uint32_t type, boost::uint32_t length)
45 BOOST_CHECK_EQUAL( tlvPacket->type(), type );
46 BOOST_CHECK_EQUAL( tlvPacket->length(), length );
48 PacketData & tlvPacket_value (tlvPacket.next().data());
49 BOOST_CHECK_EQUAL( tlvPacket_value.size(), length);
50 for (int i=0, j=tlvPacket_value.size(); i<j; i++)
51 BOOST_CHECK_EQUAL( tlvPacket_value[i], i );
55 BOOST_AUTO_UNIT_TEST(TLVPacket_static)
57 // check static values:
58 // number of bytes to allocate for a new TLVPacket should be 5
59 // BOOST_CHECK_EQUAL( TLVPacket::type::initSize(), 5u );
63 BOOST_AUTO_UNIT_TEST(TLVPacket_parse_packet_with_simple_length)
65 typedef ConcretePacket<TLVPacketType<UInt32Parser, DynamicTLVLengthParser> > TestTLVPacket;
66 unsigned char data[] = {
67 0x01, 0x23, 0x45, 0x67, // type
68 0x0A, // first bit not set, length=10
69 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value (payload)
71 TestTLVPacket tlvPacket (TestTLVPacket::create(data));
72 check_TLVPacket( tlvPacket, 0x01234567u, 0x0Au );
76 BOOST_AUTO_UNIT_TEST(TLVPacket_parse_packet_with_extended_length)
78 typedef ConcretePacket<TLVPacketType<UInt32Parser, DynamicTLVLengthParser> > TestTLVPacket;
79 unsigned char data[] = {
80 0x01, 0x23, 0x45, 0x67, // type
81 0x81, // first and last bit set => one byte length following
82 0x0A, // length (10 bytes value)
83 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value (payload)
85 TestTLVPacket tlvPacket (TestTLVPacket::create(data));
86 check_TLVPacket( tlvPacket, 0x01234567u, 0x0Au );
90 BOOST_AUTO_UNIT_TEST(TLVPacket_create_packet_with_simple_length)
92 typedef ConcretePacket<TLVPacketType<UInt32Parser, DynamicTLVLengthParser> > TestTLVPacket;
93 std::string payload ("Hello, world!");
94 TestTLVPacket tlvPacket (TestTLVPacket::create());
95 tlvPacket->type() = 42u;
96 DataPacket::createAfter( tlvPacket, payload );
97 tlvPacket.finalizeAll();
99 BOOST_CHECK_EQUAL( tlvPacket->type(), 42u);
100 BOOST_CHECK_EQUAL( tlvPacket->length(), 13u);
102 PacketData & tlvPacket_value (tlvPacket.next().data());
103 BOOST_CHECK( equal( tlvPacket_value.begin(), tlvPacket_value.end(), payload.begin() ));
107 BOOST_AUTO_UNIT_TEST(TLVPacket_create_packet_with_extended_length)
109 typedef ConcretePacket<TLVPacketType<UInt32Parser, DynamicTLVLengthParser> > TestTLVPacket;
110 std::string payload (
111 "This is a very long string with more than 127 characters to check if the TLV-Packet "
112 "works correctly with an extended length. That's all." );
113 TestTLVPacket tlvPacket (TestTLVPacket::create());
114 tlvPacket->type() = 42u;
115 DataPacket::createAfter( tlvPacket, payload );
116 tlvPacket.finalizeAll();
118 BOOST_CHECK_EQUAL( tlvPacket->type(), 42u );
119 BOOST_CHECK_EQUAL( tlvPacket->length(), payload.size() );
121 PacketData & tlvPacket_value (tlvPacket.next().data());
122 BOOST_CHECK( equal( tlvPacket_value.begin(), tlvPacket_value.end(), payload.begin() ));
124 payload = std::string("This is a short string with less than 127 characters. That's all.");
125 DataPacket::createAfter( tlvPacket, payload );
126 tlvPacket.finalizeAll();
128 BOOST_CHECK_EQUAL( tlvPacket->type(), 42u );
129 BOOST_CHECK_EQUAL( tlvPacket->length(), payload.size() );
131 PacketData & tlvPacket_value2 (tlvPacket.next().data());
132 BOOST_CHECK( equal( tlvPacket_value2.begin(), tlvPacket_value2.end(), payload.begin() ));
136 BOOST_AUTO_UNIT_TEST(TLVPacket_create_invalid_packet)
142 BOOST_AUTO_UNIT_TEST(TLVFixPacket_static)
144 // check static values:
145 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt8Parser> > TestTLVPacket8;
146 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt24Parser> > TestTLVPacket24;
148 BOOST_CHECK_EQUAL( TestTLVPacket8::type::initSize(), 4+1u );
149 BOOST_CHECK_EQUAL( TestTLVPacket24::type::initSize(), 4+3u );
153 template <class TLVFixPacketType>
154 void test_TLVFixPacket_parsing(unsigned lengthParser_size)
156 std::vector<char> data;
157 data.push_back(0x01); data.push_back(0x23); data.push_back(0x45); data.push_back(0x67); // type
158 data.insert(data.end(), lengthParser_size-1, 0x00);
159 data.push_back(0x0A); // length
160 for( int i=0; i < 10; i++ ) {
161 data.push_back(i); // payload
163 TLVFixPacketType tlvPacket (TLVFixPacketType::create(
164 boost::make_iterator_range(data.begin(), data.end())));
165 check_TLVPacket( tlvPacket, 0x01234567u, 0x0Au );
168 BOOST_AUTO_UNIT_TEST(TLVFixPacket_parse_packet)
170 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt8Parser> > TestTLVPacket8;
171 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt16Parser> > TestTLVPacket16;
172 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt24Parser> > TestTLVPacket24;
173 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt32Parser> > TestTLVPacket32;
175 test_TLVFixPacket_parsing<TestTLVPacket8>( UInt8Parser::fixed_bytes);
176 test_TLVFixPacket_parsing<TestTLVPacket16>( UInt16Parser::fixed_bytes);
177 test_TLVFixPacket_parsing<TestTLVPacket24>( UInt24Parser::fixed_bytes);
178 test_TLVFixPacket_parsing<TestTLVPacket32>( UInt32Parser::fixed_bytes);
182 template <class TLVFixPacketType>
183 void test_TLVFixPacket_creating()
185 std::string payload ("Hello, world!");
186 TLVFixPacketType tlvPacket (TLVFixPacketType::create());
187 tlvPacket->type() = 42u;
188 DataPacket::createAfter( tlvPacket, payload );
189 tlvPacket.finalizeAll();
191 BOOST_CHECK_EQUAL( tlvPacket->type(), 42u);
192 BOOST_CHECK_EQUAL( tlvPacket->length(), 13u);
194 PacketData & tlvPacket_value (tlvPacket.next().data());
195 BOOST_CHECK( equal( tlvPacket_value.begin(), tlvPacket_value.end(), payload.begin() ));
198 BOOST_AUTO_UNIT_TEST(TLVFixPacket_create_packet)
200 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt8Parser> > TestTLVPacket8;
201 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt16Parser> > TestTLVPacket16;
202 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt24Parser> > TestTLVPacket24;
203 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt32Parser> > TestTLVPacket32;
205 test_TLVFixPacket_creating<TestTLVPacket8>();
206 test_TLVFixPacket_creating<TestTLVPacket16>();
207 test_TLVFixPacket_creating<TestTLVPacket24>();
208 test_TLVFixPacket_creating<TestTLVPacket32>();
212 template <class TLVFixPacketType>
213 void test_invalid_TLVFixPacket_creating(boost::uint32_t max_value)
215 TLVFixPacketType tlvPacket (TLVFixPacketType::create());
216 tlvPacket->type() = 42u;
217 DataPacket payload (DataPacket::createAfter( tlvPacket, max_value+1));
218 //DataPacket::createAfter( payload, 1); // this is one byte to much.
219 BOOST_CHECK_THROW( tlvPacket.finalizeAll(), UnsuportedTLVPacketException);
222 BOOST_AUTO_UNIT_TEST(TLVFixPacket_create_invalid_packet)
224 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt8Parser> > TestTLVPacket8;
225 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt16Parser> > TestTLVPacket16;
226 typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt24Parser> > TestTLVPacket24;
228 test_invalid_TLVFixPacket_creating<TestTLVPacket8> ( UInt8Parser::max_value);
229 test_invalid_TLVFixPacket_creating<TestTLVPacket16>( UInt16Parser::max_value);
230 test_invalid_TLVFixPacket_creating<TestTLVPacket24>( UInt24Parser::max_value);
234 ///////////////////////////////cc.e////////////////////////////////////////
241 // c-file-style: "senf"
242 // indent-tabs-mode: nil
243 // ispell-local-dictionary: "american"
244 // compile-command: "scons -u test"
245 // comment-column: 40