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_testtools.hxx"
26 
27 #include <stdio.h>
28 #include <string.h>
29 #include <osl/diagnose.h>
30 #include "osl/diagnose.hxx"
31 #include <osl/time.h>
32 #include <sal/types.h>
33 #include "typelib/typedescription.hxx"
34 #include <uno/dispatcher.hxx>
35 #include "uno/mapping.hxx"
36 #include <uno/data.h>
37 #include "uno/environment.hxx"
38 
39 #include <cppuhelper/factory.hxx>
40 #include <cppuhelper/implbase2.hxx>
41 #include <cppuhelper/implbase1.hxx>
42 
43 #include <com/sun/star/lang/XServiceInfo.hpp>
44 #include <com/sun/star/lang/XComponent.hpp>
45 #include <com/sun/star/lang/XMain.hpp>
46 #include <com/sun/star/bridge/UnoUrlResolver.hpp>
47 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
48 #include "com/sun/star/uno/RuntimeException.hpp"
49 #include "com/sun/star/uno/Type.hxx"
50 
51 #include "test/testtools/bridgetest/BadConstructorArguments.hpp"
52 #include "test/testtools/bridgetest/TestPolyStruct.hpp"
53 #include "test/testtools/bridgetest/XBridgeTest.hpp"
54 #include "test/testtools/bridgetest/XBridgeTest2.hpp"
55 #include "test/testtools/bridgetest/XMulti.hpp"
56 
57 #include "currentcontextchecker.hxx"
58 #include "multi.hxx"
59 
60 using namespace rtl;
61 using namespace osl;
62 using namespace cppu;
63 using namespace com::sun::star::uno;
64 using namespace com::sun::star::lang;
65 using namespace com::sun::star::registry;
66 using namespace com::sun::star::bridge;
67 using namespace test::testtools::bridgetest;
68 
69 #define SERVICENAME		"com.sun.star.test.bridge.BridgeTest"
70 #define IMPLNAME		"com.sun.star.comp.bridge.BridgeTest"
71 
72 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
73 #define STRING_TEST_CONSTANT "\" paco\' chorizo\\\' \"\'"
74 
75 namespace bridge_test
76 {
77 template< class T>
78 Sequence<T> cloneSequence(const Sequence<T>& val);
79 
80 //--------------------------------------------------------------------------------------------------
getSupportedServiceNames()81 inline static Sequence< OUString > getSupportedServiceNames()
82 {
83 	OUString aName( RTL_CONSTASCII_USTRINGPARAM(SERVICENAME) );
84 	return Sequence< OUString >( &aName, 1 );
85 }
86 
check(bool b,char const * message)87 static bool check( bool b , char const * message )
88 {
89     if ( ! b )
90         fprintf( stderr, "%s failed\n" , message );
91     return b;
92 }
93 
94 namespace {
95 
checkEmpty(rtl::OUString const & string,char const * message)96 bool checkEmpty(rtl::OUString const & string, char const * message) {
97     bool ok = string.getLength() == 0;
98     if (!ok) {
99         fprintf(
100             stderr, "%s failed: %s\n", message,
101             rtl::OUStringToOString(string, RTL_TEXTENCODING_UTF8).getStr());
102     }
103     return ok;
104 }
105 
106 }
107 
108 //==================================================================================================
109 class TestBridgeImpl : public osl::DebugBase<TestBridgeImpl>,
110                        public WeakImplHelper2< XMain, XServiceInfo >
111 {
112 	Reference< XComponentContext > m_xContext;
113 
114 public:
TestBridgeImpl(const Reference<XComponentContext> & xContext)115 	TestBridgeImpl( const Reference< XComponentContext > & xContext )
116 		: m_xContext( xContext )
117 		{}
~TestBridgeImpl()118     virtual ~TestBridgeImpl()
119     {
120     }
121 
122 	// XServiceInfo
123 	virtual OUString SAL_CALL getImplementationName() throw (RuntimeException);
124 	virtual sal_Bool SAL_CALL supportsService( const OUString & rServiceName ) throw (RuntimeException);
125 	virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw (RuntimeException);
126 
127 	// XMain
128     virtual sal_Int32 SAL_CALL run( const Sequence< OUString > & rArgs ) throw (RuntimeException);
129 };
130 
131 //==================================================================================================
equals(const TestElement & rData1,const TestElement & rData2)132 static sal_Bool equals( const TestElement & rData1, const TestElement & rData2 )
133 {
134 	check( rData1.Bool == rData2.Bool, "### bool does not match!" );
135 	check( rData1.Char == rData2.Char, "### char does not match!" );
136 	check( rData1.Byte == rData2.Byte, "### byte does not match!" );
137 	check( rData1.Short == rData2.Short, "### short does not match!" );
138 	check( rData1.UShort == rData2.UShort, "### unsigned short does not match!" );
139 	check( rData1.Long == rData2.Long, "### long does not match!" );
140 	check( rData1.ULong == rData2.ULong, "### unsigned long does not match!" );
141 	check( rData1.Hyper == rData2.Hyper, "### hyper does not match!" );
142 	check( rData1.UHyper == rData2.UHyper, "### unsigned hyper does not match!" );
143 	check( rData1.Float == rData2.Float, "### float does not match!" );
144 	check( rData1.Double == rData2.Double, "### double does not match!" );
145 	check( rData1.Enum == rData2.Enum, "### enum does not match!" );
146 	check( rData1.String == rData2.String, "### string does not match!" );
147 	check( rData1.Interface == rData2.Interface, "### interface does not match!" );
148 	check( rData1.Any == rData2.Any, "### any does not match!" );
149 
150 	return (rData1.Bool == rData2.Bool &&
151 			rData1.Char == rData2.Char &&
152 			rData1.Byte == rData2.Byte &&
153 			rData1.Short == rData2.Short &&
154 			rData1.UShort == rData2.UShort &&
155 			rData1.Long == rData2.Long &&
156 			rData1.ULong == rData2.ULong &&
157 			rData1.Hyper == rData2.Hyper &&
158 			rData1.UHyper == rData2.UHyper &&
159 			rData1.Float == rData2.Float &&
160 			rData1.Double == rData2.Double &&
161 			rData1.Enum == rData2.Enum &&
162 			rData1.String == rData2.String &&
163 			rData1.Interface == rData2.Interface &&
164 			rData1.Any == rData2.Any);
165 }
166 //==================================================================================================
equals(const TestData & rData1,const TestData & rData2)167 static sal_Bool equals( const TestData & rData1, const TestData & rData2 )
168 {
169 	sal_Int32 nLen;
170 
171 	if ((rData1.Sequence == rData2.Sequence) &&
172 		equals( (const TestElement &)rData1, (const TestElement &)rData2 ) &&
173 		(nLen = rData1.Sequence.getLength()) == rData2.Sequence.getLength())
174 	{
175 		// once again by hand sequence ==
176 		const TestElement * pElements1 = rData1.Sequence.getConstArray();
177 		const TestElement * pElements2 = rData2.Sequence.getConstArray();
178 		for ( ; nLen--; )
179 		{
180 			if (! equals( pElements1[nLen], pElements2[nLen] ))
181 			{
182 				check( sal_False, "### sequence element did not match!" );
183 				return sal_False;
184 			}
185 		}
186 		return sal_True;
187 	}
188 	return sal_False;
189 }
190 //==================================================================================================
assign(TestElement & rData,sal_Bool bBool,sal_Unicode cChar,sal_Int8 nByte,sal_Int16 nShort,sal_uInt16 nUShort,sal_Int32 nLong,sal_uInt32 nULong,sal_Int64 nHyper,sal_uInt64 nUHyper,float fFloat,double fDouble,TestEnum eEnum,const::rtl::OUString & rStr,const::com::sun::star::uno::Reference<::com::sun::star::uno::XInterface> & xTest,const::com::sun::star::uno::Any & rAny)191 static void assign( TestElement & rData,
192 					sal_Bool bBool, sal_Unicode cChar, sal_Int8 nByte,
193 					sal_Int16 nShort, sal_uInt16 nUShort,
194 					sal_Int32 nLong, sal_uInt32 nULong,
195 					sal_Int64 nHyper, sal_uInt64 nUHyper,
196 					float fFloat, double fDouble,
197 					TestEnum eEnum, const ::rtl::OUString& rStr,
198 					const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& xTest,
199 					const ::com::sun::star::uno::Any& rAny )
200 {
201 	rData.Bool = bBool;
202 	rData.Char = cChar;
203 	rData.Byte = nByte;
204 	rData.Short = nShort;
205 	rData.UShort = nUShort;
206 	rData.Long = nLong;
207 	rData.ULong = nULong;
208 	rData.Hyper = nHyper;
209 	rData.UHyper = nUHyper;
210 	rData.Float = fFloat;
211 	rData.Double = fDouble;
212 	rData.Enum = eEnum;
213 	rData.String = rStr;
214 	rData.Interface = xTest;
215 	rData.Any = rAny;
216 }
217 
218 namespace {
219 
220 template < typename T >
testAny(T const & value,Reference<XBridgeTest> const & xLBT,char const * typeName=0)221 bool testAny(
222     T const & value, Reference< XBridgeTest > const & xLBT,
223     char const * typeName = 0)
224 {
225 	Any any;
226 	any <<=  value;
227 	Any any2 = xLBT->transportAny(any);
228     bool success = true;
229 	if (any != any2) {
230         fprintf(
231             stderr, "any is different after roundtrip: in %s, out %s\n",
232             OUStringToOString(
233                 any.getValueType().getTypeName(),
234                 RTL_TEXTENCODING_ASCII_US).getStr(),
235             OUStringToOString(
236                 any2.getValueType().getTypeName(),
237                 RTL_TEXTENCODING_ASCII_US).getStr());
238         success = false;
239     }
240     if (typeName != 0
241         && !any2.getValueType().getTypeName().equalsAscii(typeName))
242     {
243         fprintf(
244             stderr, "any has wrong type after roundtrip: %s instead of %s\n",
245             OUStringToOString(
246                 any2.getValueType().getTypeName(),
247                 RTL_TEXTENCODING_ASCII_US).getStr(),
248             typeName);
249         success = false;
250     }
251 	return success;
252 }
253 
254 }
255 
performAnyTest(const Reference<XBridgeTest> & xLBT,const TestData & data)256 static sal_Bool performAnyTest( const Reference< XBridgeTest > &xLBT, const TestData &data)
257 {
258 	bool bReturn = true;
259 	bReturn = testAny( data.Byte ,xLBT ) && bReturn;
260 	bReturn = testAny( data.Short,xLBT ) && bReturn;
261 	bReturn = testAny( data.UShort,xLBT ) && bReturn;
262 	bReturn = testAny( data.Long,xLBT ) && bReturn;
263 	bReturn = testAny( data.ULong,xLBT ) && bReturn;
264 	bReturn = testAny( data.Hyper,xLBT ) && bReturn;
265 	bReturn = testAny( data.UHyper,xLBT ) && bReturn;
266 	bReturn = testAny( data.Float,xLBT ) && bReturn;
267 	bReturn = testAny( data.Double,xLBT ) && bReturn;
268 	bReturn = testAny( data.Enum,xLBT ) && bReturn;
269 	bReturn = testAny( data.String,xLBT ) && bReturn;
270 	bReturn = testAny( data.Interface,xLBT ) && bReturn;
271 	bReturn = testAny( data, xLBT ) && bReturn;
272     bReturn &= testAny(
273         TestPolyStruct< sal_Unicode >(' '), xLBT,
274         "test.testtools.bridgetest.TestPolyStruct<char>");
275 
276 	Any a;
277 	{
278 		a.setValue( &(data.Bool) , getCppuBooleanType() );
279 		Any a2 = xLBT->transportAny( a );
280 		OSL_ASSERT( a2 == a );
281 	}
282 
283 	{
284 		a.setValue( &(data.Char) , getCppuCharType() );
285 		Any a2 = xLBT->transportAny( a );
286 		OSL_ASSERT( a2 == a );
287 	}
288 
289 	return bReturn;
290 }
291 
292 //_______________________________________________________________________________________
performSequenceOfCallTest(const Reference<XBridgeTest> & xLBT)293 static sal_Bool performSequenceOfCallTest( const Reference < XBridgeTest > &xLBT )
294 {
295 	sal_Int32 i,nRounds;
296 	sal_Int32 nGlobalIndex = 0;
297 	const sal_Int32 nWaitTimeSpanMUSec = 10000;
298 	for( nRounds = 0 ; nRounds < 10 ; nRounds ++ )
299 	{
300 		for( i = 0 ; i < nRounds ; i ++ )
301 		{
302 			// fire oneways
303 			xLBT->callOneway( nGlobalIndex , nWaitTimeSpanMUSec );
304 			nGlobalIndex ++;
305 		}
306 
307 		// call synchron
308 		xLBT->call( nGlobalIndex , nWaitTimeSpanMUSec );
309 		nGlobalIndex ++;
310 	}
311 
312 	return xLBT->sequenceOfCallTestPassed();
313 }
314 
315 class ORecursiveCall : public WeakImplHelper1< XRecursiveCall >
316 {
317 private:
318 	Mutex m_mutex;
319 
320 public:
callRecursivly(const::com::sun::star::uno::Reference<XRecursiveCall> & xCall,sal_Int32 nToCall)321 	void SAL_CALL callRecursivly(
322 		const ::com::sun::star::uno::Reference< XRecursiveCall >& xCall,
323 		sal_Int32 nToCall )
324 		throw(::com::sun::star::uno::RuntimeException)
325 		{
326 			MutexGuard guard( m_mutex );
327 			if( nToCall )
328 			{
329 				nToCall --;
330 				xCall->callRecursivly( this , nToCall );
331 			}
332 
333 		}
334 };
335 
336 
337 //_______________________________________________________________________________________
performRecursiveCallTest(const Reference<XBridgeTest> & xLBT)338 static sal_Bool performRecursiveCallTest( const Reference < XBridgeTest > & xLBT )
339 {
340 	xLBT->startRecursiveCall( new ORecursiveCall , 50 );
341 	// on failure, the test would lock up or crash
342 	return sal_True;
343 }
344 
345 class MyClass : public osl::DebugBase<MyClass>, public OWeakObject
346 {
347 public:
348     MyClass();
349     virtual ~MyClass();
350     virtual void SAL_CALL acquire() throw ();
351     virtual void SAL_CALL release() throw ();
352 };
353 
354 //______________________________________________________________________________
MyClass()355 MyClass::MyClass()
356 {
357 }
358 //______________________________________________________________________________
~MyClass()359 MyClass::~MyClass()
360 {
361 }
362 //______________________________________________________________________________
acquire()363 void MyClass::acquire() throw ()
364 {
365     OWeakObject::acquire();
366 }
367 //______________________________________________________________________________
release()368 void MyClass::release() throw ()
369 {
370     OWeakObject::release();
371 }
372 
373 //==================================================================================================
performTest(const Reference<XComponentContext> & xContext,const Reference<XBridgeTest> & xLBT,bool noCurrentContext)374 static sal_Bool performTest(
375     const Reference<XComponentContext> & xContext,
376     const Reference<XBridgeTest > & xLBT,
377     bool noCurrentContext )
378 {
379     check(xLBT.is(), "### no test interface!");
380     bool bRet = true;
381     if (xLBT.is()) {
382         // this data is never ever granted access to by calls other than
383         // equals(), assign()!
384         TestData aData; // test against this data
385         Reference< XInterface > xI(new MyClass);
386         assign(
387             (TestElement &) aData, true, '@', 17, 0x1234, 0xFEDC, 0x12345678,
388             0xFEDCBA98, SAL_CONST_INT64(0x123456789ABCDEF0),
389             SAL_CONST_UINT64(0xFEDCBA9876543210), 17.0815f, 3.1415926359,
390             TestEnum_LOLA, OUSTR(STRING_TEST_CONSTANT), xI,
391             Any(&xI, getCppuType((Reference< XInterface > const *) 0)));
392         bRet &= check(aData.Any == xI, "### unexpected any!");
393         bRet &= check(!(aData.Any != xI), "### unexpected any!");
394         aData.Sequence.realloc(2);
395         aData.Sequence[0] = *(TestElement const *) &aData;
396         // aData.Sequence[1] is empty
397         // aSetData is a manually copy of aData for first setting:
398         TestData aSetData;
399         assign(
400             (TestElement &) aSetData, aData.Bool, aData.Char, aData.Byte,
401             aData.Short, aData.UShort, aData.Long, aData.ULong, aData.Hyper,
402             aData.UHyper, aData.Float, aData.Double, aData.Enum, aData.String,
403             xI, Any(&xI, getCppuType((Reference< XInterface > const *) 0)));
404         aSetData.Sequence.realloc(2);
405         aSetData.Sequence[0] = *(TestElement const *) &aSetData;
406         // aSetData.Sequence[1] is empty
407         xLBT->setValues(
408             aSetData.Bool, aSetData.Char, aSetData.Byte, aSetData.Short,
409             aSetData.UShort, aSetData.Long, aSetData.ULong, aSetData.Hyper,
410             aSetData.UHyper, aSetData.Float, aSetData.Double, aSetData.Enum,
411             aSetData.String, aSetData.Interface, aSetData.Any,
412             aSetData.Sequence, aSetData);
413         {
414             TestData aRet;
415             TestData aRet2;
416             xLBT->getValues(
417                 aRet.Bool, aRet.Char, aRet.Byte, aRet.Short, aRet.UShort,
418                 aRet.Long, aRet.ULong, aRet.Hyper, aRet.UHyper, aRet.Float,
419                 aRet.Double, aRet.Enum, aRet.String, aRet.Interface, aRet.Any,
420                 aRet.Sequence, aRet2);
421             bRet &= check(
422                 equals(aData, aRet) && equals(aData, aRet2), "getValues test");
423             // Set last retrieved values:
424             TestData aSV2ret(
425                 xLBT->setValues2(
426                     aRet.Bool, aRet.Char, aRet.Byte, aRet.Short, aRet.UShort,
427                     aRet.Long, aRet.ULong, aRet.Hyper, aRet.UHyper, aRet.Float,
428                     aRet.Double, aRet.Enum, aRet.String, aRet.Interface,
429                     aRet.Any, aRet.Sequence, aRet2));
430             // Check inout sequence order (=> inout sequence parameter was
431             // switched by test objects):
432             TestElement temp(aRet.Sequence[0]);
433             aRet.Sequence[0] = aRet.Sequence[1];
434             aRet.Sequence[1] = temp;
435             bRet &= check(
436                 equals(aData, aSV2ret) && equals(aData, aRet2),
437                 "getValues2 test");
438         }
439         {
440             TestData aRet;
441             TestData aRet2;
442             TestData aGVret(
443                 xLBT->getValues(
444                     aRet.Bool, aRet.Char, aRet.Byte, aRet.Short, aRet.UShort,
445                     aRet.Long, aRet.ULong, aRet.Hyper, aRet.UHyper, aRet.Float,
446                     aRet.Double, aRet.Enum, aRet.String, aRet.Interface,
447                     aRet.Any, aRet.Sequence, aRet2));
448             bRet &= check(
449                 (equals(aData, aRet) && equals(aData, aRet2) &&
450                  equals(aData, aGVret)),
451                 "getValues test");
452             // Set last retrieved values:
453             xLBT->setBool(aRet.Bool);
454             xLBT->setChar(aRet.Char);
455             xLBT->setByte(aRet.Byte);
456             xLBT->setShort(aRet.Short);
457             xLBT->setUShort(aRet.UShort);
458             xLBT->setLong(aRet.Long);
459             xLBT->setULong(aRet.ULong);
460             xLBT->setHyper(aRet.Hyper);
461             xLBT->setUHyper(aRet.UHyper);
462             xLBT->setFloat(aRet.Float);
463             xLBT->setDouble(aRet.Double);
464             xLBT->setEnum(aRet.Enum);
465             xLBT->setString(aRet.String);
466             xLBT->setInterface(aRet.Interface);
467             xLBT->setAny(aRet.Any);
468             xLBT->setSequence(aRet.Sequence);
469             xLBT->setStruct(aRet2);
470         }
471         {
472             TestData aRet;
473             aRet.Hyper = xLBT->getHyper();
474             aRet.UHyper = xLBT->getUHyper();
475             aRet.Float = xLBT->getFloat();
476             aRet.Double = xLBT->getDouble();
477             aRet.Byte = xLBT->getByte();
478             aRet.Char = xLBT->getChar();
479             aRet.Bool = xLBT->getBool();
480             aRet.Short = xLBT->getShort();
481             aRet.UShort = xLBT->getUShort();
482             aRet.Long = xLBT->getLong();
483             aRet.ULong = xLBT->getULong();
484             aRet.Enum = xLBT->getEnum();
485             aRet.String = xLBT->getString();
486             aRet.Interface = xLBT->getInterface();
487             aRet.Any = xLBT->getAny();
488             aRet.Sequence = xLBT->getSequence();
489             TestData aRet2(xLBT->getStruct());
490             bRet &= check(
491                 equals(aData, aRet) && equals(aData, aRet2),
492                 "struct comparison test");
493             {
494                 SmallStruct aIn(1, 2);
495                 SmallStruct aOut(xLBT->echoSmallStruct(aIn));
496                 bRet &= check(
497                     memcmp(&aIn, &aOut, sizeof(SmallStruct)) == 0,
498                     "small struct test");
499             }
500             {
501                 MediumStruct aIn(1, 2, 3, 4);
502                 MediumStruct aOut(xLBT->echoMediumStruct(aIn));
503                 bRet &= check(
504                     memcmp(&aIn, &aOut, sizeof(MediumStruct)) == 0,
505                     "medium struct test");
506             }
507             {
508                 BigStruct aIn(1, 2, 3, 4, 5, 6, 7, 8);
509                 BigStruct aOut(xLBT->echoBigStruct(aIn));
510                 bRet &= check(
511                     memcmp(&aIn, &aOut, sizeof(BigStruct)) == 0,
512                     "big struct test");
513             }
514             {
515                 AllFloats aIn(1.1f, 2.2f, 3.3f, 4.4f);
516                 AllFloats aOut(xLBT->echoAllFloats(aIn));
517                 bRet &= check(
518                     memcmp(&aIn, &aOut, sizeof(AllFloats)) == 0,
519                     "all floats struct test");
520             }
521             {
522                 sal_Int32 i2 = xLBT->testPPCAlignment(0, 0, 0, 0, 0xBEAF);
523                 bRet &= check(i2 == 0xBEAF, "ppc-style alignment test");
524             }
525             // Test extended attributes that raise exceptions:
526             try {
527                 xLBT->getRaiseAttr1();
528                 bRet &= check(false, "getRaiseAttr1 did not throw");
529             } catch (RuntimeException &) {
530             } catch (...) {
531                 bRet &= check(false, "getRaiseAttr1 threw wrong type");
532             }
533             try {
534                 xLBT->setRaiseAttr1(0);
535                 bRet &= check(false, "setRaiseAttr1 did not throw");
536             } catch (IllegalArgumentException &) {
537             } catch (...) {
538                 bRet &= check(false, "setRaiseAttr1 threw wrong type");
539             }
540             try {
541                 xLBT->getRaiseAttr2();
542                 bRet &= check(false, "getRaiseAttr2 did not throw");
543             } catch (IllegalArgumentException &) {
544             } catch (...) {
545                 bRet &= check(false, "getRaiseAttr2 threw wrong type");
546             }
547 #if !defined(OS2) && !(defined(FREEBSD) && defined(INTEL))
548 // see i120310 for OS2 details
549 // FreeBSD i386 coredumps on this test in cpp_vtable_call():
550 //  pTypeDescr appears to point to garbage, pMapFunctionIndexToMemberIndex
551 //  points to unreadable memory, as does abase.pTypeName.  Refcounts
552 //  don't look reasonable, etc.
553             // Test instantiated polymorphic struct types:
554             {
555                 bRet &= check(
556                     (xLBT->transportPolyBoolean(
557                         TestPolyStruct< sal_Bool >(true)).
558                      member),
559                     "transportPolyBoolean");
560                 TestPolyStruct< sal_Int64 > tps1(12345);
561                 xLBT->transportPolyHyper(tps1);
562                 bRet &= check(tps1.member == 12345, "transportPolyHyper");
563                 Sequence< Any > seq(2);
564                 seq[0] <<= static_cast< sal_uInt32 >(33);
565                 seq[1] <<= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ABC"));
566                 TestPolyStruct< Sequence< Any > > tps2(seq);
567                 TestPolyStruct< Sequence< Any > > tps3;
568                 xLBT->transportPolySequence(tps2, tps3);
569                 bRet &= check(
570                     tps3.member.getLength() == 2,
571                     "transportPolySequence, length");
572                 sal_uInt32 v0 = sal_uInt32();
573                 tps3.member[0] >>= v0;
574                 bRet &= check(v0 == 33, "transportPolySequence, element 0");
575                 rtl::OUString v1;
576                 tps3.member[1] >>= v1;
577                 bRet &= check(
578                     v1.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("ABC")),
579                     "transportPolySequence, element 1");
580                 bRet &= check(
581                     xLBT->getNullPolyLong().member == 0, "getNullPolyLong");
582                 bRet &= check(
583                     xLBT->getNullPolyString().member.getLength() == 0,
584                     "getNullPolyString");
585                 bRet &= check(
586                     xLBT->getNullPolyType().member == Type(),
587                     "getNullPolyType");
588                 Any nullAny(xLBT->getNullPolyAny().member);
589                 bRet &= check(
590                     (((nullAny.getValueTypeName() ==
591                        rtl::OUString(
592                            RTL_CONSTASCII_USTRINGPARAM(
593                                "com.sun.star.uno.XInterface"))) &&
594                       !static_cast< Reference< XInterface > const * >(
595                           nullAny.getValue())->is())
596                      || nullAny == Any()),
597                     "getNullPolyAny");
598                 bRet &= check(
599                     xLBT->getNullPolySequence().member.getLength() == 0,
600                     "getNullPolySequence");
601                 bRet &= check(
602                     xLBT->getNullPolyEnum().member == TestEnum_TEST,
603                     "getNullPolyEnum");
604                 bRet &= check(
605                     xLBT->getNullPolyBadEnum().member == TestBadEnum_M,
606                     "getNullPolyBadEnum");
607                 bRet &= check(
608                     xLBT->getNullPolyStruct().member.member == 0,
609                     "getNullPolyStruct");
610                 bRet &= check(
611                     !xLBT->getNullPolyInterface().member.is(),
612                     "getNullPolyInterface");
613             }
614 #endif // OS2
615 
616             // Any test:
617             bRet &= check(performAnyTest(xLBT , aData), "any test");
618             // Sequence of call test:
619             bRet &= check(
620                 performSequenceOfCallTest(xLBT), "sequence of call test");
621             // Recursive call test:
622             bRet &= check(performRecursiveCallTest(xLBT), "recursive test");
623             bRet &= check(
624                 equals(aData, aRet) && equals(aData, aRet2),
625                 "recursive test results");
626             // Multiple inheritance test:
627             bRet &= checkEmpty(
628                 testtools::bridgetest::testMulti(xLBT->getMulti()),
629                 "remote multi");
630             bRet &= checkEmpty(
631                 xLBT->testMulti(new testtools::bridgetest::Multi),
632                 "local multi");
633         }
634     }
635     {
636         Reference< XBridgeTest2 > xBT2(xLBT, UNO_QUERY);
637         if (!xBT2.is()) {
638             return bRet;
639         }
640         // Perform sequence tests (XBridgeTest2); create the sequence which is
641         // compared with the results:
642         sal_Bool _arBool[] = { true, false, true };
643         sal_Unicode _arChar[] = { 0x0065, 0x0066, 0x0067 };
644         sal_Int8 _arByte[] = { 1, 2, -1 };
645         sal_Int16 _arShort[] = { -0x8000, 1, 0x7FFF };
646         sal_uInt16 _arUShort[] = { 0 , 1, 0xFFFF };
647         sal_Int32 _arLong[] = { -0x7FFFFFFF, 1, 0x7FFFFFFF };
648         sal_uInt32 _arULong[] = { 0, 1, 0xFFFFFFFF };
649         sal_Int64 _arHyper[] = {
650             SAL_CONST_INT64(-0x7FFFFFFFFFFFFFFF), 1,
651             SAL_CONST_INT64(+0x7FFFFFFFFFFFFFFF) };
652         sal_uInt64 _arUHyper[] = { 0, 1, SAL_CONST_UINT64(0xFFFFFFFFFFFFFFFF) };
653         float _arFloat[] = { 1.1f, 2.2f, 3.3f };
654         double _arDouble[] = { 1.11, 2.22, 3.33 };
655         OUString _arString[] = {
656             OUString(RTL_CONSTASCII_USTRINGPARAM("String 1")),
657             OUString(RTL_CONSTASCII_USTRINGPARAM("String 2")),
658             OUString(RTL_CONSTASCII_USTRINGPARAM("String 3")) };
659         sal_Bool _aBool = true;
660         sal_Int32 _aInt = 0xBABEBABE;
661         float _aFloat = 3.14f;
662         Any _any1(&_aBool, getCppuBooleanType());
663         Any _any2(&_aInt, getCppuType((sal_Int32 *) 0));
664         Any _any3(&_aFloat, getCppuType((float *) 0));
665         Any _arAny[] = { _any1, _any2, _any3 };
666         Reference< XInterface > _arObj[3];
667         _arObj[0] = new OWeakObject();
668         _arObj[1] = new OWeakObject();
669         _arObj[2] = new OWeakObject();
670         TestEnum _arEnum[] = { TestEnum_ONE, TestEnum_TWO, TestEnum_CHECK };
671         TestElement _arStruct[3];
672         assign(
673             _arStruct[0], true, '@', 17, 0x1234, 0xFEDC, 0x12345678, 0xFEDCBA98,
674             SAL_CONST_INT64(0x123456789ABCDEF0),
675             SAL_CONST_UINT64(0xFEDCBA9876543210), 17.0815f, 3.1415926359,
676             TestEnum_LOLA, OUSTR(STRING_TEST_CONSTANT), _arObj[0],
677             Any(&_arObj[0], getCppuType((Reference< XInterface > const *) 0)));
678         assign(
679             _arStruct[1], true, 'A', 17, 0x1234, 0xFEDC, 0x12345678, 0xFEDCBA98,
680             SAL_CONST_INT64(0x123456789ABCDEF0),
681             SAL_CONST_UINT64(0xFEDCBA9876543210), 17.0815f, 3.1415926359,
682             TestEnum_TWO, OUSTR(STRING_TEST_CONSTANT), _arObj[1],
683             Any(&_arObj[1], getCppuType((Reference< XInterface > const *) 0)));
684         assign(
685             _arStruct[2], true, 'B', 17, 0x1234, 0xFEDC, 0x12345678, 0xFEDCBA98,
686             SAL_CONST_INT64(0x123456789ABCDEF0),
687             SAL_CONST_UINT64(0xFEDCBA9876543210), 17.0815f, 3.1415926359,
688             TestEnum_CHECK, OUSTR(STRING_TEST_CONSTANT), _arObj[2],
689             Any(&_arObj[2], getCppuType((Reference< XInterface > const *) 0)));
690         {
691             Sequence<sal_Bool> arBool(_arBool, 3);
692             Sequence<sal_Unicode> arChar( _arChar, 3);
693             Sequence<sal_Int8> arByte(_arByte, 3);
694             Sequence<sal_Int16> arShort(_arShort, 3);
695             Sequence<sal_uInt16> arUShort(_arUShort, 3);
696             Sequence<sal_Int32> arLong(_arLong, 3);
697             Sequence<sal_uInt32> arULong(_arULong, 3);
698             Sequence<sal_Int64> arHyper(_arHyper, 3);
699             Sequence<sal_uInt64> arUHyper(_arUHyper, 3);
700             Sequence<float> arFloat(_arFloat, 3);
701             Sequence<double> arDouble(_arDouble, 3);
702             Sequence<OUString> arString(_arString, 3);
703             Sequence<Any> arAny(_arAny, 3);
704             Sequence<Reference<XInterface> > arObject(_arObj, 3);
705             Sequence<TestEnum> arEnum(_arEnum, 3);
706             Sequence<TestElement> arStruct(_arStruct, 3);
707             Sequence<Sequence<sal_Int32> > _arSeqLong2[3];
708             for (int j = 0; j != 3; ++j) {
709                 Sequence< sal_Int32 > _arSeqLong[3];
710                 for (int i = 0; i != 3; ++i) {
711                     _arSeqLong[i] = Sequence< sal_Int32 >(_arLong, 3);
712                 }
713                 _arSeqLong2[j] = Sequence< Sequence< sal_Int32 > >(
714                     _arSeqLong, 3);
715             }
716             Sequence< Sequence< Sequence< sal_Int32> > > arLong3(
717                 _arSeqLong2, 3);
718             Sequence< Sequence< sal_Int32 > > seqSeqRet(
719                 xBT2->setDim2(arLong3[0]));
720             bRet &= check(seqSeqRet == arLong3[0], "sequence test");
721             Sequence< Sequence< Sequence< sal_Int32 > > > seqSeqRet2(
722                 xBT2->setDim3(arLong3));
723             bRet &= check(seqSeqRet2 == arLong3, "sequence test");
724             Sequence< Any > seqAnyRet(xBT2->setSequenceAny(arAny));
725             bRet &= check(seqAnyRet == arAny, "sequence test");
726             Sequence< sal_Bool > seqBoolRet(xBT2->setSequenceBool(arBool));
727             bRet &= check(seqBoolRet == arBool, "sequence test");
728             Sequence< sal_Int8 > seqByteRet(xBT2->setSequenceByte(arByte));
729             bRet &= check(seqByteRet == arByte, "sequence test");
730             Sequence< sal_Unicode > seqCharRet(xBT2->setSequenceChar(arChar));
731             bRet &= check(seqCharRet == arChar, "sequence test");
732             Sequence< sal_Int16 > seqShortRet(xBT2->setSequenceShort(arShort));
733             bRet &= check(seqShortRet == arShort, "sequence test");
734             Sequence< sal_Int32 > seqLongRet(xBT2->setSequenceLong(arLong));
735             bRet &= check(seqLongRet == arLong, "sequence test");
736             Sequence< sal_Int64 > seqHyperRet(xBT2->setSequenceHyper(arHyper));
737             bRet &= check(seqHyperRet == arHyper, "sequence test");
738             Sequence< float > seqFloatRet(xBT2->setSequenceFloat(arFloat));
739             bRet &= check(seqFloatRet == arFloat, "sequence test");
740             Sequence< double > seqDoubleRet(xBT2->setSequenceDouble(arDouble));
741             bRet &= check(seqDoubleRet == arDouble, "sequence test");
742             Sequence< TestEnum > seqEnumRet(xBT2->setSequenceEnum(arEnum));
743             bRet &= check(seqEnumRet == arEnum, "sequence test");
744             Sequence< sal_uInt16 > seqUShortRet(
745                 xBT2->setSequenceUShort(arUShort));
746             bRet &= check(seqUShortRet == arUShort, "sequence test");
747             Sequence< sal_uInt32 > seqULongRet(xBT2->setSequenceULong(arULong));
748             bRet &= check(seqULongRet == arULong, "sequence test");
749             Sequence< sal_uInt64 > seqUHyperRet(
750                 xBT2->setSequenceUHyper(arUHyper));
751             bRet &= check(seqUHyperRet == arUHyper, "sequence test");
752             Sequence< Reference< XInterface > > seqObjectRet(
753                 xBT2->setSequenceXInterface(arObject));
754             bRet &= check(seqObjectRet == arObject, "sequence test");
755             Sequence< OUString > seqStringRet(
756                 xBT2->setSequenceString(arString));
757             bRet &= check(seqStringRet == arString, "sequence test");
758             Sequence< TestElement > seqStructRet(
759                 xBT2->setSequenceStruct(arStruct));
760             bRet &= check(seqStructRet == arStruct, "sequence test");
761             Sequence< sal_Bool > arBoolTemp(cloneSequence(arBool));
762             Sequence< sal_Unicode > arCharTemp(cloneSequence(arChar));
763             Sequence< sal_Int8 > arByteTemp(cloneSequence(arByte));
764             Sequence< sal_Int16 > arShortTemp(cloneSequence(arShort));
765             Sequence< sal_uInt16 > arUShortTemp(cloneSequence(arUShort));
766             Sequence< sal_Int32 > arLongTemp(cloneSequence(arLong));
767             Sequence< sal_uInt32 > arULongTemp(cloneSequence(arULong));
768             Sequence< sal_Int64 > arHyperTemp(cloneSequence(arHyper));
769             Sequence< sal_uInt64 > arUHyperTemp(cloneSequence(arUHyper));
770             Sequence< float > arFloatTemp(cloneSequence(arFloat));
771             Sequence< double > arDoubleTemp(cloneSequence(arDouble));
772             Sequence< TestEnum > arEnumTemp(cloneSequence(arEnum));
773             Sequence< OUString > arStringTemp(cloneSequence(arString));
774             Sequence< Reference< XInterface > > arObjectTemp(
775                 cloneSequence(arObject));
776             Sequence< Any > arAnyTemp(cloneSequence(arAny));
777             Sequence< Sequence< sal_Int32 > > arLong2Temp(arLong3[0]);
778             Sequence< Sequence< Sequence< sal_Int32 > > > arLong3Temp(arLong3);
779             xBT2->setSequencesInOut(
780                 arBoolTemp, arCharTemp, arByteTemp, arShortTemp, arUShortTemp,
781                 arLongTemp,arULongTemp, arHyperTemp, arUHyperTemp, arFloatTemp,
782                 arDoubleTemp, arEnumTemp, arStringTemp, arObjectTemp, arAnyTemp,
783                 arLong2Temp, arLong3Temp);
784             bRet &= check(
785                 (arBoolTemp == arBool && arCharTemp == arChar &&
786                  arByteTemp == arByte && arShortTemp == arShort &&
787                  arUShortTemp == arUShort && arLongTemp == arLong &&
788                  arULongTemp == arULong && arHyperTemp == arHyper &&
789                  arUHyperTemp == arUHyper && arFloatTemp == arFloat &&
790                  arDoubleTemp == arDouble && arEnumTemp == arEnum &&
791                  arStringTemp == arString && arObjectTemp == arObject &&
792                  arAnyTemp == arAny && arLong2Temp == arLong3[0] &&
793                  arLong3Temp == arLong3),
794                 "sequence test");
795             Sequence< sal_Bool > arBoolOut;
796             Sequence< sal_Unicode > arCharOut;
797             Sequence< sal_Int8 > arByteOut;
798             Sequence< sal_Int16 > arShortOut;
799             Sequence< sal_uInt16 > arUShortOut;
800             Sequence< sal_Int32 > arLongOut;
801             Sequence< sal_uInt32 > arULongOut;
802             Sequence< sal_Int64 > arHyperOut;
803             Sequence< sal_uInt64 > arUHyperOut;
804             Sequence< float > arFloatOut;
805             Sequence< double > arDoubleOut;
806             Sequence< TestEnum > arEnumOut;
807             Sequence< OUString > arStringOut;
808             Sequence< Reference< XInterface > > arObjectOut;
809             Sequence< Any > arAnyOut;
810             Sequence< Sequence< sal_Int32 > > arLong2Out;
811             Sequence< Sequence< Sequence< sal_Int32 > > > arLong3Out;
812             xBT2->setSequencesOut(
813                 arBoolOut, arCharOut, arByteOut, arShortOut, arUShortOut,
814                 arLongOut,arULongOut, arHyperOut, arUHyperOut, arFloatOut,
815                 arDoubleOut, arEnumOut, arStringOut, arObjectOut, arAnyOut,
816                 arLong2Out, arLong3Out);
817             bRet &= check(
818                 (arBoolOut == arBool && arCharOut == arChar &&
819                  arByteOut == arByte && arShortOut == arShort &&
820                  arUShortOut == arUShort && arLongOut == arLong &&
821                  arULongOut == arULong && arHyperOut == arHyper &&
822                  arUHyperOut == arUHyper && arFloatOut == arFloat &&
823                  arDoubleOut == arDouble && arEnumOut == arEnum &&
824                  arStringOut == arString && arObjectOut == arObject &&
825                  arAnyOut == arAny && arLong2Out == arLong3[0] &&
826                  arLong3Out == arLong3),
827                 "sequence test");
828         }
829         {
830             // Test with empty sequences:
831             Sequence< Sequence< sal_Int32 > > arLong2;
832             Sequence< Sequence< sal_Int32 > > seqSeqRet(xBT2->setDim2(arLong2));
833             bRet &= check(seqSeqRet == arLong2, "sequence test");
834             Sequence< Sequence< Sequence< sal_Int32 > > > arLong3;
835             Sequence< Sequence< Sequence< sal_Int32 > > > seqSeqRet2(
836                 xBT2->setDim3(arLong3));
837             bRet &= check(seqSeqRet2 == arLong3, "sequence test");
838             Sequence< Any > arAny;
839             Sequence< Any > seqAnyRet(xBT2->setSequenceAny(arAny));
840             bRet &= check(seqAnyRet == arAny, "sequence test");
841             Sequence< sal_Bool > arBool;
842             Sequence< sal_Bool > seqBoolRet(xBT2->setSequenceBool(arBool));
843             bRet &= check(seqBoolRet == arBool, "sequence test");
844             Sequence< sal_Int8 > arByte;
845             Sequence< sal_Int8 > seqByteRet(xBT2->setSequenceByte(arByte));
846             bRet &= check(seqByteRet == arByte, "sequence test");
847             Sequence< sal_Unicode > arChar;
848             Sequence< sal_Unicode > seqCharRet(xBT2->setSequenceChar(arChar));
849             bRet &= check(seqCharRet == arChar, "sequence test");
850             Sequence< sal_Int16 > arShort;
851             Sequence< sal_Int16 > seqShortRet(xBT2->setSequenceShort(arShort));
852             bRet &= check(seqShortRet == arShort, "sequence test");
853             Sequence< sal_Int32 > arLong;
854             Sequence< sal_Int32 > seqLongRet(xBT2->setSequenceLong(arLong));
855             bRet &= check(seqLongRet == arLong, "sequence test");
856             Sequence< sal_Int64 > arHyper;
857             Sequence< sal_Int64 > seqHyperRet(xBT2->setSequenceHyper(arHyper));
858             bRet &= check(seqHyperRet == arHyper, "sequence test");
859             Sequence< float > arFloat;
860             Sequence< float > seqFloatRet(xBT2->setSequenceFloat(arFloat));
861             bRet &= check(seqFloatRet == arFloat, "sequence test");
862             Sequence< double > arDouble;
863             Sequence< double > seqDoubleRet(xBT2->setSequenceDouble(arDouble));
864             bRet &= check(seqDoubleRet == arDouble, "sequence test");
865             Sequence< TestEnum > arEnum;
866             Sequence< TestEnum > seqEnumRet(xBT2->setSequenceEnum(arEnum));
867             bRet &= check(seqEnumRet == arEnum, "sequence test");
868             Sequence< sal_uInt16 > arUShort;
869             Sequence< sal_uInt16 > seqUShortRet(
870                 xBT2->setSequenceUShort(arUShort));
871             bRet &= check(seqUShortRet == arUShort, "sequence test");
872             Sequence< sal_uInt32 > arULong;
873             Sequence< sal_uInt32 > seqULongRet(xBT2->setSequenceULong(arULong));
874             bRet &= check(seqULongRet == arULong, "sequence test");
875             Sequence< sal_uInt64 > arUHyper;
876             Sequence< sal_uInt64 > seqUHyperRet(
877                 xBT2->setSequenceUHyper(arUHyper));
878             bRet &= check(seqUHyperRet == arUHyper, "sequence test");
879             Sequence< Reference< XInterface > > arObject;
880             Sequence< Reference< XInterface > > seqObjectRet(
881                 xBT2->setSequenceXInterface(arObject));
882             bRet &= check(seqObjectRet == arObject, "sequence test");
883             Sequence< OUString > arString;
884             Sequence< OUString > seqStringRet(
885                 xBT2->setSequenceString(arString));
886             bRet &= check(seqStringRet == arString, "sequence test");
887             Sequence< TestElement > arStruct;
888             Sequence< TestElement > seqStructRet(
889                 xBT2->setSequenceStruct(arStruct));
890             bRet &= check(seqStructRet == arStruct, "sequence test");
891         }
892         // Issue #i60341# shows that the most interesting case is were Java
893         // calls the constructors; however, since this client is currently not
894         // available in Java, while the server is, the logic is reversed here:
895         try {
896             xBT2->testConstructorsService(xContext);
897         } catch (BadConstructorArguments &) {
898             bRet = false;
899         }
900         if (!noCurrentContext) {
901             if (!(new testtools::bridgetest::CurrentContextChecker)->perform(
902                     xBT2->getCurrentContextChecker(), 0, 1))
903             {
904                 bRet = false;
905             }
906             if (!(new testtools::bridgetest::CurrentContextChecker)->perform(
907                     xBT2->getCurrentContextChecker(), 0, 2))
908             {
909                 bRet = false;
910             }
911             if (!(new testtools::bridgetest::CurrentContextChecker)->perform(
912                     xBT2->getCurrentContextChecker(), 1, 2))
913             {
914                 bRet = false;
915             }
916             if (!(new testtools::bridgetest::CurrentContextChecker)->perform(
917                     xBT2->getCurrentContextChecker(), 1, 3))
918             {
919                 bRet = false;
920             }
921         }
922     }
923     return bRet;
924 }
925 
raiseOnewayException(const Reference<XBridgeTest> & xLBT)926 static sal_Bool raiseOnewayException( const Reference < XBridgeTest > & xLBT )
927 {
928 	sal_Bool bReturn = sal_True;
929 	OUString sCompare = OUSTR(STRING_TEST_CONSTANT);
930     Reference<XInterface> const x(xLBT->getInterface());
931 	try
932 	{
933 		// Note : the exception may fly or not (e.g. remote scenario).
934 		//        When it flies, it must contain the correct elements.
935 		xLBT->raiseRuntimeExceptionOneway( sCompare, x );
936 	}
937 	catch( RuntimeException & e )
938 	{
939 		bReturn = (
940 #if OSL_DEBUG_LEVEL == 0
941             // java stack traces trash Message
942             e.Message == sCompare &&
943 #endif
944             xLBT->getInterface() == e.Context &&
945             x == e.Context );
946 	}
947 	return bReturn;
948 }
949 
950 //==================================================================================================
raiseException(const Reference<XBridgeTest> & xLBT)951 static sal_Bool raiseException( const Reference< XBridgeTest > & xLBT )
952 {
953 	sal_Int32 nCount = 0;
954 	try
955 	{
956 		try
957 		{
958 			try
959 			{
960 				TestData aRet, aRet2;
961 				xLBT->raiseException(
962 					5, OUSTR(STRING_TEST_CONSTANT),
963 					xLBT->getInterface() );
964 			}
965 			catch (IllegalArgumentException aExc)
966 			{
967 				if (aExc.ArgumentPosition == 5 &&
968 #if OSL_DEBUG_LEVEL == 0
969                     // java stack traces trash Message
970                     aExc.Message.compareToAscii( STRING_TEST_CONSTANT ) == 0 &&
971 #endif
972 					aExc.Context == xLBT->getInterface())
973 				{
974 #ifdef COMPCHECK
975                     //When we check if a new compiler still works then we must not call
976                     //getRuntimeException because it uses cppu::getCaughtException which
977                     //does only work if all libs are build with the same runtime.
978                     return true;
979 #else
980 					++nCount;
981 #endif
982 				}
983 				else
984 				{
985 					check( sal_False, "### unexpected exception content!" );
986 				}
987 
988 				/** it is certain, that the RuntimeException testing will fail, if no */
989 				xLBT->getRuntimeException();
990 			}
991 		}
992 		catch (const RuntimeException & rExc)
993 		{
994 			if (rExc.Context == xLBT->getInterface()
995 #if OSL_DEBUG_LEVEL == 0
996                     // java stack traces trash Message
997                 && rExc.Message.compareToAscii( STRING_TEST_CONSTANT ) == 0
998 #endif
999                 )
1000 			{
1001 				++nCount;
1002 			}
1003 			else
1004 			{
1005 				check( sal_False, "### unexpected exception content!" );
1006 			}
1007 
1008 			/** it is certain, that the RuntimeException testing will fail, if no */
1009 			xLBT->setRuntimeException( 0xcafebabe );
1010 		}
1011 	}
1012 	catch (Exception & rExc)
1013 	{
1014 		if (rExc.Context == xLBT->getInterface()
1015 #if OSL_DEBUG_LEVEL == 0
1016             // java stack traces trash Message
1017             && rExc.Message.compareToAscii( STRING_TEST_CONSTANT ) == 0
1018 #endif
1019             )
1020 		{
1021 			++nCount;
1022 		}
1023 		else
1024 		{
1025 			check( sal_False, "### unexpected exception content!" );
1026 		}
1027 		return (nCount == 3);
1028 	}
1029 	return sal_False;
1030 }
1031 
1032 /* Returns an acquired sequence
1033  */
cloneSequence(const uno_Sequence * val,const Type & type)1034 uno_Sequence* cloneSequence(const uno_Sequence* val, const Type& type)
1035 {
1036     TypeDescription td(type);
1037     td.makeComplete();
1038     typelib_TypeDescription* pTdRaw = td.get();
1039     typelib_IndirectTypeDescription* pIndirectTd =
1040         (typelib_IndirectTypeDescription*) pTdRaw;
1041 
1042     typelib_TypeDescription* pTdElem = pIndirectTd->pType->pType;
1043     sal_Int8* buf = new sal_Int8[pTdElem->nSize * val->nElements];
1044     sal_Int8* pBufCur = buf;
1045 
1046     uno_Sequence* retSeq = NULL;
1047     switch (pTdElem->eTypeClass)
1048     {
1049     case TypeClass_SEQUENCE:
1050     {
1051         Type _tElem(pTdElem->pWeakRef);
1052         for (int i = 0; i < val->nElements; i++)
1053         {
1054             uno_Sequence* seq = cloneSequence(
1055                 *(uno_Sequence**) (&val->elements + i * pTdElem->nSize),
1056                 _tElem);
1057             *((uno_Sequence**) pBufCur) = seq;
1058             pBufCur += pTdElem->nSize;
1059         }
1060         break;
1061     }
1062     default:
1063         uno_type_sequence_construct(
1064             &retSeq, type.getTypeLibType(), (void*) val->elements,
1065             val->nElements, reinterpret_cast< uno_AcquireFunc >(cpp_acquire));
1066         break;
1067     }
1068     delete[] buf;
1069     return retSeq;
1070 }
1071 
1072 template< class T>
cloneSequence(const Sequence<T> & val)1073 Sequence<T> cloneSequence(const Sequence<T>& val)
1074 {
1075     Sequence<T> seq( cloneSequence(val.get(), getCppuType(&val)), SAL_NO_ACQUIRE);
1076     return seq;
1077 }
1078 
1079 template< class T >
makeSurrogate(Reference<T> & rOut,Reference<T> const & rOriginal)1080 inline bool makeSurrogate(
1081     Reference< T > & rOut, Reference< T > const & rOriginal )
1082 {
1083     rOut.clear();
1084     if (! rOriginal.is())
1085         return false;
1086 
1087     Environment aCppEnv_official;
1088     Environment aUnoEnv_ano;
1089     Environment aCppEnv_ano;
1090 
1091     OUString aCppEnvTypeName(
1092         RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME) );
1093     OUString aUnoEnvTypeName(
1094         RTL_CONSTASCII_USTRINGPARAM(UNO_LB_UNO) );
1095     // official:
1096     uno_getEnvironment(
1097         reinterpret_cast< uno_Environment ** >( &aCppEnv_official ),
1098         aCppEnvTypeName.pData, 0 );
1099     // anonymous:
1100     uno_createEnvironment(
1101         reinterpret_cast< uno_Environment ** >( &aCppEnv_ano ),
1102         aCppEnvTypeName.pData, 0 );
1103     uno_createEnvironment(
1104         reinterpret_cast< uno_Environment ** >( &aUnoEnv_ano ),
1105         aUnoEnvTypeName.pData, 0 );
1106 
1107     UnoInterfaceReference unoI;
1108     Mapping cpp2uno( aCppEnv_official.get(), aUnoEnv_ano.get() );
1109     Mapping uno2cpp( aUnoEnv_ano.get(), aCppEnv_ano.get() );
1110     if (!cpp2uno.is() || !uno2cpp.is())
1111     {
1112         throw RuntimeException(
1113             OUSTR("cannot get C++-UNO mappings!"),
1114             Reference< XInterface >() );
1115     }
1116     cpp2uno.mapInterface(
1117         reinterpret_cast< void ** >( &unoI.m_pUnoI ),
1118         rOriginal.get(), ::getCppuType( &rOriginal ) );
1119     if (! unoI.is())
1120     {
1121         throw RuntimeException(
1122             OUSTR("mapping C++ to binary UNO failed!"),
1123             Reference< XInterface >() );
1124     }
1125     uno2cpp.mapInterface(
1126         reinterpret_cast< void ** >( &rOut ),
1127         unoI.get(), ::getCppuType( &rOriginal ) );
1128     if (! rOut.is())
1129     {
1130         throw RuntimeException(
1131             OUSTR("mapping binary UNO to C++ failed!"),
1132             Reference< XInterface >() );
1133     }
1134 
1135 	return rOut.is();
1136 }
1137 
1138 //==================================================================================================
run(const Sequence<OUString> & rArgs)1139 sal_Int32 TestBridgeImpl::run( const Sequence< OUString > & rArgs )
1140 	throw (RuntimeException)
1141 {
1142     bool bRet = false;
1143     try
1144     {
1145         if (! rArgs.getLength())
1146         {
1147             throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM(
1148                                                   "no test object specified!\n"
1149                                                   "usage : ServiceName of test object | -u unourl of test object\n" ) ),
1150                                     Reference< XInterface >() );
1151         }
1152 
1153         Reference< XInterface > xOriginal;
1154         bool remote;
1155         sal_Int32 i;
1156         if( rArgs.getLength() > 1 && 0 == rArgs[0].compareToAscii( "-u" ) )
1157         {
1158             remote = true;
1159             i = 2;
1160         }
1161         else
1162         {
1163             remote = false;
1164             i = 1;
1165         }
1166         bool noCurrentContext = false;
1167         if (i < rArgs.getLength()
1168             && rArgs[i].equalsAsciiL(
1169                 RTL_CONSTASCII_STRINGPARAM("noCurrentContext")))
1170         {
1171             noCurrentContext = true;
1172             ++i;
1173         }
1174         bool stress = false;
1175         if (i < rArgs.getLength()
1176             && rArgs[i].equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("stress")))
1177         {
1178             stress = true;
1179             ++i;
1180         }
1181 
1182         for (;;) {
1183             Reference< XInterface > o;
1184             if (remote) {
1185                 o = UnoUrlResolver::create(m_xContext)->resolve(rArgs[1]);
1186             } else {
1187                 o = m_xContext->getServiceManager()->createInstanceWithContext(
1188                     rArgs[0], m_xContext);
1189             }
1190             if (!stress) {
1191                 xOriginal = o;
1192                 break;
1193             }
1194         }
1195 
1196         if (! xOriginal.is())
1197         {
1198             throw RuntimeException(
1199                 OUString( RTL_CONSTASCII_USTRINGPARAM(
1200                               "cannot get test object!") ),
1201                 Reference< XInterface >() );
1202         }
1203         Reference< XBridgeTest > xTest( xOriginal, UNO_QUERY );
1204         if (! xTest.is())
1205         {
1206             throw RuntimeException(
1207                 OUString( RTL_CONSTASCII_USTRINGPARAM("test object does not implement XBridgeTest!") ),
1208                 Reference< XInterface >() );
1209         }
1210 
1211         Reference<XBridgeTest > xLBT;
1212         bRet = check( makeSurrogate( xLBT, xTest ), "makeSurrogate" );
1213         bRet = check(
1214             performTest( m_xContext, xLBT, noCurrentContext ), "standard test" )
1215             && bRet;
1216         bRet = check( raiseException( xLBT ) , "exception test" )&& bRet;
1217         bRet = check( raiseOnewayException( xLBT ),
1218                       "oneway exception test" ) && bRet;
1219         if (! bRet)
1220         {
1221             throw RuntimeException(
1222                 OUString( RTL_CONSTASCII_USTRINGPARAM("error: test failed!") ),
1223                 Reference< XInterface >() );
1224         }
1225 	}
1226     catch (Exception & exc)
1227     {
1228         OString cstr( OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
1229         fprintf( stderr, "exception occurred: %s\n", cstr.getStr() );
1230         throw;
1231     }
1232 
1233     if( bRet )
1234     {
1235         printf( "\n\n ### test succeeded!\n" );
1236     }
1237     else
1238     {
1239         printf( "\n> ### test failed!\n" );
1240     }
1241 
1242 	return 0;
1243 }
1244 
1245 // XServiceInfo
1246 //__________________________________________________________________________________________________
getImplementationName()1247 OUString TestBridgeImpl::getImplementationName()
1248 	throw (RuntimeException)
1249 {
1250 	return OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME) );
1251 }
1252 //__________________________________________________________________________________________________
supportsService(const OUString & rServiceName)1253 sal_Bool TestBridgeImpl::supportsService( const OUString & rServiceName )
1254 	throw (RuntimeException)
1255 {
1256 	const Sequence< OUString > & rSNL = getSupportedServiceNames();
1257 	const OUString * pArray = rSNL.getConstArray();
1258 	for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
1259 	{
1260 		if (pArray[nPos] == rServiceName)
1261 			return sal_True;
1262 	}
1263 	return sal_False;
1264 }
1265 //__________________________________________________________________________________________________
getSupportedServiceNames()1266 Sequence< OUString > TestBridgeImpl::getSupportedServiceNames()
1267 	throw (RuntimeException)
1268 {
1269 	return bridge_test::getSupportedServiceNames();
1270 }
1271 
1272 // ...
1273 
1274 //==================================================================================================
TestBridgeImpl_create(const Reference<XComponentContext> & xContext)1275 static Reference< XInterface > SAL_CALL TestBridgeImpl_create(
1276 	const Reference< XComponentContext > & xContext )
1277 {
1278 	return Reference< XInterface >(
1279         static_cast< OWeakObject * >( new TestBridgeImpl( xContext ) ) );
1280 }
1281 
1282 }
1283 
1284 extern "C"
1285 {
1286 //==================================================================================================
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)1287 void SAL_CALL component_getImplementationEnvironment(
1288 	const sal_Char ** ppEnvTypeName, uno_Environment ** )
1289 {
1290 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
1291 }
1292 //==================================================================================================
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void *)1293 void * SAL_CALL component_getFactory(
1294 	const sal_Char * pImplName, void * pServiceManager, void * )
1295 {
1296 	void * pRet = 0;
1297 
1298 	if (pServiceManager && rtl_str_compare( pImplName, IMPLNAME ) == 0)
1299 	{
1300 		Reference< XInterface > xFactory(
1301             createSingleComponentFactory(
1302                 bridge_test::TestBridgeImpl_create,
1303                 OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME) ),
1304                 bridge_test::getSupportedServiceNames() ) );
1305 
1306 		if (xFactory.is())
1307 		{
1308 			xFactory->acquire();
1309 			pRet = xFactory.get();
1310 		}
1311 	}
1312 
1313 	return pRet;
1314 }
1315 }
1316