Fixed whitespace in all files (no tabs)
[senf.git] / Packets / PacketRegistry.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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 // Unit tests
24
25 //#include "PacketRegistry.test.hh"
26 //#include "PacketRegistry.test.ih"
27
28 // Custom includes
29 #include <string>
30 #include "PacketRegistry.hh"
31 #include "DataPacket.hh"
32 #include "ParseInt.hh"
33
34 #include <boost/test/auto_unit_test.hpp>
35 #include <boost/test/test_tools.hpp>
36
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 namespace {
41
42     using namespace senf;
43
44     struct BaseTag {
45         typedef unsigned key_t;
46     };
47
48     struct StringTag {
49         typedef std::string key_t;
50     };
51
52     class BasePacket
53         : public Packet, public PacketRegistryMixin<BaseTag,BasePacket>
54     {
55         using PacketRegistryMixin<BaseTag,BasePacket>::registerInterpreter;
56     public:
57         typedef ptr_t<BasePacket>::ptr ptr;
58         typedef iterator byte_iterator;
59
60         typedef Parse_UInt16<iterator> Parse_Type;
61
62         Parse_Type type() const { return Parse_Type(begin()); }
63         static bool check(iterator b, iterator e) { return true; }
64
65     private:
66         template <class Arg>
67         BasePacket(Arg const & arg) : Packet(arg) {}
68
69         virtual void v_nextInterpreter() const
70             { registerInterpreter(type(), begin()+2, end()); }
71         virtual void v_finalize() {}
72         virtual void v_dump(std::ostream & os) const {}
73
74         friend class Packet;
75     };
76
77     class FooPacket : public Packet
78     {
79     public:
80         typedef ptr_t<FooPacket>::ptr ptr;
81         typedef iterator byte_iterator;
82
83         static bool check(iterator b, iterator e) { return true; }
84
85     private:
86         template <class Arg>
87         FooPacket(Arg const & arg) : Packet(arg) {}
88
89         virtual void v_nextInterpreter() const {}
90         virtual void v_finalize() {}
91         virtual void v_dump(std::ostream & os) const {}
92
93         friend class Packet;
94     };
95
96     class BarPacket : public Packet
97     {
98     public:
99         typedef ptr_t<BarPacket>::ptr ptr;
100         typedef iterator byte_iterator;
101
102         static bool check(iterator b, iterator e) { return true; }
103
104     private:
105         template <class Arg>
106         BarPacket(Arg const & arg) : Packet(arg) {}
107
108         virtual void v_nextInterpreter() const {}
109         virtual void v_finalize() {}
110         virtual void v_dump(std::ostream & os) const {}
111
112         friend class Packet;
113     };
114
115     namespace reg {
116         PacketRegistry<StringTag>::RegistrationProxy<FooPacket> registerFoo ("foo");
117         PacketRegistry<StringTag>::RegistrationProxy<BarPacket> registerBar ("bar");
118     }
119
120 }
121
122 BOOST_AUTO_UNIT_TEST(packetRegistry_test)
123 {
124     unsigned char data[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
125                              0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
126
127     {
128         BasePacket::ptr p (Packet::create<BasePacket>(data, data+sizeof(data)));
129         BOOST_CHECK( p->next()->is<DataPacket>() );
130     }
131
132     PacketRegistry<BaseTag>::registerPacket<FooPacket>(1u);
133     PacketRegistry<BaseTag>::registerPacket<BarPacket>(2u);
134
135     BOOST_CHECK_EQUAL( PacketRegistry<BaseTag>::key<FooPacket>(), 1u );
136     BOOST_CHECK_EQUAL( PacketRegistry<BaseTag>::key<BarPacket>(), 2u );
137     BOOST_CHECK_THROW( PacketRegistry<BaseTag>::key<DataPacket>(), PacketTypeNotRegistered );
138
139     {
140         BasePacket::ptr p (Packet::create<BasePacket>(data, data+sizeof(data)));
141         BOOST_CHECK( p->next()->is<FooPacket>() );
142     }
143
144     data[1] = 0x02;
145
146     {
147         BasePacket::ptr p (Packet::create<BasePacket>(data, data+sizeof(data)));
148         BOOST_CHECK( p->next()->is<BarPacket>() );
149     }
150
151     data[0] = 0x01;
152
153     {
154         BasePacket::ptr p (Packet::create<BasePacket>(data, data+sizeof(data)));
155         BOOST_CHECK( p->next()->is<DataPacket>() );
156     }
157
158     BOOST_CHECK_EQUAL( PacketRegistry<StringTag>::key<FooPacket>(), "foo" );
159     BOOST_CHECK( PacketRegistry<StringTag>::create("foo",data,data+sizeof(data))
160                  ->is<FooPacket>() );
161
162 }
163
164 ///////////////////////////////cc.e////////////////////////////////////////
165 #undef prefix_
166
167 \f
168 // Local Variables:
169 // mode: c++
170 // fill-column: 100
171 // c-file-style: "senf"
172 // indent-tabs-mode: nil
173 // ispell-local-dictionary: "american"
174 // End: