Packets/80221Bundle: extended MIHMessageType / MIHMessageRegistry to validate MIH...
[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 using namespace senf;
39
40 namespace {
41
42     class TestMessagePacketParser
43         : public senf::PacketParserBase
44     {
45     #   include SENF_PARSER()
46         SENF_PARSER_FIELD ( registerRequestCodeTLV, senf::MIHRegisterReqCodeTLVParser );
47         SENF_PARSER_FINALIZE ( TestMessagePacketParser );
48     };
49
50
51     struct TestMessagePacketType
52         : public senf::PacketTypeBase,
53           public senf::PacketTypeMixin<TestMessagePacketType>
54     {
55         typedef senf::ConcretePacket<TestMessagePacketType> packet;
56         typedef senf::PacketTypeMixin<TestMessagePacketType> mixin;
57         typedef TestMessagePacketParser parser;
58
59         using mixin::nextPacketRange;
60         using mixin::init;
61         using mixin::initSize;
62
63         static const boost::uint16_t MESSAGE_ID;
64     };
65
66     struct ValidatedTestMessagePacketType
67         : public senf::PacketTypeBase,
68           public senf::PacketTypeMixin<ValidatedTestMessagePacketType>
69     {
70         typedef senf::ConcretePacket<ValidatedTestMessagePacketType> packet;
71         typedef senf::PacketTypeMixin<ValidatedTestMessagePacketType> mixin;
72         typedef TestMessagePacketParser parser;
73
74         using mixin::nextPacketRange;
75         using mixin::init;
76         using mixin::initSize;
77
78         static const boost::uint16_t MESSAGE_ID;
79
80         static std::pair<bool, std::string> validate(packet message) {
81             return std::make_pair(message->registerRequestCodeTLV().value() == 1, "");
82         }
83     };
84
85     typedef senf::ConcretePacket<TestMessagePacketType> TestMessagePacket;
86     typedef senf::ConcretePacket<ValidatedTestMessagePacketType> ValidatedTestMessagePacket;
87
88     const boost::uint16_t TestMessagePacketType::MESSAGE_ID = 42;
89     const boost::uint16_t ValidatedTestMessagePacketType::MESSAGE_ID = 43;
90
91     SENF_MIH_PACKET_REGISTRY_REGISTER( TestMessagePacket );
92     SENF_MIH_PACKET_REGISTRY_REGISTER( ValidatedTestMessagePacket );
93 }
94
95
96 SENF_AUTO_UNIT_TEST(MIHMessageRegistry_validate)
97 {
98     MIHPacket mihPacket (MIHPacket::create());
99     mihPacket->transactionId() = 21;
100     mihPacket->src_mihfId().value( "senf@berlios.de");
101     mihPacket->dst_mihfId().value( "test");
102
103     BOOST_CHECK(! MIHPacketType::validate( mihPacket).first);
104
105     mihPacket.finalizeThis();
106     BOOST_CHECK( MIHPacketType::validate( mihPacket).first);
107
108     {
109         TestMessagePacket testMessage (TestMessagePacket::createAfter(mihPacket));
110         mihPacket.finalizeAll();
111         BOOST_CHECK( MIHPacketType::validate( mihPacket).first);
112     }
113     {
114         ValidatedTestMessagePacket testMessage (ValidatedTestMessagePacket::createAfter(mihPacket));
115         mihPacket.finalizeAll();
116         BOOST_CHECK(! MIHPacketType::validate( mihPacket).first);
117         testMessage->registerRequestCodeTLV().value() << 1;
118         BOOST_CHECK( MIHPacketType::validate( mihPacket).first);
119     }
120 }
121
122 //-/////////////////////////////////////////////////////////////////////////////////////////////////
123 #undef prefix_
124
125 \f
126 // Local Variables:
127 // mode: c++
128 // fill-column: 100
129 // c-file-style: "senf"
130 // indent-tabs-mode: nil
131 // ispell-local-dictionary: "american"
132 // compile-command: "scons -u test"
133 // comment-column: 40
134 // End: