Packets: first steps for a generic TLV base class
[senf.git] / senf / Packets / GenericTLV.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     @AUTHOR@
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 #include <senf/Utils/hexdump.hh>
29
30 #include <senf/Utils/auto_unit_test.hh>
31 #include <boost/test/test_tools.hpp>
32
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 namespace {
37     struct MyTLVParserBase 
38         : public senf::PacketParserBase
39     {
40 #       include SENF_PARSER()
41         SENF_PARSER_FIELD    ( type,   senf::UInt8Parser );
42         SENF_PARSER_FIELD_RO ( length, senf::UInt8Parser );
43         SENF_PARSER_FINALIZE ( MyTLVParserBase           );
44     };
45     
46     struct MyGenericTLVParser
47         : public senf::GenericTLVParserBase<MyTLVParserBase>
48     {
49         typedef senf::GenericTLVParserBase<MyTLVParserBase> base;
50         MyGenericTLVParser(data_iterator i, state_type s) : base(i,s) {}
51     };
52         
53    struct MyConcreteTLVParser
54        : public MyTLVParserBase
55    {
56    #   include SENF_PARSER()
57        SENF_PARSER_INHERIT  ( MyTLVParserBase             );
58        SENF_PARSER_FIELD    ( myValue, senf::UInt32Parser );
59        SENF_PARSER_FINALIZE ( MyConcreteTLVParser         );
60     
61        SENF_PARSER_INIT() {
62            type() = TYPEID;
63            length_() = 4;
64        }        
65        static const type_t::value_type TYPEID = 0x42;
66    };
67         
68     class MyTestPacketParser
69         : public senf::PacketParserBase
70     {
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 );
75     };
76         
77     struct MyTestPacketType
78         : public senf::PacketTypeBase,
79           public senf::PacketTypeMixin<MyTestPacketType>
80     {
81         typedef senf::PacketTypeMixin<MyTestPacketType> mixin;
82         typedef MyTestPacketParser parser;
83
84         using mixin::nextPacketRange;
85         using mixin::init;
86         using mixin::initSize;
87     };
88     typedef senf::ConcretePacket<MyTestPacketType> MyTestPacket;
89 }
90
91
92 BOOST_AUTO_UNIT_TEST(GenericTLV_parser)
93 {
94     BOOST_CHECK_EQUAL( senf::init_bytes<MyGenericTLVParser>::value, 
95             senf::init_bytes<MyTLVParserBase>::value) ;
96
97     unsigned char data[] = {
98             0x42, 0x04,             // type, length
99             0x00, 0x01, 0x02, 0x03  // value
100     };
101
102     senf::DataPacket dataPacket ( senf::DataPacket::create(data));
103     MyGenericTLVParser genericTLVParser (dataPacket.data().begin(), &dataPacket.data());
104
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() );
113
114     std::vector<unsigned char> value (4, 0xab);   
115     genericTLVParser.value( std::make_pair(0x42, value));
116     
117     BOOST_CHECK( genericTLVParser.is<MyConcreteTLVParser>());
118
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 );
123 }
124
125 BOOST_AUTO_UNIT_TEST(GenericTLV_packet)
126 {
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;
131     p.finalizeThis();
132     
133     unsigned char data[] = {
134             0x01, // tlv list length
135             // first tlv:
136             0x42, 0x04,             // type, length
137             0x00, 0x00, 0xff, 0xff  // value
138     };
139     SENF_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data),
140             p.data().begin(), p.data().end() );
141 }
142
143
144 ///////////////////////////////cc.e////////////////////////////////////////
145 #undef prefix_
146
147 \f
148 // Local Variables:
149 // mode: c++
150 // fill-column: 100
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"
156 // End: