1 // $Id: ListOptionTypeParser.test.cc 1345 2009-08-26 15:40:55Z pug $
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Philipp Batroff <Philipp.Batroff@fokus.fraunhofer.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <senf/Packets/Packets.hh>
25 #include "IPv6ExtOptionType.hh"
26 #include "ListOptionTypeParser.hh"
28 #include <senf/Utils/auto_unit_test.hh>
29 #include <boost/test/test_tools.hpp>
32 ///////////////////////////////cc.p////////////////////////////////////////
35 struct VoidPacket : public senf::PacketTypeBase
38 struct OptionParser : public senf::PacketParserBase
40 # include SENF_PARSER()
42 SENF_PARSER_FIELD( size, senf::UInt8Parser );
43 typedef senf::detail::FixedAuxParserPolicy<senf::UInt8Parser, 1u> ListOptionTypeAuxPolicy;
44 typedef senf::detail::ListOptionTypeParser_Policy<senf::GenericOptTypeTLVPacketParser, ListOptionTypeAuxPolicy> ListOptionTypePolicy;
45 typedef senf::ListParser<ListOptionTypePolicy> ListOptionTypeParser;
46 SENF_PARSER_FIELD ( list, ListOptionTypeParser);
48 SENF_PARSER_FINALIZE(OptionParser);
52 BOOST_AUTO_UNIT_TEST(ListOptionTypeParser)
54 senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
55 OptionParser::init_bytes));
56 OptionParser p ( pi->data().begin() ,&pi->data());
57 BOOST_CHECK_EQUAL(OptionParser::init_bytes +0, 7u);
61 BOOST_CHECK_EQUAL( p.list().size(), 0u );
62 BOOST_CHECK_EQUAL( p.list().bytes(), 6u ); //padding has to be accounted for!
63 BOOST_CHECK( p.list().empty() );
65 // the mutators are really tested together with the container wrappers since they are based
66 // on the container wrapper. Here we only need one call to make the list larger ...
68 // this doesn't make sense, since padding is added and the size doesn't really change
69 // should be tested later with the container
70 p.list().push_back_space();
71 p = OptionParser(pi->data().begin(),&pi->data());
72 BOOST_CHECK_EQUAL( p.list().bytes(), 6u ); //padding bytes
73 BOOST_CHECK_EQUAL( p.list().size(), 1u ); //padding
74 BOOST_CHECK_EQUAL( p.list().empty(), false );
77 BOOST_AUTO_UNIT_TEST(ListOptionTypeParser_container)
79 senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
80 OptionParser::init_bytes));
83 OptionParser p ( pi->data().begin() ,&pi->data());
85 OptionParser::list_t::container c (p.list());
87 BOOST_CHECK_EQUAL( c.size(), 0u );
88 BOOST_CHECK_EQUAL( c.bytes(), 0u ); // padding bytes wont be in here, added/removed automatically in destructor
89 BOOST_CHECK( c.begin() == c.end() );
91 unsigned char d[] = {0x65, 0x02, 0x40, 0x34};
92 unsigned char d1[] = {0x03, 0x01, 0x77};
93 unsigned char d2[] = {0x07, 0x01, 0x13};
95 SENF_CHECK_NO_THROW( c.push_back( d ) );
96 BOOST_CHECK_EQUAL( c.bytes(), 4u );
97 BOOST_CHECK_EQUAL( c.size(), 1u );
99 SENF_CHECK_NO_THROW( c.push_back( d1 ) );
100 BOOST_CHECK_EQUAL( c.bytes(), 7u );
101 BOOST_CHECK_EQUAL( c.size(), 2u );
103 SENF_CHECK_NO_THROW( c.push_back( d2 ) );
104 BOOST_CHECK_EQUAL( c.bytes(), 10u );
105 BOOST_CHECK_EQUAL( c.size(), 3u );
107 OptionParser::list_t::container::iterator cIter (c.begin());
108 BOOST_CHECK_EQUAL( cIter->altAction(), 1u);
109 BOOST_CHECK_EQUAL( cIter->changeFlag(), 1u);
110 BOOST_CHECK_EQUAL( cIter->optionType(), 5u);
111 BOOST_CHECK_EQUAL( cIter->optionLength(), 2u);
112 BOOST_CHECK_EQUAL( *(boost::begin(cIter->value()) ), 0x40);
113 BOOST_CHECK_EQUAL( *(boost::next(boost::begin(cIter->value()) )), 0x34);
115 BOOST_CHECK_EQUAL( cIter->optionType(), 3u);
116 BOOST_CHECK_EQUAL( cIter->optionLength(), 1u);
117 BOOST_CHECK_EQUAL( *(boost::begin(cIter->value() )), 0x77);
119 BOOST_CHECK_EQUAL( cIter->optionType(), 7u);
120 BOOST_CHECK_EQUAL( cIter->optionLength(), 1u);
121 BOOST_CHECK_EQUAL( *(boost::begin(cIter->value())), 0x13);
123 //deletes first element
124 c.erase(c.begin(),1);
125 BOOST_CHECK_EQUAL( c.size(), 2u );
126 BOOST_CHECK_EQUAL( c.bytes(), 6u );
127 //deletes first 2 elements
128 c.erase(c.begin(),1);
129 BOOST_CHECK_EQUAL( c.size(), 1u );
130 BOOST_CHECK_EQUAL( c.bytes(), 3u );
131 BOOST_CHECK_EQUAL( c.empty(), false);
134 SENF_CHECK_NO_THROW( c.clear() );
135 BOOST_CHECK_EQUAL( c.size(), 0u );
136 BOOST_CHECK_EQUAL( c.bytes(), 0u );
137 BOOST_CHECK_EQUAL( c.empty(), true);
144 // comment-column: 40
145 // c-file-style: "senf"
146 // indent-tabs-mode: nil
147 // ispell-local-dictionary: "american"
148 // compile-command: "scons -u test"