switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / DefaultBundle / ListOptionTypeParser.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Philipp Batroff <pug@berlios.de>
27
28 // Custom includes
29 #include <senf/Packets/Packets.hh>
30 #include "IPv6ExtOptions.hh"
31 #include "ListOptionTypeParser.hh"
32
33 #include <senf/Utils/auto_unit_test.hh>
34 #include <boost/test/test_tools.hpp>
35
36 #define prefix_
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38
39 namespace {
40     struct VoidPacket : public senf::PacketTypeBase
41     {};
42
43     struct OptionParser : public senf::PacketParserBase
44     {
45 #       include SENF_PARSER()
46
47         SENF_PARSER_FIELD( size, senf::UInt8Parser );
48         typedef senf::detail::FixedAuxParserPolicy<senf::UInt8Parser, 1u> ListOptionTypeAuxPolicy;
49         typedef senf::detail::ListOptionTypeParser_Policy<
50             senf::IPv6GenericOptionParser, ListOptionTypeAuxPolicy> ListOptionTypePolicy;
51         typedef senf::ListParser<ListOptionTypePolicy> ListOptionTypeParser;
52         SENF_PARSER_FIELD ( list, ListOptionTypeParser);
53
54         SENF_PARSER_FINALIZE(OptionParser);
55     };
56 }
57
58 SENF_AUTO_UNIT_TEST(ListOptionTypeParser)
59 {
60     senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
61             OptionParser::init_bytes));
62     OptionParser p ( pi->data().begin() ,&pi->data());
63     BOOST_CHECK_EQUAL(OptionParser::init_bytes +0, 7u);
64
65     p.list().init();
66
67     BOOST_CHECK_EQUAL( p.list().size(), 0u );
68     BOOST_CHECK_EQUAL( p.list().bytes(), 6u ); //padding has to be accounted for!
69     BOOST_CHECK( p.list().empty() );
70
71      // the mutators are really tested together with the container wrappers since they are based
72      // on the container wrapper. Here we only need one call to make the list larger ...
73
74     // this doesn't make sense, since padding is added and the size doesn't really change
75     // should be tested later with the container
76     p.list().push_back_space();
77     p = OptionParser(pi->data().begin(),&pi->data());
78     BOOST_CHECK_EQUAL( p.list().bytes(), 6u );  //padding bytes
79     BOOST_CHECK_EQUAL( p.list().size(), 1u );   //padding
80     BOOST_CHECK_EQUAL( p.list().empty(), false );
81 }
82
83 SENF_AUTO_UNIT_TEST(ListOptionTypeParser_container)
84 {
85     senf::PacketInterpreterBase::ptr pi (senf::PacketInterpreter<VoidPacket>::create(
86             OptionParser::init_bytes));
87
88     {
89         OptionParser p ( pi->data().begin() ,&pi->data());
90         p.init();
91         OptionParser::list_t::container c (p.list());
92
93         BOOST_CHECK_EQUAL( c.size(), 0u );
94         BOOST_CHECK_EQUAL( c.bytes(), 0u ); // padding bytes wont be in here, added/removed automatically in destructor
95         BOOST_CHECK( c.begin() == c.end() );
96
97         std::vector<unsigned char> d (2, 0xab);
98         std::vector<unsigned char> d1 (1, 0x77);
99         std::vector<unsigned char> d2 (1, 0x13);
100
101         SENF_CHECK_NO_THROW( c.push_back( std::make_pair(0x65, d) ));
102         BOOST_CHECK_EQUAL( c.bytes(), 4u );
103         BOOST_CHECK_EQUAL( c.size(), 1u );
104
105         SENF_CHECK_NO_THROW( c.push_back( std::make_pair(0x03, d1) ));
106         BOOST_CHECK_EQUAL( c.bytes(), 7u );
107         BOOST_CHECK_EQUAL( c.size(), 2u );
108
109         SENF_CHECK_NO_THROW( c.push_back( std::make_pair(0x07, d2) ));
110         BOOST_CHECK_EQUAL( c.bytes(), 10u );
111         BOOST_CHECK_EQUAL( c.size(), 3u );
112
113         OptionParser::list_t::container::iterator cIter (c.begin());
114         BOOST_CHECK_EQUAL( cIter->altAction(), 1u);
115         BOOST_CHECK_EQUAL( cIter->changeFlag(), 1u);
116         BOOST_CHECK_EQUAL( cIter->optionType(), 5u);
117         BOOST_CHECK_EQUAL( cIter->length(), 2u);
118         BOOST_CHECK_EQUAL( *(boost::begin(cIter->value()) ), 0xab);
119         BOOST_CHECK_EQUAL( *(boost::next(boost::begin(cIter->value()) )), 0xab);
120         cIter++;
121         BOOST_CHECK_EQUAL( cIter->optionType(), 3u);
122         BOOST_CHECK_EQUAL( cIter->length(), 1u);
123         BOOST_CHECK_EQUAL( *(boost::begin(cIter->value() )), 0x77);
124         cIter++;
125         BOOST_CHECK_EQUAL( cIter->optionType(), 7u);
126         BOOST_CHECK_EQUAL( cIter->length(), 1u);
127         BOOST_CHECK_EQUAL( *(boost::begin(cIter->value())), 0x13);
128
129         //deletes first element
130         c.erase(c.begin(),1);
131         BOOST_CHECK_EQUAL( c.size(), 2u );
132         BOOST_CHECK_EQUAL( c.bytes(), 6u );
133         //deletes first 2 elements
134         c.erase(c.begin(),1);
135         BOOST_CHECK_EQUAL( c.size(), 1u );
136         BOOST_CHECK_EQUAL( c.bytes(), 3u );
137         BOOST_CHECK_EQUAL( c.empty(), false);
138
139         //clear whole list
140         SENF_CHECK_NO_THROW( c.clear() );
141         BOOST_CHECK_EQUAL( c.size(), 0u );
142         BOOST_CHECK_EQUAL( c.bytes(), 0u );
143         BOOST_CHECK_EQUAL( c.empty(), true);
144     }
145 }
146 //-/////////////////////////////////////////////////////////////////////////////////////////////////
147
148 \f
149 // Local Variables:
150 // mode: c++
151 // fill-column: 100
152 // comment-column: 40
153 // c-file-style: "senf"
154 // indent-tabs-mode: nil
155 // ispell-local-dictionary: "american"
156 // compile-command: "scons -u test"
157 // End: