6d143d68c988938fd917eee00d3306fc0f1c1301
[senf.git] / senf / 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 /** \file
24     \brief TLVPacket unit tests */
25
26 //#include "TLVPacket.test.hh"
27 //#include "TLVPacket.test.ih"
28
29 // Custom includes
30 #include "TLVPacket.hh"
31 #include <senf/Packets/DefaultBundle/EthernetPacket.hh>
32
33 #include <senf/Utils/auto_unit_test.hh>
34 #include <boost/test/test_tools.hpp>
35
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 using namespace senf;
40
41 namespace {
42
43 #define CHECK_TLVPacket(tlvPacket, ptype, plength)                            \
44 {                                                                             \
45     BOOST_CHECK_EQUAL( tlvPacket->type(),         ptype   );                  \
46     BOOST_CHECK_EQUAL( tlvPacket->length(),       plength );                  \
47     BOOST_CHECK_EQUAL( tlvPacket->value().size(), int(plength) );             \
48     std::ostringstream oss (std::ostringstream::out);                         \
49     SENF_CHECK_NO_THROW( tlvPacket.dump( oss));                               \
50     senf::PacketData::iterator dataIterator (tlvPacket->value().begin());     \
51     for (unsigned i=0; i<plength; i++) {                                      \
52         BOOST_CHECK_EQUAL( *dataIterator, i );                                \
53         dataIterator++;                                                       \
54     }                                                                         \
55 }
56 }
57
58
59 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_parse_packet_with_simple_length)
60 {
61     unsigned char data[] = {
62         0x01, // type
63         0x0A, // first bit not set, length=10
64         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value
65     };
66     GenericTLVPacket tlvPacket (GenericTLVPacket::create(data));
67     CHECK_TLVPacket( tlvPacket, 0x01, 0x0Au );
68 }
69
70
71 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_parse_packet_with_extended_length)
72 {
73     unsigned char data[] = {
74         0x01, // type
75         0x81, // first and last bit set => one byte length following
76         0x0A, // length (10 = 138 bytes value follows)
77         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 
78         0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 
79         0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
80         0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 
81         0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 
82         0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
83         0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 
84         0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 
85         0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 
86         0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 
87         0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
88         0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 
89         0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 
90         0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89
91     };
92     GenericTLVPacket tlvPacket (GenericTLVPacket::create(data));
93     CHECK_TLVPacket( tlvPacket, 0x01, 0x8au );
94 }
95
96
97 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_packet_with_simple_length)
98 {
99     unsigned char value[] = {
100            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09
101     };
102     GenericTLVPacket tlvPacket (GenericTLVPacket::create());
103     tlvPacket->type() = 42u;
104     tlvPacket->value( value);
105     tlvPacket.finalizeThis();
106
107     CHECK_TLVPacket( tlvPacket, 42u, 0x0Au );
108
109     unsigned char data[] = {
110         0x2a, // type
111         0x0A, // first bit not set, length=10
112         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value
113     };
114     BOOST_CHECK( equal( tlvPacket.data().begin(), tlvPacket.data().end(), data ));
115 }
116
117
118 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_packet_with_extended_length)
119 {
120     unsigned char value[255];
121     for (unsigned i=0; i<sizeof(value); i++)
122         value[i] = i;
123     GenericTLVPacket tlvPacket (GenericTLVPacket::create());
124     tlvPacket->maxLengthValue( DynamicTLVLengthParser::max_value);
125     SENF_CHECK_NO_THROW( tlvPacket->type() = 42u);
126     SENF_CHECK_NO_THROW( tlvPacket->value( value));
127     SENF_CHECK_NO_THROW( tlvPacket.finalizeThis());
128
129     CHECK_TLVPacket( tlvPacket, 42u, sizeof(value) );
130
131     unsigned char data[] = {
132         0x2a, // type
133         0x81, // first and last bit set => one byte length following
134         0x7f, // length (127 = 255 bytes value)
135         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // first bytes of value
136     };
137     BOOST_CHECK( equal(
138             tlvPacket.data().begin(),
139             boost::next( tlvPacket.data().begin(), sizeof(data)),
140             data ));
141 }
142
143
144 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_invalid_packet)
145 {
146     GenericTLVPacket tlvPacket (GenericTLVPacket::create());
147     tlvPacket->type() = 42u;
148     tlvPacket.finalizeThis();
149
150     unsigned char value[255];
151     for (unsigned i=0; i<sizeof(value); i++)
152         value[i] = i;
153
154     BOOST_CHECK_THROW( tlvPacket->value( value), TLVLengthException);
155     tlvPacket->maxLengthValue( sizeof(value));
156     tlvPacket->value( value);
157     tlvPacket.finalizeThis();
158     CHECK_TLVPacket( tlvPacket, 42u, sizeof(value) );
159 }
160
161
162 namespace {
163
164     struct TestMacAddressTLVPacketParser : public BaseTLVPacketParser
165     {
166     #   include SENF_PARSER()
167         SENF_PARSER_INHERIT ( BaseTLVPacketParser );
168         SENF_PARSER_VECTOR  ( value, bytes(length), senf::MACAddressParser );
169         SENF_PARSER_FINALIZE( TestMacAddressTLVPacketParser );
170     };
171
172     struct TestMacAddressTLVPacketType
173         : public PacketTypeBase,
174           public PacketTypeMixin<TestMacAddressTLVPacketType>
175     {
176         typedef PacketTypeMixin<TestMacAddressTLVPacketType> mixin;
177         typedef TestMacAddressTLVPacketParser parser;
178
179         using mixin::nextPacketRange;
180         using mixin::init;
181         using mixin::initSize;
182
183         static void finalize(ConcretePacket<TestMacAddressTLVPacketType> p) {
184             p->finalizeLength();
185         }
186     };
187     typedef ConcretePacket<TestMacAddressTLVPacketType> TestMacAddressTLVPacket;
188 }
189
190 BOOST_AUTO_UNIT_TEST(TestMacAddressTLVPacket_create)
191 {
192     TestMacAddressTLVPacket tlvPacket (TestMacAddressTLVPacket::create());
193     tlvPacket->type() = 42;
194     tlvPacket->value().push_back( senf::MACAddress::from_string("01:23:45:67:89:ab") );
195     tlvPacket->value().push_back( senf::MACAddress::from_string("cd:ef:01:23:45:67") );
196     tlvPacket.finalizeThis();
197
198     unsigned char data[] = {
199         0x2a, // type
200         0x0c, // length
201         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67  // value
202     };
203     BOOST_CHECK( equal( tlvPacket.data().begin(), tlvPacket.data().end(), data ));
204 }
205
206
207 ///////////////////////////////cc.e////////////////////////////////////////
208 #undef prefix_
209
210
211 // Local Variables:
212 // mode: c++
213 // fill-column: 100
214 // c-file-style: "senf"
215 // indent-tabs-mode: nil
216 // ispell-local-dictionary: "american"
217 // compile-command: "scons -u test"
218 // comment-column: 40
219 // End: