bump product version to 5.0.4.1
[LibreOffice.git] / sal / qa / osl / socket / osl_AcceptorSocket.cxx
blob85d5d4c63fb9af31863c45f9c9d01e002b4adf70
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 // include files
48 #include <sal/types.h>
49 #include <cppunit/TestFixture.h>
50 #include <cppunit/extensions/HelperMacros.h>
51 #include <cppunit/plugin/TestPlugIn.h>
53 #include "osl_Socket_Const.h"
54 #include "sockethelper.hxx"
56 using namespace osl;
57 using ::rtl::OUString;
59 #define IP_PORT_FTP 21
60 #define IP_PORT_MYPORT9 8897
61 #define IP_PORT_MYPORT4 8885
62 #define IP_PORT_MYPORT3 8884
64 // helper functions
66 // just used to test socket::close() when accepting
67 class AcceptorThread : public Thread
69 ::osl::AcceptorSocket asAcceptorSocket;
70 ::rtl::OUString aHostIP;
71 sal_Bool bOK;
72 protected:
73 void SAL_CALL run( )
75 ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 );
76 ::osl::StreamSocket ssStreamConnection;
78 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
79 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
80 if ( sal_True != bOK1 )
82 t_print("# AcceptorSocket bind address failed.\n" ) ;
83 return;
85 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
86 if ( sal_True != bOK2 )
88 t_print("# AcceptorSocket listen address failed.\n" ) ;
89 return;
92 asAcceptorSocket.enableNonBlockingMode( sal_False );
94 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
95 if (eResult != osl_Socket_Ok )
97 bOK = sal_True;
98 t_print("AcceptorThread: acceptConnection failed! \n");
101 public:
102 AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString const& aBindIP )
103 : asAcceptorSocket( asSocket ), aHostIP( aBindIP )
105 bOK = sal_False;
108 sal_Bool isOK() { return bOK; }
110 ~AcceptorThread( )
112 if ( isRunning( ) )
114 asAcceptorSocket.shutdown();
115 t_print("# error: Acceptor thread not terminated.\n" );
120 namespace osl_AcceptorSocket
123 /** testing the methods:
124 inline AcceptorSocket(oslAddrFamily Family = osl_Socket_FamilyInet,
125 oslProtocol Protocol = osl_Socket_ProtocolIp,
126 oslSocketType Type = osl_Socket_TypeStream);
129 class ctors : public CppUnit::TestFixture
131 public:
133 void ctors_001()
135 /// Socket constructor.
136 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
138 CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the acceptor socket was created successfully.",
139 osl_Socket_TypeStream == asSocket.getType( ) );
142 CPPUNIT_TEST_SUITE( ctors );
143 CPPUNIT_TEST( ctors_001 );
144 CPPUNIT_TEST_SUITE_END();
146 }; // class ctors
148 /** testing the method:
149 inline sal_Bool SAL_CALL listen(sal_Int32 MaxPendingConnections= -1);
150 inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection);
151 inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection, SocketAddr & PeerAddr);
154 class listen_accept : public CppUnit::TestFixture
156 public:
157 TimeValue *pTimeout;
158 ::osl::AcceptorSocket asAcceptorSocket;
159 ::osl::ConnectorSocket csConnectorSocket;
161 // initialization
162 void setUp( )
164 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
165 pTimeout->Seconds = 3;
166 pTimeout->Nanosec = 0;
167 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1);
168 // sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
171 void tearDown( )
173 free( pTimeout );
174 // sHandle = NULL;
175 asAcceptorSocket.close( );
176 csConnectorSocket.close( );
179 void listen_accept_001()
181 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT3 );
182 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT3 );
183 ::osl::StreamSocket ssConnection;
185 /// launch server socket
186 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
187 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
188 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
189 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
190 asAcceptorSocket.enableNonBlockingMode( sal_True );
192 /// launch client socket
193 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
195 oslSocketResult eResult = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
197 CPPUNIT_ASSERT_MESSAGE( "test for listen_accept function: try to create a connection with remote host, using listen and accept.",
198 ( osl_Socket_Ok == eResult ) );
201 void listen_accept_002()
203 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT4 );
204 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT4 );
205 ::osl::SocketAddr saPeerSocketAddr( rtl::OUString("129.158.217.202"), IP_PORT_FTP );
206 ::osl::StreamSocket ssConnection;
208 /// launch server socket
209 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
210 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
211 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
212 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
213 asAcceptorSocket.enableNonBlockingMode( sal_True );
215 /// launch client socket
216 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
218 oslSocketResult eResult = asAcceptorSocket.acceptConnection(ssConnection, saPeerSocketAddr); /// waiting for incoming connection...
220 CPPUNIT_ASSERT_MESSAGE( "test for listen_accept function: try to create a connection with remote host, using listen and accept, accept with peer address.",
221 ( sal_True == bOK2 ) &&
222 ( osl_Socket_Ok == eResult ) &&
223 ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) );
226 CPPUNIT_TEST_SUITE( listen_accept );
227 CPPUNIT_TEST( listen_accept_001 );
228 CPPUNIT_TEST( listen_accept_002 );
229 CPPUNIT_TEST_SUITE_END();
231 }; // class listen_accept
233 CPPUNIT_TEST_SUITE_REGISTRATION(osl_AcceptorSocket::ctors);
234 //CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_AcceptorSocket::operator_assign, "osl_AcceptorSocket");
235 CPPUNIT_TEST_SUITE_REGISTRATION(osl_AcceptorSocket::listen_accept);
237 } // namespace osl_AcceptorSocket
239 // this macro creates an empty function, which will called by the RegisterAllFunctions()
240 // to let the user the possibility to also register some functions by hand.
241 CPPUNIT_PLUGIN_IMPLEMENT();
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */