SerializationTest.cs 25 KB

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