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