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