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