X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=senf%2FPackets%2FGenericTLV.test.cc;h=535875b801e089c5ac86d9259e363517a28268a4;hb=refs%2Fheads%2Fmaster;hp=84be10c9df42ff8e6afa96dfde0b26d283b3f8cc;hpb=a79c5e98760ea8232c13d8266eb7ca0ac5cdefd3;p=senf.git diff --git a/senf/Packets/GenericTLV.test.cc b/senf/Packets/GenericTLV.test.cc index 84be10c..535875b 100644 --- a/senf/Packets/GenericTLV.test.cc +++ b/senf/Packets/GenericTLV.test.cc @@ -1,55 +1,62 @@ // $Id$ // -// Copyright (C) 2008 +// Copyright (C) 2009 // Fraunhofer Institute for Open Communication Systems (FOKUS) -// Competence Center NETwork research (NET), St. Augustin, GERMANY -// @AUTHOR@ // -// 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. +// The contents of this file are subject to the Fraunhofer FOKUS Public License +// Version 1.0 (the "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// http://senf.berlios.de/license.html // -// 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. +// The Fraunhofer FOKUS Public License Version 1.0 is based on, +// but modifies the Mozilla Public License Version 1.1. +// See the full license text for the amendments. // -// 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. +// Software distributed under the License is distributed on an "AS IS" basis, +// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License +// for the specific language governing rights and limitations under the License. +// +// The Original Code is Fraunhofer FOKUS code. +// +// The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. +// (registered association), Hansastraße 27 c, 80686 Munich, Germany. +// All Rights Reserved. +// +// Contributor(s): +// Thorsten Horstmann /** \file \brief GenericTLV unit tests */ // Custom includes #include "GenericTLV.hh" -#include +#include #include #include #define prefix_ -///////////////////////////////cc.p//////////////////////////////////////// +//-///////////////////////////////////////////////////////////////////////////////////////////////// namespace { - struct MyTLVParserBase + struct MyTLVParserBase : public senf::PacketParserBase { # include SENF_PARSER() SENF_PARSER_FIELD ( type, senf::UInt8Parser ); SENF_PARSER_FIELD_RO ( length, senf::UInt8Parser ); SENF_PARSER_FINALIZE ( MyTLVParserBase ); + + typedef senf::GenericTLVParserRegistry Registry; }; - + struct MyGenericTLVParser : public senf::GenericTLVParserBase { typedef senf::GenericTLVParserBase base; MyGenericTLVParser(data_iterator i, state_type s) : base(i,s) {} }; - + struct MyConcreteTLVParser : public MyTLVParserBase { @@ -57,14 +64,45 @@ namespace { SENF_PARSER_INHERIT ( MyTLVParserBase ); SENF_PARSER_FIELD ( myValue, senf::UInt32Parser ); SENF_PARSER_FINALIZE ( MyConcreteTLVParser ); - + SENF_PARSER_INIT() { - type() = TYPEID; + type() = typeId; length_() = 4; - } - static const type_t::value_type TYPEID = 0x42; + } + static type_t::value_type const typeId = 0x42; + + void dump(std::ostream & os) const { + boost::io::ios_all_saver ias(os); + os << " MyConcreteTLVParser\n" + << " type: " << senf::format::dumpint(type()) << "\n" + << " length: " << senf::format::dumpint(length()) << "\n" + << " value: " << senf::format::dumpint(myValue()) << "\n"; + } }; - + + struct MyConcrete2TLVParser + : public MyTLVParserBase + { + # include SENF_PARSER() + SENF_PARSER_INHERIT ( MyTLVParserBase ); + SENF_PARSER_FIELD ( myValue, senf::UInt32Parser ); + SENF_PARSER_FINALIZE ( MyConcrete2TLVParser ); + + SENF_PARSER_INIT() { + type() = typeId; + length_() = 4; + } + static type_t::value_type const typeId = 0x47; + + void dump(std::ostream & os) const { + boost::io::ios_all_saver ias(os); + os << " MyConcreteTLVParser\n" + << " type: " << senf::format::dumpint(type()) << "\n" + << " length: " << senf::format::dumpint(length()) << "\n" + << " value: " << senf::format::dumpint(myValue()) << "\n"; + } + }; + class MyTestPacketParser : public senf::PacketParserBase { @@ -73,7 +111,7 @@ namespace { SENF_PARSER_LIST ( tlv_list, list_length, MyGenericTLVParser ); SENF_PARSER_FINALIZE ( MyTestPacketParser ); }; - + struct MyTestPacketType : public senf::PacketTypeBase, public senf::PacketTypeMixin @@ -89,9 +127,9 @@ namespace { } -BOOST_AUTO_UNIT_TEST(GenericTLV_parser) +SENF_AUTO_UNIT_TEST(GenericTLV_parser) { - BOOST_CHECK_EQUAL( senf::init_bytes::value, + BOOST_CHECK_EQUAL( senf::init_bytes::value, senf::init_bytes::value) ; unsigned char data[] = { @@ -111,9 +149,9 @@ BOOST_AUTO_UNIT_TEST(GenericTLV_parser) SENF_CHECK_EQUAL_COLLECTIONS( data, data+sizeof(data), genericTLVParser.value().begin(), genericTLVParser.value().end() ); - std::vector value (4, 0xab); + std::vector value (4, 0xab); genericTLVParser.value( std::make_pair(0x42, value)); - + BOOST_CHECK( genericTLVParser.is()); MyConcreteTLVParser concreteTLVParser ( genericTLVParser.as()); @@ -122,14 +160,14 @@ BOOST_AUTO_UNIT_TEST(GenericTLV_parser) BOOST_CHECK_EQUAL( concreteTLVParser.myValue(), 0xabababab ); } -BOOST_AUTO_UNIT_TEST(GenericTLV_packet) +SENF_AUTO_UNIT_TEST(GenericTLV_packet) { MyTestPacket p ( MyTestPacket::create()); MyTestPacket::Parser::tlv_list_t::container tlvContainer (p->tlv_list() ); MyConcreteTLVParser tlv ( tlvContainer.push_back_space().init()); tlv.myValue() << 0xffff; p.finalizeThis(); - + unsigned char data[] = { 0x01, // tlv list length // first tlv: @@ -141,7 +179,57 @@ BOOST_AUTO_UNIT_TEST(GenericTLV_packet) } -///////////////////////////////cc.e//////////////////////////////////////// +SENF_AUTO_UNIT_TEST(GenericTLV_registry) +{ + typedef senf::GenericTLVParserRegistry MyTLVParserRegistry; + MyTestPacket p ( MyTestPacket::create()); + MyTestPacket::Parser::tlv_list_t::container tlvContainer (p->tlv_list() ); + MyConcreteTLVParser conreteTLVParser ( + tlvContainer.push_back_space().init()); + conreteTLVParser.myValue() << 0xffff; + p.finalizeThis(); + + std::stringstream ss; + tlvContainer.begin()->dump( ss); + BOOST_CHECK_EQUAL( ss.str().substr(0,58), + " GenericTLVParser<(anonymous namespace)::MyTLVParserBase>" ); + BOOST_CHECK( ! MyTLVParserRegistry::instance().isRegistered( tlvContainer.begin()->type())); + + MyTLVParserRegistry::instance().registerParser(); + BOOST_CHECK( MyTLVParserRegistry::instance().isRegistered( tlvContainer.begin()->type())); + BOOST_CHECK_EQUAL( + MyTLVParserRegistry::instance().bytes( *tlvContainer.begin()), + senf::bytes( *tlvContainer.begin()) ); + + ss.str(""); ss.clear(); + + tlvContainer.begin()->dump( ss); + BOOST_CHECK_EQUAL( ss.str().substr(0,21), " MyConcreteTLVParser" ); +} + +SENF_AUTO_UNIT_TEST(GenericTLV_predicate) +{ + MyTestPacket p ( MyTestPacket::create() ); + MyTestPacket::Parser::tlv_list_t::container tlvContainer (p->tlv_list() ); + MyConcreteTLVParser conreteTLVParser ( + tlvContainer.push_back_space().init()); + conreteTLVParser.myValue() << 0xffff; + MyConcrete2TLVParser conreteTLVParser2 ( + tlvContainer.push_back_space().init()); + conreteTLVParser2.myValue() << 0xdddd; + p.finalizeThis(); + +// typedef senf::IPv6HopByHopOptionsPacket::Parser::options_t::container optContainer_t; +// optContainer_t optC (p->tlv_list() ); + + MyTestPacket::Parser::tlv_list_t::container testTlvContainer (p->tlv_list() ); + MyTestPacket::Parser::tlv_list_t::container::iterator it = std::find_if ( + testTlvContainer.begin(), testTlvContainer.end(), + senf::detail::Predicate< senf::GenericTLVParserBase, MyConcreteTLVParser>() ); + BOOST_CHECK( it->is()) ; +} + +//-///////////////////////////////////////////////////////////////////////////////////////////////// #undef prefix_