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 "IPv6ExtOptions.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<
45 senf::IPv6GenericOptionParser, ListOptionTypeAuxPolicy> ListOptionTypePolicy;
46 typedef senf::ListParser<ListOptionTypePolicy> ListOptionTypeParser;
47 SENF_PARSER_FIELD ( list, ListOptionTypeParser);
49 SENF_PARSER_FINALIZE(OptionParser);
53 BOOST_AUTO_UNIT_TEST(ListOptionTypeParser)
55 senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
56 OptionParser::init_bytes));
57 OptionParser p ( pi->data().begin() ,&pi->data());
58 BOOST_CHECK_EQUAL(OptionParser::init_bytes +0, 7u);
62 BOOST_CHECK_EQUAL( p.list().size(), 0u );
63 BOOST_CHECK_EQUAL( p.list().bytes(), 6u ); //padding has to be accounted for!
64 BOOST_CHECK( p.list().empty() );
66 // the mutators are really tested together with the container wrappers since they are based
67 // on the container wrapper. Here we only need one call to make the list larger ...
69 // this doesn't make sense, since padding is added and the size doesn't really change
70 // should be tested later with the container
71 p.list().push_back_space();
72 p = OptionParser(pi->data().begin(),&pi->data());
73 BOOST_CHECK_EQUAL( p.list().bytes(), 6u ); //padding bytes
74 BOOST_CHECK_EQUAL( p.list().size(), 1u ); //padding
75 BOOST_CHECK_EQUAL( p.list().empty(), false );
78 BOOST_AUTO_UNIT_TEST(ListOptionTypeParser_container)
80 senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
81 OptionParser::init_bytes));
84 OptionParser p ( pi->data().begin() ,&pi->data());
86 OptionParser::list_t::container c (p.list());
88 BOOST_CHECK_EQUAL( c.size(), 0u );
89 BOOST_CHECK_EQUAL( c.bytes(), 0u ); // padding bytes wont be in here, added/removed automatically in destructor
90 BOOST_CHECK( c.begin() == c.end() );
92 std::vector<unsigned char> d (2, 0xab);
93 std::vector<unsigned char> d1 (1, 0x77);
94 std::vector<unsigned char> d2 (1, 0x13);
96 SENF_CHECK_NO_THROW( c.push_back( std::make_pair(0x65, d) ));
97 BOOST_CHECK_EQUAL( c.bytes(), 4u );
98 BOOST_CHECK_EQUAL( c.size(), 1u );
100 SENF_CHECK_NO_THROW( c.push_back( std::make_pair(0x03, d1) ));
101 BOOST_CHECK_EQUAL( c.bytes(), 7u );
102 BOOST_CHECK_EQUAL( c.size(), 2u );
104 SENF_CHECK_NO_THROW( c.push_back( std::make_pair(0x07, d2) ));
105 BOOST_CHECK_EQUAL( c.bytes(), 10u );
106 BOOST_CHECK_EQUAL( c.size(), 3u );
108 OptionParser::list_t::container::iterator cIter (c.begin());
109 BOOST_CHECK_EQUAL( cIter->altAction(), 1u);
110 BOOST_CHECK_EQUAL( cIter->changeFlag(), 1u);
111 BOOST_CHECK_EQUAL( cIter->optionType(), 5u);
112 BOOST_CHECK_EQUAL( cIter->length(), 2u);
113 BOOST_CHECK_EQUAL( *(boost::begin(cIter->value()) ), 0xab);
114 BOOST_CHECK_EQUAL( *(boost::next(boost::begin(cIter->value()) )), 0xab);
116 BOOST_CHECK_EQUAL( cIter->optionType(), 3u);
117 BOOST_CHECK_EQUAL( cIter->length(), 1u);
118 BOOST_CHECK_EQUAL( *(boost::begin(cIter->value() )), 0x77);
120 BOOST_CHECK_EQUAL( cIter->optionType(), 7u);
121 BOOST_CHECK_EQUAL( cIter->length(), 1u);
122 BOOST_CHECK_EQUAL( *(boost::begin(cIter->value())), 0x13);
124 //deletes first element
125 c.erase(c.begin(),1);
126 BOOST_CHECK_EQUAL( c.size(), 2u );
127 BOOST_CHECK_EQUAL( c.bytes(), 6u );
128 //deletes first 2 elements
129 c.erase(c.begin(),1);
130 BOOST_CHECK_EQUAL( c.size(), 1u );
131 BOOST_CHECK_EQUAL( c.bytes(), 3u );
132 BOOST_CHECK_EQUAL( c.empty(), false);
135 SENF_CHECK_NO_THROW( c.clear() );
136 BOOST_CHECK_EQUAL( c.size(), 0u );
137 BOOST_CHECK_EQUAL( c.bytes(), 0u );
138 BOOST_CHECK_EQUAL( c.empty(), true);
141 ///////////////////////////////cc.p////////////////////////////////////////
147 // comment-column: 40
148 // c-file-style: "senf"
149 // indent-tabs-mode: nil
150 // ispell-local-dictionary: "american"
151 // compile-command: "scons -u test"