4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@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 Packet unit tests */
26 //#include "Packet.test.hh"
27 //#include "Packet.test.ih"
31 #include <boost/static_assert.hpp>
32 #include <boost/cstdint.hpp>
35 #include <senf/Utils/auto_unit_test.hh>
36 #include <boost/test/test_tools.hpp>
39 ///////////////////////////////cc.p////////////////////////////////////////
44 typedef unsigned key_t;
48 : public senf::PacketTypeBase,
49 public senf::PacketTypeMixin<FooPacketType>
51 using senf::PacketTypeMixin<FooPacketType>::nextPacketRange;
52 using senf::PacketTypeMixin<FooPacketType>::initSize;
53 using senf::PacketTypeMixin<FooPacketType>::init;
54 static size_type initSize()
57 // We need to implement initHeadSize() to force the mixin to switch into 'fixed-size'
58 // mode. Otherwise, mixin::nextPacketRange() would query the parser for it's size to find
59 // the header size. Since the parser is VoidPacketParser, the header size would therefore be
61 static size_type initHeadSize()
62 { return initSize(); }
64 typedef senf::ConcretePacket<FooPacketType> FooPacket;
66 struct BarPacketParser : public senf::PacketParserBase
68 # include SENF_FIXED_PARSER()
70 SENF_PARSER_FIELD( type, senf::UInt16Parser );
71 SENF_PARSER_FIELD( length, senf::Int32Parser );
72 SENF_PARSER_FIELD( reserved, senf::UInt16Parser );
75 reserved() << 0xA0A0u;
78 SENF_PARSER_FINALIZE(BarPacketParser);
82 : public senf::PacketTypeBase,
83 public senf::PacketTypeMixin<BarPacketType,RegTag>
85 typedef senf::PacketTypeMixin<BarPacketType,RegTag> mixin;
86 typedef senf::ConcretePacket<BarPacketType> packet;
87 typedef BarPacketParser parser;
88 using mixin::nextPacketRange;
89 using mixin::nextPacketType;
90 using mixin::initSize;
92 static void dump(packet p, std::ostream & os) {
94 << senf::fieldName("type") << p->type() << "\n"
95 << senf::fieldName("length") << p->length() << "\n";
97 static void finalize(packet p) {
98 if (p.next(senf::nothrow))
99 p->type() = senf::PacketRegistry<RegTag>::key(p.next());
103 static key_t nextPacketKey(packet p) {
107 typedef BarPacketType::packet BarPacket;
110 senf::PacketRegistry<RegTag>::ScopedRegistrationProxy<FooPacket> registerFoo(1u);
111 senf::PacketRegistry<RegTag>::ScopedRegistrationProxy<BarPacket> registerBar(2u);
114 struct IntAnnotation {
115 boost::uint32_t value;
118 std::ostream & operator<<(std::ostream & os, IntAnnotation const & v)
119 { os << v.value; return os; }
121 struct LargeAnnotation {
125 std::ostream & operator<<(std::ostream & os, LargeAnnotation const & v)
126 { os << v.value; return os; }
128 struct ComplexAnnotation : senf::ComplexAnnotation
130 ComplexAnnotation() : s(), i() {}
135 std::ostream & operator<<(std::ostream & os, ComplexAnnotation const & v)
136 { os << "('" << v.s << "' " << v.i << ')'; return os; }
138 struct ComplexEmptyAnnotation : senf::ComplexAnnotation
141 std::ostream & operator<<(std::ostream & os, ComplexEmptyAnnotation const & v)
142 { os << "(empty)"; return os; }
144 struct InvalidAnnotation
149 std::ostream & operator<<(std::ostream & os, InvalidAnnotation const & v)
150 { os << v.value; return os; }
154 SENF_AUTO_UNIT_TEST(packet)
156 BOOST_CHECK(! senf::Packet().is<BarPacket>() );
157 senf::Packet packet (FooPacket::create());
158 BarPacket::createAfter(packet);
160 BOOST_REQUIRE( packet );
161 BOOST_CHECK( packet.next() );
162 BOOST_CHECK( ! packet.next().next(senf::nothrow) );
163 BOOST_CHECK( ! packet.next().next(senf::nothrow).is<BarPacket>() );
164 BOOST_CHECK( ! packet.prev(senf::nothrow) );
165 BOOST_CHECK( packet.next().prev() == packet );
166 SENF_CHECK_NOT_EQUAL( packet.next(), packet );
167 BOOST_CHECK_EQUAL( std::distance(packet.data().begin(), packet.next().data().begin()), 4 );
168 BOOST_CHECK_EQUAL( std::distance(packet.data().begin(), packet.data().end()), 12 );
169 BOOST_CHECK_EQUAL( std::distance(packet.next().data().begin(), packet.next().data().end()), 8 );
170 BOOST_CHECK( packet.data().end() == packet.next().data().end() );
171 BOOST_CHECK_EQUAL( packet.size(), 12u );
172 BOOST_CHECK_EQUAL( packet.next().size(), 8u );
173 BOOST_CHECK( packet.is<FooPacket>() );
174 BOOST_CHECK( packet.next().is<BarPacket>() );
175 BOOST_CHECK( packet.first() == packet );
176 BOOST_CHECK( packet.last() == packet.next() );
178 senf::Packet p2 (packet.next());
180 packet.parseNextAs<FooPacket>();
181 BOOST_CHECK_EQUAL( packet.size(), 12u );
182 BOOST_CHECK_EQUAL( packet.next().size(), 8u );
183 BOOST_CHECK( packet.next().is<FooPacket>() );
185 BOOST_CHECK( packet.next().as<FooPacket>() );
187 p2 = packet.next().clone();
189 packet.next().append( p2 );
190 BOOST_REQUIRE( packet.next().next() );
191 BOOST_CHECK( packet.next().next().next() );
192 BOOST_CHECK( packet.next().next().next().is<senf::DataPacket>() );
193 BOOST_CHECK_EQUAL( packet.size(), 16u );
195 // This calls and checks typeId()
196 BOOST_CHECK_EQUAL( senf::PacketRegistry<RegTag>::key(packet), 1u );
197 packet.next().parseNextAs( senf::PacketRegistry<RegTag>::lookup(2u).factory() );
198 BOOST_CHECK( packet.next().next().is<BarPacket>() );
202 BOOST_CHECK_EQUAL( s.str(),
204 " (anonymous namespace)::IntAnnotation : 0\n"
209 packet.finalizeAll();
210 BOOST_CHECK_EQUAL( packet.last().as<BarPacket>()->type(),
211 BarPacket::Parser::type_t::value_type(-1) );
212 packet.last().append(FooPacket::create());
213 packet.finalizeThis();
214 packet.finalizeTo<BarPacket>();
215 packet.finalizeTo(packet.find<BarPacket>());
216 packet.finalizeAll();
217 BOOST_CHECK_EQUAL( packet.find<BarPacket>()->type(), 1u );
219 BOOST_CHECK( packet.factory() == FooPacket::factory() );
221 senf::PacketData::byte data[] = { 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
224 BarPacket::createAfter(packet, data);
225 BOOST_REQUIRE( packet.next() );
226 BOOST_REQUIRE( packet.next().is<BarPacket>() );
227 BOOST_CHECK( packet.last().is<FooPacket>() );
228 BOOST_CHECK_EQUAL( packet.last().rfind<BarPacket>()->type(), 1u );
229 BOOST_CHECK_EQUAL( packet.next().size(), 11u );
230 BOOST_REQUIRE( packet.next().next() );
231 BOOST_CHECK( packet.next().next().is<FooPacket>() );
232 BOOST_CHECK( ! packet.next().next().next(senf::nothrow) );
233 BOOST_CHECK_EQUAL( packet.next().next().data()[0], 0x81u );
235 BOOST_CHECK( packet.first().find<FooPacket>() == packet );
236 BOOST_CHECK( packet.last().rfind<BarPacket>() == packet.last().prev() );
237 BOOST_CHECK( packet.find<FooPacket>() == packet );
238 BOOST_CHECK( packet.last().rfind<FooPacket>() == packet.last() );
239 BOOST_CHECK( packet.next<BarPacket>() == packet.next() );
240 BOOST_CHECK( packet.last().prev().prev<FooPacket>() == packet );
242 senf::DataPacket::createAfter(packet);
243 BOOST_CHECK_THROW( packet.next().next().next().parseNextAs<BarPacket>(),
244 senf::InvalidPacketChainException );
246 SENF_CHECK_NO_THROW( BarPacket::create(senf::noinit).dump(s));
249 SENF_AUTO_UNIT_TEST(concretePacket)
251 senf::PacketData::byte data[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
253 BOOST_CHECK_EQUAL( FooPacket::create().size(), 4u );
254 BOOST_CHECK_EQUAL( FooPacket::create(senf::noinit).size(), 0u );
255 BOOST_CHECK_THROW( FooPacket::create(2u), senf::TruncatedPacketException );
256 // No 'u' suffix here to check, that the disable_if works ...
257 BOOST_CHECK_EQUAL( FooPacket::create(10).size(), 10u );
258 BOOST_CHECK_EQUAL( FooPacket::create(2u,senf::noinit).size(), 2u );
259 BOOST_CHECK_EQUAL( FooPacket::create(data).size(), 6u );
261 senf::Packet packet (FooPacket::create());
263 BOOST_CHECK_EQUAL( FooPacket::createAfter(packet).size(), 4u );
264 BOOST_CHECK_EQUAL( packet.size(), 8u );
266 BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,senf::noinit).size(), 0u );
267 BOOST_CHECK_EQUAL( packet.size(), 4u );
269 BOOST_CHECK_THROW( FooPacket::createAfter(packet,2u), senf::TruncatedPacketException );
270 // No 'u' suffix here to check, that the disable_if works ...
271 BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,10).size(), 10u );
272 BOOST_CHECK_EQUAL( packet.size(), 14u );
274 BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,2u,senf::noinit).size(), 2u );
275 BOOST_CHECK_EQUAL( packet.size(), 6u );
277 BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,data).size(), 6u );
278 BOOST_CHECK_EQUAL( packet.size(), 10u );
280 BOOST_CHECK_EQUAL( FooPacket::createBefore(packet).size(), 14u );
281 BOOST_CHECK_EQUAL( packet.size(), 10u );
283 BOOST_CHECK_EQUAL( FooPacket::createBefore(packet,senf::noinit).size(), 10u );
284 BOOST_CHECK_EQUAL( packet.size(), 10u );
286 BOOST_CHECK_EQUAL( FooPacket::createInsertBefore(packet).size(), 14u );
287 BOOST_CHECK_EQUAL( packet.size(), 10u );
288 BOOST_REQUIRE( packet.prev() );
289 BOOST_CHECK_EQUAL( packet.prev().size(), 14u );
290 BOOST_REQUIRE( packet.prev().prev() );
291 BOOST_CHECK_EQUAL( packet.prev().prev().size(), 14u );
293 BOOST_CHECK_EQUAL( FooPacket::createInsertBefore(packet,senf::noinit).size(), 10u );
294 BOOST_CHECK_EQUAL( packet.size(), 10u );
295 BOOST_REQUIRE_NO_THROW( packet.prev().prev().prev() );
296 BOOST_CHECK_THROW( packet.prev().prev().prev().prev(), senf::InvalidPacketChainException );
297 BOOST_CHECK_EQUAL( packet.prev().size(), 10u );
298 BOOST_CHECK_EQUAL( packet.prev().prev().size(), 14u );
299 BOOST_CHECK_EQUAL( packet.prev().prev().prev().size(), 14u );
301 SENF_CHECK_NOT_EQUAL( packet.clone(), packet );
302 BOOST_CHECK_EQUAL( BarPacket::create()->reserved(), 0xA0A0u );
305 SENF_AUTO_UNIT_TEST(packetAssign)
307 BarPacket bar1 (BarPacket::create());
308 BarPacket bar2 (BarPacket::create());
310 bar2->type() << 0x2A2Bu;
311 bar1.parser() << bar2;
313 BOOST_CHECK_EQUAL( bar1->type(), 0x2A2Bu );
316 SENF_AUTO_UNIT_TEST(packetAnnotation)
318 typedef senf::detail::AnnotationRegistry Reg;
320 senf::Packet packet (FooPacket::create());
321 BarPacket::createAfter(packet);
323 ComplexAnnotation & ca (packet.annotation<ComplexAnnotation>());
326 SENF_CHECK_NO_THROW( packet.annotation<IntAnnotation>().value = 0xDEADBEEF );
328 senf::Packet p2 (packet.next());
330 BOOST_CHECK_EQUAL( p2.annotation<IntAnnotation>().value, 0xDEADBEEFu );
331 BOOST_CHECK_EQUAL( p2.annotation<ComplexAnnotation>().s, "dead beef" );
332 BOOST_CHECK_EQUAL( p2.annotation<ComplexAnnotation>().i, 0x12345678 );
334 BOOST_CHECK( Reg::lookup<IntAnnotation>() >= 0 );
335 BOOST_CHECK( Reg::lookup<LargeAnnotation>() < 0 );
336 BOOST_CHECK( Reg::lookup<ComplexAnnotation>() < 0 );
337 BOOST_CHECK( Reg::lookup<ComplexEmptyAnnotation>() < 0 );
340 std::stringstream ss;
342 senf::dumpPacketAnnotationRegistry(ss);
345 "SENF_PACKET_ANNOTATION_SLOTS = 8\n"
346 "SENF_PACKET_ANNOTATION_SLOTSIZE = 16\n"
347 "TYPE FAST COMPLEX SIZE\n"
348 "(anonymous namespace)::ComplexAnnotation no yes 8\n"
349 "(anonymous namespace)::ComplexEmptyAnnotation no yes 1\n"
350 "(anonymous namespace)::IntAnnotation yes no 4\n"
351 "(anonymous namespace)::LargeAnnotation no no 32\n" );
357 COMPILE_FAIL(invalidAnnotation)
359 #if 0 // The traits check fails for user defined but trivial constructors so ...
360 # ifdef BOOST_HAS_TYPE_TRAITS_INTRINSICS
362 senf::Packet packet (FooPacket::create());
363 senf::IGNORE( packet.annotation<InvalidAnnotation>() );
369 invalid_annotation_check_disabled();
375 ///////////////////////////////cc.e////////////////////////////////////////
382 // c-file-style: "senf"
383 // indent-tabs-mode: nil
384 // ispell-local-dictionary: "american"
385 // compile-command: "scons -u test"
386 // comment-column: 40