[corlib] Fixed StringBuilder construction bugs in marshalling caused by changes to...
[mono.git] / mcs / class / System.Runtime.Serialization.Formatters.Soap / Test / SerializationTest.cs
1 //\r
2 // System.Runtime.Serialization.SerializationTest.cs\r
3 //\r
4 // Author: Lluis Sanchez Gual  (lluis@ximian.com)\r
5 //\r
6 // (C) Ximian, Inc.\r
7 //\r
8 \r
9 using System;\r
10 using System.Diagnostics;\r
11 using System.IO;\r
12 using System.Runtime.Serialization;\r
13 using System.Runtime.Serialization.Formatters.Soap;\r
14 using System.Reflection;\r
15 using System.Runtime.Remoting;\r
16 using System.Runtime.Remoting.Channels;\r
17 using System.Runtime.Remoting.Proxies;\r
18 using System.Runtime.Remoting.Messaging;\r
19 using System.Collections;\r
20 using NUnit.Framework;\r
21 \r
22 namespace MonoTests.System.Runtime.Serialization.Formatters.Soap\r
23 {\r
24         [TestFixture]\r
25         public class SerializationTest\r
26         {\r
27                 MemoryStream ms;\r
28 \r
29                 [Test]\r
30                 public void TestSerialization ()\r
31                 {\r
32                         MethodTester mt = new MethodTester();\r
33                         RemotingServices.Marshal (mt, "myuri");\r
34 \r
35                         WriteData();\r
36                         ReadData();\r
37 \r
38                         RemotingServices.Disconnect (mt);\r
39                 }\r
40 \r
41                 public static void Main() \r
42                 {\r
43                         SerializationTest test = new SerializationTest();\r
44                         test.TestSerialization();\r
45                 }\r
46 \r
47                 void WriteData ()\r
48                 {\r
49                         StreamingContext context = new StreamingContext (StreamingContextStates.Other);\r
50                         SurrogateSelector sel = new SurrogateSelector();\r
51                         sel.AddSurrogate (typeof (Point), context, new PointSurrogate());\r
52 \r
53                         List list = CreateTestData();\r
54                         BinderTester_A bta = CreateBinderTestData();\r
55 \r
56                         ms = new MemoryStream();\r
57                         SoapFormatter f = new SoapFormatter (sel, new StreamingContext(StreamingContextStates.Other));\r
58                         f.Serialize (ms, list);\r
59 //                      ProcessMessages (ms, null);\r
60 //                      f.Serialize (ms, bta);\r
61                         ms.Flush ();\r
62                         ms.Position = 0;\r
63                         StreamReader reader = new StreamReader(ms);\r
64                         Console.WriteLine(reader.ReadToEnd());\r
65                         ms.Position = 0;\r
66                 }\r
67 \r
68                 void ReadData()\r
69                 {\r
70                         StreamingContext context = new StreamingContext (StreamingContextStates.Other);\r
71                         SurrogateSelector sel = new SurrogateSelector();\r
72                         sel.AddSurrogate (typeof (Point), context, new PointSurrogate());\r
73 \r
74                         SoapFormatter f = new SoapFormatter (sel, context);\r
75 \r
76                         object list = f.Deserialize (ms);\r
77 \r
78                         object[][] originalMsgData = null;\r
79                         IMessage[] calls = null;\r
80                         IMessage[] resps = null;\r
81 \r
82 //                      originalMsgData = ProcessMessages (null, null);\r
83 \r
84 //                      calls = new IMessage[originalMsgData.Length];\r
85 //                      resps = new IMessage[originalMsgData.Length];\r
86 \r
87 \r
88 //                      for (int n=0; n<originalMsgData.Length; n++)\r
89 //                      {\r
90 //                              calls[n] = (IMessage) f.Deserialize (ms);\r
91 //                              resps[n] = (IMessage) f.DeserializeMethodResponse (ms, null, (IMethodCallMessage)calls[n]);\r
92 //                      }\r
93 //\r
94 //                      f.Binder = new TestBinder ();\r
95 //                      object btbob = f.Deserialize (ms);\r
96 \r
97                         ms.Close();\r
98 \r
99                         ((List)list).CheckEquals(CreateTestData());\r
100 //\r
101 //                      BinderTester_A bta = CreateBinderTestData();\r
102 //                      Assertion.AssertEquals ("BinderTest.class", btbob.GetType(), typeof (BinderTester_B));\r
103 //                      BinderTester_B btb = btbob as BinderTester_B;\r
104 //                      if (btb != null)\r
105 //                      {\r
106 //                              Assertion.AssertEquals ("BinderTest.x", btb.x, bta.x);\r
107 //                              Assertion.AssertEquals ("BinderTest.y", btb.y, bta.y);\r
108 //                      }\r
109 //                      \r
110 //                      CheckMessages ("MethodCall", originalMsgData, ProcessMessages (null, calls));\r
111 //                      CheckMessages ("MethodResponse", originalMsgData, ProcessMessages (null, resps));\r
112                 }\r
113 \r
114                 BinderTester_A CreateBinderTestData ()\r
115                 {\r
116                         BinderTester_A bta = new BinderTester_A();\r
117                         bta.x = 11;\r
118                         bta.y = "binder tester";\r
119                         return bta;\r
120                 }\r
121 \r
122                 List CreateTestData()\r
123                 {\r
124                         List list = new List();\r
125                         list.name = "my list";\r
126                         list.values = new SomeValues();\r
127                         list.values.Init();\r
128 \r
129                         ListItem item1 = new ListItem();\r
130                         ListItem item2 = new ListItem();\r
131                         ListItem item3 = new ListItem();\r
132 \r
133                         item1.label = "value label 1";\r
134                         item1.next = item2;\r
135                         item1.value.color = 111;\r
136                         item1.value.point = new Point();\r
137                         item1.value.point.x = 11;\r
138                         item1.value.point.y = 22;\r
139 \r
140                         item2.label = "value label 2";\r
141                         item2.next = item3;\r
142                         item2.value.color = 222;\r
143 \r
144                         item2.value.point = new Point();\r
145                         item2.value.point.x = 33;\r
146                         item2.value.point.y = 44;\r
147 \r
148                         item3.label = "value label 3";\r
149                         item3.value.color = 333;\r
150                         item3.value.point = new Point();\r
151                         item3.value.point.x = 55;\r
152                         item3.value.point.y = 66;\r
153 \r
154                         list.children = new ListItem[3];\r
155 \r
156                         list.children[0] = item1;\r
157                         list.children[1] = item2;\r
158                         list.children[2] = item3;\r
159 \r
160                         return list;\r
161                 }\r
162 \r
163 \r
164                 object[][] ProcessMessages (Stream stream, IMessage[] messages)\r
165                 {\r
166                         object[][] results = new object[9][];\r
167 \r
168                         AuxProxy prx = new AuxProxy (stream, "myuri");\r
169                         MethodTester mt = (MethodTester)prx.GetTransparentProxy();\r
170                         object res;\r
171 \r
172                         if (messages != null) prx.SetTestMessage (messages[0]);\r
173                         res = mt.OverloadedMethod();\r
174                         results[0] = new object[] {res};\r
175 \r
176                         if (messages != null) prx.SetTestMessage (messages[1]);\r
177                         res = mt.OverloadedMethod(22);\r
178                         results[1] = new object[] {res};\r
179 \r
180                         if (messages != null) prx.SetTestMessage (messages[2]);\r
181                         int[] par1 = new int[] {1,2,3};\r
182                         res = mt.OverloadedMethod(par1);\r
183                         results[2] = new object[] { res, par1 };\r
184 \r
185                         if (messages != null) prx.SetTestMessage (messages[3]);\r
186                         mt.NoReturn();\r
187 \r
188                         if (messages != null) prx.SetTestMessage (messages[4]);\r
189                         res = mt.Simple ("hello",44);\r
190                         results[4] = new object[] { res };\r
191 \r
192                         if (messages != null) prx.SetTestMessage (messages[5]);\r
193                         res = mt.Simple2 ('F');\r
194                         results[5] = new object[] { res };\r
195 \r
196                         if (messages != null) prx.SetTestMessage (messages[6]);\r
197                         char[] par2 = new char[] { 'G' };\r
198                         res = mt.Simple3 (par2);\r
199                         results[6] = new object[] { res, par2 };\r
200 \r
201                         if (messages != null) prx.SetTestMessage (messages[7]);\r
202                         res = mt.Simple3 (null);\r
203                         results[7] = new object[] { res };\r
204 \r
205                         if (messages != null) prx.SetTestMessage (messages[8]);\r
206 \r
207                         SimpleClass b = new SimpleClass ('H');\r
208                         res = mt.SomeMethod (123456, b);\r
209                         results[8] = new object[] { res, b };\r
210 \r
211                         return results;\r
212                 }\r
213 \r
214                 void CheckMessages (string label, object[][] original, object[][] serialized)\r
215                 {\r
216                         for (int n=0; n<original.Length; n++)\r
217                                 EqualsArray (label + " " + n, original[n], serialized[n]);\r
218                 }\r
219 \r
220                 public static void AssertEquals(string message, Object expected, Object actual)\r
221                 {\r
222                         if (expected != null && expected.GetType().IsArray)\r
223                                 EqualsArray (message, (Array)expected, (Array)actual);\r
224                         else\r
225                                 Assertion.AssertEquals (message, expected, actual);\r
226                 }\r
227 \r
228                 public static void EqualsArray (string message, object oar1, object oar2)\r
229                 {\r
230                         if (oar1 == null || oar2 == null || !(oar1 is Array) || !(oar2 is Array))\r
231                         {\r
232                                 SerializationTest.AssertEquals (message, oar1, oar2);\r
233                                 return;\r
234                         }\r
235 \r
236                         Array ar1 = (Array) oar1;\r
237                         Array ar2 = (Array) oar2;\r
238 \r
239                         SerializationTest.AssertEquals(message + ".Length", ar1.Length,ar2.Length);\r
240 \r
241                         for (int n=0; n<ar1.Length; n++)\r
242                         {\r
243                                 object av1 = ar1.GetValue(n);\r
244                                 object av2 = ar2.GetValue(n);\r
245                                 SerializationTest.AssertEquals (message + "[" + n + "]", av1, av2);\r
246                         }\r
247                 }\r
248         }\r
249 \r
250 \r
251 \r
252         class PointSurrogate: ISerializationSurrogate\r
253         {\r
254                 public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)\r
255                 {\r
256                         Point p = (Point)obj;\r
257                         info.AddValue ("xv",p.x);\r
258                         info.AddValue ("yv",p.y);\r
259                 }\r
260 \r
261                 public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)\r
262                 {\r
263                         typeof (Point).GetField ("x").SetValue (obj, info.GetInt32 ("xv"));\r
264                         typeof (Point).GetField ("y").SetValue (obj, info.GetInt32 ("yv"));\r
265                         return obj;\r
266                 }\r
267         }\r
268 \r
269         [Serializable]\r
270         public class List\r
271         {\r
272                 public string name = null;\r
273                 public ListItem[] children = null; \r
274                 public SomeValues values;\r
275 \r
276                 public void CheckEquals(List val)\r
277                 {\r
278                         SerializationTest.AssertEquals ("List.children.Length", children.Length, val.children.Length);\r
279 \r
280                         for (int n=0; n<children.Length; n++)\r
281                                 children[n].CheckEquals (val.children[n]);\r
282 \r
283                         SerializationTest.AssertEquals ("List.name", name, val.name);\r
284                         values.CheckEquals (val.values);\r
285                 }\r
286         }\r
287 \r
288         [Serializable]\r
289         public class ListItem: ISerializable\r
290         {\r
291                 public ListItem()\r
292                 {\r
293                 }\r
294 \r
295                 ListItem (SerializationInfo info, StreamingContext ctx)\r
296                 {\r
297                         next = (ListItem)info.GetValue ("next", typeof (ListItem));\r
298                         value = (ListValue)info.GetValue ("value", typeof (ListValue));\r
299                         label = info.GetString ("label");\r
300                 }\r
301 \r
302                 public void GetObjectData (SerializationInfo info, StreamingContext ctx)\r
303                 {\r
304                         info.AddValue ("next", next);\r
305                         info.AddValue ("value", value);\r
306                         info.AddValue ("label", label);\r
307                 }\r
308 \r
309                 public void CheckEquals(ListItem val)\r
310                 {\r
311                         SerializationTest.AssertEquals ("ListItem.next", next, val.next);\r
312                         SerializationTest.AssertEquals ("ListItem.label", label, val.label);\r
313                         value.CheckEquals (val.value);\r
314                 }\r
315                 \r
316                 public override bool Equals(object obj)\r
317                 {\r
318                         ListItem val = (ListItem)obj;\r
319                         if ((next == null || val.next == null) && (next != val.next)) return false;\r
320                         if (next == null) return true;\r
321                         if (!next.Equals(val.next)) return false;\r
322                         return value.Equals (val.value) && label == val.label;\r
323                 }\r
324 \r
325                 public override int GetHashCode ()\r
326                 {\r
327                         return base.GetHashCode ();\r
328                 }\r
329 \r
330                 public ListItem next;\r
331                 public ListValue value;\r
332                 public string label;\r
333         }\r
334 \r
335         [Serializable]\r
336         public struct ListValue\r
337         {\r
338                 public int color;\r
339                 public Point point;\r
340                 \r
341                 public override bool Equals(object obj)\r
342                 {\r
343                         ListValue val = (ListValue)obj;\r
344                         return (color == val.color && point.Equals(val.point));\r
345                 }\r
346 \r
347                 public void CheckEquals(ListValue val)\r
348                 {\r
349                         SerializationTest.AssertEquals ("ListValue.color", color, val.color);\r
350                         point.CheckEquals (val.point);\r
351                 }\r
352 \r
353                 public override int GetHashCode ()\r
354                 {\r
355                         return base.GetHashCode ();\r
356                 }\r
357         }\r
358 \r
359 //      [Serializable]\r
360         public struct Point\r
361         {\r
362                 public int x;\r
363                 public int y;\r
364 \r
365                 public override bool Equals(object obj)\r
366                 {\r
367                         Point p = (Point)obj;\r
368                         return (x == p.x && y == p.y);\r
369                 }\r
370 \r
371                 public void CheckEquals(Point p)\r
372                 {\r
373                         SerializationTest.AssertEquals ("Point.x", x, p.x);\r
374                         SerializationTest.AssertEquals ("Point.y", y, p.y);\r
375                 }\r
376 \r
377                 public override int GetHashCode ()\r
378                 {\r
379                         return base.GetHashCode ();\r
380                 }\r
381         }\r
382 \r
383         [Serializable]\r
384         public class SimpleClass\r
385         {\r
386                 public SimpleClass (char v) { val = v; }\r
387 \r
388                 public override bool Equals(object obj)\r
389                 {\r
390                         if (obj == null) return false;\r
391                         return val == ((SimpleClass)obj).val;\r
392                 }\r
393 \r
394                 public override int GetHashCode()\r
395                 {\r
396                         return val.GetHashCode();\r
397                 }\r
398 \r
399                 public int SampleCall (string str, SomeValues sv, ref int acum)\r
400                 {\r
401                         acum += (int)val;\r
402                         return (int)val;\r
403                 }\r
404 \r
405                 public char val;\r
406         }\r
407 \r
408         enum IntEnum { aaa, bbb, ccc }\r
409         enum ByteEnum: byte { aaa=221, bbb=3, ccc=44 }\r
410 \r
411         delegate int SampleDelegate (string str, SomeValues sv, ref int acum);\r
412 \r
413         [Serializable]\r
414         public class SomeValues\r
415         {\r
416                 Type _type;\r
417                 Type _type2;\r
418                 DBNull _dbnull;\r
419                 Assembly _assembly;\r
420                 IntEnum _intEnum;\r
421                 ByteEnum _byteEnum;\r
422 \r
423                 bool _bool;\r
424                 bool _bool2;\r
425                 byte _byte;\r
426                 char _char;\r
427                 DateTime _dateTime;\r
428                 decimal _decimal;\r
429                 double _double;\r
430                 short _short;\r
431                 int _int;\r
432                 long _long;\r
433                 sbyte _sbyte;\r
434                 float _float;\r
435                 ushort _ushort;\r
436                 uint _uint;\r
437                 ulong _ulong;\r
438 \r
439                 object[] _objects;\r
440                 string[] _strings;\r
441                 int[] _ints;\r
442                 public int[,,] _intsMulti;\r
443                 int[][] _intsJagged;\r
444                 SimpleClass[] _simples;\r
445                 SimpleClass[,] _simplesMulti;\r
446                 SimpleClass[][] _simplesJagged;\r
447                 double[] _doubles;\r
448                 object[] _almostEmpty;\r
449 \r
450                 object[] _emptyObjectArray;\r
451                 Type[] _emptyTypeArray;\r
452                 SimpleClass[] _emptySimpleArray;\r
453                 int[] _emptyIntArray;\r
454                 string[] _emptyStringArray;\r
455 \r
456 \r
457                 SampleDelegate _sampleDelegate;\r
458                 SampleDelegate _sampleDelegate2;\r
459                 SampleDelegate _sampleDelegate3;\r
460                 SampleDelegate _sampleDelegateStatic;\r
461                 SampleDelegate _sampleDelegateCombined;\r
462 \r
463                 SimpleClass _shared1;\r
464                 SimpleClass _shared2;\r
465                 SimpleClass _shared3;\r
466 \r
467                 public void Init()\r
468                 {\r
469                         _type = typeof (string);\r
470                         _type2 = typeof (SomeValues);\r
471                         _dbnull = DBNull.Value;\r
472                         _assembly = typeof (SomeValues).Assembly;\r
473                         _intEnum = IntEnum.bbb;\r
474                         _byteEnum = ByteEnum.ccc;\r
475                         _bool = true;\r
476                         _bool2 = false;\r
477                         _byte = 254;\r
478                         _char = 'A';\r
479                         _dateTime = new DateTime (1972,7,13,1,20,59);\r
480                         _decimal = (decimal)101010.10101;\r
481                         _double = 123456.6789;\r
482                         _short = -19191;\r
483                         _int = -28282828;\r
484                         _long = 37373737373;\r
485                         _sbyte = -123;\r
486                         _float = (float)654321.321;\r
487                         _ushort = 61616;\r
488                         _uint = 464646464;\r
489                         _ulong = 55555555;\r
490 \r
491                         Point p = new Point();\r
492                         p.x = 56; p.y = 67;\r
493                         object boxedPoint = p;\r
494 \r
495                         long i = 22;\r
496                         object boxedLong = i;\r
497 \r
498                         _objects = new object[] { "string", (int)1234, null , /*boxedPoint, boxedPoint,*/ boxedLong, boxedLong};\r
499                         _strings = new string[] { "an", "array", "of", "strings","I","repeat","an", "array", "of", "strings" };\r
500                         _ints = new int[] { 4,5,6,7,8 };\r
501                         _intsMulti = new int[2,3,4] { { {1,2,3,4},{5,6,7,8},{9,10,11,12}}, { {13,14,15,16},{17,18,19,20},{21,22,23,24} } };\r
502                         _intsJagged = new int[2][] { new int[3] {1,2,3}, new int[2] {4,5} };\r
503                         _simples = new SimpleClass[] { new SimpleClass('a'),new SimpleClass('b'),new SimpleClass('c') };\r
504                         _simplesMulti = new SimpleClass[2,3] {{new SimpleClass('d'),new SimpleClass('e'),new SimpleClass('f')}, {new SimpleClass('g'),new SimpleClass('j'),new SimpleClass('h')}};\r
505                         _simplesJagged = new SimpleClass[2][] { new SimpleClass[1] { new SimpleClass('i') }, new SimpleClass[2] {null, new SimpleClass('k')}};\r
506                         _almostEmpty = new object[2000];\r
507                         _almostEmpty[1000] = 4;\r
508 \r
509                         _emptyObjectArray = new object[0];\r
510                         _emptyTypeArray = new Type[0];\r
511                         _emptySimpleArray = new SimpleClass[0];\r
512                         _emptyIntArray = new int[0];\r
513                         _emptyStringArray = new string[0];\r
514 \r
515                         // FIXME: Once double.ToString("G17") is implemented\r
516                         // we'll be able to serialize double.MaxValue and double.MinValue.\r
517                         // Currently, it throws a System.OverflowException.\r
518                         //_doubles = new double[] { 1010101.101010, 292929.29292, 3838383.38383, 4747474.474, 56565.5656565, 0, Double.NaN, Double.MaxValue, Double.MinValue, Double.NegativeInfinity, Double.PositiveInfinity };\r
519                         _doubles = new double[] { 1010101.101010, 292929.29292, 3838383.38383, 4747474.474, 56565.5656565, 0, Double.NaN, Double.NegativeInfinity, Double.PositiveInfinity };\r
520 \r
521                         _sampleDelegate = new SampleDelegate(SampleCall);\r
522                         _sampleDelegate2 = new SampleDelegate(_simples[0].SampleCall);\r
523                         _sampleDelegate3 = new SampleDelegate(new SimpleClass('x').SampleCall);\r
524                         _sampleDelegateStatic = new SampleDelegate(SampleStaticCall);\r
525                         _sampleDelegateCombined = (SampleDelegate)Delegate.Combine (new Delegate[] {_sampleDelegate, _sampleDelegate2, _sampleDelegate3, _sampleDelegateStatic });\r
526 \r
527                         // This is to test that references are correctly solved\r
528                         _shared1 = new SimpleClass('A');\r
529                         _shared2 = new SimpleClass('A');\r
530                         _shared3 = _shared1;\r
531                 }\r
532 \r
533                 public int SampleCall (string str, SomeValues sv, ref int acum)\r
534                 {\r
535                         acum += _int;\r
536                         return _int;\r
537                 }\r
538 \r
539                 public static int SampleStaticCall (string str, SomeValues sv, ref int acum)\r
540                 {\r
541                         acum += 99;\r
542                         return 99;\r
543                 }\r
544 \r
545                 public void CheckEquals(SomeValues obj)\r
546                 {\r
547                         SerializationTest.AssertEquals ("SomeValues._type", _type, obj._type);\r
548                         SerializationTest.AssertEquals ("SomeValues._type2", _type2, obj._type2);\r
549                         SerializationTest.AssertEquals ("SomeValues._dbnull", _dbnull, obj._dbnull);\r
550                         SerializationTest.AssertEquals ("SomeValues._assembly", _assembly, obj._assembly);\r
551 \r
552                         SerializationTest.AssertEquals ("SomeValues._intEnum", _intEnum, obj._intEnum);\r
553                         SerializationTest.AssertEquals ("SomeValues._byteEnum", _byteEnum, obj._byteEnum);\r
554                         SerializationTest.AssertEquals ("SomeValues._bool", _bool, obj._bool);\r
555                         SerializationTest.AssertEquals ("SomeValues._bool2", _bool2, obj._bool2);\r
556                         SerializationTest.AssertEquals ("SomeValues._byte", _byte, obj._byte);\r
557                         SerializationTest.AssertEquals ("SomeValues._char", _char, obj._char);\r
558                         SerializationTest.AssertEquals ("SomeValues._dateTime", _dateTime, obj._dateTime);\r
559                         SerializationTest.AssertEquals ("SomeValues._decimal", _decimal, obj._decimal);\r
560                         SerializationTest.AssertEquals ("SomeValues._int", _int, obj._int);\r
561                         SerializationTest.AssertEquals ("SomeValues._long", _long, obj._long);\r
562                         SerializationTest.AssertEquals ("SomeValues._sbyte", _sbyte, obj._sbyte);\r
563                         SerializationTest.AssertEquals ("SomeValues._float", _float, obj._float);\r
564                         SerializationTest.AssertEquals ("SomeValues._ushort", _ushort, obj._ushort);\r
565                         SerializationTest.AssertEquals ("SomeValues._uint", _uint, obj._uint);\r
566                         SerializationTest.AssertEquals ("SomeValues._ulong", _ulong, obj._ulong);\r
567 \r
568                         SerializationTest.EqualsArray ("SomeValues._objects", _objects, obj._objects);\r
569                         SerializationTest.EqualsArray ("SomeValues._strings", _strings, obj._strings);\r
570                         SerializationTest.EqualsArray ("SomeValues._doubles", _doubles, obj._doubles);\r
571                         SerializationTest.EqualsArray ("SomeValues._ints", _ints, obj._ints);\r
572                         SerializationTest.EqualsArray ("SomeValues._simples", _simples, obj._simples);\r
573                         SerializationTest.EqualsArray ("SomeValues._almostEmpty", _almostEmpty, obj._almostEmpty);\r
574 \r
575                         SerializationTest.EqualsArray ("SomeValues._emptyObjectArray", _emptyObjectArray, obj._emptyObjectArray);\r
576                         SerializationTest.EqualsArray ("SomeValues._emptyTypeArray", _emptyTypeArray, obj._emptyTypeArray);\r
577                         SerializationTest.EqualsArray ("SomeValues._emptySimpleArray", _emptySimpleArray, obj._emptySimpleArray);\r
578                         SerializationTest.EqualsArray ("SomeValues._emptyIntArray", _emptyIntArray, obj._emptyIntArray);\r
579                         SerializationTest.EqualsArray ("SomeValues._emptyStringArray", _emptyStringArray, obj._emptyStringArray);\r
580 \r
581                         for (int i=0; i<2; i++)\r
582                                 for (int j=0; j<3; j++)\r
583                                         for (int k=0; k<4; k++)\r
584                                                 SerializationTest.AssertEquals("SomeValues._intsMulti[" + i + "," + j + "," + k + "]", _intsMulti[i,j,k], obj._intsMulti[i,j,k]);\r
585 \r
586                         for (int i=0; i<_intsJagged.Length; i++)\r
587                                 for (int j=0; j<_intsJagged[i].Length; j++)\r
588                                         SerializationTest.AssertEquals ("SomeValues._intsJagged[" + i + "][" + j + "]", _intsJagged[i][j], obj._intsJagged[i][j]);\r
589 \r
590                         for (int i=0; i<2; i++)\r
591                                 for (int j=0; j<3; j++)\r
592                                         SerializationTest.AssertEquals ("SomeValues._simplesMulti[" + i + "," + j + "]", _simplesMulti[i,j], obj._simplesMulti[i,j]);\r
593 \r
594                         for (int i=0; i<_simplesJagged.Length; i++)\r
595                                 SerializationTest.EqualsArray ("SomeValues._simplesJagged", _simplesJagged[i], obj._simplesJagged[i]);\r
596 \r
597                         int acum = 0;\r
598                         SerializationTest.AssertEquals ("SomeValues._sampleDelegate", _sampleDelegate ("hi", this, ref acum), _int);\r
599                         SerializationTest.AssertEquals ("SomeValues._sampleDelegate_bis", _sampleDelegate ("hi", this, ref acum), obj._sampleDelegate ("hi", this, ref acum));\r
600 \r
601                         SerializationTest.AssertEquals ("SomeValues._sampleDelegate2", _sampleDelegate2 ("hi", this, ref acum), (int)_simples[0].val);\r
602                         SerializationTest.AssertEquals ("SomeValues._sampleDelegate2_bis", _sampleDelegate2 ("hi", this, ref acum), obj._sampleDelegate2 ("hi", this, ref acum));\r
603 \r
604                         SerializationTest.AssertEquals ("SomeValues._sampleDelegate3", _sampleDelegate3 ("hi", this, ref acum), (int)'x');\r
605                         SerializationTest.AssertEquals ("SomeValues._sampleDelegate3_bis", _sampleDelegate3 ("hi", this, ref acum), obj._sampleDelegate3 ("hi", this, ref acum));\r
606 \r
607                         SerializationTest.AssertEquals ("SomeValues._sampleDelegateStatic", _sampleDelegateStatic ("hi", this, ref acum), 99);\r
608                         SerializationTest.AssertEquals ("SomeValues._sampleDelegateStatic_bis", _sampleDelegateStatic ("hi", this, ref acum), obj._sampleDelegateStatic ("hi", this, ref acum));\r
609 \r
610                         int acum1 = 0;\r
611                         int acum2 = 0;\r
612                         _sampleDelegateCombined ("hi", this, ref acum1);\r
613                         obj._sampleDelegateCombined ("hi", this, ref acum2);\r
614 \r
615                         SerializationTest.AssertEquals ("_sampleDelegateCombined", acum1, _int + (int)_simples[0].val + (int)'x' + 99);\r
616                         SerializationTest.AssertEquals ("_sampleDelegateCombined_bis", acum1, acum2);\r
617 \r
618                         SerializationTest.AssertEquals ("SomeValues._shared1", _shared1, _shared2);\r
619                         SerializationTest.AssertEquals ("SomeValues._shared1_bis", _shared1, _shared3);\r
620 \r
621                         _shared1.val = 'B';\r
622                         SerializationTest.AssertEquals ("SomeValues._shared2", _shared2.val, 'A');\r
623                         SerializationTest.AssertEquals ("SomeValues._shared3", _shared3.val, 'B');\r
624                 }\r
625         }\r
626 \r
627         class MethodTester : MarshalByRefObject\r
628         {\r
629                 public int OverloadedMethod ()\r
630                 {\r
631                         return 123456789;\r
632                 }\r
633 \r
634                 public int OverloadedMethod (int a)\r
635                 {\r
636                         return a+2;\r
637                 }\r
638 \r
639                 public int OverloadedMethod (int[] a)\r
640                 {\r
641                         return a.Length;\r
642                 }\r
643 \r
644                 public void NoReturn ()\r
645                 {}\r
646 \r
647                 public string Simple (string a, int b)\r
648                 {\r
649                         return a + b;\r
650                 }\r
651 \r
652                 public SimpleClass Simple2 (char c)\r
653                 {\r
654                         return new SimpleClass(c);\r
655                 }\r
656 \r
657                 public SimpleClass Simple3 (char[] c)\r
658                 {\r
659                         if (c != null) return new SimpleClass(c[0]);\r
660                         else return null;\r
661                 }\r
662 \r
663                 public int SomeMethod (int a, SimpleClass b)\r
664                 {\r
665                         object[] d;\r
666                         string c = "hi";\r
667                         int r = a + c.Length;\r
668                         c = "bye";\r
669                         d = new object[3];\r
670                         d[1] = b;\r
671                         return r;\r
672                 }\r
673         }\r
674 \r
675         class AuxProxy: RealProxy\r
676         {\r
677                 public static bool useHeaders = false;\r
678                 Stream _stream;\r
679                 string _uri;\r
680                 IMethodMessage _testMsg;\r
681 \r
682                 public AuxProxy(Stream stream, string uri): base(typeof(MethodTester))\r
683                 {\r
684                         _stream = stream;\r
685                         _uri = uri;\r
686                 }\r
687 \r
688                 public void SetTestMessage (IMessage msg)\r
689                 {\r
690                         _testMsg = (IMethodMessage)msg;\r
691                         _testMsg.Properties["__Uri"] = _uri;\r
692                 }\r
693 \r
694                 public override IMessage Invoke(IMessage msg)\r
695                 {\r
696                         IMethodCallMessage call = (IMethodCallMessage)msg;\r
697                         if (call.MethodName.StartsWith ("Initialize")) return new ReturnMessage(null,null,0,null,(IMethodCallMessage)msg);\r
698 \r
699                         call.Properties["__Uri"] = _uri;\r
700 \r
701                         if (_stream != null)\r
702                         {\r
703                                 SerializeCall (call);\r
704                                 IMessage response = ChannelServices.SyncDispatchMessage (call);\r
705                                 SerializeResponse (response);\r
706                                 return response;\r
707                         }\r
708                         else if (_testMsg != null)\r
709                         {\r
710                                 if (_testMsg is IMethodCallMessage)\r
711                                         return ChannelServices.SyncDispatchMessage (_testMsg);\r
712                                 else\r
713                                         return _testMsg;\r
714                         }\r
715                         else\r
716                                 return ChannelServices.SyncDispatchMessage (call);\r
717                 }\r
718 \r
719                 void SerializeCall (IMessage call)\r
720                 {\r
721                         RemotingSurrogateSelector rss = new RemotingSurrogateSelector();\r
722                         IRemotingFormatter fmt = new SoapFormatter (rss, new StreamingContext(StreamingContextStates.Remoting));\r
723                         fmt.Serialize (_stream, call, GetHeaders());\r
724                 }\r
725 \r
726                 void SerializeResponse (IMessage resp)\r
727                 {\r
728                         RemotingSurrogateSelector rss = new RemotingSurrogateSelector();\r
729                         IRemotingFormatter fmt = new SoapFormatter (rss, new StreamingContext(StreamingContextStates.Remoting));\r
730                         fmt.Serialize (_stream, resp, GetHeaders());\r
731                 }\r
732 \r
733                 Header[] GetHeaders()\r
734                 {\r
735                         Header[] hs = null;\r
736                         if (useHeaders)\r
737                         {\r
738                                 hs = new Header[1];\r
739                                 hs[0] = new Header("unom",new SimpleClass('R'));\r
740                         }\r
741                         return hs;\r
742                 }\r
743         }\r
744 \r
745         public class TestBinder : SerializationBinder\r
746         {\r
747                 public override Type BindToType (string assemblyName, string typeName)\r
748                 {\r
749                         if (typeName.IndexOf("BinderTester_A") != -1)\r
750                                 typeName = typeName.Replace ("BinderTester_A", "BinderTester_B");\r
751 \r
752                         return Assembly.Load (assemblyName).GetType (typeName);\r
753                 }\r
754         }\r
755 \r
756         [Serializable]\r
757         public class BinderTester_A\r
758         {\r
759                 public int x;\r
760                 public string y;\r
761         }\r
762 \r
763         [Serializable]\r
764         public class BinderTester_B\r
765         {\r
766                 public string y;\r
767                 public int x;\r
768         }\r
769 \r
770 \r
771 }\r