b5c49494c4b14a6044685c4c08000af80ee53a9f
[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 "../DefaultBundle/EthernetPacket.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 namespace {
41
42 void check_TLVPacket(GenericTLVPacket &tlvPacket, boost::uint8_t type, boost::uint32_t length)
43 {
44     BOOST_CHECK_EQUAL( tlvPacket->type(),         type   );
45     BOOST_CHECK_EQUAL( tlvPacket->length(),       length );
46     BOOST_CHECK_EQUAL( tlvPacket->value().size(), int(length) );
47     senf::PacketData::iterator dataIterator (tlvPacket->value().begin());
48     for (unsigned i=0; i<length; i++) {
49         BOOST_CHECK_EQUAL( *dataIterator, i );
50         dataIterator++;
51     }
52 }
53 }
54
55
56 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_parse_packet_with_simple_length)
57 {
58     unsigned char data[] = {
59         0x01, // type
60         0x0A, // first bit not set, length=10
61         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value
62     };
63     GenericTLVPacket tlvPacket (GenericTLVPacket::create(data));
64     check_TLVPacket( tlvPacket, 0x01, 0x0Au );
65 }
66
67
68 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_parse_packet_with_extended_length)
69 {
70     unsigned char data[] = {
71         0x01, // type
72         0x81, // first and last bit set => one byte length following
73         0x0A, // length (10 bytes value)
74         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value
75     };
76     GenericTLVPacket tlvPacket (GenericTLVPacket::create(data));
77     check_TLVPacket( tlvPacket, 0x01, 0x0Au );
78 }
79
80
81 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_packet_with_simple_length)
82 {
83     unsigned char value[] = {
84            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09
85     };
86     GenericTLVPacket tlvPacket (GenericTLVPacket::create());
87     tlvPacket->type() = 42u;
88     tlvPacket->value( value);
89     tlvPacket.finalizeThis();
90
91     check_TLVPacket( tlvPacket, 42u, 0x0Au );
92
93     unsigned char data[] = {
94         0x2a, // type
95         0x0A, // first bit not set, length=10
96         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value
97     };
98     BOOST_CHECK( equal( tlvPacket.data().begin(), tlvPacket.data().end(), data ));
99 }
100
101
102 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_packet_with_extended_length)
103 {
104     unsigned char value[255];
105     for (unsigned i=0; i<sizeof(value); i++)
106         value[i] = i;
107     GenericTLVPacket tlvPacket (GenericTLVPacket::create());
108     tlvPacket->type() = 42u;
109     tlvPacket->value( value);
110     tlvPacket.finalizeThis();
111
112     check_TLVPacket( tlvPacket, 42u, sizeof(value) );
113
114     unsigned char data[] = {
115         0x2a, // type
116         0x81, // first and last bit set => one byte length following
117         0xff, // length (255 bytes value)
118         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // first bytes of value
119     };
120     BOOST_CHECK( equal(
121             tlvPacket.data().begin(),
122             boost::next( tlvPacket.data().begin(), sizeof(data)),
123             data ));
124 }
125
126
127 BOOST_AUTO_UNIT_TEST(GenericTLVPacket_create_invalid_packet)
128 {
129     GenericTLVPacket tlvPacket (GenericTLVPacket::create());
130     tlvPacket->type() = 42u;
131     tlvPacket.finalizeThis();
132
133     unsigned char value[255];
134     for (unsigned i=0; i<sizeof(value); i++)
135         value[i] = i;
136
137     BOOST_CHECK_THROW( tlvPacket->value( value), TLVLengthException);
138     tlvPacket->maxLengthValue( sizeof(value));
139     tlvPacket->value( value);
140     tlvPacket.finalizeThis();
141     check_TLVPacket( tlvPacket, 42u, sizeof(value) );
142 }
143
144
145 namespace {
146
147     struct TestMacAddressTLVPacketParser : public BaseTLVPacketParser
148     {
149     #   include SENF_PARSER()
150         SENF_PARSER_INHERIT ( BaseTLVPacketParser );
151         SENF_PARSER_VECTOR  ( value, bytes(length), senf::MACAddressParser );
152         SENF_PARSER_FINALIZE( TestMacAddressTLVPacketParser );
153     };
154
155     struct TestMacAddressTLVPacketType
156         : public PacketTypeBase,
157           public PacketTypeMixin<TestMacAddressTLVPacketType>
158     {
159         typedef PacketTypeMixin<TestMacAddressTLVPacketType> mixin;
160         typedef TestMacAddressTLVPacketParser parser;
161
162         using mixin::nextPacketRange;
163         using mixin::init;
164         using mixin::initSize;
165
166         static void finalize(ConcretePacket<TestMacAddressTLVPacketType> p) {
167             p->shrinkLength();
168         }
169     };
170     typedef ConcretePacket<TestMacAddressTLVPacketType> TestMacAddressTLVPacket;
171 }
172
173 BOOST_AUTO_UNIT_TEST(TestMacAddressTLVPacket_create)
174 {
175     TestMacAddressTLVPacket tlvPacket (TestMacAddressTLVPacket::create());
176     tlvPacket->type() = 42;
177     tlvPacket->value().push_back( senf::MACAddress::from_string("01:23:45:67:89:ab") );
178     tlvPacket->value().push_back( senf::MACAddress::from_string("cd:ef:01:23:45:67") );
179     tlvPacket.finalizeThis();
180
181     unsigned char data[] = {
182         0x2a, // type
183         0x0c, // length
184         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67  // value
185     };
186     BOOST_CHECK( equal( tlvPacket.data().begin(), tlvPacket.data().end(), data ));
187 }
188
189
190 ///////////////////////////////cc.e////////////////////////////////////////
191 #undef prefix_
192
193
194 // Local Variables:
195 // mode: c++
196 // fill-column: 100
197 // c-file-style: "senf"
198 // indent-tabs-mode: nil
199 // ispell-local-dictionary: "american"
200 // compile-command: "scons -u test"
201 // comment-column: 40
202 // End: