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