SoapFormatterTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. // project created on 09/05/2003 at 18:07
  2. using System;
  3. using System.Collections;
  4. using System.Reflection;
  5. using System.Runtime.Remoting;
  6. using System.Runtime.Remoting.Messaging;
  7. using System.Runtime.Serialization;
  8. using System.Runtime.Serialization.Formatters;
  9. using System.Runtime.Serialization.Formatters.Soap;
  10. using System.Runtime.Remoting.Metadata.W3cXsd2001;
  11. using System.IO;
  12. using NUnit.Framework;
  13. namespace MonoTests.System.Runtime.Serialization.Formatters.Soap {
  14. internal class NonSerializableObject {
  15. }
  16. public delegate void TrucDlg(string s);
  17. [Serializable]
  18. public class MoreComplexObject {
  19. public event TrucDlg TrucEvent;
  20. private string _string;
  21. private string[] _strings = new string[]{};
  22. private Queue _queue = new Queue();
  23. public Hashtable _table = new Hashtable();
  24. public string ObjString {
  25. get { return _string; }
  26. }
  27. public MoreComplexObject() {
  28. TrucEvent += new TrucDlg(WriteString);
  29. _queue.Enqueue(1);
  30. _queue.Enqueue(null);
  31. _queue.Enqueue("foo");
  32. _table["foo"]="barr";
  33. _table[1]="foo";
  34. _table['c'] = "barr";
  35. _table["barr"] = 1234567890;
  36. }
  37. public void OnTrucEvent(string s) {
  38. TrucEvent(s);
  39. }
  40. public void WriteString(string s) {
  41. _string = s;
  42. }
  43. public override bool Equals(object obj) {
  44. MoreComplexObject objReturn = obj as MoreComplexObject;
  45. if(objReturn == null) return false;
  46. if(objReturn._string != this._string) return false;
  47. IEnumerator myEnum = this._table.GetEnumerator();
  48. foreach(DictionaryEntry e in objReturn._table) {
  49. myEnum.MoveNext();
  50. DictionaryEntry s = (DictionaryEntry) myEnum.Current;
  51. Assertion.AssertEquals("#_table", s.Key, e.Key);
  52. Assertion.AssertEquals("#_table", s.Value, e.Value);
  53. if(s.Key.ToString() != e.Key.ToString() || s.Value.ToString() != e.Value.ToString()) return false;
  54. }
  55. // Assertion.Assert("#_table is null", objReturn._table != null);
  56. // Console.WriteLine("_table[foo]: {0}", objReturn._table["foo"]);
  57. // Assertion.AssertEquals("#_table[\"foo\"]", "barr", objReturn._table["foo"]);
  58. // Console.WriteLine("_table[1]: {0}", objReturn._table[1]);
  59. // Assertion.AssertEquals("#_table[1]", "foo", objReturn._table[1]);
  60. // Console.WriteLine("_table['c']: {0}", objReturn._table['c']);
  61. // Assertion.AssertEquals("#_table['c']", "barr", objReturn._table['c']);
  62. // Console.WriteLine("_table[barr]: {0}", objReturn._table["barr"]);
  63. // Assertion.AssertEquals("#_table[\"barr\"]", 1234567890, objReturn._table["barr"]);
  64. return SoapFormatterTest.CheckArray(this._queue.ToArray(), objReturn._queue.ToArray());
  65. }
  66. }
  67. [Serializable]
  68. internal class MarshalObject: MarshalByRefObject {
  69. private string _name;
  70. private long _id;
  71. public MarshalObject() {
  72. }
  73. public MarshalObject(string name, long id) {
  74. _name = name;
  75. _id = id;
  76. }
  77. }
  78. [Serializable]
  79. internal class SimpleObject {
  80. private string _name;
  81. private int _id;
  82. public SimpleObject(string name, int id) {
  83. _name = name;
  84. _id = id;
  85. }
  86. public override bool Equals(object obj) {
  87. SimpleObject objCmp = obj as SimpleObject;
  88. if(objCmp == null) return false;
  89. if(objCmp._name != this._name) return false;
  90. if(objCmp._id != this._id) return false;
  91. return true;
  92. }
  93. }
  94. [Serializable]
  95. internal class Version1 {
  96. public int _value;
  97. public Version1(int value) {
  98. _value = value;
  99. }
  100. }
  101. [Serializable]
  102. internal class Version2: ISerializable {
  103. public int _value;
  104. public string _foo;
  105. public Version2(int value, string foo) {
  106. _value = value;
  107. _foo = foo;
  108. }
  109. public void GetObjectData(SerializationInfo info, StreamingContext context) {
  110. info.AddValue("_value", _value);
  111. info.AddValue("_foo", _foo);
  112. }
  113. private Version2(SerializationInfo info, StreamingContext context) {
  114. _value = info.GetInt32("_value");
  115. try{
  116. _foo = info.GetString("_foo");
  117. }
  118. catch(SerializationException) {
  119. _foo = "Default value";
  120. }
  121. }
  122. }
  123. public class Version1ToVersion2Binder: SerializationBinder {
  124. public override Type BindToType (string assemblyName, string typeName) {
  125. Type returnType = null;
  126. string typeVersion1 = "MonoTests.System.Runtime.Serialization.Formatters.Soap.Version1";
  127. string assemName = Assembly.GetExecutingAssembly().FullName;
  128. if(typeName == typeVersion1) {
  129. typeName = "MonoTests.System.Runtime.Serialization.Formatters.Soap.Version2";
  130. }
  131. string typeFormat = String.Format("{0}, {1}", typeName, assemName);
  132. returnType = Type.GetType( typeFormat);
  133. return returnType;
  134. }
  135. }
  136. [TestFixture]
  137. public class SoapFormatterTest
  138. {
  139. private SoapFormatter _soapFormatter;
  140. private SoapFormatter _soapFormatterDeserializer;
  141. private RemotingSurrogateSelector _surrogate;
  142. #if DEBUG
  143. private void Out(MemoryStream stream, object objGraph) {
  144. Console.WriteLine("\n---------------------\n{0}\n", objGraph.ToString());
  145. stream.Position = 0;
  146. StreamReader r = new StreamReader(stream);
  147. Console.WriteLine(r.ReadToEnd());
  148. }
  149. #endif
  150. private object Serialize(object objGraph) {
  151. MemoryStream stream = new MemoryStream();
  152. Assertion.Assert(objGraph != null);
  153. Assertion.Assert(stream != null);
  154. _soapFormatter.SurrogateSelector = _surrogate;
  155. _soapFormatter.Serialize(stream, objGraph);
  156. #if DEBUG
  157. Out(stream, objGraph);
  158. #endif
  159. stream.Position = 0;
  160. object objReturn = _soapFormatterDeserializer.Deserialize(stream);
  161. Assertion.Assert(objReturn != null);
  162. Assertion.AssertEquals("#Tests "+objGraph.GetType(), objGraph.GetType(), objReturn.GetType());
  163. stream = new MemoryStream();
  164. _soapFormatter.Serialize(stream, objReturn);
  165. stream.Position = 0;
  166. return objReturn;
  167. }
  168. [SetUp]
  169. public void GetReady() {
  170. StreamingContext context = new StreamingContext(StreamingContextStates.All);
  171. _surrogate = new RemotingSurrogateSelector();
  172. _soapFormatter = new SoapFormatter(_surrogate, context);
  173. _soapFormatterDeserializer = new SoapFormatter(null, context);
  174. }
  175. [TearDown]
  176. public void Clean() {
  177. }
  178. [Test]
  179. public void TestValueTypes() {
  180. object objReturn;
  181. objReturn = Serialize((short)1);
  182. Assertion.AssertEquals("#int16", objReturn, 1);
  183. objReturn = Serialize(1);
  184. Assertion.AssertEquals("#int32", objReturn, 1);
  185. objReturn = Serialize((Single)0.1234);
  186. Assertion.AssertEquals("#Single", objReturn, 0.1234);
  187. objReturn = Serialize((Double)1234567890.0987654321);
  188. Assertion.AssertEquals("#iDouble", objReturn, 1234567890.0987654321);
  189. objReturn = Serialize(true);
  190. Assertion.AssertEquals("#Bool", objReturn, true);
  191. objReturn = Serialize((Int64) 1234567890);
  192. Assertion.AssertEquals("#Int64", objReturn, 1234567890);
  193. objReturn = Serialize('c');
  194. Assertion.AssertEquals("#Char", objReturn, 'c');
  195. }
  196. [Test]
  197. public void TestObjects() {
  198. object objReturn;
  199. objReturn = Serialize("");
  200. objReturn = Serialize("hello world!");
  201. Assertion.AssertEquals("#string", "hello world!", objReturn);
  202. SoapMessage soapMsg = new SoapMessage();
  203. soapMsg.Headers = new Header[0];
  204. soapMsg.MethodName = "Equals";
  205. soapMsg.ParamNames = new String[0];
  206. soapMsg.ParamTypes = new Type[0];
  207. soapMsg.ParamValues = new object[0];
  208. soapMsg.XmlNameSpace = SoapServices.CodeXmlNamespaceForClrTypeNamespace("String", "System");
  209. _soapFormatterDeserializer.TopObject = new SoapMessage();
  210. objReturn = Serialize(soapMsg);
  211. _soapFormatterDeserializer.TopObject = null;
  212. SimpleObject obj = new SimpleObject("simple object", 1);
  213. objReturn = Serialize(obj);
  214. Assertion.AssertEquals("#SimpleObject", obj, objReturn);
  215. objReturn = Serialize(typeof(SimpleObject));
  216. Assertion.AssertEquals("#Type", typeof(SimpleObject), (Type)objReturn);
  217. objReturn = Serialize(obj.GetType().Assembly);
  218. Assertion.AssertEquals("#Assembly", obj.GetType().Assembly, objReturn);
  219. }
  220. public static bool CheckArray(object objTest, object objReturn) {
  221. Array objTestAsArray = objTest as Array;
  222. Array objReturnAsArray = objReturn as Array;
  223. Assertion.Assert("#Not an Array "+objTest, objReturnAsArray != null);
  224. Assertion.AssertEquals("#Different lengths "+objTest, objTestAsArray.Length, objReturnAsArray.Length);
  225. IEnumerator iEnum = objReturnAsArray.GetEnumerator();
  226. iEnum.Reset();
  227. object obj2;
  228. foreach(object obj1 in objTestAsArray) {
  229. iEnum.MoveNext();
  230. obj2 = iEnum.Current;
  231. Assertion.AssertEquals("#The content of the 2 arrays is different", obj1, obj2);
  232. }
  233. return true;
  234. }
  235. [Test]
  236. public void TestArray() {
  237. object objReturn;
  238. object objTest;
  239. objReturn = Serialize(new int[]{});
  240. objTest = new int[]{1, 2, 3, 4};
  241. objReturn = Serialize(objTest);
  242. CheckArray(objTest, objReturn);
  243. objReturn = Serialize(new long[]{1, 2, 3, 4});
  244. objTest = new object[]{1, null, ":-)", 1234567890};
  245. objReturn = Serialize(objTest);
  246. objTest = new int[,]{{0, 1}, {2, 3}, {123, 4}};
  247. objReturn = Serialize(objTest);
  248. CheckArray(objTest, objReturn);
  249. objTest = new string[]{};
  250. objReturn = Serialize(objTest);
  251. CheckArray(objTest, objReturn);
  252. object[,,] objArray = new object[3,2,1];
  253. objArray[0,0,0] = 1;
  254. objArray[2,1,0] = "end";
  255. objReturn = Serialize(objArray);
  256. CheckArray(objArray, objReturn);
  257. }
  258. [Test]
  259. public void TestMarshalByRefObject() {
  260. Serialize(new MarshalObject("thing", 1234567890));
  261. }
  262. [Test]
  263. [ExpectedException(typeof(ArgumentNullException))]
  264. public void TestNullObject() {
  265. MemoryStream stream = new MemoryStream();
  266. _soapFormatter.Serialize(stream, null);
  267. }
  268. [Test]
  269. [ExpectedException(typeof(SerializationException))]
  270. public void TestNonSerialisable() {
  271. Serialize(new NonSerializableObject());
  272. }
  273. [Test]
  274. public void TestMoreComplexObject() {
  275. MoreComplexObject objReturn;
  276. MoreComplexObject objTest = new MoreComplexObject();
  277. objReturn = (MoreComplexObject) Serialize(objTest);
  278. Assertion.AssertEquals("#Equals", objTest, objReturn);
  279. objReturn.OnTrucEvent("bidule");
  280. Assertion.AssertEquals("#dlg", "bidule", objReturn.ObjString);
  281. }
  282. [Test]
  283. public void TestSerializationbinder() {
  284. Object objReturn;
  285. MemoryStream stream = new MemoryStream();
  286. Version1 objVer1 = new Version1(123);
  287. _soapFormatter.SurrogateSelector = _surrogate;
  288. _soapFormatter.Serialize(stream, objVer1);
  289. stream.Position = 0;
  290. _soapFormatterDeserializer.Binder = new Version1ToVersion2Binder();
  291. objReturn = _soapFormatterDeserializer.Deserialize(stream);
  292. Assertion.AssertEquals("#Version1 Version2", "Version2", objReturn.GetType().Name);
  293. Assertion.AssertEquals("#_value", 123, ((Version2) objReturn)._value);
  294. Assertion.AssertEquals("#_foo", "Default value", ((Version2) objReturn)._foo);
  295. }
  296. [Test]
  297. public void TestMethodSignatureSerialization ()
  298. {
  299. Header h = new Header ("__MethodSignature", new Type [] { typeof(string),typeof(SignatureTest[]) }, false, "http://schemas.microsoft.com/clr/soap/messageProperties");
  300. SoapMessage msg = new SoapMessage ();
  301. msg.MethodName = "Run";
  302. msg.ParamNames = new string [] { "nom" };
  303. msg.ParamTypes = new Type [] { typeof(SignatureTest) };
  304. msg.ParamValues = new object[] { new SignatureTest () };
  305. msg.Headers = new Header[] { h};
  306. MemoryStream ms = new MemoryStream ();
  307. SoapFormatter sf = new SoapFormatter ();
  308. sf.Serialize (ms, msg);
  309. ms.Position = 0;
  310. SoapMessage t = new SoapMessage ();
  311. sf.TopObject = t;
  312. t = (SoapMessage) sf.Deserialize (ms);
  313. Assertion.AssertNotNull ("#1", t.Headers[0].Value);
  314. Assertion.AssertEquals ("#2", t.Headers[0].Value.GetType (), typeof(Type[]));
  315. Type[] ts = (Type[]) t.Headers[0].Value;
  316. Assertion.AssertEquals ("#3", 2, ts.Length);
  317. Assertion.AssertNotNull ("#4", ts[0]);
  318. Assertion.AssertNotNull ("#5", ts[1]);
  319. Console.WriteLine ("PPP:" + ts[0].GetType());
  320. Assertion.AssertEquals ("#6", typeof(string), ts[0]);
  321. Assertion.AssertEquals ("#7", typeof(SignatureTest[]), ts[1]);
  322. }
  323. }
  324. [Serializable]
  325. public class SignatureTest
  326. {
  327. public SoapQName qn = new SoapQName ("e", "name", "espai");
  328. }
  329. }