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"
28 #include "PacketParser.hh"
29 #include "PacketInterpreter.hh"
30 #include "PacketType.hh"
31 #include "ParseInt.hh"
33 #include <boost/test/auto_unit_test.hpp>
34 #include <boost/test/test_tools.hpp>
37 ///////////////////////////////cc.p////////////////////////////////////////
40 struct VoidPacket : public senf::PacketTypeBase {};
42 struct SimpleParser : public senf::PacketParserBase
44 SENF_PACKET_PARSER_INIT(SimpleParser);
46 using senf::PacketParserBase::check;
47 using senf::PacketParserBase::validate;
50 struct FooParser : public senf::PacketParserBase
52 SENF_PACKET_PARSER_INIT(FooParser);
54 SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
55 ((Field)( name, senf::Parse_UInt16 ))
56 ((Field)( id, senf::Parse_Int32 )) );
59 struct BarParser : public senf::PacketParserBase
61 SENF_PACKET_PARSER_INIT(BarParser);
63 SENF_PACKET_PARSER_DEFINE_FIELDS(
64 ((Field)( name, senf::Parse_UInt16 ))
65 ((Field)( id, senf::Parse_Int32 )) );
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"