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>
34 #include "../Utils/auto_unit_test.hh"
35 #include <boost/test/test_tools.hpp>
38 ///////////////////////////////cc.p////////////////////////////////////////
43 typedef unsigned key_t;
47 : public senf::PacketTypeBase,
48 public senf::PacketTypeMixin<FooPacketType>
50 using senf::PacketTypeMixin<FooPacketType>::nextPacketRange;
51 using senf::PacketTypeMixin<FooPacketType>::initSize;
52 using senf::PacketTypeMixin<FooPacketType>::init;
53 static size_type initSize()
56 // We need to implement initHeadSize() to force the mixin to switch into 'fixed-size'
57 // mode. Otherwise, mixin::nextPacketRange() would query the parser for it's size to find
58 // the header size. Since the parser is VoidPacketParser, the header size would therefore be
60 static size_type initHeadSize()
61 { return initSize(); }
63 typedef senf::ConcretePacket<FooPacketType> FooPacket;
65 struct BarPacketParser : public senf::PacketParserBase
67 # include SENF_FIXED_PARSER()
69 SENF_PARSER_FIELD( type, senf::UInt16Parser );
70 SENF_PARSER_FIELD( length, senf::Int32Parser );
71 SENF_PARSER_FIELD( reserved, senf::UInt16Parser );
74 reserved() << 0xA0A0u;
77 SENF_PARSER_FINALIZE(BarPacketParser);
81 : public senf::PacketTypeBase,
82 public senf::PacketTypeMixin<BarPacketType,RegTag>
84 typedef senf::PacketTypeMixin<BarPacketType,RegTag> mixin;
85 typedef senf::ConcretePacket<BarPacketType> packet;
86 typedef BarPacketParser parser;
87 using mixin::nextPacketRange;
88 using mixin::nextPacketType;
89 using mixin::initSize;
91 static void dump(packet p, std::ostream & os) {
93 << "type: " << p->type() << "\n"
94 << "length: " << p->length() << "\n";
96 static void finalize(packet p) {
97 if (p.next(senf::nothrow))
98 p->type() = senf::PacketRegistry<RegTag>::key(p.next());
102 static key_t nextPacketKey(packet p) {
106 typedef BarPacketType::packet BarPacket;
109 senf::PacketRegistry<RegTag>::RegistrationProxy<FooPacket> registerFoo(1u);
110 senf::PacketRegistry<RegTag>::RegistrationProxy<BarPacket> registerBar(2u);
113 struct IntAnnotation {
117 struct LargeAnnotation {
121 struct ComplexAnnotation : senf::ComplexAnnotation
127 struct ComplexEmptyAnnotation : senf::ComplexAnnotation
130 struct InvalidAnnotation
137 BOOST_AUTO_UNIT_TEST(packet)
139 senf::Packet packet (FooPacket::create());
140 BarPacket::createAfter(packet);
142 BOOST_REQUIRE( packet );
143 BOOST_CHECK( packet.next() );
144 BOOST_CHECK( ! packet.next().next(senf::nothrow) );
145 BOOST_CHECK( ! packet.prev(senf::nothrow) );
146 BOOST_CHECK( packet.next().prev() == packet );
147 BOOST_CHECK( packet.next() != packet );
148 BOOST_CHECK_EQUAL( std::distance(packet.data().begin(), packet.next().data().begin()), 4 );
149 BOOST_CHECK_EQUAL( std::distance(packet.data().begin(), packet.data().end()), 12 );
150 BOOST_CHECK_EQUAL( std::distance(packet.next().data().begin(), packet.next().data().end()), 8 );
151 BOOST_CHECK( packet.data().end() == packet.next().data().end() );
152 BOOST_CHECK_EQUAL( packet.size(), 12u );
153 BOOST_CHECK_EQUAL( packet.next().size(), 8u );
154 BOOST_CHECK( packet.is<FooPacket>() );
155 BOOST_CHECK( packet.next().is<BarPacket>() );
156 BOOST_CHECK( packet.first() == packet );
157 BOOST_CHECK( packet.last() == packet.next() );
159 senf::Packet p2 (packet.next());
161 packet.parseNextAs<FooPacket>();
162 BOOST_CHECK_EQUAL( packet.size(), 12u );
163 BOOST_CHECK_EQUAL( packet.next().size(), 8u );
164 BOOST_CHECK( packet.next().is<FooPacket>() );
166 BOOST_CHECK( packet.next().as<FooPacket>() );
168 p2 = packet.next().clone();
170 packet.next().append( p2 );
171 BOOST_REQUIRE( packet.next().next() );
172 BOOST_CHECK( packet.next().next().next() );
173 BOOST_CHECK( packet.next().next().next().is<senf::DataPacket>() );
174 BOOST_CHECK_EQUAL( packet.size(), 16u );
176 // This calls and checks typeId()
177 BOOST_CHECK_EQUAL( senf::PacketRegistry<RegTag>::key(packet), 1u );
178 packet.next().parseNextAs( senf::PacketRegistry<RegTag>::lookup(2u).factory() );
179 BOOST_CHECK( packet.next().next().is<BarPacket>() );
183 BOOST_CHECK_EQUAL( s.str(), "BarPacket:\ntype: 0\nlength: 0\n" );
185 packet.finalizeAll();
186 BOOST_CHECK_EQUAL( packet.last().as<BarPacket>()->type(),
187 BarPacket::Parser::type_t::value_type(-1) );
188 packet.last().append(FooPacket::create());
189 packet.finalizeThis();
190 packet.finalizeTo<BarPacket>();
191 packet.finalizeTo(packet.find<BarPacket>());
192 packet.finalizeAll();
193 BOOST_CHECK_EQUAL( packet.find<BarPacket>()->type(), 1u );
195 BOOST_CHECK( packet.factory() == FooPacket::factory() );
197 senf::PacketData::byte data[] = { 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
200 BarPacket::createAfter(packet, data);
201 BOOST_REQUIRE( packet.next() );
202 BOOST_REQUIRE( packet.next().is<BarPacket>() );
203 BOOST_CHECK( packet.last().is<FooPacket>() );
204 BOOST_CHECK_EQUAL( packet.last().rfind<BarPacket>()->type(), 1u );
205 BOOST_CHECK_EQUAL( packet.next().size(), 11u );
206 BOOST_REQUIRE( packet.next().next() );
207 BOOST_CHECK( packet.next().next().is<FooPacket>() );
208 BOOST_CHECK( ! packet.next().next().next(senf::nothrow) );
209 BOOST_CHECK_EQUAL( packet.next().next().data()[0], 0x81u );
211 BOOST_CHECK( packet.first().find<FooPacket>() == packet );
212 BOOST_CHECK( packet.last().rfind<BarPacket>() == packet.last().prev() );
213 BOOST_CHECK( packet.find<FooPacket>() == packet );
214 BOOST_CHECK( packet.last().rfind<FooPacket>() == packet.last() );
215 BOOST_CHECK( packet.next<BarPacket>() == packet.next() );
216 BOOST_CHECK( packet.last().prev().prev<FooPacket>() == packet );
218 senf::DataPacket::createAfter(packet);
219 BOOST_CHECK_THROW( packet.next().next().next().parseNextAs<BarPacket>(),
220 senf::InvalidPacketChainException );
223 BOOST_AUTO_UNIT_TEST(concretePacket)
225 senf::PacketData::byte data[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
227 BOOST_CHECK_EQUAL( FooPacket::create().size(), 4u );
228 BOOST_CHECK_EQUAL( FooPacket::create(senf::noinit).size(), 0u );
229 BOOST_CHECK_THROW( FooPacket::create(2u), senf::TruncatedPacketException );
230 // No 'u' suffix here to check, that the disable_if works ...
231 BOOST_CHECK_EQUAL( FooPacket::create(10).size(), 10u );
232 BOOST_CHECK_EQUAL( FooPacket::create(2u,senf::noinit).size(), 2u );
233 BOOST_CHECK_EQUAL( FooPacket::create(data).size(), 6u );
235 senf::Packet packet (FooPacket::create());
237 BOOST_CHECK_EQUAL( FooPacket::createAfter(packet).size(), 4u );
238 BOOST_CHECK_EQUAL( packet.size(), 8u );
240 BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,senf::noinit).size(), 0u );
241 BOOST_CHECK_EQUAL( packet.size(), 4u );
243 BOOST_CHECK_THROW( FooPacket::createAfter(packet,2u), senf::TruncatedPacketException );
244 // No 'u' suffix here to check, that the disable_if works ...
245 BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,10).size(), 10u );
246 BOOST_CHECK_EQUAL( packet.size(), 14u );
248 BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,2u,senf::noinit).size(), 2u );
249 BOOST_CHECK_EQUAL( packet.size(), 6u );
251 BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,data).size(), 6u );
252 BOOST_CHECK_EQUAL( packet.size(), 10u );
254 BOOST_CHECK_EQUAL( FooPacket::createBefore(packet).size(), 14u );
255 BOOST_CHECK_EQUAL( packet.size(), 10u );
257 BOOST_CHECK_EQUAL( FooPacket::createBefore(packet,senf::noinit).size(), 10u );
258 BOOST_CHECK_EQUAL( packet.size(), 10u );
260 BOOST_CHECK( packet.clone() != packet );
261 BOOST_CHECK_EQUAL( BarPacket::create()->reserved(), 0xA0A0u );
264 BOOST_AUTO_UNIT_TEST(packetAssign)
266 BarPacket bar1 (BarPacket::create());
267 BarPacket bar2 (BarPacket::create());
269 bar2->type() << 0x2A2Bu;
270 bar1.parser() << bar2;
272 BOOST_CHECK_EQUAL( bar1->type(), 0x2A2Bu );
275 BOOST_AUTO_UNIT_TEST(packetAnnotation)
277 senf::Packet packet (FooPacket::create());
278 BarPacket::createAfter(packet);
280 ComplexAnnotation & ca (packet.annotation<ComplexAnnotation>());
283 SENF_CHECK_NO_THROW( packet.annotation<IntAnnotation>().value = 0xDEADBEEF );
285 senf::Packet p2 (packet.next());
287 BOOST_CHECK_EQUAL( p2.annotation<IntAnnotation>().value, 0xDEADBEEFu );
288 BOOST_CHECK_EQUAL( p2.annotation<ComplexAnnotation>().s, "dead beef" );
289 BOOST_CHECK_EQUAL( p2.annotation<ComplexAnnotation>().i, 0x12345678 );
291 BOOST_CHECK( senf::detail::AnnotationIndexer<IntAnnotation>::Small );
292 BOOST_CHECK( ! senf::detail::AnnotationIndexer<LargeAnnotation>::Small );
293 BOOST_CHECK( ! senf::detail::AnnotationIndexer<ComplexAnnotation>::Small );
294 BOOST_CHECK( ! senf::detail::AnnotationIndexer<ComplexEmptyAnnotation>::Small );
299 COMPILE_FAIL(invalidAnnotation)
301 #if 0 // The traits check fails for user defined but trivial constructors so ...
302 # ifdef BOOST_HAS_TYPE_TRAITS_INTRINSICS
304 senf::Packet packet (FooPacket::create());
305 (void) packet.annotation<InvalidAnnotation>();
311 invalid_annotation_check_disabled();
317 ///////////////////////////////cc.e////////////////////////////////////////
324 // c-file-style: "senf"
325 // indent-tabs-mode: nil
326 // ispell-local-dictionary: "american"
327 // compile-command: "scons -u test"
328 // comment-column: 40