9fb12ef0134a83ee1ccb6905992135ef0b8a7b33
[senf.git] / senf / Packets / 80221Bundle / MIHMessageRegistry.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2011
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Thorsten Horstmann <tho@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief MIH Message-Registry unit tests */
25
26 //#include "MIHPacket.test.hh"
27 //#include "MIHPacket.test.ih"
28
29 // Custom includes
30 #include "MIHMessageRegistry.hh"
31 #include "MIHPacket.hh"
32
33 #include <senf/Utils/auto_unit_test.hh>
34 #include <boost/test/test_tools.hpp>
35
36 #define prefix_
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38
39 // we cannot put these test classes into the anonymous namespace because of a g++ bug
40 // see https://svn.boost.org/trac/boost/ticket/3729
41
42 namespace senf {
43 namespace test {
44
45     class TestMessagePacketParser
46         : public senf::PacketParserBase
47     {
48     #   include SENF_PARSER()
49         SENF_PARSER_FIELD ( registerRequestCodeTLV, senf::MIHRegisterReqCodeTLVParser );
50         SENF_PARSER_FINALIZE ( TestMessagePacketParser );
51     };
52
53
54     struct TestMessagePacketType
55         : public senf::PacketTypeBase,
56           public senf::PacketTypeMixin<TestMessagePacketType>
57     {
58         typedef senf::ConcretePacket<TestMessagePacketType> packet;
59         typedef senf::PacketTypeMixin<TestMessagePacketType> mixin;
60         typedef TestMessagePacketParser parser;
61
62         using mixin::nextPacketRange;
63         using mixin::init;
64         using mixin::initSize;
65
66         static const boost::uint16_t MESSAGE_ID;
67     };
68
69     struct ValidatedTestMessagePacketType
70         : public senf::PacketTypeBase,
71           public senf::PacketTypeMixin<ValidatedTestMessagePacketType>
72     {
73         typedef senf::ConcretePacket<ValidatedTestMessagePacketType> packet;
74         typedef senf::PacketTypeMixin<ValidatedTestMessagePacketType> mixin;
75         typedef TestMessagePacketParser parser;
76
77         using mixin::nextPacketRange;
78         using mixin::init;
79         using mixin::initSize;
80
81         static const boost::uint16_t MESSAGE_ID;
82
83         static void validate(packet message) {
84             message->registerRequestCodeTLV().validate();
85         }
86     };
87
88     typedef senf::ConcretePacket<TestMessagePacketType> TestMessagePacket;
89     typedef senf::ConcretePacket<ValidatedTestMessagePacketType> ValidatedTestMessagePacket;
90
91     const boost::uint16_t TestMessagePacketType::MESSAGE_ID = 42;
92     const boost::uint16_t ValidatedTestMessagePacketType::MESSAGE_ID = 43;
93 }
94 }
95
96 using namespace senf;
97
98 SENF_MIH_PACKET_REGISTRY_REGISTER( test::TestMessagePacket );
99 SENF_MIH_PACKET_REGISTRY_REGISTER( test::ValidatedTestMessagePacket );
100
101
102 SENF_AUTO_UNIT_TEST(MIHMessageRegistry_validate)
103 {
104     MIHPacket mihPacket (MIHPacket::create());
105     mihPacket->transactionId() = 21;
106     mihPacket->src_mihfId().value( "senf@berlios.de");
107     mihPacket->dst_mihfId().value( "test");
108
109     BOOST_CHECK_THROW( MIHPacketType::validate( mihPacket), InvalidMIHPacketException);
110
111     mihPacket.finalizeThis();
112     BOOST_CHECK_NO_THROW( MIHPacketType::validate( mihPacket));
113
114     {
115         test::TestMessagePacket testMessage (test::TestMessagePacket::createAfter(mihPacket));
116         mihPacket.finalizeAll();
117         BOOST_CHECK_NO_THROW( MIHPacketType::validate( mihPacket));
118     }
119     {
120         test::ValidatedTestMessagePacket testMessage (test::ValidatedTestMessagePacket::createAfter(mihPacket));
121         mihPacket.finalizeAll();
122         testMessage->registerRequestCodeTLV().value() << 3;
123         BOOST_CHECK_THROW( MIHPacketType::validate( mihPacket), InvalidMIHPacketException);
124         testMessage->registerRequestCodeTLV().value() << 1;
125         BOOST_CHECK_NO_THROW( MIHPacketType::validate( mihPacket));
126     }
127 }
128
129 //-/////////////////////////////////////////////////////////////////////////////////////////////////
130 #undef prefix_
131
132 \f
133 // Local Variables:
134 // mode: c++
135 // fill-column: 100
136 // c-file-style: "senf"
137 // indent-tabs-mode: nil
138 // ispell-local-dictionary: "american"
139 // compile-command: "scons -u test"
140 // comment-column: 40
141 // End: