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 //--------------------------------------------------------------------------------------------------
81 inline static Sequence< OUString > getSupportedServiceNames()
82 {
83 	OUString aName( RTL_CONSTASCII_USTRINGPARAM(SERVICENAME) );
84 	return Sequence< OUString >( &aName, 1 );
85 }
86 
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 
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:
115 	TestBridgeImpl( const Reference< XComponentContext > & xContext )
116 		: m_xContext( xContext )
117 		{}
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 //==================================================================================================
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 //==================================================================================================
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 //==================================================================================================
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 >
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 
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 //_______________________________________________________________________________________
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:
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 //_______________________________________________________________________________________
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 //______________________________________________________________________________
355 MyClass::MyClass()
356 {
357 }
358 //______________________________________________________________________________
359 MyClass::~MyClass()
360 {
361 }
362 //______________________________________________________________________________
363 void MyClass::acquire() throw ()
364 {
365     OWeakObject::acquire();
366 }
367 //______________________________________________________________________________
368 void MyClass::release() throw ()
369 {
370     OWeakObject::release();
371 }
372 
373 //==================================================================================================
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 #ifndef OS2 // see i120310 for details
548             // Test instantiated polymorphic struct types:
549             {
550                 bRet &= check(
551                     (xLBT->transportPolyBoolean(
552                         TestPolyStruct< sal_Bool >(true)).
553                      member),
554                     "transportPolyBoolean");
555                 TestPolyStruct< sal_Int64 > tps1(12345);
556                 xLBT->transportPolyHyper(tps1);
557                 bRet &= check(tps1.member == 12345, "transportPolyHyper");
558                 Sequence< Any > seq(2);
559                 seq[0] <<= static_cast< sal_uInt32 >(33);
560                 seq[1] <<= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ABC"));
561                 TestPolyStruct< Sequence< Any > > tps2(seq);
562                 TestPolyStruct< Sequence< Any > > tps3;
563                 xLBT->transportPolySequence(tps2, tps3);
564                 bRet &= check(
565                     tps3.member.getLength() == 2,
566                     "transportPolySequence, length");
567                 sal_uInt32 v0 = sal_uInt32();
568                 tps3.member[0] >>= v0;
569                 bRet &= check(v0 == 33, "transportPolySequence, element 0");
570                 rtl::OUString v1;
571                 tps3.member[1] >>= v1;
572                 bRet &= check(
573                     v1.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("ABC")),
574                     "transportPolySequence, element 1");
575                 bRet &= check(
576                     xLBT->getNullPolyLong().member == 0, "getNullPolyLong");
577                 bRet &= check(
578                     xLBT->getNullPolyString().member.getLength() == 0,
579                     "getNullPolyString");
580                 bRet &= check(
581                     xLBT->getNullPolyType().member == Type(),
582                     "getNullPolyType");
583                 Any nullAny(xLBT->getNullPolyAny().member);
584                 bRet &= check(
585                     (((nullAny.getValueTypeName() ==
586                        rtl::OUString(
587                            RTL_CONSTASCII_USTRINGPARAM(
588                                "com.sun.star.uno.XInterface"))) &&
589                       !static_cast< Reference< XInterface > const * >(
590                           nullAny.getValue())->is())
591                      || nullAny == Any()),
592                     "getNullPolyAny");
593                 bRet &= check(
594                     xLBT->getNullPolySequence().member.getLength() == 0,
595                     "getNullPolySequence");
596                 bRet &= check(
597                     xLBT->getNullPolyEnum().member == TestEnum_TEST,
598                     "getNullPolyEnum");
599                 bRet &= check(
600                     xLBT->getNullPolyBadEnum().member == TestBadEnum_M,
601                     "getNullPolyBadEnum");
602                 bRet &= check(
603                     xLBT->getNullPolyStruct().member.member == 0,
604                     "getNullPolyStruct");
605                 bRet &= check(
606                     !xLBT->getNullPolyInterface().member.is(),
607                     "getNullPolyInterface");
608             }
609 #endif // OS2
610 
611             // Any test:
612             bRet &= check(performAnyTest(xLBT , aData), "any test");
613             // Sequence of call test:
614             bRet &= check(
615                 performSequenceOfCallTest(xLBT), "sequence of call test");
616             // Recursive call test:
617             bRet &= check(performRecursiveCallTest(xLBT), "recursive test");
618             bRet &= check(
619                 equals(aData, aRet) && equals(aData, aRet2),
620                 "recursive test results");
621             // Multiple inheritance test:
622             bRet &= checkEmpty(
623                 testtools::bridgetest::testMulti(xLBT->getMulti()),
624                 "remote multi");
625             bRet &= checkEmpty(
626                 xLBT->testMulti(new testtools::bridgetest::Multi),
627                 "local multi");
628         }
629     }
630     {
631         Reference< XBridgeTest2 > xBT2(xLBT, UNO_QUERY);
632         if (!xBT2.is()) {
633             return bRet;
634         }
635         // Perform sequence tests (XBridgeTest2); create the sequence which is
636         // compared with the results:
637         sal_Bool _arBool[] = { true, false, true };
638         sal_Unicode _arChar[] = { 0x0065, 0x0066, 0x0067 };
639         sal_Int8 _arByte[] = { 1, 2, -1 };
640         sal_Int16 _arShort[] = { -0x8000, 1, 0x7FFF };
641         sal_uInt16 _arUShort[] = { 0 , 1, 0xFFFF };
642         sal_Int32 _arLong[] = { -0x7FFFFFFF, 1, 0x7FFFFFFF };
643         sal_uInt32 _arULong[] = { 0, 1, 0xFFFFFFFF };
644         sal_Int64 _arHyper[] = {
645             SAL_CONST_INT64(-0x7FFFFFFFFFFFFFFF), 1,
646             SAL_CONST_INT64(+0x7FFFFFFFFFFFFFFF) };
647         sal_uInt64 _arUHyper[] = { 0, 1, SAL_CONST_UINT64(0xFFFFFFFFFFFFFFFF) };
648         float _arFloat[] = { 1.1f, 2.2f, 3.3f };
649         double _arDouble[] = { 1.11, 2.22, 3.33 };
650         OUString _arString[] = {
651             OUString(RTL_CONSTASCII_USTRINGPARAM("String 1")),
652             OUString(RTL_CONSTASCII_USTRINGPARAM("String 2")),
653             OUString(RTL_CONSTASCII_USTRINGPARAM("String 3")) };
654         sal_Bool _aBool = true;
655         sal_Int32 _aInt = 0xBABEBABE;
656         float _aFloat = 3.14f;
657         Any _any1(&_aBool, getCppuBooleanType());
658         Any _any2(&_aInt, getCppuType((sal_Int32 *) 0));
659         Any _any3(&_aFloat, getCppuType((float *) 0));
660         Any _arAny[] = { _any1, _any2, _any3 };
661         Reference< XInterface > _arObj[3];
662         _arObj[0] = new OWeakObject();
663         _arObj[1] = new OWeakObject();
664         _arObj[2] = new OWeakObject();
665         TestEnum _arEnum[] = { TestEnum_ONE, TestEnum_TWO, TestEnum_CHECK };
666         TestElement _arStruct[3];
667         assign(
668             _arStruct[0], true, '@', 17, 0x1234, 0xFEDC, 0x12345678, 0xFEDCBA98,
669             SAL_CONST_INT64(0x123456789ABCDEF0),
670             SAL_CONST_UINT64(0xFEDCBA9876543210), 17.0815f, 3.1415926359,
671             TestEnum_LOLA, OUSTR(STRING_TEST_CONSTANT), _arObj[0],
672             Any(&_arObj[0], getCppuType((Reference< XInterface > const *) 0)));
673         assign(
674             _arStruct[1], true, 'A', 17, 0x1234, 0xFEDC, 0x12345678, 0xFEDCBA98,
675             SAL_CONST_INT64(0x123456789ABCDEF0),
676             SAL_CONST_UINT64(0xFEDCBA9876543210), 17.0815f, 3.1415926359,
677             TestEnum_TWO, OUSTR(STRING_TEST_CONSTANT), _arObj[1],
678             Any(&_arObj[1], getCppuType((Reference< XInterface > const *) 0)));
679         assign(
680             _arStruct[2], true, 'B', 17, 0x1234, 0xFEDC, 0x12345678, 0xFEDCBA98,
681             SAL_CONST_INT64(0x123456789ABCDEF0),
682             SAL_CONST_UINT64(0xFEDCBA9876543210), 17.0815f, 3.1415926359,
683             TestEnum_CHECK, OUSTR(STRING_TEST_CONSTANT), _arObj[2],
684             Any(&_arObj[2], getCppuType((Reference< XInterface > const *) 0)));
685         {
686             Sequence<sal_Bool> arBool(_arBool, 3);
687             Sequence<sal_Unicode> arChar( _arChar, 3);
688             Sequence<sal_Int8> arByte(_arByte, 3);
689             Sequence<sal_Int16> arShort(_arShort, 3);
690             Sequence<sal_uInt16> arUShort(_arUShort, 3);
691             Sequence<sal_Int32> arLong(_arLong, 3);
692             Sequence<sal_uInt32> arULong(_arULong, 3);
693             Sequence<sal_Int64> arHyper(_arHyper, 3);
694             Sequence<sal_uInt64> arUHyper(_arUHyper, 3);
695             Sequence<float> arFloat(_arFloat, 3);
696             Sequence<double> arDouble(_arDouble, 3);
697             Sequence<OUString> arString(_arString, 3);
698             Sequence<Any> arAny(_arAny, 3);
699             Sequence<Reference<XInterface> > arObject(_arObj, 3);
700             Sequence<TestEnum> arEnum(_arEnum, 3);
701             Sequence<TestElement> arStruct(_arStruct, 3);
702             Sequence<Sequence<sal_Int32> > _arSeqLong2[3];
703             for (int j = 0; j != 3; ++j) {
704                 Sequence< sal_Int32 > _arSeqLong[3];
705                 for (int i = 0; i != 3; ++i) {
706                     _arSeqLong[i] = Sequence< sal_Int32 >(_arLong, 3);
707                 }
708                 _arSeqLong2[j] = Sequence< Sequence< sal_Int32 > >(
709                     _arSeqLong, 3);
710             }
711             Sequence< Sequence< Sequence< sal_Int32> > > arLong3(
712                 _arSeqLong2, 3);
713             Sequence< Sequence< sal_Int32 > > seqSeqRet(
714                 xBT2->setDim2(arLong3[0]));
715             bRet &= check(seqSeqRet == arLong3[0], "sequence test");
716             Sequence< Sequence< Sequence< sal_Int32 > > > seqSeqRet2(
717                 xBT2->setDim3(arLong3));
718             bRet &= check(seqSeqRet2 == arLong3, "sequence test");
719             Sequence< Any > seqAnyRet(xBT2->setSequenceAny(arAny));
720             bRet &= check(seqAnyRet == arAny, "sequence test");
721             Sequence< sal_Bool > seqBoolRet(xBT2->setSequenceBool(arBool));
722             bRet &= check(seqBoolRet == arBool, "sequence test");
723             Sequence< sal_Int8 > seqByteRet(xBT2->setSequenceByte(arByte));
724             bRet &= check(seqByteRet == arByte, "sequence test");
725             Sequence< sal_Unicode > seqCharRet(xBT2->setSequenceChar(arChar));
726             bRet &= check(seqCharRet == arChar, "sequence test");
727             Sequence< sal_Int16 > seqShortRet(xBT2->setSequenceShort(arShort));
728             bRet &= check(seqShortRet == arShort, "sequence test");
729             Sequence< sal_Int32 > seqLongRet(xBT2->setSequenceLong(arLong));
730             bRet &= check(seqLongRet == arLong, "sequence test");
731             Sequence< sal_Int64 > seqHyperRet(xBT2->setSequenceHyper(arHyper));
732             bRet &= check(seqHyperRet == arHyper, "sequence test");
733             Sequence< float > seqFloatRet(xBT2->setSequenceFloat(arFloat));
734             bRet &= check(seqFloatRet == arFloat, "sequence test");
735             Sequence< double > seqDoubleRet(xBT2->setSequenceDouble(arDouble));
736             bRet &= check(seqDoubleRet == arDouble, "sequence test");
737             Sequence< TestEnum > seqEnumRet(xBT2->setSequenceEnum(arEnum));
738             bRet &= check(seqEnumRet == arEnum, "sequence test");
739             Sequence< sal_uInt16 > seqUShortRet(
740                 xBT2->setSequenceUShort(arUShort));
741             bRet &= check(seqUShortRet == arUShort, "sequence test");
742             Sequence< sal_uInt32 > seqULongRet(xBT2->setSequenceULong(arULong));
743             bRet &= check(seqULongRet == arULong, "sequence test");
744             Sequence< sal_uInt64 > seqUHyperRet(
745                 xBT2->setSequenceUHyper(arUHyper));
746             bRet &= check(seqUHyperRet == arUHyper, "sequence test");
747             Sequence< Reference< XInterface > > seqObjectRet(
748                 xBT2->setSequenceXInterface(arObject));
749             bRet &= check(seqObjectRet == arObject, "sequence test");
750             Sequence< OUString > seqStringRet(
751                 xBT2->setSequenceString(arString));
752             bRet &= check(seqStringRet == arString, "sequence test");
753             Sequence< TestElement > seqStructRet(
754                 xBT2->setSequenceStruct(arStruct));
755             bRet &= check(seqStructRet == arStruct, "sequence test");
756             Sequence< sal_Bool > arBoolTemp(cloneSequence(arBool));
757             Sequence< sal_Unicode > arCharTemp(cloneSequence(arChar));
758             Sequence< sal_Int8 > arByteTemp(cloneSequence(arByte));
759             Sequence< sal_Int16 > arShortTemp(cloneSequence(arShort));
760             Sequence< sal_uInt16 > arUShortTemp(cloneSequence(arUShort));
761             Sequence< sal_Int32 > arLongTemp(cloneSequence(arLong));
762             Sequence< sal_uInt32 > arULongTemp(cloneSequence(arULong));
763             Sequence< sal_Int64 > arHyperTemp(cloneSequence(arHyper));
764             Sequence< sal_uInt64 > arUHyperTemp(cloneSequence(arUHyper));
765             Sequence< float > arFloatTemp(cloneSequence(arFloat));
766             Sequence< double > arDoubleTemp(cloneSequence(arDouble));
767             Sequence< TestEnum > arEnumTemp(cloneSequence(arEnum));
768             Sequence< OUString > arStringTemp(cloneSequence(arString));
769             Sequence< Reference< XInterface > > arObjectTemp(
770                 cloneSequence(arObject));
771             Sequence< Any > arAnyTemp(cloneSequence(arAny));
772             Sequence< Sequence< sal_Int32 > > arLong2Temp(arLong3[0]);
773             Sequence< Sequence< Sequence< sal_Int32 > > > arLong3Temp(arLong3);
774             xBT2->setSequencesInOut(
775                 arBoolTemp, arCharTemp, arByteTemp, arShortTemp, arUShortTemp,
776                 arLongTemp,arULongTemp, arHyperTemp, arUHyperTemp, arFloatTemp,
777                 arDoubleTemp, arEnumTemp, arStringTemp, arObjectTemp, arAnyTemp,
778                 arLong2Temp, arLong3Temp);
779             bRet &= check(
780                 (arBoolTemp == arBool && arCharTemp == arChar &&
781                  arByteTemp == arByte && arShortTemp == arShort &&
782                  arUShortTemp == arUShort && arLongTemp == arLong &&
783                  arULongTemp == arULong && arHyperTemp == arHyper &&
784                  arUHyperTemp == arUHyper && arFloatTemp == arFloat &&
785                  arDoubleTemp == arDouble && arEnumTemp == arEnum &&
786                  arStringTemp == arString && arObjectTemp == arObject &&
787                  arAnyTemp == arAny && arLong2Temp == arLong3[0] &&
788                  arLong3Temp == arLong3),
789                 "sequence test");
790             Sequence< sal_Bool > arBoolOut;
791             Sequence< sal_Unicode > arCharOut;
792             Sequence< sal_Int8 > arByteOut;
793             Sequence< sal_Int16 > arShortOut;
794             Sequence< sal_uInt16 > arUShortOut;
795             Sequence< sal_Int32 > arLongOut;
796             Sequence< sal_uInt32 > arULongOut;
797             Sequence< sal_Int64 > arHyperOut;
798             Sequence< sal_uInt64 > arUHyperOut;
799             Sequence< float > arFloatOut;
800             Sequence< double > arDoubleOut;
801             Sequence< TestEnum > arEnumOut;
802             Sequence< OUString > arStringOut;
803             Sequence< Reference< XInterface > > arObjectOut;
804             Sequence< Any > arAnyOut;
805             Sequence< Sequence< sal_Int32 > > arLong2Out;
806             Sequence< Sequence< Sequence< sal_Int32 > > > arLong3Out;
807             xBT2->setSequencesOut(
808                 arBoolOut, arCharOut, arByteOut, arShortOut, arUShortOut,
809                 arLongOut,arULongOut, arHyperOut, arUHyperOut, arFloatOut,
810                 arDoubleOut, arEnumOut, arStringOut, arObjectOut, arAnyOut,
811                 arLong2Out, arLong3Out);
812             bRet &= check(
813                 (arBoolOut == arBool && arCharOut == arChar &&
814                  arByteOut == arByte && arShortOut == arShort &&
815                  arUShortOut == arUShort && arLongOut == arLong &&
816                  arULongOut == arULong && arHyperOut == arHyper &&
817                  arUHyperOut == arUHyper && arFloatOut == arFloat &&
818                  arDoubleOut == arDouble && arEnumOut == arEnum &&
819                  arStringOut == arString && arObjectOut == arObject &&
820                  arAnyOut == arAny && arLong2Out == arLong3[0] &&
821                  arLong3Out == arLong3),
822                 "sequence test");
823         }
824         {
825             // Test with empty sequences:
826             Sequence< Sequence< sal_Int32 > > arLong2;
827             Sequence< Sequence< sal_Int32 > > seqSeqRet(xBT2->setDim2(arLong2));
828             bRet &= check(seqSeqRet == arLong2, "sequence test");
829             Sequence< Sequence< Sequence< sal_Int32 > > > arLong3;
830             Sequence< Sequence< Sequence< sal_Int32 > > > seqSeqRet2(
831                 xBT2->setDim3(arLong3));
832             bRet &= check(seqSeqRet2 == arLong3, "sequence test");
833             Sequence< Any > arAny;
834             Sequence< Any > seqAnyRet(xBT2->setSequenceAny(arAny));
835             bRet &= check(seqAnyRet == arAny, "sequence test");
836             Sequence< sal_Bool > arBool;
837             Sequence< sal_Bool > seqBoolRet(xBT2->setSequenceBool(arBool));
838             bRet &= check(seqBoolRet == arBool, "sequence test");
839             Sequence< sal_Int8 > arByte;
840             Sequence< sal_Int8 > seqByteRet(xBT2->setSequenceByte(arByte));
841             bRet &= check(seqByteRet == arByte, "sequence test");
842             Sequence< sal_Unicode > arChar;
843             Sequence< sal_Unicode > seqCharRet(xBT2->setSequenceChar(arChar));
844             bRet &= check(seqCharRet == arChar, "sequence test");
845             Sequence< sal_Int16 > arShort;
846             Sequence< sal_Int16 > seqShortRet(xBT2->setSequenceShort(arShort));
847             bRet &= check(seqShortRet == arShort, "sequence test");
848             Sequence< sal_Int32 > arLong;
849             Sequence< sal_Int32 > seqLongRet(xBT2->setSequenceLong(arLong));
850             bRet &= check(seqLongRet == arLong, "sequence test");
851             Sequence< sal_Int64 > arHyper;
852             Sequence< sal_Int64 > seqHyperRet(xBT2->setSequenceHyper(arHyper));
853             bRet &= check(seqHyperRet == arHyper, "sequence test");
854             Sequence< float > arFloat;
855             Sequence< float > seqFloatRet(xBT2->setSequenceFloat(arFloat));
856             bRet &= check(seqFloatRet == arFloat, "sequence test");
857             Sequence< double > arDouble;
858             Sequence< double > seqDoubleRet(xBT2->setSequenceDouble(arDouble));
859             bRet &= check(seqDoubleRet == arDouble, "sequence test");
860             Sequence< TestEnum > arEnum;
861             Sequence< TestEnum > seqEnumRet(xBT2->setSequenceEnum(arEnum));
862             bRet &= check(seqEnumRet == arEnum, "sequence test");
863             Sequence< sal_uInt16 > arUShort;
864             Sequence< sal_uInt16 > seqUShortRet(
865                 xBT2->setSequenceUShort(arUShort));
866             bRet &= check(seqUShortRet == arUShort, "sequence test");
867             Sequence< sal_uInt32 > arULong;
868             Sequence< sal_uInt32 > seqULongRet(xBT2->setSequenceULong(arULong));
869             bRet &= check(seqULongRet == arULong, "sequence test");
870             Sequence< sal_uInt64 > arUHyper;
871             Sequence< sal_uInt64 > seqUHyperRet(
872                 xBT2->setSequenceUHyper(arUHyper));
873             bRet &= check(seqUHyperRet == arUHyper, "sequence test");
874             Sequence< Reference< XInterface > > arObject;
875             Sequence< Reference< XInterface > > seqObjectRet(
876                 xBT2->setSequenceXInterface(arObject));
877             bRet &= check(seqObjectRet == arObject, "sequence test");
878             Sequence< OUString > arString;
879             Sequence< OUString > seqStringRet(
880                 xBT2->setSequenceString(arString));
881             bRet &= check(seqStringRet == arString, "sequence test");
882             Sequence< TestElement > arStruct;
883             Sequence< TestElement > seqStructRet(
884                 xBT2->setSequenceStruct(arStruct));
885             bRet &= check(seqStructRet == arStruct, "sequence test");
886         }
887         // Issue #i60341# shows that the most interesting case is were Java
888         // calls the constructors; however, since this client is currently not
889         // available in Java, while the server is, the logic is reversed here:
890         try {
891             xBT2->testConstructorsService(xContext);
892         } catch (BadConstructorArguments &) {
893             bRet = false;
894         }
895         if (!noCurrentContext) {
896             if (!(new testtools::bridgetest::CurrentContextChecker)->perform(
897                     xBT2->getCurrentContextChecker(), 0, 1))
898             {
899                 bRet = false;
900             }
901             if (!(new testtools::bridgetest::CurrentContextChecker)->perform(
902                     xBT2->getCurrentContextChecker(), 0, 2))
903             {
904                 bRet = false;
905             }
906             if (!(new testtools::bridgetest::CurrentContextChecker)->perform(
907                     xBT2->getCurrentContextChecker(), 1, 2))
908             {
909                 bRet = false;
910             }
911             if (!(new testtools::bridgetest::CurrentContextChecker)->perform(
912                     xBT2->getCurrentContextChecker(), 1, 3))
913             {
914                 bRet = false;
915             }
916         }
917     }
918     return bRet;
919 }
920 
921 static sal_Bool raiseOnewayException( const Reference < XBridgeTest > & xLBT )
922 {
923 	sal_Bool bReturn = sal_True;
924 	OUString sCompare = OUSTR(STRING_TEST_CONSTANT);
925     Reference<XInterface> const x(xLBT->getInterface());
926 	try
927 	{
928 		// Note : the exception may fly or not (e.g. remote scenario).
929 		//        When it flies, it must contain the correct elements.
930 		xLBT->raiseRuntimeExceptionOneway( sCompare, x );
931 	}
932 	catch( RuntimeException & e )
933 	{
934 		bReturn = (
935 #if OSL_DEBUG_LEVEL == 0
936             // java stack traces trash Message
937             e.Message == sCompare &&
938 #endif
939             xLBT->getInterface() == e.Context &&
940             x == e.Context );
941 	}
942 	return bReturn;
943 }
944 
945 //==================================================================================================
946 static sal_Bool raiseException( const Reference< XBridgeTest > & xLBT )
947 {
948 	sal_Int32 nCount = 0;
949 	try
950 	{
951 		try
952 		{
953 			try
954 			{
955 				TestData aRet, aRet2;
956 				xLBT->raiseException(
957 					5, OUSTR(STRING_TEST_CONSTANT),
958 					xLBT->getInterface() );
959 			}
960 			catch (IllegalArgumentException aExc)
961 			{
962 				if (aExc.ArgumentPosition == 5 &&
963 #if OSL_DEBUG_LEVEL == 0
964                     // java stack traces trash Message
965                     aExc.Message.compareToAscii( STRING_TEST_CONSTANT ) == 0 &&
966 #endif
967 					aExc.Context == xLBT->getInterface())
968 				{
969 #ifdef COMPCHECK
970                     //When we check if a new compiler still works then we must not call
971                     //getRuntimeException because it uses cppu::getCaughtException which
972                     //does only work if all libs are build with the same runtime.
973                     return true;
974 #else
975 					++nCount;
976 #endif
977 				}
978 				else
979 				{
980 					check( sal_False, "### unexpected exception content!" );
981 				}
982 
983 				/** it is certain, that the RuntimeException testing will fail, if no */
984 				xLBT->getRuntimeException();
985 			}
986 		}
987 		catch (const RuntimeException & rExc)
988 		{
989 			if (rExc.Context == xLBT->getInterface()
990 #if OSL_DEBUG_LEVEL == 0
991                     // java stack traces trash Message
992                 && rExc.Message.compareToAscii( STRING_TEST_CONSTANT ) == 0
993 #endif
994                 )
995 			{
996 				++nCount;
997 			}
998 			else
999 			{
1000 				check( sal_False, "### unexpected exception content!" );
1001 			}
1002 
1003 			/** it is certain, that the RuntimeException testing will fail, if no */
1004 			xLBT->setRuntimeException( 0xcafebabe );
1005 		}
1006 	}
1007 	catch (Exception & rExc)
1008 	{
1009 		if (rExc.Context == xLBT->getInterface()
1010 #if OSL_DEBUG_LEVEL == 0
1011             // java stack traces trash Message
1012             && rExc.Message.compareToAscii( STRING_TEST_CONSTANT ) == 0
1013 #endif
1014             )
1015 		{
1016 			++nCount;
1017 		}
1018 		else
1019 		{
1020 			check( sal_False, "### unexpected exception content!" );
1021 		}
1022 		return (nCount == 3);
1023 	}
1024 	return sal_False;
1025 }
1026 
1027 /* Returns an acquired sequence
1028  */
1029 uno_Sequence* cloneSequence(const uno_Sequence* val, const Type& type)
1030 {
1031     TypeDescription td(type);
1032     td.makeComplete();
1033     typelib_TypeDescription* pTdRaw = td.get();
1034     typelib_IndirectTypeDescription* pIndirectTd =
1035         (typelib_IndirectTypeDescription*) pTdRaw;
1036 
1037     typelib_TypeDescription* pTdElem = pIndirectTd->pType->pType;
1038     sal_Int8* buf = new sal_Int8[pTdElem->nSize * val->nElements];
1039     sal_Int8* pBufCur = buf;
1040 
1041     uno_Sequence* retSeq = NULL;
1042     switch (pTdElem->eTypeClass)
1043     {
1044     case TypeClass_SEQUENCE:
1045     {
1046         Type _tElem(pTdElem->pWeakRef);
1047         for (int i = 0; i < val->nElements; i++)
1048         {
1049             uno_Sequence* seq = cloneSequence(
1050                 *(uno_Sequence**) (&val->elements + i * pTdElem->nSize),
1051                 _tElem);
1052             *((uno_Sequence**) pBufCur) = seq;
1053             pBufCur += pTdElem->nSize;
1054         }
1055         break;
1056     }
1057     default:
1058         uno_type_sequence_construct(
1059             &retSeq, type.getTypeLibType(), (void*) val->elements,
1060             val->nElements, reinterpret_cast< uno_AcquireFunc >(cpp_acquire));
1061         break;
1062     }
1063     delete[] buf;
1064     return retSeq;
1065 }
1066 
1067 template< class T>
1068 Sequence<T> cloneSequence(const Sequence<T>& val)
1069 {
1070     Sequence<T> seq( cloneSequence(val.get(), getCppuType(&val)), SAL_NO_ACQUIRE);
1071     return seq;
1072 }
1073 
1074 template< class T >
1075 inline bool makeSurrogate(
1076     Reference< T > & rOut, Reference< T > const & rOriginal )
1077 {
1078     rOut.clear();
1079     if (! rOriginal.is())
1080         return false;
1081 
1082     Environment aCppEnv_official;
1083     Environment aUnoEnv_ano;
1084     Environment aCppEnv_ano;
1085 
1086     OUString aCppEnvTypeName(
1087         RTL_CONSTASCII_USTRINGPARAM(CPPU_CURRENT_LANGUAGE_BINDING_NAME) );
1088     OUString aUnoEnvTypeName(
1089         RTL_CONSTASCII_USTRINGPARAM(UNO_LB_UNO) );
1090     // official:
1091     uno_getEnvironment(
1092         reinterpret_cast< uno_Environment ** >( &aCppEnv_official ),
1093         aCppEnvTypeName.pData, 0 );
1094     // anonymous:
1095     uno_createEnvironment(
1096         reinterpret_cast< uno_Environment ** >( &aCppEnv_ano ),
1097         aCppEnvTypeName.pData, 0 );
1098     uno_createEnvironment(
1099         reinterpret_cast< uno_Environment ** >( &aUnoEnv_ano ),
1100         aUnoEnvTypeName.pData, 0 );
1101 
1102     UnoInterfaceReference unoI;
1103     Mapping cpp2uno( aCppEnv_official.get(), aUnoEnv_ano.get() );
1104     Mapping uno2cpp( aUnoEnv_ano.get(), aCppEnv_ano.get() );
1105     if (!cpp2uno.is() || !uno2cpp.is())
1106     {
1107         throw RuntimeException(
1108             OUSTR("cannot get C++-UNO mappings!"),
1109             Reference< XInterface >() );
1110     }
1111     cpp2uno.mapInterface(
1112         reinterpret_cast< void ** >( &unoI.m_pUnoI ),
1113         rOriginal.get(), ::getCppuType( &rOriginal ) );
1114     if (! unoI.is())
1115     {
1116         throw RuntimeException(
1117             OUSTR("mapping C++ to binary UNO failed!"),
1118             Reference< XInterface >() );
1119     }
1120     uno2cpp.mapInterface(
1121         reinterpret_cast< void ** >( &rOut ),
1122         unoI.get(), ::getCppuType( &rOriginal ) );
1123     if (! rOut.is())
1124     {
1125         throw RuntimeException(
1126             OUSTR("mapping binary UNO to C++ failed!"),
1127             Reference< XInterface >() );
1128     }
1129 
1130 	return rOut.is();
1131 }
1132 
1133 //==================================================================================================
1134 sal_Int32 TestBridgeImpl::run( const Sequence< OUString > & rArgs )
1135 	throw (RuntimeException)
1136 {
1137     bool bRet = false;
1138     try
1139     {
1140         if (! rArgs.getLength())
1141         {
1142             throw RuntimeException( OUString( RTL_CONSTASCII_USTRINGPARAM(
1143                                                   "no test object specified!\n"
1144                                                   "usage : ServiceName of test object | -u unourl of test object\n" ) ),
1145                                     Reference< XInterface >() );
1146         }
1147 
1148         Reference< XInterface > xOriginal;
1149         bool remote;
1150         sal_Int32 i;
1151         if( rArgs.getLength() > 1 && 0 == rArgs[0].compareToAscii( "-u" ) )
1152         {
1153             remote = true;
1154             i = 2;
1155         }
1156         else
1157         {
1158             remote = false;
1159             i = 1;
1160         }
1161         bool noCurrentContext = false;
1162         if (i < rArgs.getLength()
1163             && rArgs[i].equalsAsciiL(
1164                 RTL_CONSTASCII_STRINGPARAM("noCurrentContext")))
1165         {
1166             noCurrentContext = true;
1167             ++i;
1168         }
1169         bool stress = false;
1170         if (i < rArgs.getLength()
1171             && rArgs[i].equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("stress")))
1172         {
1173             stress = true;
1174             ++i;
1175         }
1176 
1177         for (;;) {
1178             Reference< XInterface > o;
1179             if (remote) {
1180                 o = UnoUrlResolver::create(m_xContext)->resolve(rArgs[1]);
1181             } else {
1182                 o = m_xContext->getServiceManager()->createInstanceWithContext(
1183                     rArgs[0], m_xContext);
1184             }
1185             if (!stress) {
1186                 xOriginal = o;
1187                 break;
1188             }
1189         }
1190 
1191         if (! xOriginal.is())
1192         {
1193             throw RuntimeException(
1194                 OUString( RTL_CONSTASCII_USTRINGPARAM(
1195                               "cannot get test object!") ),
1196                 Reference< XInterface >() );
1197         }
1198         Reference< XBridgeTest > xTest( xOriginal, UNO_QUERY );
1199         if (! xTest.is())
1200         {
1201             throw RuntimeException(
1202                 OUString( RTL_CONSTASCII_USTRINGPARAM("test object does not implement XBridgeTest!") ),
1203                 Reference< XInterface >() );
1204         }
1205 
1206         Reference<XBridgeTest > xLBT;
1207         bRet = check( makeSurrogate( xLBT, xTest ), "makeSurrogate" );
1208         bRet = check(
1209             performTest( m_xContext, xLBT, noCurrentContext ), "standard test" )
1210             && bRet;
1211         bRet = check( raiseException( xLBT ) , "exception test" )&& bRet;
1212         bRet = check( raiseOnewayException( xLBT ),
1213                       "oneway exception test" ) && bRet;
1214         if (! bRet)
1215         {
1216             throw RuntimeException(
1217                 OUString( RTL_CONSTASCII_USTRINGPARAM("error: test failed!") ),
1218                 Reference< XInterface >() );
1219         }
1220 	}
1221     catch (Exception & exc)
1222     {
1223         OString cstr( OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
1224         fprintf( stderr, "exception occurred: %s\n", cstr.getStr() );
1225         throw;
1226     }
1227 
1228     if( bRet )
1229     {
1230         printf( "\n\n ### test succeeded!\n" );
1231     }
1232     else
1233     {
1234         printf( "\n> ### test failed!\n" );
1235     }
1236 
1237 	return 0;
1238 }
1239 
1240 // XServiceInfo
1241 //__________________________________________________________________________________________________
1242 OUString TestBridgeImpl::getImplementationName()
1243 	throw (RuntimeException)
1244 {
1245 	return OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME) );
1246 }
1247 //__________________________________________________________________________________________________
1248 sal_Bool TestBridgeImpl::supportsService( const OUString & rServiceName )
1249 	throw (RuntimeException)
1250 {
1251 	const Sequence< OUString > & rSNL = getSupportedServiceNames();
1252 	const OUString * pArray = rSNL.getConstArray();
1253 	for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
1254 	{
1255 		if (pArray[nPos] == rServiceName)
1256 			return sal_True;
1257 	}
1258 	return sal_False;
1259 }
1260 //__________________________________________________________________________________________________
1261 Sequence< OUString > TestBridgeImpl::getSupportedServiceNames()
1262 	throw (RuntimeException)
1263 {
1264 	return bridge_test::getSupportedServiceNames();
1265 }
1266 
1267 // ...
1268 
1269 //==================================================================================================
1270 static Reference< XInterface > SAL_CALL TestBridgeImpl_create(
1271 	const Reference< XComponentContext > & xContext )
1272 {
1273 	return Reference< XInterface >(
1274         static_cast< OWeakObject * >( new TestBridgeImpl( xContext ) ) );
1275 }
1276 
1277 }
1278 
1279 extern "C"
1280 {
1281 //==================================================================================================
1282 void SAL_CALL component_getImplementationEnvironment(
1283 	const sal_Char ** ppEnvTypeName, uno_Environment ** )
1284 {
1285 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
1286 }
1287 //==================================================================================================
1288 void * SAL_CALL component_getFactory(
1289 	const sal_Char * pImplName, void * pServiceManager, void * )
1290 {
1291 	void * pRet = 0;
1292 
1293 	if (pServiceManager && rtl_str_compare( pImplName, IMPLNAME ) == 0)
1294 	{
1295 		Reference< XInterface > xFactory(
1296             createSingleComponentFactory(
1297                 bridge_test::TestBridgeImpl_create,
1298                 OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME) ),
1299                 bridge_test::getSupportedServiceNames() ) );
1300 
1301 		if (xFactory.is())
1302 		{
1303 			xFactory->acquire();
1304 			pRet = xFactory.get();
1305 		}
1306 	}
1307 
1308 	return pRet;
1309 }
1310 }
1311