c0802d8011540a39676fae8f48b8e2c74854be52
[senf.git] / senf / Packets / DefaultBundle / ListOptionTypeParser.test.cc
1 // $Id: ListOptionTypeParser.test.cc 1345 2009-08-26 15:40:55Z pug $
2 //
3 // Copyright (C) 2009
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>
7 //
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.
12 //
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.
17 //
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.
22
23 // Custom includes
24 #include <senf/Packets/Packets.hh>
25 #include "IPv6ExtOptionType.hh"
26 #include "ListOptionTypeParser.hh"
27
28 #include <senf/Utils/auto_unit_test.hh>
29 #include <boost/test/test_tools.hpp>
30
31 #define prefix_
32 ///////////////////////////////cc.p////////////////////////////////////////
33
34 namespace {
35     struct VoidPacket : public senf::PacketTypeBase
36     {};
37
38     struct OptionParser : public senf::PacketParserBase
39     {
40 #       include SENF_PARSER()
41
42         SENF_PARSER_FIELD( size, senf::UInt8Parser );
43         typedef senf::detail::FixedAuxParserPolicy<senf::UInt8Parser, 1u> ListOptionTypeAuxPolicy;
44         typedef senf::detail::ListOptionTypeParser_Policy<
45             senf::IPv6GenericOptionTLVParser, ListOptionTypeAuxPolicy> ListOptionTypePolicy;
46         typedef senf::ListParser<ListOptionTypePolicy> ListOptionTypeParser;
47         SENF_PARSER_FIELD ( list, ListOptionTypeParser);
48
49         SENF_PARSER_FINALIZE(OptionParser);
50     };
51 }
52
53 BOOST_AUTO_UNIT_TEST(ListOptionTypeParser)
54 {
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);
59
60     p.list().init();
61
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() );
65
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 ...
68
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 );
76 }
77
78 BOOST_AUTO_UNIT_TEST(ListOptionTypeParser_container)
79 {
80     senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
81             OptionParser::init_bytes));
82
83     {
84         OptionParser p ( pi->data().begin() ,&pi->data());
85         p.init();
86         OptionParser::list_t::container c (p.list());
87
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() );
91
92         unsigned char d[]  = {0x65, 0x02, 0x40, 0x34};
93         unsigned char d1[] = {0x03, 0x01, 0x77};
94         unsigned char d2[] = {0x07, 0x01, 0x13};
95
96         SENF_CHECK_NO_THROW( c.push_back( d ) );
97         BOOST_CHECK_EQUAL( c.bytes(), 4u );
98         BOOST_CHECK_EQUAL( c.size(), 1u );
99
100         SENF_CHECK_NO_THROW( c.push_back( d1 ) );
101         BOOST_CHECK_EQUAL( c.bytes(), 7u );
102         BOOST_CHECK_EQUAL( c.size(), 2u );
103
104         SENF_CHECK_NO_THROW( c.push_back( d2 ) );
105         BOOST_CHECK_EQUAL( c.bytes(), 10u );
106         BOOST_CHECK_EQUAL( c.size(), 3u );
107
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->optionLength(), 2u);
113         BOOST_CHECK_EQUAL( *(boost::begin(cIter->value()) ), 0x40);
114         BOOST_CHECK_EQUAL( *(boost::next(boost::begin(cIter->value()) )), 0x34);
115         cIter++;
116         BOOST_CHECK_EQUAL( cIter->optionType(), 3u);
117         BOOST_CHECK_EQUAL( cIter->optionLength(), 1u);
118         BOOST_CHECK_EQUAL( *(boost::begin(cIter->value() )), 0x77);
119         cIter++;
120         BOOST_CHECK_EQUAL( cIter->optionType(), 7u);
121         BOOST_CHECK_EQUAL( cIter->optionLength(), 1u);
122         BOOST_CHECK_EQUAL( *(boost::begin(cIter->value())), 0x13);
123
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);
133
134         //clear whole list
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);
139     }
140 }
141
142 // Local Variables:
143 // mode: c++
144 // fill-column: 100
145 // comment-column: 40
146 // c-file-style: "senf"
147 // indent-tabs-mode: nil
148 // ispell-local-dictionary: "american"
149 // compile-command: "scons -u test"
150 // End: