osl_Socket2.cxx (86e1cf34) osl_Socket2.cxx (63d99982)
1/**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance

--- 40 unchanged lines hidden (view full) ---

49 4. following functions are declared but not implemented:
50 inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const;
51 */
52
53//------------------------------------------------------------------------
54// include files
55//------------------------------------------------------------------------
56
1/**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance

--- 40 unchanged lines hidden (view full) ---

49 4. following functions are declared but not implemented:
50 inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const;
51 */
52
53//------------------------------------------------------------------------
54// include files
55//------------------------------------------------------------------------
56
57#include <testshl/simpleheader.hxx>
57#include "gtest/gtest.h"
58
59//#include "osl_Socket_Const.h"
60#include "sockethelper.hxx"
61
62using namespace osl;
63using namespace rtl;
64
65#define IP_PORT_FTP 21

--- 25 unchanged lines hidden (view full) ---

91 {
92 ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 );
93 ::osl::StreamSocket ssStreamConnection;
94
95 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
96 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
97 if ( sal_True != bOK1 )
98 {
58
59//#include "osl_Socket_Const.h"
60#include "sockethelper.hxx"
61
62using namespace osl;
63using namespace rtl;
64
65#define IP_PORT_FTP 21

--- 25 unchanged lines hidden (view full) ---

91 {
92 ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 );
93 ::osl::StreamSocket ssStreamConnection;
94
95 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
96 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
97 if ( sal_True != bOK1 )
98 {
99 t_print("# AcceptorSocket bind address failed.\n" ) ;
99 printf("# AcceptorSocket bind address failed.\n" ) ;
100 return;
101 }
102 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
103 if ( sal_True != bOK2 )
104 {
100 return;
101 }
102 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
103 if ( sal_True != bOK2 )
104 {
105 t_print("# AcceptorSocket listen address failed.\n" ) ;
105 printf("# AcceptorSocket listen address failed.\n" ) ;
106 return;
107 }
108
109 asAcceptorSocket.enableNonBlockingMode( sal_False );
110
111 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
112 if (eResult != osl_Socket_Ok )
113 {
114 bOK = sal_True;
106 return;
107 }
108
109 asAcceptorSocket.enableNonBlockingMode( sal_False );
110
111 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
112 if (eResult != osl_Socket_Ok )
113 {
114 bOK = sal_True;
115 t_print("AcceptorThread: acceptConnection failed! \n");
115 printf("AcceptorThread: acceptConnection failed! \n");
116 }
117 }
118public:
119 AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString const& aBindIP )
120 : asAcceptorSocket( asSocket ), aHostIP( aBindIP )
121 {
122 bOK = sal_False;
123 }
124
125 sal_Bool isOK() { return bOK; }
126
127 ~AcceptorThread( )
128 {
129 if ( isRunning( ) )
130 {
131 asAcceptorSocket.shutdown();
116 }
117 }
118public:
119 AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString const& aBindIP )
120 : asAcceptorSocket( asSocket ), aHostIP( aBindIP )
121 {
122 bOK = sal_False;
123 }
124
125 sal_Bool isOK() { return bOK; }
126
127 ~AcceptorThread( )
128 {
129 if ( isRunning( ) )
130 {
131 asAcceptorSocket.shutdown();
132 t_print("# error: Acceptor thread not terminated.\n" );
132 printf("# error: Acceptor thread not terminated.\n" );
133 }
134 }
135};
136
137namespace osl_Socket
138{
139
140 /** testing the methods:

--- 9 unchanged lines hidden (view full) ---

150 constructor is the most convenient way to create a new socket.
151 it only allow the method of C function osl_createSocket like:
152 ::osl::Socket sSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream,
153 osl_Socket_ProtocolIp ) );
154 the use of C method lost some of the transparent of tester using C++ wrapper.
155 */
156
157
133 }
134 }
135};
136
137namespace osl_Socket
138{
139
140 /** testing the methods:

--- 9 unchanged lines hidden (view full) ---

150 constructor is the most convenient way to create a new socket.
151 it only allow the method of C function osl_createSocket like:
152 ::osl::Socket sSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream,
153 osl_Socket_ProtocolIp ) );
154 the use of C method lost some of the transparent of tester using C++ wrapper.
155 */
156
157
158 class ctors : public CppUnit::TestFixture
158 class ctors : public ::testing::Test
159 {
160 public:
161 oslSocket sHandle;
162 // initialization
159 {
160 public:
161 oslSocket sHandle;
162 // initialization
163 void setUp( )
163 void SetUp( )
164 {
165 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
166 }
167
164 {
165 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
166 }
167
168 void tearDown( )
168 void TearDown( )
169 {
170 sHandle = NULL;
171 }
169 {
170 sHandle = NULL;
171 }
172 }; // class ctors
172
173
174 TEST_F(ctors, ctors_none)
175 {
176 /// Socket constructor.
177 // ::osl::Socket sSocket();
173
178
174 void ctors_none()
175 {
176 /// Socket constructor.
177 // ::osl::Socket sSocket();
179 ASSERT_TRUE(1 == 1) << "test for ctors_none constructor function: check if the socket was created successfully, if no exception occurred";
180 }
178
181
179 CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the socket was created successfully, if no exception occurred",
180 1 == 1 );
181 }
182 TEST_F(ctors, ctors_acquire)
183 {
184 /// Socket constructor.
185 ::osl::Socket sSocket( sHandle );
182
186
183 void ctors_acquire()
184 {
185 /// Socket constructor.
186 ::osl::Socket sSocket( sHandle );
187 ASSERT_TRUE(osl_Socket_TypeStream == sSocket.getType( )) << "test for ctors_acquire constructor function: check if the socket was created successfully";
188 }
187
189
188 CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully",
189 osl_Socket_TypeStream == sSocket.getType( ) );
190 }
190 TEST_F(ctors, ctors_no_acquire)
191 {
192 /// Socket constructor.
193 ::osl::Socket sSocket( sHandle, SAL_NO_ACQUIRE );
191
194
192 void ctors_no_acquire()
193 {
194 /// Socket constructor.
195 ::osl::Socket sSocket( sHandle, SAL_NO_ACQUIRE );
195 ASSERT_TRUE(osl_Socket_TypeStream == sSocket.getType( )) << " test for ctors_no_acquire constructor function: check if the socket was created successfully";
196 }
196
197
197 CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully",
198 osl_Socket_TypeStream == sSocket.getType( ) );
199 }
198 TEST_F(ctors, ctors_copy_ctor)
199 {
200 ::osl::Socket sSocket( sHandle );
201 /// Socket copy constructor.
202 ::osl::Socket copySocket( sSocket );
200
203
201 void ctors_copy_ctor()
202 {
203 ::osl::Socket sSocket( sHandle );
204 /// Socket copy constructor.
205 ::osl::Socket copySocket( sSocket );
204 ASSERT_TRUE(osl_Socket_TypeStream == copySocket.getType( )) << " test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor";
205 }
206
206
207 CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor",
208 osl_Socket_TypeStream == copySocket.getType( ) );
209 }
210
211 void ctors_TypeRaw()
212 {
207 TEST_F(ctors, ctors_TypeRaw)
208 {
213#ifdef WNT
209#ifdef WNT
214 oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
215// LLA: ? ::osl::Socket sSocket( sHandleRaw );
216 CPPUNIT_ASSERT_MESSAGE( " type osl_Socket_TypeRaw socket create failed on UNX ", sHandleRaw != NULL);
210 oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
211// LLA: ? ::osl::Socket sSocket( sHandleRaw );
212 ASSERT_TRUE(sHandleRaw != NULL) << " type osl_Socket_TypeRaw socket create failed on UNX ";
217#else
213#else
218 oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
219 CPPUNIT_ASSERT_MESSAGE( " can't create socket with type osl_Socket_TypeRaw within UNX is ok.", sHandleRaw == NULL);
214 oslSocket sHandleRaw = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
215 ASSERT_TRUE(sHandleRaw == NULL) << " can't create socket with type osl_Socket_TypeRaw within UNX is ok.";
220#endif
216#endif
221 }
217 }
222
218
223 void ctors_family_Ipx()
224 {
225 oslSocket sHandleIpx = osl_createSocket( osl_Socket_FamilyIpx, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
226 CPPUNIT_ASSERT_MESSAGE( " family osl_Socket_FamilyIpx socket create failed! ", sHandleIpx != NULL);
227 ::osl::Socket sSocket( sHandleIpx ); //, SAL_NO_ACQUIRE );
228 t_print("#Type is %d \n", sSocket.getType( ) );
219 TEST_F(ctors, ctors_family_Ipx)
220 {
221 oslSocket sHandleIpx = osl_createSocket( osl_Socket_FamilyIpx, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
222 ASSERT_TRUE(sHandleIpx != NULL) << " family osl_Socket_FamilyIpx socket create failed! ";
223 ::osl::Socket sSocket( sHandleIpx ); //, SAL_NO_ACQUIRE );
224 printf("#Type is %d \n", sSocket.getType( ) );
229
225
230 CPPUNIT_ASSERT_MESSAGE(" test for create new Socket instance that family is osl_Socket_FamilyIpx",
231 osl_Socket_TypeStream == sSocket.getType( ) );
232 }
226 ASSERT_TRUE(osl_Socket_TypeStream == sSocket.getType( )) << " test for create new Socket instance that family is osl_Socket_FamilyIpx";
227 }
233
228
234
235
236 CPPUNIT_TEST_SUITE( ctors );
237 CPPUNIT_TEST( ctors_none );
238 CPPUNIT_TEST( ctors_acquire );
239 CPPUNIT_TEST( ctors_no_acquire );
240 CPPUNIT_TEST( ctors_copy_ctor );
241 CPPUNIT_TEST( ctors_TypeRaw );
242 CPPUNIT_TEST( ctors_family_Ipx );
243 CPPUNIT_TEST_SUITE_END();
244
245 }; // class ctors
246
247
248 /** testing the methods:
249 inline Socket& SAL_CALL operator= ( oslSocket socketHandle);
250 inline Socket& SAL_CALL operator= (const Socket& sock);
251 inline sal_Bool SAL_CALL operator==( const Socket& rSocket ) const ;
252 inline sal_Bool SAL_CALL operator==( const oslSocket socketHandle ) const;
253 */
254
229 /** testing the methods:
230 inline Socket& SAL_CALL operator= ( oslSocket socketHandle);
231 inline Socket& SAL_CALL operator= (const Socket& sock);
232 inline sal_Bool SAL_CALL operator==( const Socket& rSocket ) const ;
233 inline sal_Bool SAL_CALL operator==( const oslSocket socketHandle ) const;
234 */
235
255 class operators : public CppUnit::TestFixture
236 class operators : public ::testing::Test
256 {
257 public:
258 oslSocket sHandle;
259 // initialization
237 {
238 public:
239 oslSocket sHandle;
240 // initialization
260 void setUp( )
241 void SetUp( )
261 {
262 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
263 }
264
242 {
243 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
244 }
245
265 void tearDown( )
246 void TearDown( )
266 {
267 sHandle = NULL;
268 }
247 {
248 sHandle = NULL;
249 }
250 }; // class operators
269
251
270
271 /** test writer's comment:
272
252 /** test writer's comment:
253
273 the assignment operator does not support direct assinment like:
274 ::osl::Socket sSocket = sHandle.
254 the assignment operator does not support direct assinment like:
255 ::osl::Socket sSocket = sHandle.
275 */
256 */
276 void operators_assignment_handle()
277 {
278 ::osl::Socket sSocket(sHandle);
279 ::osl::Socket assignSocket = sSocket.getHandle();
257 TEST_F(operators, operators_assignment_handle)
258 {
259 ::osl::Socket sSocket(sHandle);
260 ::osl::Socket assignSocket = sSocket.getHandle();
280
261
281 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.",
282 osl_Socket_TypeStream == assignSocket.getType( ) );
283 }
262 ASSERT_TRUE(osl_Socket_TypeStream == assignSocket.getType( )) << "test for operators_assignment_handle function: test the assignment operator.";
263 }
284
264
285 void operators_assignment()
286 {
287 ::osl::Socket sSocket( sHandle );
288 ::osl::Socket assignSocket = sSocket;
265 TEST_F(operators, operators_assignment)
266 {
267 ::osl::Socket sSocket( sHandle );
268 ::osl::Socket assignSocket = sSocket;
289
269
290 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator",
291 osl_Socket_TypeStream == assignSocket.getType( ) );
292 }
270 ASSERT_TRUE(osl_Socket_TypeStream == assignSocket.getType( )) << "test for operators_assignment function: assignment operator";
271 }
293
272
294 void operators_equal_handle_001()
295 {
296 /// Socket constructor.
297 ::osl::Socket sSocket( sHandle );
298 ::osl::Socket equalSocket = sSocket;
273 TEST_F(operators, operators_equal_handle_001)
274 {
275 /// Socket constructor.
276 ::osl::Socket sSocket( sHandle );
277 ::osl::Socket equalSocket = sSocket;
299
278
300 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check equal.",
301 equalSocket == sHandle );
302 }
279 ASSERT_TRUE(equalSocket == sHandle) << " test for operators_equal_handle_001 function: check equal.";
280 }
303
281
304 void operators_equal_handle_002()
305 {
306 /// Socket constructor.
307 ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) );
282 TEST_F(operators, operators_equal_handle_002)
283 {
284 /// Socket constructor.
285 ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) );
308
286
309 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_handle_001 function: check unequal.",
310 !( equalSocket == sHandle ) );
311 }
287 ASSERT_TRUE(!( equalSocket == sHandle )) << " test for operators_equal_handle_001 function: check unequal.";
288 }
312
289
313 void operators_equal_001()
314 {
315 ::osl::Socket sSocket( sHandle );
316 /// Socket copy constructor.
317 ::osl::Socket equalSocket( sSocket );
290 TEST_F(operators, operators_equal_001)
291 {
292 ::osl::Socket sSocket( sHandle );
293 /// Socket copy constructor.
294 ::osl::Socket equalSocket( sSocket );
318
295
319 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal function: check equal.",
320 equalSocket == sSocket );
321 }
296 ASSERT_TRUE(equalSocket == sSocket) << " test for operators_equal function: check equal.";
297 }
322
298
323 void operators_equal_002()
324 {
325 ::osl::Socket sSocket( sHandle );
326 /// Socket copy constructor.
327 ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) );
299 TEST_F(operators, operators_equal_002)
300 {
301 ::osl::Socket sSocket( sHandle );
302 /// Socket copy constructor.
303 ::osl::Socket equalSocket( osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp ) );
328
304
329 CPPUNIT_ASSERT_MESSAGE(" test for operators_equal_002 function: check unequal.",
330 !( equalSocket == sSocket ) );
331 }
305 ASSERT_TRUE(!( equalSocket == sSocket )) << " test for operators_equal_002 function: check unequal.";
306 }
332
307
333 CPPUNIT_TEST_SUITE( operators );
334 CPPUNIT_TEST( operators_assignment_handle );
335 CPPUNIT_TEST( operators_assignment );
336 CPPUNIT_TEST( operators_equal_handle_001 );
337 CPPUNIT_TEST( operators_equal_handle_002 );
338 CPPUNIT_TEST( operators_equal_001 );
339 CPPUNIT_TEST( operators_equal_002 );
340 CPPUNIT_TEST_SUITE_END();
341
342 }; // class operators
343
344
345 /** testing the methods:
346 inline void SAL_CALL shutdown( oslSocketDirection Direction = osl_Socket_DirReadWrite );
347 inline void SAL_CALL close();
348 */
349
308 /** testing the methods:
309 inline void SAL_CALL shutdown( oslSocketDirection Direction = osl_Socket_DirReadWrite );
310 inline void SAL_CALL close();
311 */
312
350 class close : public CppUnit::TestFixture
313 class close : public ::testing::Test
351 {
352 public:
353 oslSocket sHandle;
354 // initialization
314 {
315 public:
316 oslSocket sHandle;
317 // initialization
355 void setUp( )
318 void SetUp( )
356 {
357 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
358 }
359
319 {
320 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
321 }
322
360 void tearDown( )
323 void TearDown( )
361 {
362 sHandle = NULL;
363 }
324 {
325 sHandle = NULL;
326 }
327 }; // class close
364
328
329 TEST_F(close, close_001)
330 {
331 ::osl::Socket sSocket(sHandle);
332 sSocket.close();
365
333
366 void close_001()
367 {
368 ::osl::Socket sSocket(sHandle);
369 sSocket.close();
334 ASSERT_TRUE(sSocket.getHandle() == sHandle) << "test for close_001 function: this function is reserved for test.";
335 }
370
336
371 CPPUNIT_ASSERT_MESSAGE( "test for close_001 function: this function is reserved for test.",
372 sSocket.getHandle() == sHandle );
373 }
337 TEST_F(close, close_002)
338 {
339 // This blocks forever on FreeBSD
340#if defined(LINUX)
341 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
342 AcceptorThread myAcceptorThread( asSocket, rtl::OUString::createFromAscii("127.0.0.1") );
343 myAcceptorThread.create();
374
344
375 void close_002()
376 {
377//#if defined(LINUX)
378 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
379 AcceptorThread myAcceptorThread( asSocket, rtl::OUString::createFromAscii("127.0.0.1") );
380 myAcceptorThread.create();
345 thread_sleep( 1 );
346 //when accepting, close the socket, the thread will not block for accepting
347 //man close:Any locks held on the file it was associated with, and owned by the process, are removed
348 asSocket.close();
349 //thread_sleep( 2 );
350 myAcceptorThread.join();
381
351
382 thread_sleep( 1 );
383 //when accepting, close the socket, the thread will not block for accepting
384 //man close:Any locks held on the file it was associated with, and owned by the process, are removed
385 asSocket.close();
386 //thread_sleep( 2 );
387 myAcceptorThread.join();
352 ASSERT_TRUE(myAcceptorThread.isOK() == sal_True) << "test for close when is accepting: the socket will quit accepting status.";
353#endif
354 }
388
355
389 CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.",
390 myAcceptorThread.isOK() == sal_True );
391//#endif
392 }
356 // to cover "if ( pSockAddrIn->sin_addr.s_addr == htonl(INADDR_ANY) )" in osl_closeSocket( )
357 TEST_F(close, close_003)
358 {
359 // This blocks forever on FreeBSD
360#if defined(LINUX)
361 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
362 AcceptorThread myAcceptorThread( asSocket, rtl::OUString::createFromAscii("0.0.0.0") );
363 myAcceptorThread.create();
393
364
394 // to cover "if ( pSockAddrIn->sin_addr.s_addr == htonl(INADDR_ANY) )" in osl_closeSocket( )
395 void close_003()
396 {
397 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
398 AcceptorThread myAcceptorThread( asSocket, rtl::OUString::createFromAscii("0.0.0.0") );
399 myAcceptorThread.create();
365 thread_sleep( 1 );
366 asSocket.close();
367 myAcceptorThread.join();
400
368
401 thread_sleep( 1 );
402 asSocket.close();
403 myAcceptorThread.join();
369 ASSERT_TRUE(myAcceptorThread.isOK() == sal_True) << "test for close when is accepting: the socket will quit accepting status.";
370#endif
371 }
404
372
405 CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.",
406 myAcceptorThread.isOK() == sal_True );
407 }
408
409 CPPUNIT_TEST_SUITE( close );
410 CPPUNIT_TEST( close_001 );
411 CPPUNIT_TEST( close_002 );
412 CPPUNIT_TEST( close_003 );
413 CPPUNIT_TEST_SUITE_END();
414
415 }; // class close
416
417 /** testing the method:
418 inline void SAL_CALL getLocalAddr( SocketAddr &Addr ) const;
419 */
420
373 /** testing the method:
374 inline void SAL_CALL getLocalAddr( SocketAddr &Addr ) const;
375 */
376
421 class getLocalAddr : public CppUnit::TestFixture
377 class getLocalAddr : public ::testing::Test
422 {
423 public:
424 oslSocket sHandle;
425 // initialization
378 {
379 public:
380 oslSocket sHandle;
381 // initialization
426 void setUp( )
382 void SetUp( )
427 {
428 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
429 }
430
383 {
384 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
385 }
386
431 void tearDown( )
387 void TearDown( )
432 {
433 sHandle = NULL;
434 }
388 {
389 sHandle = NULL;
390 }
391 }; // class getLocalAddr
435
392
436 // get the Address of the local end of the socket
437 void getLocalAddr_001()
438 {
439 ::osl::Socket sSocket(sHandle);
440 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT8 );
441 ::osl::SocketAddr saLocalSocketAddr;
393 // get the Address of the local end of the socket
394 TEST_F(getLocalAddr, getLocalAddr_001)
395 {
396 ::osl::Socket sSocket(sHandle);
397 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT8 );
398 ::osl::SocketAddr saLocalSocketAddr;
442
399
443 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
400 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
444
401
445 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
446 ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString();
447 CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 );
402 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
403 ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString();
404 ASSERT_TRUE(sal_True == bOK1) << suError1.pData;
448
405
449 sSocket.getLocalAddr( saLocalSocketAddr );
406 sSocket.getLocalAddr( saLocalSocketAddr );
450
407
451 sal_Bool bOK = compareUString( saLocalSocketAddr.getHostname( 0 ), sSocket.getLocalHost() ) ;
408 sal_Bool bOK = compareUString( saLocalSocketAddr.getHostname( 0 ), sSocket.getLocalHost() ) ;
452
409
453 CPPUNIT_ASSERT_MESSAGE( "test for getLocalAddr function: first create a new socket, then a socket address, bind them, and check the address.",
454 sal_True == bOK );
455 }
410 ASSERT_TRUE(sal_True == bOK) << "test for getLocalAddr function: first create a new socket, then a socket address, bind them, and check the address.";
411 }
456
412
457
458 CPPUNIT_TEST_SUITE( getLocalAddr );
459 CPPUNIT_TEST( getLocalAddr_001 );
460 CPPUNIT_TEST_SUITE_END();
461
462 }; // class getLocalAddr
463
464
465 /** testing the method:
466 inline sal_Int32 SAL_CALL getLocalPort() const;
467 */
468
413 /** testing the method:
414 inline sal_Int32 SAL_CALL getLocalPort() const;
415 */
416
469 class getLocalPort : public CppUnit::TestFixture
417 class getLocalPort : public ::testing::Test
470 {
471 public:
472 oslSocket sHandle;
473 // initialization
418 {
419 public:
420 oslSocket sHandle;
421 // initialization
474 void setUp( )
422 void SetUp( )
475 {
476 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
477 }
478
423 {
424 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
425 }
426
479 void tearDown( )
427 void TearDown( )
480 {
481 sHandle = NULL;
482 }
428 {
429 sHandle = NULL;
430 }
431 }; // class getLocalPort
483
432
433 TEST_F(getLocalPort, getLocalPort_001)
434 {
435 ::osl::Socket sSocket(sHandle);
436 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT7 ); // aHostIp1 localhost
437 ::osl::SocketAddr saLocalSocketAddr;
484
438
485 void getLocalPort_001()
486 {
487 ::osl::Socket sSocket(sHandle);
488 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT7 ); // aHostIp1 localhost
489 ::osl::SocketAddr saLocalSocketAddr;
439 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
490
440
491 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
441 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
442 ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString();
443 ASSERT_TRUE(sal_True == bOK1) << suError1.pData;
444 sal_Bool bOK = ( IP_PORT_MYPORT7 == sSocket.getLocalPort( ) );
492
445
493 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
494 ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString();
495 CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 );
496 sal_Bool bOK = ( IP_PORT_MYPORT7 == sSocket.getLocalPort( ) );
446 ASSERT_TRUE(sal_True == bOK) << "test for getLocalPort function: first create a new socket, then a socket address, bind them, and check the port.";
447 }
497
448
498 CPPUNIT_ASSERT_MESSAGE( "test for getLocalPort function: first create a new socket, then a socket address, bind them, and check the port.",
499 sal_True == bOK );
500 }
449/** test writer's comment:
501
450
502 /** test writer's comment:
503
504 the invalid port number can not be set by giving invalid port number
505 such as 99999 or -1, it will convert to ( x mod 65535 ), so it will always be
506 valid, the only instance that the getLocalPort returns OSL_INVALID_PORT
507 is when saSocketAddr itself is an invalid one, that is , the IP or host name
508 can not be found, then the created socket address is not valid.
509 */
510 void getLocalPort_002()
511 {
512 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_TELNET);
451 the invalid port number can not be set by giving invalid port number
452 such as 99999 or -1, it will convert to ( x mod 65535 ), so it will always be
453 valid, the only instance that the getLocalPort returns OSL_INVALID_PORT
454 is when saSocketAddr itself is an invalid one, that is , the IP or host name
455 can not be found, then the created socket address is not valid.
456*/
457 TEST_F(getLocalPort, getLocalPort_002)
458 {
459 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_TELNET);
513#ifdef WNT
460#ifdef WNT
514 ::osl::Socket sSocket(sHandle);
515 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
516 sSocket.bind( saBindSocketAddr );
517 //Invalid IP, so bind should fail
518 ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )),
519 ::rtl::OUString::valueOf((sal_Int32)OSL_INVALID_PORT),
520 "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned.");
521 sal_Bool bOK = ( OSL_INVALID_PORT == sSocket.getLocalPort( ) );
522 (void)bOK;
461 ::osl::Socket sSocket(sHandle);
462 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
463 sSocket.bind( saBindSocketAddr );
464 //Invalid IP, so bind should fail
465 ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )),
466 ::rtl::OUString::valueOf((sal_Int32)OSL_INVALID_PORT),
467 "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned.");
468 sal_Bool bOK = ( OSL_INVALID_PORT == sSocket.getLocalPort( ) );
469 (void)bOK;
523#else
470#else
524 //on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT
525 ::rtl::OUString suError = ::rtl::OUString::createFromAscii( "on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT, but can not create Addr of that case");
471 //on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT
472 ::rtl::OUString suError = ::rtl::OUString::createFromAscii( "on Unix, if Addr is not an address of type osl_Socket_FamilyInet, it returns OSL_INVALID_PORT, but can not create Addr of that case");
526#endif
473#endif
527 CPPUNIT_ASSERT_MESSAGE( suError, sal_False );
474 ASSERT_TRUE(sal_False) << suError.pData;
528
475
529 }
476 }
530
477
531 void getLocalPort_003()
532 {
533 ::osl::Socket sSocket(sHandle);
534 ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_INVAL);
478 TEST_F(getLocalPort, getLocalPort_003)
479 {
480 ::osl::Socket sSocket(sHandle);
481 ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_INVAL);
535
482
536 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
483 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
537
484
538 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
539 ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString();
540 CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 );
541 ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )),
542 ::rtl::OUString::createFromAscii("34463"),
543 "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned");
544 sal_Bool bOK = ( sSocket.getLocalPort( ) >= 1 && sSocket.getLocalPort( ) <= 65535);
485 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
486 ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString();
487 ASSERT_TRUE(sal_True == bOK1) << suError1.pData;
488 ::rtl::OUString suError = outputError(::rtl::OUString::valueOf(sSocket.getLocalPort( )),
489 ::rtl::OUString::createFromAscii("34463"),
490 "test for getLocalPort function: first create a new socket, then an invalid socket address, bind them, and check the port assigned");
491 sal_Bool bOK = ( sSocket.getLocalPort( ) >= 1 && sSocket.getLocalPort( ) <= 65535);
545
492
546 CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK );
547 }
493 ASSERT_TRUE(sal_True == bOK) << suError.pData;
494 }
548
495
549 CPPUNIT_TEST_SUITE( getLocalPort );
550 CPPUNIT_TEST( getLocalPort_001 );
551// LLA: CPPUNIT_TEST( getLocalPort_002 );
552 CPPUNIT_TEST( getLocalPort_003 );
553 CPPUNIT_TEST_SUITE_END();
554
555 }; // class getLocalPort
556
557
558 /** testing the method:
559 inline ::rtl::OUString SAL_CALL getLocalHost() const;
560
561 Mindyliu: on Linux, at first it will check the binded in /etc/hosts, if it has the binded IP, it will return the hostname in it;
562 else if the binded IP is "127.0.0.1", it will return "localhost", if it's the machine's ethernet ip such as "129.158.217.90", it
563 will return hostname of current processor such as "aegean.PRC.Sun.COM"
564 */
565
496 /** testing the method:
497 inline ::rtl::OUString SAL_CALL getLocalHost() const;
498
499 Mindyliu: on Linux, at first it will check the binded in /etc/hosts, if it has the binded IP, it will return the hostname in it;
500 else if the binded IP is "127.0.0.1", it will return "localhost", if it's the machine's ethernet ip such as "129.158.217.90", it
501 will return hostname of current processor such as "aegean.PRC.Sun.COM"
502 */
503
566 class getLocalHost : public CppUnit::TestFixture
504 class getLocalHost : public ::testing::Test
567 {
568 public:
569 oslSocket sHandle;
570 // initialization
505 {
506 public:
507 oslSocket sHandle;
508 // initialization
571 void setUp( )
509 void SetUp( )
572 {
573 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
574 }
575
510 {
511 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
512 }
513
576 void tearDown( )
514 void TearDown( )
577 {
578 sHandle = NULL;
579 }
515 {
516 sHandle = NULL;
517 }
518 }; // class getLocalHost
580
519
520 TEST_F(getLocalHost, getLocalHost_001)
521 {
522 ::osl::Socket sSocket(sHandle);
523 //port number from IP_PORT_HTTP1 to IP_PORT_MYPORT6, mindyliu
524 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT6 );
581
525
582 void getLocalHost_001()
583 {
584 ::osl::Socket sSocket(sHandle);
585 //port number from IP_PORT_HTTP1 to IP_PORT_MYPORT6, mindyliu
586 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT6 );
526 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
587
527
588 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
589
590 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
591 ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString();
592 CPPUNIT_ASSERT_MESSAGE( suError1, sal_True == bOK1 );
593 sal_Bool bOK;
594 ::rtl::OUString suError;
528 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
529 ::rtl::OUString suError1 = ::rtl::OUString::createFromAscii("Socket bind fail:") + sSocket.getErrorAsString();
530 ASSERT_TRUE(sal_True == bOK1) << suError1.pData;
531 sal_Bool bOK;
532 ::rtl::OUString suError;
595#ifdef WNT
533#ifdef WNT
596 bOK = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ;
597 suError = outputError(sSocket.getLocalHost( ), getThisHostname( ),
534 bOK = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ;
535 suError = outputError(sSocket.getLocalHost( ), getThisHostname( ),
598"test for getLocalHost function: create localhost socket and check name");
599#else
536"test for getLocalHost function: create localhost socket and check name");
537#else
600 ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) "localhost" );
601 sal_Bool bRes1, bRes2;
602 bRes1 = compareUString( sSocket.getLocalHost( ), aUString ) ;
603 bRes2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname(0) ) ;
604 bOK = bRes1 || bRes2;
605 suError = outputError(sSocket.getLocalHost( ), aUString, "test for getLocalHost function: create localhost socket and check name");
538 ::rtl::OUString aUString = ::rtl::OUString::createFromAscii( (const sal_Char *) "localhost" );
539 sal_Bool bRes1, bRes2;
540 bRes1 = compareUString( sSocket.getLocalHost( ), aUString ) ;
541 bRes2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname(0) ) ;
542 bOK = bRes1 || bRes2;
543 suError = outputError(sSocket.getLocalHost( ), aUString, "test for getLocalHost function: create localhost socket and check name");
606#endif
544#endif
607 CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK );
608 }
545 ASSERT_TRUE(sal_True == bOK) << suError.pData;
546 }
609
547
610 void getLocalHost_002()
611 {
612 ::osl::Socket sSocket(sHandle);
613 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_POP3);
614 ::osl::SocketAddr saLocalSocketAddr;
548 TEST_F(getLocalHost, getLocalHost_002)
549 {
550 ::osl::Socket sSocket(sHandle);
551 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_POP3);
552 ::osl::SocketAddr saLocalSocketAddr;
615
553
616 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
617 sSocket.bind( saBindSocketAddr );
618 //Invalid IP, so bind should fail
619 sal_Bool bOK = compareUString( sSocket.getLocalHost( ), rtl::OUString::createFromAscii("") ) ;
620 ::rtl::OUString suError = outputError(sSocket.getLocalHost( ), rtl::OUString::createFromAscii(""), "test for getLocalHost function: getLocalHost with invalid SocketAddr");
554 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
555 sSocket.bind( saBindSocketAddr );
556 //Invalid IP, so bind should fail
557 sal_Bool bOK = compareUString( sSocket.getLocalHost( ), rtl::OUString::createFromAscii("") ) ;
558 ::rtl::OUString suError = outputError(sSocket.getLocalHost( ), rtl::OUString::createFromAscii(""), "test for getLocalHost function: getLocalHost with invalid SocketAddr");
621
559
622 CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK );
623 }
560 ASSERT_TRUE(sal_True == bOK) << suError.pData;
561 }
624
562
625 CPPUNIT_TEST_SUITE( getLocalHost );
626 CPPUNIT_TEST( getLocalHost_001 );
627 CPPUNIT_TEST( getLocalHost_002 );
628 CPPUNIT_TEST_SUITE_END();
629
630 }; // class getLocalHost
631
632
633 /** testing the methods:
634 inline void SAL_CALL getPeerAddr( SocketAddr & Addr) const;
635 inline sal_Int32 SAL_CALL getPeerPort() const;
636 inline ::rtl::OUString SAL_CALL getPeerHost() const;
637 */
563 /** testing the methods:
564 inline void SAL_CALL getPeerAddr( SocketAddr & Addr) const;
565 inline sal_Int32 SAL_CALL getPeerPort() const;
566 inline ::rtl::OUString SAL_CALL getPeerHost() const;
567 */
638 class getPeer : public CppUnit::TestFixture
568 class getPeer : public ::testing::Test
639 {
640 public:
641 oslSocket sHandle;
642 TimeValue *pTimeout;
643 ::osl::AcceptorSocket asAcceptorSocket;
644 ::osl::ConnectorSocket csConnectorSocket;
645
646
647 // initialization
569 {
570 public:
571 oslSocket sHandle;
572 TimeValue *pTimeout;
573 ::osl::AcceptorSocket asAcceptorSocket;
574 ::osl::ConnectorSocket csConnectorSocket;
575
576
577 // initialization
648 void setUp( )
578 void SetUp( )
649 {
650 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
651 pTimeout->Seconds = 3;
652 pTimeout->Nanosec = 0;
653 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
654 }
655
579 {
580 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
581 pTimeout->Seconds = 3;
582 pTimeout->Nanosec = 0;
583 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
584 }
585
656 void tearDown( )
586 void TearDown( )
657 {
658 free( pTimeout );
659 sHandle = NULL;
660 asAcceptorSocket.close( );
661 csConnectorSocket.close( );
662 }
587 {
588 free( pTimeout );
589 sHandle = NULL;
590 asAcceptorSocket.close( );
591 csConnectorSocket.close( );
592 }
593 }; // class getPeer
663
594
595 TEST_F(getPeer, getPeer_001)
596 {
597 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT );
598 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT );
599 ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
600 ::osl::StreamSocket ssConnection;
601 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
602 /// launch server socket
603 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
604 ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind '127.0.0.1' address failed.";
605 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
606 ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed.";
664
607
665 void getPeer_001()
666 {
667 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT );
668 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT );
669 ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
670 ::osl::StreamSocket ssConnection;
671 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
672 /// launch server socket
673 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
674 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind '127.0.0.1' address failed.", sal_True == bOK1 );
675 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
676 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
608 asAcceptorSocket.enableNonBlockingMode( sal_True );
609 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
677
610
678 asAcceptorSocket.enableNonBlockingMode( sal_True );
679 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
611 /// launch client socket
612 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
680
613
681 /// launch client socket
682 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
614 /// get peer information
615 csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected.
616 sal_Int32 peerPort = csConnectorSocket.getPeerPort( );
617 ::rtl::OUString peerHost = csConnectorSocket.getPeerHost( );
683
618
684 /// get peer information
685 csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected.
686 sal_Int32 peerPort = csConnectorSocket.getPeerPort( );
687 ::rtl::OUString peerHost = csConnectorSocket.getPeerHost( );
619 ASSERT_TRUE(( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) )&&
620 ( sal_True == compareUString( peerHost, saLocalSocketAddr.getHostname( 0 ) ) ) &&
621 ( peerPort == saLocalSocketAddr.getPort( ) )) << "test for getPeer function: setup a connection and then get the peer address, port and host from client side.";
622 }
688
623
689 CPPUNIT_ASSERT_MESSAGE( "test for getPeer function: setup a connection and then get the peer address, port and host from client side.",
690 ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) )&&
691 ( sal_True == compareUString( peerHost, saLocalSocketAddr.getHostname( 0 ) ) ) &&
692 ( peerPort == saLocalSocketAddr.getPort( ) ));
693 }
694
695
696 CPPUNIT_TEST_SUITE( getPeer );
697 CPPUNIT_TEST( getPeer_001 );
698 CPPUNIT_TEST_SUITE_END();
699
700 }; // class getPeer
701
702
703 /** testing the methods:
704 inline sal_Bool SAL_CALL bind(const SocketAddr& LocalInterface);
705 */
706
707
624 /** testing the methods:
625 inline sal_Bool SAL_CALL bind(const SocketAddr& LocalInterface);
626 */
627
628
708 class bind : public CppUnit::TestFixture
629 class bind : public ::testing::Test
709 {
710 public:
711 oslSocket sHandle;
712 // initialization
630 {
631 public:
632 oslSocket sHandle;
633 // initialization
713 void setUp( )
634 void SetUp( )
714 {
715 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
716 }
717
635 {
636 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
637 }
638
718 void tearDown( )
639 void TearDown( )
719 {
720 sHandle = NULL;
721 }
640 {
641 sHandle = NULL;
642 }
643 }; // class bind
722
644
645 TEST_F(bind, bind_001)
646 {
647 ::osl::Socket sSocket(sHandle);
648 //bind must use local IP address ---mindyliu
649 ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_MYPORT5 );
723
650
724 void bind_001()
725 {
726 ::osl::Socket sSocket(sHandle);
727 //bind must use local IP address ---mindyliu
728 ::osl::SocketAddr saBindSocketAddr( getLocalIP(), IP_PORT_MYPORT5 );
651 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
652 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
653 ASSERT_TRUE(sal_True == bOK1) << "Socket bind fail.";
729
654
730 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
731 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
732 CPPUNIT_ASSERT_MESSAGE( "Socket bind fail.", sal_True == bOK1 );
655 sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname( ) ) ;
733
656
734 sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), saBindSocketAddr.getHostname( ) ) ;
657 sSocket.close();
658 ASSERT_TRUE(sal_True == bOK2) << "test for bind function: bind a valid address.";
659 }
735
660
736 sSocket.close();
737 CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.", sal_True == bOK2 );
738 }
661 TEST_F(bind, bind_002)
662 {
663 ::osl::Socket sSocket(sHandle);
664 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_NETBIOS );
665 ::osl::SocketAddr saLocalSocketAddr;
739
666
740 void bind_002()
741 {
742 ::osl::Socket sSocket(sHandle);
743 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_NETBIOS );
744 ::osl::SocketAddr saLocalSocketAddr;
667 sSocket.setOption( osl_Socket_OptionReuseAddr, 1); // sal_True);
668 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
669 sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ;
745
670
746 sSocket.setOption( osl_Socket_OptionReuseAddr, 1); // sal_True);
747 sal_Bool bOK1 = sSocket.bind( saBindSocketAddr );
748 sal_Bool bOK2 = compareUString( sSocket.getLocalHost( ), getThisHostname( ) ) ;
671 ASSERT_TRUE(( sal_False == bOK1 ) && ( sal_False == bOK2 )) << "test for bind function: bind a valid address.";
672 }
749
673
750 CPPUNIT_ASSERT_MESSAGE( "test for bind function: bind a valid address.",
751 ( sal_False == bOK1 ) && ( sal_False == bOK2 ) );
752 }
753
754 CPPUNIT_TEST_SUITE( bind );
755 CPPUNIT_TEST( bind_001 );
756 CPPUNIT_TEST( bind_002 );
757 CPPUNIT_TEST_SUITE_END();
758
759 }; // class bind
760
761
762 /** testing the methods:
763 inline sal_Bool SAL_CALL isRecvReady(const TimeValue *pTimeout = 0) const;
764
765 */
674 /** testing the methods:
675 inline sal_Bool SAL_CALL isRecvReady(const TimeValue *pTimeout = 0) const;
676
677 */
766 class isRecvReady : public CppUnit::TestFixture
678 class isRecvReady : public ::testing::Test
767 {
768 public:
769 oslSocket sHandle;
770 TimeValue *pTimeout;
771 ::osl::AcceptorSocket asAcceptorSocket;
772 ::osl::ConnectorSocket csConnectorSocket;
773
774
775 // initialization
679 {
680 public:
681 oslSocket sHandle;
682 TimeValue *pTimeout;
683 ::osl::AcceptorSocket asAcceptorSocket;
684 ::osl::ConnectorSocket csConnectorSocket;
685
686
687 // initialization
776 void setUp( )
688 void SetUp( )
777 {
778 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
779 pTimeout->Seconds = 3;
780 pTimeout->Nanosec = 0;
781 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
782 }
783
689 {
690 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
691 pTimeout->Seconds = 3;
692 pTimeout->Nanosec = 0;
693 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
694 }
695
784 void tearDown( )
696 void TearDown( )
785 {
786 free( pTimeout );
787 sHandle = NULL;
788 asAcceptorSocket.close( );
789 csConnectorSocket.close( );
790 }
697 {
698 free( pTimeout );
699 sHandle = NULL;
700 asAcceptorSocket.close( );
701 csConnectorSocket.close( );
702 }
703 }; // class isRecvReady
791
704
705 TEST_F(isRecvReady, isRecvReady_001)
706 {
707 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT1 );
708 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT1 );
709 ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
710 ::osl::StreamSocket ssConnection;
711 /// launch server socket
712 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
713 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
714 ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed.";
715 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
716 ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed.";
717 asAcceptorSocket.enableNonBlockingMode( sal_True );
718 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
792
719
793 void isRecvReady_001()
794 {
795 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT1 );
796 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT1 );
797 ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
798 ::osl::StreamSocket ssConnection;
799 /// launch server socket
800 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
801 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
802 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
803 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
804 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
805 asAcceptorSocket.enableNonBlockingMode( sal_True );
806 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
720 /// launch client socket
721 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
807
722
808 /// launch client socket
809 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
723 /// is receive ready?
724 sal_Bool bOK3 = asAcceptorSocket.isRecvReady( pTimeout );
810
725
811 /// is receive ready?
812 sal_Bool bOK3 = asAcceptorSocket.isRecvReady( pTimeout );
726 ASSERT_TRUE(( sal_True == bOK3 )) << "test for isRecvReady function: setup a connection and then check if it can transmit data.";
727 }
813
728
814 CPPUNIT_ASSERT_MESSAGE( "test for isRecvReady function: setup a connection and then check if it can transmit data.",
815 ( sal_True == bOK3 ) );
816 }
817
818
819 CPPUNIT_TEST_SUITE( isRecvReady );
820 CPPUNIT_TEST( isRecvReady_001 );
821 CPPUNIT_TEST_SUITE_END();
822
823 }; // class isRecvReady
824
825
826 /** testing the methods:
827 inline sal_Bool SAL_CALL isSendReady(const TimeValue *pTimeout = 0) const;
828 */
729 /** testing the methods:
730 inline sal_Bool SAL_CALL isSendReady(const TimeValue *pTimeout = 0) const;
731 */
829 class isSendReady : public CppUnit::TestFixture
732 class isSendReady : public ::testing::Test
830 {
831 public:
832 oslSocket sHandle;
833 TimeValue *pTimeout;
834 ::osl::AcceptorSocket asAcceptorSocket;
835 ::osl::ConnectorSocket csConnectorSocket;
836
837
838 // initialization
733 {
734 public:
735 oslSocket sHandle;
736 TimeValue *pTimeout;
737 ::osl::AcceptorSocket asAcceptorSocket;
738 ::osl::ConnectorSocket csConnectorSocket;
739
740
741 // initialization
839 void setUp( )
742 void SetUp( )
840 {
841 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
842 pTimeout->Seconds = 3;
843 pTimeout->Nanosec = 0;
844 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
845 }
846
743 {
744 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
745 pTimeout->Seconds = 3;
746 pTimeout->Nanosec = 0;
747 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
748 }
749
847 void tearDown( )
750 void TearDown( )
848 {
849 free( pTimeout );
850 sHandle = NULL;
851 asAcceptorSocket.close( );
852 csConnectorSocket.close( );
853 }
751 {
752 free( pTimeout );
753 sHandle = NULL;
754 asAcceptorSocket.close( );
755 csConnectorSocket.close( );
756 }
757 }; // class isSendReady
854
758
759 TEST_F(isSendReady, isSendReady_001)
760 {
761 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT );
762 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT );
763 ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
764 ::osl::StreamSocket ssConnection;
855
765
856 void isSendReady_001()
857 {
858 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT );
859 ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT );
860 ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
861 ::osl::StreamSocket ssConnection;
766 /// launch server socket
767 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
768 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
769 ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed.";
770 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
771 ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed.";
772 asAcceptorSocket.enableNonBlockingMode( sal_True );
773 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
862
774
863 /// launch server socket
864 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
865 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
866 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
867 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
868 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
869 asAcceptorSocket.enableNonBlockingMode( sal_True );
870 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
775 /// launch client socket
776 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
871
777
872 /// launch client socket
873 csConnectorSocket.connect( saTargetSocketAddr, pTimeout ); /// connecting to server...
778 /// is send ready?
779 sal_Bool bOK3 = csConnectorSocket.isSendReady( pTimeout );
874
780
875 /// is send ready?
876 sal_Bool bOK3 = csConnectorSocket.isSendReady( pTimeout );
781 ASSERT_TRUE(( sal_True == bOK3 )) << "test for isSendReady function: setup a connection and then check if it can transmit data.";
782 }
877
783
878 CPPUNIT_ASSERT_MESSAGE( "test for isSendReady function: setup a connection and then check if it can transmit data.",
879 ( sal_True == bOK3 ) );
880 }
881
882
883 CPPUNIT_TEST_SUITE( isSendReady );
884 CPPUNIT_TEST( isSendReady_001 );
885 CPPUNIT_TEST_SUITE_END();
886
887 }; // class isSendReady
888
889
890 /** testing the methods:
891 inline oslSocketType SAL_CALL getType() const;
892
893 */
894
784 /** testing the methods:
785 inline oslSocketType SAL_CALL getType() const;
786
787 */
788
895 class getType : public CppUnit::TestFixture
789 class getType : public ::testing::Test
896 {
897 public:
898 oslSocket sHandle;
899 // initialization
790 {
791 public:
792 oslSocket sHandle;
793 // initialization
900 void setUp( )
794 void SetUp( )
901 {
902
903 }
904
795 {
796
797 }
798
905 void tearDown( )
799 void TearDown( )
906 {
907 sHandle = NULL;
908 }
800 {
801 sHandle = NULL;
802 }
803 }; // class getType
909
804
805 TEST_F(getType, getType_001)
806 {
807 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
808 ::osl::Socket sSocket(sHandle);
910
809
911 void getType_001()
912 {
913 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
914 ::osl::Socket sSocket(sHandle);
810 ASSERT_TRUE(osl_Socket_TypeStream == sSocket.getType( )) << "test for getType function: get type of socket.";
811 }
915
812
916 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.",
917 osl_Socket_TypeStream == sSocket.getType( ) );
918 }
813 TEST_F(getType, getType_002)
814 {
815 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
816 ::osl::Socket sSocket(sHandle);
919
817
920 void getType_002()
921 {
922 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
923 ::osl::Socket sSocket(sHandle);
818 ASSERT_TRUE(osl_Socket_TypeDgram == sSocket.getType( )) << "test for getType function: get type of socket.";
819 }
924
820
925 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.",
926 osl_Socket_TypeDgram == sSocket.getType( ) );
927 }
928
929#ifdef UNX
821#ifdef UNX
930 // mindy: since on LINUX and SOLARIS, Raw type socket can not be created, so do not test getType() here
931 // mindy: and add one test case to test creating Raw type socket--> ctors_TypeRaw()
932 void getType_003()
933 {
934 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.this is not passed in (LINUX, SOLARIS), the osl_Socket_TypeRaw, type socket can not be created.",
935 sal_True);
936 }
822 // mindy: since on LINUX and SOLARIS, Raw type socket can not be created, so do not test getType() here
823 // mindy: and add one test case to test creating Raw type socket--> ctors_TypeRaw()
824 TEST_F(getType, getType_003)
825 {
826 ASSERT_TRUE(sal_True) << "test for getType function: get type of socket.this is not passed in (LINUX, SOLARIS), the osl_Socket_TypeRaw, type socket can not be created.";
827 }
937#else
828#else
938 void getType_003()
939 {
940 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
941 ::osl::Socket sSocket(sHandle);
829 TEST_F(getType, getType_003)
830 {
831 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeRaw, osl_Socket_ProtocolIp );
832 ::osl::Socket sSocket(sHandle);
942
833
943 CPPUNIT_ASSERT_MESSAGE( "test for getType function: get type of socket.",
944 osl_Socket_TypeRaw == sSocket.getType( ) );
945 }
834 ASSERT_TRUE(osl_Socket_TypeRaw == sSocket.getType( )) << "test for getType function: get type of socket.";
835 }
946#endif
947
836#endif
837
948 CPPUNIT_TEST_SUITE( getType );
949 CPPUNIT_TEST( getType_001 );
950 CPPUNIT_TEST( getType_002 );
951 CPPUNIT_TEST( getType_003 );
952 CPPUNIT_TEST_SUITE_END();
953
838
954 }; // class getType
955
956
957
958 /** testing the methods:
959 inline sal_Int32 SAL_CALL getOption(
960 oslSocketOption Option,
961 void* pBuffer,
962 sal_uInt32 BufferLen,
963 oslSocketOptionLevel Level= osl_Socket_LevelSocket) const;
964
965 inline sal_Int32 getOption( oslSocketOption option ) const;
966
967 */
968
839 /** testing the methods:
840 inline sal_Int32 SAL_CALL getOption(
841 oslSocketOption Option,
842 void* pBuffer,
843 sal_uInt32 BufferLen,
844 oslSocketOptionLevel Level= osl_Socket_LevelSocket) const;
845
846 inline sal_Int32 getOption( oslSocketOption option ) const;
847
848 */
849
969 class getOption : public CppUnit::TestFixture
850 class getOption : public ::testing::Test
970 {
971 public:
972 oslSocket sHandle;
973 // initialization
851 {
852 public:
853 oslSocket sHandle;
854 // initialization
974 void setUp( )
855 void SetUp( )
975 {
976
977 }
978
856 {
857
858 }
859
979 void tearDown( )
860 void TearDown( )
980 {
981 sHandle = NULL;
982 }
861 {
862 sHandle = NULL;
863 }
864 }; // class getOption
983
865
984 /** test writer's comment:
985
866
986 in oslSocketOption, the osl_Socket_OptionType denote 1 as osl_Socket_TypeStream.
987 2 as osl_Socket_TypeDgram, etc which is not mapping the oslSocketType enum. differ
988 in 1.
989 */
867 /** test writer's comment:
990
868
991 void getOption_001()
992 {
993 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
994 ::osl::Socket sSocket(sHandle);
995 sal_Int32 * pType = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
996 *pType = 0;
997 sSocket.getOption( osl_Socket_OptionType, pType, sizeof ( sal_Int32 ) );
998 sal_Bool bOK = ( SOCK_STREAM == *pType );
999 // there is a TypeMap(socket.c) which map osl_Socket_TypeStream to SOCK_STREAM on UNX, and SOCK_STREAM != osl_Socket_TypeStream
1000 //sal_Bool bOK = ( TYPE_TO_NATIVE(osl_Socket_TypeStream) == *pType );
1001 free( pType );
869 in oslSocketOption, the osl_Socket_OptionType denote 1 as osl_Socket_TypeStream.
870 2 as osl_Socket_TypeDgram, etc which is not mapping the oslSocketType enum. differ
871 in 1.
872 */
1002
873
1003 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get type option of socket.",
1004 sal_True == bOK );
1005 }
874 TEST_F(getOption, getOption_001)
875 {
876 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
877 ::osl::Socket sSocket(sHandle);
878 sal_Int32 * pType = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
879 *pType = 0;
880 sSocket.getOption( osl_Socket_OptionType, pType, sizeof ( sal_Int32 ) );
881 sal_Bool bOK = ( SOCK_STREAM == *pType );
882 // there is a TypeMap(socket.c) which map osl_Socket_TypeStream to SOCK_STREAM on UNX, and SOCK_STREAM != osl_Socket_TypeStream
883 //sal_Bool bOK = ( TYPE_TO_NATIVE(osl_Socket_TypeStream) == *pType );
884 free( pType );
1006
885
1007 // getsockopt error
1008 void getOption_004()
1009 {
1010 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
1011 ::osl::Socket sSocket(sHandle);
886 ASSERT_TRUE(sal_True == bOK) << "test for getOption function: get type option of socket.";
887 }
1012
888
1013 sal_Bool * pbDontRoute = ( sal_Bool * )malloc( sizeof ( sal_Bool ) );
1014 sal_Int32 nRes = sSocket.getOption( osl_Socket_OptionInvalid, pbDontRoute, sizeof ( sal_Bool ) );
1015 free( pbDontRoute );
889 // getsockopt error
890 TEST_F(getOption, getOption_004)
891 {
892 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
893 ::osl::Socket sSocket(sHandle);
1016
894
1017 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get invalid option of socket, should return -1.",
1018 nRes == -1 );
1019 }
895 sal_Bool * pbDontRoute = ( sal_Bool * )malloc( sizeof ( sal_Bool ) );
896 sal_Int32 nRes = sSocket.getOption( osl_Socket_OptionInvalid, pbDontRoute, sizeof ( sal_Bool ) );
897 free( pbDontRoute );
1020
898
1021 void getOption_simple_001()
1022 {
1023 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
1024 ::osl::Socket sSocket(sHandle);
899 ASSERT_TRUE(nRes == -1) << "test for getOption function: get invalid option of socket, should return -1.";
900 }
1025
901
1026 sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDontRoute ) );
902 TEST_F(getOption, getOption_simple_001)
903 {
904 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
905 ::osl::Socket sSocket(sHandle);
1027
906
1028 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.",
1029 sal_True == bOK );
1030 }
907 sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDontRoute ) );
1031
908
1032 void getOption_simple_002()
1033 {
1034 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
1035 ::osl::Socket sSocket(sHandle);
909 ASSERT_TRUE(sal_True == bOK) << "test for getOption function: get debug option of socket.";
910 }
1036
911
1037 sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDebug ) );
912 TEST_F(getOption, getOption_simple_002)
913 {
914 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeDgram, osl_Socket_ProtocolIp );
915 ::osl::Socket sSocket(sHandle);
1038
916
1039 CPPUNIT_ASSERT_MESSAGE( "test for getOption function: get debug option of socket.",
1040 sal_True == bOK );
1041 }
917 sal_Bool bOK = ( sal_False == sSocket.getOption( osl_Socket_OptionDebug ) );
1042
918
1043 CPPUNIT_TEST_SUITE( getOption );
1044 CPPUNIT_TEST( getOption_001 );
1045 CPPUNIT_TEST( getOption_004 );
1046 CPPUNIT_TEST( getOption_simple_001 );
1047 CPPUNIT_TEST( getOption_simple_002 );
1048 CPPUNIT_TEST_SUITE_END();
919 ASSERT_TRUE(sal_True == bOK) << "test for getOption function: get debug option of socket.";
920 }
1049
921
1050 }; // class getOption
1051
1052
1053 /** testing the methods:
1054 inline sal_Bool SAL_CALL setOption( oslSocketOption Option,
1055 void* pBuffer,
1056 sal_uInt32 BufferLen,
1057 oslSocketOptionLevel Level= osl_Socket_LevelSocket ) const;
1058 */
1059
922 /** testing the methods:
923 inline sal_Bool SAL_CALL setOption( oslSocketOption Option,
924 void* pBuffer,
925 sal_uInt32 BufferLen,
926 oslSocketOptionLevel Level= osl_Socket_LevelSocket ) const;
927 */
928
1060 class setOption : public CppUnit::TestFixture
929 class setOption : public ::testing::Test
1061 {
1062 public:
1063 TimeValue *pTimeout;
1064// LLA: maybe there is an error in the source,
1065// as long as I remember, if a derived class do not overload all ctors there is a problem.
1066
1067 ::osl::AcceptorSocket asAcceptorSocket;
1068
930 {
931 public:
932 TimeValue *pTimeout;
933// LLA: maybe there is an error in the source,
934// as long as I remember, if a derived class do not overload all ctors there is a problem.
935
936 ::osl::AcceptorSocket asAcceptorSocket;
937
1069 void setUp( )
938 void SetUp( )
1070 {
1071
1072 }
1073
939 {
940
941 }
942
1074 void tearDown( )
943 void TearDown( )
1075 {
1076 asAcceptorSocket.close( );
1077 }
944 {
945 asAcceptorSocket.close( );
946 }
947 }; // class setOption
1078
948
949 // LLA:
950 // getSocketOption returns BufferLen, or -1 if something failed
1079
951
1080 // LLA:
1081 // getSocketOption returns BufferLen, or -1 if something failed
952 // setSocketOption returns sal_True, if option could stored
953 // else sal_False
1082
954
1083 // setSocketOption returns sal_True, if option could stored
1084 // else sal_False
955 TEST_F(setOption, setOption_001)
956 {
957 /// set and get option.
958 int nBufferLen = sizeof ( sal_Int32);
959 // LLA: SO_DONTROUTE expect an integer boolean, what ever it is, it's not sal_Bool!
1085
960
1086 void setOption_001()
1087 {
1088 /// set and get option.
1089 int nBufferLen = sizeof ( sal_Int32);
1090 // LLA: SO_DONTROUTE expect an integer boolean, what ever it is, it's not sal_Bool!
961 sal_Int32 * pbDontRouteSet = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
962 *pbDontRouteSet = 1; // sal_True;
1091
963
1092 sal_Int32 * pbDontRouteSet = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
1093 *pbDontRouteSet = 1; // sal_True;
964 sal_Int32 * pGetBuffer = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
965 *pGetBuffer = 0;
1094
966
1095 sal_Int32 * pGetBuffer = ( sal_Int32 * )malloc( sizeof ( sal_Int32 ) );
1096 *pGetBuffer = 0;
967 // maybe asAcceptorSocket is not right initialized
968 sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, nBufferLen );
969 ASSERT_TRUE(( sal_True == b1 )) << "setOption function failed.";
970 sal_Int32 n2 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen );
971 ASSERT_TRUE(( n2 == nBufferLen )) << "getOption function failed.";
1097
972
1098 // maybe asAcceptorSocket is not right initialized
1099 sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, nBufferLen );
1100 CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True == b1 ) );
1101 sal_Int32 n2 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen );
1102 CPPUNIT_ASSERT_MESSAGE( "getOption function failed.", ( n2 == nBufferLen ) );
973 // on Linux, the value of option is 1, on Solaris, it's 16, but it's not important the exact value,
974 // just judge it is zero or not!
975 sal_Bool bOK = ( 0 != *pGetBuffer );
976 printf("#setOption_001: getOption is %d \n", *pGetBuffer);
1103
977
1104 // on Linux, the value of option is 1, on Solaris, it's 16, but it's not important the exact value,
1105 // just judge it is zero or not!
1106 sal_Bool bOK = ( 0 != *pGetBuffer );
1107 t_print("#setOption_001: getOption is %d \n", *pGetBuffer);
978 // toggle check, set to 0
979 *pbDontRouteSet = 0;
1108
980
1109 // toggle check, set to 0
1110 *pbDontRouteSet = 0;
981 sal_Bool b3 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, sizeof ( sal_Int32 ) );
982 ASSERT_TRUE(( sal_True == b3 )) << "setOption function failed.";
983 sal_Int32 n4 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen );
984 ASSERT_TRUE(( n4 == nBufferLen )) << "getOption (DONTROUTE) function failed.";
1111
985
1112 sal_Bool b3 = asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontRouteSet, sizeof ( sal_Int32 ) );
1113 CPPUNIT_ASSERT_MESSAGE( "setOption function failed.", ( sal_True == b3 ) );
1114 sal_Int32 n4 = asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pGetBuffer, nBufferLen );
1115 CPPUNIT_ASSERT_MESSAGE( "getOption (DONTROUTE) function failed.", ( n4 == nBufferLen ) );
986 sal_Bool bOK2 = ( 0 == *pGetBuffer );
1116
987
1117 sal_Bool bOK2 = ( 0 == *pGetBuffer );
988 printf("#setOption_001: getOption is %d \n", *pGetBuffer);
1118
989
1119 t_print("#setOption_001: getOption is %d \n", *pGetBuffer);
1120
1121// LLA: sal_Bool * pbDontTouteSet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) );
1122// LLA: *pbDontTouteSet = sal_True;
1123// LLA: sal_Bool * pbDontTouteGet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) );
1124// LLA: *pbDontTouteGet = sal_False;
1125// LLA: asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontTouteSet, sizeof ( sal_Bool ) );
1126// LLA: asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pbDontTouteGet, sizeof ( sal_Bool ) );
1127// LLA: ::rtl::OUString suError = outputError(::rtl::OUString::valueOf((sal_Int32)*pbDontTouteGet),
1128// LLA: ::rtl::OUString::valueOf((sal_Int32)*pbDontTouteSet),
1129// LLA: "test for setOption function: set osl_Socket_OptionDontRoute and then check");
990// LLA: sal_Bool * pbDontTouteSet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) );
991// LLA: *pbDontTouteSet = sal_True;
992// LLA: sal_Bool * pbDontTouteGet = ( sal_Bool * )malloc( sizeof ( sal_Bool ) );
993// LLA: *pbDontTouteGet = sal_False;
994// LLA: asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, pbDontTouteSet, sizeof ( sal_Bool ) );
995// LLA: asAcceptorSocket.getOption( osl_Socket_OptionDontRoute, pbDontTouteGet, sizeof ( sal_Bool ) );
996// LLA: ::rtl::OUString suError = outputError(::rtl::OUString::valueOf((sal_Int32)*pbDontTouteGet),
997// LLA: ::rtl::OUString::valueOf((sal_Int32)*pbDontTouteSet),
998// LLA: "test for setOption function: set osl_Socket_OptionDontRoute and then check");
1130// LLA:
999// LLA:
1131// LLA: sal_Bool bOK = ( sal_True == *pbDontTouteGet );
1132// LLA: free( pbDontTouteSet );
1133// LLA: free( pbDontTouteGet );
1000// LLA: sal_Bool bOK = ( sal_True == *pbDontTouteGet );
1001// LLA: free( pbDontTouteSet );
1002// LLA: free( pbDontTouteGet );
1134
1003
1135 CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.",
1136 ( sal_True == bOK ) && (sal_True == bOK2) );
1004 ASSERT_TRUE(( sal_True == bOK ) && (sal_True == bOK2)) << "test for setOption function: set option of a socket and then check.";
1137
1005
1138 free( pbDontRouteSet );
1139 free( pGetBuffer );
1140// LLA: CPPUNIT_ASSERT_MESSAGE( suError, sal_True == bOK );
1141 }
1006 free( pbDontRouteSet );
1007 free( pGetBuffer );
1008// LLA: ASSERT_TRUE(sal_True == bOK) << suError;
1009 }
1142
1010
1143 void setOption_002()
1144 {
1145 /// set and get option.
1011 TEST_F(setOption, setOption_002)
1012 {
1013 /// set and get option.
1146
1014
1147 // sal_Int32 * pbLingerSet = ( sal_Int32 * )malloc( nBufferLen );
1148 // *pbLingerSet = 7;
1149 // sal_Int32 * pbLingerGet = ( sal_Int32 * )malloc( nBufferLen );
1150 /* struct */linger aLingerSet;
1151 sal_Int32 nBufferLen = sizeof( struct linger );
1152 aLingerSet.l_onoff = 1;
1153 aLingerSet.l_linger = 7;
1015 // sal_Int32 * pbLingerSet = ( sal_Int32 * )malloc( nBufferLen );
1016 // *pbLingerSet = 7;
1017 // sal_Int32 * pbLingerGet = ( sal_Int32 * )malloc( nBufferLen );
1018 /* struct */linger aLingerSet;
1019 sal_Int32 nBufferLen = sizeof( struct linger );
1020 aLingerSet.l_onoff = 1;
1021 aLingerSet.l_linger = 7;
1154
1022
1155 linger aLingerGet;
1023 linger aLingerGet;
1156
1024
1157 asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen );
1025 asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen );
1158
1026
1159 sal_Int32 n1 = asAcceptorSocket.getOption( osl_Socket_OptionLinger, &aLingerGet, nBufferLen );
1160 CPPUNIT_ASSERT_MESSAGE( "getOption (SO_LINGER) function failed.", ( n1 == nBufferLen ) );
1027 sal_Int32 n1 = asAcceptorSocket.getOption( osl_Socket_OptionLinger, &aLingerGet, nBufferLen );
1028 ASSERT_TRUE(( n1 == nBufferLen )) << "getOption (SO_LINGER) function failed.";
1161
1029
1162 //t_print("#setOption_002: getOption is %d \n", aLingerGet.l_linger);
1163 sal_Bool bOK = ( 7 == aLingerGet.l_linger );
1164 CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check. ",
1165 sal_True == bOK );
1030 //printf("#setOption_002: getOption is %d \n", aLingerGet.l_linger);
1031 sal_Bool bOK = ( 7 == aLingerGet.l_linger );
1032 ASSERT_TRUE(sal_True == bOK) << "test for setOption function: set option of a socket and then check. ";
1166
1033
1167 }
1034 }
1168
1035
1169 void setOption_003()
1170 {
1171 linger aLingerSet;
1172 aLingerSet.l_onoff = 1;
1173 aLingerSet.l_linger = 7;
1036 TEST_F(setOption, setOption_003)
1037 {
1038 linger aLingerSet;
1039 aLingerSet.l_onoff = 1;
1040 aLingerSet.l_linger = 7;
1174
1041
1175 sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, 0 );
1176 printUString( asAcceptorSocket.getErrorAsString( ) );
1177 CPPUNIT_ASSERT_MESSAGE( "setOption (SO_LINGER) function failed for optlen is 0.",
1178 ( b1 == sal_False ) );
1179 }
1042 sal_Bool b1 = asAcceptorSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, 0 );
1043 printUString( asAcceptorSocket.getErrorAsString( ) );
1044 ASSERT_TRUE(( b1 == sal_False )) << "setOption (SO_LINGER) function failed for optlen is 0.";
1045 }
1180
1046
1181 void setOption_simple_001()
1182 {
1183 /// set and get option.
1184 asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, 1 ); //sal_True );
1185 sal_Bool bOK = ( 0 != asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) );
1047 TEST_F(setOption, setOption_simple_001)
1048 {
1049 /// set and get option.
1050 asAcceptorSocket.setOption( osl_Socket_OptionDontRoute, 1 ); //sal_True );
1051 sal_Bool bOK = ( 0 != asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) );
1186
1052
1187 t_print("setOption_simple_001(): getoption is %d \n", asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) );
1188 CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.",
1189 ( sal_True == bOK ) );
1190 }
1053 printf("setOption_simple_001(): getoption is %d \n", asAcceptorSocket.getOption( osl_Socket_OptionDontRoute ) );
1054 ASSERT_TRUE(( sal_True == bOK )) << "test for setOption function: set option of a socket and then check.";
1055 }
1191
1056
1192 void setOption_simple_002()
1193 {
1194 /// set and get option.
1195 // LLA: this does not work, due to the fact that SO_LINGER is a structure
1196// LLA: asAcceptorSocket.setOption( osl_Socket_OptionLinger, 7 );
1197// LLA: sal_Bool bOK = ( 7 == asAcceptorSocket.getOption( osl_Socket_OptionLinger ) );
1057 TEST_F(setOption, setOption_simple_002)
1058 {
1059 /// set and get option.
1060 // LLA: this does not work, due to the fact that SO_LINGER is a structure
1061// LLA: asAcceptorSocket.setOption( osl_Socket_OptionLinger, 7 );
1062// LLA: sal_Bool bOK = ( 7 == asAcceptorSocket.getOption( osl_Socket_OptionLinger ) );
1198
1063
1199// LLA: CPPUNIT_ASSERT_MESSAGE( "test for setOption function: set option of a socket and then check.",
1200// LLA: ( sal_True == bOK ) );
1201 }
1064// LLA: ASSERT_TRUE(// LLA: ( sal_True == bOK )) << "test for setOption function: set option of a socket and then check.";
1065 }
1202
1066
1203 CPPUNIT_TEST_SUITE( setOption );
1204 CPPUNIT_TEST( setOption_001 );
1205 CPPUNIT_TEST( setOption_002 );
1206 CPPUNIT_TEST( setOption_003 );
1207 CPPUNIT_TEST( setOption_simple_001 );
1208// LLA: CPPUNIT_TEST( setOption_simple_002 );
1209 CPPUNIT_TEST_SUITE_END();
1210
1067
1211 }; // class setOption
1212
1213
1214
1215 /** testing the method:
1216 inline sal_Bool SAL_CALL enableNonBlockingMode( sal_Bool bNonBlockingMode);
1217 */
1068 /** testing the method:
1069 inline sal_Bool SAL_CALL enableNonBlockingMode( sal_Bool bNonBlockingMode);
1070 */
1218 class enableNonBlockingMode : public CppUnit::TestFixture
1071 class enableNonBlockingMode : public ::testing::Test
1219 {
1220 public:
1221 ::osl::AcceptorSocket asAcceptorSocket;
1072 {
1073 public:
1074 ::osl::AcceptorSocket asAcceptorSocket;
1075 }; // class enableNonBlockingMode
1222
1076
1223 void enableNonBlockingMode_001()
1224 {
1225 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT );
1226 ::osl::StreamSocket ssConnection;
1077 TEST_F(enableNonBlockingMode, enableNonBlockingMode_001)
1078 {
1079 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT );
1080 ::osl::StreamSocket ssConnection;
1227
1081
1228 /// launch server socket
1229 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1230 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
1231 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
1232 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
1233 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
1234 asAcceptorSocket.enableNonBlockingMode( sal_True );
1235 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
1082 /// launch server socket
1083 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1084 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
1085 ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed.";
1086 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
1087 ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed.";
1088 asAcceptorSocket.enableNonBlockingMode( sal_True );
1089 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
1236
1090
1237 /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default.
1238 sal_Bool bOK = sal_True;
1239 asAcceptorSocket.close( );
1091 /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default.
1092 sal_Bool bOK = sal_True;
1093 asAcceptorSocket.close( );
1240
1094
1241 CPPUNIT_ASSERT_MESSAGE( "test for enableNonBlockingMode function: launch a server socket and make it non blocking. if it can pass the acceptConnection statement, it is non-blocking",
1242 ( sal_True == bOK ) );
1243 }
1095 ASSERT_TRUE(( sal_True == bOK )) << "test for enableNonBlockingMode function: launch a server socket and make it non blocking. if it can pass the acceptConnection statement, it is non-blocking";
1096 }
1244
1245
1097
1098
1246 CPPUNIT_TEST_SUITE( enableNonBlockingMode );
1247 CPPUNIT_TEST( enableNonBlockingMode_001 );
1248 CPPUNIT_TEST_SUITE_END();
1249
1250 }; // class enableNonBlockingMode
1251
1252
1253 /** testing the method:
1254 inline sal_Bool SAL_CALL isNonBlockingMode() const;
1255 */
1099 /** testing the method:
1100 inline sal_Bool SAL_CALL isNonBlockingMode() const;
1101 */
1256 class isNonBlockingMode : public CppUnit::TestFixture
1102 class isNonBlockingMode : public ::testing::Test
1257 {
1258 public:
1259 ::osl::AcceptorSocket asAcceptorSocket;
1103 {
1104 public:
1105 ::osl::AcceptorSocket asAcceptorSocket;
1106 }; // class isNonBlockingMode
1260
1107
1261 void isNonBlockingMode_001()
1262 {
1263 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT );
1264 ::osl::StreamSocket ssConnection;
1108 TEST_F(isNonBlockingMode, isNonBlockingMode_001)
1109 {
1110 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT );
1111 ::osl::StreamSocket ssConnection;
1265
1112
1266 /// launch server socket
1267 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
1268 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
1269 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket bind address failed.", sal_True == bOK1 );
1270 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
1271 CPPUNIT_ASSERT_MESSAGE( "AcceptorSocket listen failed.", sal_True == bOK2 );
1113 /// launch server socket
1114 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); // sal_True);
1115 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
1116 ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed.";
1117 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
1118 ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed.";
1272
1119
1273 sal_Bool bOK3 = asAcceptorSocket.isNonBlockingMode( );
1274 asAcceptorSocket.enableNonBlockingMode( sal_True );
1275 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
1120 sal_Bool bOK3 = asAcceptorSocket.isNonBlockingMode( );
1121 asAcceptorSocket.enableNonBlockingMode( sal_True );
1122 asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
1276
1123
1277 /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default.
1278 sal_Bool bOK4 = asAcceptorSocket.isNonBlockingMode( );
1279 asAcceptorSocket.close( );
1124 /// if reach this statement, it is non-blocking mode, since acceptConnection will blocked by default.
1125 sal_Bool bOK4 = asAcceptorSocket.isNonBlockingMode( );
1126 asAcceptorSocket.close( );
1280
1127
1281 CPPUNIT_ASSERT_MESSAGE( "test for isNonBlockingMode function: launch a server socket and make it non blocking. it is expected to change from blocking mode to non-blocking mode.",
1282 ( sal_False == bOK3 ) && ( sal_True == bOK4 ) );
1283 }
1128 ASSERT_TRUE(( sal_False == bOK3 ) && ( sal_True == bOK4 )) << "test for isNonBlockingMode function: launch a server socket and make it non blocking. it is expected to change from blocking mode to non-blocking mode.";
1129 }
1284
1130
1285
1286 CPPUNIT_TEST_SUITE( isNonBlockingMode );
1287 CPPUNIT_TEST( isNonBlockingMode_001 );
1288 CPPUNIT_TEST_SUITE_END();
1289
1290 }; // class isNonBlockingMode
1291
1292 /** testing the method:
1293 inline void SAL_CALL clearError() const;
1294 */
1131 /** testing the method:
1132 inline void SAL_CALL clearError() const;
1133 */
1295 class clearError : public CppUnit::TestFixture
1134 class clearError : public ::testing::Test
1296 {
1297 public:
1298 oslSocket sHandle;
1299 // initialization
1135 {
1136 public:
1137 oslSocket sHandle;
1138 // initialization
1300 void setUp( )
1139 void SetUp( )
1301 {
1302 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1303 }
1304
1140 {
1141 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1142 }
1143
1305 void tearDown( )
1144 void TearDown( )
1306 {
1307 sHandle = NULL;
1308 }
1145 {
1146 sHandle = NULL;
1147 }
1309
1310
1311 void clearError_001()
1312 {
1313 ::osl::Socket sSocket(sHandle);
1314 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_HTTP2 );
1315 ::osl::SocketAddr saLocalSocketAddr;
1316 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1317 sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail"
1318 oslSocketError seBind = sSocket.getError( );
1319 sSocket.clearError( );
1320
1321 CPPUNIT_ASSERT_MESSAGE( "test for clearError function: trick an error called sSocket.getError( ), and then clear the error states, check the result.",
1322 osl_Socket_E_None == sSocket.getError( ) && seBind != osl_Socket_E_None );
1323 }
1324
1325
1326 CPPUNIT_TEST_SUITE( clearError );
1327 CPPUNIT_TEST( clearError_001 );
1328 CPPUNIT_TEST_SUITE_END();
1329
1330 }; // class clearError
1331
1148 }; // class clearError
1149
1150 TEST_F(clearError, clearError_001)
1151 {
1152 ::osl::Socket sSocket(sHandle);
1153 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_HTTP2 );
1154 ::osl::SocketAddr saLocalSocketAddr;
1155 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1156 sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail"
1157 oslSocketError seBind = sSocket.getError( );
1158 sSocket.clearError( );
1332
1159
1160 ASSERT_TRUE(osl_Socket_E_None == sSocket.getError( ) && seBind != osl_Socket_E_None) << "test for clearError function: trick an error called sSocket.getError( ), and then clear the error states, check the result.";
1161 }
1162
1333 /** testing the methods:
1334 inline oslSocketError getError() const;
1335 inline ::rtl::OUString getErrorAsString( ) const;
1336 */
1163 /** testing the methods:
1164 inline oslSocketError getError() const;
1165 inline ::rtl::OUString getErrorAsString( ) const;
1166 */
1337 class getError : public CppUnit::TestFixture
1167 class getError : public ::testing::Test
1338 {
1339 public:
1340 oslSocket sHandle;
1341 // initialization
1168 {
1169 public:
1170 oslSocket sHandle;
1171 // initialization
1342 void setUp( )
1172 void SetUp( )
1343 {
1344 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1345 }
1346
1173 {
1174 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1175 }
1176
1347 void tearDown( )
1177 void TearDown( )
1348 {
1349 sHandle = NULL;
1350 }
1178 {
1179 sHandle = NULL;
1180 }
1181 }; // class getError
1351
1182
1183 TEST_F(getError, getError_001)
1184 {
1185 ::osl::Socket sSocket(sHandle);
1186 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP );
1187 ::osl::SocketAddr saLocalSocketAddr;
1352
1188
1353 void getError_001()
1354 {
1355 ::osl::Socket sSocket(sHandle);
1356 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_FTP );
1357 ::osl::SocketAddr saLocalSocketAddr;
1189 ASSERT_TRUE(osl_Socket_E_None == sSocket.getError( )) << "test for getError function: should get no error.";
1190 }
1358
1191
1359 CPPUNIT_ASSERT_MESSAGE( "test for getError function: should get no error.",
1360 osl_Socket_E_None == sSocket.getError( ) );
1361 }
1362
1363 void getError_002()
1364 {
1365 ::osl::Socket sSocket(sHandle);
1366 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_FTP );
1367 ::osl::SocketAddr saLocalSocketAddr;
1368 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1369 sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail"
1370 //on Solaris, the error no is EACCES, but it has no mapped value, so getError() returned osl_Socket_E_InvalidError.
1192 TEST_F(getError, getError_002)
1193 {
1194 ::osl::Socket sSocket(sHandle);
1195 ::osl::SocketAddr saBindSocketAddr( rtl::OUString::createFromAscii("123.45.67.89"), IP_PORT_FTP );
1196 ::osl::SocketAddr saLocalSocketAddr;
1197 sSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1198 sSocket.bind( saBindSocketAddr );//build an error "osl_Socket_E_AddrNotAvail"
1199 //on Solaris, the error no is EACCES, but it has no mapped value, so getError() returned osl_Socket_E_InvalidError.
1371#if defined(SOLARIS)
1200#if defined(SOLARIS)
1372 CPPUNIT_ASSERT_MESSAGE( "trick an error called sSocket.getError( ), check the getError result.Failed on Solaris, returned osl_Socket_E_InvalidError because no entry to map the errno EACCES. ",
1373 osl_Socket_E_InvalidError == sSocket.getError( ) );
1201 ASSERT_TRUE(osl_Socket_E_InvalidError == sSocket.getError( )) << "trick an error called sSocket.getError( ), check the getError result.Failed on Solaris, returned osl_Socket_E_InvalidError because no entry to map the errno EACCES. ";
1374#else
1202#else
1375 //while on Linux & Win32, the errno is EADDRNOTAVAIL, getError returned osl_Socket_E_AddrNotAvail.
1203 //while on Linux & Win32, the errno is EADDRNOTAVAIL, getError returned osl_Socket_E_AddrNotAvail.
1376
1204
1377 CPPUNIT_ASSERT_MESSAGE( "trick an error called sSocket.getError( ), check the getError result.Failed on Solaris, returned osl_Socket_E_InvalidError because no entry to map the errno EACCES. Passed on Linux & Win32",
1378 osl_Socket_E_AddrNotAvail == sSocket.getError( ) );
1205 ASSERT_TRUE(osl_Socket_E_AddrNotAvail == sSocket.getError( )) << "trick an error called sSocket.getError( ), check the getError result.Failed on Solaris, returned osl_Socket_E_InvalidError because no entry to map the errno EACCES. Passed on Linux & Win32";
1379#endif
1206#endif
1380 }
1207 }
1381
1208
1382 CPPUNIT_TEST_SUITE( getError );
1383 CPPUNIT_TEST( getError_001 );
1384 CPPUNIT_TEST( getError_002 );
1385 CPPUNIT_TEST_SUITE_END();
1386
1209
1387 }; // class getError
1388
1389
1390
1391 /** testing the methods:
1392 inline oslSocket getHandle() const;
1393 */
1394
1210 /** testing the methods:
1211 inline oslSocket getHandle() const;
1212 */
1213
1395 class getHandle : public CppUnit::TestFixture
1214 class getHandle : public ::testing::Test
1396 {
1397 public:
1398 oslSocket sHandle;
1399 // initialization
1215 {
1216 public:
1217 oslSocket sHandle;
1218 // initialization
1400 void setUp( )
1219 void SetUp( )
1401 {
1402 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1403 }
1404
1220 {
1221 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
1222 }
1223
1405 void tearDown( )
1224 void TearDown( )
1406 {
1407 sHandle = NULL;
1408 }
1225 {
1226 sHandle = NULL;
1227 }
1409
1410 void getHandle_001()
1411 {
1412 ::osl::Socket sSocket(sHandle);
1413 ::osl::Socket assignSocket = sSocket.getHandle();
1414
1415 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment_handle function: test the assignment operator.",
1416 osl_Socket_TypeStream == assignSocket.getType( ) );
1417 }
1418
1419 void getHandle_002()
1420 {
1421 ::osl::Socket sSocket( sHandle );
1422 ::osl::Socket assignSocket ( sSocket.getHandle( ) );
1423
1424 CPPUNIT_ASSERT_MESSAGE( "test for operators_assignment function: assignment operator",
1425 osl_Socket_TypeStream == assignSocket.getType( ) );
1426 }
1427
1428 CPPUNIT_TEST_SUITE( getHandle );
1429 CPPUNIT_TEST( getHandle_001 );
1430 CPPUNIT_TEST( getHandle_002 );
1431 CPPUNIT_TEST_SUITE_END();
1432
1433 }; // class getHandle
1434
1228 }; // class getHandle
1229
1230 TEST_F(getHandle, getHandle_001)
1231 {
1232 ::osl::Socket sSocket(sHandle);
1233 ::osl::Socket assignSocket = sSocket.getHandle();
1435
1234
1436// -----------------------------------------------------------------------------
1235 ASSERT_TRUE(osl_Socket_TypeStream == assignSocket.getType( )) << "test for operators_assignment_handle function: test the assignment operator.";
1236 }
1437
1237
1238 TEST_F(getHandle, getHandle_002)
1239 {
1240 ::osl::Socket sSocket( sHandle );
1241 ::osl::Socket assignSocket ( sSocket.getHandle( ) );
1438
1242
1439CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::ctors, "osl_Socket");
1440CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::operators, "osl_Socket");
1441CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::close, "osl_Socket");
1442CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalAddr, "osl_Socket");
1443CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalPort, "osl_Socket");
1444CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getLocalHost, "osl_Socket");
1445CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getPeer, "osl_Socket");
1446CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::bind, "osl_Socket");
1447CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isRecvReady, "osl_Socket");
1448CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isSendReady, "osl_Socket");
1449CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getType, "osl_Socket");
1450CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getOption, "osl_Socket");
1451CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::setOption, "osl_Socket");
1452CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::enableNonBlockingMode, "osl_Socket");
1453CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::isNonBlockingMode, "osl_Socket");
1454CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::clearError, "osl_Socket");
1455CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getError, "osl_Socket");
1456CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Socket::getHandle, "osl_Socket");
1243 ASSERT_TRUE(osl_Socket_TypeStream == assignSocket.getType( )) << "test for operators_assignment function: assignment operator";
1244 }
1457
1245
1246
1458} // namespace osl_Socket
1459
1247} // namespace osl_Socket
1248
1460// -----------------------------------------------------------------------------
1461
1462// this macro creates an empty function, which will called by the RegisterAllFunctions()
1463// to let the user the possibility to also register some functions by hand.
1464NOADDITIONAL;
1249int main(int argc, char **argv)
1250{
1251 ::testing::InitGoogleTest(&argc, argv);
1252 return RUN_ALL_TESTS();
1253}