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
47 public SerializationTest (bool b, int i)
54 get { return boolean; }
55 set { boolean = value; }
59 get { return integer; }
60 set { integer = value; }
64 class SurrogateSelector: ISurrogateSelector
66 public void ChainSelector (ISurrogateSelector selector)
70 public ISurrogateSelector GetNextSelector ()
75 public ISerializationSurrogate GetSurrogate (Type type, StreamingContext context, out ISurrogateSelector selector)
83 sealed class ThisObjectReference : IObjectReference
85 internal static int Count;
87 internal ThisObjectReference()
91 public object GetRealObject(StreamingContext context)
99 sealed class NewObjectReference : IObjectReference
101 internal static int Count;
103 internal NewObjectReference()
107 public object GetRealObject(StreamingContext context)
110 return new NewObjectReference();
117 private int privateFoo;
118 protected int familyFoo;
119 protected internal int familyANDAssemFoo;
120 public int publicFoo;
121 internal int assemblyFoo;
123 public int PrivateFoo {
124 get { return privateFoo; }
127 public int FamilyFoo {
128 get { return familyFoo; }
131 public int FamilyANDAssemFoo {
132 get { return familyANDAssemFoo; }
135 public int PublicFoo {
136 get { return publicFoo; }
139 public int AssemblyFoo {
140 get { return assemblyFoo; }
143 public virtual void Init ()
147 familyANDAssemFoo = 4;
156 private int privateBar;
157 protected int familyBar;
158 protected internal int familyANDAssemBar;
159 public int publicBar;
160 internal int assemblyBar;
162 public int PrivateBar {
163 get { return privateBar; }
166 public int FamilyBar {
167 get { return familyBar; }
170 public int FamilyANDAssemBar {
171 get { return familyANDAssemBar; }
174 public int PublicBar {
175 get { return publicBar; }
178 public int AssemblyBar {
179 get { return assemblyBar; }
182 public override void Init ()
186 familyANDAssemBar = 4;
195 public class BinaryFormatterTest
198 public void Constructor_Default ()
200 BinaryFormatter bf = new BinaryFormatter ();
202 Assert.AreEqual (FormatterAssemblyStyle.Simple, bf.AssemblyFormat, "AssemblyFormat");
204 Assert.AreEqual (FormatterAssemblyStyle.Full, bf.AssemblyFormat, "AssemblyFormat");
206 Assert.IsNull (bf.Binder, "Binder");
207 Assert.AreEqual (StreamingContextStates.All, bf.Context.State, "Context");
208 Assert.AreEqual (TypeFilterLevel.Full, bf.FilterLevel, "FilterLevel");
209 Assert.IsNull (bf.SurrogateSelector, "SurrogateSelector");
210 Assert.AreEqual (FormatterTypeStyle.TypesAlways, bf.TypeFormat, "TypeFormat");
214 public void Constructor ()
216 SurrogateSelector ss = new SurrogateSelector ();
217 BinaryFormatter bf = new BinaryFormatter (ss, new StreamingContext (StreamingContextStates.CrossMachine));
219 Assert.AreEqual (FormatterAssemblyStyle.Simple, bf.AssemblyFormat, "AssemblyFormat");
221 Assert.AreEqual (FormatterAssemblyStyle.Full, bf.AssemblyFormat, "AssemblyFormat");
223 Assert.IsNull (bf.Binder, "Binder");
224 Assert.AreEqual (StreamingContextStates.CrossMachine, bf.Context.State, "Context");
225 Assert.AreEqual (TypeFilterLevel.Full, bf.FilterLevel, "FilterLevel");
226 Assert.AreSame (ss, bf.SurrogateSelector, "SurrogateSelector");
227 Assert.AreEqual (FormatterTypeStyle.TypesAlways, bf.TypeFormat, "TypeFormat");
231 public void Inheritance ()
233 MemoryStream ms = new MemoryStream ();
234 BinaryFormatter bf = new BinaryFormatter ();
236 Bar bar = new Bar ();
239 bf.Serialize (ms, bar);
242 Bar clone = (Bar) bf.Deserialize (ms);
243 Assert.AreEqual (bar.PrivateBar, clone.PrivateBar, "#1");
244 Assert.AreEqual (bar.FamilyBar, clone.FamilyBar, "#2");
245 Assert.AreEqual (bar.FamilyANDAssemBar, clone.FamilyANDAssemBar, "#3");
246 Assert.AreEqual (bar.PublicBar, clone.PublicBar, "#4");
247 Assert.AreEqual (bar.AssemblyBar, clone.AssemblyBar, "#5");
248 Assert.AreEqual (bar.PrivateFoo, clone.PrivateFoo, "#6");
249 Assert.AreEqual (bar.FamilyFoo, clone.FamilyFoo, "#7");
250 Assert.AreEqual (bar.FamilyANDAssemFoo, clone.FamilyANDAssemFoo, "#8");
251 Assert.AreEqual (bar.PublicFoo, clone.PublicFoo, "#9");
252 Assert.AreEqual (bar.AssemblyFoo, clone.AssemblyFoo, "#10");
256 public void SerializationRoundtrip ()
258 Stream s = GetSerializedStream ();
259 BinaryFormatter bf = new BinaryFormatter ();
260 SerializationTest clone = (SerializationTest) bf.Deserialize (s);
261 Assert.AreEqual (Int32.MinValue, clone.Integer, "Integer");
262 Assert.IsFalse (clone.Boolean, "Boolean");
266 public void SerializationUnsafeRoundtrip ()
268 Stream s = GetSerializedStream ();
269 BinaryFormatter bf = new BinaryFormatter ();
270 SerializationTest clone = (SerializationTest) bf.UnsafeDeserialize (s, null);
271 Assert.AreEqual (Int32.MinValue, clone.Integer, "Integer");
272 Assert.IsFalse (clone.Boolean, "Boolean");
276 public void NestedObjectReference ()
278 MemoryStream ms = new MemoryStream();
279 BinaryFormatter bf = new BinaryFormatter();
281 bf.Serialize(ms, new ThisObjectReference());
282 bf.Serialize(ms, new NewObjectReference());
284 Assert.AreEqual (0, ThisObjectReference.Count, "#1");
287 Assert.AreEqual (2, ThisObjectReference.Count, "#2");
288 Assert.AreEqual (0, NewObjectReference.Count, "#3");
291 } catch (SerializationException) {
293 Assert.AreEqual (101, NewObjectReference.Count, "#4");
297 public void DateTimeArray ()
299 DateTime [] e = new DateTime [6];
300 string [] names = new string [6];
302 names [0] = "Today"; e [0] = DateTime.Today;
303 names [1] = "Min"; e [1] = DateTime.MinValue;
304 names [2] = "Max"; e [2] = DateTime.MaxValue;
305 names [3] = "BiCent"; e [3] = new DateTime (1976, 07, 04);
306 names [4] = "Now"; e [4] = DateTime.Now;
307 names [5] = "UtcNow"; e [5] = DateTime.UtcNow;
309 BinaryFormatter bf = new BinaryFormatter ();
310 MemoryStream ms = new MemoryStream ();
312 bf.Serialize (ms, e);
315 DateTime [] a = (DateTime []) bf.Deserialize (ms);
317 Assert.AreEqual (e.Length, a.Length);
318 for (int i = 0; i < e.Length; ++i)
319 Assert.AreEqual (e [i], a [i], names [i]);
322 public Stream GetSerializedStream ()
324 SerializationTest test = new SerializationTest (true, Int32.MinValue);
325 BinaryFormatter bf = new BinaryFormatter ();
326 MemoryStream ms = new MemoryStream ();
327 bf.Serialize (ms, test);