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 <boost/test/auto_unit_test.hpp>
31 #include <boost/test/test_tools.hpp>
34 ///////////////////////////////cc.p////////////////////////////////////////
37 struct VoidPacket : public senf::PacketTypeBase {};
39 struct SimpleParser : public senf::PacketParserBase
41 SENF_PACKET_PARSER_INIT(SimpleParser);
43 using senf::PacketParserBase::check;
44 using senf::PacketParserBase::validate;
47 struct FooParser : public senf::PacketParserBase
49 SENF_PACKET_PARSER_INIT(FooParser);
51 SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
52 ((Field)( name, senf::Parse_UInt16 ))
53 ((Field)( id, senf::Parse_Int32 )) );
56 struct BarParser : public senf::PacketParserBase
58 SENF_PACKET_PARSER_INIT(BarParser);
60 SENF_PACKET_PARSER_DEFINE_FIELDS(
61 ((Field)( name, senf::Parse_UInt16 ))
62 ((Field)( id, senf::Parse_Int32 )) );
66 BOOST_AUTO_UNIT_TEST(packetParserBase)
68 senf::PacketInterpreter<VoidPacket>::ptr pi (senf::PacketInterpreter<VoidPacket>::create(6u));
69 SimpleParser p (pi->data().begin(),&pi->data());
71 BOOST_CHECK( pi->data().begin() == p.i() );
72 BOOST_CHECK( p.check(6u) );
73 BOOST_CHECK( ! p.check(7u) );
74 BOOST_CHECK_NO_THROW( p.validate(6u) );
75 BOOST_CHECK_THROW( p.validate(7u), senf::TruncatedPacketException );
77 // ?? Why the heck do I need the +0? I get an 'undefined symbol FooParser::fixed_bytes'
79 BOOST_CHECK_EQUAL( FooParser::fixed_bytes+0, 6u );
80 BOOST_CHECK_EQUAL( BarParser(pi->data().begin(),&pi->data()).bytes(), 6u );
81 BOOST_CHECK_EQUAL( senf::bytes(senf::Parse_UInt16(pi->data().begin(),&pi->data())), 2u );
82 BOOST_CHECK_EQUAL( senf::bytes(FooParser(pi->data().begin(),&pi->data())), 6u );
83 BOOST_CHECK_EQUAL( senf::bytes(BarParser(pi->data().begin(),&pi->data())), 6u );
85 BOOST_CHECK_EQUAL( senf::init_bytes<FooParser>::value, 6u );
86 BOOST_CHECK_EQUAL( senf::init_bytes<BarParser>::value, 6u );
89 BOOST_AUTO_UNIT_TEST(safePacketParser)
91 senf::PacketInterpreter<VoidPacket>::ptr pi (senf::PacketInterpreter<VoidPacket>::create(6u));
92 senf::SafePacketParser<senf::Parse_UInt16> p;
96 p = senf::Parse_UInt16(pi->data().begin(),&pi->data());
101 BOOST_CHECK_EQUAL( (*p), 0x1234u );
102 BOOST_CHECK_EQUAL( p->data()[0], 0x12u );
104 p->data().resize(1024u);
105 BOOST_CHECK_EQUAL( (*p), 0x1234u );
107 BOOST_CHECK_EQUAL( p->data()[0], 0x23u );
110 ///////////////////////////////cc.e////////////////////////////////////////
117 // c-file-style: "senf"
118 // indent-tabs-mode: nil
119 // ispell-local-dictionary: "american"
120 // compile-command: "scons -u test"
121 // comment-column: 40