187d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
387d2adbcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
487d2adbcSAndrew Rist * or more contributor license agreements. See the NOTICE file
587d2adbcSAndrew Rist * distributed with this work for additional information
687d2adbcSAndrew Rist * regarding copyright ownership. The ASF licenses this file
787d2adbcSAndrew Rist * to you under the Apache License, Version 2.0 (the
887d2adbcSAndrew Rist * "License"); you may not use this file except in compliance
987d2adbcSAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
1187d2adbcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
1387d2adbcSAndrew Rist * Unless required by applicable law or agreed to in writing,
1487d2adbcSAndrew Rist * software distributed under the License is distributed on an
1587d2adbcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1687d2adbcSAndrew Rist * KIND, either express or implied. See the License for the
1787d2adbcSAndrew Rist * specific language governing permissions and limitations
1887d2adbcSAndrew Rist * under the License.
19cdf0e10cSrcweir *
2087d2adbcSAndrew Rist *************************************************************/
2187d2adbcSAndrew Rist
2287d2adbcSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sal.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir /** test coder preface:
28cdf0e10cSrcweir 1. the BSD socket function will meet "unresolved external symbol error" on Windows platform
29cdf0e10cSrcweir if you are not including ws2_32.lib in makefile.mk, the including format will be like this:
30cdf0e10cSrcweir
31cdf0e10cSrcweir .IF "$(GUI)" == "WNT"
32cdf0e10cSrcweir SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib
33cdf0e10cSrcweir SHL1STDLIBS += ws2_32.lib
34cdf0e10cSrcweir .ENDIF
35cdf0e10cSrcweir
36cdf0e10cSrcweir likewise on Solaris platform.
37cdf0e10cSrcweir .IF "$(GUI)" == "UNX"
38cdf0e10cSrcweir SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a
39cdf0e10cSrcweir SHL1STDLIBS += -lsocket -ldl -lnsl
40cdf0e10cSrcweir .ENDIF
41cdf0e10cSrcweir
42cdf0e10cSrcweir 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4
43cdf0e10cSrcweir category.
44cdf0e10cSrcweir
45cdf0e10cSrcweir 3. some fragment of Socket source implementation are lack of comment so it is hard for testers
46cdf0e10cSrcweir guess what the exact functionality or usage of a member. Hope the Socket section's comment
47cdf0e10cSrcweir will be added.
48cdf0e10cSrcweir
49cdf0e10cSrcweir 4. following functions are declared but not implemented:
50cdf0e10cSrcweir inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const;
51cdf0e10cSrcweir */
52cdf0e10cSrcweir
53cdf0e10cSrcweir
54cdf0e10cSrcweir //------------------------------------------------------------------------
55cdf0e10cSrcweir // include files
56cdf0e10cSrcweir //------------------------------------------------------------------------
57cdf0e10cSrcweir
58cdf0e10cSrcweir #ifndef _OSL_SOCKET_CONST_H_
59cdf0e10cSrcweir #include <osl_Socket_Const_orig.h>
60cdf0e10cSrcweir #endif
61cdf0e10cSrcweir
6263d99982SDamjan Jovanovic #include <osl/signal.h>
6363d99982SDamjan Jovanovic #include "gtest/gtest.h"
64cdf0e10cSrcweir
65cdf0e10cSrcweir using namespace osl;
66cdf0e10cSrcweir using namespace rtl;
67cdf0e10cSrcweir
68cdf0e10cSrcweir //------------------------------------------------------------------------
69cdf0e10cSrcweir // helper functions
70cdf0e10cSrcweir //------------------------------------------------------------------------
71cdf0e10cSrcweir
72cdf0e10cSrcweir /** compare two OUString.
73cdf0e10cSrcweir */
compareUString(const::rtl::OUString & ustr1,const::rtl::OUString & ustr2)74cdf0e10cSrcweir inline sal_Bool compareUString( const ::rtl::OUString & ustr1, const ::rtl::OUString & ustr2 )
75cdf0e10cSrcweir {
76cdf0e10cSrcweir sal_Bool bOk = ustr1.equalsIgnoreAsciiCase( ustr2 );
77cdf0e10cSrcweir
78cdf0e10cSrcweir return bOk;
79cdf0e10cSrcweir }
80cdf0e10cSrcweir
81cdf0e10cSrcweir /** compare a OUString and an ASCII string.
82cdf0e10cSrcweir */
compareUString(const::rtl::OUString & ustr,const sal_Char * astr)83cdf0e10cSrcweir inline sal_Bool compareUString( const ::rtl::OUString & ustr, const sal_Char *astr )
84cdf0e10cSrcweir {
85cdf0e10cSrcweir ::rtl::OUString ustr2 = rtl::OUString::createFromAscii( astr );
86cdf0e10cSrcweir sal_Bool bOk = ustr.equalsIgnoreAsciiCase( ustr2 );
87cdf0e10cSrcweir
88cdf0e10cSrcweir return bOk;
89cdf0e10cSrcweir }
90cdf0e10cSrcweir
91cdf0e10cSrcweir /** compare two socket address.
92cdf0e10cSrcweir */
compareSocketAddr(const::osl::SocketAddr & addr1,const::osl::SocketAddr & addr2)93cdf0e10cSrcweir inline sal_Bool compareSocketAddr( const ::osl::SocketAddr & addr1 , const ::osl::SocketAddr & addr2 )
94cdf0e10cSrcweir {
95cdf0e10cSrcweir return ( ( sal_True == compareUString( addr1.getHostname( 0 ), addr2.getHostname( 0 ) ) ) && ( addr2.getPort( ) == addr2.getPort( ) ) );
96cdf0e10cSrcweir }
97cdf0e10cSrcweir
oustring2char(const::rtl::OUString & str)98cdf0e10cSrcweir inline char * oustring2char( const ::rtl::OUString & str )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir rtl::OString aString;
101cdf0e10cSrcweir aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
102cdf0e10cSrcweir return (char *)aString.getStr( );
103cdf0e10cSrcweir }
104cdf0e10cSrcweir
105cdf0e10cSrcweir /** print a UNI_CODE String. And also print some comments of the string.
106cdf0e10cSrcweir */
printUString(const::rtl::OUString & str,const sal_Char * msg="")107cdf0e10cSrcweir inline void printUString( const ::rtl::OUString & str, const sal_Char * msg = "" )
108cdf0e10cSrcweir {
10963d99982SDamjan Jovanovic printf("#%s #printUString_u# ", msg );
11063d99982SDamjan Jovanovic printf("%s\n", oustring2char( str ) );
111cdf0e10cSrcweir }
112cdf0e10cSrcweir
113cdf0e10cSrcweir /** get the local host name.
114cdf0e10cSrcweir mindy: gethostbyname( "localhost" ), on Linux, it returns the hostname in /etc/hosts + domain name,
115cdf0e10cSrcweir if no entry in /etc/hosts, it returns "localhost" + domain name
116cdf0e10cSrcweir */
getHost(void)117cdf0e10cSrcweir inline ::rtl::OUString getHost( void )
118cdf0e10cSrcweir {
119cdf0e10cSrcweir struct hostent *hptr;
120cdf0e10cSrcweir
121cdf0e10cSrcweir hptr = gethostbyname( "localhost" );
12263d99982SDamjan Jovanovic EXPECT_TRUE(hptr != NULL) << "#In getHostname function, error on gethostbyname()";
123cdf0e10cSrcweir ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) hptr->h_name );
124cdf0e10cSrcweir
125cdf0e10cSrcweir return aUString;
126cdf0e10cSrcweir }
127cdf0e10cSrcweir
128cdf0e10cSrcweir /** get the full host name of the current processor, such as "aegean.prc.sun.com" --mindyliu
129cdf0e10cSrcweir */
getThisHostname(void)130cdf0e10cSrcweir inline ::rtl::OUString getThisHostname( void )
131cdf0e10cSrcweir {
132cdf0e10cSrcweir ::rtl::OUString aUString;
133cdf0e10cSrcweir #ifdef WNT
134cdf0e10cSrcweir struct hostent *hptr;
135cdf0e10cSrcweir hptr = gethostbyname( "localhost" );
13663d99982SDamjan Jovanovic EXPECT_TRUE(hptr != NULL) << "#In getHostname function, error on gethostbyname()";
137cdf0e10cSrcweir aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) hptr->h_name );
138cdf0e10cSrcweir #else
139cdf0e10cSrcweir char hostname[255];
14063d99982SDamjan Jovanovic EXPECT_TRUE(gethostname(hostname, 255) == 0) << "#Error: gethostname failed.";
141cdf0e10cSrcweir
142cdf0e10cSrcweir struct hostent *hptr;
143cdf0e10cSrcweir //first search /ets/hosts, then search from dns
144cdf0e10cSrcweir hptr = gethostbyname( hostname);
145cdf0e10cSrcweir if ( hptr != NULL )
146cdf0e10cSrcweir {
147cdf0e10cSrcweir strcpy( hostname, hptr->h_name );
148cdf0e10cSrcweir }
149cdf0e10cSrcweir
15063d99982SDamjan Jovanovic printf("hostname is %s \n", hostname );
151cdf0e10cSrcweir aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) hostname );
152cdf0e10cSrcweir #endif
153cdf0e10cSrcweir return aUString;
154cdf0e10cSrcweir }
155cdf0e10cSrcweir
156cdf0e10cSrcweir /** get IP by name, search /etc/hosts first, then search from dns, fail return OUString("")
157cdf0e10cSrcweir */
getIPbyName(rtl::OString const & str_name)158cdf0e10cSrcweir inline ::rtl::OUString getIPbyName( rtl::OString const& str_name )
159cdf0e10cSrcweir {
160cdf0e10cSrcweir ::rtl::OUString aUString;
161cdf0e10cSrcweir struct hostent *hptr;
162cdf0e10cSrcweir //first search /ets/hosts, then search from dns
163cdf0e10cSrcweir hptr = gethostbyname( str_name.getStr());
164cdf0e10cSrcweir if ( hptr != NULL )
165cdf0e10cSrcweir {
166cdf0e10cSrcweir struct in_addr ** addrptr;
167cdf0e10cSrcweir addrptr = (struct in_addr **) hptr->h_addr_list ;
168cdf0e10cSrcweir //if there are more than one IPs on the same machine, we select one
169cdf0e10cSrcweir for (; *addrptr; addrptr++)
170cdf0e10cSrcweir {
17163d99982SDamjan Jovanovic printf("#Local IP Address: %s\n", inet_ntoa(**addrptr));
172cdf0e10cSrcweir aUString = ::rtl::OUString::createFromAscii( (sal_Char *) (inet_ntoa(**addrptr)) );
173cdf0e10cSrcweir }
174cdf0e10cSrcweir }
175cdf0e10cSrcweir return aUString;
176cdf0e10cSrcweir }
177cdf0e10cSrcweir
178cdf0e10cSrcweir /** get local ethernet IP
179cdf0e10cSrcweir */
getLocalIP()180cdf0e10cSrcweir inline ::rtl::OUString getLocalIP( )
181cdf0e10cSrcweir {
182cdf0e10cSrcweir char hostname[255];
183cdf0e10cSrcweir gethostname(hostname, 255);
184cdf0e10cSrcweir
185cdf0e10cSrcweir return getIPbyName( hostname );
186cdf0e10cSrcweir }
187cdf0e10cSrcweir
188cdf0e10cSrcweir /** construct error message
189cdf0e10cSrcweir */
outputError(const::rtl::OUString & returnVal,const::rtl::OUString & rightVal,const sal_Char * msg="")190cdf0e10cSrcweir inline ::rtl::OUString outputError( const ::rtl::OUString & returnVal, const ::rtl::OUString & rightVal, const sal_Char * msg = "")
191cdf0e10cSrcweir {
192cdf0e10cSrcweir ::rtl::OUString aUString;
193cdf0e10cSrcweir if ( returnVal.equals( rightVal ) )
194cdf0e10cSrcweir return aUString;
195cdf0e10cSrcweir aUString += ::rtl::OUString::createFromAscii(msg);
196cdf0e10cSrcweir aUString += ::rtl::OUString::createFromAscii(": the returned value is '");
197cdf0e10cSrcweir aUString += returnVal;
198cdf0e10cSrcweir aUString += ::rtl::OUString::createFromAscii("', but the value should be '");
199cdf0e10cSrcweir aUString += rightVal;
200cdf0e10cSrcweir aUString += ::rtl::OUString::createFromAscii("'.");
201cdf0e10cSrcweir return aUString;
202cdf0e10cSrcweir }
203cdf0e10cSrcweir
204cdf0e10cSrcweir /** wait _nSec seconds.
205cdf0e10cSrcweir */
thread_sleep(sal_Int32 _nSec)206cdf0e10cSrcweir void thread_sleep( sal_Int32 _nSec )
207cdf0e10cSrcweir {
208cdf0e10cSrcweir /// print statement in thread process must use fflush() to force display.
20963d99982SDamjan Jovanovic printf("# wait %d seconds. ", _nSec );
210cdf0e10cSrcweir fflush(stdout);
211cdf0e10cSrcweir
212cdf0e10cSrcweir #ifdef WNT //Windows
213cdf0e10cSrcweir Sleep( _nSec * 100 );
214cdf0e10cSrcweir #endif
215cdf0e10cSrcweir #if ( defined UNX ) || ( defined OS2 ) //Unix
216cdf0e10cSrcweir usleep(_nSec * 100000);
217cdf0e10cSrcweir #endif
21863d99982SDamjan Jovanovic printf("# done\n" );
219cdf0e10cSrcweir }
220cdf0e10cSrcweir
221cdf0e10cSrcweir /** print Boolean value.
222cdf0e10cSrcweir */
printBool(sal_Bool bOk)223cdf0e10cSrcweir inline void printBool( sal_Bool bOk )
224cdf0e10cSrcweir {
22563d99982SDamjan Jovanovic printf("#printBool# " );
22663d99982SDamjan Jovanovic ( sal_True == bOk ) ? printf("YES!\n" ): printf("NO!\n" );
227cdf0e10cSrcweir }
228cdf0e10cSrcweir
229cdf0e10cSrcweir /** print content of a ByteSequence.
230cdf0e10cSrcweir */
printByteSequence_IP(const::rtl::ByteSequence & bsByteSeq,sal_Int32 nLen)231cdf0e10cSrcweir inline void printByteSequence_IP( const ::rtl::ByteSequence & bsByteSeq, sal_Int32 nLen )
232cdf0e10cSrcweir {
23363d99982SDamjan Jovanovic printf("#ByteSequence is: " );
234cdf0e10cSrcweir for ( int i = 0; i < nLen; i++ ){
235cdf0e10cSrcweir if ( bsByteSeq[i] < 0 )
23663d99982SDamjan Jovanovic printf("%d ", 256 + bsByteSeq[i] );
237cdf0e10cSrcweir else
23863d99982SDamjan Jovanovic printf("%d ", bsByteSeq[i] );
239cdf0e10cSrcweir }
24063d99982SDamjan Jovanovic printf(" .\n" );
241cdf0e10cSrcweir }
242cdf0e10cSrcweir
243cdf0e10cSrcweir /** convert an IP which is stored as a UString format to a ByteSequence array for later use.
244cdf0e10cSrcweir */
UStringIPToByteSequence(::rtl::OUString aUStr)245cdf0e10cSrcweir inline ::rtl::ByteSequence UStringIPToByteSequence( ::rtl::OUString aUStr )
246cdf0e10cSrcweir {
247cdf0e10cSrcweir
248cdf0e10cSrcweir rtl::OString aString = ::rtl::OUStringToOString( aUStr, RTL_TEXTENCODING_ASCII_US );
249cdf0e10cSrcweir const sal_Char *pChar = aString.getStr( ) ;
250cdf0e10cSrcweir sal_Char tmpBuffer[4];
251cdf0e10cSrcweir sal_Int32 nCharCounter = 0;
252cdf0e10cSrcweir ::rtl::ByteSequence bsByteSequence( IP_VER );
253cdf0e10cSrcweir sal_Int32 nByteSeqCounter = 0;
254cdf0e10cSrcweir
255cdf0e10cSrcweir for ( int i = 0; i < aString.getLength( ) + 1 ; i++ )
256cdf0e10cSrcweir {
257cdf0e10cSrcweir if ( ( *pChar != '.' ) && ( i !=aString.getLength( ) ) )
258cdf0e10cSrcweir tmpBuffer[nCharCounter++] = *pChar;
259cdf0e10cSrcweir else
260cdf0e10cSrcweir {
261cdf0e10cSrcweir tmpBuffer[nCharCounter] = '\0';
262cdf0e10cSrcweir nCharCounter = 0;
263cdf0e10cSrcweir bsByteSequence[nByteSeqCounter++] = static_cast<sal_Int8>( atoi( tmpBuffer ) );
264cdf0e10cSrcweir }
265cdf0e10cSrcweir pChar++;
266cdf0e10cSrcweir }
267cdf0e10cSrcweir return bsByteSequence;
268cdf0e10cSrcweir }
269cdf0e10cSrcweir
270cdf0e10cSrcweir /** print a socket result name.
271cdf0e10cSrcweir */
printSocketResult(oslSocketResult eResult)272cdf0e10cSrcweir inline void printSocketResult( oslSocketResult eResult )
273cdf0e10cSrcweir {
27463d99982SDamjan Jovanovic printf("#printSocketResult# " );
275cdf0e10cSrcweir if (!eResult)
276cdf0e10cSrcweir switch (eResult)
277cdf0e10cSrcweir {
278cdf0e10cSrcweir case osl_Socket_Ok:
27963d99982SDamjan Jovanovic printf("client connected\n");
280cdf0e10cSrcweir break;
281cdf0e10cSrcweir case osl_Socket_Error:
28263d99982SDamjan Jovanovic printf("got an error ... exiting\r\n\r\n" );
283cdf0e10cSrcweir break;
284cdf0e10cSrcweir case osl_Socket_TimedOut:
28563d99982SDamjan Jovanovic printf("timeout\n");
286cdf0e10cSrcweir break;
287cdf0e10cSrcweir
288cdf0e10cSrcweir case osl_Socket_FORCE_EQUAL_SIZE:
28963d99982SDamjan Jovanovic printf("FORCE EQUAL SIZE\n");
290cdf0e10cSrcweir break;
291cdf0e10cSrcweir case osl_Socket_InProgress:
29263d99982SDamjan Jovanovic printf("In Progress\n");
293cdf0e10cSrcweir break;
294cdf0e10cSrcweir case osl_Socket_Interrupted:
29563d99982SDamjan Jovanovic printf("Interrupted\n");
296cdf0e10cSrcweir break;
297cdf0e10cSrcweir }
298cdf0e10cSrcweir }
299cdf0e10cSrcweir
300cdf0e10cSrcweir /** Client Socket Thread, served as a temp little client to communicate with server.
301cdf0e10cSrcweir */
302cdf0e10cSrcweir class ClientSocketThread : public Thread
303cdf0e10cSrcweir {
304cdf0e10cSrcweir protected:
305cdf0e10cSrcweir oslThreadIdentifier m_id;
306cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr;
307cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket;
308cdf0e10cSrcweir
run()309cdf0e10cSrcweir void SAL_CALL run( )
310cdf0e10cSrcweir {
311cdf0e10cSrcweir TimeValue *pTimeout;
312cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
313cdf0e10cSrcweir pTimeout->Seconds = 5;
314cdf0e10cSrcweir pTimeout->Nanosec = 0;
315cdf0e10cSrcweir
316cdf0e10cSrcweir /// if the thread should terminate, schedule return false
317cdf0e10cSrcweir //while ( schedule( ) == sal_True )
318cdf0e10cSrcweir //{
319cdf0e10cSrcweir if ( osl_Socket_Ok == csConnectorSocket.connect( saTargetSocketAddr, pTimeout ))
320cdf0e10cSrcweir {
321cdf0e10cSrcweir csConnectorSocket.send( pTestString1, 11 ); // "test socket"
322cdf0e10cSrcweir csConnectorSocket.send( pTestString2, 10);
323cdf0e10cSrcweir }
324cdf0e10cSrcweir else
32563d99982SDamjan Jovanovic printf("# ClientSocketThread: connect failed! \n");
326cdf0e10cSrcweir // terminate();
327cdf0e10cSrcweir //}
328cdf0e10cSrcweir csConnectorSocket.close();
329cdf0e10cSrcweir free( pTimeout );
330cdf0e10cSrcweir }
331cdf0e10cSrcweir
onTerminated()332cdf0e10cSrcweir void SAL_CALL onTerminated( )
333cdf0e10cSrcweir {
33463d99982SDamjan Jovanovic //printf("# normally terminate this thread %d!\n", m_id );
335cdf0e10cSrcweir }
336cdf0e10cSrcweir
337cdf0e10cSrcweir public:
ClientSocketThread()338cdf0e10cSrcweir ClientSocketThread( ):
339cdf0e10cSrcweir saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT9 ),
340cdf0e10cSrcweir csConnectorSocket( )
341cdf0e10cSrcweir {
342cdf0e10cSrcweir m_id = getIdentifier( );
34363d99982SDamjan Jovanovic //printf("# successfully creat this client thread %d!\n", m_id );
344cdf0e10cSrcweir }
345cdf0e10cSrcweir
~ClientSocketThread()346cdf0e10cSrcweir ~ClientSocketThread( )
347cdf0e10cSrcweir {
348cdf0e10cSrcweir if ( isRunning( ) )
34963d99982SDamjan Jovanovic printf("# error: client thread not terminated.\n" );
350cdf0e10cSrcweir }
351cdf0e10cSrcweir
352cdf0e10cSrcweir };
353cdf0e10cSrcweir
354cdf0e10cSrcweir
355cdf0e10cSrcweir /** Server Socket Thread, served as a temp little server to communicate with client.
356cdf0e10cSrcweir */
357cdf0e10cSrcweir class ServerSocketThread : public Thread
358cdf0e10cSrcweir {
359cdf0e10cSrcweir protected:
360cdf0e10cSrcweir oslThreadIdentifier m_id;
361cdf0e10cSrcweir
run()362cdf0e10cSrcweir void SAL_CALL run( )
363cdf0e10cSrcweir {
364cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
365cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9 );
366cdf0e10cSrcweir ::osl::StreamSocket ssStreamConnection;
367cdf0e10cSrcweir
368cdf0e10cSrcweir //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
369cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
370cdf0e10cSrcweir while ( schedule( ) == sal_True )
371cdf0e10cSrcweir {
372cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
373cdf0e10cSrcweir if ( sal_True != bOK1 )
374cdf0e10cSrcweir {
37563d99982SDamjan Jovanovic printf("# ServerSocketThread: AcceptorSocket bind address failed.\n" ) ;
376cdf0e10cSrcweir break;
377cdf0e10cSrcweir }
378cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
379cdf0e10cSrcweir if ( sal_True != bOK2 )
380cdf0e10cSrcweir {
38163d99982SDamjan Jovanovic printf("# ServerSocketThread: AcceptorSocket listen address failed.\n" ) ;
382cdf0e10cSrcweir break;
383cdf0e10cSrcweir }
384cdf0e10cSrcweir
385cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_False );
386cdf0e10cSrcweir
387cdf0e10cSrcweir oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
388cdf0e10cSrcweir if (eResult != osl_Socket_Ok )
389cdf0e10cSrcweir {
39063d99982SDamjan Jovanovic printf("ServerSocketThread: acceptConnection failed! \n");
391cdf0e10cSrcweir break;
392cdf0e10cSrcweir }
393cdf0e10cSrcweir sal_Int32 nReadNumber1 = ssStreamConnection.recv( pReadBuffer, 11 );
394cdf0e10cSrcweir sal_Int32 nReadNumber2 = ssStreamConnection.recv( pReadBuffer + nReadNumber1, 11 );
395cdf0e10cSrcweir pReadBuffer[nReadNumber1 + nReadNumber2] = '\0';
39663d99982SDamjan Jovanovic //printf("# read buffer content: %s\n", pReadBuffer );
397cdf0e10cSrcweir break;
398cdf0e10cSrcweir }
399cdf0e10cSrcweir ssStreamConnection.close();
400cdf0e10cSrcweir asAcceptorSocket.close();
401cdf0e10cSrcweir
402cdf0e10cSrcweir }
403cdf0e10cSrcweir
onTerminated()404cdf0e10cSrcweir void SAL_CALL onTerminated( )
405cdf0e10cSrcweir {
40663d99982SDamjan Jovanovic //printf("# normally terminate this server thread %d!\n", m_id );
407cdf0e10cSrcweir }
408cdf0e10cSrcweir
409cdf0e10cSrcweir public:
410*99ad85ffSJohn Bampton // public to check if data transmission is OK
411cdf0e10cSrcweir sal_Char pReadBuffer[30];
ServerSocketThread()412cdf0e10cSrcweir ServerSocketThread( )
413cdf0e10cSrcweir {
414cdf0e10cSrcweir m_id = getIdentifier( );
415*99ad85ffSJohn Bampton //printf("# successfully create this server thread %d!\n", m_id );
416cdf0e10cSrcweir }
417cdf0e10cSrcweir
~ServerSocketThread()418cdf0e10cSrcweir ~ServerSocketThread( )
419cdf0e10cSrcweir {
420cdf0e10cSrcweir if ( isRunning( ) )
42163d99982SDamjan Jovanovic printf("# error: server thread not terminated.\n" );
422cdf0e10cSrcweir }
423cdf0e10cSrcweir };
424cdf0e10cSrcweir
425cdf0e10cSrcweir // -----------------------------------------------------------------------------
426cdf0e10cSrcweir // Helper functions, to create buffers, check buffers
427cdf0e10cSrcweir class ValueCheckProvider
428cdf0e10cSrcweir {
429cdf0e10cSrcweir bool m_bFoundFailure;
430cdf0e10cSrcweir char *m_pBuffer;
431cdf0e10cSrcweir sal_Int32 m_nBufferSize;
432cdf0e10cSrcweir
433cdf0e10cSrcweir public:
ValueCheckProvider()434cdf0e10cSrcweir ValueCheckProvider()
435cdf0e10cSrcweir :
436cdf0e10cSrcweir m_bFoundFailure(false),
437cdf0e10cSrcweir m_pBuffer(NULL),
438cdf0e10cSrcweir m_nBufferSize(0)
439cdf0e10cSrcweir {
440cdf0e10cSrcweir }
441cdf0e10cSrcweir
isFailure()442cdf0e10cSrcweir bool isFailure() {return m_bFoundFailure;}
443cdf0e10cSrcweir
getBuffer()444cdf0e10cSrcweir const char* getBuffer() {return m_pBuffer;}
getWriteBuffer()445cdf0e10cSrcweir char* getWriteBuffer() {return m_pBuffer;}
446cdf0e10cSrcweir
getBufferSize()447cdf0e10cSrcweir sal_Int32 getBufferSize() {return m_nBufferSize;}
448cdf0e10cSrcweir
checkValues(sal_Int32 _nLength,int _nValue)449cdf0e10cSrcweir bool checkValues(sal_Int32 _nLength, int _nValue)
450cdf0e10cSrcweir {
451cdf0e10cSrcweir m_bFoundFailure = false;
452cdf0e10cSrcweir for(sal_Int32 i=0;i<_nLength;i++)
453cdf0e10cSrcweir {
454cdf0e10cSrcweir if (m_pBuffer[i] != _nValue)
455cdf0e10cSrcweir {
456cdf0e10cSrcweir m_bFoundFailure = true;
457cdf0e10cSrcweir }
458cdf0e10cSrcweir }
459cdf0e10cSrcweir return m_bFoundFailure;
460cdf0e10cSrcweir }
461cdf0e10cSrcweir
createBuffer(sal_Int32 _nLength,int _nValue)462cdf0e10cSrcweir void createBuffer(sal_Int32 _nLength, int _nValue)
463cdf0e10cSrcweir {
464cdf0e10cSrcweir m_nBufferSize = _nLength;
465cdf0e10cSrcweir m_pBuffer = (char*) malloc(m_nBufferSize);
466cdf0e10cSrcweir if (m_pBuffer)
467cdf0e10cSrcweir {
468cdf0e10cSrcweir memset(m_pBuffer, _nValue, m_nBufferSize);
469cdf0e10cSrcweir }
470cdf0e10cSrcweir }
471cdf0e10cSrcweir
freeBuffer()472cdf0e10cSrcweir void freeBuffer()
473cdf0e10cSrcweir {
474cdf0e10cSrcweir if (m_pBuffer) free(m_pBuffer);
475cdf0e10cSrcweir }
476cdf0e10cSrcweir
477cdf0e10cSrcweir };
478cdf0e10cSrcweir
479cdf0e10cSrcweir // -----------------------------------------------------------------------------
480cdf0e10cSrcweir /** Client Socket Thread, served as a temp little client to communicate with server.
481cdf0e10cSrcweir */
482cdf0e10cSrcweir
483cdf0e10cSrcweir class ReadSocketThread : public Thread
484cdf0e10cSrcweir {
485cdf0e10cSrcweir int m_nValue;
486cdf0e10cSrcweir ValueCheckProvider m_aValues;
487cdf0e10cSrcweir
488cdf0e10cSrcweir protected:
489cdf0e10cSrcweir oslThreadIdentifier m_id;
490cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr;
491cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket;
492cdf0e10cSrcweir
run()493cdf0e10cSrcweir void SAL_CALL run( )
494cdf0e10cSrcweir {
495cdf0e10cSrcweir TimeValue *pTimeout;
496cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
497cdf0e10cSrcweir pTimeout->Seconds = 5;
498cdf0e10cSrcweir pTimeout->Nanosec = 0;
499cdf0e10cSrcweir
500cdf0e10cSrcweir /// if the thread should terminate, schedule return false
501cdf0e10cSrcweir //while ( schedule( ) == sal_True )
502cdf0e10cSrcweir //{
503cdf0e10cSrcweir if ( osl_Socket_Ok == csConnectorSocket.connect( saTargetSocketAddr, pTimeout ))
504cdf0e10cSrcweir {
505cdf0e10cSrcweir sal_Int32 nReadCount = csConnectorSocket.read( m_aValues.getWriteBuffer(), m_aValues.getBufferSize() );
506cdf0e10cSrcweir m_aValues.checkValues(nReadCount, m_nValue);
507cdf0e10cSrcweir }
508cdf0e10cSrcweir else
509cdf0e10cSrcweir {
51063d99982SDamjan Jovanovic printf("# ReadSocketThread: connect failed! \n");
511cdf0e10cSrcweir }
512cdf0e10cSrcweir // terminate();
513cdf0e10cSrcweir //}
514cdf0e10cSrcweir //remove this line for deadlock on solaris( margritte.germany )
515cdf0e10cSrcweir csConnectorSocket.close();
516cdf0e10cSrcweir free( pTimeout );
517cdf0e10cSrcweir }
518cdf0e10cSrcweir
onTerminated()519cdf0e10cSrcweir void SAL_CALL onTerminated( )
520cdf0e10cSrcweir {
52163d99982SDamjan Jovanovic //printf("# normally terminate this thread %d!\n", m_id );
522cdf0e10cSrcweir }
523cdf0e10cSrcweir
524cdf0e10cSrcweir public:
getCount()525cdf0e10cSrcweir sal_Int32 getCount() {return m_aValues.getBufferSize();}
isOk()526cdf0e10cSrcweir bool isOk() {return m_aValues.isFailure() == true ? false : true;}
527cdf0e10cSrcweir
ReadSocketThread(sal_Int32 _nBufferSize,int _nValue)528cdf0e10cSrcweir ReadSocketThread(sal_Int32 _nBufferSize, int _nValue )
529cdf0e10cSrcweir :
530cdf0e10cSrcweir m_nValue( _nValue ),
531cdf0e10cSrcweir saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT10 ),
532cdf0e10cSrcweir csConnectorSocket( )
533cdf0e10cSrcweir {
534cdf0e10cSrcweir m_id = getIdentifier( );
53563d99982SDamjan Jovanovic //printf("# successfully creat this client thread %d!\n", m_id );
536cdf0e10cSrcweir m_aValues.createBuffer(_nBufferSize, 0);
537cdf0e10cSrcweir }
538cdf0e10cSrcweir
~ReadSocketThread()539cdf0e10cSrcweir ~ReadSocketThread( )
540cdf0e10cSrcweir {
541cdf0e10cSrcweir if ( isRunning( ) )
54263d99982SDamjan Jovanovic printf("# error: client thread not terminated.\n" );
543cdf0e10cSrcweir m_aValues.freeBuffer();
544cdf0e10cSrcweir }
545cdf0e10cSrcweir
546cdf0e10cSrcweir };
547cdf0e10cSrcweir
548cdf0e10cSrcweir /** Server Socket Thread, write a file which is large
549cdf0e10cSrcweir */
550cdf0e10cSrcweir class WriteSocketThread : public Thread
551cdf0e10cSrcweir {
552cdf0e10cSrcweir ValueCheckProvider m_aValues;
553cdf0e10cSrcweir
554cdf0e10cSrcweir protected:
555cdf0e10cSrcweir oslThreadIdentifier m_id;
556cdf0e10cSrcweir
run()557cdf0e10cSrcweir void SAL_CALL run( )
558cdf0e10cSrcweir {
559cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
560cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT10 );
561cdf0e10cSrcweir ::osl::StreamSocket ssStreamConnection;
562cdf0e10cSrcweir
563cdf0e10cSrcweir //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
564cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
565cdf0e10cSrcweir
566cdf0e10cSrcweir /// if the thread should terminate, schedule return false
567cdf0e10cSrcweir while ( schedule( ) == sal_True )
568cdf0e10cSrcweir {
569cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
570cdf0e10cSrcweir if ( sal_True != bOK1 )
571cdf0e10cSrcweir {
57263d99982SDamjan Jovanovic printf("# WriteSocketThread: AcceptorSocket bind address failed. \n" ) ;
573cdf0e10cSrcweir break;
574cdf0e10cSrcweir }
575cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
576cdf0e10cSrcweir if ( sal_True != bOK2 )
577cdf0e10cSrcweir {
57863d99982SDamjan Jovanovic printf("# WriteSocketThread: AcceptorSocket listen address failed. \n" ) ;
579cdf0e10cSrcweir break;
580cdf0e10cSrcweir }
581cdf0e10cSrcweir // blocking mode, if read/recv failed, block until success
582cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_False);
583cdf0e10cSrcweir
584cdf0e10cSrcweir oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
585cdf0e10cSrcweir if (eResult != osl_Socket_Ok )
586cdf0e10cSrcweir {
58763d99982SDamjan Jovanovic printf("WriteSocketThread: acceptConnection failed! \n");
588cdf0e10cSrcweir break;
589cdf0e10cSrcweir }
590cdf0e10cSrcweir
591cdf0e10cSrcweir ssStreamConnection.write( m_aValues.getBuffer(), m_aValues.getBufferSize() );
592cdf0e10cSrcweir break;
593cdf0e10cSrcweir }
594cdf0e10cSrcweir ssStreamConnection.close();
595cdf0e10cSrcweir asAcceptorSocket.close();
596cdf0e10cSrcweir }
597cdf0e10cSrcweir
onTerminated()598cdf0e10cSrcweir void SAL_CALL onTerminated( )
599cdf0e10cSrcweir {
60063d99982SDamjan Jovanovic //printf("# normally terminate this server thread %d!\n", m_id );
601cdf0e10cSrcweir }
602cdf0e10cSrcweir
603cdf0e10cSrcweir public:
604*99ad85ffSJohn Bampton // public to check if data transmission is OK
WriteSocketThread(sal_Int32 _nBufferSize,int _nValue)605cdf0e10cSrcweir WriteSocketThread(sal_Int32 _nBufferSize, int _nValue )
606cdf0e10cSrcweir {
607cdf0e10cSrcweir m_id = getIdentifier( );
608*99ad85ffSJohn Bampton //printf("# successfully create this server thread %d!\n", m_id );
609cdf0e10cSrcweir
610cdf0e10cSrcweir m_aValues.createBuffer(_nBufferSize, _nValue);
611cdf0e10cSrcweir }
612cdf0e10cSrcweir
~WriteSocketThread()613cdf0e10cSrcweir ~WriteSocketThread( )
614cdf0e10cSrcweir {
615cdf0e10cSrcweir if ( isRunning( ) )
61663d99982SDamjan Jovanovic printf("# error: server thread not terminated.\n" );
617cdf0e10cSrcweir m_aValues.freeBuffer();
618cdf0e10cSrcweir }
619cdf0e10cSrcweir
620cdf0e10cSrcweir };
621cdf0e10cSrcweir
622cdf0e10cSrcweir // -----------------------------------------------------------------------------
623cdf0e10cSrcweir // just used to test socket::close() when accepting
624cdf0e10cSrcweir class AcceptorThread : public Thread
625cdf0e10cSrcweir {
626cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket;
627cdf0e10cSrcweir ::rtl::OUString aHostIP;
628cdf0e10cSrcweir sal_Bool bOK;
629cdf0e10cSrcweir protected:
run()630cdf0e10cSrcweir void SAL_CALL run( )
631cdf0e10cSrcweir {
632cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 );
633cdf0e10cSrcweir ::osl::StreamSocket ssStreamConnection;
634cdf0e10cSrcweir
635cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
636cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
637cdf0e10cSrcweir if ( sal_True != bOK1 )
638cdf0e10cSrcweir {
63963d99982SDamjan Jovanovic printf("# AcceptorSocket bind address failed.\n" ) ;
640cdf0e10cSrcweir return;
641cdf0e10cSrcweir }
642cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
643cdf0e10cSrcweir if ( sal_True != bOK2 )
644cdf0e10cSrcweir {
64563d99982SDamjan Jovanovic printf("# AcceptorSocket listen address failed.\n" ) ;
646cdf0e10cSrcweir return;
647cdf0e10cSrcweir }
648cdf0e10cSrcweir
649cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_False );
650cdf0e10cSrcweir
651cdf0e10cSrcweir oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
652cdf0e10cSrcweir if (eResult != osl_Socket_Ok )
653cdf0e10cSrcweir {
654cdf0e10cSrcweir bOK = sal_True;
65563d99982SDamjan Jovanovic printf("AcceptorThread: acceptConnection failed! \n");
656cdf0e10cSrcweir }
657cdf0e10cSrcweir }
658cdf0e10cSrcweir public:
AcceptorThread(::osl::AcceptorSocket & asSocket,::rtl::OUString & aBindIP)659cdf0e10cSrcweir AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString & aBindIP )
660cdf0e10cSrcweir : asAcceptorSocket( asSocket ), aHostIP( aBindIP )
661cdf0e10cSrcweir {
662cdf0e10cSrcweir bOK = sal_False;
663cdf0e10cSrcweir }
664cdf0e10cSrcweir
isOK()665cdf0e10cSrcweir sal_Bool isOK() { return bOK; }
666cdf0e10cSrcweir
~AcceptorThread()667cdf0e10cSrcweir ~AcceptorThread( )
668cdf0e10cSrcweir {
669cdf0e10cSrcweir if ( isRunning( ) )
670cdf0e10cSrcweir {
671cdf0e10cSrcweir asAcceptorSocket.shutdown();
67263d99982SDamjan Jovanovic printf("# error: Acceptor thread not terminated.\n" );
673cdf0e10cSrcweir }
674cdf0e10cSrcweir }
675cdf0e10cSrcweir };
676cdf0e10cSrcweir
677cdf0e10cSrcweir class CloseSocketThread : public Thread
678cdf0e10cSrcweir {
679cdf0e10cSrcweir ::osl::Socket m_sSocket;
680cdf0e10cSrcweir protected:
run()681cdf0e10cSrcweir void SAL_CALL run( )
682cdf0e10cSrcweir {
683cdf0e10cSrcweir thread_sleep( 1 );
684cdf0e10cSrcweir m_sSocket.close( );
685cdf0e10cSrcweir }
686cdf0e10cSrcweir public:
CloseSocketThread(::osl::Socket & sSocket)687cdf0e10cSrcweir CloseSocketThread(::osl::Socket & sSocket )
688cdf0e10cSrcweir : m_sSocket( sSocket )
689cdf0e10cSrcweir {
690cdf0e10cSrcweir }
691cdf0e10cSrcweir
~CloseSocketThread()692cdf0e10cSrcweir ~CloseSocketThread( )
693cdf0e10cSrcweir {
694cdf0e10cSrcweir if ( isRunning( ) )
695cdf0e10cSrcweir {
69663d99982SDamjan Jovanovic printf("# error: CloseSocketThread not terminated.\n" );
697cdf0e10cSrcweir }
698cdf0e10cSrcweir }
699cdf0e10cSrcweir };
700cdf0e10cSrcweir
701cdf0e10cSrcweir //------------------------------------------------------------------------
702cdf0e10cSrcweir // tests cases begins here
703cdf0e10cSrcweir //------------------------------------------------------------------------
704cdf0e10cSrcweir
705cdf0e10cSrcweir namespace osl_SocketAddr
706cdf0e10cSrcweir {
707cdf0e10cSrcweir
708cdf0e10cSrcweir /** testing the methods:
709cdf0e10cSrcweir inline SocketAddr();
710cdf0e10cSrcweir inline SocketAddr(const SocketAddr& Addr);
711cdf0e10cSrcweir inline SocketAddr(const oslSocketAddr , __osl_socket_NoCopy nocopy );
712cdf0e10cSrcweir inline SocketAddr(oslSocketAddr Addr);
713cdf0e10cSrcweir inline SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort );
714cdf0e10cSrcweir */
715cdf0e10cSrcweir
71663d99982SDamjan Jovanovic class SocketAddrCtors : public ::testing::Test
717cdf0e10cSrcweir {
718cdf0e10cSrcweir public:
71963d99982SDamjan Jovanovic }; // class ctors
720cdf0e10cSrcweir
TEST_F(SocketAddrCtors,ctors_none)72163d99982SDamjan Jovanovic TEST_F(SocketAddrCtors, ctors_none)
722cdf0e10cSrcweir {
723cdf0e10cSrcweir /// SocketAddr constructor.
724cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr;
725cdf0e10cSrcweir
726cdf0e10cSrcweir // oslSocketResult aResult;
727cdf0e10cSrcweir // rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult);
728cdf0e10cSrcweir
729cdf0e10cSrcweir // rtl::OUString suHost2 = getThisHostname();
730cdf0e10cSrcweir
73163d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == saSocketAddr.is( )) << "test for none parameter constructor function: check if the socket address was created successfully";
732cdf0e10cSrcweir }
733cdf0e10cSrcweir
TEST_F(SocketAddrCtors,ctors_none_000)73463d99982SDamjan Jovanovic TEST_F(SocketAddrCtors, ctors_none_000)
735cdf0e10cSrcweir {
736cdf0e10cSrcweir /// SocketAddr constructor.
737cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr;
738cdf0e10cSrcweir
739cdf0e10cSrcweir oslSocketResult aResult;
740cdf0e10cSrcweir rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult);
741cdf0e10cSrcweir rtl::OUString suHost2 = getThisHostname();
742cdf0e10cSrcweir
743cdf0e10cSrcweir sal_Bool bOk = compareUString(suHost, suHost2);
744cdf0e10cSrcweir
745cdf0e10cSrcweir rtl::OUString suError = rtl::OUString::createFromAscii("Host names should be the same. From SocketAddr.getLocalHostname() it is'");
746cdf0e10cSrcweir suError += suHost;
747cdf0e10cSrcweir suError += rtl::OUString::createFromAscii("', from getThisHostname() it is '");
748cdf0e10cSrcweir suError += suHost2;
749cdf0e10cSrcweir suError += rtl::OUString::createFromAscii("'.");
750cdf0e10cSrcweir
75163d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOk) << suError.pData;
752cdf0e10cSrcweir }
753cdf0e10cSrcweir
TEST_F(SocketAddrCtors,ctors_copy)75463d99982SDamjan Jovanovic TEST_F(SocketAddrCtors, ctors_copy)
755cdf0e10cSrcweir {
756cdf0e10cSrcweir /// SocketAddr copy constructor.
757cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_HTTP1 );
758cdf0e10cSrcweir ::osl::SocketAddr saCopySocketAddr( saSocketAddr );
759cdf0e10cSrcweir
760cdf0e10cSrcweir sal_Int32 nPort = saCopySocketAddr.getPort( );
761cdf0e10cSrcweir
76263d99982SDamjan Jovanovic ASSERT_TRUE(( sal_True == saCopySocketAddr.is( ) ) && ( nPort == IP_PORT_HTTP1 )) << "test for SocketAddr copy constructor function: copy constructor, do an action of copy construction then check the port with original set.";
763cdf0e10cSrcweir }
764cdf0e10cSrcweir
TEST_F(SocketAddrCtors,ctors_copy_no_001)76563d99982SDamjan Jovanovic TEST_F(SocketAddrCtors, ctors_copy_no_001)
766cdf0e10cSrcweir {
767cdf0e10cSrcweir #if 0
768cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_HTTP1 );
769cdf0e10cSrcweir oslSocketAddr psaOSLSocketAddr = saSocketAddr.getHandle( );
770cdf0e10cSrcweir
771cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrCopy( psaOSLSocketAddr, SAL_NO_COPY );
772cdf0e10cSrcweir saSocketAddrCopy.setPort( IP_PORT_HTTP2 );
773cdf0e10cSrcweir
77463d99982SDamjan Jovanovic ASSERT_TRUE(saSocketAddr.getPort( ) == IP_PORT_HTTP2) << "test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.";
775cdf0e10cSrcweir #endif
776cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( aHostName1, IP_PORT_HTTP1 );
77763d99982SDamjan Jovanovic ASSERT_TRUE(pSocketAddr != NULL) << "check for new SocketAddr";
778cdf0e10cSrcweir
779cdf0e10cSrcweir oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( );
780cdf0e10cSrcweir
781cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY );
782cdf0e10cSrcweir
783cdf0e10cSrcweir pSocketAddrCopy->setPort( IP_PORT_HTTP2 );
78463d99982SDamjan Jovanovic ASSERT_TRUE(pSocketAddr->getPort( ) == IP_PORT_HTTP2) << "test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.";
785cdf0e10cSrcweir
786cdf0e10cSrcweir delete pSocketAddrCopy;
787cdf0e10cSrcweir // LLA: don't do this also: delete pSocketAddr;
788cdf0e10cSrcweir }
789cdf0e10cSrcweir
TEST_F(SocketAddrCtors,ctors_copy_no_002)79063d99982SDamjan Jovanovic TEST_F(SocketAddrCtors, ctors_copy_no_002)
791cdf0e10cSrcweir {
792cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( aHostName1, IP_PORT_HTTP1 );
79363d99982SDamjan Jovanovic ASSERT_TRUE(pSocketAddr != NULL) << "check for new SocketAddr";
794cdf0e10cSrcweir oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( );
795cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY );
796cdf0e10cSrcweir
79763d99982SDamjan Jovanovic ASSERT_TRUE(pSocketAddr->getHandle( ) == pSocketAddrCopy->getHandle( )) << "test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.";
798cdf0e10cSrcweir
799cdf0e10cSrcweir delete pSocketAddrCopy;
800cdf0e10cSrcweir }
801cdf0e10cSrcweir
TEST_F(SocketAddrCtors,ctors_copy_handle_001)80263d99982SDamjan Jovanovic TEST_F(SocketAddrCtors, ctors_copy_handle_001)
803cdf0e10cSrcweir {
804cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_HTTP1 );
805cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrCopy( saSocketAddr.getHandle( ) );
806cdf0e10cSrcweir
80763d99982SDamjan Jovanovic ASSERT_TRUE(saSocketAddrCopy.getPort( ) == IP_PORT_HTTP1) << "test for SocketAddr copy handle constructor function: copy another Socket's handle, get its port to check copy effect.";
808cdf0e10cSrcweir }
809cdf0e10cSrcweir
TEST_F(SocketAddrCtors,ctors_copy_handle_002)81063d99982SDamjan Jovanovic TEST_F(SocketAddrCtors, ctors_copy_handle_002)
811cdf0e10cSrcweir {
812cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_HTTP1 );
813cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrCopy( saSocketAddr.getHandle( ) );
814cdf0e10cSrcweir saSocketAddrCopy.setPort( IP_PORT_HTTP2 );
815cdf0e10cSrcweir
81663d99982SDamjan Jovanovic ASSERT_TRUE(saSocketAddr.getPort( ) != IP_PORT_HTTP2) << "test for SocketAddr copy handle constructor function: copy another Socket's handle, the original one should not be changed.";
817cdf0e10cSrcweir }
818cdf0e10cSrcweir
TEST_F(SocketAddrCtors,ctors_hostname_port_001)81963d99982SDamjan Jovanovic TEST_F(SocketAddrCtors, ctors_hostname_port_001)
820cdf0e10cSrcweir {
821cdf0e10cSrcweir /// tcpip-specif constructor.
822cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP );
823cdf0e10cSrcweir printUString(saSocketAddr.getHostname( ), "ctors_hostname_port_001:getHostname");
824cdf0e10cSrcweir
82563d99982SDamjan Jovanovic ASSERT_TRUE(saSocketAddr.is( ) == sal_True &&
826cdf0e10cSrcweir ( saSocketAddr.getPort( ) == IP_PORT_FTP )/*&&
82730acf5e8Spfg ( sal_True == compareUString( saSocketAddr.getHostname( ), aHostName1 ) ) */) << "test for SocketAddr tcpip specific constructor function: do a constructor using tcpip spec, check the result.";
828cdf0e10cSrcweir }
829cdf0e10cSrcweir
830cdf0e10cSrcweir //same as is_002
TEST_F(SocketAddrCtors,ctors_hostname_port_002)83163d99982SDamjan Jovanovic TEST_F(SocketAddrCtors, ctors_hostname_port_002)
832cdf0e10cSrcweir {
833cdf0e10cSrcweir /// tcpip-specif constructor.
834cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIpInval1, IP_PORT_MYPORT2 );
835cdf0e10cSrcweir
83630acf5e8Spfg ASSERT_TRUE(sal_False == saSocketAddr.is( )) << "test for SocketAddr tcpip specific constructor function: using an invalid IP address, the socketaddr ctors should fail";
837cdf0e10cSrcweir }
838cdf0e10cSrcweir
839cdf0e10cSrcweir /** testing the method:
840cdf0e10cSrcweir inline sal_Bool is() const;
841cdf0e10cSrcweir */
842cdf0e10cSrcweir
84363d99982SDamjan Jovanovic class is : public ::testing::Test
844cdf0e10cSrcweir {
845cdf0e10cSrcweir public:
84663d99982SDamjan Jovanovic }; // class is
84763d99982SDamjan Jovanovic
TEST_F(is,is_001)84863d99982SDamjan Jovanovic TEST_F(is, is_001)
849cdf0e10cSrcweir {
850cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr;
851cdf0e10cSrcweir
85263d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == saSocketAddr.is( )) << "test for is() function: create an unknown type socket, it should be True when call is.";
853cdf0e10cSrcweir }
854cdf0e10cSrcweir // refer to setPort_003()
TEST_F(is,is_002)85563d99982SDamjan Jovanovic TEST_F(is, is_002)
856cdf0e10cSrcweir {
857cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_INVAL );
858cdf0e10cSrcweir
85963d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == saSocketAddr.is( )) << "test for is() function: create a tcp-ip socket using invalid port number";
860cdf0e10cSrcweir }
861cdf0e10cSrcweir
TEST_F(is,is_003)86263d99982SDamjan Jovanovic TEST_F(is, is_003)
863cdf0e10cSrcweir {
864cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIpInval1, IP_PORT_MYPORT );
865cdf0e10cSrcweir
86663d99982SDamjan Jovanovic ASSERT_TRUE(sal_True != saSocketAddr.is( )) << "test for is() function: create a tcp-ip socket using invalid Ip number";
867cdf0e10cSrcweir }
868cdf0e10cSrcweir
869cdf0e10cSrcweir /** testing the method:
870cdf0e10cSrcweir inline ::rtl::OUString SAL_CALL getHostname( oslSocketResult *pResult = 0 ) const;
871cdf0e10cSrcweir */
872cdf0e10cSrcweir
87363d99982SDamjan Jovanovic class getHostname : public ::testing::Test
874cdf0e10cSrcweir {
875cdf0e10cSrcweir public:
SetUp()87663d99982SDamjan Jovanovic void SetUp()
877cdf0e10cSrcweir {
878cdf0e10cSrcweir }
879cdf0e10cSrcweir
TearDown()88063d99982SDamjan Jovanovic void TearDown()
881cdf0e10cSrcweir {
882cdf0e10cSrcweir }
88363d99982SDamjan Jovanovic }; // class getHostname
884cdf0e10cSrcweir
TEST_F(getHostname,getHostname_000)88563d99982SDamjan Jovanovic TEST_F(getHostname, getHostname_000)
886cdf0e10cSrcweir {
887cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp4, IP_PORT_FTP );
888cdf0e10cSrcweir
889cdf0e10cSrcweir }
890cdf0e10cSrcweir
891cdf0e10cSrcweir /** it will search the Ip in current machine's /etc/hosts at first, if find, then return the
892cdf0e10cSrcweir mapped hostname, otherwise, it will search via DNS server, and often return hostname+ Domain name
893cdf0e10cSrcweir like "sceri.PRC.Sun.COM"
894cdf0e10cSrcweir The process is same as Socket::getLocalHost(), but getLocalHost can only return hostname of the current machine.
895cdf0e10cSrcweir */
TEST_F(getHostname,getHostname_001)89663d99982SDamjan Jovanovic TEST_F(getHostname, getHostname_001)
897cdf0e10cSrcweir {
898cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp4, IP_PORT_FTP );
899cdf0e10cSrcweir rtl::OUString suResult = saSocketAddr.getHostname( 0 );
900cdf0e10cSrcweir rtl::OUString suError = outputError(suResult, aHostName4, "test for getHostname(0)");
901cdf0e10cSrcweir sal_Bool bOK = compareUString( suResult, aHostName4 );
902cdf0e10cSrcweir // search the returned hostname in /etc/hosts, if find, and the IP in the row is same as IP
903cdf0e10cSrcweir // in the Addr, it's right also.
904cdf0e10cSrcweir if ( bOK == sal_False)
905cdf0e10cSrcweir {
906cdf0e10cSrcweir if ( compareUString( getIPbyName( oustring2char( suResult ) ), aHostIp4 ) == sal_True )
907cdf0e10cSrcweir bOK = sal_True;
908cdf0e10cSrcweir }
90963d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << suError.pData;
910cdf0e10cSrcweir }
911cdf0e10cSrcweir
912cdf0e10cSrcweir // LLA: now we have to control, if this behaviour is right.
913cdf0e10cSrcweir // LLA: this function does not work in company (Linux, Windows) but at home
TEST_F(getHostname,getHostname_002)91463d99982SDamjan Jovanovic TEST_F(getHostname, getHostname_002)
915cdf0e10cSrcweir {
916cdf0e10cSrcweir rtl::OUString suHostname = rtl::OUString::createFromAscii("cn-1.germany.sun.com");
917cdf0e10cSrcweir rtl::OUString aHostIP = getIPbyName( oustring2char( suHostname ) );
918cdf0e10cSrcweir
919cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostName1, IP_PORT_FTP );
920cdf0e10cSrcweir sal_Bool bOK = saSocketAddr.setHostname( suHostname );
92163d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << "#SocketAddr.setHostname failed";
922cdf0e10cSrcweir oslSocketResult aResult;
923cdf0e10cSrcweir rtl::OUString suResult = saSocketAddr.getHostname( &aResult );
92463d99982SDamjan Jovanovic ASSERT_TRUE(aResult == osl_Socket_Ok) << "SocketAddr.getHostname failed.";
925cdf0e10cSrcweir
926cdf0e10cSrcweir rtl::OUString suError = outputError(suResult, suHostname, "test for getHostname(0)");
927cdf0e10cSrcweir bOK = compareUString( suResult, suHostname );
928cdf0e10cSrcweir if ( bOK == sal_False)
929cdf0e10cSrcweir {
930cdf0e10cSrcweir rtl::OString aString = ::rtl::OUStringToOString( suResult, RTL_TEXTENCODING_ASCII_US );
931cdf0e10cSrcweir if ( compareUString( getIPbyName( aString) , aHostIp6 ) == sal_True )
932cdf0e10cSrcweir {
933cdf0e10cSrcweir bOK = sal_True;
934cdf0e10cSrcweir }
935cdf0e10cSrcweir }
936cdf0e10cSrcweir
93763d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << suError.pData;
938cdf0e10cSrcweir }
939cdf0e10cSrcweir
940cdf0e10cSrcweir /** testing the method:
941cdf0e10cSrcweir inline sal_Int32 SAL_CALL getPort() const;
942cdf0e10cSrcweir */
943cdf0e10cSrcweir
94463d99982SDamjan Jovanovic class getPort : public ::testing::Test
945cdf0e10cSrcweir {
946cdf0e10cSrcweir public:
94763d99982SDamjan Jovanovic }; // class getPort
94863d99982SDamjan Jovanovic
TEST_F(getPort,getPort_001)94963d99982SDamjan Jovanovic TEST_F(getPort, getPort_001)
950cdf0e10cSrcweir {
951cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP );
952cdf0e10cSrcweir
95363d99982SDamjan Jovanovic ASSERT_TRUE(IP_PORT_FTP == saSocketAddr.getPort( )) << "test for getPort() function: get a normal port number.";
954cdf0e10cSrcweir }
955cdf0e10cSrcweir
TEST_F(getPort,getPort_002)95663d99982SDamjan Jovanovic TEST_F(getPort, getPort_002)
957cdf0e10cSrcweir {
958cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp2, IP_PORT_INVAL );
959cdf0e10cSrcweir
96063d99982SDamjan Jovanovic //printf("#getPort_002: Port number is %d \n", saSocketAddr.getPort( ));
961cdf0e10cSrcweir
96263d99982SDamjan Jovanovic ASSERT_TRUE(saSocketAddr.getPort( )>=1 && saSocketAddr.getPort( ) <= 65535) << "test for getPort( ) function: give an invalid port to a SocketAddr, get the port to see if it can detect. it did not pass in (W32).";
963cdf0e10cSrcweir }
964cdf0e10cSrcweir //two cases will return OSL_INVALID_PORT: 1. not valid SocketAddr
965cdf0e10cSrcweir //2. SocketAddr family is not osl_Socket_FamilyInet, but case 2 could not be constructed
TEST_F(getPort,getPort_003)96663d99982SDamjan Jovanovic TEST_F(getPort, getPort_003)
967cdf0e10cSrcweir {
968cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIpInval1, IP_PORT_MYPORT );
969cdf0e10cSrcweir
97063d99982SDamjan Jovanovic ASSERT_TRUE(saSocketAddr.getPort( ) == OSL_INVALID_PORT) << "test for getPort( ) function: give an invalid IP to a SocketAddr, get the port to see returned value. ";
971cdf0e10cSrcweir }
972cdf0e10cSrcweir
973cdf0e10cSrcweir /** testing the method:
974cdf0e10cSrcweir inline sal_Bool SAL_CALL setPort( sal_Int32 nPort );
975cdf0e10cSrcweir rfc1413.txt: TCP port numbers are from 1-65535
976cdf0e10cSrcweir rfc1700.txt: 0/tcp Reserved ; 0/udp Reserved
977cdf0e10cSrcweir */
978cdf0e10cSrcweir
97963d99982SDamjan Jovanovic class setPort : public ::testing::Test
980cdf0e10cSrcweir {
981cdf0e10cSrcweir public:
98263d99982SDamjan Jovanovic }; // class setPort
98363d99982SDamjan Jovanovic
TEST_F(setPort,setPort_001)98463d99982SDamjan Jovanovic TEST_F(setPort, setPort_001)
985cdf0e10cSrcweir {
986cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP );
987cdf0e10cSrcweir sal_Bool bOK = saSocketAddr.setPort( IP_PORT_TELNET );
988cdf0e10cSrcweir
98963d99982SDamjan Jovanovic ASSERT_TRUE(( sal_True == bOK ) &&
99063d99982SDamjan Jovanovic ( IP_PORT_TELNET == saSocketAddr.getPort( ) )) << "test for setPort() function: modify a port number setting, and check it.";
991cdf0e10cSrcweir }
992cdf0e10cSrcweir
993cdf0e10cSrcweir /** 0 to 1024 is known as the reserved port range (traditionally only root can assign programs to ports in
994cdf0e10cSrcweir this range) and the ephemeral port range from 1025 to 65535.
995cdf0e10cSrcweir As many of you programmers will know, when you specify the source port of 0 when you connect to a host,
996cdf0e10cSrcweir the OS automatically reassigns the port number to high numbered ephemeral port. The same happens if you
997cdf0e10cSrcweir try to bind a listening socket to port 0.
998cdf0e10cSrcweir http://www.securiteam.com/securityreviews/5XP0Q2AAKS.html
999cdf0e10cSrcweir another: http://www.muq.org/~cynbe/muq/mufref_564.html
1000cdf0e10cSrcweir */
TEST_F(setPort,setPort_002)100163d99982SDamjan Jovanovic TEST_F(setPort, setPort_002)
1002cdf0e10cSrcweir {
1003cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP );
1004cdf0e10cSrcweir sal_Bool bOK = saSocketAddr.setPort( IP_PORT_ZERO );
1005cdf0e10cSrcweir
1006cdf0e10cSrcweir oslSocket sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1007cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
1008cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 );//sal_True);
1009cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saSocketAddr );
101063d99982SDamjan Jovanovic ASSERT_TRUE(bOK1 == sal_True) << "bind SocketAddr failed";
1011cdf0e10cSrcweir
1012cdf0e10cSrcweir sal_Int32 newPort = sSocket.getLocalPort();
101363d99982SDamjan Jovanovic //printf("#new port is %d\n", newPort );
1014cdf0e10cSrcweir
101563d99982SDamjan Jovanovic ASSERT_TRUE(( 1024 <= newPort ) && ( 65535 >= newPort ) && ( bOK == sal_True )) << "test for setPort() function: port number should be in 1 ~ 65535, set port 0, it should be converted to a port number between 1024~65535.";
1016cdf0e10cSrcweir
1017cdf0e10cSrcweir }
1018cdf0e10cSrcweir
TEST_F(setPort,setPort_003)101963d99982SDamjan Jovanovic TEST_F(setPort, setPort_003)
1020cdf0e10cSrcweir {
1021cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP);
1022cdf0e10cSrcweir sal_Bool bOK = saSocketAddr.setPort( IP_PORT_INVAL );
1023cdf0e10cSrcweir //on Linux, getPort return 34463
102463d99982SDamjan Jovanovic //printf("#Port number is %d \n", saSocketAddr.getPort( ));
1025cdf0e10cSrcweir
102663d99982SDamjan Jovanovic ASSERT_TRUE(( ( 1 <= saSocketAddr.getPort( ) ) && ( 65535 >= saSocketAddr.getPort( ) ) &&( bOK == sal_True ) ) ||
102763d99982SDamjan Jovanovic bOK == sal_False) << "test for setPort( ) function: set an address with invalid port. it should return error or convert it to a valid port.";
1028cdf0e10cSrcweir }
1029cdf0e10cSrcweir
1030cdf0e10cSrcweir /* this is not a inet-addr => can't set port */
TEST_F(setPort,setPort_004)103163d99982SDamjan Jovanovic TEST_F(setPort, setPort_004)
1032cdf0e10cSrcweir {
1033cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIpInval1, IP_PORT_FTP);
1034cdf0e10cSrcweir sal_Bool bOK = saSocketAddr.setPort( IP_PORT_MYPORT );
1035cdf0e10cSrcweir
103663d99982SDamjan Jovanovic ASSERT_TRUE(bOK == sal_False) << "test for setPort( ) function: set an invalid address with valid port. it should return error.";
1037cdf0e10cSrcweir }
1038cdf0e10cSrcweir
1039cdf0e10cSrcweir /** tester comment:
1040cdf0e10cSrcweir
1041cdf0e10cSrcweir In the following two functions, it use ::rtl::ByteSequence as an intermediate storage for address,
1042cdf0e10cSrcweir the ByteSequence object can hold sal_Int8 arrays, which is raged [-127, 127], in case of IP addr
1043cdf0e10cSrcweir that is greater than 127, say 129.158.217.202, it will stored as -127, -98, -39, -54, it is unique
1044cdf0e10cSrcweir in the range of sal_Int8, but lack of readability.
1045cdf0e10cSrcweir so may be a sal_uInt8 array is better.
1046cdf0e10cSrcweir */
1047cdf0e10cSrcweir
1048cdf0e10cSrcweir
1049cdf0e10cSrcweir /** testing the method:
1050cdf0e10cSrcweir inline sal_Bool SAL_CALL setAddr( const ::rtl::ByteSequence & address );
1051cdf0e10cSrcweir */
1052cdf0e10cSrcweir
105363d99982SDamjan Jovanovic class setAddr : public ::testing::Test
1054cdf0e10cSrcweir {
1055cdf0e10cSrcweir public:
105663d99982SDamjan Jovanovic }; // class setAddr
105763d99982SDamjan Jovanovic
TEST_F(setAddr,setAddr_001)105863d99982SDamjan Jovanovic TEST_F(setAddr, setAddr_001)
1059cdf0e10cSrcweir {
1060cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp2, IP_PORT_FTP );
1061cdf0e10cSrcweir saSocketAddr.setAddr( UStringIPToByteSequence( aHostIp1 ) );
1062cdf0e10cSrcweir ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( 0 );
1063cdf0e10cSrcweir sal_Bool bOK = sal_False;
1064cdf0e10cSrcweir
1065cdf0e10cSrcweir if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) && ( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
1066cdf0e10cSrcweir bOK = sal_True;
1067cdf0e10cSrcweir
106863d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << "test for setAddr() function: construct Addr with \"129.158.217.202\", set it to \"127.0.0.1\", and check the correctness ";
1069cdf0e10cSrcweir }
1070cdf0e10cSrcweir
1071cdf0e10cSrcweir /** testing the method:
1072cdf0e10cSrcweir inline ::rtl::ByteSequence SAL_CALL getAddr( oslSocketResult *pResult = 0 ) const;
1073cdf0e10cSrcweir */
1074cdf0e10cSrcweir
107563d99982SDamjan Jovanovic class getAddr : public ::testing::Test
1076cdf0e10cSrcweir {
1077cdf0e10cSrcweir public:
107863d99982SDamjan Jovanovic }; // class getAddr
107963d99982SDamjan Jovanovic
TEST_F(getAddr,getAddr_001)108063d99982SDamjan Jovanovic TEST_F(getAddr, getAddr_001)
1081cdf0e10cSrcweir {
1082cdf0e10cSrcweir oslSocketResult SocketResult;
1083cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_FTP );
1084cdf0e10cSrcweir ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( &SocketResult );
1085cdf0e10cSrcweir
1086cdf0e10cSrcweir sal_Bool bOK = sal_False;
1087cdf0e10cSrcweir
1088cdf0e10cSrcweir if ( ( osl_Socket_Ok == SocketResult ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
1089cdf0e10cSrcweir bOK = sal_True;
1090cdf0e10cSrcweir
109163d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK && SocketResult == osl_Socket_Ok) << "test for getAddr() function: construct a socketaddr with IP assigned, get the address to check correctness.Caught unknown exception on (Win32)";
1092cdf0e10cSrcweir }
1093cdf0e10cSrcweir
1094cdf0e10cSrcweir /** testing the methods:
1095cdf0e10cSrcweir inline SocketAddr & SAL_CALL operator= (oslSocketAddr Addr);
1096cdf0e10cSrcweir inline SocketAddr & SAL_CALL operator= (const SocketAddr& Addr);
1097cdf0e10cSrcweir inline SocketAddr & SAL_CALL assign( oslSocketAddr Addr, __osl_socket_NoCopy nocopy );
1098cdf0e10cSrcweir inline sal_Bool SAL_CALL operator== (oslSocketAddr Addr) const;
1099cdf0e10cSrcweir inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; /// not implemented.
1100cdf0e10cSrcweir */
1101cdf0e10cSrcweir
110263d99982SDamjan Jovanovic class operator_equal : public ::testing::Test
1103cdf0e10cSrcweir {
1104cdf0e10cSrcweir public:
110563d99982SDamjan Jovanovic }; // class operator_equal
110663d99982SDamjan Jovanovic
TEST_F(operator_equal,operator_equal_001)110763d99982SDamjan Jovanovic TEST_F(operator_equal, operator_equal_001)
1108cdf0e10cSrcweir {
1109cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_TELNET);
1110cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrEqual( aHostIp2, IP_PORT_FTP );
1111cdf0e10cSrcweir
1112cdf0e10cSrcweir saSocketAddrEqual = saSocketAddr;
1113cdf0e10cSrcweir sal_Bool bOK = sal_False;
1114cdf0e10cSrcweir ::rtl::ByteSequence bsSocketAddr = saSocketAddrEqual.getAddr( 0 );
1115cdf0e10cSrcweir
1116cdf0e10cSrcweir if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
1117cdf0e10cSrcweir bOK = sal_True;
1118cdf0e10cSrcweir
111963d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << "test for operator_equal() function: use operator= to assign Ip1 to Ip2, check its modification.";
1120cdf0e10cSrcweir }
1121cdf0e10cSrcweir
1122cdf0e10cSrcweir
TEST_F(operator_equal,operator_equal_002)112363d99982SDamjan Jovanovic TEST_F(operator_equal, operator_equal_002)
1124cdf0e10cSrcweir {
1125cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp3, IP_PORT_TELNET);
1126cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrEqual( aHostIp2, IP_PORT_FTP );
1127cdf0e10cSrcweir
1128cdf0e10cSrcweir saSocketAddrEqual = saSocketAddr;
112963d99982SDamjan Jovanovic ASSERT_TRUE(IP_PORT_TELNET == saSocketAddrEqual.getPort( )) << "after assign, the assigned SocketAddr is not same as the original Addr";
1130cdf0e10cSrcweir saSocketAddrEqual.setPort( IP_PORT_MYPORT3 );
1131cdf0e10cSrcweir saSocketAddr.setPort( IP_PORT_HTTP2 );
1132cdf0e10cSrcweir
113363d99982SDamjan Jovanovic ASSERT_TRUE(IP_PORT_MYPORT3 == saSocketAddrEqual.getPort( )) << "test for operator_equal() function: perform an equal action, then try to change the original address's port. it should not be changed ( handle released), it did not pass in (W32), this is under discussion.";
1134cdf0e10cSrcweir }
1135cdf0e10cSrcweir
TEST_F(operator_equal,operator_equal_const_001)113663d99982SDamjan Jovanovic TEST_F(operator_equal, operator_equal_const_001)
1137cdf0e10cSrcweir {
1138cdf0e10cSrcweir const ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_TELNET);
1139cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrEqual( aHostIp2, IP_PORT_FTP );
1140cdf0e10cSrcweir
1141cdf0e10cSrcweir saSocketAddrEqual = saSocketAddr;
1142cdf0e10cSrcweir sal_Bool bOK = sal_False;
1143cdf0e10cSrcweir ::rtl::ByteSequence bsSocketAddr = saSocketAddrEqual.getAddr( 0 );
1144cdf0e10cSrcweir
1145cdf0e10cSrcweir if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
1146cdf0e10cSrcweir bOK = sal_True;
1147cdf0e10cSrcweir
114863d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << "test for operator_equal_const() function: use operator= const to assign Ip1 to Ip2, verify the change on the second one.";
1149cdf0e10cSrcweir }
1150cdf0e10cSrcweir
TEST_F(operator_equal,operator_equal_const_002)115163d99982SDamjan Jovanovic TEST_F(operator_equal, operator_equal_const_002)
1152cdf0e10cSrcweir {
1153cdf0e10cSrcweir const ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_TELNET);
1154cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrEqual( aHostIp2, IP_PORT_FTP );
1155cdf0e10cSrcweir
1156cdf0e10cSrcweir saSocketAddrEqual = saSocketAddr;
1157cdf0e10cSrcweir saSocketAddrEqual.setPort( IP_PORT_HTTP1 );
1158cdf0e10cSrcweir
115963d99982SDamjan Jovanovic ASSERT_TRUE(IP_PORT_HTTP1 != saSocketAddr.getPort( )) << "test for operator_equal_const() function: change the second instance, the first one should not be altered, since it does not released the handle.";
1160cdf0e10cSrcweir }
1161cdf0e10cSrcweir
TEST_F(operator_equal,operator_equal_assign_001)116263d99982SDamjan Jovanovic TEST_F(operator_equal, operator_equal_assign_001)
1163cdf0e10cSrcweir {
1164cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( aHostIp1, IP_PORT_TELNET );
116563d99982SDamjan Jovanovic ASSERT_TRUE(pSocketAddr != NULL) << "check for new SocketAddr";
1166cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddrAssign = new ::osl::SocketAddr( aHostIp2, IP_PORT_FTP );
1167cdf0e10cSrcweir oslSocketAddr poslSocketAddr = pSocketAddr->getHandle( );
1168cdf0e10cSrcweir //if( m_handle ) osl_destroySocketAddr( m_handle ); so pSocketAddrAssign had been destroyed and then point to pSocketAddr
1169cdf0e10cSrcweir pSocketAddrAssign->assign(poslSocketAddr, SAL_NO_COPY);
1170cdf0e10cSrcweir
117163d99982SDamjan Jovanovic ASSERT_TRUE(pSocketAddrAssign->getPort( ) == IP_PORT_TELNET) << "test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.";
1172cdf0e10cSrcweir
1173cdf0e10cSrcweir delete pSocketAddrAssign;
1174cdf0e10cSrcweir }
1175cdf0e10cSrcweir
TEST_F(operator_equal,operator_is_equal_001)117663d99982SDamjan Jovanovic TEST_F(operator_equal, operator_is_equal_001)
1177cdf0e10cSrcweir {
1178cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_TELNET);
1179cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrequal( aHostIp1, IP_PORT_TELNET );
1180cdf0e10cSrcweir
118163d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == ( saSocketAddrequal == saSocketAddr.getHandle( ) )) << "test for operator_equal_equal() function: check two identical Address.";
1182cdf0e10cSrcweir }
1183cdf0e10cSrcweir
TEST_F(operator_equal,operator_is_equal_002)118463d99982SDamjan Jovanovic TEST_F(operator_equal, operator_is_equal_002)
1185cdf0e10cSrcweir {
1186cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp2, IP_PORT_FTP);
1187cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrequal( aHostIp1, IP_PORT_TELNET );
1188cdf0e10cSrcweir
118963d99982SDamjan Jovanovic ASSERT_TRUE(sal_False == ( saSocketAddrequal == saSocketAddr.getHandle( ) )) << "test for operator_equal_equal() function: check two different Address.";
1190cdf0e10cSrcweir }
1191cdf0e10cSrcweir
1192cdf0e10cSrcweir
1193cdf0e10cSrcweir /** testing the method:
1194cdf0e10cSrcweir inline oslSocketAddr SAL_CALL getHandle() const;
1195cdf0e10cSrcweir */
1196cdf0e10cSrcweir
119763d99982SDamjan Jovanovic class getSocketAddrHandle : public ::testing::Test
1198cdf0e10cSrcweir {
1199cdf0e10cSrcweir public:
120063d99982SDamjan Jovanovic }; // class getSocketAddrHandle
1201cdf0e10cSrcweir
TEST_F(getSocketAddrHandle,getSocketAddrHandle_001)120263d99982SDamjan Jovanovic TEST_F(getSocketAddrHandle, getSocketAddrHandle_001)
1203cdf0e10cSrcweir {
1204cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( aHostName1, IP_PORT_HTTP1 );
120563d99982SDamjan Jovanovic ASSERT_TRUE(pSocketAddr != NULL) << "check for new SocketAddr";
1206cdf0e10cSrcweir oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( );
1207cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY );
1208cdf0e10cSrcweir
120963d99982SDamjan Jovanovic ASSERT_TRUE(pSocketAddr->getHandle( ) == pSocketAddrCopy->getHandle( )) << "test for SocketAddr no copy constructor function: do a no copy constructor on a given SocketAddr instance, modify the new instance's port, check the original one.";
1210cdf0e10cSrcweir
1211cdf0e10cSrcweir delete pSocketAddrCopy;
1212cdf0e10cSrcweir }
1213cdf0e10cSrcweir
TEST_F(getSocketAddrHandle,getSocketAddrHandle_002)121463d99982SDamjan Jovanovic TEST_F(getSocketAddrHandle, getSocketAddrHandle_002)
1215cdf0e10cSrcweir {
1216cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostName3, IP_PORT_MYPORT4 );
1217cdf0e10cSrcweir oslSocketAddr poslSocketAddr = saSocketAddr.getHandle( );
1218cdf0e10cSrcweir
1219cdf0e10cSrcweir sal_Bool bOK = ( saSocketAddr == poslSocketAddr );
122063d99982SDamjan Jovanovic //printf("getSocketAddrHandle_002\n");
122163d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << "test for getHandle() function: use getHandle() function as an intermediate way to create identical address.";
1222cdf0e10cSrcweir }
1223cdf0e10cSrcweir
1224cdf0e10cSrcweir /** testing the method:
1225cdf0e10cSrcweir static inline ::rtl::OUString SAL_CALL getLocalHostname( oslSocketResult *pResult = 0);
1226cdf0e10cSrcweir */
1227cdf0e10cSrcweir
122863d99982SDamjan Jovanovic class getLocalHostname : public ::testing::Test
1229cdf0e10cSrcweir {
1230cdf0e10cSrcweir public:
123163d99982SDamjan Jovanovic }; // class getLocalHostname
123263d99982SDamjan Jovanovic
1233cdf0e10cSrcweir /* the process of getLocalHostname: 1.gethostname (same as /bin/hostname) returned name A
1234cdf0e10cSrcweir 2. search A in /etc/hosts, if there is an alias name is A, return the name in the same row
1235cdf0e10cSrcweir */
1236cdf0e10cSrcweir
TEST_F(getLocalHostname,getLocalHostname_000)123763d99982SDamjan Jovanovic TEST_F(getLocalHostname, getLocalHostname_000)
1238cdf0e10cSrcweir {
1239cdf0e10cSrcweir // _osl_getFullQualifiedDomainName( );
1240cdf0e10cSrcweir oslSocketResult aResult = osl_Socket_Error;
1241cdf0e10cSrcweir rtl::OUString suHostname = osl::SocketAddr::getLocalHostname(&aResult);
124263d99982SDamjan Jovanovic ASSERT_TRUE(aResult == osl_Socket_Ok) << "getLocalHostname failed";
1243cdf0e10cSrcweir }
1244cdf0e10cSrcweir
TEST_F(getLocalHostname,getLocalHostname_001)124563d99982SDamjan Jovanovic TEST_F(getLocalHostname, getLocalHostname_001)
1246cdf0e10cSrcweir {
1247cdf0e10cSrcweir oslSocketResult *pResult = NULL;
1248cdf0e10cSrcweir //printSocketResult(*pResult);
1249cdf0e10cSrcweir ::rtl::OUString suResult = ::osl::SocketAddr::getLocalHostname( pResult );
1250cdf0e10cSrcweir
1251cdf0e10cSrcweir // LLA: IMHO localhost, or hostname by itself should be ok.
1252cdf0e10cSrcweir rtl::OUString suThisHost = getThisHostname( );
1253cdf0e10cSrcweir bool bOk = false;
1254cdf0e10cSrcweir if (suThisHost.equals(rtl::OUString::createFromAscii("localhost")))
1255cdf0e10cSrcweir {
1256cdf0e10cSrcweir bOk = true;
1257cdf0e10cSrcweir }
1258cdf0e10cSrcweir else
1259cdf0e10cSrcweir {
1260cdf0e10cSrcweir if (suThisHost.equals(suResult))
1261cdf0e10cSrcweir {
1262cdf0e10cSrcweir bOk = true;
1263cdf0e10cSrcweir }
1264cdf0e10cSrcweir }
1265cdf0e10cSrcweir
1266cdf0e10cSrcweir ::rtl::OUString suError;
1267cdf0e10cSrcweir suError = outputError(suResult, getThisHostname( ), "test for getLocalHostname() function");
1268cdf0e10cSrcweir
126963d99982SDamjan Jovanovic ASSERT_TRUE(bOk == true) << suError.pData;
1270cdf0e10cSrcweir }
1271cdf0e10cSrcweir
1272cdf0e10cSrcweir /** testing the method:
1273cdf0e10cSrcweir static inline void SAL_CALL resolveHostname( const ::rtl::OUString & strHostName , SocketAddr & Addr );
1274cdf0e10cSrcweir */
1275cdf0e10cSrcweir
127663d99982SDamjan Jovanovic class resolveHostname : public ::testing::Test
1277cdf0e10cSrcweir {
1278cdf0e10cSrcweir public:
127963d99982SDamjan Jovanovic }; // class resolveHostname
128063d99982SDamjan Jovanovic
TEST_F(resolveHostname,resolveHostname_001)128163d99982SDamjan Jovanovic TEST_F(resolveHostname, resolveHostname_001)
1282cdf0e10cSrcweir {
1283cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr;
1284cdf0e10cSrcweir ::osl::SocketAddr::resolveHostname( aHostIp1, saSocketAddr );
1285cdf0e10cSrcweir ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( 0 );
1286cdf0e10cSrcweir sal_Bool bOK = sal_False;
1287cdf0e10cSrcweir
1288cdf0e10cSrcweir if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) )
1289cdf0e10cSrcweir bOK = sal_True;
1290cdf0e10cSrcweir
129163d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << "test for resolveHostname() function: try to resolve localhost to 127.0.0.1.";
1292cdf0e10cSrcweir }
1293cdf0e10cSrcweir
1294cdf0e10cSrcweir /** testing the method:
1295cdf0e10cSrcweir static inline sal_Int32 SAL_CALL getServicePort(
1296cdf0e10cSrcweir const ::rtl::OUString& strServiceName,
1297cdf0e10cSrcweir const ::rtl::OUString & strProtocolName= ::rtl::OUString::createFromAscii( "tcp" ) );
1298cdf0e10cSrcweir */
1299cdf0e10cSrcweir
130063d99982SDamjan Jovanovic class gettheServicePort : public ::testing::Test
1301cdf0e10cSrcweir {
1302cdf0e10cSrcweir public:
1303cdf0e10cSrcweir }; // class gettheServicePort
1304cdf0e10cSrcweir
TEST_F(gettheServicePort,gettheServicePort_001)130563d99982SDamjan Jovanovic TEST_F(gettheServicePort, gettheServicePort_001)
130663d99982SDamjan Jovanovic {
130763d99982SDamjan Jovanovic ASSERT_TRUE(IP_PORT_FTP== ::osl::SocketAddr::getServicePort( aServiceFTP, aProtocolTCP )) << "test for getServicePort() function: try to get ftp service port on TCP protocol.";
130863d99982SDamjan Jovanovic }
1309cdf0e10cSrcweir
TEST_F(gettheServicePort,gettheServicePort_002)131063d99982SDamjan Jovanovic TEST_F(gettheServicePort, gettheServicePort_002)
131163d99982SDamjan Jovanovic {
131263d99982SDamjan Jovanovic ASSERT_TRUE(IP_PORT_TELNET== ::osl::SocketAddr::getServicePort( aServiceTELNET, aProtocolTCP )) << "test for getServicePort() function: try to get telnet service port on TCP protocol.";
131363d99982SDamjan Jovanovic }
1314cdf0e10cSrcweir
TEST_F(gettheServicePort,gettheServicePort_003)131563d99982SDamjan Jovanovic TEST_F(gettheServicePort, gettheServicePort_003)
131663d99982SDamjan Jovanovic {
131763d99982SDamjan Jovanovic //Solaris has no service called "https", please see /etc/services
131863d99982SDamjan Jovanovic ASSERT_TRUE(IP_PORT_NETBIOS_DGM == ::osl::SocketAddr::getServicePort( aServiceNETBIOS, aProtocolUDP )) << "test for getServicePort() function: try to get netbios-ssn service port on UDP protocol.";
131963d99982SDamjan Jovanovic }
132063d99982SDamjan Jovanovic
TEST_F(gettheServicePort,gettheServicePort_004)132163d99982SDamjan Jovanovic TEST_F(gettheServicePort, gettheServicePort_004)
132263d99982SDamjan Jovanovic {
132363d99982SDamjan Jovanovic ASSERT_TRUE(OSL_INVALID_PORT == ::osl::SocketAddr::getServicePort( ::rtl::OUString::createFromAscii( "notexist" ), aProtocolUDP )) << "test for getServicePort() function: try to get a service port which is not exist.";
132463d99982SDamjan Jovanovic }
1325cdf0e10cSrcweir
1326cdf0e10cSrcweir
1327cdf0e10cSrcweir } // namespace osl_SocketAddr
1328cdf0e10cSrcweir
1329cdf0e10cSrcweir
1330cdf0e10cSrcweir
1331cdf0e10cSrcweir namespace osl_Socket
1332cdf0e10cSrcweir {
1333cdf0e10cSrcweir
1334cdf0e10cSrcweir /** testing the methods:
1335cdf0e10cSrcweir inline Socket( );
1336cdf0e10cSrcweir inline Socket( const Socket & socket );
1337cdf0e10cSrcweir inline Socket( oslSocket socketHandle );
1338cdf0e10cSrcweir inline Socket( oslSocket socketHandle, __sal_NoAcquire noacquire );
1339cdf0e10cSrcweir */
1340cdf0e10cSrcweir
1341cdf0e10cSrcweir /** test writer's comment:
1342cdf0e10cSrcweir
1343cdf0e10cSrcweir class Socket can not be initialized by its protected constructor, though the protected
1344cdf0e10cSrcweir constructor is the most convenient way to create a new socket.
1345cdf0e10cSrcweir it only allow the method of C function osl_createSocket like:
1346cdf0e10cSrcweir ::osl::Socket sSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream,
1347cdf0e10cSrcweir osl_Socket_ProtocolIp ) );
1348cdf0e10cSrcweir the use of C method lost some of the transparent of tester using C++ wrapper.
1349cdf0e10cSrcweir */
1350cdf0e10cSrcweir
1351cdf0e10cSrcweir
135263d99982SDamjan Jovanovic class OslSocketCtors : public ::testing::Test
1353cdf0e10cSrcweir {
1354cdf0e10cSrcweir public:
1355cdf0e10cSrcweir oslSocket sHandle;
1356cdf0e10cSrcweir // initialization
SetUp()135763d99982SDamjan Jovanovic void SetUp()
1358cdf0e10cSrcweir {
1359cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1360cdf0e10cSrcweir }
1361cdf0e10cSrcweir
TearDown()136263d99982SDamjan Jovanovic void TearDown()
1363cdf0e10cSrcweir {
1364cdf0e10cSrcweir sHandle = NULL;
1365cdf0e10cSrcweir }
136663d99982SDamjan Jovanovic };
1367cdf0e10cSrcweir
TEST_F(OslSocketCtors,ctors_none)136863d99982SDamjan Jovanovic TEST_F(OslSocketCtors, ctors_none)
1369cdf0e10cSrcweir {
1370cdf0e10cSrcweir /// Socket constructor.
1371cdf0e10cSrcweir // ::osl::Socket sSocket;
1372cdf0e10cSrcweir
137363d99982SDamjan Jovanovic ASSERT_TRUE(1 == 1) << "test for ctors_none constructor function: check if the socket was created successfully, if no exception occurred";
1374cdf0e10cSrcweir }
1375cdf0e10cSrcweir
TEST_F(OslSocketCtors,ctors_acquire)137663d99982SDamjan Jovanovic TEST_F(OslSocketCtors, ctors_acquire)
1377cdf0e10cSrcweir {
1378cdf0e10cSrcweir /// Socket constructor.
1379cdf0e10cSrcweir ::osl::Socket sSocket( sHandle );
1380cdf0e10cSrcweir
138163d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == sSocket.getType( )) << "test for ctors_acquire constructor function: check if the socket was created successfully";
1382cdf0e10cSrcweir }
1383cdf0e10cSrcweir
TEST_F(OslSocketCtors,ctors_no_acquire)138463d99982SDamjan Jovanovic TEST_F(OslSocketCtors, ctors_no_acquire)
1385cdf0e10cSrcweir {
1386cdf0e10cSrcweir /// Socket constructor.
1387cdf0e10cSrcweir ::osl::Socket sSocket( sHandle, SAL_NO_ACQUIRE );
1388cdf0e10cSrcweir
138963d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == sSocket.getType( )) << " test for ctors_no_acquire constructor function: check if the socket was created successfully";
1390cdf0e10cSrcweir }
1391cdf0e10cSrcweir
TEST_F(OslSocketCtors,ctors_copy_ctor)139263d99982SDamjan Jovanovic TEST_F(OslSocketCtors, ctors_copy_ctor)
1393cdf0e10cSrcweir {
1394cdf0e10cSrcweir ::osl::Socket sSocket( sHandle );
1395cdf0e10cSrcweir /// Socket copy constructor.
1396cdf0e10cSrcweir ::osl::Socket copySocket( sSocket );
1397cdf0e10cSrcweir
139863d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == copySocket.getType( )) << " test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor";
1399cdf0e10cSrcweir }
1400cdf0e10cSrcweir
TEST_F(OslSocketCtors,ctors_TypeRaw)140163d99982SDamjan Jovanovic TEST_F(OslSocketCtors, ctors_TypeRaw)
1402cdf0e10cSrcweir {
1403cdf0e10cSrcweir #ifdef WNT
1404cdf0e10cSrcweir oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
1405cdf0e10cSrcweir // LLA: ? ::osl::Socket sSocket( sHandleRaw );
140663d99982SDamjan Jovanovic ASSERT_TRUE(sHandleRaw != NULL) << " type osl_Socket_TypeRaw socket create failed on UNX ";
1407cdf0e10cSrcweir #else
1408cdf0e10cSrcweir oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
140963d99982SDamjan Jovanovic ASSERT_TRUE(sHandleRaw == NULL) << " can't create socket with type osl_Socket_TypeRaw within UNX is ok.";
1410cdf0e10cSrcweir #endif
1411cdf0e10cSrcweir }
1412cdf0e10cSrcweir
TEST_F(OslSocketCtors,ctors_family_Ipx)141363d99982SDamjan Jovanovic TEST_F(OslSocketCtors, ctors_family_Ipx)
1414cdf0e10cSrcweir {
1415cdf0e10cSrcweir oslSocket sHandleIpx = osl_createSocket( osl_Socket_FamilyIpx, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
141663d99982SDamjan Jovanovic ASSERT_TRUE(sHandleIpx != NULL) << " family osl_Socket_FamilyIpx socket create failed! ";
1417cdf0e10cSrcweir ::osl::Socket sSocket( sHandleIpx ); //, SAL_NO_ACQUIRE );
141863d99982SDamjan Jovanovic printf("#Type is %d \n", sSocket.getType( ) );
1419cdf0e10cSrcweir
142063d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == sSocket.getType( )) << " test for create new Socket instance that family is osl_Socket_FamilyIpx";
1421cdf0e10cSrcweir }
1422cdf0e10cSrcweir
1423cdf0e10cSrcweir
1424cdf0e10cSrcweir /** testing the methods:
1425cdf0e10cSrcweir inline Socket& SAL_CALL operator= ( oslSocket socketHandle);
1426cdf0e10cSrcweir inline Socket& SAL_CALL operator= (const Socket& sock);
1427cdf0e10cSrcweir inline sal_Bool SAL_CALL operator==( const Socket& rSocket ) const ;
1428cdf0e10cSrcweir inline sal_Bool SAL_CALL operator==( const oslSocket socketHandle ) const;
1429cdf0e10cSrcweir */
1430cdf0e10cSrcweir
143163d99982SDamjan Jovanovic class operators : public ::testing::Test
1432cdf0e10cSrcweir {
1433cdf0e10cSrcweir public:
1434cdf0e10cSrcweir oslSocket sHandle;
1435cdf0e10cSrcweir // initialization
SetUp()143663d99982SDamjan Jovanovic void SetUp( )
1437cdf0e10cSrcweir {
1438cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1439cdf0e10cSrcweir }
1440cdf0e10cSrcweir
TearDown()144163d99982SDamjan Jovanovic void TearDown( )
1442cdf0e10cSrcweir {
1443cdf0e10cSrcweir sHandle = NULL;
1444cdf0e10cSrcweir }
144563d99982SDamjan Jovanovic }; // class operators
144663d99982SDamjan Jovanovic
1447cdf0e10cSrcweir
1448cdf0e10cSrcweir
1449cdf0e10cSrcweir /** test writer's comment:
1450cdf0e10cSrcweir
1451*99ad85ffSJohn Bampton the assignment operator does not support direct assignment like:
1452cdf0e10cSrcweir ::osl::Socket sSocket = sHandle.
1453cdf0e10cSrcweir */
TEST_F(operators,operators_assignment_handle)145463d99982SDamjan Jovanovic TEST_F(operators, operators_assignment_handle)
1455cdf0e10cSrcweir {
1456cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
1457cdf0e10cSrcweir ::osl::Socket assignSocket = sSocket.getHandle();
1458cdf0e10cSrcweir
145963d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == assignSocket.getType( )) << "test for operators_assignment_handle function: test the assignment operator.";
1460cdf0e10cSrcweir }
1461cdf0e10cSrcweir
TEST_F(operators,operators_assignment)146263d99982SDamjan Jovanovic TEST_F(operators, operators_assignment)
1463cdf0e10cSrcweir {
1464cdf0e10cSrcweir ::osl::Socket sSocket( sHandle );
1465cdf0e10cSrcweir ::osl::Socket assignSocket = sSocket;
1466cdf0e10cSrcweir
146763d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == assignSocket.getType( )) << "test for operators_assignment function: assignment operator";
1468cdf0e10cSrcweir }
1469cdf0e10cSrcweir
TEST_F(operators,operators_equal_handle_001)147063d99982SDamjan Jovanovic TEST_F(operators, operators_equal_handle_001)
1471cdf0e10cSrcweir {
1472cdf0e10cSrcweir /// Socket constructor.
1473cdf0e10cSrcweir ::osl::Socket sSocket( sHandle );
1474cdf0e10cSrcweir ::osl::Socket equalSocket = sSocket;
1475cdf0e10cSrcweir
147663d99982SDamjan Jovanovic ASSERT_TRUE(equalSocket == sHandle) << " test for operators_equal_handle_001 function: check equal.";
1477cdf0e10cSrcweir }
1478cdf0e10cSrcweir
TEST_F(operators,operators_equal_handle_002)147963d99982SDamjan Jovanovic TEST_F(operators, operators_equal_handle_002)
1480cdf0e10cSrcweir {
1481cdf0e10cSrcweir /// Socket constructor.
1482cdf0e10cSrcweir ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) );
1483cdf0e10cSrcweir
148463d99982SDamjan Jovanovic ASSERT_TRUE(!( equalSocket == sHandle )) << " test for operators_equal_handle_001 function: check unequal.";
1485cdf0e10cSrcweir }
1486cdf0e10cSrcweir
TEST_F(operators,operators_equal_001)148763d99982SDamjan Jovanovic TEST_F(operators, operators_equal_001)
1488cdf0e10cSrcweir {
1489cdf0e10cSrcweir ::osl::Socket sSocket( sHandle );
1490cdf0e10cSrcweir /// Socket copy constructor.
1491cdf0e10cSrcweir ::osl::Socket equalSocket( sSocket );
1492cdf0e10cSrcweir
149363d99982SDamjan Jovanovic ASSERT_TRUE(equalSocket == sSocket) << " test for operators_equal function: check equal.";
1494cdf0e10cSrcweir }
1495cdf0e10cSrcweir
TEST_F(operators,operators_equal_002)149663d99982SDamjan Jovanovic TEST_F(operators, operators_equal_002)
1497cdf0e10cSrcweir {
1498cdf0e10cSrcweir ::osl::Socket sSocket( sHandle );
1499cdf0e10cSrcweir /// Socket copy constructor.
1500cdf0e10cSrcweir ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) );
1501cdf0e10cSrcweir
150263d99982SDamjan Jovanovic ASSERT_TRUE(!( equalSocket == sSocket )) << " test for operators_equal_002 function: check unequal.";
1503cdf0e10cSrcweir }
1504cdf0e10cSrcweir
1505cdf0e10cSrcweir /** testing the methods:
1506cdf0e10cSrcweir inline void SAL_CALL shutdown( oslSocketDirection Direction = osl_Socket_DirReadWrite );
1507cdf0e10cSrcweir inline void SAL_CALL close();
1508cdf0e10cSrcweir */
1509cdf0e10cSrcweir
151063d99982SDamjan Jovanovic class close : public ::testing::Test
1511cdf0e10cSrcweir {
1512cdf0e10cSrcweir public:
1513cdf0e10cSrcweir oslSocket sHandle;
1514cdf0e10cSrcweir // initialization
SetUp()151563d99982SDamjan Jovanovic void SetUp( )
1516cdf0e10cSrcweir {
1517cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1518cdf0e10cSrcweir }
1519cdf0e10cSrcweir
TearDown()152063d99982SDamjan Jovanovic void TearDown( )
1521cdf0e10cSrcweir {
1522cdf0e10cSrcweir sHandle = NULL;
1523cdf0e10cSrcweir }
152463d99982SDamjan Jovanovic }; // class close
1525cdf0e10cSrcweir
TEST_F(close,close_001)152663d99982SDamjan Jovanovic TEST_F(close, close_001)
1527cdf0e10cSrcweir {
1528cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
1529cdf0e10cSrcweir sSocket.close();
1530cdf0e10cSrcweir
153163d99982SDamjan Jovanovic ASSERT_TRUE(sSocket.getHandle() == sHandle) << "test for close_001 function: this function is reserved for test.";
1532cdf0e10cSrcweir }
1533cdf0e10cSrcweir
TEST_F(close,close_002)153463d99982SDamjan Jovanovic TEST_F(close, close_002)
1535cdf0e10cSrcweir {
153663d99982SDamjan Jovanovic // This blocks forever on FreeBSD
153763d99982SDamjan Jovanovic #if defined(LINUX)
1538cdf0e10cSrcweir ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1539cdf0e10cSrcweir AcceptorThread myAcceptorThread( asSocket, aHostIp1 );
1540cdf0e10cSrcweir myAcceptorThread.create();
1541cdf0e10cSrcweir
1542cdf0e10cSrcweir thread_sleep( 1 );
1543cdf0e10cSrcweir //when accepting, close the socket, the thread will not block for accepting
1544cdf0e10cSrcweir //man close:Any locks held on the file it was associated with, and owned by the process, are removed
1545cdf0e10cSrcweir asSocket.close();
1546cdf0e10cSrcweir //thread_sleep( 2 );
1547cdf0e10cSrcweir myAcceptorThread.join();
1548cdf0e10cSrcweir
154963d99982SDamjan Jovanovic ASSERT_TRUE(myAcceptorThread.isOK() == sal_True) << "test for close when is accepting: the socket will quit accepting status.";
155063d99982SDamjan Jovanovic #endif
1551cdf0e10cSrcweir }
1552cdf0e10cSrcweir
1553cdf0e10cSrcweir // to cover "if ( pSockAddrIn->sin_addr.s_addr == htonl(INADDR_ANY) )" in osl_closeSocket( )
TEST_F(close,close_003)155463d99982SDamjan Jovanovic TEST_F(close, close_003)
1555cdf0e10cSrcweir {
155663d99982SDamjan Jovanovic // This blocks forever on FreeBSD
155763d99982SDamjan Jovanovic #if defined(LINUX)
1558cdf0e10cSrcweir ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1559cdf0e10cSrcweir AcceptorThread myAcceptorThread( asSocket, aHostIpZero );
1560cdf0e10cSrcweir myAcceptorThread.create();
1561cdf0e10cSrcweir
1562cdf0e10cSrcweir thread_sleep( 1 );
1563cdf0e10cSrcweir asSocket.close();
1564cdf0e10cSrcweir myAcceptorThread.join();
1565cdf0e10cSrcweir
156663d99982SDamjan Jovanovic ASSERT_TRUE(myAcceptorThread.isOK() == sal_True) << "test for close when is accepting: the socket will quit accepting status.";
156763d99982SDamjan Jovanovic #endif
1568cdf0e10cSrcweir }
1569cdf0e10cSrcweir
1570cdf0e10cSrcweir /** testing the method:
1571cdf0e10cSrcweir inline void SAL_CALL getLocalAddr( SocketAddr &Addr ) const;
1572cdf0e10cSrcweir */
1573cdf0e10cSrcweir
157463d99982SDamjan Jovanovic class getLocalAddr : public ::testing::Test
1575cdf0e10cSrcweir {
1576cdf0e10cSrcweir public:
1577cdf0e10cSrcweir oslSocket sHandle;
1578cdf0e10cSrcweir // initialization
SetUp()157963d99982SDamjan Jovanovic void SetUp( )
1580cdf0e10cSrcweir {
1581cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1582cdf0e10cSrcweir }
1583cdf0e10cSrcweir
TearDown()158463d99982SDamjan Jovanovic void TearDown( )
1585cdf0e10cSrcweir {
1586cdf0e10cSrcweir sHandle = NULL;
1587cdf0e10cSrcweir }
158863d99982SDamjan Jovanovic }; // class getLocalAddr
1589cdf0e10cSrcweir
1590cdf0e10cSrcweir // get the Address of the local end of the socket
TEST_F(getLocalAddr,getLocalAddr_001)159163d99982SDamjan Jovanovic TEST_F(getLocalAddr, getLocalAddr_001)
1592cdf0e10cSrcweir {
1593cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
1594cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( aHostIp1, IP_PORT_MYPORT8 );
1595cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr;
1596cdf0e10cSrcweir
1597cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1598cdf0e10cSrcweir
1599cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
1600cdf0e10cSrcweir ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString();
160163d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << suError1.pData;
1602cdf0e10cSrcweir
1603cdf0e10cSrcweir sSocket.getLocalAddr( saLocalSocketAddr );
1604cdf0e10cSrcweir
1605cdf0e10cSrcweir sal_Bool bOK = compareUString( saLocalSocketAddr.getHostname( 0 ), sSocket.getLocalHost() ) ;
1606cdf0e10cSrcweir
160763d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << "test for getLocalAddr function: first create a new socket, then a socket address, bind them, and check the address.";
1608cdf0e10cSrcweir }
1609cdf0e10cSrcweir
1610cdf0e10cSrcweir
1611cdf0e10cSrcweir /** testing the method:
1612cdf0e10cSrcweir inline sal_Int32 SAL_CALL getLocalPort() const;
1613cdf0e10cSrcweir */
1614cdf0e10cSrcweir
161563d99982SDamjan Jovanovic class getLocalPort : public ::testing::Test
1616cdf0e10cSrcweir {
1617cdf0e10cSrcweir public:
1618cdf0e10cSrcweir oslSocket sHandle;
1619cdf0e10cSrcweir // initialization
SetUp()162063d99982SDamjan Jovanovic void SetUp( )
1621cdf0e10cSrcweir {
1622cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1623cdf0e10cSrcweir }
1624cdf0e10cSrcweir
TearDown()162563d99982SDamjan Jovanovic void TearDown( )
1626cdf0e10cSrcweir {
1627cdf0e10cSrcweir sHandle = NULL;
1628cdf0e10cSrcweir }
162963d99982SDamjan Jovanovic }; // class getLocalPort
1630cdf0e10cSrcweir
TEST_F(getLocalPort,getLocalPort_001)163163d99982SDamjan Jovanovic TEST_F(getLocalPort, getLocalPort_001)
1632cdf0e10cSrcweir {
1633cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
1634cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( aHostIp1, IP_PORT_MYPORT7 ); // aHostIp1 localhost
1635cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr;
1636cdf0e10cSrcweir
1637cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1638cdf0e10cSrcweir
1639cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
1640cdf0e10cSrcweir ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString();
164163d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << suError1.pData;
1642cdf0e10cSrcweir sal_Bool bOK = ( IP_PORT_MYPORT7 == sSocket.getLocalPort( ) );
1643cdf0e10cSrcweir
164463d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << "test for getLocalPort function: first create a new socket, then a socket address, bind them, and check the port.";
1645cdf0e10cSrcweir }
1646cdf0e10cSrcweir
1647cdf0e10cSrcweir /** test writer's comment:
1648cdf0e10cSrcweir
1649cdf0e10cSrcweir the invalid port number can not be set by giving invalid port number
1650cdf0e10cSrcweir such as 99999 or -1, it will convert to ( x mod 65535 ), so it will always be
1651cdf0e10cSrcweir valid, the only instance that the getLocalPort returns OSL_INVALID_PORT
1652cdf0e10cSrcweir is when saSocketAddr itself is an invalid one, that is , the IP or host name
1653cdf0e10cSrcweir can not be found, then the created socket address is not valid.
1654cdf0e10cSrcweir */
165563d99982SDamjan Jovanovic #if 0
165663d99982SDamjan Jovanovic TEST_F(getLocalPort, getLocalPort_002)
1657cdf0e10cSrcweir {
1658cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_TELNET);
1659cdf0e10cSrcweir #ifdef WNT
1660cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
1661cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
1662cdf0e10cSrcweir sSocket.bind( saBindSocketAddr );
1663cdf0e10cSrcweir //Invalid IP, so bind should fail
1664cdf0e10cSrcweir ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )),
1665cdf0e10cSrcweir ::rtl::OUString::valueOf((sal_Int32)OSL_INVALID_PORT),
1666cdf0e10cSrcweir "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned.");
1667cdf0e10cSrcweir sal_Bool bOK = ( OSL_INVALID_PORT == sSocket.getLocalPort( ) );
1668cdf0e10cSrcweir (void)bOK;
1669cdf0e10cSrcweir #else
1670cdf0e10cSrcweir //on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT
1671cdf0e10cSrcweir ::rtl::OUString suError = ::rtl::OUString::createFromAscii( "on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT, but can not create Addr of that case");
1672cdf0e10cSrcweir #endif
167363d99982SDamjan Jovanovic ASSERT_TRUE(sal_False) << suError;
1674cdf0e10cSrcweir
1675cdf0e10cSrcweir }
167663d99982SDamjan Jovanovic #endif
1677cdf0e10cSrcweir
TEST_F(getLocalPort,getLocalPort_003)167863d99982SDamjan Jovanovic TEST_F(getLocalPort, getLocalPort_003)
1679cdf0e10cSrcweir {
1680cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
1681cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_INVAL);
1682cdf0e10cSrcweir
1683cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1684cdf0e10cSrcweir
1685cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
1686cdf0e10cSrcweir ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString();
168763d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << suError1.pData;
1688cdf0e10cSrcweir ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )),
1689cdf0e10cSrcweir ::rtl::OUString::createFromAscii("34463"),
1690cdf0e10cSrcweir "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned");
1691cdf0e10cSrcweir sal_Bool bOK = ( sSocket.getLocalPort( ) >= 1 && sSocket.getLocalPort( ) <= 65535);
1692cdf0e10cSrcweir
169363d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << suError.pData;
1694cdf0e10cSrcweir }
1695cdf0e10cSrcweir
1696cdf0e10cSrcweir /** testing the method:
1697cdf0e10cSrcweir inline ::rtl::OUString SAL_CALL getLocalHost() const;
1698cdf0e10cSrcweir
1699cdf0e10cSrcweir Mindyliu: on Linux, at first it will check the binded in /etc/hosts, if it has the binded IP, it will return the hostname in it;
1700cdf0e10cSrcweir else if the binded IP is "127.0.0.1", it will return "localhost", if it's the machine's ethernet ip such as "129.158.217.90", it
1701cdf0e10cSrcweir will return hostname of current processor such as "aegean.PRC.Sun.COM"
1702cdf0e10cSrcweir */
1703cdf0e10cSrcweir
170463d99982SDamjan Jovanovic class getLocalHost : public ::testing::Test
1705cdf0e10cSrcweir {
1706cdf0e10cSrcweir public:
1707cdf0e10cSrcweir oslSocket sHandle;
1708cdf0e10cSrcweir // initialization
SetUp()170963d99982SDamjan Jovanovic void SetUp( )
1710cdf0e10cSrcweir {
1711cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1712cdf0e10cSrcweir }
1713cdf0e10cSrcweir
TearDown()171463d99982SDamjan Jovanovic void TearDown( )
1715cdf0e10cSrcweir {
1716cdf0e10cSrcweir sHandle = NULL;
1717cdf0e10cSrcweir }
171863d99982SDamjan Jovanovic }; // class getLocalHost
1719cdf0e10cSrcweir
TEST_F(getLocalHost,getLocalHost_001)172063d99982SDamjan Jovanovic TEST_F(getLocalHost, getLocalHost_001)
1721cdf0e10cSrcweir {
1722cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
1723cdf0e10cSrcweir //port number from IP_PORT_HTTP1 to IP_PORT_MYPORT6, mindyliu
1724cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( aHostIp1, IP_PORT_MYPORT6 );
1725cdf0e10cSrcweir
1726cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1727cdf0e10cSrcweir
1728cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
1729cdf0e10cSrcweir ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString();
173063d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << suError1.pData;
1731cdf0e10cSrcweir sal_Bool bOK;
1732cdf0e10cSrcweir ::rtl::OUString suError;
1733cdf0e10cSrcweir #ifdef WNT
1734cdf0e10cSrcweir bOK = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ;
1735cdf0e10cSrcweir suError = outputError(sSocket.getLocalHost( ), getThisHostname( ),
1736cdf0e10cSrcweir "test for getLocalHost function: create localhost socket and check name");
1737cdf0e10cSrcweir #else
1738cdf0e10cSrcweir ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) "localhost" );
1739cdf0e10cSrcweir sal_Bool bRes1, bRes2;
1740cdf0e10cSrcweir bRes1 = compareUString( sSocket.getLocalHost( ), aUString ) ;
1741cdf0e10cSrcweir bRes2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname(0) ) ;
1742cdf0e10cSrcweir bOK = bRes1 || bRes2;
1743cdf0e10cSrcweir suError = outputError(sSocket.getLocalHost( ), aUString, "test for getLocalHost function: create localhost socket and check name");
1744cdf0e10cSrcweir #endif
174563d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << suError.pData;
1746cdf0e10cSrcweir }
1747cdf0e10cSrcweir
TEST_F(getLocalHost,getLocalHost_002)174863d99982SDamjan Jovanovic TEST_F(getLocalHost, getLocalHost_002)
1749cdf0e10cSrcweir {
1750cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
1751cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_POP3);
1752cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr;
1753cdf0e10cSrcweir
1754cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1755cdf0e10cSrcweir sSocket.bind( saBindSocketAddr );
1756cdf0e10cSrcweir //Invalid IP, so bind should fail
1757cdf0e10cSrcweir sal_Bool bOK = compareUString( sSocket.getLocalHost( ), aNullURL ) ;
1758cdf0e10cSrcweir ::rtl::OUString suError = outputError(sSocket.getLocalHost( ), aNullURL, "test for getLocalHost function: getLocalHost with invalid SocketAddr");
1759cdf0e10cSrcweir
176063d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << suError.pData;
1761cdf0e10cSrcweir }
1762cdf0e10cSrcweir
1763cdf0e10cSrcweir /** testing the methods:
1764cdf0e10cSrcweir inline void SAL_CALL getPeerAddr( SocketAddr & Addr) const;
1765cdf0e10cSrcweir inline sal_Int32 SAL_CALL getPeerPort() const;
1766cdf0e10cSrcweir inline ::rtl::OUString SAL_CALL getPeerHost() const;
1767cdf0e10cSrcweir */
176863d99982SDamjan Jovanovic class getPeer : public ::testing::Test
1769cdf0e10cSrcweir {
1770cdf0e10cSrcweir public:
1771cdf0e10cSrcweir oslSocket sHandle;
1772cdf0e10cSrcweir TimeValue *pTimeout;
1773cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket;
1774cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket;
1775cdf0e10cSrcweir
1776cdf0e10cSrcweir
1777cdf0e10cSrcweir // initialization
SetUp()177863d99982SDamjan Jovanovic void SetUp( )
1779cdf0e10cSrcweir {
1780cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
1781cdf0e10cSrcweir pTimeout->Seconds = 3;
1782cdf0e10cSrcweir pTimeout->Nanosec = 0;
1783cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1784cdf0e10cSrcweir }
1785cdf0e10cSrcweir
TearDown()178663d99982SDamjan Jovanovic void TearDown( )
1787cdf0e10cSrcweir {
1788cdf0e10cSrcweir free( pTimeout );
1789cdf0e10cSrcweir sHandle = NULL;
1790cdf0e10cSrcweir asAcceptorSocket.close( );
1791cdf0e10cSrcweir csConnectorSocket.close( );
1792cdf0e10cSrcweir }
179363d99982SDamjan Jovanovic }; // class getPeer
1794cdf0e10cSrcweir
TEST_F(getPeer,getPeer_001)179563d99982SDamjan Jovanovic TEST_F(getPeer, getPeer_001)
1796cdf0e10cSrcweir {
1797cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT );
1798cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT );
1799cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP );
1800cdf0e10cSrcweir ::osl::StreamSocket ssConnection;
1801cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1802cdf0e10cSrcweir /// launch server socket
1803cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
180463d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind '127.0.0.1' address failed.";
1805cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
180663d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed.";
1807cdf0e10cSrcweir
1808cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True );
1809cdf0e10cSrcweir asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
1810cdf0e10cSrcweir
1811cdf0e10cSrcweir /// launch client socket
1812cdf0e10cSrcweir csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
1813cdf0e10cSrcweir
1814cdf0e10cSrcweir /// get peer information
1815cdf0e10cSrcweir csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected.
1816cdf0e10cSrcweir sal_Int32 peerPort = csConnectorSocket.getPeerPort( );
1817cdf0e10cSrcweir ::rtl::OUString peerHost = csConnectorSocket.getPeerHost( );
1818cdf0e10cSrcweir
181963d99982SDamjan Jovanovic ASSERT_TRUE(( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) )&&
1820cdf0e10cSrcweir ( sal_True == compareUString( peerHost, saLocalSocketAddr.getHostname( 0 ) ) ) &&
182163d99982SDamjan Jovanovic ( peerPort == saLocalSocketAddr.getPort( ) )) << "test for getPeer function: setup a connection and then get the peer address, port and host from client side.";
1822cdf0e10cSrcweir }
1823cdf0e10cSrcweir
1824cdf0e10cSrcweir /** testing the methods:
1825cdf0e10cSrcweir inline sal_Bool SAL_CALL bind(const SocketAddr& LocalInterface);
1826cdf0e10cSrcweir */
1827cdf0e10cSrcweir
1828cdf0e10cSrcweir
182963d99982SDamjan Jovanovic class bind : public ::testing::Test
1830cdf0e10cSrcweir {
1831cdf0e10cSrcweir public:
1832cdf0e10cSrcweir oslSocket sHandle;
1833cdf0e10cSrcweir // initialization
SetUp()183463d99982SDamjan Jovanovic void SetUp( )
1835cdf0e10cSrcweir {
1836cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1837cdf0e10cSrcweir }
1838cdf0e10cSrcweir
TearDown()183963d99982SDamjan Jovanovic void TearDown( )
1840cdf0e10cSrcweir {
1841cdf0e10cSrcweir sHandle = NULL;
1842cdf0e10cSrcweir }
184363d99982SDamjan Jovanovic }; // class bind
1844cdf0e10cSrcweir
TEST_F(bind,bind_001)184563d99982SDamjan Jovanovic TEST_F(bind, bind_001)
1846cdf0e10cSrcweir {
1847cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
1848cdf0e10cSrcweir //bind must use local IP address ---mindyliu
1849cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_MYPORT5 );
1850cdf0e10cSrcweir
1851cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1852cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
185363d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << "Socket bind fail.";
1854cdf0e10cSrcweir
1855cdf0e10cSrcweir sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname( ) ) ;
1856cdf0e10cSrcweir
1857cdf0e10cSrcweir sSocket.close();
185863d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK2) << "test for bind function: bind a valid address.";
1859cdf0e10cSrcweir }
1860cdf0e10cSrcweir
TEST_F(bind,bind_002)186163d99982SDamjan Jovanovic TEST_F(bind, bind_002)
1862cdf0e10cSrcweir {
1863cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
1864cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_NETBIOS );
1865cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr;
1866cdf0e10cSrcweir
1867cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1); // sal_True);
1868cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
1869cdf0e10cSrcweir sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ;
1870cdf0e10cSrcweir
187163d99982SDamjan Jovanovic ASSERT_TRUE(( sal_False == bOK1 ) && ( sal_False == bOK2 )) << "test for bind function: bind a valid address.";
1872cdf0e10cSrcweir }
1873cdf0e10cSrcweir
1874cdf0e10cSrcweir
1875cdf0e10cSrcweir /** testing the methods:
1876cdf0e10cSrcweir inline sal_Bool SAL_CALL isRecvReady(const TimeValue *pTimeout = 0) const;
1877cdf0e10cSrcweir
1878cdf0e10cSrcweir */
187963d99982SDamjan Jovanovic class isRecvReady : public ::testing::Test
1880cdf0e10cSrcweir {
1881cdf0e10cSrcweir public:
1882cdf0e10cSrcweir oslSocket sHandle;
1883cdf0e10cSrcweir TimeValue *pTimeout;
1884cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket;
1885cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket;
1886cdf0e10cSrcweir
1887cdf0e10cSrcweir
1888cdf0e10cSrcweir // initialization
SetUp()188963d99982SDamjan Jovanovic void SetUp( )
1890cdf0e10cSrcweir {
1891cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
1892cdf0e10cSrcweir pTimeout->Seconds = 3;
1893cdf0e10cSrcweir pTimeout->Nanosec = 0;
1894cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1895cdf0e10cSrcweir }
1896cdf0e10cSrcweir
TearDown()189763d99982SDamjan Jovanovic void TearDown( )
1898cdf0e10cSrcweir {
1899cdf0e10cSrcweir free( pTimeout );
1900cdf0e10cSrcweir sHandle = NULL;
1901cdf0e10cSrcweir asAcceptorSocket.close( );
1902cdf0e10cSrcweir csConnectorSocket.close( );
1903cdf0e10cSrcweir }
190463d99982SDamjan Jovanovic }; // class isRecvReady
1905cdf0e10cSrcweir
TEST_F(isRecvReady,isRecvReady_001)190663d99982SDamjan Jovanovic TEST_F(isRecvReady, isRecvReady_001)
1907cdf0e10cSrcweir {
1908cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT1 );
1909cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT1 );
1910cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP );
1911cdf0e10cSrcweir ::osl::StreamSocket ssConnection;
1912cdf0e10cSrcweir /// launch server socket
1913cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
1914cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
191563d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed.";
1916cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
191763d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed.";
1918cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True );
1919cdf0e10cSrcweir asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
1920cdf0e10cSrcweir
1921cdf0e10cSrcweir /// launch client socket
1922cdf0e10cSrcweir csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
1923cdf0e10cSrcweir
1924cdf0e10cSrcweir /// is receive ready?
1925cdf0e10cSrcweir sal_Bool bOK3 = asAcceptorSocket.isRecvReady( pTimeout );
1926cdf0e10cSrcweir
192763d99982SDamjan Jovanovic ASSERT_TRUE(( sal_True == bOK3 )) << "test for isRecvReady function: setup a connection and then check if it can transmit data.";
1928cdf0e10cSrcweir }
1929cdf0e10cSrcweir
1930cdf0e10cSrcweir /** testing the methods:
1931cdf0e10cSrcweir inline sal_Bool SAL_CALL isSendReady(const TimeValue *pTimeout = 0) const;
1932cdf0e10cSrcweir */
193363d99982SDamjan Jovanovic class isSendReady : public ::testing::Test
1934cdf0e10cSrcweir {
1935cdf0e10cSrcweir public:
1936cdf0e10cSrcweir oslSocket sHandle;
1937cdf0e10cSrcweir TimeValue *pTimeout;
1938cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket;
1939cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket;
1940cdf0e10cSrcweir
1941cdf0e10cSrcweir
1942cdf0e10cSrcweir // initialization
SetUp()194363d99982SDamjan Jovanovic void SetUp( )
1944cdf0e10cSrcweir {
1945cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
1946cdf0e10cSrcweir pTimeout->Seconds = 3;
1947cdf0e10cSrcweir pTimeout->Nanosec = 0;
1948cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1949cdf0e10cSrcweir }
1950cdf0e10cSrcweir
TearDown()195163d99982SDamjan Jovanovic void TearDown( )
1952cdf0e10cSrcweir {
1953cdf0e10cSrcweir free( pTimeout );
1954cdf0e10cSrcweir sHandle = NULL;
1955cdf0e10cSrcweir asAcceptorSocket.close( );
1956cdf0e10cSrcweir csConnectorSocket.close( );
1957cdf0e10cSrcweir }
195863d99982SDamjan Jovanovic }; // class isSendReady
1959cdf0e10cSrcweir
TEST_F(isSendReady,isSendReady_001)196063d99982SDamjan Jovanovic TEST_F(isSendReady, isSendReady_001)
1961cdf0e10cSrcweir {
1962cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT );
1963cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT );
1964cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP );
1965cdf0e10cSrcweir ::osl::StreamSocket ssConnection;
1966cdf0e10cSrcweir
1967cdf0e10cSrcweir /// launch server socket
1968cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1969cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
197063d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed.";
1971cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
197263d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed.";
1973cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True );
1974cdf0e10cSrcweir asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
1975cdf0e10cSrcweir
1976cdf0e10cSrcweir /// launch client socket
1977cdf0e10cSrcweir csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
1978cdf0e10cSrcweir
1979cdf0e10cSrcweir /// is send ready?
1980cdf0e10cSrcweir sal_Bool bOK3 = csConnectorSocket.isSendReady( pTimeout );
1981cdf0e10cSrcweir
198263d99982SDamjan Jovanovic ASSERT_TRUE(( sal_True == bOK3 )) << "test for isSendReady function: setup a connection and then check if it can transmit data.";
1983cdf0e10cSrcweir }
1984cdf0e10cSrcweir
1985cdf0e10cSrcweir /** testing the methods:
1986cdf0e10cSrcweir inline oslSocketType SAL_CALL getType() const;
1987cdf0e10cSrcweir
1988cdf0e10cSrcweir */
1989cdf0e10cSrcweir
199063d99982SDamjan Jovanovic class getType : public ::testing::Test
1991cdf0e10cSrcweir {
1992cdf0e10cSrcweir public:
1993cdf0e10cSrcweir oslSocket sHandle;
1994cdf0e10cSrcweir // initialization
SetUp()199563d99982SDamjan Jovanovic void SetUp( )
1996cdf0e10cSrcweir {
1997cdf0e10cSrcweir
1998cdf0e10cSrcweir }
1999cdf0e10cSrcweir
TearDown()200063d99982SDamjan Jovanovic void TearDown( )
2001cdf0e10cSrcweir {
2002cdf0e10cSrcweir sHandle = NULL;
2003cdf0e10cSrcweir }
200463d99982SDamjan Jovanovic }; // class getType
2005cdf0e10cSrcweir
TEST_F(getType,getType_001)200663d99982SDamjan Jovanovic TEST_F(getType, getType_001)
2007cdf0e10cSrcweir {
2008cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2009cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
2010cdf0e10cSrcweir
201163d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == sSocket.getType( )) << "test for getType function: get type of socket.";
2012cdf0e10cSrcweir }
2013cdf0e10cSrcweir
TEST_F(getType,getType_002)201463d99982SDamjan Jovanovic TEST_F(getType, getType_002)
2015cdf0e10cSrcweir {
2016cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
2017cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
2018cdf0e10cSrcweir
201963d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeDgram == sSocket.getType( )) << "test for getType function: get type of socket.";
2020cdf0e10cSrcweir }
2021cdf0e10cSrcweir
2022cdf0e10cSrcweir #ifdef UNX
2023cdf0e10cSrcweir // mindy: since on LINUX and SOLARIS, Raw type socket can not be created, so do not test getType() here
2024cdf0e10cSrcweir // mindy: and add one test case to test creating Raw type socket--> ctors_TypeRaw()
TEST_F(getType,getType_003)202563d99982SDamjan Jovanovic TEST_F(getType, getType_003)
2026cdf0e10cSrcweir {
202763d99982SDamjan Jovanovic ASSERT_TRUE(sal_True) << "test for getType function: get type of socket.this is not passed in (LINUX, SOLARIS), the osl_Socket_TypeRaw, type socket can not be created.";
2028cdf0e10cSrcweir }
2029cdf0e10cSrcweir #else
TEST_F(getType,getType_003)203063d99982SDamjan Jovanovic TEST_F(getType, getType_003)
2031cdf0e10cSrcweir {
2032cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
2033cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
2034cdf0e10cSrcweir
203563d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeRaw == sSocket.getType( )) << "test for getType function: get type of socket.";
2036cdf0e10cSrcweir }
2037cdf0e10cSrcweir #endif
2038cdf0e10cSrcweir
2039cdf0e10cSrcweir
2040cdf0e10cSrcweir /** testing the methods:
2041cdf0e10cSrcweir inline sal_Int32 SAL_CALL getOption(
2042cdf0e10cSrcweir oslSocketOption Option,
2043cdf0e10cSrcweir void* pBuffer,
2044cdf0e10cSrcweir sal_uInt32 BufferLen,
2045cdf0e10cSrcweir oslSocketOptionLevel Level= osl_Socket_LevelSocket) const;
2046cdf0e10cSrcweir
2047cdf0e10cSrcweir inline sal_Int32 getOption( oslSocketOption option ) const;
2048cdf0e10cSrcweir
2049cdf0e10cSrcweir */
2050cdf0e10cSrcweir
205163d99982SDamjan Jovanovic class getOption : public ::testing::Test
2052cdf0e10cSrcweir {
2053cdf0e10cSrcweir public:
2054cdf0e10cSrcweir oslSocket sHandle;
2055cdf0e10cSrcweir // initialization
SetUp()205663d99982SDamjan Jovanovic void SetUp( )
2057cdf0e10cSrcweir {
2058cdf0e10cSrcweir
2059cdf0e10cSrcweir }
2060cdf0e10cSrcweir
TearDown()206163d99982SDamjan Jovanovic void TearDown( )
2062cdf0e10cSrcweir {
2063cdf0e10cSrcweir sHandle = NULL;
2064cdf0e10cSrcweir }
206563d99982SDamjan Jovanovic }; // class getOption
2066cdf0e10cSrcweir
2067cdf0e10cSrcweir /** test writer's comment:
2068cdf0e10cSrcweir
2069cdf0e10cSrcweir in oslSocketOption, the osl_Socket_OptionType denote 1 as osl_Socket_TypeStream.
2070cdf0e10cSrcweir 2 as osl_Socket_TypeDgram, etc which is not mapping the oslSocketType enum. differ
2071cdf0e10cSrcweir in 1.
2072cdf0e10cSrcweir */
2073cdf0e10cSrcweir
TEST_F(getOption,getOption_001)207463d99982SDamjan Jovanovic TEST_F(getOption, getOption_001)
2075cdf0e10cSrcweir {
2076cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2077cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
2078cdf0e10cSrcweir sal_Int32 * pType = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
2079cdf0e10cSrcweir *pType = 0;
2080cdf0e10cSrcweir sSocket.getOption( osl_Socket_OptionType, pType, sizeof ( sal_Int32 ) );
2081cdf0e10cSrcweir sal_Bool bOK = ( SOCK_STREAM == *pType );
2082cdf0e10cSrcweir // there is a TypeMap(socket.c) which map osl_Socket_TypeStream to SOCK_STREAM on UNX, and SOCK_STREAM != osl_Socket_TypeStream
2083cdf0e10cSrcweir //sal_Bool bOK = ( TYPE_TO_NATIVE(osl_Socket_TypeStream) == *pType );
2084cdf0e10cSrcweir free( pType );
2085cdf0e10cSrcweir
208663d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << "test for getOption function: get type option of socket.";
2087cdf0e10cSrcweir }
2088cdf0e10cSrcweir
2089cdf0e10cSrcweir // getsockopt error
TEST_F(getOption,getOption_004)209063d99982SDamjan Jovanovic TEST_F(getOption, getOption_004)
2091cdf0e10cSrcweir {
2092cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
2093cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
2094cdf0e10cSrcweir
2095cdf0e10cSrcweir sal_Bool * pbDontRoute = ( sal_Bool * )malloc( sizeof ( sal_Bool ) );
2096cdf0e10cSrcweir sal_Int32 nRes = sSocket.getOption( osl_Socket_OptionInvalid, pbDontRoute, sizeof ( sal_Bool ) );
2097cdf0e10cSrcweir free( pbDontRoute );
2098cdf0e10cSrcweir
209963d99982SDamjan Jovanovic ASSERT_TRUE(nRes == -1) << "test for getOption function: get invalid option of socket, should return -1.";
2100cdf0e10cSrcweir }
2101cdf0e10cSrcweir
TEST_F(getOption,getOption_simple_001)210263d99982SDamjan Jovanovic TEST_F(getOption, getOption_simple_001)
2103cdf0e10cSrcweir {
2104cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
2105cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
2106cdf0e10cSrcweir
2107cdf0e10cSrcweir sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDontRoute ) );
2108cdf0e10cSrcweir
210963d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << "test for getOption function: get debug option of socket.";
2110cdf0e10cSrcweir }
2111cdf0e10cSrcweir
TEST_F(getOption,getOption_simple_002)211263d99982SDamjan Jovanovic TEST_F(getOption, getOption_simple_002)
2113cdf0e10cSrcweir {
2114cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
2115cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
2116cdf0e10cSrcweir
2117cdf0e10cSrcweir sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDebug ) );
2118cdf0e10cSrcweir
211963d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << "test for getOption function: get debug option of socket.";
2120cdf0e10cSrcweir }
2121cdf0e10cSrcweir
2122cdf0e10cSrcweir /** testing the methods:
2123cdf0e10cSrcweir inline sal_Bool SAL_CALL setOption( oslSocketOption Option,
2124cdf0e10cSrcweir void* pBuffer,
2125cdf0e10cSrcweir sal_uInt32 BufferLen,
2126cdf0e10cSrcweir oslSocketOptionLevel Level= osl_Socket_LevelSocket ) const;
2127cdf0e10cSrcweir */
2128cdf0e10cSrcweir
212963d99982SDamjan Jovanovic class setOption : public ::testing::Test
2130cdf0e10cSrcweir {
2131cdf0e10cSrcweir public:
2132cdf0e10cSrcweir TimeValue *pTimeout;
2133cdf0e10cSrcweir // LLA: maybe there is an error in the source,
2134cdf0e10cSrcweir // as long as I remember, if a derived class do not overload all ctors there is a problem.
2135cdf0e10cSrcweir
2136cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket;
2137cdf0e10cSrcweir
SetUp()213863d99982SDamjan Jovanovic void SetUp( )
2139cdf0e10cSrcweir {
2140cdf0e10cSrcweir
2141cdf0e10cSrcweir }
2142cdf0e10cSrcweir
TearDown()214363d99982SDamjan Jovanovic void TearDown( )
2144cdf0e10cSrcweir {
2145cdf0e10cSrcweir asAcceptorSocket.close( );
2146cdf0e10cSrcweir }
214763d99982SDamjan Jovanovic }; // class setOption
2148cdf0e10cSrcweir
2149cdf0e10cSrcweir // LLA:
2150cdf0e10cSrcweir // getSocketOption returns BufferLen, or -1 if something failed
2151cdf0e10cSrcweir
2152cdf0e10cSrcweir // setSocketOption returns sal_True, if option could stored
2153cdf0e10cSrcweir // else sal_False
2154cdf0e10cSrcweir
TEST_F(setOption,setOption_001)215563d99982SDamjan Jovanovic TEST_F(setOption, setOption_001)
2156cdf0e10cSrcweir {
2157cdf0e10cSrcweir /// set and get option.
2158cdf0e10cSrcweir int nBufferLen = sizeof ( sal_Int32);
2159cdf0e10cSrcweir // LLA: SO_DONTROUTE expect an integer boolean, what ever it is, it's not sal_Bool!
2160cdf0e10cSrcweir
2161cdf0e10cSrcweir sal_Int32 * pbDontRouteSet = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
2162cdf0e10cSrcweir *pbDontRouteSet = 1; // sal_True;
2163cdf0e10cSrcweir
2164cdf0e10cSrcweir sal_Int32 * pGetBuffer = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
2165cdf0e10cSrcweir *pGetBuffer = 0;
2166cdf0e10cSrcweir
2167cdf0e10cSrcweir // maybe asAcceptorSocket is not right initialized
2168cdf0e10cSrcweir sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, nBufferLen );
216963d99982SDamjan Jovanovic ASSERT_TRUE(( sal_True == b1 )) << "setOption function failed.";
2170cdf0e10cSrcweir sal_Int32 n2 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen );
217163d99982SDamjan Jovanovic ASSERT_TRUE(( n2 == nBufferLen )) << "getOption function failed.";
2172cdf0e10cSrcweir
2173cdf0e10cSrcweir // on Linux, the value of option is 1, on Solaris, it's 16, but it's not important the exact value,
2174cdf0e10cSrcweir // just judge it is zero or not!
2175cdf0e10cSrcweir sal_Bool bOK = ( 0 != *pGetBuffer );
217663d99982SDamjan Jovanovic printf("#setOption_001: getOption is %d \n", *pGetBuffer);
2177cdf0e10cSrcweir
2178cdf0e10cSrcweir // toggle check, set to 0
2179cdf0e10cSrcweir *pbDontRouteSet = 0;
2180cdf0e10cSrcweir
2181cdf0e10cSrcweir sal_Bool b3 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, sizeof ( sal_Int32 ) );
218263d99982SDamjan Jovanovic ASSERT_TRUE(( sal_True == b3 )) << "setOption function failed.";
2183cdf0e10cSrcweir sal_Int32 n4 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen );
218463d99982SDamjan Jovanovic ASSERT_TRUE(( n4 == nBufferLen )) << "getOption (DONTROUTE) function failed.";
2185cdf0e10cSrcweir
2186cdf0e10cSrcweir sal_Bool bOK2 = ( 0 == *pGetBuffer );
2187cdf0e10cSrcweir
218863d99982SDamjan Jovanovic printf("#setOption_001: getOption is %d \n", *pGetBuffer);
2189cdf0e10cSrcweir
2190cdf0e10cSrcweir // LLA: sal_Bool * pbDontTouteSet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) );
2191cdf0e10cSrcweir // LLA: *pbDontTouteSet = sal_True;
2192cdf0e10cSrcweir // LLA: sal_Bool * pbDontTouteGet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) );
2193cdf0e10cSrcweir // LLA: *pbDontTouteGet = sal_False;
2194cdf0e10cSrcweir // LLA: asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontTouteSet, sizeof ( sal_Bool ) );
2195cdf0e10cSrcweir // LLA: asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pbDontTouteGet, sizeof ( sal_Bool ) );
2196cdf0e10cSrcweir // LLA: ::rtl::OUString suError = outputError(::rtl::OUString::valueOf((sal_Int32)*pbDontTouteGet),
2197cdf0e10cSrcweir // LLA: ::rtl::OUString::valueOf((sal_Int32)*pbDontTouteSet),
2198cdf0e10cSrcweir // LLA: "test for setOption function: set osl_Socket_OptionDontRoute and then check");
2199cdf0e10cSrcweir // LLA:
2200cdf0e10cSrcweir // LLA: sal_Bool bOK = ( sal_True == *pbDontTouteGet );
2201cdf0e10cSrcweir // LLA: free( pbDontTouteSet );
2202cdf0e10cSrcweir // LLA: free( pbDontTouteGet );
2203cdf0e10cSrcweir
220463d99982SDamjan Jovanovic ASSERT_TRUE(( sal_True == bOK ) && (sal_True == bOK2)) << "test for setOption function: set option of a socket and then check.";
2205cdf0e10cSrcweir
2206cdf0e10cSrcweir free( pbDontRouteSet );
2207cdf0e10cSrcweir free( pGetBuffer );
220863d99982SDamjan Jovanovic // LLA: ASSERT_TRUE(sal_True == bOK) << suError;
2209cdf0e10cSrcweir }
2210cdf0e10cSrcweir
TEST_F(setOption,setOption_002)221163d99982SDamjan Jovanovic TEST_F(setOption, setOption_002)
2212cdf0e10cSrcweir {
2213cdf0e10cSrcweir /// set and get option.
2214cdf0e10cSrcweir
2215cdf0e10cSrcweir // sal_Int32 * pbLingerSet = ( sal_Int32 * )malloc( nBufferLen );
2216cdf0e10cSrcweir // *pbLingerSet = 7;
2217cdf0e10cSrcweir // sal_Int32 * pbLingerGet = ( sal_Int32 * )malloc( nBufferLen );
2218cdf0e10cSrcweir /* struct */linger aLingerSet;
2219cdf0e10cSrcweir sal_Int32 nBufferLen = sizeof( struct linger );
2220cdf0e10cSrcweir aLingerSet.l_onoff = 1;
2221cdf0e10cSrcweir aLingerSet.l_linger = 7;
2222cdf0e10cSrcweir
2223cdf0e10cSrcweir linger aLingerGet;
2224cdf0e10cSrcweir
2225cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen );
2226cdf0e10cSrcweir
2227cdf0e10cSrcweir sal_Int32 n1 = asAcceptorSocket.getOption( osl_Socket_OptionLinger, &aLingerGet, nBufferLen );
222863d99982SDamjan Jovanovic ASSERT_TRUE(( n1 == nBufferLen )) << "getOption (SO_LINGER) function failed.";
2229cdf0e10cSrcweir
223063d99982SDamjan Jovanovic //printf("#setOption_002: getOption is %d \n", aLingerGet.l_linger);
2231cdf0e10cSrcweir sal_Bool bOK = ( 7 == aLingerGet.l_linger );
223263d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK) << "test for setOption function: set option of a socket and then check. ";
2233cdf0e10cSrcweir
2234cdf0e10cSrcweir }
2235cdf0e10cSrcweir
TEST_F(setOption,setOption_003)223663d99982SDamjan Jovanovic TEST_F(setOption, setOption_003)
2237cdf0e10cSrcweir {
2238cdf0e10cSrcweir linger aLingerSet;
2239cdf0e10cSrcweir aLingerSet.l_onoff = 1;
2240cdf0e10cSrcweir aLingerSet.l_linger = 7;
2241cdf0e10cSrcweir
2242cdf0e10cSrcweir sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, 0 );
2243cdf0e10cSrcweir printUString( asAcceptorSocket.getErrorAsString() );
224463d99982SDamjan Jovanovic ASSERT_TRUE(( b1 == sal_False )) << "setOption (SO_LINGER) function failed for optlen is 0.";
2245cdf0e10cSrcweir }
2246cdf0e10cSrcweir
TEST_F(setOption,setOption_simple_001)224763d99982SDamjan Jovanovic TEST_F(setOption, setOption_simple_001)
2248cdf0e10cSrcweir {
2249cdf0e10cSrcweir /// set and get option.
2250cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, 1 ); //sal_True );
2251cdf0e10cSrcweir sal_Bool bOK = ( 0 != asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) );
2252cdf0e10cSrcweir
225363d99982SDamjan Jovanovic printf("setOption_simple_001(): getoption is %d \n", asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) );
225463d99982SDamjan Jovanovic ASSERT_TRUE(( sal_True == bOK )) << "test for setOption function: set option of a socket and then check.";
2255cdf0e10cSrcweir }
2256cdf0e10cSrcweir
TEST_F(setOption,setOption_simple_002)225763d99982SDamjan Jovanovic TEST_F(setOption, setOption_simple_002)
2258cdf0e10cSrcweir {
2259cdf0e10cSrcweir /// set and get option.
2260cdf0e10cSrcweir // LLA: this does not work, due to the fact that SO_LINGER is a structure
2261cdf0e10cSrcweir // LLA: asAcceptorSocket.setOption( osl_Socket_OptionLinger, 7 );
2262cdf0e10cSrcweir // LLA: sal_Bool bOK = ( 7 == asAcceptorSocket.getOption( osl_Socket_OptionLinger ) );
2263cdf0e10cSrcweir
226463d99982SDamjan Jovanovic // LLA: ASSERT_TRUE(// LLA: ( sal_True == bOK )) << "test for setOption function: set option of a socket and then check.";
2265cdf0e10cSrcweir }
2266cdf0e10cSrcweir
2267cdf0e10cSrcweir
2268cdf0e10cSrcweir /** testing the method:
2269cdf0e10cSrcweir inline sal_Bool SAL_CALL enableNonBlockingMode( sal_Bool bNonBlockingMode);
2270cdf0e10cSrcweir */
227163d99982SDamjan Jovanovic class enableNonBlockingMode : public ::testing::Test
2272cdf0e10cSrcweir {
2273cdf0e10cSrcweir public:
2274cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket;
227563d99982SDamjan Jovanovic }; // class enableNonBlockingMode
2276cdf0e10cSrcweir
TEST_F(enableNonBlockingMode,enableNonBlockingMode_001)227763d99982SDamjan Jovanovic TEST_F(enableNonBlockingMode, enableNonBlockingMode_001)
2278cdf0e10cSrcweir {
2279cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT );
2280cdf0e10cSrcweir ::osl::StreamSocket ssConnection;
2281cdf0e10cSrcweir
2282cdf0e10cSrcweir /// launch server socket
2283cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
2284cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
228563d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed.";
2286cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
228763d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed.";
2288cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True );
2289cdf0e10cSrcweir asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
2290cdf0e10cSrcweir
2291cdf0e10cSrcweir /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default.
2292cdf0e10cSrcweir sal_Bool bOK = sal_True;
2293cdf0e10cSrcweir asAcceptorSocket.close( );
2294cdf0e10cSrcweir
229563d99982SDamjan Jovanovic ASSERT_TRUE(( sal_True == bOK )) << "test for enableNonBlockingMode function: launch a server socket and make it non blocking. if it can pass the acceptConnection statement, it is non-blocking";
2296cdf0e10cSrcweir }
2297cdf0e10cSrcweir
2298cdf0e10cSrcweir /** testing the method:
2299cdf0e10cSrcweir inline sal_Bool SAL_CALL isNonBlockingMode() const;
2300cdf0e10cSrcweir */
230163d99982SDamjan Jovanovic class isNonBlockingMode : public ::testing::Test
2302cdf0e10cSrcweir {
2303cdf0e10cSrcweir public:
2304cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket;
230563d99982SDamjan Jovanovic }; // class isNonBlockingMode
2306cdf0e10cSrcweir
TEST_F(isNonBlockingMode,isNonBlockingMode_001)230763d99982SDamjan Jovanovic TEST_F(isNonBlockingMode, isNonBlockingMode_001)
2308cdf0e10cSrcweir {
2309cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT );
2310cdf0e10cSrcweir ::osl::StreamSocket ssConnection;
2311cdf0e10cSrcweir
2312cdf0e10cSrcweir /// launch server socket
2313cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
2314cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
231563d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed.";
2316cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
231763d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed.";
2318cdf0e10cSrcweir
2319cdf0e10cSrcweir sal_Bool bOK3 = asAcceptorSocket.isNonBlockingMode( );
2320cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True );
2321cdf0e10cSrcweir asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
2322cdf0e10cSrcweir
2323cdf0e10cSrcweir /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default.
2324cdf0e10cSrcweir sal_Bool bOK4 = asAcceptorSocket.isNonBlockingMode( );
2325cdf0e10cSrcweir asAcceptorSocket.close( );
2326cdf0e10cSrcweir
232763d99982SDamjan Jovanovic ASSERT_TRUE(( sal_False == bOK3 ) && ( sal_True == bOK4 )) << "test for isNonBlockingMode function: launch a server socket and make it non blocking. it is expected to change from blocking mode to non-blocking mode.";
2328cdf0e10cSrcweir }
2329cdf0e10cSrcweir
2330cdf0e10cSrcweir /** testing the method:
2331cdf0e10cSrcweir inline void SAL_CALL clearError() const;
2332cdf0e10cSrcweir */
233363d99982SDamjan Jovanovic class clearError : public ::testing::Test
2334cdf0e10cSrcweir {
2335cdf0e10cSrcweir public:
2336cdf0e10cSrcweir oslSocket sHandle;
2337cdf0e10cSrcweir // initialization
SetUp()233863d99982SDamjan Jovanovic void SetUp( )
2339cdf0e10cSrcweir {
2340cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2341cdf0e10cSrcweir }
2342cdf0e10cSrcweir
TearDown()234363d99982SDamjan Jovanovic void TearDown( )
2344cdf0e10cSrcweir {
2345cdf0e10cSrcweir sHandle = NULL;
2346cdf0e10cSrcweir }
234763d99982SDamjan Jovanovic }; // class clearError
2348cdf0e10cSrcweir
TEST_F(clearError,clearError_001)234963d99982SDamjan Jovanovic TEST_F(clearError, clearError_001)
2350cdf0e10cSrcweir {
2351cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
2352cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_HTTP2 );
2353cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr;
2354cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
2355cdf0e10cSrcweir sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail"
2356cdf0e10cSrcweir oslSocketError seBind = sSocket.getError( );
2357cdf0e10cSrcweir sSocket.clearError( );
2358cdf0e10cSrcweir
235963d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_E_None == sSocket.getError( ) && seBind != osl_Socket_E_None) << "test for clearError function: trick an error called sSocket.getError( ), and then clear the error states, check the result.";
2360cdf0e10cSrcweir }
2361cdf0e10cSrcweir
2362cdf0e10cSrcweir /** testing the methods:
2363cdf0e10cSrcweir inline oslSocketError getError() const;
2364cdf0e10cSrcweir inline ::rtl::OUString getErrorAsString( ) const;
2365cdf0e10cSrcweir */
236663d99982SDamjan Jovanovic class getError : public ::testing::Test
2367cdf0e10cSrcweir {
2368cdf0e10cSrcweir public:
2369cdf0e10cSrcweir oslSocket sHandle;
2370cdf0e10cSrcweir // initialization
SetUp()237163d99982SDamjan Jovanovic void SetUp( )
2372cdf0e10cSrcweir {
2373cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2374cdf0e10cSrcweir }
2375cdf0e10cSrcweir
TearDown()237663d99982SDamjan Jovanovic void TearDown( )
2377cdf0e10cSrcweir {
2378cdf0e10cSrcweir sHandle = NULL;
2379cdf0e10cSrcweir }
238063d99982SDamjan Jovanovic }; // class getError
2381cdf0e10cSrcweir
TEST_F(getError,getError_001)238263d99982SDamjan Jovanovic TEST_F(getError, getError_001)
2383cdf0e10cSrcweir {
2384cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
2385cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( aHostIp1, IP_PORT_FTP );
2386cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr;
2387cdf0e10cSrcweir
238863d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_E_None == sSocket.getError( )) << "test for getError function: should get no error.";
2389cdf0e10cSrcweir }
2390cdf0e10cSrcweir
TEST_F(getError,getError_002)239163d99982SDamjan Jovanovic TEST_F(getError, getError_002)
2392cdf0e10cSrcweir {
2393cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
2394cdf0e10cSrcweir ::osl::SocketAddr saBindSocketAddr( aHostIpInval, IP_PORT_FTP );
2395cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr;
2396cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
2397cdf0e10cSrcweir sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail"
2398cdf0e10cSrcweir //on Solaris, the error no is EACCES, but it has no mapped value, so getError() returned osl_Socket_E_InvalidError.
2399cdf0e10cSrcweir #if defined(SOLARIS)
240063d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_E_InvalidError == sSocket.getError( )) << "trick an error called sSocket.getError( ), check the getError result.Failed on Solaris, returned osl_Socket_E_InvalidError because no entry to map the errno EACCES. ";
2401cdf0e10cSrcweir #else
2402cdf0e10cSrcweir //while on Linux & Win32, the errno is EADDRNOTAVAIL, getError returned osl_Socket_E_AddrNotAvail.
2403cdf0e10cSrcweir
240463d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_E_AddrNotAvail == sSocket.getError( )) << "trick an error called sSocket.getError( ), check the getError result.Failed on Solaris, returned osl_Socket_E_InvalidError because no entry to map the errno EACCES. Passed on Linux & Win32";
2405cdf0e10cSrcweir #endif
2406cdf0e10cSrcweir }
2407cdf0e10cSrcweir
2408cdf0e10cSrcweir
2409cdf0e10cSrcweir /** testing the methods:
2410cdf0e10cSrcweir inline oslSocket getHandle() const;
2411cdf0e10cSrcweir */
2412cdf0e10cSrcweir
241363d99982SDamjan Jovanovic class getHandle : public ::testing::Test
2414cdf0e10cSrcweir {
2415cdf0e10cSrcweir public:
2416cdf0e10cSrcweir oslSocket sHandle;
2417cdf0e10cSrcweir // initialization
SetUp()241863d99982SDamjan Jovanovic void SetUp( )
2419cdf0e10cSrcweir {
2420cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2421cdf0e10cSrcweir }
2422cdf0e10cSrcweir
TearDown()242363d99982SDamjan Jovanovic void TearDown( )
2424cdf0e10cSrcweir {
2425cdf0e10cSrcweir sHandle = NULL;
2426cdf0e10cSrcweir }
242763d99982SDamjan Jovanovic }; // class getHandle
2428cdf0e10cSrcweir
242963d99982SDamjan Jovanovic
TEST_F(getHandle,getHandle_001)243063d99982SDamjan Jovanovic TEST_F(getHandle, getHandle_001)
2431cdf0e10cSrcweir {
2432cdf0e10cSrcweir ::osl::Socket sSocket(sHandle);
2433cdf0e10cSrcweir ::osl::Socket assignSocket = sSocket.getHandle();
2434cdf0e10cSrcweir
243563d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == assignSocket.getType( )) << "test for operators_assignment_handle function: test the assignment operator.";
2436cdf0e10cSrcweir }
2437cdf0e10cSrcweir
TEST_F(getHandle,getHandle_002)243863d99982SDamjan Jovanovic TEST_F(getHandle, getHandle_002)
2439cdf0e10cSrcweir {
2440cdf0e10cSrcweir ::osl::Socket sSocket( sHandle );
2441cdf0e10cSrcweir ::osl::Socket assignSocket ( sSocket.getHandle( ) );
2442cdf0e10cSrcweir
244363d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == assignSocket.getType( )) << "test for operators_assignment function: assignment operator";
2444cdf0e10cSrcweir }
2445cdf0e10cSrcweir
2446cdf0e10cSrcweir
2447cdf0e10cSrcweir } // namespace osl_Socket
2448cdf0e10cSrcweir
2449cdf0e10cSrcweir
2450cdf0e10cSrcweir
2451cdf0e10cSrcweir namespace osl_StreamSocket
2452cdf0e10cSrcweir {
2453cdf0e10cSrcweir
2454cdf0e10cSrcweir /** testing the methods:
2455cdf0e10cSrcweir inline StreamSocket(oslAddrFamily Family = osl_Socket_FamilyInet,
2456cdf0e10cSrcweir oslProtocol Protocol = osl_Socket_ProtocolIp,
2457cdf0e10cSrcweir oslSocketType Type = osl_Socket_TypeStream);
2458cdf0e10cSrcweir
2459cdf0e10cSrcweir inline StreamSocket( const StreamSocket & );
2460cdf0e10cSrcweir
2461cdf0e10cSrcweir inline StreamSocket( oslSocket Socket , __sal_NoAcquire noacquire );
2462cdf0e10cSrcweir
2463cdf0e10cSrcweir inline StreamSocket( oslSocket Socket );
2464cdf0e10cSrcweir */
2465cdf0e10cSrcweir
246663d99982SDamjan Jovanovic class OslStreamSocketCtors : public ::testing::Test
2467cdf0e10cSrcweir {
2468cdf0e10cSrcweir public:
2469cdf0e10cSrcweir oslSocket sHandle;
2470cdf0e10cSrcweir // initialization
SetUp()247163d99982SDamjan Jovanovic void SetUp( )
2472cdf0e10cSrcweir {
2473cdf0e10cSrcweir sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2474cdf0e10cSrcweir }
2475cdf0e10cSrcweir
TearDown()247663d99982SDamjan Jovanovic void TearDown( )
2477cdf0e10cSrcweir {
2478cdf0e10cSrcweir sHandle = NULL;
2479cdf0e10cSrcweir }
248063d99982SDamjan Jovanovic }; // class ctors
2481cdf0e10cSrcweir
TEST_F(OslStreamSocketCtors,ctors_none)248263d99982SDamjan Jovanovic TEST_F(OslStreamSocketCtors, ctors_none)
2483cdf0e10cSrcweir {
2484cdf0e10cSrcweir /// Socket constructor.
2485cdf0e10cSrcweir ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
2486cdf0e10cSrcweir
248763d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == ssSocket.getType( )) << "test for ctors_none constructor function: check if the stream socket was created successfully.";
2488cdf0e10cSrcweir }
2489cdf0e10cSrcweir
TEST_F(OslStreamSocketCtors,ctors_acquire)249063d99982SDamjan Jovanovic TEST_F(OslStreamSocketCtors, ctors_acquire)
2491cdf0e10cSrcweir {
2492cdf0e10cSrcweir /// Socket constructor.
2493cdf0e10cSrcweir ::osl::StreamSocket ssSocket( sHandle );
2494cdf0e10cSrcweir
249563d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == ssSocket.getType( )) << "test for ctors_acquire constructor function: check if the socket was created successfully";
2496cdf0e10cSrcweir }
2497cdf0e10cSrcweir
TEST_F(OslStreamSocketCtors,ctors_no_acquire)249863d99982SDamjan Jovanovic TEST_F(OslStreamSocketCtors, ctors_no_acquire)
2499cdf0e10cSrcweir {
2500cdf0e10cSrcweir /// Socket constructor.
2501cdf0e10cSrcweir ::osl::StreamSocket ssSocket( sHandle, SAL_NO_ACQUIRE );
2502cdf0e10cSrcweir
250363d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == ssSocket.getType( )) << " test for ctors_no_acquire constructor function: check if the socket was created successfully";
2504cdf0e10cSrcweir }
2505cdf0e10cSrcweir
TEST_F(OslStreamSocketCtors,ctors_copy_ctor)250663d99982SDamjan Jovanovic TEST_F(OslStreamSocketCtors, ctors_copy_ctor)
2507cdf0e10cSrcweir {
2508cdf0e10cSrcweir /// Socket constructor.
2509cdf0e10cSrcweir ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
2510cdf0e10cSrcweir /// Socket copy constructor.
2511cdf0e10cSrcweir ::osl::StreamSocket copySocket( ssSocket );
2512cdf0e10cSrcweir
251363d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == copySocket.getType( )) << " test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor";
2514cdf0e10cSrcweir }
2515cdf0e10cSrcweir
251663d99982SDamjan Jovanovic class send_recv: public ::testing::Test
2517cdf0e10cSrcweir {
2518cdf0e10cSrcweir public:
2519cdf0e10cSrcweir // initialization
SetUp()252063d99982SDamjan Jovanovic void SetUp( )
2521cdf0e10cSrcweir {
2522cdf0e10cSrcweir }
2523cdf0e10cSrcweir
TearDown()252463d99982SDamjan Jovanovic void TearDown( )
2525cdf0e10cSrcweir {
2526cdf0e10cSrcweir
2527cdf0e10cSrcweir }
2528cdf0e10cSrcweir
write_read(sal_Int32 _nBufferSize,int _nValue)2529cdf0e10cSrcweir void write_read(sal_Int32 _nBufferSize, int _nValue)
2530cdf0e10cSrcweir {
2531cdf0e10cSrcweir //client sent two strings, and server received, check the order and value
2532cdf0e10cSrcweir WriteSocketThread myServerThread(_nBufferSize, _nValue);
2533cdf0e10cSrcweir ReadSocketThread myClientThread(_nBufferSize, _nValue);
2534cdf0e10cSrcweir myServerThread.create( );
2535cdf0e10cSrcweir // thread_sleep( 1 );
2536cdf0e10cSrcweir myClientThread.create( );
2537cdf0e10cSrcweir
2538cdf0e10cSrcweir //wait until the thread terminate
2539cdf0e10cSrcweir myClientThread.join( );
2540cdf0e10cSrcweir myServerThread.join( );
2541cdf0e10cSrcweir
2542cdf0e10cSrcweir //Maximum Packet Size is ( ARPANET, MILNET = 1007 Ethernet (10Mb) = 1500
2543cdf0e10cSrcweir // Proteon PRONET = 2046), so here test read 4000 bytes
2544cdf0e10cSrcweir sal_Int32 nLength = myClientThread.getCount();
2545cdf0e10cSrcweir bool bIsOk = myClientThread.isOk(); // check if the values are right.
2546cdf0e10cSrcweir
254763d99982SDamjan Jovanovic printf("Length:=%d\n", nLength);
254863d99982SDamjan Jovanovic printf(" bIsOk:=%d\n", bIsOk);
2549cdf0e10cSrcweir
255063d99982SDamjan Jovanovic ASSERT_TRUE(nLength == _nBufferSize && bIsOk == true) << " test for write/read values with two threads: send data from server, check readed data in client.";
255163d99982SDamjan Jovanovic }
255263d99982SDamjan Jovanovic }; // class send_recv
255363d99982SDamjan Jovanovic
TEST_F(send_recv,send_recv1)255463d99982SDamjan Jovanovic TEST_F(send_recv, send_recv1)
255563d99982SDamjan Jovanovic {
255663d99982SDamjan Jovanovic //client sent two strings, and server received, check the order and value
255763d99982SDamjan Jovanovic ServerSocketThread myServerThread;
255863d99982SDamjan Jovanovic ClientSocketThread myClientThread;
255963d99982SDamjan Jovanovic myServerThread.create( );
256063d99982SDamjan Jovanovic myClientThread.create( );
256163d99982SDamjan Jovanovic
256263d99982SDamjan Jovanovic //wait until the thread terminate
256363d99982SDamjan Jovanovic myClientThread.join( );
256463d99982SDamjan Jovanovic myServerThread.join( );
256563d99982SDamjan Jovanovic sal_Char myStr[30] = "";
256663d99982SDamjan Jovanovic strcat( myStr, pTestString1 );
256763d99982SDamjan Jovanovic strcat( myStr, pTestString2 );
256863d99982SDamjan Jovanovic sal_Int32 nRes = strcmp( myServerThread.pReadBuffer, myStr );
256963d99982SDamjan Jovanovic ASSERT_TRUE(nRes == 0) << " test for send/recv with two threads: launch Server/Client threads, send data from client, check received data in Server thread.";
2570cdf0e10cSrcweir }
2571cdf0e10cSrcweir
257263d99982SDamjan Jovanovic // error when recv
TEST_F(send_recv,send_recv2)257363d99982SDamjan Jovanovic TEST_F(send_recv, send_recv2)
257463d99982SDamjan Jovanovic {
257563d99982SDamjan Jovanovic ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
257663d99982SDamjan Jovanovic ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9 );
257763d99982SDamjan Jovanovic ::osl::StreamSocket ssStreamConnection;
257863d99982SDamjan Jovanovic sal_Char pReadBuffer[30] = "";
257963d99982SDamjan Jovanovic
258063d99982SDamjan Jovanovic ClientSocketThread myClientThread;
258163d99982SDamjan Jovanovic myClientThread.create( );
258263d99982SDamjan Jovanovic
258363d99982SDamjan Jovanovic asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
258463d99982SDamjan Jovanovic
258563d99982SDamjan Jovanovic asAcceptorSocket.bind( saLocalSocketAddr );
258663d99982SDamjan Jovanovic asAcceptorSocket.listen( 1 );
258763d99982SDamjan Jovanovic asAcceptorSocket.enableNonBlockingMode( sal_True );
258863d99982SDamjan Jovanovic asAcceptorSocket.acceptConnection( ssStreamConnection );
258963d99982SDamjan Jovanovic sal_Int32 nReadNumber = ssStreamConnection.recv( pReadBuffer, 11 );
259063d99982SDamjan Jovanovic
259163d99982SDamjan Jovanovic myClientThread.join( ) ;
259263d99982SDamjan Jovanovic ssStreamConnection.close();
259363d99982SDamjan Jovanovic asAcceptorSocket.close();
259463d99982SDamjan Jovanovic ASSERT_TRUE(nReadNumber == -1) << " test for send/recv, recv error!";
259563d99982SDamjan Jovanovic }
259663d99982SDamjan Jovanovic
TEST_F(send_recv,write_read_001)259763d99982SDamjan Jovanovic TEST_F(send_recv, write_read_001)
2598cdf0e10cSrcweir {
2599cdf0e10cSrcweir write_read(50, 10);
2600cdf0e10cSrcweir }
TEST_F(send_recv,write_read_002)260163d99982SDamjan Jovanovic TEST_F(send_recv, write_read_002)
2602cdf0e10cSrcweir {
2603cdf0e10cSrcweir write_read(1024, 20);
2604cdf0e10cSrcweir }
TEST_F(send_recv,write_read_003)260563d99982SDamjan Jovanovic TEST_F(send_recv, write_read_003)
2606cdf0e10cSrcweir {
2607cdf0e10cSrcweir write_read(4000, 1);
2608cdf0e10cSrcweir }
TEST_F(send_recv,write_read_004)260963d99982SDamjan Jovanovic TEST_F(send_recv, write_read_004)
2610cdf0e10cSrcweir {
2611cdf0e10cSrcweir write_read(8192, 3);
2612cdf0e10cSrcweir }
2613cdf0e10cSrcweir
2614cdf0e10cSrcweir
2615cdf0e10cSrcweir class SendClientThread : public ClientSocketThread
2616cdf0e10cSrcweir {
2617cdf0e10cSrcweir protected:
2618cdf0e10cSrcweir
run()2619cdf0e10cSrcweir void SAL_CALL run( )
2620cdf0e10cSrcweir {
2621cdf0e10cSrcweir TimeValue *pTimeout;
2622cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
2623cdf0e10cSrcweir pTimeout->Seconds = 5;
2624cdf0e10cSrcweir pTimeout->Nanosec = 0;
2625cdf0e10cSrcweir
2626cdf0e10cSrcweir if ( osl_Socket_Ok == csConnectorSocket.connect( saTargetSocketAddr, pTimeout ))
2627cdf0e10cSrcweir {
2628cdf0e10cSrcweir sal_Int32 nWrite1 = csConnectorSocket.write( pTestString1, 11 ); // "test socket"
2629cdf0e10cSrcweir
2630cdf0e10cSrcweir sal_Int32 nWrite2 = csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 );
2631cdf0e10cSrcweir thread_sleep( 2 );
2632cdf0e10cSrcweir csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 );
263363d99982SDamjan Jovanovic printf("nWrite1 is %d, nWrite2 is %d\n", nWrite1, nWrite2 );
2634cdf0e10cSrcweir //thread_sleep( 1 );
2635cdf0e10cSrcweir }
2636cdf0e10cSrcweir else
263763d99982SDamjan Jovanovic printf("# SendClientThread: connect failed! \n");
2638cdf0e10cSrcweir
2639cdf0e10cSrcweir csConnectorSocket.close();
2640cdf0e10cSrcweir free( pTimeout );
2641cdf0e10cSrcweir }
2642cdf0e10cSrcweir
2643cdf0e10cSrcweir };
2644cdf0e10cSrcweir
264563d99982SDamjan Jovanovic class shutdown: public ::testing::Test
2646cdf0e10cSrcweir {
2647cdf0e10cSrcweir public:
2648cdf0e10cSrcweir // initialization
SetUp()264963d99982SDamjan Jovanovic void SetUp( )
2650cdf0e10cSrcweir {
2651cdf0e10cSrcweir }
2652cdf0e10cSrcweir
TearDown()265363d99982SDamjan Jovanovic void TearDown( )
2654cdf0e10cSrcweir {
2655cdf0e10cSrcweir
2656cdf0e10cSrcweir }
265763d99982SDamjan Jovanovic }; // class shutdown
2658cdf0e10cSrcweir
2659cdf0e10cSrcweir // similar to close_002
TEST_F(shutdown,shutdown_001)266063d99982SDamjan Jovanovic TEST_F(shutdown, shutdown_001)
2661cdf0e10cSrcweir {
2662cdf0e10cSrcweir #if defined(LINUX)
2663cdf0e10cSrcweir ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
2664cdf0e10cSrcweir AcceptorThread myAcceptorThread( asSocket, aHostIp1 );
2665cdf0e10cSrcweir myAcceptorThread.create();
2666cdf0e10cSrcweir
2667cdf0e10cSrcweir thread_sleep( 1 );
2668cdf0e10cSrcweir
2669cdf0e10cSrcweir //when accepting, shutdown the socket, the thread will not block for accepting
2670cdf0e10cSrcweir asSocket.shutdown();
2671cdf0e10cSrcweir myAcceptorThread.join();
2672cdf0e10cSrcweir
267363d99982SDamjan Jovanovic ASSERT_TRUE(myAcceptorThread.isOK( ) == sal_True) << "test for close when is accepting: the socket will quit accepting status.";
2674cdf0e10cSrcweir #endif
2675cdf0e10cSrcweir }
2676cdf0e10cSrcweir
TEST_F(shutdown,shutdown_002)267763d99982SDamjan Jovanovic TEST_F(shutdown, shutdown_002)
2678cdf0e10cSrcweir {
2679cdf0e10cSrcweir ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
2680cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9);
2681cdf0e10cSrcweir asSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
268263d99982SDamjan Jovanovic ASSERT_TRUE(asSocket.bind( saLocalSocketAddr ) == sal_True) << "shutdown_002: bind fail";
268363d99982SDamjan Jovanovic ASSERT_TRUE(asSocket.listen( 1 ) == sal_True) << "shutdown_002: listen fail";
2684cdf0e10cSrcweir sal_Char pReadBuffer[40];
2685cdf0e10cSrcweir SendClientThread mySendThread;
2686cdf0e10cSrcweir mySendThread.create();
2687cdf0e10cSrcweir
2688cdf0e10cSrcweir asSocket.enableNonBlockingMode( sal_False );
2689cdf0e10cSrcweir ::osl::StreamSocket ssConnectionSocket;
2690cdf0e10cSrcweir oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket );
269163d99982SDamjan Jovanovic ASSERT_TRUE(eResult == osl_Socket_Ok) << "shutdown_002: acceptConnection fail";
2692cdf0e10cSrcweir
269386e1cf34SPedro Giffuni /* set socket option SO_LINGER 0, so close immediately */
2694cdf0e10cSrcweir linger aLingerSet;
2695cdf0e10cSrcweir sal_Int32 nBufferLen = sizeof( struct linger );
2696cdf0e10cSrcweir aLingerSet.l_onoff = 0;
2697cdf0e10cSrcweir aLingerSet.l_linger = 0;
2698cdf0e10cSrcweir
2699cdf0e10cSrcweir ssConnectionSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen );
2700cdf0e10cSrcweir thread_sleep( 1 );
2701cdf0e10cSrcweir //sal_uInt32 nRecv1 = 0;
2702cdf0e10cSrcweir sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 );
2703cdf0e10cSrcweir
2704cdf0e10cSrcweir //shutdown read after client the first send complete
2705cdf0e10cSrcweir ssConnectionSocket.shutdown( osl_Socket_DirRead );
2706cdf0e10cSrcweir
2707cdf0e10cSrcweir sal_Int32 nRead2 = ssConnectionSocket.read( pReadBuffer + nRead1, 12 );
2708cdf0e10cSrcweir sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 + nRead2, 12 );
270963d99982SDamjan Jovanovic printf("after read 2, nRead1 is %d, nRead2 is %d, nRead3 is %d \n", nRead1, nRead2, nRead3 );
2710cdf0e10cSrcweir mySendThread.join();
2711cdf0e10cSrcweir
2712cdf0e10cSrcweir ssConnectionSocket.close();
2713cdf0e10cSrcweir asSocket.close();
2714cdf0e10cSrcweir
2715cdf0e10cSrcweir /* on Linux, if send is before shutdown(DirRead), can read, nRecv2 still > 0,
2716cdf0e10cSrcweir http://dbforums.com/arch/186/2002/12/586417
2717cdf0e10cSrcweir While on Solaris, after shutdown(DirRead), all read will return 0
2718cdf0e10cSrcweir */
2719cdf0e10cSrcweir #ifdef LINUX
272063d99982SDamjan Jovanovic ASSERT_TRUE(nRead1 > 0 && nRead3 == 0) << "test for shutdown read direction: the socket can not read(recv).";
2721cdf0e10cSrcweir #else
272263d99982SDamjan Jovanovic ASSERT_TRUE(nRead1 > 0 && nRead2 == 0 && nRead3 == 0) << "test for shutdown read direction: the socket can not read(recv).";
2723cdf0e10cSrcweir #endif
2724cdf0e10cSrcweir
2725cdf0e10cSrcweir }
2726cdf0e10cSrcweir
TEST_F(shutdown,shutdown_003)272763d99982SDamjan Jovanovic TEST_F(shutdown, shutdown_003)
2728cdf0e10cSrcweir {
2729cdf0e10cSrcweir ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
2730cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9);
2731cdf0e10cSrcweir asSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
273263d99982SDamjan Jovanovic ASSERT_TRUE(asSocket.bind( saLocalSocketAddr ) == sal_True) << "shutdown_002: bind fail";
273363d99982SDamjan Jovanovic ASSERT_TRUE(asSocket.listen( 1 ) == sal_True) << "shutdown_002: listen fail";
2734cdf0e10cSrcweir sal_Char pReadBuffer[40];
2735cdf0e10cSrcweir SendClientThread mySendThread;
2736cdf0e10cSrcweir mySendThread.create();
2737cdf0e10cSrcweir
2738cdf0e10cSrcweir asSocket.enableNonBlockingMode( sal_False );
2739cdf0e10cSrcweir ::osl::StreamSocket ssConnectionSocket;
2740cdf0e10cSrcweir oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket );
274163d99982SDamjan Jovanovic ASSERT_TRUE(eResult == osl_Socket_Ok) << "shutdown_002: acceptConnection fail";
2742cdf0e10cSrcweir
2743cdf0e10cSrcweir thread_sleep( 1 );
2744cdf0e10cSrcweir //shutdown write after client the first send complete
2745cdf0e10cSrcweir ssConnectionSocket.shutdown( osl_Socket_DirWrite );
2746cdf0e10cSrcweir
2747cdf0e10cSrcweir // recv should not shutdown
2748cdf0e10cSrcweir sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 );
2749cdf0e10cSrcweir
2750cdf0e10cSrcweir sal_Int32 nWrite = ssConnectionSocket.write( pReadBuffer, 11 );
2751cdf0e10cSrcweir // still can read
2752cdf0e10cSrcweir sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 , 12 );
275363d99982SDamjan Jovanovic printf("after read 2, nRead1 is %d, nWrite is %d, nRead3 is %d\n", nRead1, nWrite, nRead3 );
2754cdf0e10cSrcweir mySendThread.join();
2755cdf0e10cSrcweir ssConnectionSocket.close();
2756cdf0e10cSrcweir asSocket.close();
2757cdf0e10cSrcweir
275863d99982SDamjan Jovanovic ASSERT_TRUE(nRead1 > 0 && nWrite == 0 && nRead3 > 0) << "test for shutdown read direction: the socket can not send(write).";
2759cdf0e10cSrcweir
2760cdf0e10cSrcweir }
2761cdf0e10cSrcweir
276263d99982SDamjan Jovanovic class isExceptionPending: public ::testing::Test
2763cdf0e10cSrcweir {
2764cdf0e10cSrcweir public:
276563d99982SDamjan Jovanovic }; // class isExceptionPending
276663d99982SDamjan Jovanovic
276763d99982SDamjan Jovanovic /**tester's comments: lack of a case that return sal_True, do not know when it will return sal_True*/
TEST_F(isExceptionPending,isExPending_001)276863d99982SDamjan Jovanovic TEST_F(isExceptionPending, isExPending_001)
2769cdf0e10cSrcweir {
2770cdf0e10cSrcweir ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
2771cdf0e10cSrcweir TimeValue *pTimeout;
2772cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
2773cdf0e10cSrcweir pTimeout->Seconds = 3;
2774cdf0e10cSrcweir pTimeout->Nanosec = 0;
2775cdf0e10cSrcweir sal_Bool bOk = asSocket.isExceptionPending( pTimeout );
2776cdf0e10cSrcweir free( pTimeout );
2777cdf0e10cSrcweir
277863d99982SDamjan Jovanovic ASSERT_TRUE(bOk == sal_False) << "test for isExceptionPending.";
2779cdf0e10cSrcweir }
2780cdf0e10cSrcweir
2781cdf0e10cSrcweir
2782cdf0e10cSrcweir } // namespace osl_StreamSocket
2783cdf0e10cSrcweir
2784cdf0e10cSrcweir
2785cdf0e10cSrcweir namespace osl_ConnectorSocket
2786cdf0e10cSrcweir {
2787cdf0e10cSrcweir
2788cdf0e10cSrcweir /** testing the method:
2789cdf0e10cSrcweir ConnectorSocket(oslAddrFamily Family = osl_Socket_FamilyInet,
2790cdf0e10cSrcweir oslProtocol Protocol = osl_Socket_ProtocolIp,
2791cdf0e10cSrcweir oslSocketType Type = osl_Socket_TypeStream);
2792cdf0e10cSrcweir */
2793cdf0e10cSrcweir
279463d99982SDamjan Jovanovic class OslConnectorSocketCtors : public ::testing::Test
2795cdf0e10cSrcweir {
2796cdf0e10cSrcweir public:
279763d99982SDamjan Jovanovic }; // class ctors
279863d99982SDamjan Jovanovic
TEST_F(OslConnectorSocketCtors,ctors_001)279963d99982SDamjan Jovanovic TEST_F(OslConnectorSocketCtors, ctors_001)
2800cdf0e10cSrcweir {
2801cdf0e10cSrcweir /// Socket constructor.
2802cdf0e10cSrcweir ::osl::ConnectorSocket csSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
2803cdf0e10cSrcweir
280463d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == csSocket.getType( )) << "test for ctors_001 constructor function: check if the connector socket was created successfully.";
2805cdf0e10cSrcweir }
2806cdf0e10cSrcweir
2807cdf0e10cSrcweir /** testing the method:
2808cdf0e10cSrcweir oslSocketResult SAL_CALL connect(const SocketAddr& TargetHost, const TimeValue* pTimeout = 0);
2809cdf0e10cSrcweir */
2810cdf0e10cSrcweir
281163d99982SDamjan Jovanovic class connect : public ::testing::Test
2812cdf0e10cSrcweir {
2813cdf0e10cSrcweir public:
2814cdf0e10cSrcweir TimeValue *pTimeout;
2815cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket;
2816cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket;
2817cdf0e10cSrcweir
2818cdf0e10cSrcweir
2819cdf0e10cSrcweir // initialization
SetUp()282063d99982SDamjan Jovanovic void SetUp( )
2821cdf0e10cSrcweir {
2822cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
2823cdf0e10cSrcweir pTimeout->Seconds = 3;
2824cdf0e10cSrcweir pTimeout->Nanosec = 0;
2825cdf0e10cSrcweir // sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
2826cdf0e10cSrcweir }
2827cdf0e10cSrcweir
TearDown()282863d99982SDamjan Jovanovic void TearDown( )
2829cdf0e10cSrcweir {
2830cdf0e10cSrcweir free( pTimeout );
2831cdf0e10cSrcweir // sHandle = NULL;
2832cdf0e10cSrcweir asAcceptorSocket.close( );
2833cdf0e10cSrcweir csConnectorSocket.close( );
2834cdf0e10cSrcweir }
283563d99982SDamjan Jovanovic }; // class connect
2836cdf0e10cSrcweir
TEST_F(connect,connect_001)283763d99982SDamjan Jovanovic TEST_F(connect, connect_001)
2838cdf0e10cSrcweir {
2839cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT2 );
2840cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT2 );
2841cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP );
2842cdf0e10cSrcweir ::osl::StreamSocket ssConnection;
2843cdf0e10cSrcweir
2844cdf0e10cSrcweir /// launch server socket
2845cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
2846cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
284763d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed.";
2848cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
284963d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed.";
2850cdf0e10cSrcweir
2851cdf0e10cSrcweir //asAcceptorSocket.enableNonBlockingMode( sal_True );
2852cdf0e10cSrcweir //oslSocketResult eResultAccept = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
285363d99982SDamjan Jovanovic //ASSERT_TRUE(osl_Socket_Ok == eResultAccept) << "accept failed.";
2854cdf0e10cSrcweir /// launch client socket
2855cdf0e10cSrcweir oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
285663d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_Ok == eResult) << "connect failed.";
2857cdf0e10cSrcweir
2858cdf0e10cSrcweir /// get peer information
2859cdf0e10cSrcweir csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected.
2860cdf0e10cSrcweir
286163d99982SDamjan Jovanovic ASSERT_TRUE(( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) &&
286263d99982SDamjan Jovanovic ( osl_Socket_Ok == eResult )) << "test for connect function: try to create a connection with remote host. and check the setup address.";
2863cdf0e10cSrcweir }
2864cdf0e10cSrcweir //non-blocking mode connect?
TEST_F(connect,connect_002)286563d99982SDamjan Jovanovic TEST_F(connect, connect_002)
2866cdf0e10cSrcweir {
2867cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT3 );
2868cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT3 );
2869cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP );
2870cdf0e10cSrcweir
2871cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
2872cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True );
2873cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
287463d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed.";
2875cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
287663d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed.";
2877cdf0e10cSrcweir
2878cdf0e10cSrcweir csConnectorSocket.enableNonBlockingMode( sal_True );
2879cdf0e10cSrcweir
2880cdf0e10cSrcweir oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
288163d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_InProgress == eResult || osl_Socket_Ok == eResult) << "connect failed.";
2882cdf0e10cSrcweir
2883cdf0e10cSrcweir /// get peer information
2884cdf0e10cSrcweir csConnectorSocket.getPeerAddr( saPeerSocketAddr );
2885cdf0e10cSrcweir
288663d99982SDamjan Jovanovic ASSERT_TRUE( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) )
288763d99982SDamjan Jovanovic << "test for connect function: try to create a connection with remote host. and check the setup address.";
2888cdf0e10cSrcweir }
2889cdf0e10cSrcweir // really an error or just delayed
289086e1cf34SPedro Giffuni // how to design scenarios that will return osl_Socket_Interrupted, osl_Socket_TimedOut
TEST_F(connect,connect_003)289163d99982SDamjan Jovanovic TEST_F(connect, connect_003)
2892cdf0e10cSrcweir {
2893cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr1( aHostIp1, IP_PORT_MYPORT3 );
2894cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr2( aHostIpInval1, IP_PORT_MYPORT3 );
2895cdf0e10cSrcweir
2896cdf0e10cSrcweir csConnectorSocket.enableNonBlockingMode( sal_False );
2897cdf0e10cSrcweir
2898cdf0e10cSrcweir oslSocketResult eResult1 = csConnectorSocket.connect( saTargetSocketAddr1, pTimeout );
2899cdf0e10cSrcweir oslSocketResult eResult2 = csConnectorSocket.connect( saTargetSocketAddr2, pTimeout );
2900cdf0e10cSrcweir CloseSocketThread myCloseThread( csConnectorSocket );
2901cdf0e10cSrcweir oslSocketResult eResult3 = csConnectorSocket.connect( saTargetSocketAddr2, pTimeout );
2902cdf0e10cSrcweir myCloseThread.join();
290363d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_Error == eResult1 &&
290463d99982SDamjan Jovanovic osl_Socket_Error == eResult2 && osl_Socket_Error == eResult3) << "connect should failed.";
2905cdf0e10cSrcweir
2906cdf0e10cSrcweir }
2907cdf0e10cSrcweir
2908cdf0e10cSrcweir // really an error in non-blocking mode
TEST_F(connect,connect_004)290963d99982SDamjan Jovanovic TEST_F(connect, connect_004)
2910cdf0e10cSrcweir {
2911cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( aHostIpInval1, IP_PORT_MYPORT3 );
2912cdf0e10cSrcweir
2913cdf0e10cSrcweir csConnectorSocket.enableNonBlockingMode( sal_True );
2914cdf0e10cSrcweir
2915cdf0e10cSrcweir oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
291663d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_Error == eResult) << "connect should failed.";
2917cdf0e10cSrcweir }
2918cdf0e10cSrcweir /** here need a case: immediate connection, say in non-blocking mode connect return osl_Socket_Ok
2919cdf0e10cSrcweir */
2920cdf0e10cSrcweir
2921cdf0e10cSrcweir
2922cdf0e10cSrcweir } // namespace osl_ConnectorSocket
2923cdf0e10cSrcweir
2924cdf0e10cSrcweir
2925cdf0e10cSrcweir
2926cdf0e10cSrcweir namespace osl_AcceptorSocket
2927cdf0e10cSrcweir {
2928cdf0e10cSrcweir
2929cdf0e10cSrcweir /** testing the methods:
2930cdf0e10cSrcweir inline AcceptorSocket(oslAddrFamily Family = osl_Socket_FamilyInet,
2931cdf0e10cSrcweir oslProtocol Protocol = osl_Socket_ProtocolIp,
2932cdf0e10cSrcweir oslSocketType Type = osl_Socket_TypeStream);
2933cdf0e10cSrcweir */
2934cdf0e10cSrcweir
293563d99982SDamjan Jovanovic class OslAcceptorSocketCtors : public ::testing::Test
2936cdf0e10cSrcweir {
2937cdf0e10cSrcweir public:
293863d99982SDamjan Jovanovic }; // class ctors
2939cdf0e10cSrcweir
TEST_F(OslAcceptorSocketCtors,ctors_001)294063d99982SDamjan Jovanovic TEST_F(OslAcceptorSocketCtors, ctors_001)
2941cdf0e10cSrcweir {
2942cdf0e10cSrcweir /// Socket constructor.
2943cdf0e10cSrcweir ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
2944cdf0e10cSrcweir
294563d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeStream == asSocket.getType( )) << "test for ctors_001 constructor function: check if the acceptor socket was created successfully.";
2946cdf0e10cSrcweir }
2947cdf0e10cSrcweir
2948cdf0e10cSrcweir #if 0
294963d99982SDamjan Jovanovic class operator_assign : public ::testing::Test
2950cdf0e10cSrcweir {
2951cdf0e10cSrcweir public:
295263d99982SDamjan Jovanovic }; // class operator_assign
2953cdf0e10cSrcweir
295463d99982SDamjan Jovanovic TEST_F(operator_assign, assign_001)
2955cdf0e10cSrcweir {
2956cdf0e10cSrcweir #if defined(LINUX)
2957cdf0e10cSrcweir ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
2958cdf0e10cSrcweir ::osl::AcceptorSocket asSocketAssign( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
2959cdf0e10cSrcweir asSocket.setOption( osl_Socket_OptionReuseAddr, 1);
2960cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( aHostIp1, IP_PORT_MYPORT4 );
2961cdf0e10cSrcweir asSocket.bind( saSocketAddr );
2962cdf0e10cSrcweir
2963cdf0e10cSrcweir AcceptorThread myAcceptorThread( asSocketAssign, aHostIp1 );
2964cdf0e10cSrcweir myAcceptorThread.create();
2965cdf0e10cSrcweir
2966cdf0e10cSrcweir thread_sleep( 1 );
2967cdf0e10cSrcweir //when accepting, assign another socket to the socket, the thread will not be closed, so is blocking
2968cdf0e10cSrcweir asSocketAssign = asSocket;
2969cdf0e10cSrcweir
297063d99982SDamjan Jovanovic printf("#asSocketAssign port number is %d\n", asSocketAssign.getLocalPort() );
2971cdf0e10cSrcweir
2972cdf0e10cSrcweir asSocketAssign.shutdown();
2973cdf0e10cSrcweir myAcceptorThread.join();
2974cdf0e10cSrcweir
297563d99982SDamjan Jovanovic ASSERT_TRUE(myAcceptorThread.isOK() == sal_True) << "test for close when is accepting: the socket will quit accepting status.";
2976cdf0e10cSrcweir
2977cdf0e10cSrcweir
2978cdf0e10cSrcweir #endif /* LINUX */
2979cdf0e10cSrcweir }
2980cdf0e10cSrcweir #endif
2981cdf0e10cSrcweir
2982cdf0e10cSrcweir /** testing the method:
2983cdf0e10cSrcweir inline sal_Bool SAL_CALL listen(sal_Int32 MaxPendingConnections= -1);
2984cdf0e10cSrcweir inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection);
2985cdf0e10cSrcweir inline oslSocketResult SAL_CALL acceptConnection( StreamSocket& Connection, SocketAddr & PeerAddr);
2986cdf0e10cSrcweir */
2987cdf0e10cSrcweir
298863d99982SDamjan Jovanovic class listen_accept : public ::testing::Test
2989cdf0e10cSrcweir {
2990cdf0e10cSrcweir public:
2991cdf0e10cSrcweir TimeValue *pTimeout;
2992cdf0e10cSrcweir ::osl::AcceptorSocket asAcceptorSocket;
2993cdf0e10cSrcweir ::osl::ConnectorSocket csConnectorSocket;
2994cdf0e10cSrcweir
2995cdf0e10cSrcweir
2996cdf0e10cSrcweir // initialization
SetUp()299763d99982SDamjan Jovanovic void SetUp( )
2998cdf0e10cSrcweir {
2999cdf0e10cSrcweir pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
3000cdf0e10cSrcweir pTimeout->Seconds = 3;
3001cdf0e10cSrcweir pTimeout->Nanosec = 0;
3002cdf0e10cSrcweir asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1);
3003cdf0e10cSrcweir // sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
3004cdf0e10cSrcweir }
3005cdf0e10cSrcweir
TearDown()300663d99982SDamjan Jovanovic void TearDown( )
3007cdf0e10cSrcweir {
3008cdf0e10cSrcweir free( pTimeout );
3009cdf0e10cSrcweir // sHandle = NULL;
3010cdf0e10cSrcweir asAcceptorSocket.close( );
3011cdf0e10cSrcweir csConnectorSocket.close( );
3012cdf0e10cSrcweir }
301363d99982SDamjan Jovanovic }; // class listen_accept
3014cdf0e10cSrcweir
TEST_F(listen_accept,listen_accept_001)301563d99982SDamjan Jovanovic TEST_F(listen_accept, listen_accept_001)
3016cdf0e10cSrcweir {
3017cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT3 );
3018cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT3 );
3019cdf0e10cSrcweir ::osl::StreamSocket ssConnection;
3020cdf0e10cSrcweir
3021cdf0e10cSrcweir /// launch server socket
3022cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
302363d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed.";
3024cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
302563d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed.";
3026cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True );
3027cdf0e10cSrcweir
3028cdf0e10cSrcweir /// launch client socket
3029cdf0e10cSrcweir csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
3030cdf0e10cSrcweir
3031cdf0e10cSrcweir oslSocketResult eResult = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
3032cdf0e10cSrcweir
303363d99982SDamjan Jovanovic ASSERT_TRUE(( osl_Socket_Ok == eResult )) << "test for listen_accept function: try to create a connection with remote host, using listen and accept.";
3034cdf0e10cSrcweir }
3035cdf0e10cSrcweir
TEST_F(listen_accept,listen_accept_002)303663d99982SDamjan Jovanovic TEST_F(listen_accept, listen_accept_002)
3037cdf0e10cSrcweir {
3038cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT4 );
3039cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT4 );
3040cdf0e10cSrcweir ::osl::SocketAddr saPeerSocketAddr( aHostIp2, IP_PORT_FTP );
3041cdf0e10cSrcweir ::osl::StreamSocket ssConnection;
3042cdf0e10cSrcweir
3043cdf0e10cSrcweir /// launch server socket
3044cdf0e10cSrcweir sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
304563d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed.";
3046cdf0e10cSrcweir sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
304763d99982SDamjan Jovanovic ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed.";
3048cdf0e10cSrcweir asAcceptorSocket.enableNonBlockingMode( sal_True );
3049cdf0e10cSrcweir
3050cdf0e10cSrcweir /// launch client socket
3051cdf0e10cSrcweir csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
3052cdf0e10cSrcweir
3053cdf0e10cSrcweir oslSocketResult eResult = asAcceptorSocket.acceptConnection(ssConnection, saPeerSocketAddr); /// waiting for incoming connection...
3054cdf0e10cSrcweir
305563d99982SDamjan Jovanovic ASSERT_TRUE(( sal_True == bOK2 ) &&
3056cdf0e10cSrcweir ( osl_Socket_Ok == eResult ) &&
305763d99982SDamjan Jovanovic ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) )) << "test for listen_accept function: try to create a connection with remote host, using listen and accept, accept with peer address.";
3058cdf0e10cSrcweir }
3059cdf0e10cSrcweir
TEST_F(listen_accept,listen_accept_003)306063d99982SDamjan Jovanovic TEST_F(listen_accept, listen_accept_003)
3061cdf0e10cSrcweir {
3062cdf0e10cSrcweir
3063cdf0e10cSrcweir }
3064cdf0e10cSrcweir
3065cdf0e10cSrcweir
3066cdf0e10cSrcweir } // namespace osl_AcceptorSocket
3067cdf0e10cSrcweir
3068cdf0e10cSrcweir
3069cdf0e10cSrcweir namespace osl_DatagramSocket
3070cdf0e10cSrcweir {
3071cdf0e10cSrcweir
3072cdf0e10cSrcweir /** testing the methods:
3073cdf0e10cSrcweir inline DatagramSocket(oslAddrFamily Family= osl_Socket_FamilyInet,
3074cdf0e10cSrcweir oslProtocol Protocol= osl_Socket_ProtocolIp,
3075cdf0e10cSrcweir oslSocketType Type= osl_Socket_TypeDgram);
3076cdf0e10cSrcweir */
3077cdf0e10cSrcweir
307863d99982SDamjan Jovanovic class DatagramSocketCtors : public ::testing::Test
3079cdf0e10cSrcweir {
3080cdf0e10cSrcweir public:
308163d99982SDamjan Jovanovic }; // class ctors
3082cdf0e10cSrcweir
TEST_F(DatagramSocketCtors,ctors_001)308363d99982SDamjan Jovanovic TEST_F(DatagramSocketCtors, ctors_001)
3084cdf0e10cSrcweir {
3085cdf0e10cSrcweir /// Socket constructor.
3086cdf0e10cSrcweir ::osl::DatagramSocket dsSocket;
3087cdf0e10cSrcweir
308863d99982SDamjan Jovanovic ASSERT_TRUE(osl_Socket_TypeDgram == dsSocket.getType( )) << "test for ctors_001 constructor function: check if the datagram socket was created successfully.";
3089cdf0e10cSrcweir }
3090cdf0e10cSrcweir
3091cdf0e10cSrcweir /**thread do sendTo, refer to http://www.coding-zone.co.uk/cpp/articles/140101networkprogrammingv.shtml
3092cdf0e10cSrcweir */
3093cdf0e10cSrcweir class TalkerThread : public Thread
3094cdf0e10cSrcweir {
3095cdf0e10cSrcweir protected:
3096cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr;
3097cdf0e10cSrcweir ::osl::DatagramSocket dsSocket;
3098cdf0e10cSrcweir
run()3099cdf0e10cSrcweir void SAL_CALL run( )
3100cdf0e10cSrcweir {
3101cdf0e10cSrcweir dsSocket.sendTo( saTargetSocketAddr, pTestString1, strlen( pTestString1 ) + 1 ); // "test socket"
3102cdf0e10cSrcweir dsSocket.shutdown();
3103cdf0e10cSrcweir }
3104cdf0e10cSrcweir
onTerminated()3105cdf0e10cSrcweir void SAL_CALL onTerminated( )
3106cdf0e10cSrcweir {
3107cdf0e10cSrcweir }
3108cdf0e10cSrcweir
3109cdf0e10cSrcweir public:
TalkerThread()3110cdf0e10cSrcweir TalkerThread( ):
3111cdf0e10cSrcweir saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT9 )
3112cdf0e10cSrcweir {
3113cdf0e10cSrcweir }
3114cdf0e10cSrcweir
~TalkerThread()3115cdf0e10cSrcweir ~TalkerThread( )
3116cdf0e10cSrcweir {
3117cdf0e10cSrcweir if ( isRunning( ) )
311863d99982SDamjan Jovanovic printf("# error: TalkerThread not terminated normally.\n" );
3119cdf0e10cSrcweir }
3120cdf0e10cSrcweir };
3121cdf0e10cSrcweir
3122cdf0e10cSrcweir /**thread do listen, refer to http://www.coding-zone.co.uk/cpp/articles/140101networkprogrammingv.shtml
3123cdf0e10cSrcweir */
3124cdf0e10cSrcweir class ListenerThread : public Thread
3125cdf0e10cSrcweir {
3126cdf0e10cSrcweir protected:
3127cdf0e10cSrcweir ::osl::SocketAddr saTargetSocketAddr;
3128cdf0e10cSrcweir ::osl::DatagramSocket dsSocket;
3129cdf0e10cSrcweir
run()3130cdf0e10cSrcweir void SAL_CALL run( )
3131cdf0e10cSrcweir {
3132cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT10 );
3133cdf0e10cSrcweir dsSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
3134cdf0e10cSrcweir if ( dsSocket.bind( saLocalSocketAddr ) == sal_False )
3135cdf0e10cSrcweir {
313663d99982SDamjan Jovanovic printf("DatagramSocket bind failed \n");
3137cdf0e10cSrcweir return;
3138cdf0e10cSrcweir }
3139cdf0e10cSrcweir //blocking mode: default
3140cdf0e10cSrcweir sal_Int32 nRecv = dsSocket.recvFrom( pRecvBuffer, 30, &saTargetSocketAddr); //strlen( pTestString2 ) + 1
314163d99982SDamjan Jovanovic printf("After recvFrom, nRecv is %d\n", nRecv);
3142cdf0e10cSrcweir }
3143cdf0e10cSrcweir
onTerminated()3144cdf0e10cSrcweir void SAL_CALL onTerminated( )
3145cdf0e10cSrcweir {
3146cdf0e10cSrcweir }
3147cdf0e10cSrcweir
3148cdf0e10cSrcweir public:
3149cdf0e10cSrcweir sal_Char pRecvBuffer[30];
ListenerThread()3150cdf0e10cSrcweir ListenerThread( ):
3151cdf0e10cSrcweir saTargetSocketAddr( aHostIp1, IP_PORT_MYPORT10 )
3152cdf0e10cSrcweir {
3153cdf0e10cSrcweir pRecvBuffer[0] = '\0';
3154cdf0e10cSrcweir }
3155cdf0e10cSrcweir
~ListenerThread()3156cdf0e10cSrcweir ~ListenerThread( )
3157cdf0e10cSrcweir {
3158cdf0e10cSrcweir if ( isRunning( ) )
315963d99982SDamjan Jovanovic printf("# error: ListenerThread not terminated normally.\n" );
3160cdf0e10cSrcweir }
3161cdf0e10cSrcweir
3162cdf0e10cSrcweir };
3163cdf0e10cSrcweir
3164cdf0e10cSrcweir /** testing the methods:
3165cdf0e10cSrcweir inline sal_Int32 DatagramSocket::recvFrom(void* pBuffer, sal_uInt32 BufferSize,
3166cdf0e10cSrcweir SocketAddr* pSenderAddr, oslSocketMsgFlag Flag )
3167cdf0e10cSrcweir inline sal_Int32 DatagramSocket::sendTo( const SocketAddr& ReceiverAddr,
3168cdf0e10cSrcweir const void* pBuffer, sal_uInt32 BufferSize, oslSocketMsgFlag Flag )
3169cdf0e10cSrcweir */
3170cdf0e10cSrcweir
317163d99982SDamjan Jovanovic class sendTo_recvFrom : public ::testing::Test
3172cdf0e10cSrcweir {
3173cdf0e10cSrcweir public:
317463d99982SDamjan Jovanovic }; // class sendTo_recvFrom
3175cdf0e10cSrcweir
TEST_F(sendTo_recvFrom,sr_001)317663d99982SDamjan Jovanovic TEST_F(sendTo_recvFrom, sr_001)
3177cdf0e10cSrcweir {
3178cdf0e10cSrcweir ::osl::SocketAddr saLocalSocketAddr( aHostIp1, IP_PORT_MYPORT9 );
3179cdf0e10cSrcweir ::osl::DatagramSocket dsSocket;
3180cdf0e10cSrcweir dsSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
3181cdf0e10cSrcweir dsSocket.bind( saLocalSocketAddr );
3182cdf0e10cSrcweir
3183cdf0e10cSrcweir sal_Char pReadBuffer[30];
3184cdf0e10cSrcweir TalkerThread myTalkThread;
3185cdf0e10cSrcweir myTalkThread.create();
3186cdf0e10cSrcweir sal_Int32 nRecv = dsSocket.recvFrom( pReadBuffer, 30, &saLocalSocketAddr);
3187cdf0e10cSrcweir myTalkThread.join();
318863d99982SDamjan Jovanovic //printf("#received buffer is %s# \n", pReadBuffer);
3189cdf0e10cSrcweir
3190cdf0e10cSrcweir sal_Bool bOk = ( strcmp(pReadBuffer, pTestString1) == 0 );
3191cdf0e10cSrcweir
319263d99982SDamjan Jovanovic ASSERT_TRUE(nRecv > 0 && bOk == sal_True) << "test for sendTo/recvFrom function: create a talker thread and recvFrom in the main thread, check if the datagram socket can communicate successfully.";
3193cdf0e10cSrcweir }
3194cdf0e10cSrcweir
TEST_F(sendTo_recvFrom,sr_002)319563d99982SDamjan Jovanovic TEST_F(sendTo_recvFrom, sr_002)
3196cdf0e10cSrcweir {
3197cdf0e10cSrcweir ::osl::SocketAddr saListenSocketAddr( aHostIp1, IP_PORT_MYPORT10 );
3198cdf0e10cSrcweir ::osl::DatagramSocket dsSocket;
3199cdf0e10cSrcweir
3200cdf0e10cSrcweir //listener thread construct a DatagramSocket, recvFrom waiting for data, then main thread sendto data
3201cdf0e10cSrcweir ListenerThread myListenThread;
3202cdf0e10cSrcweir myListenThread.create();
3203cdf0e10cSrcweir //to grantee the recvFrom is before sendTo
3204cdf0e10cSrcweir thread_sleep( 1 );
3205cdf0e10cSrcweir
3206cdf0e10cSrcweir sal_Int32 nSend = dsSocket.sendTo( saListenSocketAddr, pTestString2, strlen( pTestString2 ) + 1 );
3207cdf0e10cSrcweir
320863d99982SDamjan Jovanovic ASSERT_TRUE(nSend > 0) << "DatagramSocket sendTo failed: nSend <= 0.";
3209cdf0e10cSrcweir
3210cdf0e10cSrcweir myListenThread.join();
321163d99982SDamjan Jovanovic //printf("#received buffer is %s# \n", myListenThread.pRecvBuffer);
3212cdf0e10cSrcweir
3213cdf0e10cSrcweir sal_Bool bOk = ( strcmp( myListenThread.pRecvBuffer, pTestString2) == 0 );
3214cdf0e10cSrcweir
321563d99982SDamjan Jovanovic ASSERT_TRUE(bOk == sal_True) << "test for sendTo/recvFrom function: create a listener thread and sendTo in the main thread, check if the datagram socket can communicate successfully.";
3216cdf0e10cSrcweir }
3217cdf0e10cSrcweir
3218cdf0e10cSrcweir //sendTo error, return -1; recvFrom error, return -1
TEST_F(sendTo_recvFrom,sr_003)321963d99982SDamjan Jovanovic TEST_F(sendTo_recvFrom, sr_003)
3220cdf0e10cSrcweir {
3221cdf0e10cSrcweir ::osl::SocketAddr saListenSocketAddr( aHostIpInval1, IP_PORT_MYPORT10 );
3222cdf0e10cSrcweir ::osl::DatagramSocket dsSocket;
3223cdf0e10cSrcweir // Transport endpoint is not connected
3224cdf0e10cSrcweir sal_Int32 nSend = dsSocket.sendTo( saListenSocketAddr, pTestString2, strlen( pTestString2 ) + 1 );
322563d99982SDamjan Jovanovic ASSERT_TRUE(nSend == -1) << "DatagramSocket sendTo should fail: nSend <= 0.";
3226cdf0e10cSrcweir }
3227cdf0e10cSrcweir
TEST_F(sendTo_recvFrom,sr_004)322863d99982SDamjan Jovanovic TEST_F(sendTo_recvFrom, sr_004)
3229cdf0e10cSrcweir {
3230cdf0e10cSrcweir ::osl::SocketAddr saListenSocketAddr1( aHostIpInval1, IP_PORT_MYPORT10 );
3231cdf0e10cSrcweir ::osl::SocketAddr saListenSocketAddr2( aHostIp2, IP_PORT_MYPORT10 );
3232cdf0e10cSrcweir ::osl::DatagramSocket dsSocket;
3233cdf0e10cSrcweir
3234cdf0e10cSrcweir dsSocket.enableNonBlockingMode( sal_True );
3235cdf0e10cSrcweir
3236cdf0e10cSrcweir sal_Char pReadBuffer[30];
3237cdf0e10cSrcweir //sal_Int32 nRecv1 = dsSocket.recvFrom( pReadBuffer, 30, &saListenSocketAddr1 );
3238cdf0e10cSrcweir
3239cdf0e10cSrcweir // will block ?
3240cdf0e10cSrcweir CloseSocketThread myThread( dsSocket );
3241cdf0e10cSrcweir myThread.create();
3242cdf0e10cSrcweir sal_Int32 nRecv2 = dsSocket.recvFrom( pReadBuffer, 30, &saListenSocketAddr1 );
3243cdf0e10cSrcweir myThread.join();
324463d99982SDamjan Jovanovic //printf("#nRecv1 is %d nRecv2 is %d\n", nRecv1, nRecv2 );
324563d99982SDamjan Jovanovic ASSERT_TRUE(nRecv2 == -1) << "DatagramSocket sendTo should fail: nSend <= 0.";
3246cdf0e10cSrcweir }
3247cdf0e10cSrcweir
3248cdf0e10cSrcweir
3249cdf0e10cSrcweir } // namespace osl_DatagramSocket
3250cdf0e10cSrcweir
signalHandler(void * pData,oslSignalInfo * pInfo)325163d99982SDamjan Jovanovic static oslSignalAction SAL_CALL signalHandler(void* pData, oslSignalInfo* pInfo)
325263d99982SDamjan Jovanovic {
325363d99982SDamjan Jovanovic return osl_Signal_ActCallNextHdl;
325463d99982SDamjan Jovanovic }
3255cdf0e10cSrcweir
main(int argc,char ** argv)325663d99982SDamjan Jovanovic int main(int argc, char **argv)
325763d99982SDamjan Jovanovic {
325863d99982SDamjan Jovanovic osl_addSignalHandler(signalHandler, NULL);
325963d99982SDamjan Jovanovic ::testing::InitGoogleTest(&argc, argv);
326063d99982SDamjan Jovanovic return RUN_ALL_TESTS();
326163d99982SDamjan Jovanovic }
3262