4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
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/Utils/hexdump.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 );
46 struct MyGenericTLVParser
47 : public senf::GenericTLVParserBase<MyTLVParserBase>
49 typedef senf::GenericTLVParserBase<MyTLVParserBase> base;
50 MyGenericTLVParser(data_iterator i, state_type s) : base(i,s) {}
53 struct MyConcreteTLVParser
54 : public MyTLVParserBase
56 # include SENF_PARSER()
57 SENF_PARSER_INHERIT ( MyTLVParserBase );
58 SENF_PARSER_FIELD ( myValue, senf::UInt32Parser );
59 SENF_PARSER_FINALIZE ( MyConcreteTLVParser );
65 static const type_t::value_type TYPEID = 0x42;
68 class MyTestPacketParser
69 : public senf::PacketParserBase
71 # include SENF_PARSER()
72 SENF_PARSER_FIELD_RO ( list_length, senf::UInt8Parser );
73 SENF_PARSER_LIST ( tlv_list, list_length, MyGenericTLVParser );
74 SENF_PARSER_FINALIZE ( MyTestPacketParser );
77 struct MyTestPacketType
78 : public senf::PacketTypeBase,
79 public senf::PacketTypeMixin<MyTestPacketType>
81 typedef senf::PacketTypeMixin<MyTestPacketType> mixin;
82 typedef MyTestPacketParser parser;
84 using mixin::nextPacketRange;
86 using mixin::initSize;
88 typedef senf::ConcretePacket<MyTestPacketType> MyTestPacket;
92 BOOST_AUTO_UNIT_TEST(GenericTLV_parser)
94 BOOST_CHECK_EQUAL( senf::init_bytes<MyGenericTLVParser>::value,
95 senf::init_bytes<MyTLVParserBase>::value) ;
97 unsigned char data[] = {
98 0x42, 0x04, // type, length
99 0x00, 0x01, 0x02, 0x03 // value
102 senf::DataPacket dataPacket ( senf::DataPacket::create(data));
103 MyGenericTLVParser genericTLVParser (dataPacket.data().begin(), &dataPacket.data());
105 BOOST_CHECK_EQUAL( senf::bytes( genericTLVParser), sizeof(data) );
106 BOOST_CHECK_EQUAL( genericTLVParser.type(), 0x42 );
107 BOOST_CHECK_EQUAL( genericTLVParser.length(), 0x04 );
108 SENF_CHECK_EQUAL_COLLECTIONS( data+2, data+sizeof(data),
109 genericTLVParser.value().begin(), genericTLVParser.value().end() );
110 genericTLVParser.value( data );
111 SENF_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data),
112 genericTLVParser.value().begin(), genericTLVParser.value().end() );
114 std::vector<unsigned char> value (4, 0xab);
115 genericTLVParser.value( std::make_pair(0x42, value));
117 BOOST_CHECK( genericTLVParser.is<MyConcreteTLVParser>());
119 MyConcreteTLVParser concreteTLVParser ( genericTLVParser.as<MyConcreteTLVParser>());
120 BOOST_CHECK_EQUAL( concreteTLVParser.type(), 0x42 );
121 BOOST_CHECK_EQUAL( concreteTLVParser.length(), 0x04 );
122 BOOST_CHECK_EQUAL( concreteTLVParser.myValue(), 0xabababab );
125 BOOST_AUTO_UNIT_TEST(GenericTLV_packet)
127 MyTestPacket p ( MyTestPacket::create());
128 MyTestPacket::Parser::tlv_list_t::container tlvContainer (p->tlv_list() );
129 MyConcreteTLVParser tlv ( tlvContainer.push_back_space().init<MyConcreteTLVParser>());
130 tlv.myValue() << 0xffff;
133 unsigned char data[] = {
134 0x01, // tlv list length
136 0x42, 0x04, // type, length
137 0x00, 0x00, 0xff, 0xff // value
139 SENF_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data),
140 p.data().begin(), p.data().end() );
144 ///////////////////////////////cc.e////////////////////////////////////////
151 // comment-column: 40
152 // c-file-style: "senf"
153 // indent-tabs-mode: nil
154 // ispell-local-dictionary: "american"
155 // compile-command: "scons -u test"