2 // BinaryFormatterTest.cs - Unit tests for
3 // System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Runtime.Serialization;
33 using System.Runtime.Serialization.Formatters;
34 using System.Runtime.Serialization.Formatters.Binary;
36 using NUnit.Framework;
38 namespace MonoTests.System.Runtime.Serialization.Formatters.Binary {
41 public class SerializationTest {
46 public SerializationTest (bool b, int i)
53 get { return boolean; }
54 set { boolean = value; }
58 get { return integer; }
59 set { integer = value; }
63 class SurrogateSelector: ISurrogateSelector {
64 public void ChainSelector (ISurrogateSelector selector)
68 public ISurrogateSelector GetNextSelector ()
73 public ISerializationSurrogate GetSurrogate (Type type, StreamingContext context, out ISurrogateSelector selector)
81 public class BinaryFormatterTest {
84 public void Constructor_Default ()
86 BinaryFormatter bf = new BinaryFormatter ();
88 Assert.AreEqual (FormatterAssemblyStyle.Simple, bf.AssemblyFormat, "AssemblyFormat");
90 Assert.AreEqual (FormatterAssemblyStyle.Full, bf.AssemblyFormat, "AssemblyFormat");
92 Assert.IsNull (bf.Binder, "Binder");
93 Assert.AreEqual (StreamingContextStates.All, bf.Context.State, "Context");
94 Assert.AreEqual (TypeFilterLevel.Full, bf.FilterLevel, "FilterLevel");
95 Assert.IsNull (bf.SurrogateSelector, "SurrogateSelector");
96 Assert.AreEqual (FormatterTypeStyle.TypesAlways, bf.TypeFormat, "TypeFormat");
100 public void Constructor ()
102 SurrogateSelector ss = new SurrogateSelector ();
103 BinaryFormatter bf = new BinaryFormatter (ss, new StreamingContext (StreamingContextStates.CrossMachine));
105 Assert.AreEqual (FormatterAssemblyStyle.Simple, bf.AssemblyFormat, "AssemblyFormat");
107 Assert.AreEqual (FormatterAssemblyStyle.Full, bf.AssemblyFormat, "AssemblyFormat");
109 Assert.IsNull (bf.Binder, "Binder");
110 Assert.AreEqual (StreamingContextStates.CrossMachine, bf.Context.State, "Context");
111 Assert.AreEqual (TypeFilterLevel.Full, bf.FilterLevel, "FilterLevel");
112 Assert.AreSame (ss, bf.SurrogateSelector, "SurrogateSelector");
113 Assert.AreEqual (FormatterTypeStyle.TypesAlways, bf.TypeFormat, "TypeFormat");
116 public Stream GetSerializedStream ()
118 SerializationTest test = new SerializationTest (true, Int32.MinValue);
119 BinaryFormatter bf = new BinaryFormatter ();
120 MemoryStream ms = new MemoryStream ();
121 bf.Serialize (ms, test);
127 public void SerializationRoundtrip ()
129 Stream s = GetSerializedStream ();
130 BinaryFormatter bf = new BinaryFormatter ();
131 SerializationTest clone = (SerializationTest) bf.Deserialize (s);
132 Assert.AreEqual (Int32.MinValue, clone.Integer, "Integer");
133 Assert.IsFalse (clone.Boolean, "Boolean");
137 public void SerializationUnsafeRoundtrip ()
139 Stream s = GetSerializedStream ();
140 BinaryFormatter bf = new BinaryFormatter ();
141 SerializationTest clone = (SerializationTest) bf.UnsafeDeserialize (s, null);
142 Assert.AreEqual (Int32.MinValue, clone.Integer, "Integer");
143 Assert.IsFalse (clone.Boolean, "Boolean");