2005-12-07 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / Test / System.Runtime.Serialization.Formatters.Binary / BinaryFormatterTest.cs
1 //
2 // BinaryFormatterTest.cs - Unit tests for 
3 //      System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using System;
31 using System.IO;
32 using System.Runtime.Serialization;
33 using System.Runtime.Serialization.Formatters;
34 using System.Runtime.Serialization.Formatters.Binary;
35
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Runtime.Serialization.Formatters.Binary {
39
40         [Serializable]
41         public class SerializationTest {
42                 private int integer;
43                 [NonSerialized]
44                 private bool boolean;
45
46                 public SerializationTest (bool b, int i)
47                 {
48                         boolean = b;
49                         integer = i;
50                 }
51
52                 public bool Boolean {
53                         get { return boolean; }
54                         set { boolean = value; }
55                 }
56
57                 public int Integer {
58                         get { return integer; }
59                         set { integer = value; }
60                 }
61         }
62
63         class SurrogateSelector: ISurrogateSelector {
64                 public void ChainSelector (ISurrogateSelector selector)
65                 {
66                 }
67
68                 public ISurrogateSelector GetNextSelector ()
69                 {
70                         return null;
71                 }
72
73                 public ISerializationSurrogate GetSurrogate (Type type, StreamingContext context, out ISurrogateSelector selector)
74                 {
75                         selector = null;
76                         return null;
77                 }
78         }
79
80         [TestFixture]
81         public class BinaryFormatterTest {
82
83                 [Test]
84                 public void Constructor_Default ()
85                 {
86                         BinaryFormatter bf = new BinaryFormatter ();
87 #if NET_2_0
88                         Assert.AreEqual (FormatterAssemblyStyle.Simple, bf.AssemblyFormat, "AssemblyFormat");
89 #else
90                         Assert.AreEqual (FormatterAssemblyStyle.Full, bf.AssemblyFormat, "AssemblyFormat");
91 #endif
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");
97                 }
98
99                 [Test]
100                 public void Constructor ()
101                 {
102                         SurrogateSelector ss = new SurrogateSelector ();
103                         BinaryFormatter bf = new BinaryFormatter (ss, new StreamingContext (StreamingContextStates.CrossMachine));
104 #if NET_2_0
105                         Assert.AreEqual (FormatterAssemblyStyle.Simple, bf.AssemblyFormat, "AssemblyFormat");
106 #else
107                         Assert.AreEqual (FormatterAssemblyStyle.Full, bf.AssemblyFormat, "AssemblyFormat");
108 #endif
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");
114                 }
115
116                 public Stream GetSerializedStream ()
117                 {
118                         SerializationTest test = new SerializationTest (true, Int32.MinValue);
119                         BinaryFormatter bf = new BinaryFormatter ();
120                         MemoryStream ms = new MemoryStream ();
121                         bf.Serialize (ms, test);
122                         ms.Position = 0;
123                         return ms;
124                 }
125
126                 [Test]
127                 public void SerializationRoundtrip ()
128                 {
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");
134                 }
135
136                 [Test]
137                 public void SerializationUnsafeRoundtrip ()
138                 {
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");
144                 }
145         }
146 }