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 sealed class ThisObjectReference : IObjectReference
83 internal static int Count;
85 internal ThisObjectReference()
89 public object GetRealObject(StreamingContext context)
97 sealed class NewObjectReference : IObjectReference
99 internal static int Count;
101 internal NewObjectReference()
105 public object GetRealObject(StreamingContext context)
108 return new NewObjectReference();
113 public class BinaryFormatterTest {
116 public void Constructor_Default ()
118 BinaryFormatter bf = new BinaryFormatter ();
120 Assert.AreEqual (FormatterAssemblyStyle.Simple, bf.AssemblyFormat, "AssemblyFormat");
122 Assert.AreEqual (FormatterAssemblyStyle.Full, bf.AssemblyFormat, "AssemblyFormat");
124 Assert.IsNull (bf.Binder, "Binder");
125 Assert.AreEqual (StreamingContextStates.All, bf.Context.State, "Context");
126 Assert.AreEqual (TypeFilterLevel.Full, bf.FilterLevel, "FilterLevel");
127 Assert.IsNull (bf.SurrogateSelector, "SurrogateSelector");
128 Assert.AreEqual (FormatterTypeStyle.TypesAlways, bf.TypeFormat, "TypeFormat");
132 public void Constructor ()
134 SurrogateSelector ss = new SurrogateSelector ();
135 BinaryFormatter bf = new BinaryFormatter (ss, new StreamingContext (StreamingContextStates.CrossMachine));
137 Assert.AreEqual (FormatterAssemblyStyle.Simple, bf.AssemblyFormat, "AssemblyFormat");
139 Assert.AreEqual (FormatterAssemblyStyle.Full, bf.AssemblyFormat, "AssemblyFormat");
141 Assert.IsNull (bf.Binder, "Binder");
142 Assert.AreEqual (StreamingContextStates.CrossMachine, bf.Context.State, "Context");
143 Assert.AreEqual (TypeFilterLevel.Full, bf.FilterLevel, "FilterLevel");
144 Assert.AreSame (ss, bf.SurrogateSelector, "SurrogateSelector");
145 Assert.AreEqual (FormatterTypeStyle.TypesAlways, bf.TypeFormat, "TypeFormat");
148 public Stream GetSerializedStream ()
150 SerializationTest test = new SerializationTest (true, Int32.MinValue);
151 BinaryFormatter bf = new BinaryFormatter ();
152 MemoryStream ms = new MemoryStream ();
153 bf.Serialize (ms, test);
159 public void SerializationRoundtrip ()
161 Stream s = GetSerializedStream ();
162 BinaryFormatter bf = new BinaryFormatter ();
163 SerializationTest clone = (SerializationTest) bf.Deserialize (s);
164 Assert.AreEqual (Int32.MinValue, clone.Integer, "Integer");
165 Assert.IsFalse (clone.Boolean, "Boolean");
169 public void SerializationUnsafeRoundtrip ()
171 Stream s = GetSerializedStream ();
172 BinaryFormatter bf = new BinaryFormatter ();
173 SerializationTest clone = (SerializationTest) bf.UnsafeDeserialize (s, null);
174 Assert.AreEqual (Int32.MinValue, clone.Integer, "Integer");
175 Assert.IsFalse (clone.Boolean, "Boolean");
179 public void NestedObjectReference ()
181 MemoryStream ms = new MemoryStream();
182 BinaryFormatter bf = new BinaryFormatter();
184 bf.Serialize(ms, new ThisObjectReference());
185 bf.Serialize(ms, new NewObjectReference());
187 Assert.AreEqual (0, ThisObjectReference.Count, "#1");
190 Assert.AreEqual (2, ThisObjectReference.Count, "#2");
191 Assert.AreEqual (0, NewObjectReference.Count, "#3");
196 catch (SerializationException e)
199 Assert.AreEqual (101, NewObjectReference.Count, "#4");
203 public void DateTimeArray ()
205 DateTime [] e = new DateTime [6];
206 string [] names = new string [6];
208 names [0] = "Today"; e [0] = DateTime.Today;
209 names [1] = "Min"; e [1] = DateTime.MinValue;
210 names [2] = "Max"; e [2] = DateTime.MaxValue;
211 names [3] = "BiCent"; e [3] = new DateTime (1976, 07, 04);
212 names [4] = "Now"; e [4] = DateTime.Now;
213 names [5] = "UtcNow"; e [5] = DateTime.UtcNow;
215 BinaryFormatter bf = new BinaryFormatter ();
216 MemoryStream ms = new MemoryStream ();
218 bf.Serialize (ms, e);
221 DateTime [] a = (DateTime []) bf.Deserialize (ms);
223 Assert.AreEqual (e.Length, a.Length);
224 for (int i = 0; i < e.Length; ++i)
225 Assert.AreEqual (e [i], a [i], names [i]);