-//\r
-// System.Runtime.Serialization.SerializationTest.cs\r
-//\r
-// Author: Lluis Sanchez Gual (lluis@ximian.com)\r
-//\r
-// (C) Ximian, Inc.\r
-//\r
-\r
-using System;\r
-using System.Diagnostics;\r
-using System.IO;\r
-using System.Runtime.Serialization;\r
-using System.Runtime.Serialization.Formatters.Binary;\r
-using System.Reflection;\r
-using System.Runtime.Remoting;\r
-using System.Runtime.Remoting.Channels;\r
-using System.Runtime.Remoting.Proxies;\r
-using System.Runtime.Remoting.Messaging;\r
-using System.Collections;\r
-using NUnit.Framework;\r
-\r
-namespace MonoTests.System.Runtime.Serialization\r
-{\r
- [TestFixture]\r
- public class SerializationTest\r
- {\r
- MemoryStream ms;\r
- string uri;\r
-\r
- [Test]\r
- public void TestSerialization ()\r
- {\r
- MethodTester mt = new MethodTester();\r
- RemotingServices.Marshal (mt);\r
- uri = RemotingServices.GetObjectUri (mt);\r
-\r
- WriteData();\r
- ReadData();\r
-\r
- RemotingServices.Disconnect (mt);\r
- }\r
-\r
- void WriteData ()\r
- {\r
- StreamingContext context = new StreamingContext (StreamingContextStates.Other);\r
- SurrogateSelector sel = new SurrogateSelector();\r
- sel.AddSurrogate (typeof (Point), context, new PointSurrogate());\r
- sel.AddSurrogate (typeof (FalseISerializable), context, new FalseISerializableSurrogate());\r
-\r
- List list = CreateTestData();\r
- BinderTester_A bta = CreateBinderTestData();\r
-\r
- ms = new MemoryStream();\r
- BinaryFormatter f = new BinaryFormatter (sel, new StreamingContext(StreamingContextStates.Other));\r
- f.Serialize (ms, list);\r
- ProcessMessages (ms, null);\r
- f.Serialize (ms, bta);\r
- ms.Flush ();\r
- ms.Position = 0;\r
- }\r
-\r
- void ReadData()\r
- {\r
- StreamingContext context = new StreamingContext (StreamingContextStates.Other);\r
- SurrogateSelector sel = new SurrogateSelector();\r
- sel.AddSurrogate (typeof (Point), context, new PointSurrogate());\r
- sel.AddSurrogate (typeof (FalseISerializable), context, new FalseISerializableSurrogate());\r
-\r
- BinaryFormatter f = new BinaryFormatter (sel, context);\r
-\r
- object list = f.Deserialize (ms);\r
-\r
- object[][] originalMsgData = null;\r
- IMessage[] calls = null;\r
- IMessage[] resps = null;\r
-\r
- originalMsgData = ProcessMessages (null, null);\r
-\r
- calls = new IMessage[originalMsgData.Length];\r
- resps = new IMessage[originalMsgData.Length];\r
-\r
-\r
- for (int n=0; n<originalMsgData.Length; n++)\r
- {\r
- calls[n] = (IMessage) f.Deserialize (ms);\r
- resps[n] = (IMessage) f.DeserializeMethodResponse (ms, null, (IMethodCallMessage)calls[n]);\r
- }\r
-\r
- f.Binder = new TestBinder ();\r
- object btbob = f.Deserialize (ms);\r
-\r
- ms.Close();\r
-\r
- ((List)list).CheckEquals(CreateTestData());\r
-\r
- BinderTester_A bta = CreateBinderTestData();\r
- Assertion.AssertEquals ("BinderTest.class", btbob.GetType(), typeof (BinderTester_B));\r
- BinderTester_B btb = btbob as BinderTester_B;\r
- if (btb != null)\r
- {\r
- Assertion.AssertEquals ("BinderTest.x", btb.x, bta.x);\r
- Assertion.AssertEquals ("BinderTest.y", btb.y, bta.y);\r
- }\r
- \r
- CheckMessages ("MethodCall", originalMsgData, ProcessMessages (null, calls));\r
- CheckMessages ("MethodResponse", originalMsgData, ProcessMessages (null, resps));\r
- }\r
-\r
- BinderTester_A CreateBinderTestData ()\r
- {\r
- BinderTester_A bta = new BinderTester_A();\r
- bta.x = 11;\r
- bta.y = "binder tester";\r
- return bta;\r
- }\r
-\r
- List CreateTestData()\r
- {\r
- List list = new List();\r
- list.name = "my list";\r
- list.values = new SomeValues();\r
- list.values.Init();\r
-\r
- ListItem item1 = new ListItem();\r
- ListItem item2 = new ListItem();\r
- ListItem item3 = new ListItem();\r
-\r
- item1.label = "value label 1";\r
- item1.next = item2;\r
- item1.value.color = 111;\r
- item1.value.point = new Point();\r
- item1.value.point.x = 11;\r
- item1.value.point.y = 22;\r
-\r
- item2.label = "value label 2";\r
- item2.next = item3;\r
- item2.value.color = 222;\r
-\r
- item2.value.point = new Point();\r
- item2.value.point.x = 33;\r
- item2.value.point.y = 44;\r
-\r
- item3.label = "value label 3";\r
- item3.value.color = 333;\r
- item3.value.point = new Point();\r
- item3.value.point.x = 55;\r
- item3.value.point.y = 66;\r
-\r
- list.children = new ListItem[3];\r
-\r
- list.children[0] = item1;\r
- list.children[1] = item2;\r
- list.children[2] = item3;\r
-\r
- return list;\r
- }\r
-\r
-\r
- object[][] ProcessMessages (Stream stream, IMessage[] messages)\r
- {\r
- object[][] results = new object[9][];\r
-\r
- AuxProxy prx = new AuxProxy (stream, uri);\r
- MethodTester mt = (MethodTester)prx.GetTransparentProxy();\r
- object res;\r
-\r
- if (messages != null) prx.SetTestMessage (messages[0]);\r
- res = mt.OverloadedMethod();\r
- results[0] = new object[] {res};\r
-\r
- if (messages != null) prx.SetTestMessage (messages[1]);\r
- res = mt.OverloadedMethod(22);\r
- results[1] = new object[] {res};\r
-\r
- if (messages != null) prx.SetTestMessage (messages[2]);\r
- int[] par1 = new int[] {1,2,3};\r
- res = mt.OverloadedMethod(par1);\r
- results[2] = new object[] { res, par1 };\r
-\r
- if (messages != null) prx.SetTestMessage (messages[3]);\r
- mt.NoReturn();\r
-\r
- if (messages != null) prx.SetTestMessage (messages[4]);\r
- res = mt.Simple ("hello",44);\r
- results[4] = new object[] { res };\r
-\r
- if (messages != null) prx.SetTestMessage (messages[5]);\r
- res = mt.Simple2 ('F');\r
- results[5] = new object[] { res };\r
-\r
- if (messages != null) prx.SetTestMessage (messages[6]);\r
- char[] par2 = new char[] { 'G' };\r
- res = mt.Simple3 (par2);\r
- results[6] = new object[] { res, par2 };\r
-\r
- if (messages != null) prx.SetTestMessage (messages[7]);\r
- res = mt.Simple3 (null);\r
- results[7] = new object[] { res };\r
-\r
- if (messages != null) prx.SetTestMessage (messages[8]);\r
-\r
- SimpleClass b = new SimpleClass ('H');\r
- res = mt.SomeMethod (123456, b);\r
- results[8] = new object[] { res, b };\r
-\r
- return results;\r
- }\r
-\r
- void CheckMessages (string label, object[][] original, object[][] serialized)\r
- {\r
- for (int n=0; n<original.Length; n++)\r
- EqualsArray (label + " " + n, original[n], serialized[n]);\r
- }\r
-\r
- public static void AssertEquals(string message, Object expected, Object actual)\r
- {\r
- if (expected != null && expected.GetType().IsArray)\r
- EqualsArray (message, (Array)expected, (Array)actual);\r
- else\r
- Assertion.AssertEquals (message, expected, actual);\r
- }\r
-\r
- public static void EqualsArray (string message, object oar1, object oar2)\r
- {\r
- if (oar1 == null || oar2 == null || !(oar1 is Array) || !(oar2 is Array))\r
- {\r
- SerializationTest.AssertEquals (message, oar1, oar2);\r
- return;\r
- }\r
-\r
- Array ar1 = (Array) oar1;\r
- Array ar2 = (Array) oar2;\r
-\r
- SerializationTest.AssertEquals(message + ".Length", ar1.Length,ar2.Length);\r
-\r
- for (int n=0; n<ar1.Length; n++)\r
- {\r
- object av1 = ar1.GetValue(n);\r
- object av2 = ar2.GetValue(n);\r
- SerializationTest.AssertEquals (message + "[" + n + "]", av1, av2);\r
- }\r
- }\r
- }\r
-\r
-\r
-\r
- class PointSurrogate: ISerializationSurrogate\r
- {\r
- public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)\r
- {\r
- Point p = (Point)obj;\r
- info.AddValue ("xv",p.x);\r
- info.AddValue ("yv",p.y);\r
- }\r
-\r
- public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)\r
- {\r
- typeof (Point).GetField ("x").SetValue (obj, info.GetInt32 ("xv"));\r
- typeof (Point).GetField ("y").SetValue (obj, info.GetInt32 ("yv"));\r
- return obj;\r
- }\r
- }\r
-\r
- [Serializable]\r
- public class List\r
- {\r
- public string name = null;\r
- public ListItem[] children = null; \r
- public SomeValues values;\r
-\r
- public void CheckEquals(List val)\r
- {\r
- SerializationTest.AssertEquals ("List.children.Length", children.Length, val.children.Length);\r
-\r
- for (int n=0; n<children.Length; n++)\r
- children[n].CheckEquals (val.children[n]);\r
-\r
- SerializationTest.AssertEquals ("List.name", name, val.name);\r
- values.CheckEquals (val.values);\r
- }\r
- }\r
-\r
- [Serializable]\r
- public class ListItem: ISerializable\r
- {\r
- public ListItem()\r
- {\r
- }\r
-\r
- ListItem (SerializationInfo info, StreamingContext ctx)\r
- {\r
- next = (ListItem)info.GetValue ("next", typeof (ListItem));\r
- value = (ListValue)info.GetValue ("value", typeof (ListValue));\r
- label = info.GetString ("label");\r
- }\r
-\r
- public void GetObjectData (SerializationInfo info, StreamingContext ctx)\r
- {\r
- info.AddValue ("next", next);\r
- info.AddValue ("value", value);\r
- info.AddValue ("label", label);\r
- }\r
-\r
- public void CheckEquals(ListItem val)\r
- {\r
- SerializationTest.AssertEquals ("ListItem.next", next, val.next);\r
- SerializationTest.AssertEquals ("ListItem.label", label, val.label);\r
- value.CheckEquals (val.value);\r
- }\r
- \r
- public override bool Equals(object obj)\r
- {\r
- ListItem val = (ListItem)obj;\r
- if ((next == null || val.next == null) && (next != val.next)) return false;\r
- if (next == null) return true;\r
- if (!next.Equals(val.next)) return false;\r
- return value.Equals (val.value) && label == val.label;\r
- }\r
-\r
- public override int GetHashCode ()\r
- {\r
- return base.GetHashCode ();\r
- }\r
-\r
- public ListItem next;\r
- public ListValue value;\r
- public string label;\r
- }\r
-\r
- [Serializable]\r
- public struct ListValue\r
- {\r
- public int color;\r
- public Point point;\r
- \r
- public override bool Equals(object obj)\r
- {\r
- ListValue val = (ListValue)obj;\r
- return (color == val.color && point.Equals(val.point));\r
- }\r
-\r
- public void CheckEquals(ListValue val)\r
- {\r
- SerializationTest.AssertEquals ("ListValue.color", color, val.color);\r
- point.CheckEquals (val.point);\r
- }\r
-\r
- public override int GetHashCode ()\r
- {\r
- return base.GetHashCode ();\r
- }\r
- }\r
-\r
- public struct Point\r
- {\r
- public int x;\r
- public int y;\r
-\r
- public override bool Equals(object obj)\r
- {\r
- Point p = (Point)obj;\r
- return (x == p.x && y == p.y);\r
- }\r
-\r
- public void CheckEquals(Point p)\r
- {\r
- SerializationTest.AssertEquals ("Point.x", x, p.x);\r
- SerializationTest.AssertEquals ("Point.y", y, p.y);\r
- }\r
-\r
- public override int GetHashCode ()\r
- {\r
- return base.GetHashCode ();\r
- }\r
- }\r
-\r
- [Serializable]\r
- public class FalseISerializable : ISerializable\r
- {\r
- public int field;\r
- \r
- public FalseISerializable (int n)\r
- {\r
- field = n;\r
- }\r
- \r
- public void GetObjectData(SerializationInfo info, StreamingContext context)\r
- {\r
- throw new InvalidOperationException ("Serialize:We should not pass here.");\r
- }\r
- \r
- public FalseISerializable (SerializationInfo info, StreamingContext context)\r
- {\r
- throw new InvalidOperationException ("Deserialize:We should not pass here.");\r
- }\r
- }\r
- \r
- public class FalseISerializableSurrogate : ISerializationSurrogate\r
- {\r
- public void GetObjectData (object obj, SerializationInfo info, StreamingContext context)\r
- {\r
- info.AddValue("field", Convert.ToString (((FalseISerializable)obj).field));\r
- }\r
- \r
- public object SetObjectData (object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)\r
- {\r
- ((FalseISerializable)obj).field = Convert.ToInt32 (info.GetValue("field", typeof(string)));\r
- return obj;\r
- }\r
- }\r
-\r
- [Serializable]\r
- public class SimpleClass\r
- {\r
- public SimpleClass (char v) { val = v; }\r
-\r
- public override bool Equals(object obj)\r
- {\r
- if (obj == null) return false;\r
- return val == ((SimpleClass)obj).val;\r
- }\r
-\r
- public override int GetHashCode()\r
- {\r
- return val.GetHashCode();\r
- }\r
-\r
- public int SampleCall (string str, SomeValues sv, ref int acum)\r
- {\r
- acum += (int)val;\r
- return (int)val;\r
- }\r
-\r
- public char val;\r
- }\r
-\r
- enum IntEnum { aaa, bbb, ccc }\r
- enum ByteEnum: byte { aaa=221, bbb=3, ccc=44 }\r
-\r
- delegate int SampleDelegate (string str, SomeValues sv, ref int acum);\r
-\r
- [Serializable]\r
- public class SomeValues\r
- {\r
- Type _type;\r
- Type _type2;\r
- DBNull _dbnull;\r
- Assembly _assembly;\r
- IntEnum _intEnum;\r
- ByteEnum _byteEnum;\r
-\r
- bool _bool;\r
- bool _bool2;\r
- byte _byte;\r
- char _char;\r
- DateTime _dateTime;\r
- decimal _decimal;\r
- double _double;\r
- short _short;\r
- int _int;\r
- long _long;\r
- sbyte _sbyte;\r
- float _float;\r
- ushort _ushort;\r
- uint _uint;\r
- ulong _ulong;\r
-\r
- object[] _objects;\r
- string[] _strings;\r
- int[] _ints;\r
- public int[,,] _intsMulti;\r
- int[][] _intsJagged;\r
- SimpleClass[] _simples;\r
- SimpleClass[,] _simplesMulti;\r
- SimpleClass[][] _simplesJagged;\r
- double[] _doubles;\r
- object[] _almostEmpty;\r
-\r
- object[] _emptyObjectArray;\r
- Type[] _emptyTypeArray;\r
- SimpleClass[] _emptySimpleArray;\r
- int[] _emptyIntArray;\r
- string[] _emptyStringArray;\r
- Point[] _emptyPointArray;\r
-\r
-\r
- SampleDelegate _sampleDelegate;\r
- SampleDelegate _sampleDelegate2;\r
- SampleDelegate _sampleDelegate3;\r
- SampleDelegate _sampleDelegateStatic;\r
- SampleDelegate _sampleDelegateCombined;\r
-\r
- SimpleClass _shared1;\r
- SimpleClass _shared2;\r
- SimpleClass _shared3;\r
- \r
- FalseISerializable _falseSerializable;\r
-\r
- public void Init()\r
- {\r
- _type = typeof (string);\r
- _type2 = typeof (SomeValues);\r
- _dbnull = DBNull.Value;\r
- _assembly = typeof (SomeValues).Assembly;\r
- _intEnum = IntEnum.bbb;\r
- _byteEnum = ByteEnum.ccc;\r
- _bool = true;\r
- _bool2 = false;\r
- _byte = 254;\r
- _char = 'A';\r
- _dateTime = new DateTime (1972,7,13,1,20,59);\r
- _decimal = (decimal)101010.10101;\r
- _double = 123456.6789;\r
- _short = -19191;\r
- _int = -28282828;\r
- _long = 37373737373;\r
- _sbyte = -123;\r
- _float = (float)654321.321;\r
- _ushort = 61616;\r
- _uint = 464646464;\r
- _ulong = 55555555;\r
-\r
- Point p = new Point();\r
- p.x = 56; p.y = 67;\r
- object boxedPoint = p;\r
-\r
- long i = 22;\r
- object boxedLong = i;\r
-\r
- _objects = new object[] { "string", (int)1234, null , /*boxedPoint, boxedPoint,*/ boxedLong, boxedLong};\r
- _strings = new string[] { "an", "array", "of", "strings","I","repeat","an", "array", "of", "strings" };\r
- _ints = new int[] { 4,5,6,7,8 };\r
- _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
- _intsJagged = new int[2][] { new int[3] {1,2,3}, new int[2] {4,5} };\r
- _simples = new SimpleClass[] { new SimpleClass('a'),new SimpleClass('b'),new SimpleClass('c') };\r
- _simplesMulti = new SimpleClass[2,3] {{new SimpleClass('d'),new SimpleClass('e'),new SimpleClass('f')}, {new SimpleClass('g'),new SimpleClass('j'),new SimpleClass('h')}};\r
- _simplesJagged = new SimpleClass[2][] { new SimpleClass[1] { new SimpleClass('i') }, new SimpleClass[2] {null, new SimpleClass('k')}};\r
- _almostEmpty = new object[2000];\r
- _almostEmpty[1000] = 4;\r
-\r
- _emptyObjectArray = new object[0];\r
- _emptyTypeArray = new Type[0];\r
- _emptySimpleArray = new SimpleClass[0];\r
- _emptyIntArray = new int[0];\r
- _emptyStringArray = new string[0];\r
- _emptyPointArray = new Point[0];\r
-\r
- _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
-\r
- _sampleDelegate = new SampleDelegate(SampleCall);\r
- _sampleDelegate2 = new SampleDelegate(_simples[0].SampleCall);\r
- _sampleDelegate3 = new SampleDelegate(new SimpleClass('x').SampleCall);\r
- _sampleDelegateStatic = new SampleDelegate(SampleStaticCall);\r
- _sampleDelegateCombined = (SampleDelegate)Delegate.Combine (new Delegate[] {_sampleDelegate, _sampleDelegate2, _sampleDelegate3, _sampleDelegateStatic });\r
-\r
- // This is to test that references are correctly solved\r
- _shared1 = new SimpleClass('A');\r
- _shared2 = new SimpleClass('A');\r
- _shared3 = _shared1;\r
- \r
- _falseSerializable = new FalseISerializable (2);\r
- }\r
-\r
- public int SampleCall (string str, SomeValues sv, ref int acum)\r
- {\r
- acum += _int;\r
- return _int;\r
- }\r
-\r
- public static int SampleStaticCall (string str, SomeValues sv, ref int acum)\r
- {\r
- acum += 99;\r
- return 99;\r
- }\r
-\r
- public void CheckEquals(SomeValues obj)\r
- {\r
- SerializationTest.AssertEquals ("SomeValues._type", _type, obj._type);\r
- SerializationTest.AssertEquals ("SomeValues._type2", _type2, obj._type2);\r
- SerializationTest.AssertEquals ("SomeValues._dbnull", _dbnull, obj._dbnull);\r
- SerializationTest.AssertEquals ("SomeValues._assembly", _assembly, obj._assembly);\r
-\r
- SerializationTest.AssertEquals ("SomeValues._intEnum", _intEnum, obj._intEnum);\r
- SerializationTest.AssertEquals ("SomeValues._byteEnum", _byteEnum, obj._byteEnum);\r
- SerializationTest.AssertEquals ("SomeValues._bool", _bool, obj._bool);\r
- SerializationTest.AssertEquals ("SomeValues._bool2", _bool2, obj._bool2);\r
- SerializationTest.AssertEquals ("SomeValues._byte", _byte, obj._byte);\r
- SerializationTest.AssertEquals ("SomeValues._char", _char, obj._char);\r
- SerializationTest.AssertEquals ("SomeValues._dateTime", _dateTime, obj._dateTime);\r
- SerializationTest.AssertEquals ("SomeValues._decimal", _decimal, obj._decimal);\r
- SerializationTest.AssertEquals ("SomeValues._int", _int, obj._int);\r
- SerializationTest.AssertEquals ("SomeValues._long", _long, obj._long);\r
- SerializationTest.AssertEquals ("SomeValues._sbyte", _sbyte, obj._sbyte);\r
- SerializationTest.AssertEquals ("SomeValues._float", _float, obj._float);\r
- SerializationTest.AssertEquals ("SomeValues._ushort", _ushort, obj._ushort);\r
- SerializationTest.AssertEquals ("SomeValues._uint", _uint, obj._uint);\r
- SerializationTest.AssertEquals ("SomeValues._ulong", _ulong, obj._ulong);\r
-\r
- SerializationTest.EqualsArray ("SomeValues._objects", _objects, obj._objects);\r
- SerializationTest.EqualsArray ("SomeValues._strings", _strings, obj._strings);\r
- SerializationTest.EqualsArray ("SomeValues._doubles", _doubles, obj._doubles);\r
- SerializationTest.EqualsArray ("SomeValues._ints", _ints, obj._ints);\r
- SerializationTest.EqualsArray ("SomeValues._simples", _simples, obj._simples);\r
- SerializationTest.EqualsArray ("SomeValues._almostEmpty", _almostEmpty, obj._almostEmpty);\r
-\r
- SerializationTest.EqualsArray ("SomeValues._emptyObjectArray", _emptyObjectArray, obj._emptyObjectArray);\r
- SerializationTest.EqualsArray ("SomeValues._emptyTypeArray", _emptyTypeArray, obj._emptyTypeArray);\r
- SerializationTest.EqualsArray ("SomeValues._emptySimpleArray", _emptySimpleArray, obj._emptySimpleArray);\r
- SerializationTest.EqualsArray ("SomeValues._emptyIntArray", _emptyIntArray, obj._emptyIntArray);\r
- SerializationTest.EqualsArray ("SomeValues._emptyStringArray", _emptyStringArray, obj._emptyStringArray);\r
- SerializationTest.EqualsArray ("SomeValues._emptyPointArray", _emptyPointArray, obj._emptyPointArray);\r
-\r
- for (int i=0; i<2; i++)\r
- for (int j=0; j<3; j++)\r
- for (int k=0; k<4; k++)\r
- SerializationTest.AssertEquals("SomeValues._intsMulti[" + i + "," + j + "," + k + "]", _intsMulti[i,j,k], obj._intsMulti[i,j,k]);\r
-\r
- for (int i=0; i<_intsJagged.Length; i++)\r
- for (int j=0; j<_intsJagged[i].Length; j++)\r
- SerializationTest.AssertEquals ("SomeValues._intsJagged[" + i + "][" + j + "]", _intsJagged[i][j], obj._intsJagged[i][j]);\r
-\r
- for (int i=0; i<2; i++)\r
- for (int j=0; j<3; j++)\r
- SerializationTest.AssertEquals ("SomeValues._simplesMulti[" + i + "," + j + "]", _simplesMulti[i,j], obj._simplesMulti[i,j]);\r
-\r
- for (int i=0; i<_simplesJagged.Length; i++)\r
- SerializationTest.EqualsArray ("SomeValues._simplesJagged", _simplesJagged[i], obj._simplesJagged[i]);\r
-\r
- int acum = 0;\r
- SerializationTest.AssertEquals ("SomeValues._sampleDelegate", _sampleDelegate ("hi", this, ref acum), _int);\r
- SerializationTest.AssertEquals ("SomeValues._sampleDelegate_bis", _sampleDelegate ("hi", this, ref acum), obj._sampleDelegate ("hi", this, ref acum));\r
-\r
- SerializationTest.AssertEquals ("SomeValues._sampleDelegate2", _sampleDelegate2 ("hi", this, ref acum), (int)_simples[0].val);\r
- SerializationTest.AssertEquals ("SomeValues._sampleDelegate2_bis", _sampleDelegate2 ("hi", this, ref acum), obj._sampleDelegate2 ("hi", this, ref acum));\r
-\r
- SerializationTest.AssertEquals ("SomeValues._sampleDelegate3", _sampleDelegate3 ("hi", this, ref acum), (int)'x');\r
- SerializationTest.AssertEquals ("SomeValues._sampleDelegate3_bis", _sampleDelegate3 ("hi", this, ref acum), obj._sampleDelegate3 ("hi", this, ref acum));\r
-\r
- SerializationTest.AssertEquals ("SomeValues._sampleDelegateStatic", _sampleDelegateStatic ("hi", this, ref acum), 99);\r
- SerializationTest.AssertEquals ("SomeValues._sampleDelegateStatic_bis", _sampleDelegateStatic ("hi", this, ref acum), obj._sampleDelegateStatic ("hi", this, ref acum));\r
-\r
- int acum1 = 0;\r
- int acum2 = 0;\r
- _sampleDelegateCombined ("hi", this, ref acum1);\r
- obj._sampleDelegateCombined ("hi", this, ref acum2);\r
-\r
- SerializationTest.AssertEquals ("_sampleDelegateCombined", acum1, _int + (int)_simples[0].val + (int)'x' + 99);\r
- SerializationTest.AssertEquals ("_sampleDelegateCombined_bis", acum1, acum2);\r
-\r
- SerializationTest.AssertEquals ("SomeValues._shared1", _shared1, _shared2);\r
- SerializationTest.AssertEquals ("SomeValues._shared1_bis", _shared1, _shared3);\r
-\r
- _shared1.val = 'B';\r
- SerializationTest.AssertEquals ("SomeValues._shared2", _shared2.val, 'A');\r
- SerializationTest.AssertEquals ("SomeValues._shared3", _shared3.val, 'B');\r
- \r
- SerializationTest.AssertEquals ("SomeValues._falseSerializable", _falseSerializable.field, 2);\r
- }\r
- }\r
-\r
- class MethodTester : MarshalByRefObject\r
- {\r
- public int OverloadedMethod ()\r
- {\r
- return 123456789;\r
- }\r
-\r
- public int OverloadedMethod (int a)\r
- {\r
- return a+2;\r
- }\r
-\r
- public int OverloadedMethod (int[] a)\r
- {\r
- return a.Length;\r
- }\r
-\r
- public void NoReturn ()\r
- {}\r
-\r
- public string Simple (string a, int b)\r
- {\r
- return a + b;\r
- }\r
-\r
- public SimpleClass Simple2 (char c)\r
- {\r
- return new SimpleClass(c);\r
- }\r
-\r
- public SimpleClass Simple3 (char[] c)\r
- {\r
- if (c != null) return new SimpleClass(c[0]);\r
- else return null;\r
- }\r
-\r
- public int SomeMethod (int a, SimpleClass b)\r
- {\r
- object[] d;\r
- string c = "hi";\r
- int r = a + c.Length;\r
- c = "bye";\r
- d = new object[3];\r
- d[1] = b;\r
- return r;\r
- }\r
- }\r
-\r
- class AuxProxy: RealProxy\r
- {\r
- public static bool useHeaders = false;\r
- Stream _stream;\r
- string _uri;\r
- IMethodMessage _testMsg;\r
-\r
- public AuxProxy(Stream stream, string uri): base(typeof(MethodTester))\r
- {\r
- _stream = stream;\r
- _uri = uri;\r
- }\r
-\r
- public void SetTestMessage (IMessage msg)\r
- {\r
- _testMsg = (IMethodMessage)msg;\r
- _testMsg.Properties["__Uri"] = _uri;\r
- }\r
-\r
- public override IMessage Invoke(IMessage msg)\r
- {\r
- IMethodCallMessage call = (IMethodCallMessage)msg;\r
- if (call.MethodName.StartsWith ("Initialize")) return new ReturnMessage(null,null,0,null,(IMethodCallMessage)msg);\r
-\r
- call.Properties["__Uri"] = _uri;\r
-\r
- if (_stream != null)\r
- {\r
- SerializeCall (call);\r
- IMessage response = ChannelServices.SyncDispatchMessage (call);\r
- SerializeResponse (response);\r
- return response;\r
- }\r
- else if (_testMsg != null)\r
- {\r
- if (_testMsg is IMethodCallMessage)\r
- return ChannelServices.SyncDispatchMessage (_testMsg);\r
- else\r
- return _testMsg;\r
- }\r
- else\r
- return ChannelServices.SyncDispatchMessage (call);\r
- }\r
-\r
- void SerializeCall (IMessage call)\r
- {\r
- RemotingSurrogateSelector rss = new RemotingSurrogateSelector();\r
- IRemotingFormatter fmt = new BinaryFormatter (rss, new StreamingContext(StreamingContextStates.Remoting));\r
- fmt.Serialize (_stream, call, GetHeaders());\r
- }\r
-\r
- void SerializeResponse (IMessage resp)\r
- {\r
- RemotingSurrogateSelector rss = new RemotingSurrogateSelector();\r
- IRemotingFormatter fmt = new BinaryFormatter (rss, new StreamingContext(StreamingContextStates.Remoting));\r
- fmt.Serialize (_stream, resp, GetHeaders());\r
- }\r
-\r
- Header[] GetHeaders()\r
- {\r
- Header[] hs = null;\r
- if (useHeaders)\r
- {\r
- hs = new Header[1];\r
- hs[0] = new Header("unom",new SimpleClass('R'));\r
- }\r
- return hs;\r
- }\r
- }\r
-\r
- public class TestBinder : SerializationBinder\r
- {\r
- public override Type BindToType (string assemblyName, string typeName)\r
- {\r
- if (typeName.IndexOf("BinderTester_A") != -1)\r
- typeName = typeName.Replace ("BinderTester_A", "BinderTester_B");\r
-\r
- return Assembly.Load (assemblyName).GetType (typeName);\r
- }\r
- }\r
-\r
- [Serializable]\r
- public class BinderTester_A\r
- {\r
- public int x;\r
- public string y;\r
- }\r
-\r
- [Serializable]\r
- public class BinderTester_B\r
- {\r
- public string y;\r
- public int x;\r
- }\r
-\r
-\r
-}\r
+//
+// System.Runtime.Serialization.SerializationTest.cs
+//
+// Author: Lluis Sanchez Gual (lluis@ximian.com)
+//
+// (C) Ximian, Inc.
+//
+
+using System;
+using System.Diagnostics;
+using System.IO;
+using System.Runtime.Serialization;
+using System.Runtime.Serialization.Formatters.Binary;
+using System.Reflection;
+using System.Runtime.Remoting;
+using System.Runtime.Remoting.Channels;
+using System.Runtime.Remoting.Proxies;
+using System.Runtime.Remoting.Messaging;
+using System.Collections;
+using NUnit.Framework;
+
+namespace MonoTests.System.Runtime.Serialization
+{
+ [TestFixture]
+ public class SerializationTest
+ {
+ MemoryStream ms;
+ string uri;
+
+ [Test]
+ public void TestSerialization ()
+ {
+ MethodTester mt = new MethodTester();
+ RemotingServices.Marshal (mt);
+ uri = RemotingServices.GetObjectUri (mt);
+
+ WriteData();
+ ReadData();
+
+ RemotingServices.Disconnect (mt);
+ }
+
+ void WriteData ()
+ {
+ StreamingContext context = new StreamingContext (StreamingContextStates.Other);
+ SurrogateSelector sel = new SurrogateSelector();
+ sel.AddSurrogate (typeof (Point), context, new PointSurrogate());
+ sel.AddSurrogate (typeof (FalseISerializable), context, new FalseISerializableSurrogate());
+
+ List list = CreateTestData();
+ BinderTester_A bta = CreateBinderTestData();
+
+ ms = new MemoryStream();
+ BinaryFormatter f = new BinaryFormatter (sel, new StreamingContext(StreamingContextStates.Other));
+ f.Serialize (ms, list);
+ ProcessMessages (ms, null);
+ f.Serialize (ms, bta);
+ ms.Flush ();
+ ms.Position = 0;
+ }
+
+ void ReadData()
+ {
+ StreamingContext context = new StreamingContext (StreamingContextStates.Other);
+ SurrogateSelector sel = new SurrogateSelector();
+ sel.AddSurrogate (typeof (Point), context, new PointSurrogate());
+ sel.AddSurrogate (typeof (FalseISerializable), context, new FalseISerializableSurrogate());
+
+ BinaryFormatter f = new BinaryFormatter (sel, context);
+
+ object list = f.Deserialize (ms);
+
+ object[][] originalMsgData = null;
+ IMessage[] calls = null;
+ IMessage[] resps = null;
+
+ originalMsgData = ProcessMessages (null, null);
+
+ calls = new IMessage[originalMsgData.Length];
+ resps = new IMessage[originalMsgData.Length];
+
+
+ for (int n=0; n<originalMsgData.Length; n++)
+ {
+ calls[n] = (IMessage) f.Deserialize (ms);
+ resps[n] = (IMessage) f.DeserializeMethodResponse (ms, null, (IMethodCallMessage)calls[n]);
+ }
+
+ f.Binder = new TestBinder ();
+ object btbob = f.Deserialize (ms);
+
+ ms.Close();
+
+ List expected = CreateTestData ();
+ List actual = (List) list;
+ expected.CheckEquals (actual, "List");
+
+ for (int i = 0; i < actual.children.Length - 1; ++i)
+ if (actual.children [i].next != actual.children [i+1])
+ Assert.Fail ("Deserialization did not restore pointer graph");
+
+ BinderTester_A bta = CreateBinderTestData();
+ Assert.AreEqual (btbob.GetType(), typeof (BinderTester_B), "BinderTest.class");
+ BinderTester_B btb = btbob as BinderTester_B;
+ if (btb != null)
+ {
+ Assert.AreEqual (btb.x, bta.x, "BinderTest.x");
+ Assert.AreEqual (btb.y, bta.y, "BinderTest.y");
+ }
+
+ CheckMessages ("MethodCall", originalMsgData, ProcessMessages (null, calls));
+ CheckMessages ("MethodResponse", originalMsgData, ProcessMessages (null, resps));
+ }
+
+ BinderTester_A CreateBinderTestData ()
+ {
+ BinderTester_A bta = new BinderTester_A();
+ bta.x = 11;
+ bta.y = "binder tester";
+ return bta;
+ }
+
+ List CreateTestData()
+ {
+ List list = new List();
+ list.name = "my list";
+ list.values = new SomeValues();
+ list.values.Init();
+
+ ListItem item1 = new ListItem();
+ ListItem item2 = new ListItem();
+ ListItem item3 = new ListItem();
+
+ item1.label = "value label 1";
+ item1.next = item2;
+ item1.value.color = 111;
+ item1.value.point = new Point();
+ item1.value.point.x = 11;
+ item1.value.point.y = 22;
+
+ item2.label = "value label 2";
+ item2.next = item3;
+ item2.value.color = 222;
+
+ item2.value.point = new Point();
+ item2.value.point.x = 33;
+ item2.value.point.y = 44;
+
+ item3.label = "value label 3";
+ item3.value.color = 333;
+ item3.value.point = new Point();
+ item3.value.point.x = 55;
+ item3.value.point.y = 66;
+
+ list.children = new ListItem[3];
+
+ list.children[0] = item1;
+ list.children[1] = item2;
+ list.children[2] = item3;
+
+ return list;
+ }
+
+
+ object[][] ProcessMessages (Stream stream, IMessage[] messages)
+ {
+ object[][] results = new object[9][];
+
+ AuxProxy prx = new AuxProxy (stream, uri);
+ MethodTester mt = (MethodTester)prx.GetTransparentProxy();
+ object res;
+
+ if (messages != null) prx.SetTestMessage (messages[0]);
+ res = mt.OverloadedMethod();
+ results[0] = new object[] {res};
+
+ if (messages != null) prx.SetTestMessage (messages[1]);
+ res = mt.OverloadedMethod(22);
+ results[1] = new object[] {res};
+
+ if (messages != null) prx.SetTestMessage (messages[2]);
+ int[] par1 = new int[] {1,2,3};
+ res = mt.OverloadedMethod(par1);
+ results[2] = new object[] { res, par1 };
+
+ if (messages != null) prx.SetTestMessage (messages[3]);
+ mt.NoReturn();
+
+ if (messages != null) prx.SetTestMessage (messages[4]);
+ res = mt.Simple ("hello",44);
+ results[4] = new object[] { res };
+
+ if (messages != null) prx.SetTestMessage (messages[5]);
+ res = mt.Simple2 ('F');
+ results[5] = new object[] { res };
+
+ if (messages != null) prx.SetTestMessage (messages[6]);
+ char[] par2 = new char[] { 'G' };
+ res = mt.Simple3 (par2);
+ results[6] = new object[] { res, par2 };
+
+ if (messages != null) prx.SetTestMessage (messages[7]);
+ res = mt.Simple3 (null);
+ results[7] = new object[] { res };
+
+ if (messages != null) prx.SetTestMessage (messages[8]);
+
+ SimpleClass b = new SimpleClass ('H');
+ res = mt.SomeMethod (123456, b);
+ results[8] = new object[] { res, b };
+
+ return results;
+ }
+
+ void CheckMessages (string label, object[][] original, object[][] serialized)
+ {
+ for (int n=0; n<original.Length; n++)
+ EqualsArray (label + " " + n, original[n], serialized[n]);
+ }
+
+ public static void AssertEquals(string message, Object expected, Object actual)
+ {
+ if (expected != null && expected.GetType().IsArray)
+ EqualsArray (message, (Array)expected, (Array)actual);
+ else
+ Assert.AreEqual (expected, actual, message);
+ }
+
+ public static void EqualsArray (string message, object oar1, object oar2)
+ {
+ if (oar1 == null || oar2 == null || !(oar1 is Array) || !(oar2 is Array))
+ {
+ Assert.AreEqual (oar1, oar2, message);
+ return;
+ }
+
+ Array ar1 = (Array) oar1;
+ Array ar2 = (Array) oar2;
+
+ Assert.AreEqual (ar1.Length, ar2.Length, message + ".Length");
+
+ for (int n=0; n<ar1.Length; n++)
+ {
+ object av1 = ar1.GetValue(n);
+ object av2 = ar2.GetValue(n);
+ SerializationTest.AssertEquals (message + "[" + n + "]", av1, av2);
+ }
+ }
+ }
+
+
+
+ class PointSurrogate: ISerializationSurrogate
+ {
+ public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
+ {
+ Point p = (Point) obj;
+ info.AddValue ("xv",p.x);
+ info.AddValue ("yv",p.y);
+ }
+
+ public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
+ {
+ typeof (Point).GetField ("x").SetValue (obj, info.GetInt32 ("xv"));
+ typeof (Point).GetField ("y").SetValue (obj, info.GetInt32 ("yv"));
+ return obj;
+ }
+ }
+
+ [Serializable]
+ public class List
+ {
+ public string name = null;
+ public ListItem[] children = null;
+ public SomeValues values;
+
+ public void CheckEquals (List val, string context)
+ {
+ Assert.AreEqual (name, val.name, context + ".name");
+ values.CheckEquals (val.values, context + ".values");
+
+ Assert.AreEqual (children.Length, val.children.Length, context + ".children.Length");
+
+ for (int n=0; n<children.Length; n++)
+ children[n].CheckEquals (val.children[n], context + ".children[" + n + "]");
+ }
+ }
+
+ [Serializable]
+ public class ListItem: ISerializable
+ {
+ public ListItem()
+ {
+ }
+
+ ListItem (SerializationInfo info, StreamingContext ctx)
+ {
+ next = (ListItem)info.GetValue ("next", typeof (ListItem));
+ value = (ListValue)info.GetValue ("value", typeof (ListValue));
+ label = info.GetString ("label");
+ }
+
+ public void GetObjectData (SerializationInfo info, StreamingContext ctx)
+ {
+ info.AddValue ("next", next);
+ info.AddValue ("value", value);
+ info.AddValue ("label", label);
+ }
+
+ public void CheckEquals (ListItem val, string context)
+ {
+ Assert.AreEqual (label, val.label, context + ".label");
+ value.CheckEquals (val.value, context + ".value");
+
+ if (next == null) {
+ Assert.IsNull (val.next, context + ".next == null");
+ } else {
+ Assert.IsNotNull (val.next, context + ".next != null");
+ next.CheckEquals (val.next, context + ".next");
+ }
+ }
+
+ public override bool Equals(object obj)
+ {
+ ListItem val = (ListItem)obj;
+ if ((next == null || val.next == null) && (next != val.next)) return false;
+ if (next == null) return true;
+ if (!next.Equals(val.next)) return false;
+ return value.Equals (val.value) && label == val.label;
+ }
+
+ public override int GetHashCode ()
+ {
+ return base.GetHashCode ();
+ }
+
+ public ListItem next;
+ public ListValue value;
+ public string label;
+ }
+
+ [Serializable]
+ public struct ListValue
+ {
+ public int color;
+ public Point point;
+
+ public override bool Equals(object obj)
+ {
+ ListValue val = (ListValue)obj;
+ return (color == val.color && point.Equals(val.point));
+ }
+
+ public void CheckEquals (ListValue val, string context)
+ {
+ Assert.AreEqual (color, val.color, context + ".color");
+ point.CheckEquals (val.point, context + ".point");
+ }
+
+ public override int GetHashCode ()
+ {
+ return base.GetHashCode ();
+ }
+ }
+
+ public struct Point
+ {
+ public int x;
+ public int y;
+
+ public override bool Equals(object obj)
+ {
+ Point p = (Point)obj;
+ return (x == p.x && y == p.y);
+ }
+
+ public void CheckEquals (Point p, string context)
+ {
+ Assert.AreEqual (x, p.x, context + ".x");
+ Assert.AreEqual (y, p.y, context + ".y");
+ }
+
+ public override int GetHashCode ()
+ {
+ return base.GetHashCode ();
+ }
+ }
+
+ [Serializable]
+ public class FalseISerializable : ISerializable
+ {
+ public int field;
+
+ public FalseISerializable (int n)
+ {
+ field = n;
+ }
+
+ public void GetObjectData(SerializationInfo info, StreamingContext context)
+ {
+ throw new InvalidOperationException ("Serialize:We should not pass here.");
+ }
+
+ public FalseISerializable (SerializationInfo info, StreamingContext context)
+ {
+ throw new InvalidOperationException ("Deserialize:We should not pass here.");
+ }
+ }
+
+ public class FalseISerializableSurrogate : ISerializationSurrogate
+ {
+ public void GetObjectData (object obj, SerializationInfo info, StreamingContext context)
+ {
+ info.AddValue("field", Convert.ToString (((FalseISerializable)obj).field));
+ }
+
+ public object SetObjectData (object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
+ {
+ ((FalseISerializable)obj).field = Convert.ToInt32 (info.GetValue("field", typeof(string)));
+ return obj;
+ }
+ }
+
+ [Serializable]
+ public class SimpleClass
+ {
+ public SimpleClass (char v) { val = v; }
+
+ public override bool Equals(object obj)
+ {
+ if (obj == null) return false;
+ return val == ((SimpleClass)obj).val;
+ }
+
+ public override int GetHashCode()
+ {
+ return val.GetHashCode();
+ }
+
+ public int SampleCall (string str, SomeValues sv, ref int acum)
+ {
+ acum += (int)val;
+ return (int)val;
+ }
+
+ public char val;
+ }
+
+ enum IntEnum { aaa, bbb, ccc }
+ enum ByteEnum: byte { aaa=221, bbb=3, ccc=44 }
+
+ delegate int SampleDelegate (string str, SomeValues sv, ref int acum);
+
+ [Serializable]
+ public class SomeValues
+ {
+ Type _type;
+ Type _type2;
+ DBNull _dbnull;
+ Assembly _assembly;
+ IntEnum _intEnum;
+ ByteEnum _byteEnum;
+
+ bool _bool;
+ bool _bool2;
+ byte _byte;
+ char _char;
+ DateTime _dateTime;
+ decimal _decimal;
+ double _double;
+ short _short;
+ int _int;
+ long _long;
+ sbyte _sbyte;
+ float _float;
+ ushort _ushort;
+ uint _uint;
+ ulong _ulong;
+
+ object[] _objects;
+ string[] _strings;
+ int[] _ints;
+ public int[,,] _intsMulti;
+ int[][] _intsJagged;
+ SimpleClass[] _simples;
+ SimpleClass[,] _simplesMulti;
+ SimpleClass[][] _simplesJagged;
+ double[] _doubles;
+ object[] _almostEmpty;
+
+ object[] _emptyObjectArray;
+ Type[] _emptyTypeArray;
+ SimpleClass[] _emptySimpleArray;
+ int[] _emptyIntArray;
+ string[] _emptyStringArray;
+ Point[] _emptyPointArray;
+
+
+ SampleDelegate _sampleDelegate;
+ SampleDelegate _sampleDelegate2;
+ SampleDelegate _sampleDelegate3;
+ SampleDelegate _sampleDelegateStatic;
+ SampleDelegate _sampleDelegateCombined;
+
+ SimpleClass _shared1;
+ SimpleClass _shared2;
+ SimpleClass _shared3;
+
+ FalseISerializable _falseSerializable;
+
+ public void Init()
+ {
+ _type = typeof (string);
+ _type2 = typeof (SomeValues);
+ _dbnull = DBNull.Value;
+ _assembly = typeof (SomeValues).Assembly;
+ _intEnum = IntEnum.bbb;
+ _byteEnum = ByteEnum.ccc;
+ _bool = true;
+ _bool2 = false;
+ _byte = 254;
+ _char = 'A';
+ _dateTime = new DateTime (1972,7,13,1,20,59);
+ _decimal = (decimal)101010.10101;
+ _double = 123456.6789;
+ _short = -19191;
+ _int = -28282828;
+ _long = 37373737373;
+ _sbyte = -123;
+ _float = (float)654321.321;
+ _ushort = 61616;
+ _uint = 464646464;
+ _ulong = 55555555;
+
+ Point p = new Point();
+ p.x = 56; p.y = 67;
+ object boxedPoint = p;
+
+ long i = 22;
+ object boxedLong = i;
+
+ _objects = new object[] { "string", (int)1234, null , /*boxedPoint, boxedPoint,*/ boxedLong, boxedLong};
+ _strings = new string[] { "an", "array", "of", "strings","I","repeat","an", "array", "of", "strings" };
+ _ints = new int[] { 4,5,6,7,8 };
+ _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} } };
+ _intsJagged = new int[2][] { new int[3] {1,2,3}, new int[2] {4,5} };
+ _simples = new SimpleClass[] { new SimpleClass('a'),new SimpleClass('b'),new SimpleClass('c') };
+ _simplesMulti = new SimpleClass[2,3] {{new SimpleClass('d'),new SimpleClass('e'),new SimpleClass('f')}, {new SimpleClass('g'),new SimpleClass('j'),new SimpleClass('h')}};
+ _simplesJagged = new SimpleClass[2][] { new SimpleClass[1] { new SimpleClass('i') }, new SimpleClass[2] {null, new SimpleClass('k')}};
+ _almostEmpty = new object[2000];
+ _almostEmpty[1000] = 4;
+
+ _emptyObjectArray = new object[0];
+ _emptyTypeArray = new Type[0];
+ _emptySimpleArray = new SimpleClass[0];
+ _emptyIntArray = new int[0];
+ _emptyStringArray = new string[0];
+ _emptyPointArray = new Point[0];
+
+ _doubles = new double[] { 1010101.101010, 292929.29292, 3838383.38383, 4747474.474, 56565.5656565, 0, Double.NaN, Double.MaxValue, Double.MinValue, Double.NegativeInfinity, Double.PositiveInfinity };
+
+ _sampleDelegate = new SampleDelegate(SampleCall);
+ _sampleDelegate2 = new SampleDelegate(_simples[0].SampleCall);
+ _sampleDelegate3 = new SampleDelegate(new SimpleClass('x').SampleCall);
+ _sampleDelegateStatic = new SampleDelegate(SampleStaticCall);
+ _sampleDelegateCombined = (SampleDelegate)Delegate.Combine (new Delegate[] {_sampleDelegate, _sampleDelegate2, _sampleDelegate3, _sampleDelegateStatic });
+
+ // This is to test that references are correctly solved
+ _shared1 = new SimpleClass('A');
+ _shared2 = new SimpleClass('A');
+ _shared3 = _shared1;
+
+ _falseSerializable = new FalseISerializable (2);
+ }
+
+ public int SampleCall (string str, SomeValues sv, ref int acum)
+ {
+ acum += _int;
+ return _int;
+ }
+
+ public static int SampleStaticCall (string str, SomeValues sv, ref int acum)
+ {
+ acum += 99;
+ return 99;
+ }
+
+ public void CheckEquals (SomeValues obj, string context)
+ {
+ Assert.AreEqual (_type, obj._type, context + "._type");
+ Assert.AreEqual (_type2, obj._type2, context + "._type2");
+ Assert.AreEqual (_dbnull, obj._dbnull, context + "._dbnull");
+ Assert.AreEqual (_assembly, obj._assembly, context + "._assembly");
+
+ Assert.AreEqual (_intEnum, obj._intEnum, context + "._intEnum");
+ Assert.AreEqual (_byteEnum, obj._byteEnum, context + "._byteEnum");
+ Assert.AreEqual (_bool, obj._bool, context + "._bool");
+ Assert.AreEqual (_bool2, obj._bool2, context + "._bool2");
+ Assert.AreEqual (_byte, obj._byte, context + "._byte");
+ Assert.AreEqual (_char, obj._char, context + "._char");
+ Assert.AreEqual (_dateTime, obj._dateTime, context + "._dateTime");
+ Assert.AreEqual (_decimal, obj._decimal, context + "._decimal");
+ Assert.AreEqual (_int, obj._int, context + "._int");
+ Assert.AreEqual (_long, obj._long, context + "._long");
+ Assert.AreEqual (_sbyte, obj._sbyte, context + "._sbyte");
+ Assert.AreEqual (_float, obj._float, context + "._float");
+ Assert.AreEqual (_ushort, obj._ushort, context + "._ushort");
+ Assert.AreEqual (_uint, obj._uint, context + "._uint");
+ Assert.AreEqual (_ulong, obj._ulong, context + "._ulong");
+
+ SerializationTest.EqualsArray (context + "._objects", _objects, obj._objects);
+ SerializationTest.EqualsArray (context + "._strings", _strings, obj._strings);
+ SerializationTest.EqualsArray (context + "._doubles", _doubles, obj._doubles);
+ SerializationTest.EqualsArray (context + "._ints", _ints, obj._ints);
+ SerializationTest.EqualsArray (context + "._simples", _simples, obj._simples);
+ SerializationTest.EqualsArray (context + "._almostEmpty", _almostEmpty, obj._almostEmpty);
+
+ SerializationTest.EqualsArray (context + "._emptyObjectArray", _emptyObjectArray, obj._emptyObjectArray);
+ SerializationTest.EqualsArray (context + "._emptyTypeArray", _emptyTypeArray, obj._emptyTypeArray);
+ SerializationTest.EqualsArray (context + "._emptySimpleArray", _emptySimpleArray, obj._emptySimpleArray);
+ SerializationTest.EqualsArray (context + "._emptyIntArray", _emptyIntArray, obj._emptyIntArray);
+ SerializationTest.EqualsArray (context + "._emptyStringArray", _emptyStringArray, obj._emptyStringArray);
+ SerializationTest.EqualsArray (context + "._emptyPointArray", _emptyPointArray, obj._emptyPointArray);
+
+ for (int i=0; i<2; i++)
+ for (int j=0; j<3; j++)
+ for (int k=0; k<4; k++)
+ SerializationTest.AssertEquals("SomeValues._intsMulti[" + i + "," + j + "," + k + "]", _intsMulti[i,j,k], obj._intsMulti[i,j,k]);
+
+ for (int i=0; i<_intsJagged.Length; i++)
+ for (int j=0; j<_intsJagged[i].Length; j++)
+ SerializationTest.AssertEquals ("SomeValues._intsJagged[" + i + "][" + j + "]", _intsJagged[i][j], obj._intsJagged[i][j]);
+
+ for (int i=0; i<2; i++)
+ for (int j=0; j<3; j++)
+ SerializationTest.AssertEquals ("SomeValues._simplesMulti[" + i + "," + j + "]", _simplesMulti[i,j], obj._simplesMulti[i,j]);
+
+ for (int i=0; i<_simplesJagged.Length; i++)
+ SerializationTest.EqualsArray ("SomeValues._simplesJagged", _simplesJagged[i], obj._simplesJagged[i]);
+
+ int acum = 0;
+ SerializationTest.AssertEquals ("SomeValues._sampleDelegate", _sampleDelegate ("hi", this, ref acum), _int);
+ SerializationTest.AssertEquals ("SomeValues._sampleDelegate_bis", _sampleDelegate ("hi", this, ref acum), obj._sampleDelegate ("hi", this, ref acum));
+
+ SerializationTest.AssertEquals ("SomeValues._sampleDelegate2", _sampleDelegate2 ("hi", this, ref acum), (int)_simples[0].val);
+ SerializationTest.AssertEquals ("SomeValues._sampleDelegate2_bis", _sampleDelegate2 ("hi", this, ref acum), obj._sampleDelegate2 ("hi", this, ref acum));
+
+ SerializationTest.AssertEquals ("SomeValues._sampleDelegate3", _sampleDelegate3 ("hi", this, ref acum), (int)'x');
+ SerializationTest.AssertEquals ("SomeValues._sampleDelegate3_bis", _sampleDelegate3 ("hi", this, ref acum), obj._sampleDelegate3 ("hi", this, ref acum));
+
+ SerializationTest.AssertEquals ("SomeValues._sampleDelegateStatic", _sampleDelegateStatic ("hi", this, ref acum), 99);
+ SerializationTest.AssertEquals ("SomeValues._sampleDelegateStatic_bis", _sampleDelegateStatic ("hi", this, ref acum), obj._sampleDelegateStatic ("hi", this, ref acum));
+
+ int acum1 = 0;
+ int acum2 = 0;
+ _sampleDelegateCombined ("hi", this, ref acum1);
+ obj._sampleDelegateCombined ("hi", this, ref acum2);
+
+ SerializationTest.AssertEquals ("_sampleDelegateCombined", acum1, _int + (int)_simples[0].val + (int)'x' + 99);
+ SerializationTest.AssertEquals ("_sampleDelegateCombined_bis", acum1, acum2);
+
+ SerializationTest.AssertEquals ("SomeValues._shared1", _shared1, _shared2);
+ SerializationTest.AssertEquals ("SomeValues._shared1_bis", _shared1, _shared3);
+
+ _shared1.val = 'B';
+ SerializationTest.AssertEquals ("SomeValues._shared2", _shared2.val, 'A');
+ SerializationTest.AssertEquals ("SomeValues._shared3", _shared3.val, 'B');
+
+ SerializationTest.AssertEquals ("SomeValues._falseSerializable", _falseSerializable.field, 2);
+ }
+ }
+
+ class MethodTester : MarshalByRefObject
+ {
+ public int OverloadedMethod ()
+ {
+ return 123456789;
+ }
+
+ public int OverloadedMethod (int a)
+ {
+ return a+2;
+ }
+
+ public int OverloadedMethod (int[] a)
+ {
+ return a.Length;
+ }
+
+ public void NoReturn ()
+ {}
+
+ public string Simple (string a, int b)
+ {
+ return a + b;
+ }
+
+ public SimpleClass Simple2 (char c)
+ {
+ return new SimpleClass(c);
+ }
+
+ public SimpleClass Simple3 (char[] c)
+ {
+ if (c != null) return new SimpleClass(c[0]);
+ else return null;
+ }
+
+ public int SomeMethod (int a, SimpleClass b)
+ {
+ object[] d;
+ string c = "hi";
+ int r = a + c.Length;
+ c = "bye";
+ d = new object[3];
+ d[1] = b;
+ return r;
+ }
+ }
+
+ class AuxProxy: RealProxy
+ {
+ public static bool useHeaders = false;
+ Stream _stream;
+ string _uri;
+ IMethodMessage _testMsg;
+
+ public AuxProxy(Stream stream, string uri): base(typeof(MethodTester))
+ {
+ _stream = stream;
+ _uri = uri;
+ }
+
+ public void SetTestMessage (IMessage msg)
+ {
+ _testMsg = (IMethodMessage)msg;
+ _testMsg.Properties["__Uri"] = _uri;
+ }
+
+ public override IMessage Invoke(IMessage msg)
+ {
+ IMethodCallMessage call = (IMethodCallMessage)msg;
+ if (call.MethodName.StartsWith ("Initialize")) return new ReturnMessage(null,null,0,null,(IMethodCallMessage)msg);
+
+ call.Properties["__Uri"] = _uri;
+
+ if (_stream != null)
+ {
+ SerializeCall (call);
+ IMessage response = ChannelServices.SyncDispatchMessage (call);
+ SerializeResponse (response);
+ return response;
+ }
+ else if (_testMsg != null)
+ {
+ if (_testMsg is IMethodCallMessage)
+ return ChannelServices.SyncDispatchMessage (_testMsg);
+ else
+ return _testMsg;
+ }
+ else
+ return ChannelServices.SyncDispatchMessage (call);
+ }
+
+ void SerializeCall (IMessage call)
+ {
+ RemotingSurrogateSelector rss = new RemotingSurrogateSelector();
+ IRemotingFormatter fmt = new BinaryFormatter (rss, new StreamingContext(StreamingContextStates.Remoting));
+ fmt.Serialize (_stream, call, GetHeaders());
+ }
+
+ void SerializeResponse (IMessage resp)
+ {
+ RemotingSurrogateSelector rss = new RemotingSurrogateSelector();
+ IRemotingFormatter fmt = new BinaryFormatter (rss, new StreamingContext(StreamingContextStates.Remoting));
+ fmt.Serialize (_stream, resp, GetHeaders());
+ }
+
+ Header[] GetHeaders()
+ {
+ Header[] hs = null;
+ if (useHeaders)
+ {
+ hs = new Header[1];
+ hs[0] = new Header("unom",new SimpleClass('R'));
+ }
+ return hs;
+ }
+ }
+
+ public class TestBinder : SerializationBinder
+ {
+ public override Type BindToType (string assemblyName, string typeName)
+ {
+ if (typeName.IndexOf("BinderTester_A") != -1)
+ typeName = typeName.Replace ("BinderTester_A", "BinderTester_B");
+
+ return Assembly.Load (assemblyName).GetType (typeName);
+ }
+ }
+
+ [Serializable]
+ public class BinderTester_A
+ {
+ public int x;
+ public string y;
+ }
+
+ [Serializable]
+ public class BinderTester_B
+ {
+ public string y;
+ public int x;
+ }
+
+
+}