more work on TLV & MIH Packet
[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 #include <vector>
35
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 using namespace senf;
40
41
42 void check_TLVPacket(GenericTLVPacket &tlvPacket, boost::uint32_t type, boost::uint32_t length)
43 {
44     BOOST_CHECK_EQUAL( tlvPacket->type(), type );
45     BOOST_CHECK_EQUAL( tlvPacket->length(), length );
46
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 );
50 }
51
52
53 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_static)
54 {
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 );
58 }
59
60
61 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_parse_packet_with_simple_length)
62 {
63     unsigned char data[] = { 
64         0x01, // type
65         0x0A, // first bit not set, length=10
66         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value
67     };
68     GenericTLVPacket tlvPacket (GenericTLVPacket::create(data));
69     check_TLVPacket( tlvPacket, 0x01, 0x0Au );
70 }
71
72 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_parse_packet_with_extended_length)
73 {
74     unsigned char data[] = { 
75         0x01, // type
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
79     };        
80     GenericTLVPacket tlvPacket (GenericTLVPacket::create(data));
81     check_TLVPacket( tlvPacket, 0x01, 0x0Au );
82 }
83
84
85 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_packet_with_simple_length)
86 {
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();
92
93     check_TLVPacket( tlvPacket, 42u, 0x0Au );
94 }
95
96 /**
97 BOOST_AUTO_UNIT_TEST(TLVPacket_create_packet_with_extended_length)
98 {
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();
104
105     check_TLVPacket( tlvPacket, 42u, 129u );
106 }
107
108
109 BOOST_AUTO_UNIT_TEST(TLVPacket_create_invalid_packet)
110 {
111     
112 }
113
114
115 BOOST_AUTO_UNIT_TEST(TLVFixPacket_static)
116 {
117     // check static values:
118     typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt8Parser> > TestTLVPacket8;
119     typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt24Parser> > TestTLVPacket24;
120     
121     BOOST_CHECK_EQUAL( TestTLVPacket8::type::initSize(),  4+1u );
122     BOOST_CHECK_EQUAL( TestTLVPacket24::type::initSize(), 4+3u );
123 }
124
125
126 template <class TLVFixPacketType>
127 void test_TLVFixPacket_parsing(unsigned lengthParser_size)
128 {
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
135     }
136     TLVFixPacketType tlvPacket (TLVFixPacketType::create(
137             boost::make_iterator_range(data.begin(), data.end())));
138     check_TLVPacket( tlvPacket, 0x01234567u, 0x0Au );
139 }
140
141 BOOST_AUTO_UNIT_TEST(TLVFixPacket_parse_packet)
142 {
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;
147     
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);
152 }
153
154
155 template <class TLVFixPacketType>
156 void test_TLVFixPacket_creating()
157 {
158     std::string payload ("Hello, world!");
159     TLVFixPacketType tlvPacket (TLVFixPacketType::create());
160     tlvPacket->type() = 42u;
161     DataPacket::createAfter( tlvPacket, payload );
162     tlvPacket.finalizeAll();
163
164     BOOST_CHECK_EQUAL( tlvPacket->type(), 42u);
165     BOOST_CHECK_EQUAL( tlvPacket->length(), 13u);
166     
167     PacketData & tlvPacket_value (tlvPacket.next().data());
168     BOOST_CHECK( equal( tlvPacket_value.begin(), tlvPacket_value.end(), payload.begin() ));
169 }
170
171 BOOST_AUTO_UNIT_TEST(TLVFixPacket_create_packet)
172 {
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;
177     
178     test_TLVFixPacket_creating<TestTLVPacket8>();
179     test_TLVFixPacket_creating<TestTLVPacket16>();
180     test_TLVFixPacket_creating<TestTLVPacket24>();
181     test_TLVFixPacket_creating<TestTLVPacket32>();
182 }
183
184
185 template <class TLVFixPacketType>
186 void test_invalid_TLVFixPacket_creating(boost::uint32_t max_value)
187 {
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);
193 }
194
195 BOOST_AUTO_UNIT_TEST(TLVFixPacket_create_invalid_packet)
196 {
197     typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt8Parser> > TestTLVPacket8;
198     typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt16Parser> > TestTLVPacket16;
199     typedef ConcretePacket<TLVPacketType<UInt32Parser, UInt24Parser> > TestTLVPacket24;
200     
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);
204 }
205
206 */
207
208
209 ///////////////////////////////cc.e////////////////////////////////////////
210 #undef prefix_
211
212 \f
213 // Local Variables:
214 // mode: c++
215 // fill-column: 100
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
221 // End: