bump product version to 5.0.4.1
[LibreOffice.git] / sal / qa / osl / socket / sockethelper.cxx
blobdf4f5e7c11ca7542102ea036712bd1dc0fa5b7ac
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/types.h>
21 #include "sockethelper.hxx"
22 #include <cppunit/TestFixture.h>
23 #include <cppunit/extensions/HelperMacros.h>
24 #include <cppunit/plugin/TestPlugIn.h>
26 #if OSL_DEBUG_LEVEL > 0
27 # define SILENT_TEST 0
28 #else
29 # define SILENT_TEST 1
30 #endif
32 #if SILENT_TEST
33 # define t_print(...) { }
34 #else
35 # define t_print printf
36 #endif
38 // Ip version definition
40 #define IP_VER 4 /// currently only IPv4 is considered.
42 // helper functions
44 /** compare two OUString.
46 sal_Bool compareUString( const ::rtl::OUString & ustr1, const ::rtl::OUString & ustr2 )
48 sal_Bool bOk = ustr1.equalsIgnoreAsciiCase( ustr2 );
50 return bOk;
53 /** compare a OUString and an ASCII string.
55 sal_Bool compareUString( const ::rtl::OUString & ustr, const sal_Char *astr )
57 ::rtl::OUString ustr2 = rtl::OUString::createFromAscii( astr );
58 sal_Bool bOk = ustr.equalsIgnoreAsciiCase( ustr2 );
60 return bOk;
63 /** compare two socket address.
65 sal_Bool compareSocketAddr( const ::osl::SocketAddr & addr1 , const ::osl::SocketAddr & addr2 )
67 return ( ( sal_True == compareUString( addr1.getHostname( 0 ), addr2.getHostname( 0 ) ) ) && ( addr2.getPort( ) == addr2.getPort( ) ) );
70 /** print a UNI_CODE String. And also print some comments of the string.
72 void printUString( const ::rtl::OUString & str, const char* msg)
74 #if SILENT_TEST
75 (void)str;
76 (void)msg;
77 #else
78 t_print("#%s #printUString_u# ", msg );
79 rtl::OString aString;
80 aString = ::rtl::OUStringToOString( str, RTL_TEXTENCODING_ASCII_US );
81 t_print("%s\n", aString.getStr( ) );
82 #endif
85 /** get the local host name.
86 mindy: gethostbyname( "localhost" ), on Linux, it returns the hostname in /etc/hosts + domain name,
87 if no entry in /etc/hosts, it returns "localhost" + domain name
89 ::rtl::OUString getHost()
91 struct hostent *hptr;
93 hptr = gethostbyname( "localhost" );
94 OSL_ENSURE( hptr != NULL, "#In getHostname function, error on gethostbyname()" );
95 ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) hptr->h_name );
97 return aUString;
100 /** get the full host name of the current processor, such as "aegean.prc.sun.com" --mindyliu
102 ::rtl::OUString getThisHostname()
104 ::rtl::OUString aUString;
105 #ifdef WNT
106 struct hostent *hptr;
107 hptr = gethostbyname( "localhost" );
108 OSL_ENSURE( hptr != NULL, "#In getHostname function, error on gethostbyname()" );
109 rtl::OString sHostname(hptr->h_name);
110 aUString = ::rtl::OStringToOUString(sHostname, RTL_TEXTENCODING_ASCII_US);
111 #else
112 char hostname[255];
113 if (gethostname(hostname, 255) != 0) {
114 OSL_FAIL( "#Error: gethostname failed." );
117 struct hostent *hptr;
118 //first search /ets/hosts, then search from dns
119 hptr = gethostbyname( hostname);
120 if ( hptr != NULL )
122 strcpy( hostname, hptr->h_name );
125 t_print("hostname is %s \n", hostname );
126 rtl::OString sHostname( hostname );
127 aUString = ::rtl::OStringToOUString( sHostname, RTL_TEXTENCODING_ASCII_US );
128 aUString.getLength();
129 #endif
130 return aUString;
133 /** get IP by name, search /etc/hosts first, then search from dns, fail return OUString("")
135 ::rtl::OUString getIPbyName( rtl::OString const& str_name )
137 ::rtl::OUString aUString;
138 struct hostent *hptr;
139 //first search /ets/hosts, then search from dns
140 hptr = gethostbyname( str_name.getStr());
141 if ( hptr != NULL )
143 struct in_addr ** addrptr;
144 addrptr = (struct in_addr **) hptr->h_addr_list ;
145 //if there are more than one IPs on the same machine, we select one
146 for (; *addrptr; addrptr++)
148 t_print("#Local IP Address: %s\n", inet_ntoa(**addrptr));
149 aUString = ::rtl::OUString::createFromAscii( (sal_Char *) (inet_ntoa(**addrptr)) );
152 return aUString;
155 /** get local ethernet IP
157 ::rtl::OUString getLocalIP( )
159 char hostname[255];
160 gethostname(hostname, 255);
162 return getIPbyName( hostname );
165 /** construct error message
167 ::rtl::OUString outputError( const ::rtl::OUString & returnVal, const ::rtl::OUString & rightVal, const sal_Char * msg )
169 ::rtl::OUString aUString;
170 if ( returnVal.equals( rightVal ) )
171 return aUString;
172 aUString += ::rtl::OUString::createFromAscii(msg);
173 aUString += ::rtl::OUString(": the returned value is '");
174 aUString += returnVal;
175 aUString += ::rtl::OUString("', but the value should be '");
176 aUString += rightVal;
177 aUString += ::rtl::OUString("'.");
178 return aUString;
181 /** wait _nSec seconds.
183 void thread_sleep( sal_Int32 _nSec )
185 /// print statement in thread process must use fflush() to force display.
186 // printf("wait %d seconds. ", _nSec );
187 // fflush(stdout);
189 #ifdef WNT //Windows
190 Sleep( _nSec * 100 );
191 #endif
192 #if ( defined UNX ) //Unix
193 usleep(_nSec * 100000);
194 #endif
195 // t_print("# done\n" );
198 /** print Boolean value.
200 void printBool( sal_Bool bOk )
202 #if SILENT_TEST
203 (void)bOk;
204 #else
205 t_print("#printBool# " );
206 t_print ("%s", (sal_True == bOk) ? "YES!\n" : "NO!\n");
207 #endif
210 /** print content of a ByteSequence.
212 void printByteSequence_IP( const ::rtl::ByteSequence & bsByteSeq, sal_Int32 nLen )
214 #if SILENT_TEST
215 (void)bsByteSeq;
216 (void)nLen;
217 #else
218 t_print("#ByteSequence is: " );
219 for ( int i = 0; i < nLen; i++ ){
220 if ( bsByteSeq[i] < 0 )
221 t_print("%d ", 256 + bsByteSeq[i] );
222 else
223 t_print("%d ", bsByteSeq[i] );
225 t_print(" .\n" );
226 #endif
229 /** convert an IP which is stored as a UString format to a ByteSequence array for later use.
231 ::rtl::ByteSequence UStringIPToByteSequence( ::rtl::OUString aUStr )
234 rtl::OString aString = ::rtl::OUStringToOString( aUStr, RTL_TEXTENCODING_ASCII_US );
235 const sal_Char *pChar = aString.getStr( ) ;
236 sal_Char tmpBuffer[4];
237 sal_Int32 nCharCounter = 0;
238 ::rtl::ByteSequence bsByteSequence( IP_VER );
239 sal_Int32 nByteSeqCounter = 0;
241 for ( int i = 0; i < aString.getLength( ) + 1 ; i++ )
243 if ( ( *pChar != '.' ) && ( i !=aString.getLength( ) ) )
244 tmpBuffer[nCharCounter++] = *pChar;
245 else
247 tmpBuffer[nCharCounter] = '\0';
248 nCharCounter = 0;
249 bsByteSequence[nByteSeqCounter++] = static_cast<sal_Int8>(atoi( tmpBuffer ));
251 pChar++;
253 return bsByteSequence;
256 /** print a socket result name.
258 void printSocketResult( oslSocketResult eResult )
260 t_print("printSocketResult: " );
261 if (!eResult)
262 switch (eResult)
264 case osl_Socket_Ok:
265 t_print("client connected\n");
266 break;
267 case osl_Socket_Error:
268 t_print("got an error ... exiting\r\n\r\n" );
269 break;
270 case osl_Socket_TimedOut:
271 t_print("timeout\n");
272 break;
273 case osl_Socket_Interrupted:
274 t_print("interrupted\n");
275 break;
276 case osl_Socket_InProgress:
277 t_print("in progress\n");
278 break;
279 default:
280 t_print("unknown result\n");
281 break;
285 /** if 4 parts of an IP addr are equal to specified values
287 sal_Bool ifIpv4is( const ::rtl::ByteSequence Ipaddr, sal_Int8 seq1, sal_Int8 seq2, sal_Int8 seq3, sal_Int8 seq4 )
289 if ( ( Ipaddr[0] == seq1 ) && ( Ipaddr[1] == seq2 ) && ( Ipaddr[2] == seq3 ) && ( Ipaddr[3] == seq4 ) )
290 return sal_True;
291 return sal_False;
294 sal_Bool ifAvailable( rtl::OUString const& strAddrOrHostName )
296 ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
297 ::osl::SocketAddr aSocketAddr( strAddrOrHostName, 7 );
299 if (! aSocketAddr.is())
301 aSocket.close();
302 return sal_False;
305 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
307 TimeValue *pTimeout;
308 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
309 pTimeout->Seconds = 3;
310 pTimeout->Nanosec = 0;
312 oslSocketResult aResult = aSocket.connect( aSocketAddr, pTimeout );
313 free( pTimeout );
314 aSocket.close();
315 if ( aResult != osl_Socket_Ok )
317 t_print("Error: ");
318 printSocketResult(aResult);
319 t_print("\n");
321 return sal_False;
323 return sal_True;
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */