switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Socket / SocketHandle.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
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 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief SocketHandle unit tests */
30
31 //#include "SocketHandle.test.hh"
32 //#include "SocketHandle.test.ih"
33
34 // Custom includes
35 #include "SocketHandle.hh"
36 #include "SocketProtocol.test.hh"
37 #include "AddressingPolicy.hh"
38
39 #include <senf/Utils/auto_unit_test.hh>
40 #include <boost/test/test_tools.hpp>
41
42 #define prefix_
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44
45 namespace {
46
47     class MySocketHandle
48         : public senf::SocketHandle<senf::test::SomeSocketProtocol::Policy>
49     {
50     public:
51         MySocketHandle()
52             : senf::SocketHandle<senf::test::SomeSocketProtocol::Policy>(
53                 std::auto_ptr<senf::SocketBody>(
54                     new senf::ProtocolSocketBody<senf::test::SomeSocketProtocol>(false, 0)))
55             {}
56     };
57
58     class FDHandle
59         : public senf::FileHandle
60     {
61     public:
62         FDHandle() : senf::FileHandle(std::auto_ptr<senf::FileBody>(new senf::FileBody())) {}
63     };
64
65 }
66
67 SENF_AUTO_UNIT_TEST(socketHandle)
68 {
69     typedef senf::MakeSocketPolicy<
70         senf::test::SomeCommunicationPolicy,
71         senf::test::SomeReadPolicy
72         >::policy OtherSocketPolicy;
73     typedef senf::SocketHandle<OtherSocketPolicy> OtherSocketHandle;
74
75     typedef senf::MakeSocketPolicy<
76         senf::test::SomeCommunicationPolicy,
77         senf::test::SomeAddressingPolicy
78         >::policy AnotherSocketPolicy;
79     typedef senf::SocketHandle<AnotherSocketPolicy> AnotherSocketHandle;
80
81     {
82         MySocketHandle myh;
83         OtherSocketHandle osh (myh);
84         osh = myh;
85
86         typedef senf::SocketHandle<senf::test::SomeSocketProtocol::Policy> SomeSocketHandle;
87         SomeSocketHandle ssh = senf::static_socket_cast<SomeSocketHandle>(osh);
88
89         SENF_CHECK_NO_THROW( senf::dynamic_socket_cast<SomeSocketHandle>(osh) );
90         SENF_CHECK_NO_THROW( senf::dynamic_socket_cast<AnotherSocketHandle>(osh) );
91
92         typedef senf::SocketHandle< senf::MakeSocketPolicy<
93             OtherSocketPolicy,
94             senf::NoAddressingPolicy
95             >::policy> SomeOtherSocketHandle;
96
97         BOOST_CHECK_THROW( senf::dynamic_socket_cast<SomeOtherSocketHandle>(osh),
98                            std::bad_cast );
99         BOOST_CHECK_THROW( senf::dynamic_socket_cast<SomeSocketHandle>(
100                                senf::FileHandle(FDHandle())),
101                            std::bad_cast );
102
103         BOOST_CHECK_EQUAL( myh.dumpState(),
104                 "file.handle: 0\n"
105                 "file.refcount: 3\n"
106                 "handle: senf::SocketHandle<senf::SocketPolicy<senf::test::SomeAddressingPolicy, senf::test::SomeFramingPolicy, senf::test::SomeCommunicationPolicy, senf::test::SomeReadPolicy, senf::test::SomeWritePolicy> >\n"
107                 "socket.protocol: senf::test::SomeSocketProtocol\n"
108                 "socket.protocol.policy: senf::SocketPolicy<senf::test::SomeAddressingPolicy, senf::test::SomeFramingPolicy, senf::test::SomeCommunicationPolicy, senf::test::SomeReadPolicy, senf::test::SomeWritePolicy>\n"
109                 "socket.server: false\n"
110                 "valid: true\n" );
111
112         SENF_CHECK_NO_THROW( myh.facet<senf::test::SomeSocketProtocol>() );
113     }
114
115     // Ensure, the destructor is called and calls the correct close() implementation
116     BOOST_CHECK( senf::test::SomeSocketProtocol::closeCount() >= 1u );
117 }
118
119 //-/////////////////////////////////////////////////////////////////////////////////////////////////
120 #undef prefix_
121
122 \f
123 // Local Variables:
124 // mode: c++
125 // fill-column: 100
126 // c-file-style: "senf"
127 // indent-tabs-mode: nil
128 // ispell-local-dictionary: "american"
129 // compile-command: "scons -u test"
130 // comment-column: 40
131 // End: