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