Utils/Logger: Implement direct syslog-via-UDP target
[senf.git] / Packets / VectorParser.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.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 "VectorParser.test.hh"
26 //#include "VectorParser.test.ih"
27
28 // Custom includes
29 #include "Packets.hh"
30
31 #include "../Utils/auto_unit_test.hh"
32 #include <boost/test/test_tools.hpp>
33 #include <boost/assign.hpp>
34
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 namespace {
39     struct VoidPacket : public senf::PacketTypeBase
40     {};
41 }
42
43 BOOST_AUTO_UNIT_TEST(VectorParser)
44 {
45     unsigned char data[] = { 0x03,                                   // size
46                              0x10, 0x11,  0x12, 0x13,  0x14, 0x15,   // data
47                              0x20, 0x21,  0x22, 0x23,  0x24, 0x25 };
48     senf::PacketInterpreterBase::ptr p (senf::PacketInterpreter<VoidPacket>::create(data));
49     typedef senf::VectorParser<
50         senf::UInt16Parser,
51         senf::detail::FixedAuxParserPolicy<senf::UInt8Parser, 1u>
52         > UInt16VectorParser;
53
54     {
55         UInt16VectorParser v (boost::next(p->data().begin(), 1), &p->data());
56         
57         BOOST_CHECK_EQUAL( v[0], 0x1011 );
58         BOOST_CHECK_EQUAL( v[2], 0x1415 );
59         BOOST_CHECK_EQUAL( v.size(), 3u );
60         BOOST_CHECK_EQUAL( v.bytes(), 6u );
61         BOOST_CHECK( ! v.empty() );
62         p->data()[0] = 0x06;
63         BOOST_CHECK_EQUAL( v.size(), 6u );
64         BOOST_CHECK_EQUAL( v.bytes(), 12u );
65         
66         UInt16VectorParser::iterator b (v.begin());
67         UInt16VectorParser::iterator e (v.end());
68         BOOST_CHECK_EQUAL(std::distance(b,e), UInt16VectorParser::difference_type(v.size()));
69     }
70
71     // Warning: Each of the following operations invalidate the parser -> we need to recreate it at
72     // each step
73
74     // And since all these members directly call the corresponding members in the container wrapper,
75     // we don't need to check them again below ...
76
77     {
78 #       define v UInt16VectorParser(boost::next(p->data().begin(),1),&p->data())
79
80         v.push_back(0xf0f1u,2);
81         BOOST_CHECK_EQUAL( v.size(), 8u );
82         BOOST_CHECK_EQUAL( v[7], 0xf0f1u );
83
84         v.push_back_space();
85         BOOST_CHECK_EQUAL( v.size(), 9u );
86         BOOST_CHECK_EQUAL( v[8], 0u );
87         
88         v.push_front(0xf3f4u);
89         BOOST_CHECK_EQUAL( v.size(), 10u );
90         BOOST_CHECK_EQUAL( v[0], 0xf3f4u );
91         BOOST_CHECK_EQUAL( v[1], 0x1011u );
92
93         v.push_front_space(2);
94         BOOST_CHECK_EQUAL( v.size(), 12u );
95         BOOST_CHECK_EQUAL( v[0], 0u );
96         BOOST_CHECK_EQUAL( v[1], 0u );
97         BOOST_CHECK_EQUAL( v[2], 0xf3f4u );
98         BOOST_CHECK_EQUAL( p->data().size(), 25u );
99
100         v.resize(4);
101         BOOST_CHECK_EQUAL( v.size(), 4u );
102         BOOST_CHECK_EQUAL( p->data().size(), 9u );
103         BOOST_CHECK_EQUAL( v[3], 0x1011u );
104
105         v.resize(6);
106         BOOST_CHECK_EQUAL( v.size(), 6u );
107         BOOST_CHECK_EQUAL( v[5], 0u );
108
109         v.resize(8,0xffff);
110         BOOST_CHECK_EQUAL( v.size(), 8u );
111         BOOST_CHECK_EQUAL( p->data().size(), 17u );
112         BOOST_CHECK_EQUAL( v[6], 0xffffu );
113
114 #       undef v
115     }
116 }
117
118 BOOST_AUTO_UNIT_TEST(VectorParser_wrapper)
119 {
120     unsigned char data[] = { 0x03,                                   // size
121                              0x10, 0x11,  0x12, 0x13,  0x14, 0x15,   // data
122                              0x20, 0x21,  0x22, 0x23,  0x24, 0x25 };
123     senf::PacketInterpreterBase::ptr p (senf::PacketInterpreter<VoidPacket>::create(data));
124     typedef senf::VectorParser<
125         senf::UInt16Parser,
126         senf::detail::FixedAuxParserPolicy<senf::UInt8Parser, 1u>
127         > UInt16VectorParser;
128     UInt16VectorParser v (boost::next(p->data().begin(),1), &p->data());
129     UInt16VectorParser::container w (v);
130
131     BOOST_CHECK_EQUAL( w[0], 0x1011 );
132     BOOST_CHECK_EQUAL( w[2], 0x1415 );
133     BOOST_CHECK_EQUAL( w.size(), 3u );
134     p->data()[0] = 0x06;
135     BOOST_CHECK_EQUAL( w.size(), 6u );
136     BOOST_CHECK_EQUAL( std::distance(w.begin(),w.end()), 
137                        UInt16VectorParser::difference_type(w.size()) );
138
139     w.shift(w.begin()+1);
140     BOOST_CHECK_EQUAL( w.size(), 7u );
141     BOOST_CHECK_EQUAL( w[0], 0x1011 );
142     BOOST_CHECK_EQUAL( w[1], 0 );
143     BOOST_CHECK_EQUAL( w[2], 0x1213 );
144
145     w.insert(w.begin()+3, 2u, 0xfffe);
146     BOOST_CHECK_EQUAL( w.size(), 9u );
147     BOOST_CHECK_EQUAL( w[2], 0x1213 );
148     BOOST_CHECK_EQUAL( w[3], 0xfffe );
149     BOOST_CHECK_EQUAL( w[4], 0xfffe );
150     BOOST_CHECK_EQUAL( w[5], 0x1415 );
151
152     w.erase(w.begin()+3, w.begin()+5);
153     BOOST_CHECK_EQUAL( w.size(), 7u );
154
155     w.erase(w.begin()+1);
156     BOOST_CHECK_EQUAL( w.size(), 6u );
157
158     {
159         senf::PacketData::iterator i (p->data().begin()+1);
160         UInt16VectorParser::iterator j (w.begin());
161         UInt16VectorParser::iterator e (w.end());
162         for (;j!=e;++j, i+=2)
163             BOOST_CHECK_EQUAL( senf::UInt16Parser(i,&p->data()), *j );
164         BOOST_CHECK_EQUAL(p->data().end()-i, 0);
165     }
166
167     w.clear();
168     BOOST_CHECK_EQUAL( w.size(), 0u );
169     BOOST_CHECK( w.begin() == w.end() );
170     BOOST_CHECK_EQUAL( p->data().size(), 1u );
171
172     BOOST_CHECK_EQUAL( w.parser().size(), 0u );
173 }
174
175 BOOST_AUTO_UNIT_TEST(dynamicPolicyVector)
176 {
177     unsigned char data[] = { 0x03,                                   // size
178                              0x10, 0x11,  0x12, 0x13,  0x14, 0x15,   // data
179                              0x20, 0x21,  0x22, 0x23,  0x24, 0x25 };
180     senf::PacketInterpreterBase::ptr p (senf::PacketInterpreter<VoidPacket>::create(data));
181
182     typedef senf::VectorParser<
183         senf::UInt16Parser,
184         senf::detail::DynamicAuxParserPolicy<senf::UInt8Parser>
185         > UInt16VectorParser;
186
187     UInt16VectorParser v (senf::UInt8Parser(p->data().begin(), &p->data()),
188                           boost::next(p->data().begin(),1), &p->data());
189     UInt16VectorParser::container w (v);
190     
191     BOOST_CHECK_EQUAL( v.size(), 3u );
192     BOOST_CHECK_EQUAL( w.size(), 3u );
193
194     BOOST_CHECK_EQUAL( v[0], 0x1011 );
195     BOOST_CHECK_EQUAL( v[2], 0x1415 );
196
197     BOOST_CHECK_EQUAL( w[0], 0x1011 );
198     BOOST_CHECK_EQUAL( w[2], 0x1415 );
199 }
200
201 namespace {
202
203     struct TestTransform
204     {
205         typedef unsigned value_type;
206         static unsigned get(unsigned v) { return v-2; }
207         static unsigned set(unsigned v) { return v+2; }
208     };
209
210     struct TestVectorParser 
211         : public senf::PacketParserBase
212     {
213 #       include SENF_PARSER()
214
215         SENF_PARSER_PRIVATE_FIELD ( size1 , senf::UInt8Parser );
216         SENF_PARSER_PRIVATE_FIELD ( size2 , senf::UInt8Parser );
217         SENF_PARSER_FIELD         ( dummy , senf::UInt32Parser );
218         SENF_PARSER_VECTOR        ( vec1  , transform(TestTransform, size1) , senf::UInt16Parser );
219         SENF_PARSER_VECTOR        ( vec2  , bytes(size2) , senf::UInt16Parser );
220
221         SENF_PARSER_FINALIZE( TestVectorParser );
222     };
223
224 }
225
226 BOOST_AUTO_UNIT_TEST(vectorMacro)
227 {
228     unsigned char data[] = { 0x05,                   // size1
229                              0x04,                   // size2
230                              0x01, 0x02, 0x03, 0x04, // dummy
231                              0x05, 0x06,             // vec1[0]
232                              0x07, 0x08,             // vec1[1]
233                              0x09, 0x0A,             // vec1[2]
234                              0x0B, 0x0C,             // vec2[0]
235                              0x0D, 0x0E };           // vec2[1]
236
237     senf::DataPacket p (senf::DataPacket::create(data));
238     TestVectorParser parser (p.data().begin(), &p.data());
239     
240     BOOST_CHECK_EQUAL( parser.vec1().size(), 3u );
241     BOOST_CHECK_EQUAL( parser.vec2().size(), 2u );
242     BOOST_CHECK_EQUAL( parser.dummy(), 0x01020304u );
243     BOOST_CHECK_EQUAL( parser.vec1()[0], 0x0506u );
244     BOOST_CHECK_EQUAL( parser.vec1()[1], 0x0708u );
245     BOOST_CHECK_EQUAL( parser.vec1()[2], 0x090Au );
246     BOOST_CHECK_EQUAL( parser.vec2()[0], 0x0B0Cu );
247     BOOST_CHECK_EQUAL( parser.vec2()[1], 0x0D0Eu );
248 }
249
250 ///////////////////////////////cc.e////////////////////////////////////////
251 #undef prefix_
252
253 \f
254 // Local Variables:
255 // mode: c++
256 // fill-column: 100
257 // c-file-style: "senf"
258 // indent-tabs-mode: nil
259 // ispell-local-dictionary: "american"
260 // compile-command: "scons -u test"
261 // comment-column: 40
262 // End: