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