PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/corlib/Test/System.Runtime.Serialization/SerializationTest.cs

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