switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / 80221Bundle / MIHMessageRegistry.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2011
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Thorsten Horstmann <tho@berlios.de>
27
28 /** \file
29     \brief MIH Message-Registry unit tests */
30
31 //#include "MIHPacket.test.hh"
32 //#include "MIHPacket.test.ih"
33
34 // Custom includes
35 #include "MIHMessageRegistry.hh"
36 #include "MIHPacket.hh"
37
38 #include <senf/Utils/auto_unit_test.hh>
39 #include <boost/test/test_tools.hpp>
40
41 #define prefix_
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43
44 // we cannot put these test classes into the anonymous namespace because of a g++ bug
45 // see https://svn.boost.org/trac/boost/ticket/3729
46
47 namespace senf {
48 namespace test {
49
50     class TestMessagePacketParser
51         : public senf::PacketParserBase
52     {
53     #   include SENF_PARSER()
54         SENF_PARSER_FIELD ( registerRequestCodeTLV, senf::MIHRegisterReqCodeTLVParser );
55         SENF_PARSER_FINALIZE ( TestMessagePacketParser );
56     };
57
58
59     struct TestMessagePacketType
60         : public senf::PacketTypeBase,
61           public senf::PacketTypeMixin<TestMessagePacketType>
62     {
63         typedef senf::ConcretePacket<TestMessagePacketType> packet;
64         typedef senf::PacketTypeMixin<TestMessagePacketType> mixin;
65         typedef TestMessagePacketParser parser;
66
67         using mixin::nextPacketRange;
68         using mixin::init;
69         using mixin::initSize;
70
71         static const boost::uint16_t MESSAGE_ID;
72     };
73
74     struct ValidatedTestMessagePacketType
75         : public senf::PacketTypeBase,
76           public senf::PacketTypeMixin<ValidatedTestMessagePacketType>
77     {
78         typedef senf::ConcretePacket<ValidatedTestMessagePacketType> packet;
79         typedef senf::PacketTypeMixin<ValidatedTestMessagePacketType> mixin;
80         typedef TestMessagePacketParser parser;
81
82         using mixin::nextPacketRange;
83         using mixin::init;
84         using mixin::initSize;
85
86         static const boost::uint16_t MESSAGE_ID;
87
88         static void validate(packet message) {
89             message->registerRequestCodeTLV().validate();
90         }
91     };
92
93     typedef senf::ConcretePacket<TestMessagePacketType> TestMessagePacket;
94     typedef senf::ConcretePacket<ValidatedTestMessagePacketType> ValidatedTestMessagePacket;
95
96     const boost::uint16_t TestMessagePacketType::MESSAGE_ID = 42;
97     const boost::uint16_t ValidatedTestMessagePacketType::MESSAGE_ID = 43;
98 }
99 }
100
101 using namespace senf;
102
103 SENF_MIH_PACKET_REGISTRY_REGISTER( test::TestMessagePacket );
104 SENF_MIH_PACKET_REGISTRY_REGISTER( test::ValidatedTestMessagePacket );
105
106
107 SENF_AUTO_UNIT_TEST(MIHMessageRegistry_validate)
108 {
109     MIHPacket mihPacket (MIHPacket::create());
110     mihPacket->transactionId() = 21;
111     mihPacket->src_mihfId().value( "senf@berlios.de");
112     mihPacket->dst_mihfId().value( "test");
113
114     BOOST_CHECK_THROW( MIHPacketType::validate( mihPacket), InvalidMIHPacketException);
115
116     mihPacket.finalizeThis();
117     BOOST_CHECK_NO_THROW( MIHPacketType::validate( mihPacket));
118
119     {
120         test::TestMessagePacket testMessage (test::TestMessagePacket::createAfter(mihPacket));
121         mihPacket.finalizeAll();
122         BOOST_CHECK_NO_THROW( MIHPacketType::validate( mihPacket));
123     }
124     {
125         test::ValidatedTestMessagePacket testMessage (test::ValidatedTestMessagePacket::createAfter(mihPacket));
126         mihPacket.finalizeAll();
127         testMessage->registerRequestCodeTLV().value() << 3;
128         BOOST_CHECK_THROW( MIHPacketType::validate( mihPacket), InvalidMIHPacketException);
129         testMessage->registerRequestCodeTLV().value() << 1;
130         BOOST_CHECK_NO_THROW( MIHPacketType::validate( mihPacket));
131     }
132 }
133
134 //-/////////////////////////////////////////////////////////////////////////////////////////////////
135 #undef prefix_
136
137 \f
138 // Local Variables:
139 // mode: c++
140 // fill-column: 100
141 // c-file-style: "senf"
142 // indent-tabs-mode: nil
143 // ispell-local-dictionary: "american"
144 // compile-command: "scons -u test"
145 // comment-column: 40
146 // End: