bacb7ac8787f19c7f71f618ea32dff93be6c02c3
[senf.git] / senf / Socket / ServerSocketHandle.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 ServerSocketHandle unit tests */
25
26 //#include "ServerSocketHandle.test.hh"
27 //#include "ServerSocketHandle.test.ih"
28
29 // Custom includes
30 #include "ServerSocketHandle.hh"
31 #include "ClientSocketHandle.hh"
32 #include "SocketProtocol.test.hh"
33 #include "AddressingPolicy.hh"
34
35 #include <senf/Utils/auto_unit_test.hh>
36 #include <boost/test/test_tools.hpp>
37
38 #define prefix_
39 ///////////////////////////////cc.p////////////////////////////////////////
40
41 namespace {
42
43     class MySocketHandle
44         : public senf::ServerSocketHandle<senf::test::SomeSocketProtocol::Policy>
45     {
46     public:
47         MySocketHandle()
48             : senf::ServerSocketHandle<senf::test::SomeSocketProtocol::Policy>(
49                 std::auto_ptr<senf::SocketBody>(
50                     new senf::ProtocolSocketBody<senf::test::SomeSocketProtocol>(true)))
51             {}
52     };
53
54 }
55
56 SENF_AUTO_UNIT_TEST(serverSocketHandle)
57 {
58     typedef senf::MakeSocketPolicy<
59         senf::test::SomeFramingPolicy,
60         senf::test::SomeReadPolicy,
61         senf::test::SomeWritePolicy
62         >::policy OtherSocketPolicy;
63     typedef senf::SocketHandle<OtherSocketPolicy> OtherSocketHandle;
64
65     MySocketHandle myh;
66     OtherSocketHandle osh (myh);
67     osh = myh;
68
69     typedef senf::ServerSocketHandle<senf::test::SomeSocketProtocol::Policy> SomeSocketHandle;
70     SomeSocketHandle ssh = senf::static_socket_cast<SomeSocketHandle>(osh);
71
72     typedef senf::ServerSocketHandle<senf::MakeSocketPolicy<
73         OtherSocketPolicy,
74         senf::NoAddressingPolicy
75         >::policy> SomeOtherSocketHandle;
76     typedef senf::ClientSocketHandle<OtherSocketPolicy> OtherClientHandle;
77
78     SENF_CHECK_NO_THROW( senf::dynamic_socket_cast<SomeSocketHandle>(osh) );
79     BOOST_CHECK_THROW( senf::dynamic_socket_cast<SomeOtherSocketHandle>(osh),
80                        std::bad_cast );
81     BOOST_CHECK_THROW( senf::dynamic_socket_cast<OtherClientHandle>(osh),
82                        std::bad_cast );
83
84     SENF_CHECK_NO_THROW( myh.bind(0) );
85     BOOST_CHECK_EQUAL( myh.local(), 2u );
86
87     {
88         MySocketHandle::ClientHandle client = myh.accept();
89         BOOST_CHECK_EQUAL( client.fd(), -1 );
90     }
91
92 }
93
94
95 ///////////////////////////////cc.e////////////////////////////////////////
96 #undef prefix_
97
98 \f
99 // Local Variables:
100 // mode: c++
101 // fill-column: 100
102 // c-file-style: "senf"
103 // indent-tabs-mode: nil
104 // ispell-local-dictionary: "american"
105 // compile-command: "scons -u test"
106 // comment-column: 40
107 // End: