switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Socket / ClientSocketHandle.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief ClientSocketHandle unit tests */
30
31 //#include "ClientSocketHandle.test.hh"
32 //#include "ClientSocketHandle.test.ih"
33
34 // Custom includes
35 #include "SocketPolicy.test.hh"
36 #include "SocketProtocol.test.hh"
37 #include "ClientSocketHandle.hh"
38 #include "AddressingPolicy.hh"
39
40 #include <senf/Utils/auto_unit_test.hh>
41 #include <boost/test/test_tools.hpp>
42
43 #define prefix_
44 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45
46 namespace {
47
48     class MySocketHandle
49         : public senf::ClientSocketHandle<senf::test::SomeSocketProtocol::Policy>
50     {
51     public:
52         MySocketHandle()
53             : senf::ClientSocketHandle<senf::test::SomeSocketProtocol::Policy>(
54                 std::auto_ptr<senf::SocketBody>(
55                     new senf::ProtocolSocketBody<senf::test::SomeSocketProtocol>(false)))
56             {}
57     };
58
59 }
60
61 SENF_AUTO_UNIT_TEST(clientSocketHandle)
62 {
63     BOOST_CHECKPOINT("Constructing socket handle");
64     MySocketHandle myh;
65
66     // conversion to other socket handles
67     {
68         typedef senf::MakeSocketPolicy<
69             senf::test::SomeFramingPolicy,
70             senf::test::SomeReadPolicy,
71             senf::test::SomeWritePolicy
72             >::policy OtherSocketPolicy;
73         typedef senf::SocketHandle<OtherSocketPolicy> OtherSocketHandle;
74
75         BOOST_CHECKPOINT("Copy-constructing socket handle");
76         OtherSocketHandle osh (myh);
77         BOOST_CHECKPOINT("Assigning socket handle");
78         osh = myh;
79         typedef senf::ClientSocketHandle<senf::test::SomeSocketProtocol::Policy> SomeSocketHandle;
80         BOOST_CHECKPOINT("static_casting socket handle");
81         SomeSocketHandle ssh =
82             senf::static_socket_cast<SomeSocketHandle>(osh);
83         SENF_CHECK_NO_THROW( senf::dynamic_socket_cast<SomeSocketHandle>(osh) );
84         typedef senf::ClientSocketHandle<senf::MakeSocketPolicy<
85             OtherSocketPolicy,
86             senf::NoAddressingPolicy
87             >::policy> SomeOtherSocketHandle;
88         BOOST_CHECK_THROW( senf::dynamic_socket_cast<SomeOtherSocketHandle>(osh),
89                            std::bad_cast );
90     }
91
92     // reading and writing
93     SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( myh.read(), "TEST-READ" ) );
94     {
95         std::string buf("FOO-BAR");
96         SENF_CHECK_NO_THROW( myh.read(buf,0) );
97         BOOST_CHECK_EQUAL( buf, "TEST-READ" );
98     }
99     {
100         char buf[11];
101         ::strcpy(buf,"0123456789");
102         SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( myh.read(buf,buf+10), buf+9 ) );
103         BOOST_CHECK_EQUAL( buf, "TEST-READ9" );
104     }
105
106     SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( myh.readfrom().first, "TEST-READ" ) );
107     {
108         std::string buf("FOO-BAR");
109         unsigned addr;
110         SENF_CHECK_NO_THROW( myh.readfrom(buf,addr,0) );
111         BOOST_CHECK_EQUAL( buf, "TEST-READ" );
112     }
113     {
114         char buf[11];
115         unsigned addr;
116         ::strcpy(buf,"0123456789");
117         SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( myh.readfrom(buf,buf+10,addr), buf+9 ) );
118         BOOST_CHECK_EQUAL( buf, "TEST-READ9" );
119     }
120
121     {
122         std::string s ("TEST-WRITE");
123         SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( myh.write(s)-s.begin(), 10 ) );
124         s = "TEST";
125         // This simulates a closed file in this test policy. However, we
126         // have changed the semantics so this will not work anymore.
127         // BOOST_CHECK_THROW( myh.write(s),senf::SystemException );
128         char const * const s1 = "TEST-WRITE9";
129         SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( myh.write(s1,s1+10), s1+10u ) );
130         s = "TEST-WRITE";
131         SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( myh.writeto(0,s)-s.begin(), 10 ) );
132         SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( myh.writeto(0,s1,s1+10), s1+10 ) );
133     }
134
135     SENF_CHECK_NO_THROW( myh.connect(0) );
136     SENF_CHECK_NO_THROW( myh.bind(0) );
137     SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( myh.peer(), 1u ) );
138     SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( myh.local(), 2u ) );
139 }
140
141 //-/////////////////////////////////////////////////////////////////////////////////////////////////
142 #undef prefix_
143
144 \f
145 // Local Variables:
146 // mode: c++
147 // fill-column: 100
148 // c-file-style: "senf"
149 // indent-tabs-mode: nil
150 // ispell-local-dictionary: "american"
151 // compile-command: "scons -u test"
152 // comment-column: 40
153 // End: