3ec7604f05cd92d435d860ec0163ea8bda4b0ae6
[senf.git] / Packets / MPEGDVBBundle / TLVPacket.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS) 
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY 
6 //     Thorsten Horstmann <tho@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 // Unit tests
24
25 //#include "TLVPacket.test.hh"
26 //#include "TLVPacket.test.ih"
27
28 // Custom includes
29 #include "TLVPacket.hh"
30 #include <senf/Packets.hh>
31
32 #include "../../Utils/auto_unit_test.hh"
33 #include <boost/test/test_tools.hpp>
34 #include <vector>
35
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 using namespace senf;
40
41
42 template <class TLVPacketType>
43 void check_TLVPacket(TLVPacketType tlvPacket, boost::uint32_t type, boost::uint32_t length)
44 {
45     BOOST_CHECK_EQUAL( tlvPacket->type(), type );
46     BOOST_CHECK_EQUAL( tlvPacket->length(), length );
47
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 );
52 }
53
54
55 BOOST_AUTO_UNIT_TEST(TLVPacket_static)
56 {
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 );
60 }
61
62
63 BOOST_AUTO_UNIT_TEST(TLVPacket_parse_packet_with_simple_length)
64 {
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)
70     };
71     TestTLVPacket tlvPacket (TestTLVPacket::create(data));
72     check_TLVPacket( tlvPacket, 0x01234567u, 0x0Au );
73 }
74
75
76 BOOST_AUTO_UNIT_TEST(TLVPacket_parse_packet_with_extended_length)
77 {
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)
84     };        
85     TestTLVPacket tlvPacket (TestTLVPacket::create(data));
86     check_TLVPacket( tlvPacket, 0x01234567u, 0x0Au );
87 }
88
89
90 BOOST_AUTO_UNIT_TEST(TLVPacket_create_packet_with_simple_length)
91 {
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.finalize();
98
99     BOOST_CHECK_EQUAL( tlvPacket->type(), 42u);
100     BOOST_CHECK_EQUAL( tlvPacket->length(), 13u);
101     
102     PacketData & tlvPacket_value (tlvPacket.next().data());
103     BOOST_CHECK( equal( tlvPacket_value.begin(), tlvPacket_value.end(), payload.begin() ));
104 }
105
106
107 BOOST_AUTO_UNIT_TEST(TLVPacket_create_packet_with_extended_length)
108 {
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.finalize();
117         
118     BOOST_CHECK_EQUAL( tlvPacket->type(), 42u );
119     BOOST_CHECK_EQUAL( tlvPacket->length(), payload.size() );
120     
121     PacketData & tlvPacket_value (tlvPacket.next().data());
122     BOOST_CHECK( equal( tlvPacket_value.begin(), tlvPacket_value.end(), payload.begin() ));
123
124     payload = std::string("This is a short string with less than 127 characters. That's all.");
125     DataPacket::createAfter( tlvPacket, payload );
126     tlvPacket.finalize();
127
128     BOOST_CHECK_EQUAL( tlvPacket->type(), 42u );
129     BOOST_CHECK_EQUAL( tlvPacket->length(), payload.size() );
130
131     PacketData & tlvPacket_value2 (tlvPacket.next().data());
132     BOOST_CHECK( equal( tlvPacket_value2.begin(), tlvPacket_value2.end(), payload.begin() ));
133 }
134
135
136 BOOST_AUTO_UNIT_TEST(TLVPacket_create_invalid_packet)
137 {
138     
139 }
140
141
142 BOOST_AUTO_UNIT_TEST(TLVFixPacket_static)
143 {
144     // check static values:
145     typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt8Parser> > TestTLVPacket8;
146     typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt24Parser> > TestTLVPacket24;
147     
148     BOOST_CHECK_EQUAL( TestTLVPacket8::type::initSize(),  4+1u );
149     BOOST_CHECK_EQUAL( TestTLVPacket24::type::initSize(), 4+3u );
150 }
151
152
153 template <class TLVFixPacketType>
154 void test_TLVFixPacket_parsing(unsigned lengthParser_size)
155 {
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
162     }
163     TLVFixPacketType tlvPacket (TLVFixPacketType::create(
164             boost::make_iterator_range(data.begin(), data.end())));
165     check_TLVPacket( tlvPacket, 0x01234567u, 0x0Au );
166 }
167
168 BOOST_AUTO_UNIT_TEST(TLVFixPacket_parse_packet)
169 {
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;
174     
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);
179 }
180
181
182 template <class TLVFixPacketType>
183 void test_TLVFixPacket_creating()
184 {
185     std::string payload ("Hello, world!");
186     TLVFixPacketType tlvPacket (TLVFixPacketType::create());
187     tlvPacket->type() = 42u;
188     DataPacket::createAfter( tlvPacket, payload );
189     tlvPacket.finalize();
190
191     BOOST_CHECK_EQUAL( tlvPacket->type(), 42u);
192     BOOST_CHECK_EQUAL( tlvPacket->length(), 13u);
193     
194     PacketData & tlvPacket_value (tlvPacket.next().data());
195     BOOST_CHECK( equal( tlvPacket_value.begin(), tlvPacket_value.end(), payload.begin() ));
196 }
197
198 BOOST_AUTO_UNIT_TEST(TLVFixPacket_create_packet)
199 {
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;
204     
205     test_TLVFixPacket_creating<TestTLVPacket8>();
206     test_TLVFixPacket_creating<TestTLVPacket16>();
207     test_TLVFixPacket_creating<TestTLVPacket24>();
208     test_TLVFixPacket_creating<TestTLVPacket32>();
209 }
210
211
212 template <class TLVFixPacketType>
213 void test_invalid_TLVFixPacket_creating(boost::uint32_t max_value)
214 {
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.finalize(), UnsuportedTLVPacketException);
220 }
221
222 BOOST_AUTO_UNIT_TEST(TLVFixPacket_create_invalid_packet)
223 {
224     typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt8Parser> > TestTLVPacket8;
225     typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt16Parser> > TestTLVPacket16;
226     typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt24Parser> > TestTLVPacket24;
227     
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);
231 }
232
233
234 ///////////////////////////////cc.e////////////////////////////////////////
235 #undef prefix_
236
237 \f
238 // Local Variables:
239 // mode: c++
240 // fill-column: 100
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
246 // End: