2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 // Stefan Bund <g0dil@berlios.de>
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 \brief PacketParser.test unit tests */
24 //#include "PacketParser.test.hh"
25 //#include "PacketParser.test.ih"
30 #include "../Utils/auto_unit_test.hh"
31 #include <boost/test/test_tools.hpp>
34 ///////////////////////////////cc.p////////////////////////////////////////
37 struct VoidPacket : public senf::PacketTypeBase {};
39 struct SimpleParser : public senf::PacketParserBase
41 # include SENF_FIXED_PARSER()
42 SENF_PARSER_FINALIZE(SimpleParser);
44 using senf::PacketParserBase::check;
45 using senf::PacketParserBase::validate;
48 struct FooParser : public senf::PacketParserBase
50 # include SENF_FIXED_PARSER()
52 SENF_PARSER_FIELD( name, senf::Parse_UInt16 );
53 SENF_PARSER_FIELD( id, senf::Parse_Int32 );
55 SENF_PARSER_FINALIZE(FooParser);
58 struct BarParser : public senf::PacketParserBase
60 # include SENF_PARSER()
62 SENF_PARSER_FIELD( name, senf::Parse_UInt16 );
63 SENF_PARSER_FIELD( id, senf::Parse_Int32 );
65 SENF_PARSER_FINALIZE(BarParser);
69 BOOST_AUTO_UNIT_TEST(packetParserBase)
71 senf::PacketInterpreter<VoidPacket>::ptr pi (senf::PacketInterpreter<VoidPacket>::create(6u));
72 SimpleParser p (pi->data().begin(),&pi->data());
74 BOOST_CHECK( pi->data().begin() == p.i() );
75 BOOST_CHECK( p.check(6u) );
76 BOOST_CHECK( ! p.check(7u) );
77 BOOST_CHECK_NO_THROW( p.validate(6u) );
78 BOOST_CHECK_THROW( p.validate(7u), senf::TruncatedPacketException );
80 // ?? Why the heck do I need the +0? I get an 'undefined symbol FooParser::fixed_bytes'
82 BOOST_CHECK_EQUAL( FooParser::fixed_bytes+0, 6u );
83 BOOST_CHECK_EQUAL( BarParser(pi->data().begin(),&pi->data()).bytes(), 6u );
84 BOOST_CHECK_EQUAL( senf::bytes(senf::Parse_UInt16(pi->data().begin(),&pi->data())), 2u );
85 BOOST_CHECK_EQUAL( senf::bytes(FooParser(pi->data().begin(),&pi->data())), 6u );
86 BOOST_CHECK_EQUAL( senf::bytes(BarParser(pi->data().begin(),&pi->data())), 6u );
88 BOOST_CHECK_EQUAL( senf::init_bytes<FooParser>::value, 6u );
89 BOOST_CHECK_EQUAL( senf::init_bytes<BarParser>::value, 6u );
92 BOOST_AUTO_UNIT_TEST(safePacketParser)
94 senf::PacketInterpreter<VoidPacket>::ptr pi (senf::PacketInterpreter<VoidPacket>::create(6u));
95 senf::SafePacketParser<senf::Parse_UInt16> p;
99 p = senf::Parse_UInt16(pi->data().begin(),&pi->data());
104 BOOST_CHECK_EQUAL( (*p), 0x1234u );
105 BOOST_CHECK_EQUAL( p->data()[0], 0x12u );
107 p->data().resize(1024u);
108 BOOST_CHECK_EQUAL( (*p), 0x1234u );
110 BOOST_CHECK_EQUAL( p->data()[0], 0x23u );
113 ///////////////////////////////cc.e////////////////////////////////////////
120 // c-file-style: "senf"
121 // indent-tabs-mode: nil
122 // ispell-local-dictionary: "american"
123 // compile-command: "scons -u test"
124 // comment-column: 40