X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Packets%2FPacket.test.cc;h=18b8fe9ed174a8ee2b3453fe00b93299ca011622;hb=c45c112ae88196ea8da9c5a9efb0e167196744d2;hp=8da1546ca54543c64eb12d33a24db6ff03612f55;hpb=d6abda313f70c8a849c5e3f3bbfc12ce301789f1;p=senf.git diff --git a/Packets/Packet.test.cc b/Packets/Packet.test.cc index 8da1546..18b8fe9 100644 --- a/Packets/Packet.test.cc +++ b/Packets/Packet.test.cc @@ -1,6 +1,8 @@ -// Copyright (C) 2007 -// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) -// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// $Id$ +// +// Copyright (C) 2007 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY // Stefan Bund // // This program is free software; you can redistribute it and/or modify @@ -19,13 +21,14 @@ // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. /** \file - \brief Packet.test unit tests */ + \brief Packet unit tests */ //#include "Packet.test.hh" //#include "Packet.test.ih" // Custom includes #include +#include #include "Packets.hh" #include "../Utils/auto_unit_test.hh" @@ -47,9 +50,15 @@ namespace { using senf::PacketTypeMixin::nextPacketRange; using senf::PacketTypeMixin::initSize; using senf::PacketTypeMixin::init; - typedef senf::PacketInterpreter interpreter; - static interpreter::size_type initSize() + static size_type initSize() { return 4u; } + + // We need to implement initHeadSize() to force the mixin to switch into 'fixed-size' + // mode. Otherwise, mixin::nextPacketRange() would query the parser for it's size to find + // the header size. Since the parser is VoidPacketParser, the header size would therefore be + // 0 + static size_type initHeadSize() + { return initSize(); } }; typedef senf::ConcretePacket FooPacket; @@ -57,9 +66,13 @@ namespace { { # include SENF_FIXED_PARSER() - SENF_PARSER_FIELD( type, senf::Parse_UInt16 ); - SENF_PARSER_FIELD( length, senf::Parse_Int32 ); - SENF_PARSER_FIELD( reserved, senf::Parse_UInt16 ); + SENF_PARSER_FIELD( type, senf::UInt16Parser ); + SENF_PARSER_FIELD( length, senf::Int32Parser ); + SENF_PARSER_FIELD( reserved, senf::UInt16Parser ); + + SENF_PARSER_INIT() { + reserved() << 0xA0A0u; + } SENF_PARSER_FINALIZE(BarPacketParser); }; @@ -75,23 +88,18 @@ namespace { using mixin::nextPacketType; using mixin::initSize; using mixin::init; - static size_type initSize() - { return 8u; } - static void init(packet p) { - p->reserved() = 0xA0A0u; - } static void dump(packet p, std::ostream & os) { os << "BarPacket:\n" << "type: " << p->type() << "\n" << "length: " << p->length() << "\n"; } static void finalize(packet p) { - if (p.next()) + if (p.next(senf::nothrow)) p->type() = senf::PacketRegistry::key(p.next()); else p->type() = -1; } - static registry_key_t nextPacketKey(packet p) { + static key_t nextPacketKey(packet p) { return p->type(); } }; @@ -102,19 +110,45 @@ namespace { senf::PacketRegistry::RegistrationProxy registerBar(2u); } + struct IntAnnotation { + unsigned value; + }; + + struct LargeAnnotation { + char value[32]; + }; + + struct ComplexAnnotation : senf::ComplexAnnotation + { + std::string s; + int i; + }; + + struct ComplexEmptyAnnotation : senf::ComplexAnnotation + {}; + + struct InvalidAnnotation + { + std::string value; + }; + } BOOST_AUTO_UNIT_TEST(packet) { - senf::Packet packet (FooPacket::create()); + senf::Packet packet (FooPacket::create()); BarPacket::createAfter(packet); BOOST_REQUIRE( packet ); BOOST_CHECK( packet.next() ); - BOOST_CHECK( ! packet.next().next() ); - BOOST_CHECK( ! packet.prev() ); + BOOST_CHECK( ! packet.next().next(senf::nothrow) ); + BOOST_CHECK( ! packet.prev(senf::nothrow) ); BOOST_CHECK( packet.next().prev() == packet ); BOOST_CHECK( packet.next() != packet ); + BOOST_CHECK_EQUAL( std::distance(packet.data().begin(), packet.next().data().begin()), 4 ); + BOOST_CHECK_EQUAL( std::distance(packet.data().begin(), packet.data().end()), 12 ); + BOOST_CHECK_EQUAL( std::distance(packet.next().data().begin(), packet.next().data().end()), 8 ); + BOOST_CHECK( packet.data().end() == packet.next().data().end() ); BOOST_CHECK_EQUAL( packet.size(), 12u ); BOOST_CHECK_EQUAL( packet.next().size(), 8u ); BOOST_CHECK( packet.is() ); @@ -148,41 +182,42 @@ BOOST_AUTO_UNIT_TEST(packet) packet.dump(s); BOOST_CHECK_EQUAL( s.str(), "BarPacket:\ntype: 0\nlength: 0\n" ); - packet.finalize(); + packet.finalizeAll(); BOOST_CHECK_EQUAL( packet.last().as()->type(), - BarPacket::type::parser::type_t::value_type(-1) ); + BarPacket::Parser::type_t::value_type(-1) ); packet.last().append(FooPacket::create()); - packet.finalize(); - BOOST_CHECK_EQUAL( packet.next()->type(), 1u ); + packet.finalizeThis(); + packet.finalizeTo(); + packet.finalizeTo(packet.find()); + packet.finalizeAll(); + BOOST_CHECK_EQUAL( packet.find()->type(), 1u ); BOOST_CHECK( packet.factory() == FooPacket::factory() ); senf::PacketData::byte data[] = { 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x82, 0x83 }; - BarPacket::createAfter(packet,data); + BarPacket::createAfter(packet, data); BOOST_REQUIRE( packet.next() ); BOOST_REQUIRE( packet.next().is() ); BOOST_CHECK( packet.last().is() ); - BOOST_CHECK_EQUAL( packet.last()->type(), 1u ); + BOOST_CHECK_EQUAL( packet.last().rfind()->type(), 1u ); BOOST_CHECK_EQUAL( packet.next().size(), 11u ); BOOST_REQUIRE( packet.next().next() ); BOOST_CHECK( packet.next().next().is() ); - BOOST_CHECK( ! packet.next().next().next() ); + BOOST_CHECK( ! packet.next().next().next(senf::nothrow) ); BOOST_CHECK_EQUAL( packet.next().next().data()[0], 0x81u ); - BOOST_CHECK( packet.first() == packet ); - BOOST_CHECK( packet.first(senf::nothrow) == packet ); - BOOST_CHECK( packet.last() == packet.last().prev() ); - BOOST_CHECK( packet.last(senf::nothrow) == packet.last().prev() ); - BOOST_CHECK( packet.findNext() == packet ); - BOOST_CHECK( packet.findNext(senf::nothrow) == packet ); - BOOST_CHECK( packet.last().findPrev() == packet.last() ); - BOOST_CHECK( packet.last().findPrev(senf::nothrow) == packet.last() ); + BOOST_CHECK( packet.first().find() == packet ); + BOOST_CHECK( packet.last().rfind() == packet.last().prev() ); + BOOST_CHECK( packet.find() == packet ); + BOOST_CHECK( packet.last().rfind() == packet.last() ); BOOST_CHECK( packet.next() == packet.next() ); - BOOST_CHECK( packet.next(senf::nothrow) == packet.next() ); - BOOST_CHECK( packet.last().prev() == packet ); - BOOST_CHECK( packet.last().prev(senf::nothrow) == packet ); + BOOST_CHECK( packet.last().prev().prev() == packet ); + + senf::DataPacket::createAfter(packet); + BOOST_CHECK_THROW( packet.next().next().next().parseNextAs(), + senf::InvalidPacketChainException ); } BOOST_AUTO_UNIT_TEST(concretePacket) @@ -190,10 +225,11 @@ BOOST_AUTO_UNIT_TEST(concretePacket) senf::PacketData::byte data[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; BOOST_CHECK_EQUAL( FooPacket::create().size(), 4u ); - BOOST_CHECK_EQUAL( FooPacket::create(FooPacket::noinit).size(), 0u ); + BOOST_CHECK_EQUAL( FooPacket::create(senf::noinit).size(), 0u ); BOOST_CHECK_THROW( FooPacket::create(2u), senf::TruncatedPacketException ); - BOOST_CHECK_EQUAL( FooPacket::create(10u).size(), 10u ); - BOOST_CHECK_EQUAL( FooPacket::create(2u,FooPacket::noinit).size(), 2u ); + // No 'u' suffix here to check, that the disable_if works ... + BOOST_CHECK_EQUAL( FooPacket::create(10).size(), 10u ); + BOOST_CHECK_EQUAL( FooPacket::create(2u,senf::noinit).size(), 2u ); BOOST_CHECK_EQUAL( FooPacket::create(data).size(), 6u ); senf::Packet packet (FooPacket::create()); @@ -201,14 +237,15 @@ BOOST_AUTO_UNIT_TEST(concretePacket) BOOST_CHECK_EQUAL( FooPacket::createAfter(packet).size(), 4u ); BOOST_CHECK_EQUAL( packet.size(), 8u ); - BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,FooPacket::noinit).size(), 0u ); + BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,senf::noinit).size(), 0u ); BOOST_CHECK_EQUAL( packet.size(), 4u ); BOOST_CHECK_THROW( FooPacket::createAfter(packet,2u), senf::TruncatedPacketException ); - BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,10u).size(), 10u ); + // No 'u' suffix here to check, that the disable_if works ... + BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,10).size(), 10u ); BOOST_CHECK_EQUAL( packet.size(), 14u ); - BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,2u,FooPacket::noinit).size(), 2u ); + BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,2u,senf::noinit).size(), 2u ); BOOST_CHECK_EQUAL( packet.size(), 6u ); BOOST_CHECK_EQUAL( FooPacket::createAfter(packet,data).size(), 6u ); @@ -217,13 +254,66 @@ BOOST_AUTO_UNIT_TEST(concretePacket) BOOST_CHECK_EQUAL( FooPacket::createBefore(packet).size(), 14u ); BOOST_CHECK_EQUAL( packet.size(), 10u ); - BOOST_CHECK_EQUAL( FooPacket::createBefore(packet,FooPacket::noinit).size(), 10u ); + BOOST_CHECK_EQUAL( FooPacket::createBefore(packet,senf::noinit).size(), 10u ); BOOST_CHECK_EQUAL( packet.size(), 10u ); BOOST_CHECK( packet.clone() != packet ); BOOST_CHECK_EQUAL( BarPacket::create()->reserved(), 0xA0A0u ); } +BOOST_AUTO_UNIT_TEST(packetAssign) +{ + BarPacket bar1 (BarPacket::create()); + BarPacket bar2 (BarPacket::create()); + + bar2->type() << 0x2A2Bu; + bar1.parser() << bar2; + + BOOST_CHECK_EQUAL( bar1->type(), 0x2A2Bu ); +} + +BOOST_AUTO_UNIT_TEST(packetAnnotation) +{ + senf::Packet packet (FooPacket::create()); + BarPacket::createAfter(packet); + + ComplexAnnotation & ca (packet.annotation()); + ca.s = "dead beef"; + ca.i = 0x12345678; + SENF_CHECK_NO_THROW( packet.annotation().value = 0xDEADBEEF ); + + senf::Packet p2 (packet.next()); + + BOOST_CHECK_EQUAL( p2.annotation().value, 0xDEADBEEFu ); + BOOST_CHECK_EQUAL( p2.annotation().s, "dead beef" ); + BOOST_CHECK_EQUAL( p2.annotation().i, 0x12345678 ); + + BOOST_CHECK( senf::detail::AnnotationIndexer::Small ); + BOOST_CHECK( ! senf::detail::AnnotationIndexer::Small ); + BOOST_CHECK( ! senf::detail::AnnotationIndexer::Small ); + BOOST_CHECK( ! senf::detail::AnnotationIndexer::Small ); +} + +#ifdef COMPILE_CHECK + +COMPILE_FAIL(invalidAnnotation) +{ +#if 0 // The traits check fails for user defined but trivial constructors so ... +# ifdef BOOST_HAS_TYPE_TRAITS_INTRINSICS + + senf::Packet packet (FooPacket::create()); + (void) packet.annotation(); + +# else +# endif +#endif + + invalid_annotation_check_disabled(); + +} + +#endif + ///////////////////////////////cc.e//////////////////////////////////////// #undef prefix_