1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_sal.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir /** test coder preface: 32*cdf0e10cSrcweir 1. the BSD socket function will meet "unresolved external symbol error" on Windows platform 33*cdf0e10cSrcweir if you are not including ws2_32.lib in makefile.mk, the including format will be like this: 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir .IF "$(GUI)" == "WNT" 36*cdf0e10cSrcweir SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib 37*cdf0e10cSrcweir SHL1STDLIBS += ws2_32.lib 38*cdf0e10cSrcweir .ENDIF 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir likewise on Solaris platform. 41*cdf0e10cSrcweir .IF "$(GUI)" == "UNX" 42*cdf0e10cSrcweir SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a 43*cdf0e10cSrcweir SHL1STDLIBS += -lsocket -ldl -lnsl 44*cdf0e10cSrcweir .ENDIF 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir 2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4 47*cdf0e10cSrcweir category. 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir 3. some fragment of Socket source implementation are lack of comment so it is hard for testers 50*cdf0e10cSrcweir guess what the exact functionality or usage of a member. Hope the Socket section's comment 51*cdf0e10cSrcweir will be added. 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir 4. following functions are declared but not implemented: 54*cdf0e10cSrcweir inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; 55*cdf0e10cSrcweir */ 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir //------------------------------------------------------------------------ 58*cdf0e10cSrcweir // include files 59*cdf0e10cSrcweir //------------------------------------------------------------------------ 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir #include <testshl/simpleheader.hxx> 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir //#include "osl_Socket_Const.h" 64*cdf0e10cSrcweir #include "sockethelper.hxx" 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir using namespace osl; 67*cdf0e10cSrcweir using namespace rtl; 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir #define IP_PORT_ZERO 0 70*cdf0e10cSrcweir #define IP_PORT_FTP 21 71*cdf0e10cSrcweir #define IP_PORT_TELNET 23 72*cdf0e10cSrcweir #define IP_PORT_HTTP1 80 73*cdf0e10cSrcweir #define IP_PORT_HTTP2 8080 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir #define IP_PORT_MYPORT 8881 //8888 76*cdf0e10cSrcweir #define IP_PORT_MYPORT2 8883 //8890 77*cdf0e10cSrcweir #define IP_PORT_MYPORT3 8884 //8891 78*cdf0e10cSrcweir #define IP_PORT_INVAL 99999 79*cdf0e10cSrcweir #define IP_PORT_MYPORT4 8885 //8892 80*cdf0e10cSrcweir #define IP_PORT_NETBIOS_DGM 138 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir namespace osl_SocketAddr 84*cdf0e10cSrcweir { 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir /** testing the methods: 87*cdf0e10cSrcweir inline SocketAddr(); 88*cdf0e10cSrcweir inline SocketAddr(const SocketAddr& Addr); 89*cdf0e10cSrcweir inline SocketAddr(const oslSocketAddr , __osl_socket_NoCopy nocopy ); 90*cdf0e10cSrcweir inline SocketAddr(oslSocketAddr Addr); 91*cdf0e10cSrcweir inline SocketAddr( const ::rtl::OUString& strAddrOrHostName, sal_Int32 nPort ); 92*cdf0e10cSrcweir */ 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir class ctors : public CppUnit::TestFixture 95*cdf0e10cSrcweir { 96*cdf0e10cSrcweir public: 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir void ctors_none() 99*cdf0e10cSrcweir { 100*cdf0e10cSrcweir /// SocketAddr constructor. 101*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr; 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir // oslSocketResult aResult; 104*cdf0e10cSrcweir // rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult); 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir // rtl::OUString suHost2 = getThisHostname(); 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for none parameter constructor function: check if the socket address was created successfully", 109*cdf0e10cSrcweir sal_True == saSocketAddr.is( ) ); 110*cdf0e10cSrcweir } 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir void ctors_none_000() 113*cdf0e10cSrcweir { 114*cdf0e10cSrcweir /// SocketAddr constructor. 115*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr; 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir oslSocketResult aResult; 118*cdf0e10cSrcweir rtl::OUString suHost = saSocketAddr.getLocalHostname( &aResult); 119*cdf0e10cSrcweir rtl::OUString suHost2 = getThisHostname(); 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir sal_Bool bOk = compareUString(suHost, suHost2); 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir rtl::OUString suError = rtl::OUString::createFromAscii("Host names should be the same. From SocketAddr.getLocalHostname() it is'"); 124*cdf0e10cSrcweir suError += suHost; 125*cdf0e10cSrcweir suError += rtl::OUString::createFromAscii("', from getThisHostname() it is '"); 126*cdf0e10cSrcweir suError += suHost2; 127*cdf0e10cSrcweir suError += rtl::OUString::createFromAscii("'."); 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE(suError, sal_True == bOk); 130*cdf0e10cSrcweir } 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir void ctors_copy() 133*cdf0e10cSrcweir { 134*cdf0e10cSrcweir /// SocketAddr copy constructor. 135*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); 136*cdf0e10cSrcweir ::osl::SocketAddr saCopySocketAddr( saSocketAddr ); 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir sal_Int32 nPort = saCopySocketAddr.getPort( ); 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy constructor function: copy constructor, do an action of copy construction then check the port with original set.", 141*cdf0e10cSrcweir ( sal_True == saCopySocketAddr.is( ) ) && ( nPort == IP_PORT_HTTP1 ) ); 142*cdf0e10cSrcweir } 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir void ctors_copy_no_001() 145*cdf0e10cSrcweir { 146*cdf0e10cSrcweir #if 0 147*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); 148*cdf0e10cSrcweir oslSocketAddr psaOSLSocketAddr = saSocketAddr.getHandle( ); 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrCopy( psaOSLSocketAddr, SAL_NO_COPY ); 151*cdf0e10cSrcweir saSocketAddrCopy.setPort( IP_PORT_HTTP2 ); 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("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.", 154*cdf0e10cSrcweir saSocketAddr.getPort( ) == IP_PORT_HTTP2 ); 155*cdf0e10cSrcweir #endif 156*cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); 157*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL); 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( ); 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY ); 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir pSocketAddrCopy->setPort( IP_PORT_HTTP2 ); 164*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("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.", 165*cdf0e10cSrcweir pSocketAddr->getPort( ) == IP_PORT_HTTP2 ); 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir delete pSocketAddrCopy; 168*cdf0e10cSrcweir // LLA: don't do this also: delete pSocketAddr; 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir void ctors_copy_no_002() 172*cdf0e10cSrcweir { 173*cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); 174*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL); 175*cdf0e10cSrcweir oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( ); 176*cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY ); 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("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.", 179*cdf0e10cSrcweir pSocketAddr->getHandle( ) == pSocketAddrCopy->getHandle( ) ); 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir delete pSocketAddrCopy; 182*cdf0e10cSrcweir } 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir void ctors_copy_handle_001() 185*cdf0e10cSrcweir { 186*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); 187*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrCopy( saSocketAddr.getHandle( ) ); 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy handle constructor function: copy another Socket's handle, get its port to check copy effect.", 190*cdf0e10cSrcweir saSocketAddrCopy.getPort( ) == IP_PORT_HTTP1 ); 191*cdf0e10cSrcweir } 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir void ctors_copy_handle_002() 194*cdf0e10cSrcweir { 195*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); 196*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrCopy( saSocketAddr.getHandle( ) ); 197*cdf0e10cSrcweir saSocketAddrCopy.setPort( IP_PORT_HTTP2 ); 198*cdf0e10cSrcweir 199*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for SocketAddr copy handle constructor function: copy another Socket's handle, the original one should not be changed.", 200*cdf0e10cSrcweir saSocketAddr.getPort( ) != IP_PORT_HTTP2 ); 201*cdf0e10cSrcweir } 202*cdf0e10cSrcweir 203*cdf0e10cSrcweir void ctors_hostname_port_001() 204*cdf0e10cSrcweir { 205*cdf0e10cSrcweir /// tcpip-specif constructor. 206*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP ); 207*cdf0e10cSrcweir printUString( saSocketAddr.getHostname( ), "ctors_hostname_port_001:getHostname"); 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for SocketAddr tcpip specif constructor function: do a constructor using tcpip spec, check the result.", 210*cdf0e10cSrcweir saSocketAddr.is( ) == sal_True && 211*cdf0e10cSrcweir ( saSocketAddr.getPort( ) == IP_PORT_FTP )/*&& 212*cdf0e10cSrcweir ( sal_True == compareUString( saSocketAddr.getHostname( ), aHostName1 ) ) */); 213*cdf0e10cSrcweir } 214*cdf0e10cSrcweir 215*cdf0e10cSrcweir //same as is_002 216*cdf0e10cSrcweir void ctors_hostname_port_002() 217*cdf0e10cSrcweir { 218*cdf0e10cSrcweir /// tcpip-specif constructor. 219*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT2 ); 220*cdf0e10cSrcweir 221*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for SocketAddr tcpip specif constructor function: using an invalid IP address, the socketaddr ctors should fail", sal_False == saSocketAddr.is( )); 222*cdf0e10cSrcweir } 223*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( ctors ); 224*cdf0e10cSrcweir CPPUNIT_TEST( ctors_none ); 225*cdf0e10cSrcweir CPPUNIT_TEST( ctors_none_000 ); 226*cdf0e10cSrcweir CPPUNIT_TEST( ctors_copy ); 227*cdf0e10cSrcweir CPPUNIT_TEST( ctors_copy_no_001 ); 228*cdf0e10cSrcweir CPPUNIT_TEST( ctors_copy_no_002 ); 229*cdf0e10cSrcweir CPPUNIT_TEST( ctors_copy_handle_001 ); 230*cdf0e10cSrcweir CPPUNIT_TEST( ctors_copy_handle_002 ); 231*cdf0e10cSrcweir CPPUNIT_TEST( ctors_hostname_port_001 ); 232*cdf0e10cSrcweir CPPUNIT_TEST( ctors_hostname_port_002 ); 233*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 234*cdf0e10cSrcweir 235*cdf0e10cSrcweir }; // class ctors 236*cdf0e10cSrcweir 237*cdf0e10cSrcweir 238*cdf0e10cSrcweir /** testing the method: 239*cdf0e10cSrcweir inline sal_Bool is() const; 240*cdf0e10cSrcweir */ 241*cdf0e10cSrcweir 242*cdf0e10cSrcweir class is : public CppUnit::TestFixture 243*cdf0e10cSrcweir { 244*cdf0e10cSrcweir public: 245*cdf0e10cSrcweir void is_001() 246*cdf0e10cSrcweir { 247*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr; 248*cdf0e10cSrcweir 249*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for is() function: create an unknown type socket, it should be True when call is.", 250*cdf0e10cSrcweir sal_True == saSocketAddr.is( ) ); 251*cdf0e10cSrcweir } 252*cdf0e10cSrcweir // refer to setPort_003() 253*cdf0e10cSrcweir void is_002() 254*cdf0e10cSrcweir { 255*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_INVAL ); 256*cdf0e10cSrcweir 257*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for is() function: create a tcp-ip socket using invalid port number", 258*cdf0e10cSrcweir sal_True == saSocketAddr.is( ) ); 259*cdf0e10cSrcweir } 260*cdf0e10cSrcweir 261*cdf0e10cSrcweir void is_003() 262*cdf0e10cSrcweir { 263*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT ); 264*cdf0e10cSrcweir 265*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("test for is() function: create a tcp-ip socket using invalid Ip number", 266*cdf0e10cSrcweir sal_True != saSocketAddr.is( ) ); 267*cdf0e10cSrcweir } 268*cdf0e10cSrcweir 269*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( is ); 270*cdf0e10cSrcweir CPPUNIT_TEST( is_001 ); 271*cdf0e10cSrcweir CPPUNIT_TEST( is_002 ); 272*cdf0e10cSrcweir CPPUNIT_TEST( is_003 ); 273*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 274*cdf0e10cSrcweir 275*cdf0e10cSrcweir }; // class is 276*cdf0e10cSrcweir 277*cdf0e10cSrcweir 278*cdf0e10cSrcweir /** testing the method: 279*cdf0e10cSrcweir inline ::rtl::OUString SAL_CALL getHostname( oslSocketResult *pResult = 0 ) const; 280*cdf0e10cSrcweir */ 281*cdf0e10cSrcweir 282*cdf0e10cSrcweir class getHostname : public CppUnit::TestFixture 283*cdf0e10cSrcweir { 284*cdf0e10cSrcweir public: 285*cdf0e10cSrcweir void setUp() 286*cdf0e10cSrcweir { 287*cdf0e10cSrcweir } 288*cdf0e10cSrcweir 289*cdf0e10cSrcweir void tearDown() 290*cdf0e10cSrcweir { 291*cdf0e10cSrcweir } 292*cdf0e10cSrcweir 293*cdf0e10cSrcweir void getHostname_000() 294*cdf0e10cSrcweir { 295*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.107"), IP_PORT_FTP ); 296*cdf0e10cSrcweir rtl::OUString suResult = saSocketAddr.getHostname( 0 ); 297*cdf0e10cSrcweir 298*cdf0e10cSrcweir } 299*cdf0e10cSrcweir 300*cdf0e10cSrcweir /** it will search the Ip in current machine's /etc/hosts at first, if find, then return the 301*cdf0e10cSrcweir mapped hostname, otherwise, it will search via DNS server, and often return hostname+ Domain name 302*cdf0e10cSrcweir like "sceri.PRC.Sun.COM" 303*cdf0e10cSrcweir The process is same as Socket::getLocalHost(), but getLocalHost can only return hostname of the current machine. 304*cdf0e10cSrcweir */ 305*cdf0e10cSrcweir void getHostname_001() 306*cdf0e10cSrcweir { 307*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.107"), IP_PORT_FTP ); 308*cdf0e10cSrcweir rtl::OUString suResult = saSocketAddr.getHostname( 0 ); 309*cdf0e10cSrcweir rtl::OUString suError = outputError(suResult, rtl::OUString::createFromAscii("sceri.PRC.Sun.COM"), "test for getHostname(0)"); 310*cdf0e10cSrcweir sal_Bool bOK = compareUString( suResult, rtl::OUString::createFromAscii("sceri.PRC.Sun.COM") ); 311*cdf0e10cSrcweir // search the returned hostname in /etc/hosts, if find, and the IP in the row is same as IP 312*cdf0e10cSrcweir // in the Addr, it's right also. 313*cdf0e10cSrcweir if ( bOK == sal_False) 314*cdf0e10cSrcweir { 315*cdf0e10cSrcweir rtl::OString aString = ::rtl::OUStringToOString( suResult, RTL_TEXTENCODING_ASCII_US ); 316*cdf0e10cSrcweir if ( compareUString( getIPbyName( aString ), rtl::OUString::createFromAscii("129.158.217.107") ) == sal_True ) 317*cdf0e10cSrcweir bOK = sal_True; 318*cdf0e10cSrcweir } 319*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK); 320*cdf0e10cSrcweir } 321*cdf0e10cSrcweir 322*cdf0e10cSrcweir // LLA: now we have to control, if this behaviour is right. 323*cdf0e10cSrcweir // LLA: this function does not work in company (Linux, Windows) but at home 324*cdf0e10cSrcweir void getHostname_002() 325*cdf0e10cSrcweir { 326*cdf0e10cSrcweir rtl::OUString suHostname = rtl::OUString::createFromAscii("cn-1.germany.sun.com"); 327*cdf0e10cSrcweir rtl::OString aString = ::rtl::OUStringToOString( suHostname, RTL_TEXTENCODING_ASCII_US ); 328*cdf0e10cSrcweir rtl::OUString aHostIP = getIPbyName( aString ); 329*cdf0e10cSrcweir 330*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_FTP ); 331*cdf0e10cSrcweir sal_Bool bOK = saSocketAddr.setHostname( suHostname ); 332*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("#SocketAddr.setHostname failed", sal_True == bOK ); 333*cdf0e10cSrcweir oslSocketResult aResult; 334*cdf0e10cSrcweir rtl::OUString suResult = saSocketAddr.getHostname( &aResult ); 335*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("SocketAddr.getHostname failed.", aResult == osl_Socket_Ok); 336*cdf0e10cSrcweir 337*cdf0e10cSrcweir rtl::OUString suError = outputError(suResult, suHostname, "test for getHostname(0)"); 338*cdf0e10cSrcweir bOK = compareUString( suResult, suHostname ); 339*cdf0e10cSrcweir if ( bOK == sal_False) 340*cdf0e10cSrcweir { 341*cdf0e10cSrcweir rtl::OString aStringResult = ::rtl::OUStringToOString( suResult, RTL_TEXTENCODING_ASCII_US ); 342*cdf0e10cSrcweir rtl::OString aStringHostname = ::rtl::OUStringToOString( suHostname, RTL_TEXTENCODING_ASCII_US ); 343*cdf0e10cSrcweir if ( compareUString( getIPbyName( aStringResult ) , getIPbyName( aStringHostname ) ) == sal_True ) 344*cdf0e10cSrcweir { 345*cdf0e10cSrcweir bOK = sal_True; 346*cdf0e10cSrcweir } 347*cdf0e10cSrcweir } 348*cdf0e10cSrcweir 349*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK ); 350*cdf0e10cSrcweir } 351*cdf0e10cSrcweir 352*cdf0e10cSrcweir 353*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getHostname ); 354*cdf0e10cSrcweir CPPUNIT_TEST( getHostname_001 ); 355*cdf0e10cSrcweir CPPUNIT_TEST( getHostname_002 ); 356*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 357*cdf0e10cSrcweir 358*cdf0e10cSrcweir }; // class getHostname 359*cdf0e10cSrcweir 360*cdf0e10cSrcweir 361*cdf0e10cSrcweir /** testing the method: 362*cdf0e10cSrcweir inline sal_Int32 SAL_CALL getPort() const; 363*cdf0e10cSrcweir */ 364*cdf0e10cSrcweir 365*cdf0e10cSrcweir class getPort : public CppUnit::TestFixture 366*cdf0e10cSrcweir { 367*cdf0e10cSrcweir public: 368*cdf0e10cSrcweir void getPort_001() 369*cdf0e10cSrcweir { 370*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP ); 371*cdf0e10cSrcweir 372*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getPort() function: get a normal port number.", 373*cdf0e10cSrcweir IP_PORT_FTP == saSocketAddr.getPort( ) ); 374*cdf0e10cSrcweir } 375*cdf0e10cSrcweir 376*cdf0e10cSrcweir void getPort_002() 377*cdf0e10cSrcweir { 378*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_INVAL ); 379*cdf0e10cSrcweir 380*cdf0e10cSrcweir //t_print("#getPort_002: Port number is %d \n", saSocketAddr.getPort( )); 381*cdf0e10cSrcweir 382*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "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).", 383*cdf0e10cSrcweir saSocketAddr.getPort( )>=1 && saSocketAddr.getPort( ) <= 65535 ); 384*cdf0e10cSrcweir } 385*cdf0e10cSrcweir //two cases will return OSL_INVALID_PORT: 1. not valid SocketAddr 386*cdf0e10cSrcweir //2. SocketAddr family is not osl_Socket_FamilyInet, but case 2 could not be constructed 387*cdf0e10cSrcweir void getPort_003() 388*cdf0e10cSrcweir { 389*cdf0e10cSrcweir rtl::OUString suInvalidIP = rtl::OUString::createFromAscii("123.345.67.89"); 390*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( suInvalidIP, IP_PORT_MYPORT ); 391*cdf0e10cSrcweir 392*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getPort( ) function: give an invalid IP to a SocketAddr, get the port to see returned value. ", 393*cdf0e10cSrcweir saSocketAddr.getPort( ) == OSL_INVALID_PORT ); 394*cdf0e10cSrcweir } 395*cdf0e10cSrcweir 396*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getPort ); 397*cdf0e10cSrcweir CPPUNIT_TEST( getPort_001 ); 398*cdf0e10cSrcweir CPPUNIT_TEST( getPort_002 ); 399*cdf0e10cSrcweir CPPUNIT_TEST( getPort_003 ); 400*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( ); 401*cdf0e10cSrcweir 402*cdf0e10cSrcweir }; // class getPort 403*cdf0e10cSrcweir 404*cdf0e10cSrcweir 405*cdf0e10cSrcweir /** testing the method: 406*cdf0e10cSrcweir inline sal_Bool SAL_CALL setPort( sal_Int32 nPort ); 407*cdf0e10cSrcweir rfc1413.txt: TCP port numbers are from 1-65535 408*cdf0e10cSrcweir rfc1700.txt: 0/tcp Reserved ; 0/udp Reserved 409*cdf0e10cSrcweir */ 410*cdf0e10cSrcweir 411*cdf0e10cSrcweir class setPort : public CppUnit::TestFixture 412*cdf0e10cSrcweir { 413*cdf0e10cSrcweir public: 414*cdf0e10cSrcweir void setPort_001() 415*cdf0e10cSrcweir { 416*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP ); 417*cdf0e10cSrcweir sal_Bool bOK = saSocketAddr.setPort( IP_PORT_TELNET ); 418*cdf0e10cSrcweir 419*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for setPort() function: modify a port number setting, and check it.", 420*cdf0e10cSrcweir ( sal_True == bOK ) && 421*cdf0e10cSrcweir ( IP_PORT_TELNET == saSocketAddr.getPort( ) ) ); 422*cdf0e10cSrcweir } 423*cdf0e10cSrcweir 424*cdf0e10cSrcweir /** 0 to 1024 is known as the reserved port range (traditionally only root can assign programs to ports in 425*cdf0e10cSrcweir this range) and the ephemeral port range from 1025 to 65535. 426*cdf0e10cSrcweir As many of you programmers will know, when you specify the source port of 0 when you connect to a host, 427*cdf0e10cSrcweir the OS automatically reassigns the port number to high numbered ephemeral port. The same happens if you 428*cdf0e10cSrcweir try to bind a listening socket to port 0. 429*cdf0e10cSrcweir http://www.securiteam.com/securityreviews/5XP0Q2AAKS.html 430*cdf0e10cSrcweir another: http://www.muq.org/~cynbe/muq/mufref_564.html 431*cdf0e10cSrcweir */ 432*cdf0e10cSrcweir void setPort_002() 433*cdf0e10cSrcweir { 434*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP ); 435*cdf0e10cSrcweir sal_Bool bOK = saSocketAddr.setPort( IP_PORT_ZERO ); 436*cdf0e10cSrcweir 437*cdf0e10cSrcweir oslSocket sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp ); 438*cdf0e10cSrcweir ::osl::Socket sSocket(sHandle); 439*cdf0e10cSrcweir sSocket.setOption( osl_Socket_OptionReuseAddr, 1 );//sal_True); 440*cdf0e10cSrcweir sal_Bool bOK1 = sSocket.bind( saSocketAddr ); 441*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "bind SocketAddr failed", bOK1 == sal_True ); 442*cdf0e10cSrcweir 443*cdf0e10cSrcweir sal_Int32 newPort = sSocket.getLocalPort(); 444*cdf0e10cSrcweir //t_print("#new port is %d\n", newPort ); 445*cdf0e10cSrcweir 446*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "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.", 447*cdf0e10cSrcweir ( 1024 <= newPort ) && ( 65535 >= newPort ) && ( bOK == sal_True ) ); 448*cdf0e10cSrcweir 449*cdf0e10cSrcweir } 450*cdf0e10cSrcweir 451*cdf0e10cSrcweir void setPort_003() 452*cdf0e10cSrcweir { 453*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP); 454*cdf0e10cSrcweir sal_Bool bOK = saSocketAddr.setPort( IP_PORT_INVAL ); 455*cdf0e10cSrcweir //on Linux, getPort return 34463 456*cdf0e10cSrcweir //t_print("#Port number is %d \n", saSocketAddr.getPort( )); 457*cdf0e10cSrcweir 458*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for setPort( ) function: set an address with invalid port. it should return error or convert it to a valid port.", 459*cdf0e10cSrcweir ( ( 1 <= saSocketAddr.getPort( ) ) && ( 65535 >= saSocketAddr.getPort( ) ) &&( bOK == sal_True ) ) || 460*cdf0e10cSrcweir bOK == sal_False); 461*cdf0e10cSrcweir } 462*cdf0e10cSrcweir 463*cdf0e10cSrcweir /* this is not a inet-addr => can't set port */ 464*cdf0e10cSrcweir void setPort_004() 465*cdf0e10cSrcweir { 466*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_FTP); 467*cdf0e10cSrcweir sal_Bool bOK = saSocketAddr.setPort( IP_PORT_MYPORT ); 468*cdf0e10cSrcweir 469*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for setPort( ) function: set an invalid address with valid port. it should return error.", 470*cdf0e10cSrcweir bOK == sal_False); 471*cdf0e10cSrcweir } 472*cdf0e10cSrcweir 473*cdf0e10cSrcweir 474*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( setPort ); 475*cdf0e10cSrcweir CPPUNIT_TEST( setPort_001 ); 476*cdf0e10cSrcweir CPPUNIT_TEST( setPort_002 ); 477*cdf0e10cSrcweir CPPUNIT_TEST( setPort_003 ); 478*cdf0e10cSrcweir CPPUNIT_TEST( setPort_004 ); 479*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( ); 480*cdf0e10cSrcweir 481*cdf0e10cSrcweir }; // class setPort 482*cdf0e10cSrcweir 483*cdf0e10cSrcweir 484*cdf0e10cSrcweir /** tester comment: 485*cdf0e10cSrcweir 486*cdf0e10cSrcweir In the following two functions, it use ::rtl::ByteSequence as an intermediate storage for address, 487*cdf0e10cSrcweir the ByteSequence object can hold sal_Int8 arrays, which is raged [-127, 127], in case of IP addr 488*cdf0e10cSrcweir that is greater than 127, say 129.158.217.202, it will stored as -127, -98, -39, -54, it is unique 489*cdf0e10cSrcweir in the range of sal_Int8, but lack of readability. 490*cdf0e10cSrcweir so may be a sal_uInt8 array is better. 491*cdf0e10cSrcweir */ 492*cdf0e10cSrcweir 493*cdf0e10cSrcweir 494*cdf0e10cSrcweir /** testing the method: 495*cdf0e10cSrcweir inline sal_Bool SAL_CALL setAddr( const ::rtl::ByteSequence & address ); 496*cdf0e10cSrcweir */ 497*cdf0e10cSrcweir 498*cdf0e10cSrcweir class setAddr : public CppUnit::TestFixture 499*cdf0e10cSrcweir { 500*cdf0e10cSrcweir public: 501*cdf0e10cSrcweir void setAddr_001() 502*cdf0e10cSrcweir { 503*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 504*cdf0e10cSrcweir saSocketAddr.setAddr( UStringIPToByteSequence( rtl::OUString::createFromAscii("127.0.0.1") ) ); 505*cdf0e10cSrcweir ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( 0 ); 506*cdf0e10cSrcweir sal_Bool bOK = sal_False; 507*cdf0e10cSrcweir 508*cdf0e10cSrcweir // if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) && ( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) 509*cdf0e10cSrcweir // bOK = sal_True; 510*cdf0e10cSrcweir bOK = ifIpv4is( bsSocketAddr, 127, 0, 0, 1 ); 511*cdf0e10cSrcweir 512*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for setAddr() function: construct Addr with \"129.158.217.202\", set it to \"127.0.0.1\", and check the correctness ", 513*cdf0e10cSrcweir sal_True == bOK ); 514*cdf0e10cSrcweir } 515*cdf0e10cSrcweir 516*cdf0e10cSrcweir 517*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( setAddr ); 518*cdf0e10cSrcweir CPPUNIT_TEST( setAddr_001 ); 519*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( ); 520*cdf0e10cSrcweir 521*cdf0e10cSrcweir }; // class setAddr 522*cdf0e10cSrcweir 523*cdf0e10cSrcweir 524*cdf0e10cSrcweir /** testing the method: 525*cdf0e10cSrcweir inline ::rtl::ByteSequence SAL_CALL getAddr( oslSocketResult *pResult = 0 ) const; 526*cdf0e10cSrcweir */ 527*cdf0e10cSrcweir 528*cdf0e10cSrcweir class getAddr : public CppUnit::TestFixture 529*cdf0e10cSrcweir { 530*cdf0e10cSrcweir public: 531*cdf0e10cSrcweir void getAddr_001() 532*cdf0e10cSrcweir { 533*cdf0e10cSrcweir oslSocketResult SocketResult; 534*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP ); 535*cdf0e10cSrcweir ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( &SocketResult ); 536*cdf0e10cSrcweir 537*cdf0e10cSrcweir sal_Bool bOK = sal_False; 538*cdf0e10cSrcweir 539*cdf0e10cSrcweir //if ( ( osl_Socket_Ok == SocketResult ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) 540*cdf0e10cSrcweir // bOK = sal_True; 541*cdf0e10cSrcweir bOK = ifIpv4is( bsSocketAddr, 127, 0, 0, 1 ); 542*cdf0e10cSrcweir 543*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getAddr() function: construct a socketaddr with IP assigned, get the address to check correctness.Caught unknown exception on (Win32)", 544*cdf0e10cSrcweir sal_True == bOK && SocketResult == osl_Socket_Ok); 545*cdf0e10cSrcweir } 546*cdf0e10cSrcweir 547*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getAddr ); 548*cdf0e10cSrcweir CPPUNIT_TEST( getAddr_001 ); 549*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( ); 550*cdf0e10cSrcweir 551*cdf0e10cSrcweir }; // class getAddr 552*cdf0e10cSrcweir 553*cdf0e10cSrcweir 554*cdf0e10cSrcweir /** testing the methods: 555*cdf0e10cSrcweir inline SocketAddr & SAL_CALL operator= (oslSocketAddr Addr); 556*cdf0e10cSrcweir inline SocketAddr & SAL_CALL operator= (const SocketAddr& Addr); 557*cdf0e10cSrcweir inline SocketAddr & SAL_CALL assign( oslSocketAddr Addr, __osl_socket_NoCopy nocopy ); 558*cdf0e10cSrcweir inline sal_Bool SAL_CALL operator== (oslSocketAddr Addr) const; 559*cdf0e10cSrcweir inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const; /// not implemented. 560*cdf0e10cSrcweir */ 561*cdf0e10cSrcweir 562*cdf0e10cSrcweir class operator_equal : public CppUnit::TestFixture 563*cdf0e10cSrcweir { 564*cdf0e10cSrcweir public: 565*cdf0e10cSrcweir void operator_equal_001() 566*cdf0e10cSrcweir { 567*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET); 568*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrEqual( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 569*cdf0e10cSrcweir 570*cdf0e10cSrcweir saSocketAddrEqual = saSocketAddr; 571*cdf0e10cSrcweir sal_Bool bOK = sal_False; 572*cdf0e10cSrcweir ::rtl::ByteSequence bsSocketAddr = saSocketAddrEqual.getAddr( 0 ); 573*cdf0e10cSrcweir 574*cdf0e10cSrcweir // if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) 575*cdf0e10cSrcweir if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) && ( ifIpv4is( bsSocketAddr, 127, 0, 0, 1 ) == sal_True ) ) 576*cdf0e10cSrcweir bOK = sal_True; 577*cdf0e10cSrcweir 578*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for operator_equal() function: use operator= to assign Ip1 to Ip2, check its modification.", 579*cdf0e10cSrcweir sal_True == bOK ); 580*cdf0e10cSrcweir } 581*cdf0e10cSrcweir 582*cdf0e10cSrcweir 583*cdf0e10cSrcweir void operator_equal_002() 584*cdf0e10cSrcweir { 585*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.199"), IP_PORT_TELNET); 586*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrEqual( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 587*cdf0e10cSrcweir 588*cdf0e10cSrcweir saSocketAddrEqual = saSocketAddr; 589*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "after assign, the assigned SocketAddr is not same as the original Addr", 590*cdf0e10cSrcweir IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ); 591*cdf0e10cSrcweir saSocketAddrEqual.setPort( IP_PORT_MYPORT3 ); 592*cdf0e10cSrcweir saSocketAddr.setPort( IP_PORT_HTTP2 ); 593*cdf0e10cSrcweir 594*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "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.", 595*cdf0e10cSrcweir IP_PORT_MYPORT3 == saSocketAddrEqual.getPort( ) ); 596*cdf0e10cSrcweir } 597*cdf0e10cSrcweir 598*cdf0e10cSrcweir void operator_equal_const_001() 599*cdf0e10cSrcweir { 600*cdf0e10cSrcweir const ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET); 601*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrEqual( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 602*cdf0e10cSrcweir 603*cdf0e10cSrcweir saSocketAddrEqual = saSocketAddr; 604*cdf0e10cSrcweir sal_Bool bOK = sal_False; 605*cdf0e10cSrcweir ::rtl::ByteSequence bsSocketAddr = saSocketAddrEqual.getAddr( 0 ); 606*cdf0e10cSrcweir 607*cdf0e10cSrcweir // if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) &&( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) 608*cdf0e10cSrcweir if ( ( IP_PORT_TELNET == saSocketAddrEqual.getPort( ) ) && ifIpv4is( bsSocketAddr, 127, 0, 0, 1 ) == sal_True ) 609*cdf0e10cSrcweir bOK = sal_True; 610*cdf0e10cSrcweir 611*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_const() function: use operator= const to assign Ip1 to Ip2, verify the change on the second one.", 612*cdf0e10cSrcweir sal_True == bOK ); 613*cdf0e10cSrcweir } 614*cdf0e10cSrcweir 615*cdf0e10cSrcweir void operator_equal_const_002() 616*cdf0e10cSrcweir { 617*cdf0e10cSrcweir const ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET); 618*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrEqual( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 619*cdf0e10cSrcweir 620*cdf0e10cSrcweir saSocketAddrEqual = saSocketAddr; 621*cdf0e10cSrcweir saSocketAddrEqual.setPort( IP_PORT_HTTP1 ); 622*cdf0e10cSrcweir 623*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_const() function: change the second instance, the first one should not be altered, since it does not released the handle.", 624*cdf0e10cSrcweir IP_PORT_HTTP1 != saSocketAddr.getPort( ) ); 625*cdf0e10cSrcweir } 626*cdf0e10cSrcweir 627*cdf0e10cSrcweir void operator_equal_assign_001() 628*cdf0e10cSrcweir { 629*cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET ); 630*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL); 631*cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddrAssign = new ::osl::SocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP ); 632*cdf0e10cSrcweir oslSocketAddr poslSocketAddr = pSocketAddr->getHandle( ); 633*cdf0e10cSrcweir //if( m_handle ) osl_destroySocketAddr( m_handle ); so pSocketAddrAssign had been destroyed and then point to pSocketAddr 634*cdf0e10cSrcweir pSocketAddrAssign->assign(poslSocketAddr, SAL_NO_COPY); 635*cdf0e10cSrcweir 636*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("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.", 637*cdf0e10cSrcweir pSocketAddrAssign->getPort( ) == IP_PORT_TELNET ); 638*cdf0e10cSrcweir 639*cdf0e10cSrcweir delete pSocketAddrAssign; 640*cdf0e10cSrcweir } 641*cdf0e10cSrcweir 642*cdf0e10cSrcweir void operator_is_equal_001() 643*cdf0e10cSrcweir { 644*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET); 645*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrequal( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET ); 646*cdf0e10cSrcweir 647*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_equal() function: check two identical Address.", 648*cdf0e10cSrcweir sal_True == ( saSocketAddrequal == saSocketAddr.getHandle( ) ) ); 649*cdf0e10cSrcweir } 650*cdf0e10cSrcweir 651*cdf0e10cSrcweir void operator_is_equal_002() 652*cdf0e10cSrcweir { 653*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP); 654*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddrequal( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_TELNET ); 655*cdf0e10cSrcweir 656*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for operator_equal_equal() function: check two different Address.", 657*cdf0e10cSrcweir sal_False == ( saSocketAddrequal == saSocketAddr.getHandle( ) ) ); 658*cdf0e10cSrcweir } 659*cdf0e10cSrcweir 660*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( operator_equal ); 661*cdf0e10cSrcweir CPPUNIT_TEST( operator_equal_001 ); 662*cdf0e10cSrcweir CPPUNIT_TEST( operator_equal_002 ); 663*cdf0e10cSrcweir CPPUNIT_TEST( operator_equal_const_001 ); 664*cdf0e10cSrcweir CPPUNIT_TEST( operator_equal_const_002 ); 665*cdf0e10cSrcweir CPPUNIT_TEST( operator_equal_assign_001 ); 666*cdf0e10cSrcweir CPPUNIT_TEST( operator_is_equal_001 ); 667*cdf0e10cSrcweir CPPUNIT_TEST( operator_is_equal_002 ); 668*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( ); 669*cdf0e10cSrcweir 670*cdf0e10cSrcweir }; // class operator_equal 671*cdf0e10cSrcweir 672*cdf0e10cSrcweir 673*cdf0e10cSrcweir 674*cdf0e10cSrcweir /** testing the method: 675*cdf0e10cSrcweir inline oslSocketAddr SAL_CALL getHandle() const; 676*cdf0e10cSrcweir */ 677*cdf0e10cSrcweir 678*cdf0e10cSrcweir class getSocketAddrHandle : public CppUnit::TestFixture 679*cdf0e10cSrcweir { 680*cdf0e10cSrcweir public: 681*cdf0e10cSrcweir 682*cdf0e10cSrcweir void getSocketAddrHandle_001() 683*cdf0e10cSrcweir { 684*cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddr = new ::osl::SocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); 685*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("check for new SocketAddr", pSocketAddr != NULL); 686*cdf0e10cSrcweir oslSocketAddr psaOSLSocketAddr = pSocketAddr->getHandle( ); 687*cdf0e10cSrcweir ::osl::SocketAddr* pSocketAddrCopy = new ::osl::SocketAddr( psaOSLSocketAddr, SAL_NO_COPY ); 688*cdf0e10cSrcweir 689*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("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.", 690*cdf0e10cSrcweir pSocketAddr->getHandle( ) == pSocketAddrCopy->getHandle( ) ); 691*cdf0e10cSrcweir 692*cdf0e10cSrcweir delete pSocketAddrCopy; 693*cdf0e10cSrcweir } 694*cdf0e10cSrcweir 695*cdf0e10cSrcweir void getSocketAddrHandle_002() 696*cdf0e10cSrcweir { 697*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("deuce.PRC.Sun.COM"), IP_PORT_MYPORT4 ); 698*cdf0e10cSrcweir oslSocketAddr poslSocketAddr = saSocketAddr.getHandle( ); 699*cdf0e10cSrcweir 700*cdf0e10cSrcweir sal_Bool bOK = ( saSocketAddr == poslSocketAddr ); 701*cdf0e10cSrcweir //t_print("getSocketAddrHandle_002\n"); 702*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getHandle() function: use getHandle() function as an intermediate way to create identical address.", 703*cdf0e10cSrcweir sal_True == bOK ); 704*cdf0e10cSrcweir } 705*cdf0e10cSrcweir 706*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getSocketAddrHandle ); 707*cdf0e10cSrcweir CPPUNIT_TEST( getSocketAddrHandle_001 ); 708*cdf0e10cSrcweir CPPUNIT_TEST( getSocketAddrHandle_002 ); 709*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( ); 710*cdf0e10cSrcweir 711*cdf0e10cSrcweir }; // class getSocketAddrHandle 712*cdf0e10cSrcweir 713*cdf0e10cSrcweir 714*cdf0e10cSrcweir /** testing the method: 715*cdf0e10cSrcweir static inline ::rtl::OUString SAL_CALL getLocalHostname( oslSocketResult *pResult = 0); 716*cdf0e10cSrcweir */ 717*cdf0e10cSrcweir 718*cdf0e10cSrcweir class getLocalHostname : public CppUnit::TestFixture 719*cdf0e10cSrcweir { 720*cdf0e10cSrcweir public: 721*cdf0e10cSrcweir /* the process of getLocalHostname: 1.gethostname (same as /bin/hostname) returned name A 722*cdf0e10cSrcweir 2. search A in /etc/hosts, if there is an alias name is A, return the name in the same row 723*cdf0e10cSrcweir */ 724*cdf0e10cSrcweir 725*cdf0e10cSrcweir void getLocalHostname_000() 726*cdf0e10cSrcweir { 727*cdf0e10cSrcweir // _osl_getFullQualifiedDomainName( ); 728*cdf0e10cSrcweir oslSocketResult aResult = osl_Socket_Error; 729*cdf0e10cSrcweir rtl::OUString suHostname = osl::SocketAddr::getLocalHostname(&aResult); 730*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("getLocalHostname failed", aResult == osl_Socket_Ok); 731*cdf0e10cSrcweir } 732*cdf0e10cSrcweir 733*cdf0e10cSrcweir void getLocalHostname_001() 734*cdf0e10cSrcweir { 735*cdf0e10cSrcweir oslSocketResult *pResult = NULL; 736*cdf0e10cSrcweir //printSocketResult(*pResult); 737*cdf0e10cSrcweir ::rtl::OUString suResult = ::osl::SocketAddr::getLocalHostname( pResult ); 738*cdf0e10cSrcweir 739*cdf0e10cSrcweir // LLA: IMHO localhost, or hostname by itself should be ok. 740*cdf0e10cSrcweir rtl::OUString suThisHost = getThisHostname( ); 741*cdf0e10cSrcweir bool bOk = false; 742*cdf0e10cSrcweir if (suThisHost.equals(rtl::OUString::createFromAscii("localhost"))) 743*cdf0e10cSrcweir { 744*cdf0e10cSrcweir bOk = true; 745*cdf0e10cSrcweir } 746*cdf0e10cSrcweir else 747*cdf0e10cSrcweir { 748*cdf0e10cSrcweir if (suThisHost.equals(suResult)) 749*cdf0e10cSrcweir { 750*cdf0e10cSrcweir bOk = true; 751*cdf0e10cSrcweir } 752*cdf0e10cSrcweir } 753*cdf0e10cSrcweir 754*cdf0e10cSrcweir ::rtl::OUString suError; 755*cdf0e10cSrcweir suError = outputError(suResult, getThisHostname( ), "test for getLocalHostname() function"); 756*cdf0e10cSrcweir 757*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( suError, bOk == true ); 758*cdf0e10cSrcweir } 759*cdf0e10cSrcweir 760*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getLocalHostname ); 761*cdf0e10cSrcweir CPPUNIT_TEST( getLocalHostname_000 ); 762*cdf0e10cSrcweir CPPUNIT_TEST( getLocalHostname_001 ); 763*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( ); 764*cdf0e10cSrcweir 765*cdf0e10cSrcweir }; // class getLocalHostname 766*cdf0e10cSrcweir 767*cdf0e10cSrcweir 768*cdf0e10cSrcweir /** testing the method: 769*cdf0e10cSrcweir static inline void SAL_CALL resolveHostname( const ::rtl::OUString & strHostName , SocketAddr & Addr ); 770*cdf0e10cSrcweir */ 771*cdf0e10cSrcweir 772*cdf0e10cSrcweir class resolveHostname : public CppUnit::TestFixture 773*cdf0e10cSrcweir { 774*cdf0e10cSrcweir public: 775*cdf0e10cSrcweir void resolveHostname_001() 776*cdf0e10cSrcweir { 777*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr; 778*cdf0e10cSrcweir ::osl::SocketAddr::resolveHostname( rtl::OUString::createFromAscii("127.0.0.1"), saSocketAddr ); 779*cdf0e10cSrcweir ::rtl::ByteSequence bsSocketAddr = saSocketAddr.getAddr( 0 ); 780*cdf0e10cSrcweir sal_Bool bOK = sal_False; 781*cdf0e10cSrcweir 782*cdf0e10cSrcweir if ( ( bsSocketAddr[0] == 127 ) && ( bsSocketAddr[1] == 0 ) &&( bsSocketAddr[2] == 0 ) && ( bsSocketAddr[3] == 1 ) ) 783*cdf0e10cSrcweir bOK = sal_True; 784*cdf0e10cSrcweir 785*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for resolveHostname() function: try to resolve localhost to 127.0.0.1.", 786*cdf0e10cSrcweir sal_True == bOK ); 787*cdf0e10cSrcweir } 788*cdf0e10cSrcweir 789*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( resolveHostname ); 790*cdf0e10cSrcweir CPPUNIT_TEST( resolveHostname_001 ); 791*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( ); 792*cdf0e10cSrcweir 793*cdf0e10cSrcweir }; // class resolveHostname 794*cdf0e10cSrcweir 795*cdf0e10cSrcweir 796*cdf0e10cSrcweir /** testing the method: 797*cdf0e10cSrcweir static inline sal_Int32 SAL_CALL getServicePort( 798*cdf0e10cSrcweir const ::rtl::OUString& strServiceName, 799*cdf0e10cSrcweir const ::rtl::OUString & strProtocolName= ::rtl::OUString::createFromAscii( "tcp" ) ); 800*cdf0e10cSrcweir */ 801*cdf0e10cSrcweir 802*cdf0e10cSrcweir class gettheServicePort : public CppUnit::TestFixture 803*cdf0e10cSrcweir { 804*cdf0e10cSrcweir public: 805*cdf0e10cSrcweir void gettheServicePort_001() 806*cdf0e10cSrcweir { 807*cdf0e10cSrcweir rtl::OUString suServiceFTP = rtl::OUString::createFromAscii( "ftp" ); 808*cdf0e10cSrcweir rtl::OUString suProtocolTCP = rtl::OUString::createFromAscii( "tcp" ); 809*cdf0e10cSrcweir 810*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get ftp service port on TCP protocol.", 811*cdf0e10cSrcweir IP_PORT_FTP== ::osl::SocketAddr::getServicePort( suServiceFTP, suProtocolTCP ) ); 812*cdf0e10cSrcweir } 813*cdf0e10cSrcweir 814*cdf0e10cSrcweir void gettheServicePort_002() 815*cdf0e10cSrcweir { 816*cdf0e10cSrcweir rtl::OUString suServiceTELNET = rtl::OUString::createFromAscii( "telnet" ); 817*cdf0e10cSrcweir rtl::OUString suProtocolTCP = rtl::OUString::createFromAscii( "tcp" ); 818*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get telnet service port on TCP protocol.", 819*cdf0e10cSrcweir IP_PORT_TELNET== ::osl::SocketAddr::getServicePort( suServiceTELNET, suProtocolTCP ) ); 820*cdf0e10cSrcweir } 821*cdf0e10cSrcweir 822*cdf0e10cSrcweir void gettheServicePort_003() 823*cdf0e10cSrcweir { 824*cdf0e10cSrcweir //Solaris has no service called "https", please see /etc/services 825*cdf0e10cSrcweir rtl::OUString suServiceNETBIOS = rtl::OUString::createFromAscii( "netbios-dgm" ); 826*cdf0e10cSrcweir rtl::OUString suProtocolUDP = rtl::OUString::createFromAscii( "udp" ); 827*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get netbios-ssn service port on UDP protocol.", 828*cdf0e10cSrcweir IP_PORT_NETBIOS_DGM == ::osl::SocketAddr::getServicePort( suServiceNETBIOS, suProtocolUDP ) ); 829*cdf0e10cSrcweir } 830*cdf0e10cSrcweir 831*cdf0e10cSrcweir void gettheServicePort_004() 832*cdf0e10cSrcweir { 833*cdf0e10cSrcweir rtl::OUString suProtocolUDP = rtl::OUString::createFromAscii( "udp" ); 834*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for getServicePort() function: try to get a service port which is not exist.", 835*cdf0e10cSrcweir OSL_INVALID_PORT == ::osl::SocketAddr::getServicePort( ::rtl::OUString::createFromAscii( "notexist" ), suProtocolUDP ) ); 836*cdf0e10cSrcweir } 837*cdf0e10cSrcweir 838*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( gettheServicePort ); 839*cdf0e10cSrcweir CPPUNIT_TEST( gettheServicePort_001 ); 840*cdf0e10cSrcweir CPPUNIT_TEST( gettheServicePort_002 ); 841*cdf0e10cSrcweir CPPUNIT_TEST( gettheServicePort_003 ); 842*cdf0e10cSrcweir CPPUNIT_TEST( gettheServicePort_004 ); 843*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( ); 844*cdf0e10cSrcweir 845*cdf0e10cSrcweir }; // class gettheServicePort 846*cdf0e10cSrcweir 847*cdf0e10cSrcweir /** testing the method: 848*cdf0e10cSrcweir 849*cdf0e10cSrcweir */ 850*cdf0e10cSrcweir 851*cdf0e10cSrcweir class getFamilyOfSocketAddr : public CppUnit::TestFixture 852*cdf0e10cSrcweir { 853*cdf0e10cSrcweir public: 854*cdf0e10cSrcweir void getFamilyOfSocketAddr_001() 855*cdf0e10cSrcweir { 856*cdf0e10cSrcweir ::osl::SocketAddr saSocketAddr( rtl::OUString::createFromAscii("localhost"), IP_PORT_HTTP1 ); 857*cdf0e10cSrcweir oslSocketAddr psaOSLSocketAddr = saSocketAddr.getHandle( ); 858*cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL( 859*cdf0e10cSrcweir osl_Socket_FamilyInet, 860*cdf0e10cSrcweir osl_getFamilyOfSocketAddr( psaOSLSocketAddr ) ); 861*cdf0e10cSrcweir 862*cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( "test for osl_getFamilyOfSocketAddr.", 863*cdf0e10cSrcweir osl_getFamilyOfSocketAddr( psaOSLSocketAddr ) == osl_Socket_FamilyInet ); 864*cdf0e10cSrcweir } 865*cdf0e10cSrcweir 866*cdf0e10cSrcweir CPPUNIT_TEST_SUITE( getFamilyOfSocketAddr ); 867*cdf0e10cSrcweir CPPUNIT_TEST( getFamilyOfSocketAddr_001 ); 868*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END( ); 869*cdf0e10cSrcweir 870*cdf0e10cSrcweir }; // class getFamilyOfSocketAddr 871*cdf0e10cSrcweir 872*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 873*cdf0e10cSrcweir 874*cdf0e10cSrcweir 875*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::ctors, "osl_SocketAddr"); 876*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::is, "osl_SocketAddr"); 877*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getHostname, "osl_SocketAddr"); 878*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getPort, "osl_SocketAddr"); 879*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::setPort, "osl_SocketAddr"); 880*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::setAddr, "osl_SocketAddr"); 881*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getAddr, "osl_SocketAddr"); 882*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::operator_equal, "osl_SocketAddr"); 883*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getSocketAddrHandle, "osl_SocketAddr"); 884*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getLocalHostname, "osl_SocketAddr"); 885*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::resolveHostname, "osl_SocketAddr"); 886*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::gettheServicePort, "osl_SocketAddr"); 887*cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_SocketAddr::getFamilyOfSocketAddr, "osl_SocketAddr"); 888*cdf0e10cSrcweir 889*cdf0e10cSrcweir } // namespace osl_SocketAddr 890*cdf0e10cSrcweir 891*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 892*cdf0e10cSrcweir 893*cdf0e10cSrcweir // this macro creates an empty function, which will called by the RegisterAllFunctions() 894*cdf0e10cSrcweir // to let the user the possibility to also register some functions by hand. 895*cdf0e10cSrcweir NOADDITIONAL; 896