b80d3fef67f06eb9fe13c55013640a7c5c4a0c39
[senf.git] / senf / Packets / GenericTLV.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
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 GenericTLV unit tests */
25
26 // Custom includes
27 #include "GenericTLV.hh"
28 #include <senf/Packets/DefaultBundle/IPv6Extensions.hh>
29
30 #include <senf/Utils/auto_unit_test.hh>
31 #include <boost/test/test_tools.hpp>
32
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 namespace {
37     struct MyTLVParserBase 
38         : public senf::PacketParserBase
39     {
40 #       include SENF_PARSER()
41         SENF_PARSER_FIELD    ( type,   senf::UInt8Parser );
42         SENF_PARSER_FIELD_RO ( length, senf::UInt8Parser );
43         SENF_PARSER_FINALIZE ( MyTLVParserBase           );
44         
45         typedef senf::GenericTLVParserRegistry<MyTLVParserBase> Registry;
46     };
47     
48     struct MyGenericTLVParser
49         : public senf::GenericTLVParserBase<MyTLVParserBase>
50     {
51         typedef senf::GenericTLVParserBase<MyTLVParserBase> base;
52         MyGenericTLVParser(data_iterator i, state_type s) : base(i,s) {}
53     };
54         
55    struct MyConcreteTLVParser
56        : public MyTLVParserBase
57    {
58    #   include SENF_PARSER()
59        SENF_PARSER_INHERIT  ( MyTLVParserBase             );
60        SENF_PARSER_FIELD    ( myValue, senf::UInt32Parser );
61        SENF_PARSER_FINALIZE ( MyConcreteTLVParser         );
62     
63        SENF_PARSER_INIT() {
64            type() = typeId;
65            length_() = 4;
66        }
67        static type_t::value_type const typeId = 0x42;
68        
69        void dump(std::ostream & os) const {
70            boost::io::ios_all_saver ias(os);
71            os << "  MyConcreteTLVParser\n"
72               << "    type:   " << senf::format::dumpint(type()) << "\n"
73               << "    length: " << senf::format::dumpint(length()) << "\n"
74               << "    value:  " << senf::format::dumpint(myValue()) << "\n";
75        }
76    };
77    
78    struct MyConcrete2TLVParser
79        : public MyTLVParserBase
80    {
81    #   include SENF_PARSER()
82        SENF_PARSER_INHERIT  ( MyTLVParserBase             );
83        SENF_PARSER_FIELD    ( myValue, senf::UInt32Parser );
84        SENF_PARSER_FINALIZE ( MyConcrete2TLVParser         );
85     
86        SENF_PARSER_INIT() {
87            type() = typeId;
88            length_() = 4;
89        }
90        static type_t::value_type const typeId = 0x47;
91        
92        void dump(std::ostream & os) const {
93            boost::io::ios_all_saver ias(os);
94            os << "  MyConcreteTLVParser\n"
95               << "    type:   " << senf::format::dumpint(type()) << "\n"
96               << "    length: " << senf::format::dumpint(length()) << "\n"
97               << "    value:  " << senf::format::dumpint(myValue()) << "\n";
98        }
99    };
100         
101     class MyTestPacketParser
102         : public senf::PacketParserBase
103     {
104 #       include SENF_PARSER()
105         SENF_PARSER_FIELD_RO ( list_length, senf::UInt8Parser );
106         SENF_PARSER_LIST     ( tlv_list, list_length, MyGenericTLVParser );
107         SENF_PARSER_FINALIZE ( MyTestPacketParser );
108     };
109         
110     struct MyTestPacketType
111         : public senf::PacketTypeBase,
112           public senf::PacketTypeMixin<MyTestPacketType>
113     {
114         typedef senf::PacketTypeMixin<MyTestPacketType> mixin;
115         typedef MyTestPacketParser parser;
116
117         using mixin::nextPacketRange;
118         using mixin::init;
119         using mixin::initSize;
120     };
121     typedef senf::ConcretePacket<MyTestPacketType> MyTestPacket;
122 }
123
124
125 BOOST_AUTO_UNIT_TEST(GenericTLV_parser)
126 {
127     BOOST_CHECK_EQUAL( senf::init_bytes<MyGenericTLVParser>::value, 
128             senf::init_bytes<MyTLVParserBase>::value) ;
129
130     unsigned char data[] = {
131             0x42, 0x04,             // type, length
132             0x00, 0x01, 0x02, 0x03  // value
133     };
134
135     senf::DataPacket dataPacket ( senf::DataPacket::create(data));
136     MyGenericTLVParser genericTLVParser (dataPacket.data().begin(), &dataPacket.data());
137
138     BOOST_CHECK_EQUAL( senf::bytes( genericTLVParser), sizeof(data) );
139     BOOST_CHECK_EQUAL( genericTLVParser.type(),   0x42 );
140     BOOST_CHECK_EQUAL( genericTLVParser.length(), 0x04 );
141     SENF_CHECK_EQUAL_COLLECTIONS( data+2, data+sizeof(data),
142             genericTLVParser.value().begin(), genericTLVParser.value().end() );
143     genericTLVParser.value( data );
144     SENF_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data),
145             genericTLVParser.value().begin(), genericTLVParser.value().end() );
146
147     std::vector<unsigned char> value (4, 0xab);   
148     genericTLVParser.value( std::make_pair(0x42, value));
149     
150     BOOST_CHECK( genericTLVParser.is<MyConcreteTLVParser>());
151
152     MyConcreteTLVParser concreteTLVParser ( genericTLVParser.as<MyConcreteTLVParser>());
153     BOOST_CHECK_EQUAL( concreteTLVParser.type(),   0x42 );
154     BOOST_CHECK_EQUAL( concreteTLVParser.length(), 0x04 );
155     BOOST_CHECK_EQUAL( concreteTLVParser.myValue(), 0xabababab );
156 }
157
158 BOOST_AUTO_UNIT_TEST(GenericTLV_packet)
159 {
160     MyTestPacket p ( MyTestPacket::create());
161     MyTestPacket::Parser::tlv_list_t::container tlvContainer (p->tlv_list() );
162     MyConcreteTLVParser tlv ( tlvContainer.push_back_space().init<MyConcreteTLVParser>());
163     tlv.myValue() << 0xffff;
164     p.finalizeThis();
165     
166     unsigned char data[] = {
167             0x01, // tlv list length
168             // first tlv:
169             0x42, 0x04,             // type, length
170             0x00, 0x00, 0xff, 0xff  // value
171     };
172     SENF_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data),
173             p.data().begin(), p.data().end() );
174 }
175
176
177 BOOST_AUTO_UNIT_TEST(GenericTLV_registry)
178 {
179     MyTestPacket p ( MyTestPacket::create());
180     MyTestPacket::Parser::tlv_list_t::container tlvContainer (p->tlv_list() );
181     MyConcreteTLVParser conreteTLVParser ( 
182             tlvContainer.push_back_space().init<MyConcreteTLVParser>());
183     conreteTLVParser.myValue() << 0xffff;
184     p.finalizeThis();
185         
186     std::stringstream ss;
187     (*tlvContainer.begin()).dump( ss);
188     BOOST_CHECK_EQUAL( ss.str().substr(0,58), 
189             "  GenericTLVParser<(anonymous namespace)::MyTLVParserBase>" );
190     
191     senf::GenericTLVParserRegistry<MyTLVParserBase>::instance()
192             .registerParser<MyConcreteTLVParser>();
193     ss.str(""); ss.clear();
194     
195     (*tlvContainer.begin()).dump( ss);
196     BOOST_CHECK_EQUAL( ss.str().substr(0,21), "  MyConcreteTLVParser" );
197 }
198
199 BOOST_AUTO_UNIT_TEST(GenericTLV_predicate)
200 {
201     MyTestPacket p ( MyTestPacket::create() );
202     MyTestPacket::Parser::tlv_list_t::container tlvContainer (p->tlv_list() );
203     MyConcreteTLVParser conreteTLVParser ( 
204             tlvContainer.push_back_space().init<MyConcreteTLVParser>());
205     conreteTLVParser.myValue() << 0xffff;
206     MyConcrete2TLVParser conreteTLVParser2 ( 
207             tlvContainer.push_back_space().init<MyConcrete2TLVParser>());
208     conreteTLVParser2.myValue() << 0xdddd;
209     p.finalizeThis();
210     
211 //     typedef senf::IPv6HopByHopOptionsPacket::Parser::options_t::container optContainer_t; 
212 //     optContainer_t optC (p->tlv_list() );
213     
214     MyTestPacket::Parser::tlv_list_t::container testTlvContainer (p->tlv_list() );
215     MyTestPacket::Parser::tlv_list_t::container::iterator it = std::find_if (
216       testTlvContainer.begin(), testTlvContainer.end(), 
217       senf::detail::Predicate< senf::GenericTLVParserBase<MyTLVParserBase>, MyConcreteTLVParser>() );
218     BOOST_CHECK( it->is<MyConcreteTLVParser>()) ;
219 }
220
221 ///////////////////////////////cc.e////////////////////////////////////////
222 #undef prefix_
223
224 \f
225 // Local Variables:
226 // mode: c++
227 // fill-column: 100
228 // comment-column: 40
229 // c-file-style: "senf"
230 // indent-tabs-mode: nil
231 // ispell-local-dictionary: "american"
232 // compile-command: "scons -u test"
233 // End: