X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=senf%2FPackets%2FGenericTLV.test.cc;h=76c698dfc23fb0ad6be1e125718ed5cfaee38da3;hb=7ee689fe38d66aa3a0004d55e8708750d35adc0b;hp=84be10c9df42ff8e6afa96dfde0b26d283b3f8cc;hpb=a79c5e98760ea8232c13d8266eb7ca0ac5cdefd3;p=senf.git diff --git a/senf/Packets/GenericTLV.test.cc b/senf/Packets/GenericTLV.test.cc index 84be10c..76c698d 100644 --- a/senf/Packets/GenericTLV.test.cc +++ b/senf/Packets/GenericTLV.test.cc @@ -1,9 +1,9 @@ // $Id$ // -// Copyright (C) 2008 +// Copyright (C) 2009 // Fraunhofer Institute for Open Communication Systems (FOKUS) // Competence Center NETwork research (NET), St. Augustin, GERMANY -// @AUTHOR@ +// 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 @@ -25,31 +25,33 @@ // 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 +59,45 @@ 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; + type() = typeId; length_() = 4; - } - static const type_t::value_type TYPEID = 0x42; + } + 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 +106,7 @@ namespace { SENF_PARSER_LIST ( tlv_list, list_length, MyGenericTLVParser ); SENF_PARSER_FINALIZE ( MyTestPacketParser ); }; - + struct MyTestPacketType : public senf::PacketTypeBase, public senf::PacketTypeMixin @@ -89,9 +122,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 +144,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 +155,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 +174,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_