4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Thorsten Horstmann <tho@berlios.de>
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.
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.
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.
24 \brief GenericTLV unit tests */
27 #include "GenericTLV.hh"
28 #include <senf/Packets/DefaultBundle/IPv6Extensions.hh>
30 #include <senf/Utils/auto_unit_test.hh>
31 #include <boost/test/test_tools.hpp>
34 ///////////////////////////////cc.p////////////////////////////////////////
37 struct MyTLVParserBase
38 : public senf::PacketParserBase
40 # include SENF_PARSER()
41 SENF_PARSER_FIELD ( type, senf::UInt8Parser );
42 SENF_PARSER_FIELD_RO ( length, senf::UInt8Parser );
43 SENF_PARSER_FINALIZE ( MyTLVParserBase );
45 typedef senf::GenericTLVParserRegistry<MyTLVParserBase> Registry;
48 struct MyGenericTLVParser
49 : public senf::GenericTLVParserBase<MyTLVParserBase>
51 typedef senf::GenericTLVParserBase<MyTLVParserBase> base;
52 MyGenericTLVParser(data_iterator i, state_type s) : base(i,s) {}
55 struct MyConcreteTLVParser
56 : public MyTLVParserBase
58 # include SENF_PARSER()
59 SENF_PARSER_INHERIT ( MyTLVParserBase );
60 SENF_PARSER_FIELD ( myValue, senf::UInt32Parser );
61 SENF_PARSER_FINALIZE ( MyConcreteTLVParser );
67 static type_t::value_type const typeId = 0x42;
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";
78 struct MyConcrete2TLVParser
79 : public MyTLVParserBase
81 # include SENF_PARSER()
82 SENF_PARSER_INHERIT ( MyTLVParserBase );
83 SENF_PARSER_FIELD ( myValue, senf::UInt32Parser );
84 SENF_PARSER_FINALIZE ( MyConcrete2TLVParser );
90 static type_t::value_type const typeId = 0x47;
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";
101 class MyTestPacketParser
102 : public senf::PacketParserBase
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 );
110 struct MyTestPacketType
111 : public senf::PacketTypeBase,
112 public senf::PacketTypeMixin<MyTestPacketType>
114 typedef senf::PacketTypeMixin<MyTestPacketType> mixin;
115 typedef MyTestPacketParser parser;
117 using mixin::nextPacketRange;
119 using mixin::initSize;
121 typedef senf::ConcretePacket<MyTestPacketType> MyTestPacket;
125 SENF_AUTO_UNIT_TEST(GenericTLV_parser)
127 BOOST_CHECK_EQUAL( senf::init_bytes<MyGenericTLVParser>::value,
128 senf::init_bytes<MyTLVParserBase>::value) ;
130 unsigned char data[] = {
131 0x42, 0x04, // type, length
132 0x00, 0x01, 0x02, 0x03 // value
135 senf::DataPacket dataPacket ( senf::DataPacket::create(data));
136 MyGenericTLVParser genericTLVParser (dataPacket.data().begin(), &dataPacket.data());
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() );
147 std::vector<unsigned char> value (4, 0xab);
148 genericTLVParser.value( std::make_pair(0x42, value));
150 BOOST_CHECK( genericTLVParser.is<MyConcreteTLVParser>());
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 );
158 SENF_AUTO_UNIT_TEST(GenericTLV_packet)
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;
166 unsigned char data[] = {
167 0x01, // tlv list length
169 0x42, 0x04, // type, length
170 0x00, 0x00, 0xff, 0xff // value
172 SENF_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data),
173 p.data().begin(), p.data().end() );
177 SENF_AUTO_UNIT_TEST(GenericTLV_registry)
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;
186 std::stringstream ss;
187 (*tlvContainer.begin()).dump( ss);
188 BOOST_CHECK_EQUAL( ss.str().substr(0,58),
189 " GenericTLVParser<(anonymous namespace)::MyTLVParserBase>" );
191 senf::GenericTLVParserRegistry<MyTLVParserBase>::instance()
192 .registerParser<MyConcreteTLVParser>();
193 ss.str(""); ss.clear();
195 (*tlvContainer.begin()).dump( ss);
196 BOOST_CHECK_EQUAL( ss.str().substr(0,21), " MyConcreteTLVParser" );
199 SENF_AUTO_UNIT_TEST(GenericTLV_predicate)
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;
211 // typedef senf::IPv6HopByHopOptionsPacket::Parser::options_t::container optContainer_t;
212 // optContainer_t optC (p->tlv_list() );
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>()) ;
221 ///////////////////////////////cc.e////////////////////////////////////////
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"