From: tho Date: Fri, 9 Nov 2007 15:31:56 +0000 (+0000) Subject: new TLVPacket release :) now fixed sized length field support. X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=8e973da50bdd625d2cbdd218143a4fcd920f9109;p=senf.git new TLVPacket release :) now fixed sized length field support. git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@503 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/Packets/MPEGDVBBundle/TLVPacket.cc b/Packets/MPEGDVBBundle/TLVPacket.cc index 44c6338..f5ddb53 100644 --- a/Packets/MPEGDVBBundle/TLVPacket.cc +++ b/Packets/MPEGDVBBundle/TLVPacket.cc @@ -134,39 +134,6 @@ prefix_ void senf::Parse_TLVPacketLength::resize(size_type size) data().insert( si, size-current_size, 0); } -prefix_ void senf::TLVPacketType::dump(packet p, std::ostream & os) -{ - os << "TLVPacket:\n" - << std::dec - << " type: " << unsigned(p->type()) << "\n" - << " length: " << unsigned(p->length()) << "\n"; -} - -prefix_ senf::PacketParserBase::size_type senf::TLVPacketType::initSize() -{ - return 5; // 4 bytes type + 1 byte length -} - -prefix_ void senf::TLVPacketType::init(packet p) -{ - p->init(); -} - -prefix_ void senf::TLVPacketType::finalize(packet p) -{ - p->length() = p.next().data().size(); -} - -prefix_ senf::PacketInterpreterBase::optional_range -senf::TLVPacketType::nextPacketRange(packet p) -{ - if (p.data().size() < 5) - return no_range(); - return range( - boost::next(p.data().begin(), 4 + senf::bytes(p->length()) ), - p.data().end() ); -} - ///////////////////////////////cc.e//////////////////////////////////////// #undef prefix_ diff --git a/Packets/MPEGDVBBundle/TLVPacket.ct b/Packets/MPEGDVBBundle/TLVPacket.ct new file mode 100644 index 0000000..0aa77d1 --- /dev/null +++ b/Packets/MPEGDVBBundle/TLVPacket.ct @@ -0,0 +1,81 @@ +// $Id: TLVPacket.ct 469 2007-10-19 11:20:08Z tho $ +// +// Copyright (C) 2007 +// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) +// Kompetenzzentrum fuer Satelitenkommunikation (SatCom) +// Thorsten Horstmann +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief TLVPacket non-inline template implementation */ + +//#include "TLVPacket.ih" + +// Custom includes +#include "TLVPacket.hh" + +#define prefix_ +///////////////////////////////ct.p//////////////////////////////////////// + +template +prefix_ void senf::TLVPacketType::dump(packet p, std::ostream & os) +{ + os << "TLVPacket:" + << std::dec + << " type: " << unsigned(p->type()) << "\n" + << " length: " << unsigned(p->length()) << "\n"; +} + +template +prefix_ void senf::TLVPacketType::finalize(packet p) +{ + PacketData::size_type size = p.next().data().size(); + if ( size > LengthParser::max_value ) + throw(UnsuportedTLVPacketException()); + p->length() = size; +} + +template +prefix_ senf::PacketParserBase::size_type senf::TLVPacketType::initSize() +{ + return 4 + senf::init_bytes::value; +} + +template +prefix_ senf::PacketInterpreterBase::optional_range +senf::TLVPacketType::nextPacketRange(packet p) +{ + if (p.data().size() < 5) + return no_range(); + return range( + boost::next(p.data().begin(), 4 + senf::bytes(p->length()) ), + p.data().end() ); +} + +///////////////////////////////ct.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// comment-column: 40 +// End: diff --git a/Packets/MPEGDVBBundle/TLVPacket.hh b/Packets/MPEGDVBBundle/TLVPacket.hh index f52da1c..955160e 100644 --- a/Packets/MPEGDVBBundle/TLVPacket.hh +++ b/Packets/MPEGDVBBundle/TLVPacket.hh @@ -35,6 +35,12 @@ namespace senf { + /** \brief xxx + + \todo document me + \todo add usefull exceptions strings + + */ struct UnsuportedTLVPacketException : public std::exception { virtual char const * what() const throw() { @@ -42,6 +48,9 @@ namespace senf { } }; + /** \brief xxx + \todo document me + */ class Parse_TLVPacketLength : public detail::packet::ParseIntOps, public PacketParserBase @@ -50,15 +59,15 @@ namespace senf { Parse_TLVPacketLength(data_iterator i, state_type s) : PacketParserBase(i,s) {} typedef boost::uint32_t value_type; - - value_type value() const; + static const size_type init_bytes = 1; + static value_type const min_value = 0; + static value_type const max_value = 4294967295u; + value_type value() const; void value(value_type const & v); Parse_TLVPacketLength const & operator= (value_type other); - - static const size_type init_bytes = 1; - + size_type bytes() const; void init() const; @@ -80,52 +89,54 @@ namespace senf { /** \brief parse TLVPacket Packet - - + \todo document me + \see TLVPacketType */ + template struct Parse_TLVPacket : public PacketParserBase { # include SENF_PARSER() SENF_PARSER_FIELD( type, Parse_UInt32 ); - SENF_PARSER_FIELD( length, Parse_TLVPacketLength ); + SENF_PARSER_FIELD( length, LengthParser ); SENF_PARSER_FINALIZE(Parse_TLVPacket); }; - - /** \brief TLV Packet - \image html TLV.png + + /** \brief generic TLV Packet type + + \todo document me - \par Packet type (typedef): - \ref TLVPacket - - \par Fields: - \ref Parse_TLVPacket - \ingroup protocolbundle_mpegdvb */ + template struct TLVPacketType : public PacketTypeBase { - typedef ConcretePacket packet; - typedef Parse_TLVPacket parser; + typedef ConcretePacket > packet; + typedef Parse_TLVPacket parser; static optional_range nextPacketRange(packet p); - static void init(packet p); static size_type initSize(); + static void finalize(packet p); + static void dump(packet p, std::ostream & os); }; - - typedef TLVPacketType::packet TLVPacket; + + typedef TLVPacketType::packet TLVPacket; + typedef TLVPacketType::packet TLVFix8Packet; + typedef TLVPacketType::packet TLVFix16Packet; + typedef TLVPacketType::packet TLVFix24Packet; + typedef TLVPacketType::packet TLVFix32Packet; } ///////////////////////////////hh.e//////////////////////////////////////// //#include "TLVPacket.cci" -//#include "TLVPacket.ct" +#include "TLVPacket.ct" //#include "TLVPacket.cti" #endif diff --git a/Packets/MPEGDVBBundle/TLVPacket.test.cc b/Packets/MPEGDVBBundle/TLVPacket.test.cc index 0e3889d..86ee6bf 100644 --- a/Packets/MPEGDVBBundle/TLVPacket.test.cc +++ b/Packets/MPEGDVBBundle/TLVPacket.test.cc @@ -31,60 +31,58 @@ #include "../../Utils/auto_unit_test.hh" #include +#include #define prefix_ ///////////////////////////////cc.p//////////////////////////////////////// using namespace senf; -BOOST_AUTO_UNIT_TEST(tlvPacket_static) + +template +void check_TLVPacket(TLVPacketType tlvPacket, boost::uint32_t type, boost::uint32_t length) +{ + BOOST_CHECK_EQUAL( tlvPacket->type(), type ); + BOOST_CHECK_EQUAL( tlvPacket->length(), length ); + + PacketData & tlvPacket_value (tlvPacket.next().data()); + BOOST_CHECK_EQUAL( tlvPacket_value.size(), length); + for (int i=0, j=tlvPacket_value.size(); i::value, 5u ); - BOOST_CHECK_EQUAL( TLVPacketType::initSize(), 5u ); + BOOST_CHECK_EQUAL( TLVPacket::type::initSize(), 5u ); } -BOOST_AUTO_UNIT_TEST(tlvPacket_parse_packet_with_simple_length) +BOOST_AUTO_UNIT_TEST(TLVPacket_parse_packet_with_simple_length) { unsigned char data[] = { 0x01, 0x23, 0x45, 0x67, // type 0x0A, // first bit not set, length=10 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value (payload) }; - senf::TLVPacket tlvPacket (senf::TLVPacket::create(data)); - - BOOST_CHECK_EQUAL( tlvPacket->type(), 0x01234567u ); - BOOST_CHECK_EQUAL( tlvPacket->length(), 0x0Au ); - - PacketData & tlvPacket_value (tlvPacket.next().data()); - BOOST_CHECK_EQUAL( tlvPacket_value.size(), 0x0Au); - for (int i=0, j=tlvPacket_value.size(); i one byte length following 0x0A, // length (10 bytes value) 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 // value (payload) - }; - + }; senf::TLVPacket tlvPacket (senf::TLVPacket::create(data)); - - BOOST_CHECK_EQUAL( tlvPacket->type(), 0x01234567u ); - BOOST_CHECK_EQUAL( tlvPacket->length(), 0x0Au ); - - PacketData & tlvPacket_value (tlvPacket.next().data()); - BOOST_CHECK_EQUAL( tlvPacket_value.size(), 0x0Au); - for (int i=0, j=tlvPacket_value.size(); i +void test_TLVFixPacket_parsing(unsigned lengthParser_size) +{ + std::vector data; + data.push_back(0x01); data.push_back(0x23); data.push_back(0x45); data.push_back(0x67); // type + data.insert(data.end(), lengthParser_size-1, 0x00); + data.push_back(0x0A); // length + for( int i=0; i < 10; i++ ) { + data.push_back(i); // payload + } + TLVFixPacketType tlvPacket (TLVFixPacketType::create( + boost::make_iterator_range(data.begin(), data.end()))); + check_TLVPacket( tlvPacket, 0x01234567u, 0x0Au ); +} + +BOOST_AUTO_UNIT_TEST(TLVFixPacket_parse_packet) +{ + test_TLVFixPacket_parsing( Parse_UInt8::fixed_bytes); + test_TLVFixPacket_parsing( Parse_UInt16::fixed_bytes); + test_TLVFixPacket_parsing( Parse_UInt24::fixed_bytes); + test_TLVFixPacket_parsing( Parse_UInt32::fixed_bytes); +} + + +template +void test_TLVFixPacket_creating() +{ + std::string payload ("Hello, world!"); + TLVFixPacketType tlvPacket (TLVFixPacketType::create()); + tlvPacket->type() = 42u; + DataPacket::createAfter( tlvPacket, payload ); + tlvPacket.finalize(); + + BOOST_CHECK_EQUAL( tlvPacket->type(), 42u); + BOOST_CHECK_EQUAL( tlvPacket->length(), 13u); + + PacketData & tlvPacket_value (tlvPacket.next().data()); + BOOST_CHECK( equal( tlvPacket_value.begin(), tlvPacket_value.end(), payload.begin() )); +} + +BOOST_AUTO_UNIT_TEST(TLVFixPacket_create_packet) +{ + test_TLVFixPacket_creating(); + test_TLVFixPacket_creating(); + test_TLVFixPacket_creating(); + test_TLVFixPacket_creating(); +} + + +template +void test_invalid_TLVFixPacket_creating(boost::uint32_t max_value) +{ + TLVFixPacketType tlvPacket (TLVFixPacketType::create()); + tlvPacket->type() = 42u; + DataPacket payload (DataPacket::createAfter( tlvPacket, max_value+1)); + //DataPacket::createAfter( payload, 1); // this is one byte to much. + BOOST_CHECK_THROW( tlvPacket.finalize(), UnsuportedTLVPacketException); +} + +BOOST_AUTO_UNIT_TEST(TLVFixPacket_create_invalid_packet) +{ + test_invalid_TLVFixPacket_creating ( Parse_UInt8::max_value); + test_invalid_TLVFixPacket_creating( Parse_UInt16::max_value); + test_invalid_TLVFixPacket_creating( Parse_UInt24::max_value); + //test_invalid_TLVFixPacket_creating( Parse_UInt32::max_value); +} + ///////////////////////////////cc.e//////////////////////////////////////// #undef prefix_ diff --git a/Scheduler/Daemon.test.cc b/Scheduler/Daemon.test.cc index 9c6b22f..3e44b3e 100644 --- a/Scheduler/Daemon.test.cc +++ b/Scheduler/Daemon.test.cc @@ -35,7 +35,7 @@ #include "Daemon.hh" #include "../Utils/Exception.hh" -#include +#include "../Utils/auto_unit_test.hh" #include #define prefix_