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