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"
29 #include <senf/Utils/auto_unit_test.hh>
30 #include <boost/test/test_tools.hpp>
33 ///////////////////////////////cc.p////////////////////////////////////////
36 struct MyTLVParserBase
37 : public senf::PacketParserBase
39 # include SENF_PARSER()
40 SENF_PARSER_FIELD ( type, senf::UInt8Parser );
41 SENF_PARSER_FIELD_RO ( length, senf::UInt8Parser );
42 SENF_PARSER_FINALIZE ( MyTLVParserBase );
45 struct MyGenericTLVParser
46 : public senf::GenericTLVParserBase<MyTLVParserBase>
48 typedef senf::GenericTLVParserBase<MyTLVParserBase> base;
49 MyGenericTLVParser(data_iterator i, state_type s) : base(i,s) {}
52 struct MyConcreteTLVParser
53 : public MyTLVParserBase
55 # include SENF_PARSER()
56 SENF_PARSER_INHERIT ( MyTLVParserBase );
57 SENF_PARSER_FIELD ( myValue, senf::UInt32Parser );
58 SENF_PARSER_FINALIZE ( MyConcreteTLVParser );
64 static type_t::value_type const typeId = 0x42;
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";
75 class MyTestPacketParser
76 : public senf::PacketParserBase
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 );
84 struct MyTestPacketType
85 : public senf::PacketTypeBase,
86 public senf::PacketTypeMixin<MyTestPacketType>
88 typedef senf::PacketTypeMixin<MyTestPacketType> mixin;
89 typedef MyTestPacketParser parser;
91 using mixin::nextPacketRange;
93 using mixin::initSize;
95 typedef senf::ConcretePacket<MyTestPacketType> MyTestPacket;
99 BOOST_AUTO_UNIT_TEST(GenericTLV_parser)
101 BOOST_CHECK_EQUAL( senf::init_bytes<MyGenericTLVParser>::value,
102 senf::init_bytes<MyTLVParserBase>::value) ;
104 unsigned char data[] = {
105 0x42, 0x04, // type, length
106 0x00, 0x01, 0x02, 0x03 // value
109 senf::DataPacket dataPacket ( senf::DataPacket::create(data));
110 MyGenericTLVParser genericTLVParser (dataPacket.data().begin(), &dataPacket.data());
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() );
121 std::vector<unsigned char> value (4, 0xab);
122 genericTLVParser.value( std::make_pair(0x42, value));
124 BOOST_CHECK( genericTLVParser.is<MyConcreteTLVParser>());
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 );
132 BOOST_AUTO_UNIT_TEST(GenericTLV_packet)
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;
140 unsigned char data[] = {
141 0x01, // tlv list length
143 0x42, 0x04, // type, length
144 0x00, 0x00, 0xff, 0xff // value
146 SENF_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data),
147 p.data().begin(), p.data().end() );
151 BOOST_AUTO_UNIT_TEST(GenericTLV_registry)
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;
160 std::stringstream ss;
161 (*tlvContainer.begin()).dump( ss);
162 BOOST_CHECK_EQUAL( ss.str().substr(0,58),
163 " GenericTLVParser<(anonymous namespace)::MyTLVParserBase>" );
165 senf::GenericTLVParserRegistry<MyTLVParserBase>::instance()
166 .registerParser<MyConcreteTLVParser>();
167 ss.str(""); ss.clear();
169 (*tlvContainer.begin()).dump( ss);
170 BOOST_CHECK_EQUAL( ss.str().substr(0,21), " MyConcreteTLVParser" );
174 ///////////////////////////////cc.e////////////////////////////////////////
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"