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