Utils/Termlib: Implement LineEditor auxiliary display support
[senf.git] / Packets / 80221Bundle / 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
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 using namespace senf;
39
40
41 void check_TLVPacket(GenericTLVPacket &tlvPacket, boost::uint8_t type, boost::uint32_t length)
42 {
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 );
49         dataIterator++;
50     }
51 }
52
53
54 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_parse_packet_with_simple_length)
55 {
56     unsigned char data[] = { 
57         0x01, // type
58         0x0A, // first bit not set, length=10
59         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value
60     };
61     GenericTLVPacket tlvPacket (GenericTLVPacket::create(data));
62     check_TLVPacket( tlvPacket, 0x01, 0x0Au );
63 }
64
65
66 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_parse_packet_with_extended_length)
67 {
68     unsigned char data[] = { 
69         0x01, // type
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
73     };        
74     GenericTLVPacket tlvPacket (GenericTLVPacket::create(data));
75     check_TLVPacket( tlvPacket, 0x01, 0x0Au );
76 }
77
78
79 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_packet_with_simple_length)
80 {
81     unsigned char value[] = { 
82            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09
83     };
84     GenericTLVPacket tlvPacket (GenericTLVPacket::create());
85     tlvPacket->type() = 42u;
86     tlvPacket->value( value);
87     tlvPacket.finalizeThis();
88
89     check_TLVPacket( tlvPacket, 42u, 0x0Au );
90     
91     unsigned char data[] = { 
92         0x2a, // type
93         0x0A, // first bit not set, length=10
94         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value
95     };
96     BOOST_CHECK( equal( tlvPacket.data().begin(), tlvPacket.data().end(), data ));
97 }
98
99 #if 0
100 BOOST_AUTO_UNIT_TEST(TLVPacket_create_packet_with_extended_length)
101 {
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();
107
108     check_TLVPacket( tlvPacket, 42u, 129u );
109 }
110
111
112 BOOST_AUTO_UNIT_TEST(TLVPacket_create_invalid_packet)
113 {
114     
115 }
116
117
118 BOOST_AUTO_UNIT_TEST(TLVFixPacket_static)
119 {
120     // check static values:
121     typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt8Parser> > TestTLVPacket8;
122     typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt24Parser> > TestTLVPacket24;
123     
124     BOOST_CHECK_EQUAL( TestTLVPacket8::type::initSize(),  4+1u );
125     BOOST_CHECK_EQUAL( TestTLVPacket24::type::initSize(), 4+3u );
126 }
127
128
129 template <class TLVFixPacketType>
130 void test_TLVFixPacket_parsing(unsigned lengthParser_size)
131 {
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
138     }
139     TLVFixPacketType tlvPacket (TLVFixPacketType::create(
140             boost::make_iterator_range(data.begin(), data.end())));
141     check_TLVPacket( tlvPacket, 0x01234567u, 0x0Au );
142 }
143
144 BOOST_AUTO_UNIT_TEST(TLVFixPacket_parse_packet)
145 {
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;
150     
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);
155 }
156
157
158 template <class TLVFixPacketType>
159 void test_TLVFixPacket_creating()
160 {
161     std::string payload ("Hello, world!");
162     TLVFixPacketType tlvPacket (TLVFixPacketType::create());
163     tlvPacket->type() = 42u;
164     DataPacket::createAfter( tlvPacket, payload );
165     tlvPacket.finalizeAll();
166
167     BOOST_CHECK_EQUAL( tlvPacket->type(), 42u);
168     BOOST_CHECK_EQUAL( tlvPacket->length(), 13u);
169     
170     PacketData & tlvPacket_value (tlvPacket.next().data());
171     BOOST_CHECK( equal( tlvPacket_value.begin(), tlvPacket_value.end(), payload.begin() ));
172 }
173
174 BOOST_AUTO_UNIT_TEST(TLVFixPacket_create_packet)
175 {
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;
180     
181     test_TLVFixPacket_creating<TestTLVPacket8>();
182     test_TLVFixPacket_creating<TestTLVPacket16>();
183     test_TLVFixPacket_creating<TestTLVPacket24>();
184     test_TLVFixPacket_creating<TestTLVPacket32>();
185 }
186
187
188 template <class TLVFixPacketType>
189 void test_invalid_TLVFixPacket_creating(boost::uint32_t max_value)
190 {
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);
196 }
197
198 BOOST_AUTO_UNIT_TEST(TLVFixPacket_create_invalid_packet)
199 {
200     typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt8Parser> > TestTLVPacket8;
201     typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt16Parser> > TestTLVPacket16;
202     typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt24Parser> > TestTLVPacket24;
203     
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);
207 }
208
209 #endif
210
211 ///////////////////////////////cc.e////////////////////////////////////////
212 #undef prefix_
213
214 \f
215 // Local Variables:
216 // mode: c++
217 // fill-column: 100
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
223 // End: