Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sal / qa / osl / socket / osl_AcceptorSocket.cxx
blob30390fd72ac8fb15133ddf0895afc128d3028b36
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 /** test coder preface:
21 1. the BSD socket function will meet "unresolved external symbol error" on Windows platform
22 if you are not including ws2_32.lib in makefile.mk, the including format will be like this:
24 .IF "$(OS)" == "WNT"
25 SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib
26 SHL1STDLIBS += ws2_32.lib
27 .ENDIF
29 likewise on Solaris platform.
30 .IF "$(OS)" != "WNT"
31 SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a
32 SHL1STDLIBS += -lsocket -ldl -lnsl
33 .ENDIF
35 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4
36 category.
38 3. some fragment of Socket source implementation are lack of comment so it is hard for testers
39 guess what the exact functionality or usage of a member. Hope the Socket section's comment
40 will be added.
42 4. following functions are declared but not implemented:
43 inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const;
46 //------------------------------------------------------------------------
47 // include files
48 //------------------------------------------------------------------------
50 #include <sal/types.h>
51 #include <cppunit/TestFixture.h>
52 #include <cppunit/extensions/HelperMacros.h>
53 #include <cppunit/plugin/TestPlugIn.h>
55 #include "osl_Socket_Const.h"
56 #include "sockethelper.hxx"
58 using namespace osl;
59 using ::rtl::OUString;
61 #define IP_PORT_FTP 21
62 #define IP_PORT_MYPORT9 8897
63 #define IP_PORT_MYPORT4 8885
64 #define IP_PORT_MYPORT3 8884
66 //------------------------------------------------------------------------
67 // helper functions
68 //------------------------------------------------------------------------
70 // just used to test socket::close() when accepting
71 class AcceptorThread : public Thread
73 ::osl::AcceptorSocket asAcceptorSocket;
74 ::rtl::OUString aHostIP;
75 sal_Bool bOK;
76 protected:
77 void SAL_CALL run( )
79 ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 );
80 ::osl::StreamSocket ssStreamConnection;
82 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
83 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
84 if ( sal_True != bOK1 )
86 t_print("# AcceptorSocket bind address failed.\n" ) ;
87 return;
89 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
90 if ( sal_True != bOK2 )
92 t_print("# AcceptorSocket listen address failed.\n" ) ;
93 return;
96 asAcceptorSocket.enableNonBlockingMode( sal_False );
98 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
99 if (eResult != osl_Socket_Ok )
101 bOK = sal_True;
102 t_print("AcceptorThread: acceptConnection failed! \n");
105 public:
106 AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString const& aBindIP )
107 : asAcceptorSocket( asSocket ), aHostIP( aBindIP )
109 bOK = sal_False;
112 sal_Bool isOK() { return bOK; }
114 ~AcceptorThread( )
116 if ( isRunning( ) )
118 asAcceptorSocket.shutdown();
119 t_print("# error: Acceptor thread not terminated.\n" );
124 namespace osl_AcceptorSocket
127 /** testing the methods:
128 inline AcceptorSocket(oslAddrFamily Family = osl_Socket_FamilyInet,
129 oslProtocol Protocol = osl_Socket_ProtocolIp,
130 oslSocketType Type = osl_Socket_TypeStream);
133 class ctors : public CppUnit::TestFixture
135 public:
137 void ctors_001()
139 /// Socket constructor.
140 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
142 CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the acceptor socket was created successfully.",
143 osl_Socket_TypeStream == asSocket.getType( ) );
146 CPPUNIT_TEST_SUITE( ctors );
147 CPPUNIT_TEST( ctors_001 );
148 CPPUNIT_TEST_SUITE_END();
150 }; // class ctors
152 /** testing the method:
153 inline sal_Bool SAL_CALL listen(sal_Int32 MaxPendingConnections= -1);
154 inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection);
155 inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection, SocketAddr & PeerAddr);
158 class listen_accept : public CppUnit::TestFixture
160 public:
161 TimeValue *pTimeout;
162 ::osl::AcceptorSocket asAcceptorSocket;
163 ::osl::ConnectorSocket csConnectorSocket;
166 // initialization
167 void setUp( )
169 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
170 pTimeout->Seconds = 3;
171 pTimeout->Nanosec = 0;
172 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1);
173 // sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
176 void tearDown( )
178 free( pTimeout );
179 // sHandle = NULL;
180 asAcceptorSocket.close( );
181 csConnectorSocket.close( );
185 void listen_accept_001()
187 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT3 );
188 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT3 );
189 ::osl::StreamSocket ssConnection;
191 /// launch server socket
192 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
193 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
194 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
195 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
196 asAcceptorSocket.enableNonBlockingMode( sal_True );
198 /// launch client socket
199 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
201 oslSocketResult eResult = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
203 CPPUNIT_ASSERT_MESSAGE( "test for listen_accept function: try to create a connection with remote host, using listen and accept.",
204 ( osl_Socket_Ok == eResult ) );
207 void listen_accept_002()
209 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT4 );
210 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT4 );
211 ::osl::SocketAddr saPeerSocketAddr( rtl::OUString("129.158.217.202"), IP_PORT_FTP );
212 ::osl::StreamSocket ssConnection;
214 /// launch server socket
215 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
216 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
217 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
218 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
219 asAcceptorSocket.enableNonBlockingMode( sal_True );
221 /// launch client socket
222 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
224 oslSocketResult eResult = asAcceptorSocket.acceptConnection(ssConnection, saPeerSocketAddr); /// waiting for incoming connection...
226 CPPUNIT_ASSERT_MESSAGE( "test for listen_accept function: try to create a connection with remote host, using listen and accept, accept with peer address.",
227 ( sal_True == bOK2 ) &&
228 ( osl_Socket_Ok == eResult ) &&
229 ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) );
233 CPPUNIT_TEST_SUITE( listen_accept );
234 CPPUNIT_TEST( listen_accept_001 );
235 CPPUNIT_TEST( listen_accept_002 );
236 CPPUNIT_TEST_SUITE_END();
238 }; // class listen_accept
241 // -----------------------------------------------------------------------------
243 CPPUNIT_TEST_SUITE_REGISTRATION(osl_AcceptorSocket::ctors);
244 //CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_AcceptorSocket::operator_assign, "osl_AcceptorSocket");
245 CPPUNIT_TEST_SUITE_REGISTRATION(osl_AcceptorSocket::listen_accept);
247 } // namespace osl_AcceptorSocket
249 // -----------------------------------------------------------------------------
251 // this macro creates an empty function, which will called by the RegisterAllFunctions()
252 // to let the user the possibility to also register some functions by hand.
253 CPPUNIT_PLUGIN_IMPLEMENT();
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */