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 using System;
25 using System.Diagnostics;
26 using System.Reflection;
27 using uno;
28 using uno.util;
29 using unoidl.com.sun.star.uno;
30 using unoidl.com.sun.star.lang;
31 //using unoidl.com.sun.star.test.bridge;
32 using unoidl.test.testtools.bridgetest;
33 
34 namespace foo
35 {
36     public interface MyInterface
37     {
38     }
39 }
40 
41 namespace cs_testobj
42 {
43     class ORecursiveCall: WeakBase, XRecursiveCall
44     {
callRecursivly(XRecursiveCall xCall, int nToCall)45         public void  callRecursivly(XRecursiveCall xCall, int nToCall)
46 		{
47 			lock (this)
48             {
49                 if (nToCall > 0)
50                 {
51                     nToCall --;
52                     xCall.callRecursivly(this, nToCall);
53                 }
54             }
55         }
56     };
57 
58 class Constants
59 {
60    public const string STRING_TEST_CONSTANT = "\" paco\' chorizo\\\' \"\'";
61 }
62 
63 public class BridgeTest : WeakBase, XMain
64 {
compareData(Object val1, Object val2)65 	static bool compareData(Object val1, Object val2)
66 	{
67 		if (val1 == null && val2 == null || val1 == val2)
68 			return true;
69 		if ((val1 == null && val2 != null) ||
70 			(val1 != null && val2 == null) || val1.GetType() != val2.GetType())
71 			return false;
72 
73 		bool ret = false;
74 		Type t1  = val1.GetType();
75 			//Sequence
76 		if (t1.IsArray)
77 		{
78 			ret = compareSequence((Array) val1, (Array) val2);
79 		}
80 			//String
81 		else if (t1 == typeof(string))
82 		{
83 			ret = (string) val1 == (string) val2;
84 		}
85 			// Interface implementation
86 		else if (t1.GetInterfaces().Length > 0 && ! t1.IsValueType)
87 		{
88 			ret = val1 == val2;
89 		}
90 			// Struct
91 		else if ( ! t1.IsValueType)
92 		{
93 			ret = compareStruct(val1, val2);
94 		}
95 		else if (t1 == typeof(Any))
96 		{
97 			Any a1 = (Any) val1;
98 			Any a2 = (Any) val2;
99 			ret = a1.Type == a2.Type && compareData(a1.Value, a2.Value);
100 		}
101 		else if (t1.IsValueType)
102 		{
103 			//Any, enum, int, bool char, float, double etc.
104 			ret = val1.Equals(val2);
105 		}
106 		else
107 		{
108 			Debug.Assert(false);
109 		}
110 		return ret;
111 	}
112 
113 	// Arrays have only one dimension
compareSequence(Array ar1, Array ar2)114 	static bool compareSequence(Array ar1, Array ar2)
115 	{
116 		Debug.Assert(ar1 != null && ar2 != null);
117 		Type t1 = ar1.GetType();
118 		Type t2 = ar2.GetType();
119 
120 		if (!(ar1.Rank == 1 && ar2.Rank == 1
121 			&& ar1.Length == ar2.Length && t1.GetElementType() == t2.GetElementType()))
122 			return false;
123 
124 		//arrays have same rank and size and element type.
125 		int len  = ar1.Length;
126 		Type elemType = t1.GetElementType();
127 		bool ret = true;
128 		for (int i = 0; i < len; i++)
129 		{
130 			if (compareData(ar1.GetValue(i), ar2.GetValue(i)) == false)
131 			{
132 				ret = false;
133 				break;
134 			}
135 		}
136 		return ret;
137 	}
138 
compareStruct(Object val1, Object val2)139 	static bool compareStruct(Object val1, Object val2)
140 	{
141 		Debug.Assert(val1 != null && val2 != null);
142 		Type t1 = val1.GetType();
143 		Type t2 = val2.GetType();
144 		if (t1 != t2)
145 			return false;
146 		FieldInfo[] fields = t1.GetFields();
147 		int cFields = fields.Length;
148 		bool ret = true;
149 		for (int i = 0; i < cFields; i++)
150 		{
151 			Object fieldVal1 = fields[i].GetValue(val1);
152 			Object fieldVal2 = fields[i].GetValue(val2);
153 			if ( ! compareData(fieldVal1, fieldVal2))
154 			{
155 				ret = false;
156 				break;
157 			}
158 		}
159 		return ret;
160 	}
161 
check( bool b , string message )162     static bool check( bool b , string message )
163     {
164         if ( ! b)
165         Console.WriteLine("{0} failed\n" , message);
166         return b;
167     }
168 
equals(TestElement rData1, TestElement rData2)169     static bool equals(TestElement rData1, TestElement  rData2)
170     {
171         check( rData1.Bool == rData2.Bool, "### bool does not match!" );
172         check( rData1.Char == rData2.Char, "### char does not match!" );
173         check( rData1.Byte == rData2.Byte, "### byte does not match!" );
174         check( rData1.Short == rData2.Short, "### short does not match!" );
175         check( rData1.UShort == rData2.UShort, "### unsigned short does not match!" );
176         check( rData1.Long == rData2.Long, "### long does not match!" );
177         check( rData1.ULong == rData2.ULong, "### unsigned long does not match!" );
178         check( rData1.Hyper == rData2.Hyper, "### hyper does not match!" );
179         check( rData1.UHyper == rData2.UHyper, "### unsigned hyper does not match!" );
180         check( rData1.Float == rData2.Float, "### float does not match!" );
181         check( rData1.Double == rData2.Double, "### double does not match!" );
182         check( rData1.Enum == rData2.Enum, "### enum does not match!" );
183         check( rData1.String == rData2.String, "### string does not match!" );
184         check( rData1.Interface == rData2.Interface, "### interface does not match!" );
185         check( compareData(rData1.Any, rData2.Any), "### any does not match!" );
186 
187         return (rData1.Bool == rData2.Bool &&
188                 rData1.Char == rData2.Char &&
189                 rData1.Byte == rData2.Byte &&
190                 rData1.Short == rData2.Short &&
191                 rData1.UShort == rData2.UShort &&
192                 rData1.Long == rData2.Long &&
193                 rData1.ULong == rData2.ULong &&
194                 rData1.Hyper == rData2.Hyper &&
195                 rData1.UHyper == rData2.UHyper &&
196                 rData1.Float == rData2.Float &&
197                 rData1.Double == rData2.Double &&
198                 rData1.Enum == rData2.Enum &&
199                 rData1.String == rData2.String &&
200                 rData1.Interface == rData2.Interface &&
201                 compareData(rData1.Any, rData2.Any));
202     }
203 
assign( TestElement rData, bool bBool, char cChar, byte nByte, short nShort, ushort nUShort, int nLong, uint nULong, long nHyper, ulong nUHyper, float fFloat, double fDouble, TestEnum eEnum, string rStr, Object xTest, Any rAny )204 static void assign( TestElement rData,
205 					bool bBool, char cChar, byte nByte,
206 					short nShort, ushort nUShort,
207 					int nLong, uint nULong,
208 					long nHyper, ulong nUHyper,
209 					float fFloat, double fDouble,
210 					TestEnum eEnum, string rStr,
211 					Object xTest,
212 					Any rAny )
213 {
214 	rData.Bool = bBool;
215 	rData.Char = cChar;
216 	rData.Byte = nByte;
217 	rData.Short = nShort;
218 	rData.UShort = nUShort;
219 	rData.Long = nLong;
220 	rData.ULong = nULong;
221 	rData.Hyper = nHyper;
222 	rData.UHyper = nUHyper;
223 	rData.Float = fFloat;
224 	rData.Double = fDouble;
225 	rData.Enum = eEnum;
226 	rData.String = rStr;
227 	rData.Interface = xTest;
228 	rData.Any = rAny;
229 }
230 
assign( TestDataElements rData, bool bBool, char cChar, byte nByte, short nShort, ushort nUShort, int nLong, uint nULong, long nHyper, ulong nUHyper, float fFloat, double fDouble, TestEnum eEnum, string rStr, Object xTest, Any rAny, TestElement[] rSequence)231 static void assign( TestDataElements rData,
232 					bool bBool, char cChar, byte nByte,
233 					short nShort, ushort nUShort,
234 					int nLong, uint nULong,
235 					long nHyper, ulong nUHyper,
236 					float fFloat, double fDouble,
237 					TestEnum eEnum, string rStr,
238 					Object xTest,
239 					Any rAny,
240 					TestElement[] rSequence)
241 {
242 	assign( (TestElement) rData,
243 			bBool, cChar, nByte, nShort, nUShort, nLong, nULong, nHyper, nUHyper, fFloat, fDouble,
244 			eEnum, rStr, xTest, rAny );
245 	rData.Sequence = rSequence;
246 }
247 
248 // template < class type >
testAny(Type typ, Object value, XBridgeTest xLBT )249 static bool testAny(Type typ, Object  value, XBridgeTest xLBT )
250 {
251 	Any any;
252 	if (typ == null)
253 		any = new Any(value.GetType(), value);
254 	else
255 		any = new Any(typ, value);
256 
257 	Any any2 = xLBT.transportAny(any);
258 	bool ret;
259 	if( ! (ret= compareData(any, any2)))
260     {
261         Console.WriteLine("any is different after roundtrip: in {0}, out {1}\n",
262                           any.Type.FullName, any2.Type.FullName);
263     }
264 	return ret;
265 }
266 
267 
268 
performAnyTest(XBridgeTest xLBT, TestDataElements data)269 static bool performAnyTest(XBridgeTest xLBT,  TestDataElements data)
270 {
271 	bool bReturn = true;
272 	bReturn = testAny( null, data.Byte ,xLBT ) && bReturn;
273 	bReturn = testAny( null, data.Short,xLBT ) && bReturn;
274 	bReturn = testAny(  null, data.UShort,xLBT ) && bReturn;
275 	bReturn = testAny(  null, data.Long,xLBT ) && bReturn;
276 	bReturn = testAny(  null, data.ULong,xLBT ) && bReturn;
277 	bReturn = testAny(  null, data.Hyper,xLBT ) && bReturn;
278 	bReturn = testAny(  null,data.UHyper,xLBT ) && bReturn;
279 	bReturn = testAny( null, data.Float,xLBT ) && bReturn;
280 	bReturn = testAny( null, data.Double,xLBT ) && bReturn;
281 	bReturn = testAny( null, data.Enum,xLBT ) && bReturn;
282 	bReturn = testAny( null, data.String,xLBT ) && bReturn;
283 	bReturn = testAny(typeof(XWeak), data.Interface,xLBT ) && bReturn;
284 	bReturn = testAny(null, data, xLBT ) && bReturn;
285 
286 	{
287         Any a1= new Any(true);
288 		Any a2 = xLBT.transportAny( a1 );
289 		bReturn = compareData(a2, a1) && bReturn;
290 	}
291 
292 	{
293         Any a1= new Any('A');
294 		Any a2 = xLBT.transportAny(a1);
295 		bReturn = compareData(a2, a1) && bReturn;
296 	}
297 	return bReturn;
298 }
299 
performSequenceOfCallTest(XBridgeTest xLBT)300 static bool performSequenceOfCallTest(XBridgeTest xLBT)
301 {
302 	int i,nRounds;
303 	int nGlobalIndex = 0;
304 	const int nWaitTimeSpanMUSec = 10000;
305 	for( nRounds = 0 ; nRounds < 10 ; nRounds ++ )
306 	{
307 		for( i = 0 ; i < nRounds ; i ++ )
308 		{
309 			// fire oneways
310 			xLBT.callOneway(nGlobalIndex, nWaitTimeSpanMUSec);
311 			nGlobalIndex++;
312 		}
313 
314 		// call synchron
315 		xLBT.call(nGlobalIndex, nWaitTimeSpanMUSec);
316 		nGlobalIndex++;
317 	}
318  	return xLBT.sequenceOfCallTestPassed();
319 }
320 
321 
322 
323 
performRecursiveCallTest(XBridgeTest xLBT)324 static bool performRecursiveCallTest(XBridgeTest  xLBT)
325 {
326 	xLBT.startRecursiveCall(new ORecursiveCall(), 50);
327 	// on failure, the test would lock up or crash
328 	return true;
329 }
330 
performQueryForUnknownType(XBridgeTest xLBT)331 static bool performQueryForUnknownType(XBridgeTest xLBT)
332 {
333     bool bRet = false;
334     // test queryInterface for an unknown type
335     try
336     {
337         foo.MyInterface a = (foo.MyInterface) xLBT;
338     }
339     catch( System.InvalidCastException)
340     {
341         bRet = true;
342     }
343 
344     return bRet;
345 }
346 
347 // //==================================================================================================
performTest(XBridgeTest xLBT)348 bool performTest(XBridgeTest xLBT)
349 {
350 	check( xLBT != null, "### no test interface!" );
351     bool bRet = true;
352 	if (xLBT == null)
353         return false;
354 
355     // this data is never ever granted access to by calls other than equals(), assign()!
356     TestDataElements aData = new TestDataElements(); // test against this data
357 
358     Object xI= new WeakBase();
359 
360     Any aAny = new Any( typeof(Object), xI);
361     assign( (TestElement)aData,
362             true, '@', 17, 0x1234, 0xfedc, 0x12345678, 0xfedcba98,
363             0x123456789abcdef0, 0xfedcba9876543210,
364             17.0815f, 3.1415926359, TestEnum.LOLA,
365             Constants.STRING_TEST_CONSTANT, xI,
366             aAny);
367 
368     bRet = check( aData.Any.Value == xI, "### unexpected any!" ) && bRet;
369     bRet = check( !(aData.Any.Value != xI), "### unexpected any!" ) && bRet;
370 
371     aData.Sequence = new TestElement[2];
372     aData.Sequence[0] = new TestElement(
373         aData.Bool, aData.Char, aData.Byte, aData.Short,
374         aData.UShort, aData.Long, aData.ULong,
375         aData.Hyper, aData.UHyper, aData.Float,
376         aData.Double, aData.Enum, aData.String,
377         aData.Interface, aData.Any); //(TestElement) aData;
378     aData.Sequence[1] = new TestElement(); //is empty
379 
380     // aData complete
381     //
382     // this is a manually copy of aData for first setting...
383     TestDataElements aSetData = new TestDataElements();
384     Any aAnySet= new Any(typeof(Object), xI);
385     assign( (TestElement)aSetData,
386             aData.Bool, aData.Char, aData.Byte, aData.Short, aData.UShort,
387             aData.Long, aData.ULong, aData.Hyper, aData.UHyper, aData.Float, aData.Double,
388             aData.Enum, aData.String, xI,
389             aAnySet);
390 
391     aSetData.Sequence = new TestElement[2];
392     aSetData.Sequence[0] = new TestElement(
393         aSetData.Bool, aSetData.Char, aSetData.Byte, aSetData.Short,
394         aSetData.UShort, aSetData.Long, aSetData.ULong,
395         aSetData.Hyper, aSetData.UHyper, aSetData.Float,
396         aSetData.Double, aSetData.Enum, aSetData.String,
397         aSetData.Interface, aSetData.Any); //TestElement) aSetData;
398     aSetData.Sequence[1] = new TestElement(); // empty struct
399 
400     xLBT.setValues(
401         aSetData.Bool, aSetData.Char, aSetData.Byte, aSetData.Short, aSetData.UShort,
402         aSetData.Long, aSetData.ULong, aSetData.Hyper, aSetData.UHyper, aSetData.Float, aSetData.Double,
403         aSetData.Enum, aSetData.String, aSetData.Interface, aSetData.Any, aSetData.Sequence, aSetData );
404 
405     {
406 		TestDataElements aRet = new TestDataElements();
407         TestDataElements aRet2 = new TestDataElements();
408 		xLBT.getValues(
409 			out aRet.Bool, out aRet.Char, out aRet.Byte, out aRet.Short, out aRet.UShort,
410 			out aRet.Long, out aRet.ULong, out aRet.Hyper, out aRet.UHyper,
411             out aRet.Float, out aRet.Double, out aRet.Enum, out aRet.String,
412             out aRet.Interface, out aRet.Any, out aRet.Sequence, out aRet2 );
413 
414 		bRet = check( compareData( aData, aRet ) && compareData( aData, aRet2 ) , "getValues test") && bRet;
415 
416 		// set last retrieved values
417 		TestDataElements aSV2ret = xLBT.setValues2(
418 			ref aRet.Bool, ref aRet.Char, ref aRet.Byte, ref aRet.Short, ref aRet.UShort,
419 			ref aRet.Long, ref aRet.ULong, ref aRet.Hyper, ref aRet.UHyper, ref aRet.Float,
420             ref aRet.Double, ref aRet.Enum, ref aRet.String, ref aRet.Interface, ref aRet.Any,
421             ref aRet.Sequence, ref aRet2 );
422 
423         // check inout sequence order
424         // => inout sequence parameter was switched by test objects
425 		TestElement temp = aRet.Sequence[ 0 ];
426         aRet.Sequence[ 0 ] = aRet.Sequence[ 1 ];
427         aRet.Sequence[ 1 ] = temp;
428 
429 		bRet = check(
430             compareData( aData, aSV2ret ) && compareData( aData, aRet2 ),
431             "getValues2 test") && bRet;
432     }
433     {
434 		TestDataElements aRet = new TestDataElements();
435         TestDataElements aRet2 = new TestDataElements();
436 		TestDataElements aGVret = xLBT.getValues(
437 			out aRet.Bool, out aRet.Char, out aRet.Byte, out aRet.Short,
438             out aRet.UShort, out aRet.Long, out aRet.ULong, out aRet.Hyper,
439             out aRet.UHyper, out aRet.Float, out aRet.Double, out aRet.Enum,
440             out aRet.String, out aRet.Interface, out aRet.Any, out aRet.Sequence,
441             out aRet2 );
442 
443 		bRet = check( compareData( aData, aRet ) && compareData( aData, aRet2 ) && compareData( aData, aGVret ), "getValues test" ) && bRet;
444 
445 		// set last retrieved values
446 		xLBT.Bool = aRet.Bool;
447 		xLBT.Char = aRet.Char;
448 		xLBT.Byte = aRet.Byte;
449 		xLBT.Short = aRet.Short;
450 		xLBT.UShort = aRet.UShort;
451         xLBT.Long = aRet.Long;
452 		xLBT.ULong = aRet.ULong;
453 		xLBT.Hyper = aRet.Hyper;
454 		xLBT.UHyper = aRet.UHyper;
455 		xLBT.Float = aRet.Float;
456 		xLBT.Double = aRet.Double;
457 		xLBT.Enum = aRet.Enum;
458 		xLBT.String = aRet.String;
459 		xLBT.Interface = aRet.Interface;
460 		xLBT.Any = aRet.Any;
461 		xLBT.Sequence = aRet.Sequence;
462 		xLBT.Struct = aRet2;
463     }
464     {
465 		TestDataElements aRet = new TestDataElements();
466         TestDataElements aRet2 = new TestDataElements();
467 		aRet.Hyper = xLBT.Hyper;
468 		aRet.UHyper = xLBT.UHyper;
469 		aRet.Float = xLBT.Float;
470 		aRet.Double = xLBT.Double;
471 		aRet.Byte = xLBT.Byte;
472 		aRet.Char = xLBT.Char;
473 		aRet.Bool = xLBT.Bool;
474 		aRet.Short = xLBT.Short;
475 		aRet.UShort = xLBT.UShort;
476 		aRet.Long = xLBT.Long;
477 		aRet.ULong = xLBT.ULong;
478 		aRet.Enum = xLBT.Enum;
479 		aRet.String = xLBT.String;
480 		aRet.Interface = xLBT.Interface;
481 		aRet.Any = xLBT.Any;
482 		aRet.Sequence = xLBT.Sequence;
483 		aRet2 = xLBT.Struct;
484 
485 		bRet = check( compareData( aData, aRet ) && compareData( aData, aRet2 ) , "struct comparison test") && bRet;
486 
487 		bRet = check(performSequenceTest(xLBT), "sequence test") && bRet;
488 
489 		// any test
490 		bRet = check( performAnyTest( xLBT , aData ) , "any test" ) && bRet;
491 
492 		// sequence of call test
493 		bRet = check( performSequenceOfCallTest( xLBT ) , "sequence of call test" ) && bRet;
494 
495 		// recursive call test
496 		bRet = check( performRecursiveCallTest( xLBT ) , "recursive test" ) && bRet;
497 
498 		bRet = (compareData( aData, aRet ) && compareData( aData, aRet2 )) && bRet ;
499 
500         // check setting of null reference
501         xLBT.Interface = null;
502         aRet.Interface = xLBT.Interface;
503         bRet = (aRet.Interface == null) && bRet;
504 
505     }
506         // Test extended attributes that raise exceptions:
507     try {
508         int i = xLBT.RaiseAttr1;
509             bRet &= check(false, "getRaiseAttr1 did not throw");
510     } catch (RuntimeException )
511     {
512     }
513     catch (System.Exception) {
514         bRet &= check(false, "getRaiseAttr1 threw wrong type");
515     }
516     try {
517         xLBT.RaiseAttr1 = 0;
518         bRet &= check(false, "setRaiseAttr1 did not throw");
519     } catch (IllegalArgumentException) {
520     } catch (System.Exception) {
521         bRet &= check(false, "setRaiseAttr1 threw wrong type");
522     }
523     try {
524         int i = xLBT.RaiseAttr2;
525         bRet &= check(false, "getRaiseAttr2 did not throw");
526     } catch (IllegalArgumentException ) {
527     } catch (System.Exception) {
528         bRet &= check(false, "getRaiseAttr2 threw wrong type");
529     }
530 
531        // Test instantiated polymorphic struct types:
532     {
533         TestPolyStruct poly = new TestPolyStruct(true);
534         bRet &= check(
535             (bool) xLBT.transportPolyBoolean(poly).member,
536             "transportPolyBoolean");
537         poly = new TestPolyStruct(12345L);
538         xLBT.transportPolyHyper(ref poly);
539         bRet &= check((long)poly.member == 12345L, "transportPolyUnsignedHyper");
540 
541         Any[] seq = {  new Any(33), new Any("ABC")};
542         poly = new TestPolyStruct(seq);
543         TestPolyStruct poly2;
544         xLBT.transportPolySequence(poly, out poly2);
545         try {
546             Any[] ar = (Any[]) poly2.member;
547             bRet &= check(
548             ar.Length == 2, "transportPolySequence, length");
549 
550             int v0;
551             v0 = (int) ar[0].Value;
552             bRet &= check(v0 == 33, "transportPolySequence, element 0");
553 
554             string v1 = (string) ar[1].Value;
555             bRet &= check(
556             v1.Equals("ABC"),
557             "transportPolySequence, element 1");
558         } catch (InvalidCastException )
559         {
560             bRet &= check(false, "transportPolySequence");
561         }
562 
563         try {
564             //When the test object is a cli object then them member is null
565             //otherwise the bridge has provided a default value.
566             TestPolyStruct s =  xLBT.getNullPolyLong();
567             if (s.member != null)
568                 bRet &= check(((int) s.member) == 0, "getNullPolyLong");
569 
570             s = xLBT.getNullPolyString();
571             if (s.member != null)
572                 bRet &= check(((string) s.member).Length == 0,
573                               "getNullPolyString");
574             s = xLBT.getNullPolyType();
575             if (s.member != null)
576                 bRet &= check(((Type) s.member) == typeof(void),
577                               "getNullPolyType");
578             s = xLBT.getNullPolyAny();
579             if (s.member != null)
580             {
581                 Any nullAny = (Any) s.member;
582                 //???
583                 bRet &= check(nullAny.Type == typeof(void),
584                     "getNullPolyAny");
585             }
586             s = xLBT.getNullPolySequence();
587             if (s.member != null)
588                 bRet &= check(((bool[]) s.member).Length == 0,
589                               "getNullPolySequence");
590             s = xLBT.getNullPolyEnum();
591             if (s.member != null)
592                 bRet &= check(((TestEnum) s.member) == TestEnum.TEST,
593                               "getNullPolyEnum");
594             s = xLBT.getNullPolyStruct();
595             if (s.member != null)
596                 bRet &= check(((TestStruct) s.member).member == 0,
597                               "getNullPolyStruct");
598             s = xLBT.getNullPolyInterface();
599                 bRet &= check(s.member == null, "getNullPolyInterface");
600 
601             s = xLBT.getNullPolyBadEnum();
602             bRet &= check(((TestBadEnum)s.member) == TestBadEnum.M, "getNullPolyBadEnum");
603 
604         } catch(InvalidCastException)
605         {
606             bRet &= check(false, "getNullPolyXXX, InvalidCastException");
607         }
608 
609         }
610 
611     XBridgeTest2 xBT2 = xLBT as XBridgeTest2;
612     if (xBT2 != null) {
613         try {
614             xBT2.testConstructorsService(m_xContext);
615         } catch (BadConstructorArguments) {
616             bRet = false;
617         }
618     }
619 
620     return bRet;
621 }
performSequenceTest(XBridgeTest xBT)622 static bool performSequenceTest(XBridgeTest xBT)
623 {
624     bool bRet = true;
625     XBridgeTest2  xBT2 = xBT as XBridgeTest2;
626     if ( xBT2 == null)
627         return false;
628 
629     // perform sequence tests (XBridgeTest2)
630     // create the sequence which are compared with the results
631     bool[] arBool = {true, false, true};
632     char[] arChar = {'A','B','C'};
633     byte[] arByte = { 1,  2,  0xff};
634     short[] arShort = {Int16.MinValue, 1,  Int16.MaxValue};
635     UInt16[] arUShort = {UInt16.MinValue , 1, UInt16.MaxValue};
636     int[] arLong = {Int32.MinValue, 1, Int32.MaxValue};
637     UInt32[] arULong = {UInt32.MinValue, 1, UInt32.MaxValue};
638     long[] arHyper = {Int64.MinValue, 1, Int64.MaxValue};
639     UInt64[] arUHyper = {UInt64.MinValue, 1, UInt64.MaxValue};
640     float[] arFloat = {1.1f, 2.2f, 3.3f};
641     double[] arDouble = {1.11, 2.22, 3.33};
642     string[] arString = {"String 1", "String 2", "String 3"};
643 
644     Any[] arAny = {new Any(true), new Any(11111), new Any(3.14)};
645     Object[] arObject = {new WeakBase(), new WeakBase(), new WeakBase()};
646     TestEnum[] arEnum = {TestEnum.ONE, TestEnum.TWO, TestEnum.CHECK};
647 
648     TestElement[] arStruct = {new TestElement(), new TestElement(),
649                                new TestElement()};
650     assign( arStruct[0], true, '@', 17, 0x1234, 0xfedc, 0x12345678, 0xfedcba98,
651  			0x123456789abcdef0, 0xfedcba9876543210, 17.0815f, 3.1415926359,
652             TestEnum.LOLA, Constants.STRING_TEST_CONSTANT, arObject[0],
653             new Any( typeof(Object),  arObject[0]) );
654     assign( arStruct[1], true, 'A', 17, 0x1234, 0xfedc, 0x12345678, 0xfedcba98,
655 			0x123456789abcdef0, 0xfedcba9876543210, 17.0815f, 3.1415926359,
656             TestEnum.TWO, Constants.STRING_TEST_CONSTANT, arObject[1],
657             new Any( typeof(Object), arObject[1]) );
658     assign( arStruct[2], true, 'B', 17, 0x1234, 0xfedc, 0x12345678, 0xfedcba98,
659 			0x123456789abcdef0, 0xfedcba9876543210, 17.0815f, 3.1415926359,
660             TestEnum.CHECK, Constants.STRING_TEST_CONSTANT, arObject[2],
661             new Any( typeof(Object), arObject[2] ) );
662 
663 
664     int[][][] arLong3 = new int[][][]{
665         new int[][]{new int[]{1,2,3},new int[]{4,5,6}, new int[]{7,8,9} },
666         new int [][]{new int[]{1,2,3},new int[]{4,5,6}, new int[]{7,8,9}},
667         new int[][]{new int[]{1,2,3},new int[]{4,5,6}, new int[]{7,8,9}}};
668 
669     {
670     int[][] seqSeqRet = xBT2.setDim2(arLong3[0]);
671     bRet = check( compareData(seqSeqRet, arLong3[0]), "sequence test") && bRet;
672     int[][][] seqSeqRet2 = xBT2.setDim3(arLong3);
673     bRet = check( compareData(seqSeqRet2, arLong3), "sequence test") && bRet;
674     Any[] seqAnyRet = xBT2.setSequenceAny(arAny);
675     bRet = check( compareData(seqAnyRet, arAny), "sequence test") && bRet;
676     bool[] seqBoolRet = xBT2.setSequenceBool(arBool);
677     bRet = check( compareData(seqBoolRet, arBool), "sequence test") && bRet;
678     byte[] seqByteRet = xBT2.setSequenceByte(arByte);
679     bRet = check( compareData(seqByteRet, arByte), "sequence test") && bRet;
680     char[] seqCharRet = xBT2.setSequenceChar(arChar);
681     bRet = check( compareData(seqCharRet, arChar), "sequence test") && bRet;
682     short[] seqShortRet = xBT2.setSequenceShort(arShort);
683     bRet = check( compareData(seqShortRet, arShort), "sequence test") && bRet;
684     int[] seqLongRet = xBT2.setSequenceLong(arLong);
685     bRet = check( compareData(seqLongRet, arLong), "sequence test") && bRet;
686     long[] seqHyperRet = xBT2.setSequenceHyper(arHyper);
687     bRet = check( compareData(seqHyperRet,arHyper), "sequence test") && bRet;
688     float[] seqFloatRet = xBT2.setSequenceFloat(arFloat);
689     bRet = check( compareData(seqFloatRet, arFloat), "sequence test") && bRet;
690     double[] seqDoubleRet = xBT2.setSequenceDouble(arDouble);
691     bRet = check( compareData(seqDoubleRet, arDouble), "sequence test") && bRet;
692     TestEnum[] seqEnumRet = xBT2.setSequenceEnum(arEnum);
693     bRet = check( compareData(seqEnumRet, arEnum), "sequence test") && bRet;
694     UInt16[] seqUShortRet = xBT2.setSequenceUShort(arUShort);
695     bRet = check( compareData(seqUShortRet, arUShort), "sequence test") && bRet;
696     UInt32[] seqULongRet = xBT2.setSequenceULong(arULong);
697     bRet = check( compareData(seqULongRet, arULong), "sequence test") && bRet;
698     UInt64[] seqUHyperRet = xBT2.setSequenceUHyper(arUHyper);
699     bRet = check( compareData(seqUHyperRet, arUHyper), "sequence test") && bRet;
700     Object[] seqObjectRet = xBT2.setSequenceXInterface(arObject);
701     bRet = check( compareData(seqObjectRet, arObject), "sequence test") && bRet;
702     string[] seqStringRet = xBT2.setSequenceString(arString);
703     bRet = check( compareData(seqStringRet, arString), "sequence test") && bRet;
704     TestElement[] seqStructRet = xBT2.setSequenceStruct(arStruct);
705     bRet = check( compareData(seqStructRet, arStruct), "sequence test") && bRet;
706     }
707     {
708     bool[] arBoolTemp = (bool[]) arBool.Clone();
709     char[] arCharTemp = (char[]) arChar.Clone();
710     byte[] arByteTemp = (byte[]) arByte.Clone();
711     short[] arShortTemp = (short[]) arShort.Clone();
712     UInt16[] arUShortTemp = (UInt16[]) arUShort.Clone();
713     int[] arLongTemp = (int[]) arLong.Clone();
714     UInt32[] arULongTemp = (UInt32[]) arULong.Clone();
715     long[] arHyperTemp = (long[]) arHyper.Clone();
716     UInt64[] arUHyperTemp = (UInt64[]) arUHyper.Clone();
717     float[] arFloatTemp = (float[]) arFloat.Clone();
718     double[] arDoubleTemp = (double[]) arDouble.Clone();
719     TestEnum[] arEnumTemp = (TestEnum[]) arEnum.Clone();
720     string[] arStringTemp = (string[]) arString.Clone();
721     Object[] arObjectTemp = (Object[]) arObject.Clone();
722     Any[] arAnyTemp = (Any[]) arAny.Clone();
723     // make sure this are has the same contents as arLong3[0]
724     int[][] arLong2Temp = new int[][]{new int[]{1,2,3},new int[]{4,5,6}, new int[]{7,8,9} };
725     // make sure this are has the same contents as arLong3
726     int[][][] arLong3Temp = new int[][][]{
727         new int[][]{new int[]{1,2,3},new int[]{4,5,6}, new int[]{7,8,9} },
728         new int [][]{new int[]{1,2,3},new int[]{4,5,6}, new int[]{7,8,9}},
729         new int[][]{new int[]{1,2,3},new int[]{4,5,6}, new int[]{7,8,9}}};
730 
731     xBT2.setSequencesInOut(ref arBoolTemp, ref arCharTemp, ref arByteTemp,
732                            ref arShortTemp, ref arUShortTemp, ref arLongTemp,
733                            ref arULongTemp,ref arHyperTemp, ref arUHyperTemp,
734                            ref arFloatTemp,ref arDoubleTemp, ref arEnumTemp,
735                            ref arStringTemp, ref  arObjectTemp,
736                            ref arAnyTemp, ref arLong2Temp, ref arLong3Temp);
737     bRet = check(
738         compareData(arBoolTemp, arBool) &&
739         compareData(arCharTemp , arChar) &&
740         compareData(arByteTemp , arByte) &&
741         compareData(arShortTemp , arShort) &&
742         compareData(arUShortTemp , arUShort) &&
743         compareData(arLongTemp , arLong) &&
744         compareData(arULongTemp , arULong) &&
745         compareData(arHyperTemp , arHyper) &&
746         compareData(arUHyperTemp , arUHyper) &&
747         compareData(arFloatTemp , arFloat) &&
748         compareData(arDoubleTemp , arDouble) &&
749         compareData(arEnumTemp , arEnum) &&
750         compareData(arStringTemp , arString) &&
751         compareData(arObjectTemp , arObject) &&
752         compareData(arAnyTemp , arAny) &&
753         compareData(arLong2Temp , arLong3[0]) &&
754         compareData(arLong3Temp , arLong3), "sequence test") && bRet;
755 
756     bool[] arBoolOut;
757     char[] arCharOut;
758     byte[] arByteOut;
759     short[] arShortOut;
760     UInt16[] arUShortOut;
761     int[] arLongOut;
762     UInt32[] arULongOut;
763     long[] arHyperOut;
764     UInt64[] arUHyperOut;
765     float[] arFloatOut;
766     double[] arDoubleOut;
767     TestEnum[] arEnumOut;
768     string[] arStringOut;
769     Object[] arObjectOut;
770     Any[] arAnyOut;
771     int[][] arLong2Out;
772     int[][][] arLong3Out;
773 
774     xBT2.setSequencesOut(out arBoolOut, out arCharOut, out arByteOut,
775                          out arShortOut, out arUShortOut, out arLongOut,
776                          out arULongOut, out arHyperOut, out arUHyperOut,
777                          out arFloatOut, out arDoubleOut, out arEnumOut,
778                          out arStringOut, out arObjectOut, out arAnyOut,
779                          out arLong2Out, out arLong3Out);
780     bRet = check(
781         compareData(arBoolOut, arBool) &&
782         compareData(arCharOut, arChar) &&
783         compareData(arByteOut, arByte) &&
784         compareData(arShortOut, arShort) &&
785         compareData(arUShortOut, arUShort) &&
786         compareData(arLongOut, arLong) &&
787         compareData(arULongOut, arULong) &&
788         compareData(arHyperOut, arHyper) &&
789         compareData(arUHyperOut, arUHyper) &&
790         compareData(arFloatOut, arFloat) &&
791         compareData(arDoubleOut, arDouble) &&
792         compareData(arEnumOut, arEnum) &&
793         compareData(arStringOut, arString) &&
794         compareData(arObjectOut, arObject) &&
795         compareData(arAnyOut, arAny) &&
796         compareData(arLong2Out, arLong3[0]) &&
797         compareData(arLong3Out, arLong3), "sequence test") && bRet;
798     }
799     {
800     //test with empty sequences
801     int[][] _arLong2 = new int[0][];
802     int[][] seqSeqRet = xBT2.setDim2(_arLong2);
803     bRet = check( compareData(seqSeqRet, _arLong2), "sequence test") && bRet;
804     int[][][] _arLong3 = new int[0][][];
805     int[][][] seqSeqRet2 = xBT2.setDim3(_arLong3);
806     bRet = check( compareData(seqSeqRet2, _arLong3), "sequence test") && bRet;
807     Any[] _arAny = new Any[0];
808     Any[] seqAnyRet = xBT2.setSequenceAny(_arAny);
809     bRet = check( compareData(seqAnyRet, _arAny), "sequence test") && bRet;
810     bool[] _arBool = new bool[0];
811     bool[] seqBoolRet = xBT2.setSequenceBool(_arBool);
812     bRet = check( compareData(seqBoolRet, _arBool), "sequence test") && bRet;
813     byte[] _arByte = new byte[0];
814     byte[] seqByteRet = xBT2.setSequenceByte(_arByte);
815     bRet = check( compareData(seqByteRet, _arByte), "sequence test") && bRet;
816     char[] _arChar = new char[0];
817     char[] seqCharRet = xBT2.setSequenceChar(_arChar);
818     bRet = check( compareData(seqCharRet, _arChar), "sequence test") && bRet;
819     short[] _arShort = new short[0];
820     short[] seqShortRet = xBT2.setSequenceShort(_arShort);
821     bRet = check( compareData(seqShortRet, _arShort), "sequence test") && bRet;
822     int[] _arLong = new int[0];
823     int[] seqLongRet = xBT2.setSequenceLong(_arLong);
824     bRet = check( compareData(seqLongRet, _arLong), "sequence test") && bRet;
825     long[] _arHyper = new long[0];
826     long[] seqHyperRet = xBT2.setSequenceHyper(_arHyper);
827     bRet = check( compareData(seqHyperRet, _arHyper), "sequence test") && bRet;
828     float[] _arFloat = new float[0];
829     float[] seqFloatRet = xBT2.setSequenceFloat(_arFloat);
830     bRet = check( compareData(seqFloatRet, _arFloat), "sequence test") && bRet;
831     double[] _arDouble = new double[0];
832     double[] seqDoubleRet = xBT2.setSequenceDouble(_arDouble);
833     bRet = check( compareData(seqDoubleRet, _arDouble), "sequence test") && bRet;
834     TestEnum[] _arEnum = new TestEnum[0];
835     TestEnum[] seqEnumRet = xBT2.setSequenceEnum(_arEnum);
836     bRet = check( compareData(seqEnumRet, _arEnum), "sequence test") && bRet;
837     UInt16[] _arUShort = new UInt16[0];
838     UInt16[] seqUShortRet = xBT2.setSequenceUShort(_arUShort);
839     bRet = check( compareData(seqUShortRet, _arUShort), "sequence test") && bRet;
840     UInt32[] _arULong = new UInt32[0];
841     UInt32[] seqULongRet = xBT2.setSequenceULong(_arULong);
842     bRet = check( compareData(seqULongRet, _arULong), "sequence test") && bRet;
843     UInt64[] _arUHyper = new UInt64[0];
844     UInt64[] seqUHyperRet = xBT2.setSequenceUHyper(_arUHyper);
845     bRet = check( compareData(seqUHyperRet, _arUHyper), "sequence test") && bRet;
846     Object[] _arObject = new Object[0];
847     Object[] seqObjectRet = xBT2.setSequenceXInterface(_arObject);
848     bRet = check( compareData(seqObjectRet, _arObject), "sequence test") && bRet;
849     string[] _arString = new string[0];
850     string[] seqStringRet = xBT2.setSequenceString(_arString);
851     bRet = check( compareData(seqStringRet, _arString), "sequence test") && bRet;
852     TestElement[] _arStruct = new TestElement[0];
853     TestElement[] seqStructRet = xBT2.setSequenceStruct(_arStruct);
854     bRet = check( compareData(seqStructRet, _arStruct), "sequence test") && bRet;
855 
856     }
857 
858 
859     return bRet;
860 }
861 /** Test the System::Object method on the proxy object
862  */
testObjectMethodsImplemention(XBridgeTest xLBT)863 static bool testObjectMethodsImplemention(XBridgeTest xLBT)
864 {
865     bool ret = false;
866     Object obj = new Object();
867 	Object xInt = (Object) xLBT;
868 	XBridgeTestBase xBase = xLBT as XBridgeTestBase;
869 	if (xBase == null)
870 		return false;
871 	// Object.Equals
872 	ret = xLBT.Equals(obj) == false;
873 	ret = xLBT.Equals(xLBT) && ret;
874 	ret = Object.Equals(obj, obj) && ret;
875 	ret = Object.Equals(xLBT, xBase) && ret;
876 	//Object.GetHashCode
877 	// Don't know how to verify this. Currently it is not possible to get the object id from a proxy
878 	int nHash = xLBT.GetHashCode();
879 	ret = nHash == xBase.GetHashCode() && ret;
880 
881 	//Object.ToString
882     // Don't know how to verify this automatically.
883 	string s = xLBT.ToString();
884     ret = (s.Length > 0) && ret;
885     return ret;
886 }
887 
888 
raiseOnewayException(XBridgeTest xLBT)889 static bool raiseOnewayException(XBridgeTest xLBT)
890 {
891     bool bReturn = true;
892 	string sCompare = Constants.STRING_TEST_CONSTANT;
893 	try
894 	{
895 		// Note : the exception may fly or not (e.g. remote scenario).
896 		//        When it flies, it must contain the correct elements.
897 		xLBT.raiseRuntimeExceptionOneway(sCompare, xLBT.Interface );
898 	}
899 	catch (RuntimeException  e )
900 	{
901 		bReturn = ( xLBT.Interface == e.Context );
902 	}
903     return bReturn;
904 }
905 
906 // //==================================================================================================
raiseException(XBridgeTest xLBT )907 static bool raiseException(XBridgeTest xLBT )
908 {
909 	int nCount = 0;
910 	try
911 	{
912 		try
913 		{
914 			try
915 			{
916 				TestDataElements aRet = new TestDataElements();
917                 TestDataElements aRet2 = new TestDataElements();
918 				xLBT.raiseException(
919 					5, Constants.STRING_TEST_CONSTANT, xLBT.Interface );
920 			}
921 			catch (unoidl.com.sun.star.lang.IllegalArgumentException aExc)
922 			{
923 				if (aExc.ArgumentPosition == 5 &&
924                     aExc.Context == xLBT.Interface)
925 				{
926 					++nCount;
927 				}
928 				else
929 				{
930 					check( false, "### unexpected exception content!" );
931 				}
932 
933 				/** it is certain, that the RuntimeException testing will fail,
934                     if no */
935 				xLBT.RuntimeException = 0;
936 			}
937 		}
938 		catch (unoidl.com.sun.star.uno.RuntimeException rExc)
939 		{
940 			if (rExc.Context == xLBT.Interface )
941 			{
942 				++nCount;
943 			}
944 			else
945 			{
946 				check( false, "### unexpected exception content!" );
947 			}
948 
949 			/** it is certain, that the RuntimeException testing will fail, if no */
950             unchecked
951              {
952                  xLBT.RuntimeException = (int) 0xcafebabe;
953              }
954 		}
955 	}
956 	catch (unoidl.com.sun.star.uno.Exception  rExc)
957 	{
958 		if (rExc.Context == xLBT.Interface)
959 		{
960 			++nCount;
961 		}
962 		else
963 
964 		{
965 			check( false, "### unexpected exception content!" );
966 		}
967 		return (nCount == 3);
968 	}
969     return false;
970 }
971 
perform_test( XBridgeTest xLBT )972     private void perform_test( XBridgeTest xLBT )
973     {
974         bool bRet= true;;
975        bRet = check( performTest( xLBT ), "standard test" ) && bRet;
976        bRet = check( raiseException( xLBT ) , "exception test" )&& bRet;
977        bRet = check( raiseOnewayException( xLBT ), "oneway exception test" ) && bRet;
978        bRet = check( testObjectMethodsImplemention(xLBT), "object methods test") && bRet;
979        bRet = performQueryForUnknownType( xLBT ) && bRet;
980         if ( ! bRet)
981         {
982             throw new unoidl.com.sun.star.uno.RuntimeException( "error (cli_cs_bridgetest.cs): test failed!", null);
983         }
984     }
985 
BridgeTest( XComponentContext xContext )986     public BridgeTest( XComponentContext xContext )
987     {
988         m_xContext = xContext;
989     }
990 
991     private XComponentContext m_xContext;
992 
run( String [] args )993 	public int run( String [] args )
994 	{
995 		Debug.AutoFlush = true;
996 //		System.Diagnostics.Debugger.Launch();
997 		try
998 		{
999 			if (args.Length < 1)
1000 			{
1001 				throw new RuntimeException(
1002 					"missing argument for bridgetest!", this );
1003 			}
1004 			Object test_obj =
1005 				m_xContext.getServiceManager().createInstanceWithContext(
1006 				args[ 0 ], m_xContext );
1007 
1008 			Debug.WriteLine(
1009 				"Calling object: {0}", test_obj.ToString() );
1010 
1011 			XBridgeTest xTest = (XBridgeTest) test_obj ;
1012 			perform_test( xTest );
1013 			Console.WriteLine( "\n### cli_uno C# bridgetest succeeded." );
1014 			return 0;
1015 		}
1016 		catch (unoidl.com.sun.star.uno.RuntimeException)
1017 		{
1018 			throw;
1019 		}
1020 		catch (System.Exception exc)
1021 		{
1022 			throw new unoidl.com.sun.star.uno.RuntimeException(
1023 				"cli_cs_bridgetest.cs: unexpected exception occurred in XMain::run. Original exception: " +
1024 				exc.GetType().Name + "\n Message: " + exc.Message , null);
1025 		}
1026     }
1027 }
1028 
1029 }
1030