Linux x86 build fix
[LibreOffice.git] / io / test / testconnection.cxx
blob2e60af472d2d1cb2124d2cb5a1daf21780381474
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <stdio.h>
21 #include <osl/time.h>
23 #include <osl/diagnose.h>
24 #include <osl/thread.hxx>
26 #include <cppuhelper/servicefactory.hxx>
28 #include <com/sun/star/lang/XComponent.hpp>
30 #include <com/sun/star/registry/XImplementationRegistration.hpp>
32 #include <com/sun/star/connection/XConnector.hpp>
33 #include <com/sun/star/connection/XAcceptor.hpp>
35 using namespace ::osl;
36 using namespace ::cppu;
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::io;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::registry;
41 using namespace ::com::sun::star::connection;
44 class MyThread :
45 public Thread
47 public:
48 MyThread( const Reference< XAcceptor > &r , const OUString & sConnectionDescription) :
49 m_rAcceptor( r ),
50 m_sConnectionDescription( sConnectionDescription )
52 virtual void SAL_CALL run();
54 Reference < XAcceptor > m_rAcceptor;
55 private:
56 Reference < XConnection > m_rConnection;
57 OUString m_sConnectionDescription;
60 void doWrite( const Reference < XConnection > &r )
62 Sequence < sal_Int8 > seq(10);
63 for( sal_Int32 i = 0 ; i < 10 ; i ++ )
65 seq.getArray()[i] = i;
68 r->write( seq );
71 void doRead( const Reference < XConnection > &r )
73 Sequence < sal_Int8 > seq(10);
75 OSL_ASSERT( 10 == r->read( seq , 10 ) );
77 for( sal_Int32 i = 0 ; i < 10 ; i ++ )
79 OSL_ASSERT( seq.getConstArray()[i] == i );
84 void MyThread::run()
86 try
88 m_rConnection = m_rAcceptor->accept( m_sConnectionDescription );
90 catch ( const Exception &e)
92 OString tmp= OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
93 printf( "Exception was thrown by acceptor thread: %s\n", tmp.getStr() );
96 if( m_rConnection.is() )
98 Sequence < sal_Int8 > seq(12);
99 try
101 doWrite( m_rConnection );
102 doRead( m_rConnection );
104 catch (... )
106 printf( "unknown exception was thrown\n" );
107 throw;
117 void testConnection( const OUString &sConnectionDescription ,
118 const Reference < XAcceptor > &rAcceptor,
119 const Reference < XConnector > &rConnector )
122 MyThread thread( rAcceptor , sConnectionDescription );
123 thread.create();
125 sal_Bool bGotit = sal_False;
126 Reference < XConnection > r;
128 while( ! bGotit )
132 // Why is this wait necessary ????
133 TimeValue value = {1,0};
134 osl_waitThread( &value );
135 r = rConnector->connect( sConnectionDescription );
136 OSL_ASSERT( r.is() );
137 doWrite( r );
138 doRead( r );
139 bGotit = sal_True;
141 catch( ... )
143 printf( "Couldn't connect, retrying ...\n" );
148 r->close();
152 Sequence < sal_Int8 > seq(10);
153 r->write( seq );
154 OSL_FAIL( "expected exception not thrown" );
156 catch ( IOException & )
158 // everything is ok
160 catch ( ... )
162 OSL_FAIL( "wrong exception was thrown" );
165 thread.join();
170 int SAL_CALL main( int argc, char * argv[] )
172 Reference< XMultiServiceFactory > xMgr(
173 createRegistryServiceFactory( OUString( "applicat.rdb") ) );
175 Reference< XImplementationRegistration > xImplReg(
176 xMgr->createInstance("com.sun.star.registry.ImplementationRegistration"), UNO_QUERY );
177 OSL_ENSURE( xImplReg.is(), "### no impl reg!" );
179 OUString aLibName = "connector.uno" SAL_DLLEXTENSION;
180 xImplReg->registerImplementation(
181 OUString("com.sun.star.loader.SharedLibrary"), aLibName, Reference< XSimpleRegistry >() );
183 aLibName = "acceptor.uno" SAL_DLLEXTENSION;
184 xImplReg->registerImplementation(
185 OUString("com.sun.star.loader.SharedLibrary"), aLibName, Reference< XSimpleRegistry >() );
187 Reference < XAcceptor > rAcceptor(
188 xMgr->createInstance(
189 OUString("com.sun.star.connection.Acceptor") ) , UNO_QUERY );
191 Reference < XAcceptor > rAcceptorPipe(
192 xMgr->createInstance(
193 OUString("com.sun.star.connection.Acceptor") ) , UNO_QUERY );
195 Reference < XConnector > rConnector(
196 xMgr->createInstance("com.sun.star.connection.Connector") , UNO_QUERY );
199 printf( "Testing sockets" );
200 fflush( stdout );
201 testConnection( OUString("socket,host=localhost,port=2001"), rAcceptor , rConnector );
202 printf( " Done\n" );
204 printf( "Testing pipe" );
205 fflush( stdout );
206 testConnection( OUString("pipe,name=bla") , rAcceptorPipe , rConnector );
207 printf( " Done\n" );
209 // check, if errornous strings make any problem
210 rAcceptor = Reference< XAcceptor > (
211 xMgr->createInstance("com.sun.star.connection.Acceptor"),
212 UNO_QUERY );
216 rAcceptor->accept( OUString() );
217 OSL_FAIL( "empty connection string" );
219 catch( IllegalArgumentException & )
221 // everything is fine
223 catch( ... )
225 OSL_FAIL( "unexpected akexception with empty connection string" );
230 rConnector->connect( OUString() );
231 OSL_FAIL( "empty connection string" );
233 catch( ConnectionSetupException & )
235 // everything is fine
237 catch( ... )
239 OSL_FAIL( "unexpected exception with empty connection string" );
243 MyThread thread( rAcceptor , OUString("socket,host=localhost,port=2001") );
244 thread.create();
246 TimeValue value = {0,1};
247 osl_waitThread( &value );
250 rAcceptor->accept( OUString("socket,host=localhost,port=2001") );
251 OSL_FAIL( "already existing exception expected" );
253 catch( AlreadyAcceptingException & )
255 // everything is fine
257 catch( ... )
259 OSL_FAIL( "unknown exception, already existing existing expected" );
262 rAcceptor->stopAccepting();
263 thread.join();
265 Reference < XComponent > rComp( xMgr , UNO_QUERY );
266 if( rComp.is() )
268 rComp->dispose();
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */