LosFormatterTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. //
  2. // LosFormatterTest.cs - Unit tests for System.Web.UI.LosFormatter
  3. //
  4. // Authors:
  5. // Gert Driesen <[email protected]>
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2007 Gert Driesen
  9. // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.IO;
  32. using System.Text;
  33. using System.Web.UI;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Web.UI
  36. {
  37. [TestFixture]
  38. public class LosFormatterTest
  39. {
  40. static byte [] Empty = new byte [0];
  41. string Serialize (LosFormatter lf, object value)
  42. {
  43. StringWriter sw = new StringWriter ();
  44. lf.Serialize (sw, value);
  45. return sw.ToString ();
  46. }
  47. object Deserialize (LosFormatter lf, string serializedData)
  48. {
  49. return lf.Deserialize (serializedData);
  50. }
  51. string NoKeyRoundTrip (LosFormatter lf, string assertionMessage)
  52. {
  53. string serializedData = Serialize (lf, "Mono");
  54. Assert.AreEqual ("Mono", (string) Deserialize (lf, serializedData), assertionMessage);
  55. return serializedData;
  56. }
  57. [Test]
  58. public void Ctor_BoolByteArray ()
  59. {
  60. LosFormatter lf1 = new LosFormatter (false, (byte []) null);
  61. string expected = NoKeyRoundTrip (lf1, "false, null");
  62. LosFormatter lf2 = new LosFormatter (true, (byte []) null);
  63. Assert.AreEqual (expected, NoKeyRoundTrip (lf2, "true, null"), "2");
  64. LosFormatter lf3 = new LosFormatter (false, Empty);
  65. Assert.AreEqual (expected, NoKeyRoundTrip (lf3, "false, empty"), "3");
  66. // an empty key is still a key - a signature is appended
  67. LosFormatter lf4 = new LosFormatter (true, Empty);
  68. string signed = NoKeyRoundTrip (lf4, "true, empty");
  69. Assert.AreNotEqual (expected, signed, "4");
  70. byte [] data = Convert.FromBase64String (expected);
  71. byte [] signed_data = Convert.FromBase64String (signed);
  72. Assert.IsTrue (BitConverter.ToString (signed_data).StartsWith (BitConverter.ToString (data)), "4 / same data");
  73. #if NET_4_0
  74. // 32 bytes == 256 bits -> match HMACSHA256 as default
  75. Assert.AreEqual (32, signed_data.Length - data.Length, "signature length");
  76. #else
  77. // 20 bytes == 160 bits -> match HMACSHA1 as default
  78. Assert.AreEqual (20, signed_data.Length - data.Length, "signature length");
  79. #endif
  80. }
  81. [Test]
  82. public void Ctor_BoolString ()
  83. {
  84. LosFormatter lf1 = new LosFormatter (false, (string) null);
  85. string expected = NoKeyRoundTrip (lf1, "false, null");
  86. LosFormatter lf2 = new LosFormatter (true, (string) null);
  87. Assert.AreEqual (expected, NoKeyRoundTrip (lf2, "true, null"), "2");
  88. LosFormatter lf3 = new LosFormatter (false, String.Empty);
  89. Assert.AreEqual (expected, NoKeyRoundTrip (lf3, "false, empty"), "3");
  90. // an empty string is not an empty key!
  91. LosFormatter lf4 = new LosFormatter (true, String.Empty);
  92. Assert.AreEqual (expected, NoKeyRoundTrip (lf4, "true, empty"), "4");
  93. byte [] key = new byte [32];
  94. LosFormatter lf5 = new LosFormatter (true, Convert.ToBase64String (key));
  95. string signed = NoKeyRoundTrip (lf5, "true, b64");
  96. Assert.AreNotEqual (expected, signed, "5");
  97. byte [] data = Convert.FromBase64String (expected);
  98. byte [] signed_data = Convert.FromBase64String (signed);
  99. Assert.IsTrue (BitConverter.ToString (signed_data).StartsWith (BitConverter.ToString (data)), "5 / same data");
  100. #if NET_4_0
  101. // 32 bytes == 256 bits -> match HMACSHA256 as default
  102. Assert.AreEqual (32, signed_data.Length - data.Length, "signature length");
  103. #else
  104. // 20 bytes == 160 bits -> match HMACSHA1 as default
  105. Assert.AreEqual (20, signed_data.Length - data.Length, "signature length");
  106. #endif
  107. }
  108. string SerializeOverloads (LosFormatter lf, string message)
  109. {
  110. string stream_ser;
  111. using (MemoryStream ms = new MemoryStream ()) {
  112. lf.Serialize (ms, String.Empty);
  113. stream_ser = Convert.ToBase64String (ms.ToArray ());
  114. }
  115. string tw_ser;
  116. using (MemoryStream ms = new MemoryStream ()) {
  117. using (TextWriter tw = new StreamWriter (ms)) {
  118. lf.Serialize (tw, String.Empty);
  119. }
  120. tw_ser = Convert.ToBase64String (ms.ToArray ());
  121. }
  122. Assert.AreEqual (stream_ser, tw_ser, message);
  123. return stream_ser;
  124. }
  125. [Test]
  126. public void SerializeOverloads ()
  127. {
  128. LosFormatter lf1 = new LosFormatter (false, (string) null);
  129. string r1 = SerializeOverloads (lf1, "false, null");
  130. LosFormatter lf2 = new LosFormatter (true, (string) null);
  131. string r2 = SerializeOverloads (lf2, "true, null");
  132. Assert.AreEqual (r1, r2, "r1-r2");
  133. LosFormatter lf3 = new LosFormatter (false, String.Empty);
  134. string r3 = SerializeOverloads (lf3, "false, empty");
  135. Assert.AreEqual (r2, r3, "r2-r3");
  136. // an empty string is not an empty key!
  137. LosFormatter lf4 = new LosFormatter (true, String.Empty);
  138. string r4 = SerializeOverloads (lf4, "true, empty");
  139. Assert.AreEqual (r3, r4, "r3-r4");
  140. byte [] key = new byte [32];
  141. LosFormatter lf5 = new LosFormatter (true, Convert.ToBase64String (key));
  142. string r5 = SerializeOverloads (lf5, "false, b64");
  143. Assert.AreNotEqual (r4, r5, "r4-r5");
  144. }
  145. #if NET_4_0
  146. [Test]
  147. [ExpectedException (typeof (NotSupportedException))]
  148. public void Deserialize_Stream_NonSeekable ()
  149. {
  150. string s1 = "Hello world";
  151. NonSeekableStream ns = new NonSeekableStream ();
  152. LosFormatter lf = new LosFormatter ();
  153. lf.Serialize (ns, s1);
  154. }
  155. #else
  156. [Test] // bug #411115
  157. public void Deserialize_Stream_NonSeekable ()
  158. {
  159. string s1 = "Hello world";
  160. NonSeekableStream ns = new NonSeekableStream ();
  161. LosFormatter lf = new LosFormatter ();
  162. lf.Serialize (ns, s1);
  163. ns.Reset ();
  164. string s2 = lf.Deserialize (ns) as string;
  165. Assert.AreEqual (s1, s2);
  166. }
  167. #endif
  168. [Test] // bug #324526
  169. public void Serialize ()
  170. {
  171. string s = "Hello world";
  172. LosFormatter lf = new LosFormatter ();
  173. StringWriter sw = new StringWriter ();
  174. lf.Serialize (sw, s);
  175. string s1 = sw.ToString ();
  176. Assert.IsNotNull (s1, "#1");
  177. string s2 = lf.Deserialize (s1) as string;
  178. Assert.IsNotNull (s2, "#2");
  179. Assert.AreEqual (s, s2, "#3");
  180. }
  181. [Test]
  182. [Category ("NotWorking")]
  183. public void Serialize_Output ()
  184. {
  185. string s = "Hello world";
  186. LosFormatter lf = new LosFormatter ();
  187. StringWriter sw = new StringWriter ();
  188. lf.Serialize (sw, s);
  189. string s1 = sw.ToString ();
  190. #if NET_2_0
  191. Assert.AreEqual ("/wEFC0hlbGxvIHdvcmxk", s1, "#1");
  192. #else
  193. Assert.AreEqual ("SGVsbG8gd29ybGQ=", s1, "#1");
  194. #endif
  195. string s2 = lf.Deserialize (s1) as string;
  196. Assert.IsNotNull (s2, "#2");
  197. Assert.AreEqual (s, s2, "#3");
  198. }
  199. [Test]
  200. [Category ("NotDotNet")] // MS throws NullReferenceException
  201. public void Serialize_Output_Null ()
  202. {
  203. LosFormatter lf = new LosFormatter ();
  204. try {
  205. lf.Serialize ((TextWriter) null, "test");
  206. Assert.Fail ("#1");
  207. } catch (ArgumentNullException ex) {
  208. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  209. Assert.IsNull (ex.InnerException, "#3");
  210. Assert.IsNotNull (ex.Message, "#4");
  211. Assert.IsNotNull (ex.ParamName, "#5");
  212. Assert.AreEqual ("output", ex.ParamName, "#6");
  213. }
  214. }
  215. [Test]
  216. [Category ("NotWorking")]
  217. public void Serialize_Stream ()
  218. {
  219. string s = "Hello world";
  220. LosFormatter lf = new LosFormatter ();
  221. MemoryStream ms = new MemoryStream ();
  222. lf.Serialize (ms, s);
  223. string s1 = Encoding.UTF8.GetString (ms.GetBuffer (), 0, (int) ms.Length);
  224. #if NET_2_0
  225. Assert.AreEqual ("/wEFC0hlbGxvIHdvcmxk", s1, "#1");
  226. #else
  227. Assert.AreEqual ("SGVsbG8gd29ybGQ=", s1, "#1");
  228. #endif
  229. string s2 = lf.Deserialize (s1) as string;
  230. Assert.IsNotNull (s2, "#2");
  231. Assert.AreEqual (s, s2, "#3");
  232. }
  233. [Test]
  234. public void Serialize_Stream_Null ()
  235. {
  236. LosFormatter lf = new LosFormatter ();
  237. try {
  238. lf.Serialize ((Stream) null, "test");
  239. Assert.Fail ("#1");
  240. } catch (ArgumentNullException ex) {
  241. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  242. Assert.IsNull (ex.InnerException, "#3");
  243. Assert.IsNotNull (ex.Message, "#4");
  244. Assert.IsNotNull (ex.ParamName, "#5");
  245. Assert.AreEqual ("stream", ex.ParamName, "#6");
  246. }
  247. }
  248. [Test]
  249. [Category ("NotWorking")]
  250. public void Serialize_Value_Null ()
  251. {
  252. LosFormatter lf = new LosFormatter ();
  253. MemoryStream ms = new MemoryStream ();
  254. lf.Serialize (ms, null);
  255. string s1 = Encoding.UTF8.GetString (ms.GetBuffer (), 0, (int) ms.Length);
  256. #if NET_2_0
  257. Assert.AreEqual ("/wFk", s1, "#1");
  258. #else
  259. Assert.AreEqual (string.Empty, s1, "#1");
  260. #endif
  261. StringWriter sw = new StringWriter ();
  262. lf.Serialize (sw, null);
  263. string s2 = sw.ToString ();
  264. #if NET_2_0
  265. Assert.AreEqual ("/wFk", s1, "#2");
  266. #else
  267. Assert.AreEqual (string.Empty, s1, "#2");
  268. #endif
  269. }
  270. class NonSeekableStream : MemoryStream
  271. {
  272. private bool canSeek;
  273. public override bool CanSeek {
  274. get { return canSeek; }
  275. }
  276. public override long Length {
  277. get {
  278. if (!CanSeek)
  279. throw new NotSupportedException ();
  280. return base.Length;
  281. }
  282. }
  283. public override long Position {
  284. get{
  285. if (!CanSeek)
  286. throw new NotSupportedException ();
  287. return base.Position;
  288. }
  289. set {
  290. base.Position = value;
  291. }
  292. }
  293. public override long Seek (long offset, SeekOrigin origin)
  294. {
  295. if (!CanSeek)
  296. throw new NotSupportedException ();
  297. return base.Seek (offset, origin);
  298. }
  299. public void Reset ()
  300. {
  301. canSeek = true;
  302. Position = 0;
  303. canSeek = false;
  304. }
  305. }
  306. }
  307. }