X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=senf%2FPackets%2FGenericTLV.test.cc;h=535875b801e089c5ac86d9259e363517a28268a4;hb=ddb2132be4265f8a0d7d4c954c7c9401e59d027c;hp=6b93252cbd210406434562385e4788772c7166f9;hpb=bea7cfcf3d02688ece159d76890acfe2d0051d71;p=senf.git diff --git a/senf/Packets/GenericTLV.test.cc b/senf/Packets/GenericTLV.test.cc index 6b93252..535875b 100644 --- a/senf/Packets/GenericTLV.test.cc +++ b/senf/Packets/GenericTLV.test.cc @@ -2,55 +2,61 @@ // // Copyright (C) 2009 // Fraunhofer Institute for Open Communication Systems (FOKUS) -// Competence Center NETwork research (NET), St. Augustin, GERMANY -// 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. +// 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 #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 { @@ -58,13 +64,36 @@ namespace { SENF_PARSER_INHERIT ( MyTLVParserBase ); SENF_PARSER_FIELD ( myValue, senf::UInt32Parser ); SENF_PARSER_FINALIZE ( MyConcreteTLVParser ); - + SENF_PARSER_INIT() { type() = typeId; length_() = 4; } 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" @@ -73,7 +102,7 @@ namespace { << " value: " << senf::format::dumpint(myValue()) << "\n"; } }; - + class MyTestPacketParser : public senf::PacketParserBase { @@ -82,7 +111,7 @@ namespace { SENF_PARSER_LIST ( tlv_list, list_length, MyGenericTLVParser ); SENF_PARSER_FINALIZE ( MyTestPacketParser ); }; - + struct MyTestPacketType : public senf::PacketTypeBase, public senf::PacketTypeMixin @@ -98,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[] = { @@ -120,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()); @@ -131,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: @@ -150,30 +179,57 @@ BOOST_AUTO_UNIT_TEST(GenericTLV_packet) } -BOOST_AUTO_UNIT_TEST(GenericTLV_registry) +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 ( + 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), + tlvContainer.begin()->dump( ss); + BOOST_CHECK_EQUAL( ss.str().substr(0,58), " GenericTLVParser<(anonymous namespace)::MyTLVParserBase>" ); - - senf::GenericTLVParserRegistry::instance() - .registerParser(); + 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); + + 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()) ; +} -///////////////////////////////cc.e//////////////////////////////////////// +//-///////////////////////////////////////////////////////////////////////////////////////////////// #undef prefix_