6034a00ba6936fb0dec3c75c5eecfa9a5c20afde
[senf.git] / senf / Socket / ClientSocketHandle.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 ClientSocketHandle unit tests */
25
26 //#include "ClientSocketHandle.test.hh"
27 //#include "ClientSocketHandle.test.ih"
28
29 // Custom includes
30 #include "SocketPolicy.test.hh"
31 #include "SocketProtocol.test.hh"
32 #include "ClientSocketHandle.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::ClientSocketHandle<senf::test::SomeSocketProtocol::Policy>
45     {
46     public:
47         MySocketHandle()
48             : senf::ClientSocketHandle<senf::test::SomeSocketProtocol::Policy>(
49                 std::auto_ptr<senf::SocketBody>(
50                     new senf::ProtocolSocketBody<senf::test::SomeSocketProtocol>(false)))
51             {}
52     };
53
54 }
55
56 SENF_AUTO_UNIT_TEST(clientSocketHandle)
57 {
58     BOOST_CHECKPOINT("Constructing socket handle");
59     MySocketHandle myh;
60
61     // conversion to other socket handles
62     {
63         typedef senf::MakeSocketPolicy<
64             senf::test::SomeFramingPolicy,
65             senf::test::SomeReadPolicy,
66             senf::test::SomeWritePolicy
67             >::policy OtherSocketPolicy;
68         typedef senf::SocketHandle<OtherSocketPolicy> OtherSocketHandle;
69
70         BOOST_CHECKPOINT("Copy-constructing socket handle");
71         OtherSocketHandle osh (myh);
72         BOOST_CHECKPOINT("Assigning socket handle");
73         osh = myh;
74         typedef senf::ClientSocketHandle<senf::test::SomeSocketProtocol::Policy> SomeSocketHandle;
75         BOOST_CHECKPOINT("static_casting socket handle");
76         SomeSocketHandle ssh =
77             senf::static_socket_cast<SomeSocketHandle>(osh);
78         SENF_CHECK_NO_THROW( senf::dynamic_socket_cast<SomeSocketHandle>(osh) );
79         typedef senf::ClientSocketHandle<senf::MakeSocketPolicy<
80             OtherSocketPolicy,
81             senf::NoAddressingPolicy
82             >::policy> SomeOtherSocketHandle;
83         BOOST_CHECK_THROW( senf::dynamic_socket_cast<SomeOtherSocketHandle>(osh),
84                            std::bad_cast );
85     }
86
87     // reading and writing
88     SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( myh.read(), "TEST-READ" ) );
89     {
90         std::string buf("FOO-BAR");
91         SENF_CHECK_NO_THROW( myh.read(buf,0) );
92         BOOST_CHECK_EQUAL( buf, "TEST-READ" );
93     }
94     {
95         char buf[11];
96         ::strcpy(buf,"0123456789");
97         SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( myh.read(buf,buf+10), buf+9 ) );
98         BOOST_CHECK_EQUAL( buf, "TEST-READ9" );
99     }
100
101     SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( myh.readfrom().first, "TEST-READ" ) );
102     {
103         std::string buf("FOO-BAR");
104         unsigned addr;
105         SENF_CHECK_NO_THROW( myh.readfrom(buf,addr,0) );
106         BOOST_CHECK_EQUAL( buf, "TEST-READ" );
107     }
108     {
109         char buf[11];
110         unsigned addr;
111         ::strcpy(buf,"0123456789");
112         SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( myh.readfrom(buf,buf+10,addr), buf+9 ) );
113         BOOST_CHECK_EQUAL( buf, "TEST-READ9" );
114     }
115
116     {
117         std::string s ("TEST-WRITE");
118         SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( myh.write(s)-s.begin(), 10 ) );
119         s = "TEST";
120         // This simulates a closed file in this test policy. However, we
121         // have changed the semantics so this will not work anymore.
122         // BOOST_CHECK_THROW( myh.write(s),senf::SystemException );
123         char const * const s1 = "TEST-WRITE9";
124         SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( myh.write(s1,s1+10), s1+10u ) );
125         s = "TEST-WRITE";
126         SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( myh.writeto(0,s)-s.begin(), 10 ) );
127         SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( myh.writeto(0,s1,s1+10), s1+10 ) );
128     }
129
130     SENF_CHECK_NO_THROW( myh.connect(0) );
131     SENF_CHECK_NO_THROW( myh.bind(0) );
132     SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( myh.peer(), 1u ) );
133     SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( myh.local(), 2u ) );
134 }
135
136 ///////////////////////////////cc.e////////////////////////////////////////
137 #undef prefix_
138
139 \f
140 // Local Variables:
141 // mode: c++
142 // fill-column: 100
143 // c-file-style: "senf"
144 // indent-tabs-mode: nil
145 // ispell-local-dictionary: "american"
146 // compile-command: "scons -u test"
147 // comment-column: 40
148 // End: