1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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:
25 SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib
26 SHL1STDLIBS += ws2_32.lib
29 likewise on Solaris platform.
31 SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a
32 SHL1STDLIBS += -lsocket -ldl -lnsl
35 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4
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
42 4. following functions are declared but not implemented:
43 inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const;
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"
57 using ::rtl::OUString
;
59 #define IP_PORT_MYPORT2 8883
60 #define IP_PORT_FTP 21
61 #define IP_PORT_MYPORT3 8884
65 class CloseSocketThread
: public Thread
67 ::osl::Socket
&m_sSocket
;
75 CloseSocketThread(::osl::Socket
& sSocket
)
76 : m_sSocket( sSocket
)
84 t_print("# error: CloseSocketThread not terminated.\n" );
89 namespace osl_ConnectorSocket
92 /** testing the method:
93 ConnectorSocket(oslAddrFamily Family = osl_Socket_FamilyInet,
94 oslProtocol Protocol = osl_Socket_ProtocolIp,
95 oslSocketType Type = osl_Socket_TypeStream);
98 class ctors
: public CppUnit::TestFixture
103 /// Socket constructor.
104 ::osl::ConnectorSocket
csSocket( osl_Socket_FamilyInet
, osl_Socket_ProtocolIp
, osl_Socket_TypeStream
);
106 CPPUNIT_ASSERT_MESSAGE( "test for ctors_001 constructor function: check if the connector socket was created successfully.",
107 osl_Socket_TypeStream
== csSocket
.getType( ) );
110 CPPUNIT_TEST_SUITE( ctors
);
111 CPPUNIT_TEST( ctors_001
);
112 CPPUNIT_TEST_SUITE_END();
116 /** testing the method:
117 oslSocketResult SAL_CALL connect(const SocketAddr& TargetHost, const TimeValue* pTimeout = 0);
120 class connect
: public CppUnit::TestFixture
124 ::osl::AcceptorSocket asAcceptorSocket
;
125 ::osl::ConnectorSocket csConnectorSocket
;
130 pTimeout
= ( TimeValue
* )malloc( sizeof( TimeValue
) );
131 pTimeout
->Seconds
= 3;
132 pTimeout
->Nanosec
= 0;
133 // sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
140 asAcceptorSocket
.close( );
141 csConnectorSocket
.close( );
146 ::osl::SocketAddr
saLocalSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT2
);
147 ::osl::SocketAddr
saTargetSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT2
);
148 ::osl::SocketAddr
saPeerSocketAddr( rtl::OUString("129.158.217.202"), IP_PORT_FTP
);
149 ::osl::StreamSocket ssConnection
;
151 /// launch server socket
152 asAcceptorSocket
.setOption( osl_Socket_OptionReuseAddr
, 1 ); //sal_True);
153 sal_Bool bOK1
= asAcceptorSocket
.bind( saLocalSocketAddr
);
154 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True
== bOK1
);
155 sal_Bool bOK2
= asAcceptorSocket
.listen( 1 );
156 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True
== bOK2
);
158 //asAcceptorSocket.enableNonBlockingMode( sal_True );
159 //oslSocketResult eResultAccept = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
160 //CPPUNIT_ASSERT_MESSAGE( "accept failed.", osl_Socket_Ok == eResultAccept );
161 /// launch client socket
162 oslSocketResult eResult
= csConnectorSocket
.connect( saTargetSocketAddr
, pTimeout
); /// connecting to server...
163 CPPUNIT_ASSERT_MESSAGE( "connect failed.", osl_Socket_Ok
== eResult
);
165 /// get peer information
166 csConnectorSocket
.getPeerAddr( saPeerSocketAddr
);/// connected.
168 CPPUNIT_ASSERT_MESSAGE( "test for connect function: try to create a connection with remote host. and check the setup address.",
169 ( sal_True
== compareSocketAddr( saPeerSocketAddr
, saLocalSocketAddr
) ) &&
170 ( osl_Socket_Ok
== eResult
));
172 //non-blocking mode connect?
175 ::osl::SocketAddr
saLocalSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT3
);
176 ::osl::SocketAddr
saTargetSocketAddr( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT3
);
177 ::osl::SocketAddr
saPeerSocketAddr( rtl::OUString("129.158.217.202"), IP_PORT_FTP
);
179 asAcceptorSocket
.setOption( osl_Socket_OptionReuseAddr
, 1 ); //sal_True);
180 asAcceptorSocket
.enableNonBlockingMode( sal_True
);
181 sal_Bool bOK1
= asAcceptorSocket
.bind( saLocalSocketAddr
);
182 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True
== bOK1
);
183 sal_Bool bOK2
= asAcceptorSocket
.listen( 1 );
184 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True
== bOK2
);
186 csConnectorSocket
.enableNonBlockingMode( sal_True
);
188 oslSocketResult eResult
= csConnectorSocket
.connect( saTargetSocketAddr
, pTimeout
); /// connecting to server...
189 CPPUNIT_ASSERT_MESSAGE( "connect failed.", osl_Socket_InProgress
== eResult
|| osl_Socket_Ok
== eResult
);
191 /// get peer information
192 csConnectorSocket
.getPeerAddr( saPeerSocketAddr
);
194 CPPUNIT_ASSERT_MESSAGE( "test for connect function: try to create a connection with remote host. and check the setup address.",
195 sal_True
== compareSocketAddr( saPeerSocketAddr
, saLocalSocketAddr
) ) ;
197 // really an error or just delayed
198 // how to design scenarios that will return osl_Socket_Interrupted, osl_Socket_TimedOut
201 ::osl::SocketAddr
saTargetSocketAddr1( rtl::OUString("127.0.0.1"), IP_PORT_MYPORT3
);
202 ::osl::SocketAddr
saTargetSocketAddr2( rtl::OUString("123.345.67.89"), IP_PORT_MYPORT3
);
204 csConnectorSocket
.enableNonBlockingMode( sal_False
);
206 oslSocketResult eResult1
= csConnectorSocket
.connect( saTargetSocketAddr1
, pTimeout
);
207 oslSocketResult eResult2
= csConnectorSocket
.connect( saTargetSocketAddr2
, pTimeout
);
208 CloseSocketThread
myCloseThread( csConnectorSocket
);
209 oslSocketResult eResult3
= csConnectorSocket
.connect( saTargetSocketAddr2
, pTimeout
);
210 myCloseThread
.join();
211 CPPUNIT_ASSERT_MESSAGE( "connect should failed.", osl_Socket_Error
== eResult1
&&
212 osl_Socket_Error
== eResult2
&& osl_Socket_Error
== eResult3
);
216 // really an error in non-blocking mode
219 ::osl::SocketAddr
saTargetSocketAddr( rtl::OUString("123.345.67.89"), IP_PORT_MYPORT3
);
221 csConnectorSocket
.enableNonBlockingMode( sal_True
);
223 oslSocketResult eResult
= csConnectorSocket
.connect( saTargetSocketAddr
, pTimeout
); /// connecting to server...
224 CPPUNIT_ASSERT_MESSAGE( "connect should failed.", osl_Socket_Error
== eResult
);
226 /** here need a case: immediate connection, say in non-blocking mode connect return osl_Socket_Ok
229 CPPUNIT_TEST_SUITE( connect
);
230 CPPUNIT_TEST( connect_001
);
231 CPPUNIT_TEST( connect_002
);
232 CPPUNIT_TEST( connect_003
);
233 CPPUNIT_TEST( connect_004
);
234 CPPUNIT_TEST_SUITE_END();
238 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ConnectorSocket::ctors
);
239 CPPUNIT_TEST_SUITE_REGISTRATION(osl_ConnectorSocket::connect
);
241 } // namespace osl_ConnectorSocket
243 // this macro creates an empty function, which will called by the RegisterAllFunctions()
244 // to let the user the possibility to also register some functions by hand.
245 CPPUNIT_PLUGIN_IMPLEMENT();
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */