[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / System.Web / Test / System.Web.UI / LosFormatterTest.cs
1 //
2 // LosFormatterTest.cs - Unit tests for System.Web.UI.LosFormatter
3 //
4 // Authors:
5 //      Gert Driesen  <drieseng@users.sourceforge.net>
6 //      Sebastien Pouliot  <sebastien@ximian.com>
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
31 using System;
32 using System.IO;
33 using System.Text;
34 using System.Web.UI;
35
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Web.UI
39 {
40         [TestFixture]
41         public class LosFormatterTest
42         {
43                 static byte [] Empty = new byte [0];
44
45                 string Serialize (LosFormatter lf, object value)
46                 {
47                         StringWriter sw = new StringWriter ();
48                         lf.Serialize (sw, value);
49                         return sw.ToString ();
50                 }
51
52                 object Deserialize (LosFormatter lf, string serializedData)
53                 {
54                         return lf.Deserialize (serializedData);
55                 }
56
57                 string NoKeyRoundTrip (LosFormatter lf, string assertionMessage)
58                 {
59                         string serializedData = Serialize (lf, "Mono");
60                         Assert.AreEqual ("Mono", (string) Deserialize (lf, serializedData), assertionMessage);
61                         return serializedData;
62                 }
63
64                 [Test]
65                 public void Ctor_BoolByteArray ()
66                 {
67                         LosFormatter lf1 = new LosFormatter (false, (byte []) null);
68                         string expected = NoKeyRoundTrip (lf1, "false, null");
69
70                         LosFormatter lf2 = new LosFormatter (true, (byte []) null);
71                         Assert.AreEqual (expected, NoKeyRoundTrip (lf2, "true, null"), "2");
72
73                         LosFormatter lf3 = new LosFormatter (false, Empty);
74                         Assert.AreEqual (expected, NoKeyRoundTrip (lf3, "false, empty"), "3");
75
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");
80
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");
84 #if NET_4_0
85                         // 32 bytes == 256 bits -> match HMACSHA256 as default
86                         Assert.AreEqual (32, signed_data.Length - data.Length, "signature length");
87 #else
88                         // 20 bytes == 160 bits -> match HMACSHA1 as default
89                         Assert.AreEqual (20, signed_data.Length - data.Length, "signature length");
90 #endif
91                 }
92
93                 [Test]
94                 public void Ctor_BoolString ()
95                 {
96                         LosFormatter lf1 = new LosFormatter (false, (string) null);
97                         string expected = NoKeyRoundTrip (lf1, "false, null");
98
99                         LosFormatter lf2 = new LosFormatter (true, (string) null);
100                         Assert.AreEqual (expected, NoKeyRoundTrip (lf2, "true, null"), "2");
101
102                         LosFormatter lf3 = new LosFormatter (false, String.Empty);
103                         Assert.AreEqual (expected, NoKeyRoundTrip (lf3, "false, empty"), "3");
104
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");
108
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");
113
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");
117 #if NET_4_0
118                         // 32 bytes == 256 bits -> match HMACSHA256 as default
119                         Assert.AreEqual (32, signed_data.Length - data.Length, "signature length");
120 #else
121                         // 20 bytes == 160 bits -> match HMACSHA1 as default
122                         Assert.AreEqual (20, signed_data.Length - data.Length, "signature length");
123 #endif
124                         LosFormatter lf6 = new LosFormatter (true, "string"); // bug #649551
125                         signed = NoKeyRoundTrip (lf6, "true, plain");
126                         Assert.AreNotEqual (expected, signed, "6");
127                 }
128
129                 string SerializeOverloads (LosFormatter lf, string message)
130                 {
131                         string stream_ser;
132                         using (MemoryStream ms = new MemoryStream ()) {
133                                 lf.Serialize (ms, String.Empty);
134                                 stream_ser = Convert.ToBase64String (ms.ToArray ());
135                         }
136
137                         string tw_ser;
138                         using (MemoryStream ms = new MemoryStream ()) {
139                                 using (TextWriter tw = new StreamWriter (ms)) {
140                                         lf.Serialize (tw, String.Empty);
141                                 }
142                                 tw_ser = Convert.ToBase64String (ms.ToArray ());
143                         }
144
145                         Assert.AreEqual (stream_ser, tw_ser, message);
146                         return stream_ser;
147                 }
148
149                 [Test]
150                 public void SerializeOverloads ()
151                 {
152                         LosFormatter lf1 = new LosFormatter (false, (string) null);
153                         string r1 = SerializeOverloads (lf1, "false, null");
154
155                         LosFormatter lf2 = new LosFormatter (true, (string) null);
156                         string r2 = SerializeOverloads (lf2, "true, null");
157                         Assert.AreEqual (r1, r2, "r1-r2");
158
159                         LosFormatter lf3 = new LosFormatter (false, String.Empty);
160                         string r3 = SerializeOverloads (lf3, "false, empty");
161                         Assert.AreEqual (r2, r3, "r2-r3");
162
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");
167
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");
172                 }
173
174 #if NET_4_0
175                 [Test]
176                 [ExpectedException (typeof (NotSupportedException))]
177                 public void Deserialize_Stream_NonSeekable ()
178                 {
179                         string s1 = "Hello world";
180                         NonSeekableStream ns = new NonSeekableStream ();
181                         LosFormatter lf = new LosFormatter ();
182                         lf.Serialize (ns, s1);
183                 }
184 #else
185                 [Test] // bug #411115
186                 public void Deserialize_Stream_NonSeekable ()
187                 {
188                         string s1 = "Hello world";
189                         NonSeekableStream ns = new NonSeekableStream ();
190                         LosFormatter lf = new LosFormatter ();
191                         lf.Serialize (ns, s1);
192                         ns.Reset ();
193                         string s2 = lf.Deserialize (ns) as string;
194                         Assert.AreEqual (s1, s2);
195                 }
196 #endif
197                 [Test] // bug #324526
198                 public void Serialize ()
199                 {
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");
209                 }
210
211                 [Test]
212                 [Category ("NotWorking")]
213                 public void Serialize_Output ()
214                 {
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");
224                 }
225
226                 [Test]
227                 [Category ("NotDotNet")] // MS throws NullReferenceException
228                 public void Serialize_Output_Null ()
229                 {
230                         LosFormatter lf = new LosFormatter ();
231                         try {
232                                 lf.Serialize ((TextWriter) null, "test");
233                                 Assert.Fail ("#1");
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");
240                         }
241                 }
242
243                 [Test]
244                 [Category ("NotWorking")]
245                 public void Serialize_Stream ()
246                 {
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");
256                 }
257
258                 [Test]
259                 public void Serialize_Stream_Null ()
260                 {
261                         LosFormatter lf = new LosFormatter ();
262                         try {
263                                 lf.Serialize ((Stream) null, "test");
264                                 Assert.Fail ("#1");
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");
271                         }
272                 }
273
274                 [Test]
275                 [Category ("NotWorking")]
276                 public void Serialize_Value_Null ()
277                 {
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");
283
284                         StringWriter sw = new StringWriter ();
285                         lf.Serialize (sw, null);
286                         string s2 = sw.ToString ();
287                         Assert.AreEqual ("/wFk", s1, "#2");
288                 }
289
290                 class NonSeekableStream : MemoryStream
291                 {
292                         private bool canSeek;
293
294                         public override bool CanSeek {
295                                 get { return canSeek; }
296                         }
297
298                         public override long Length {
299                                 get {
300                                         if (!CanSeek)
301                                                 throw new NotSupportedException ();
302                                         return base.Length;
303                                 }
304                         }
305
306                         public override long Position {
307                                 get{
308                                         if (!CanSeek)
309                                                 throw new NotSupportedException ();
310                                         return base.Position;
311                                 }
312                                 set {
313                                         base.Position = value;
314                                 }
315                         }
316
317                         public override long Seek (long offset, SeekOrigin origin)
318                         {
319                                 if (!CanSeek)
320                                         throw new NotSupportedException ();
321                                 return base.Seek (offset, origin);
322                         }
323
324                         public void Reset ()
325                         {
326                                 canSeek = true;
327                                 Position = 0;
328                                 canSeek = false;
329                         }
330                 }
331         }
332 }