PageRenderTime 72ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

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

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