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