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
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sal.hxx"
26 
27 /**  test coder preface:
28 	1. the BSD socket function will meet "unresolved external symbol error" on Windows platform
29 	if you are not including ws2_32.lib in makefile.mk,  the including format will be like this:
30 
31 	.IF "$(GUI)" == "WNT"
32 	SHL1STDLIBS +=	$(SOLARLIBDIR)$/cppunit.lib
33 	SHL1STDLIBS +=  ws2_32.lib
34 	.ENDIF
35 
36 	likewise on Solaris platform.
37 	.IF "$(GUI)" == "UNX"
38 	SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a
39 	SHL1STDLIBS += -lsocket -ldl -lnsl
40 	.ENDIF
41 
42 	2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4
43 	category.
44 
45 	3. some fragment of Socket source implementation are lack of comment so it is hard for testers
46 	guess what the exact functionality or usage of a member.  Hope the Socket section's comment
47 	will be added.
48 
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 "gtest/gtest.h"
58 
59 #include "osl_Socket_Const.h"
60 #include "sockethelper.hxx"
61 
62 using namespace osl;
63 using namespace rtl;
64 
65 #define IP_PORT_MYPORT2 8883
66 #define IP_PORT_FTP     21
67 #define IP_PORT_MYPORT3 8884
68 
69 //------------------------------------------------------------------------
70 // helper functions
71 //------------------------------------------------------------------------
72 
73 class CloseSocketThread : public Thread
74 {
75 	::osl::Socket &m_sSocket;
76 protected:
run()77 	void SAL_CALL run( )
78 	{
79 		thread_sleep( 1 );
80 		m_sSocket.close( );
81 	}
82 public:
CloseSocketThread(::osl::Socket & sSocket)83 	CloseSocketThread(::osl::Socket & sSocket )
84 		: m_sSocket( sSocket )
85 	{
86 	}
87 
~CloseSocketThread()88 	~CloseSocketThread( )
89 	{
90 		if ( isRunning( ) )
91 		{
92 			printf("# error: CloseSocketThread not terminated.\n" );
93 		}
94 	}
95 };
96 
97 namespace osl_ConnectorSocket
98 {
99 
100 	/** testing the method:
101 		ConnectorSocket(oslAddrFamily Family = osl_Socket_FamilyInet,
102 						oslProtocol	Protocol = osl_Socket_ProtocolIp,
103 						oslSocketType	Type = osl_Socket_TypeStream);
104 	*/
105 
106 	class ctors : public ::testing::Test
107 	{
108 	public:
109 	}; // class ctors
110 
TEST_F(ctors,ctors_001)111     TEST_F(ctors, ctors_001)
112     {
113         /// Socket constructor.
114         ::osl::ConnectorSocket csSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
115 
116         ASSERT_TRUE( osl_Socket_TypeStream ==  csSocket.getType( ) )
117             << "test for ctors_001 constructor function: check if the connector socket was created successfully.";
118     }
119 
120 	/** testing the method:
121 		oslSocketResult SAL_CALL connect(const SocketAddr& TargetHost, const TimeValue* pTimeout = 0);
122 	*/
123 
124 	class connect : public ::testing::Test
125 	{
126 	public:
127 		TimeValue *pTimeout;
128 		::osl::AcceptorSocket asAcceptorSocket;
129 		::osl::ConnectorSocket csConnectorSocket;
130 
131 
132 		// initialization
SetUp()133 		void SetUp( )
134 		{
135 			pTimeout  = ( TimeValue* )malloc( sizeof( TimeValue ) );
136 			pTimeout->Seconds = 3;
137 			pTimeout->Nanosec = 0;
138 		//	sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
139 		}
140 
TearDown()141 		void TearDown( )
142 		{
143 			free( pTimeout );
144 		//	sHandle = NULL;
145 			asAcceptorSocket.close( );
146 			csConnectorSocket.close( );
147 		}
148 	}; // class connect
149 
TEST_F(connect,connect_001)150     TEST_F(connect, connect_001)
151     {
152         ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT2 );
153         ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT2 );
154         ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
155         ::osl::StreamSocket ssConnection;
156 
157         /// launch server socket
158         asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
159         sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
160         ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed.";
161         sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
162         ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed.";
163 
164         //asAcceptorSocket.enableNonBlockingMode( sal_True );
165         //oslSocketResult eResultAccept = asAcceptorSocket.acceptConnection(ssConnection); /// waiting for incoming connection...
166         //ASSERT_TRUE(osl_Socket_Ok == eResultAccept) << "accept failed.";
167         /// launch client socket
168         oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout );   /// connecting to server...
169         ASSERT_TRUE(osl_Socket_Ok == eResult) << "connect failed.";
170 
171         /// get peer information
172         csConnectorSocket.getPeerAddr( saPeerSocketAddr );/// connected.
173 
174         ASSERT_TRUE(
175                                 ( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr ) ) &&
176                                 ( osl_Socket_Ok == eResult ))
177             << "test for connect function: try to create a connection with remote host. and check the setup address.";
178     }
179     //non-blocking mode connect?
TEST_F(connect,connect_002)180     TEST_F(connect, connect_002)
181     {
182         ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 );
183         ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 );
184         ::osl::SocketAddr saPeerSocketAddr( rtl::OUString::createFromAscii("129.158.217.202"), IP_PORT_FTP );
185 
186         asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
187         asAcceptorSocket.enableNonBlockingMode( sal_True );
188         sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
189         ASSERT_TRUE(sal_True == bOK1) << "AcceptorSocket bind address failed.";
190         sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
191         ASSERT_TRUE(sal_True == bOK2) << "AcceptorSocket listen failed.";
192 
193         csConnectorSocket.enableNonBlockingMode( sal_True );
194 
195         oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout );   /// connecting to server...
196         ASSERT_TRUE(osl_Socket_InProgress == eResult ||  osl_Socket_Ok == eResult) << "connect failed.";
197 
198         /// get peer information
199         csConnectorSocket.getPeerAddr( saPeerSocketAddr );
200 
201         ASSERT_TRUE( sal_True == compareSocketAddr( saPeerSocketAddr, saLocalSocketAddr  )  )
202             << "test for connect function: try to create a connection with remote host. and check the setup address.";
203     }
204     // really an error or just delayed
205     // how to design scenarios that will return osl_Socket_Interrupted, osl_Socket_TimedOut
TEST_F(connect,connect_003)206     TEST_F(connect, connect_003)
207     {
208         ::osl::SocketAddr saTargetSocketAddr1( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT3 );
209         ::osl::SocketAddr saTargetSocketAddr2( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT3 );
210 
211         csConnectorSocket.enableNonBlockingMode( sal_False );
212 
213         oslSocketResult eResult1 = csConnectorSocket.connect( saTargetSocketAddr1, pTimeout );
214         oslSocketResult eResult2 = csConnectorSocket.connect( saTargetSocketAddr2, pTimeout );
215         CloseSocketThread myCloseThread( csConnectorSocket );
216         oslSocketResult eResult3 = csConnectorSocket.connect( saTargetSocketAddr2, pTimeout );
217         myCloseThread.join();
218         ASSERT_TRUE(osl_Socket_Error == eResult1 &&
219             osl_Socket_Error == eResult2 &&  osl_Socket_Error == eResult3) << "connect should failed.";
220 
221     }
222 
223     // really an error in non-blocking mode
TEST_F(connect,connect_004)224     TEST_F(connect, connect_004)
225     {
226         ::osl::SocketAddr saTargetSocketAddr( rtl::OUString::createFromAscii("123.345.67.89"), IP_PORT_MYPORT3 );
227 
228         csConnectorSocket.enableNonBlockingMode( sal_True );
229 
230         oslSocketResult eResult = csConnectorSocket.connect( saTargetSocketAddr, pTimeout );   /// connecting to server...
231         ASSERT_TRUE(osl_Socket_Error == eResult) << "connect should failed.";
232     }
233     /** here need a case: immediate connection, say in non-blocking mode connect return osl_Socket_Ok
234     */
235 
236 } // namespace osl_ConnectorSocket
237 
main(int argc,char ** argv)238 int main(int argc, char **argv)
239 {
240     ::testing::InitGoogleTest(&argc, argv);
241     return RUN_ALL_TESTS();
242 }
243