From: pug Date: Thu, 10 Sep 2009 14:50:47 +0000 (+0000) Subject: fixed ... X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=8c6873a925c79e6bb640f82895b31f4cbd84ef30;p=senf.git fixed & finished unittest for Option Type List Parser and access functions on the GenericPacketParsers git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1410 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/senf/Packets/DefaultBundle/IPv6ExtOptionType.cc b/senf/Packets/DefaultBundle/IPv6ExtOptionType.cc index f8273ae..a20bf07 100644 --- a/senf/Packets/DefaultBundle/IPv6ExtOptionType.cc +++ b/senf/Packets/DefaultBundle/IPv6ExtOptionType.cc @@ -27,8 +27,8 @@ #define prefix_ ///////////////////////////////cc.p//////////////////////////////////////// -prefix_ senf::PacketInterpreterBase::range senf::GenericOptTypeTLVPacketParser::value() const { - senf::PacketData::iterator begin (boost::next(data().begin(), 2 + optionLength() )); +prefix_ senf::PacketInterpreterBase::range senf::GenericOptTypeTLVPacketParser::value() const { + senf::PacketData::iterator begin (boost::next(i(), 2 )); return PacketInterpreterBase::range(begin, boost::next( begin, optionLength()) ); } diff --git a/senf/Packets/DefaultBundle/IPv6ExtOptionType.ct b/senf/Packets/DefaultBundle/IPv6ExtOptionType.ct index cc5f9f2..6c5a841 100644 --- a/senf/Packets/DefaultBundle/IPv6ExtOptionType.ct +++ b/senf/Packets/DefaultBundle/IPv6ExtOptionType.ct @@ -30,22 +30,22 @@ template prefix_ void senf::GenericOptTypeTLVPacketParser::value(ForwardReadableRange const &range) { - safe_data_iterator si( data(), boost::next(data().begin()+2) ); unsigned int rangeSize = boost::size(range); -// std::cout << "tmpl. method - optLength =" << (unsigned) optionLength() -// << ", rangeSize: " << rangeSize -// << ", optionType: " << (unsigned) optionType() << std::endl; - if ( rangeSize > optionLength() ){ + safe_data_iterator si( data(), boost::next(i(), 2 + optionLength() ) ); +// if ( rangeSize > optionLength() ){ // std::cout << "rangeSize > optionLength()" << std::endl; - data().insert(si, rangeSize - optionLength(),0 ); - } - if (rangeSize < optionLength() ){ +// data().insert(si, rangeSize - optionLength(),0 ); +// } +// if (rangeSize < optionLength() ){ // std::cout << "rangeSize < optionLength()" << std::endl; - data().erase(si, si + (optionLength() - rangeSize)); - } - std::copy(( boost::begin(range)), boost::end(range), si); - optionLength() = 2u; -// std::cout << "optLength AFTER =" << (unsigned) optionLength() << ", rangeSize: " << rangeSize << std::endl; +// data().erase(si, si + (optionLength() - rangeSize)); +// } + if ( (rangeSize-2) != optionLength() ) + resize(optionLength()+2, rangeSize); + + std::copy(boost::next(boost::begin(range), 2), boost::next(boost::end(range)), si); + optionType() = *(boost::begin(range)); + optionLength() = *(boost::next( boost::begin(range), 1)); } ///////////////////////////////ct.e//////////////////////////////////////// diff --git a/senf/Packets/DefaultBundle/IPv6Extensions.cc b/senf/Packets/DefaultBundle/IPv6Extensions.cc index ca4b9d7..543a3b3 100644 --- a/senf/Packets/DefaultBundle/IPv6Extensions.cc +++ b/senf/Packets/DefaultBundle/IPv6Extensions.cc @@ -25,7 +25,7 @@ #include "IPv6Extensions.hh" //#include "IPv6Extensions.ih" - +#include // Custom includes //#include "IPv6Extensions.mpp" @@ -75,9 +75,11 @@ prefix_ void senf::IPv6ExtensionType_HopByHop::dump(packet p, std::ostream & os) os << "OptionTypes:\n"; IPv6Extension_HopByHop::Parser::options_t::container options (p->options()); IPv6Extension_HopByHop::Parser::options_t::container::iterator optIter(options.begin()); - for(;optIter != options.end(); ++ optIter) + for(;optIter != options.end(); ++ optIter){ os << "Option Type:\t" << (unsigned) optIter->optionType() << "\nOptionLength:\t" << (unsigned) optIter->optionLength() <<"\n"; + senf::hexdump(boost::next(boost::begin(optIter->value())), boost::next( boost::end(optIter->value())), os ); + } } prefix_ void senf::IPv6ExtensionType_Destination::dump(packet p, std::ostream & os) diff --git a/senf/Packets/ListOptionTypeParser.cti b/senf/Packets/ListOptionTypeParser.cti index adf88ca..819f15e 100644 --- a/senf/Packets/ListOptionTypeParser.cti +++ b/senf/Packets/ListOptionTypeParser.cti @@ -109,9 +109,8 @@ construct(container_type & c) safe_data_iterator e = i + realAux_; for (n_ = 0; i != e;) { unsigned int elByte = senf::bytes(ElementParser(i, c.state())); - if (((i + elByte) == e) && (i[0] == 0u || i[0] == 1u)) { //check wether last element is padding or not + if (((i + elByte) == e) && (i[0] == 0u || i[0] == 1u)) { //check weather last element is padding or not realAux_ -= std::distance(i, e); - container_size_ -= std::distance(i, e); c.data().erase(i, e); //delete padding e = i; //set end iterator } else{ @@ -119,7 +118,6 @@ construct(container_type & c) std::advance(i, elByte); } } - // container_size_ = std::distance(i,e); container_size_ = c.data().size(); //set actual size without padding } @@ -131,12 +129,15 @@ destruct(container_type & c) { // data_iterator i (AuxPolicy::adjust(parser_type::get(p).i(), parser_type::get(p).state())); safe_data_iterator i (c.data(), c.i()) ; - safe_data_iterator const e = i + realAux_; + safe_data_iterator e = i + realAux_; unsigned int padBytes = 0; if (realAux_ == 0) //if list is empty, 6 padding bytes required! padBytes = 6; - else + else{ padBytes = ( (realAux_+2) % 8); + if (padBytes != 0) + padBytes = 8 - padBytes; + } if (padBytes > 0) { c.data().insert(e, padBytes, 0u); if (padBytes > 1) { @@ -179,9 +180,7 @@ prefix_ void senf::detail::ListOptionTypeParser_Policy::container_policy:: init(data_iterator i, state_type s) { -// i[0] = 1u; - // for (unsigned int n = 1;n < 6; ++n) -// i[n] = 0u; + realAux_ = 0; n_ = 0; container_size_ = s->size(); AuxPolicy::aux(0, i, s); @@ -193,15 +192,10 @@ prefix_ void senf::detail::ListOptionTypeParser_Policy::container_policy:: erase(container_type & c, data_iterator p) { - size_type b(senf::bytes(ElementParser(p, c.state()))); //length of parser - // AuxPolicy::aux( - // AuxPolicy::aux( c.i(), c.state()) -b, - // c.i(), - // c.state()); + size_type b(senf::bytes(ElementParser(p, c.state()))); realAux_ -= b; --n_; - // The container will be reduced by b bytes directly after this call - container_size_ -= b; + container_size_ = c.data().size() - b; } //insert() @@ -211,7 +205,6 @@ senf::detail::ListOptionTypeParser_Policy::container_po insert(container_type & c, data_iterator p) { size_type b(senf::bytes(ElementParser(p, c.state()))); - // AuxPolicy::aux( AuxPolicy::aux(c.i(), c.state())+b, c.i(), c.state()); realAux_ += b; ++n_; container_size_ = c.data().size(); @@ -230,7 +223,6 @@ update(container_type const & c) data_iterator j(i); for (size_type n(n_); n; --n, std::advance(j, senf::bytes(ElementParser(j,c.state())))); realAux_ = std::distance(i, j); - // aux( std::distance(i,j), c.i(), c.state() ); container_size_ = c.data().size(); } @@ -243,7 +235,6 @@ setBegin(container_type const & c, iterator_data & d) const { return c.i(); - // return AuxPolicy::adjust(c.i(), c.state()); } //setEnd() @@ -253,8 +244,8 @@ prefix_ typename senf::detail::ListOptionTypeParser_Policy< senf::detail::ListOptionTypeParser_Policy::container_policy:: setEnd(container_type const & c, iterator_data & d) const -{ //wtf?? - return boost::next(AuxPolicy::adjust(c.i(), c.state()), aux(c.i(),c.state())); +{ + return boost::next(c.i(),realAux_); } //setFromPosition() diff --git a/senf/Packets/ListOptionTypeParser.ih b/senf/Packets/ListOptionTypeParser.ih index d85995a..9743a50 100644 --- a/senf/Packets/ListOptionTypeParser.ih +++ b/senf/Packets/ListOptionTypeParser.ih @@ -52,7 +52,7 @@ namespace senf { typedef ListParser< ListOptionTypeParser_Policy > parser_type; typedef ListParser_Container< container_policy > container_type; - static const size_type init_bytes = 6 + AuxPolicy::aux_bytes; + static const size_type init_bytes = 6u + AuxPolicy::aux_bytes; ListOptionTypeParser_Policy(); template ListOptionTypeParser_Policy(Arg const & arg); diff --git a/senf/Packets/ListOptionTypeParser.test.cc b/senf/Packets/ListOptionTypeParser.test.cc new file mode 100644 index 0000000..2c00ff6 --- /dev/null +++ b/senf/Packets/ListOptionTypeParser.test.cc @@ -0,0 +1,149 @@ +// $Id: ListOptionTypeParser.test.cc 1345 2009-08-26 15:40:55Z pug $ +// +// Copyright (C) 2007 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Philipp Batroff +// +// 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. + +// Custom includes +#include "Packets.hh" +#include + +#include +#include + +#include + +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +namespace { + struct VoidPacket : public senf::PacketTypeBase + {}; + + struct OptionParser : public senf::PacketParserBase + { +# include SENF_PARSER() + + SENF_PARSER_FIELD( size, senf::UInt8Parser ); + typedef senf::detail::FixedAuxParserPolicy ListOptionTypeAuxPolicy; + typedef senf::detail::ListOptionTypeParser_Policy ListOptionTypePolicy; + typedef senf::ListParser ListOptionTypeParser; + SENF_PARSER_FIELD ( list, ListOptionTypeParser); + + SENF_PARSER_FINALIZE(OptionParser); + }; +} + +BOOST_AUTO_UNIT_TEST(ListOptionTypeParser) +{ + senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter::create( + OptionParser::init_bytes)); + OptionParser p ( pi->data().begin() ,&pi->data()); + BOOST_CHECK_EQUAL(OptionParser::init_bytes +0, 7u); + + p.list().init(); + + BOOST_CHECK_EQUAL( p.list().size(), 0u ); + BOOST_CHECK_EQUAL( p.list().bytes(), 6u ); //padding has to be accounted for! + BOOST_CHECK( p.list().empty() ); + + // the mutators are really tested together with the container wrappers since they are based + // on the container wrapper. Here we only need one call to make the list larger ... + + // this doesn't make sense, since padding is added and the size doesn't really change + // should be tested later with the container + p.list().push_back_space(); + p = OptionParser(pi->data().begin(),&pi->data()); + BOOST_CHECK_EQUAL( p.list().bytes(), 6u ); //padding bytes + BOOST_CHECK_EQUAL( p.list().size(), 1u ); //padding + BOOST_CHECK_EQUAL( p.list().empty(), false ); +} + +BOOST_AUTO_UNIT_TEST(ListOptionTypeParser_container) +{ + senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter::create( + OptionParser::init_bytes)); + + { + OptionParser p ( pi->data().begin() ,&pi->data()); + p.init(); + OptionParser::list_t::container c (p.list()); + + BOOST_CHECK_EQUAL( c.size(), 0u ); + BOOST_CHECK_EQUAL( c.bytes(), 0u ); // padding bytes wont be in here, added/removed automatically in destructor + BOOST_CHECK( c.begin() == c.end() ); + + unsigned char d[] = {0x05, 0x02, 0x40, 0x34}; + unsigned char d1[] = {0x03, 0x01, 0x77}; + unsigned char d2[] = {0x07, 0x01, 0x13}; + + SENF_CHECK_NO_THROW( c.push_back( d ) ); + BOOST_CHECK_EQUAL( c.bytes(), 4u ); + BOOST_CHECK_EQUAL( c.size(), 1u ); + + SENF_CHECK_NO_THROW( c.push_back( d1 ) ); + BOOST_CHECK_EQUAL( c.bytes(), 7u ); + BOOST_CHECK_EQUAL( c.size(), 2u ); + + SENF_CHECK_NO_THROW( c.push_back( d2 ) ); + BOOST_CHECK_EQUAL( c.bytes(), 10u ); + BOOST_CHECK_EQUAL( c.size(), 3u ); + + OptionParser::list_t::container::iterator cIter (c.begin()); + + BOOST_CHECK_EQUAL( cIter->optionType(), 5u); + BOOST_CHECK_EQUAL( cIter->optionLength(), 2u); + BOOST_CHECK_EQUAL( *(boost::begin(cIter->value()) ), 0x40); + BOOST_CHECK_EQUAL( *(boost::next(boost::begin(cIter->value()) )), 0x34); + cIter++; + BOOST_CHECK_EQUAL( cIter->optionType(), 3u); + BOOST_CHECK_EQUAL( cIter->optionLength(), 1u); + BOOST_CHECK_EQUAL( *(boost::begin(cIter->value() )), 0x77); + cIter++; + BOOST_CHECK_EQUAL( cIter->optionType(), 7u); + BOOST_CHECK_EQUAL( cIter->optionLength(), 1u); + BOOST_CHECK_EQUAL( *(boost::begin(cIter->value())), 0x13); + + //deletes first element + c.erase(c.begin(),1); + BOOST_CHECK_EQUAL( c.size(), 2u ); + BOOST_CHECK_EQUAL( c.bytes(), 6u ); + //deletes first 2 elements + c.erase(c.begin(),1); + BOOST_CHECK_EQUAL( c.size(), 1u ); + BOOST_CHECK_EQUAL( c.bytes(), 3u ); + BOOST_CHECK_EQUAL( c.empty(), false); + + //clear whole list + SENF_CHECK_NO_THROW( c.clear() ); + BOOST_CHECK_EQUAL( c.size(), 0u ); + BOOST_CHECK_EQUAL( c.bytes(), 0u ); + BOOST_CHECK_EQUAL( c.empty(), true); + } +} + +// Local Variables: +// mode: c++ +// fill-column: 100 +// comment-column: 40 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// End: