2 // LosFormatterTest.cs - Unit tests for System.Web.UI.LosFormatter
5 // Gert Driesen <drieseng@users.sourceforge.net>
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2007 Gert Driesen
9 // Copyright (C) 2010 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
36 using NUnit.Framework;
38 namespace MonoTests.System.Web.UI
41 public class LosFormatterTest
43 static byte [] Empty = new byte [0];
45 string Serialize (LosFormatter lf, object value)
47 StringWriter sw = new StringWriter ();
48 lf.Serialize (sw, value);
49 return sw.ToString ();
52 object Deserialize (LosFormatter lf, string serializedData)
54 return lf.Deserialize (serializedData);
57 string NoKeyRoundTrip (LosFormatter lf, string assertionMessage)
59 string serializedData = Serialize (lf, "Mono");
60 Assert.AreEqual ("Mono", (string) Deserialize (lf, serializedData), assertionMessage);
61 return serializedData;
65 public void Ctor_BoolByteArray ()
67 LosFormatter lf1 = new LosFormatter (false, (byte []) null);
68 string expected = NoKeyRoundTrip (lf1, "false, null");
70 LosFormatter lf2 = new LosFormatter (true, (byte []) null);
71 Assert.AreEqual (expected, NoKeyRoundTrip (lf2, "true, null"), "2");
73 LosFormatter lf3 = new LosFormatter (false, Empty);
74 Assert.AreEqual (expected, NoKeyRoundTrip (lf3, "false, empty"), "3");
76 // an empty key is still a key - a signature is appended
77 LosFormatter lf4 = new LosFormatter (true, Empty);
78 string signed = NoKeyRoundTrip (lf4, "true, empty");
79 Assert.AreNotEqual (expected, signed, "4");
81 byte [] data = Convert.FromBase64String (expected);
82 byte [] signed_data = Convert.FromBase64String (signed);
83 Assert.IsTrue (BitConverter.ToString (signed_data).StartsWith (BitConverter.ToString (data)), "4 / same data");
85 // 32 bytes == 256 bits -> match HMACSHA256 as default
86 Assert.AreEqual (32, signed_data.Length - data.Length, "signature length");
88 // 20 bytes == 160 bits -> match HMACSHA1 as default
89 Assert.AreEqual (20, signed_data.Length - data.Length, "signature length");
94 public void Ctor_BoolString ()
96 LosFormatter lf1 = new LosFormatter (false, (string) null);
97 string expected = NoKeyRoundTrip (lf1, "false, null");
99 LosFormatter lf2 = new LosFormatter (true, (string) null);
100 Assert.AreEqual (expected, NoKeyRoundTrip (lf2, "true, null"), "2");
102 LosFormatter lf3 = new LosFormatter (false, String.Empty);
103 Assert.AreEqual (expected, NoKeyRoundTrip (lf3, "false, empty"), "3");
105 // an empty string is not an empty key!
106 LosFormatter lf4 = new LosFormatter (true, String.Empty);
107 Assert.AreEqual (expected, NoKeyRoundTrip (lf4, "true, empty"), "4");
109 byte [] key = new byte [32];
110 LosFormatter lf5 = new LosFormatter (true, Convert.ToBase64String (key));
111 string signed = NoKeyRoundTrip (lf5, "true, b64");
112 Assert.AreNotEqual (expected, signed, "5");
114 byte [] data = Convert.FromBase64String (expected);
115 byte [] signed_data = Convert.FromBase64String (signed);
116 Assert.IsTrue (BitConverter.ToString (signed_data).StartsWith (BitConverter.ToString (data)), "5 / same data");
118 // 32 bytes == 256 bits -> match HMACSHA256 as default
119 Assert.AreEqual (32, signed_data.Length - data.Length, "signature length");
121 // 20 bytes == 160 bits -> match HMACSHA1 as default
122 Assert.AreEqual (20, signed_data.Length - data.Length, "signature length");
124 LosFormatter lf6 = new LosFormatter (true, "string"); // bug #649551
125 signed = NoKeyRoundTrip (lf6, "true, plain");
126 Assert.AreNotEqual (expected, signed, "6");
129 string SerializeOverloads (LosFormatter lf, string message)
132 using (MemoryStream ms = new MemoryStream ()) {
133 lf.Serialize (ms, String.Empty);
134 stream_ser = Convert.ToBase64String (ms.ToArray ());
138 using (MemoryStream ms = new MemoryStream ()) {
139 using (TextWriter tw = new StreamWriter (ms)) {
140 lf.Serialize (tw, String.Empty);
142 tw_ser = Convert.ToBase64String (ms.ToArray ());
145 Assert.AreEqual (stream_ser, tw_ser, message);
150 public void SerializeOverloads ()
152 LosFormatter lf1 = new LosFormatter (false, (string) null);
153 string r1 = SerializeOverloads (lf1, "false, null");
155 LosFormatter lf2 = new LosFormatter (true, (string) null);
156 string r2 = SerializeOverloads (lf2, "true, null");
157 Assert.AreEqual (r1, r2, "r1-r2");
159 LosFormatter lf3 = new LosFormatter (false, String.Empty);
160 string r3 = SerializeOverloads (lf3, "false, empty");
161 Assert.AreEqual (r2, r3, "r2-r3");
163 // an empty string is not an empty key!
164 LosFormatter lf4 = new LosFormatter (true, String.Empty);
165 string r4 = SerializeOverloads (lf4, "true, empty");
166 Assert.AreEqual (r3, r4, "r3-r4");
168 byte [] key = new byte [32];
169 LosFormatter lf5 = new LosFormatter (true, Convert.ToBase64String (key));
170 string r5 = SerializeOverloads (lf5, "false, b64");
171 Assert.AreNotEqual (r4, r5, "r4-r5");
176 [ExpectedException (typeof (NotSupportedException))]
177 public void Deserialize_Stream_NonSeekable ()
179 string s1 = "Hello world";
180 NonSeekableStream ns = new NonSeekableStream ();
181 LosFormatter lf = new LosFormatter ();
182 lf.Serialize (ns, s1);
185 [Test] // bug #411115
186 public void Deserialize_Stream_NonSeekable ()
188 string s1 = "Hello world";
189 NonSeekableStream ns = new NonSeekableStream ();
190 LosFormatter lf = new LosFormatter ();
191 lf.Serialize (ns, s1);
193 string s2 = lf.Deserialize (ns) as string;
194 Assert.AreEqual (s1, s2);
197 [Test] // bug #324526
198 public void Serialize ()
200 string s = "Hello world";
201 LosFormatter lf = new LosFormatter ();
202 StringWriter sw = new StringWriter ();
203 lf.Serialize (sw, s);
204 string s1 = sw.ToString ();
205 Assert.IsNotNull (s1, "#1");
206 string s2 = lf.Deserialize (s1) as string;
207 Assert.IsNotNull (s2, "#2");
208 Assert.AreEqual (s, s2, "#3");
212 [Category ("NotWorking")]
213 public void Serialize_Output ()
215 string s = "Hello world";
216 LosFormatter lf = new LosFormatter ();
217 StringWriter sw = new StringWriter ();
218 lf.Serialize (sw, s);
219 string s1 = sw.ToString ();
220 Assert.AreEqual ("/wEFC0hlbGxvIHdvcmxk", s1, "#1");
221 string s2 = lf.Deserialize (s1) as string;
222 Assert.IsNotNull (s2, "#2");
223 Assert.AreEqual (s, s2, "#3");
227 [Category ("NotDotNet")] // MS throws NullReferenceException
228 public void Serialize_Output_Null ()
230 LosFormatter lf = new LosFormatter ();
232 lf.Serialize ((TextWriter) null, "test");
234 } catch (ArgumentNullException ex) {
235 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
236 Assert.IsNull (ex.InnerException, "#3");
237 Assert.IsNotNull (ex.Message, "#4");
238 Assert.IsNotNull (ex.ParamName, "#5");
239 Assert.AreEqual ("output", ex.ParamName, "#6");
244 [Category ("NotWorking")]
245 public void Serialize_Stream ()
247 string s = "Hello world";
248 LosFormatter lf = new LosFormatter ();
249 MemoryStream ms = new MemoryStream ();
250 lf.Serialize (ms, s);
251 string s1 = Encoding.UTF8.GetString (ms.GetBuffer (), 0, (int) ms.Length);
252 Assert.AreEqual ("/wEFC0hlbGxvIHdvcmxk", s1, "#1");
253 string s2 = lf.Deserialize (s1) as string;
254 Assert.IsNotNull (s2, "#2");
255 Assert.AreEqual (s, s2, "#3");
259 public void Serialize_Stream_Null ()
261 LosFormatter lf = new LosFormatter ();
263 lf.Serialize ((Stream) null, "test");
265 } catch (ArgumentNullException ex) {
266 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
267 Assert.IsNull (ex.InnerException, "#3");
268 Assert.IsNotNull (ex.Message, "#4");
269 Assert.IsNotNull (ex.ParamName, "#5");
270 Assert.AreEqual ("stream", ex.ParamName, "#6");
275 [Category ("NotWorking")]
276 public void Serialize_Value_Null ()
278 LosFormatter lf = new LosFormatter ();
279 MemoryStream ms = new MemoryStream ();
280 lf.Serialize (ms, null);
281 string s1 = Encoding.UTF8.GetString (ms.GetBuffer (), 0, (int) ms.Length);
282 Assert.AreEqual ("/wFk", s1, "#1");
284 StringWriter sw = new StringWriter ();
285 lf.Serialize (sw, null);
286 string s2 = sw.ToString ();
287 Assert.AreEqual ("/wFk", s1, "#2");
290 class NonSeekableStream : MemoryStream
292 private bool canSeek;
294 public override bool CanSeek {
295 get { return canSeek; }
298 public override long Length {
301 throw new NotSupportedException ();
306 public override long Position {
309 throw new NotSupportedException ();
310 return base.Position;
313 base.Position = value;
317 public override long Seek (long offset, SeekOrigin origin)
320 throw new NotSupportedException ();
321 return base.Seek (offset, origin);