Socket: BUGFIX: Move incorrect v-function call out of FileBody destructor
[senf.git] / 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 // Unit tests
24
25 //#include "SocketHandle.test.hh"
26 //#include "SocketHandle.test.ih"
27
28 // Custom includes
29 #include "SocketHandle.hh"
30 #include "SocketProtocol.test.hh"
31 #include "AddressingPolicy.hh"
32
33 #include "../Utils/auto_unit_test.hh"
34 #include <boost/test/test_tools.hpp>
35
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 namespace {
40
41     class MySocketHandle
42         : public senf::SocketHandle<senf::test::SomeProtocol::Policy>
43     {
44     public:
45         MySocketHandle()
46             : senf::SocketHandle<senf::test::SomeProtocol::Policy>(
47                 std::auto_ptr<senf::SocketBody>(
48                     new senf::ProtocolSocketBody<senf::test::SomeProtocol>(false, 0)))
49             {}
50     };
51
52     class FDHandle
53         : public senf::FileHandle
54     {
55     public:
56         FDHandle() : senf::FileHandle(std::auto_ptr<senf::FileBody>(new senf::FileBody())) {}
57     };
58
59 }
60
61 BOOST_AUTO_UNIT_TEST(socketHandle)
62 {
63     typedef senf::MakeSocketPolicy<
64         senf::test::SomeCommunicationPolicy,
65         senf::test::SomeReadPolicy
66         >::policy OtherSocketPolicy;
67     typedef senf::SocketHandle<OtherSocketPolicy> OtherSocketHandle;
68
69     {
70         MySocketHandle myh;
71         OtherSocketHandle osh (myh);
72         osh = myh;
73
74         typedef senf::SocketHandle<senf::test::SomeProtocol::Policy> SomeSocketHandle;
75         SomeSocketHandle ssh = senf::static_socket_cast<SomeSocketHandle>(osh);
76
77         BOOST_CHECK_NO_THROW( senf::dynamic_socket_cast<SomeSocketHandle>(osh) );
78
79         typedef senf::SocketHandle< senf::MakeSocketPolicy<
80             OtherSocketPolicy,
81             senf::NoAddressingPolicy
82             >::policy> SomeOtherSocketHandle;
83
84         BOOST_CHECK_THROW( senf::dynamic_socket_cast<SomeOtherSocketHandle>(osh),
85                            std::bad_cast );
86         BOOST_CHECK_THROW( senf::dynamic_socket_cast<SomeSocketHandle>(
87                                senf::FileHandle(FDHandle())),
88                            std::bad_cast );
89
90         BOOST_CHECK_EQUAL( myh.dumpState(),
91                            "file.handle: 0\n"
92                            "file.refcount: 3\n"
93                            "handle: senf::SocketHandle<senf::SocketPolicy<senf::test::SomeAddressingPolicy, senf::test::SomeFramingPolicy, senf::test::SomeCommunicationPolicy, senf::test::SomeReadPolicy, senf::test::SomeWritePolicy> >\n"
94                            "socket.protocol: senf::test::SomeProtocol\n"
95                            "socket.protocol.policy: senf::SocketPolicy<senf::test::SomeAddressingPolicy, senf::test::SomeFramingPolicy, senf::test::SomeCommunicationPolicy, senf::test::SomeReadPolicy, senf::test::SomeWritePolicy>\n"
96                            "socket.server: false\n" );
97     }
98     
99     // Ensure, the destructor is called and calls the correct close() implementation
100     BOOST_CHECK_EQUAL( senf::test::SomeProtocol::closeCount(), 1u );
101 }
102
103 ///////////////////////////////cc.e////////////////////////////////////////
104 #undef prefix_
105
106 \f
107 // Local Variables:
108 // mode: c++
109 // fill-column: 100
110 // c-file-style: "senf"
111 // indent-tabs-mode: nil
112 // ispell-local-dictionary: "american"
113 // compile-command: "scons -u test"
114 // comment-column: 40
115 // End: