PPI: Checkin of first compiling (yet not working) version
[senf.git] / Packets / PacketParser.test.cc
1 // Copyright (C) 2007 
2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 //     Stefan Bund <g0dil@berlios.de>
5 //
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.
10 //
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.
15 //
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.
20
21 /** \file
22     \brief PacketParser.test unit tests */
23
24 //#include "PacketParser.test.hh"
25 //#include "PacketParser.test.ih"
26
27 // Custom includes
28 #include "Packets.hh"
29
30 #include <boost/test/auto_unit_test.hpp>
31 #include <boost/test/test_tools.hpp>
32
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 namespace {
37     struct VoidPacket : public senf::PacketTypeBase {};
38
39     struct SimpleParser : public senf::PacketParserBase
40     {
41         SENF_PACKET_PARSER_INIT(SimpleParser);
42         
43         using senf::PacketParserBase::check;
44         using senf::PacketParserBase::validate;
45     };
46
47     struct FooParser : public senf::PacketParserBase
48     {
49         SENF_PACKET_PARSER_INIT(FooParser);
50
51         SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
52             ((Field)( name, senf::Parse_UInt16 ))
53             ((Field)( id,   senf::Parse_Int32  )) );
54     };
55
56     struct BarParser : public senf::PacketParserBase
57     {
58         SENF_PACKET_PARSER_INIT(BarParser);
59
60         SENF_PACKET_PARSER_DEFINE_FIELDS(
61             ((Field)( name, senf::Parse_UInt16 ))
62             ((Field)( id,   senf::Parse_Int32  )) );
63     };
64 }
65
66 BOOST_AUTO_UNIT_TEST(packetParserBase)
67 {
68     senf::PacketInterpreter<VoidPacket>::ptr pi (senf::PacketInterpreter<VoidPacket>::create(6u));
69     SimpleParser p (pi->data().begin(),&pi->data());
70
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 );
76
77     // ?? Why the heck do I need the +0? I get an 'undefined symbol FooParser::fixed_bytes'
78     // otherwise ...
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 );
84
85     BOOST_CHECK_EQUAL( senf::init_bytes<FooParser>::value, 6u );
86     BOOST_CHECK_EQUAL( senf::init_bytes<BarParser>::value, 6u );
87 }
88
89 BOOST_AUTO_UNIT_TEST(safePacketParser)
90 {
91     senf::PacketInterpreter<VoidPacket>::ptr pi (senf::PacketInterpreter<VoidPacket>::create(6u));
92     senf::SafePacketParser<senf::Parse_UInt16> p;
93     
94     BOOST_CHECK( !p );
95
96     p =  senf::Parse_UInt16(pi->data().begin(),&pi->data());
97
98     BOOST_CHECK( p );
99     (*p) = 0x1234u;
100     
101     BOOST_CHECK_EQUAL( (*p), 0x1234u );
102     BOOST_CHECK_EQUAL( p->data()[0], 0x12u );
103
104     p->data().resize(1024u);
105     BOOST_CHECK_EQUAL( (*p), 0x1234u );
106     (*p) = 0x2345u;
107     BOOST_CHECK_EQUAL( p->data()[0], 0x23u );
108 }
109
110 ///////////////////////////////cc.e////////////////////////////////////////
111 #undef prefix_
112
113 \f
114 // Local Variables:
115 // mode: c++
116 // fill-column: 100
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
122 // End: