New test.
[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         [Serializable]
81         sealed class ThisObjectReference : IObjectReference
82         {
83                 internal static int Count;
84
85                 internal ThisObjectReference()
86                 {
87                 }
88
89                 public object GetRealObject(StreamingContext context)
90                 {
91                         Count++;
92                         return this;
93                 }
94         }
95
96         [Serializable]
97         sealed class NewObjectReference : IObjectReference
98         {
99                 internal static int Count;
100
101                 internal NewObjectReference()
102                 {
103                 }
104
105                 public object GetRealObject(StreamingContext context)
106                 {
107                         Count++;
108                         return new NewObjectReference();
109                 }
110         }
111
112         [TestFixture]
113         public class BinaryFormatterTest {
114
115                 [Test]
116                 public void Constructor_Default ()
117                 {
118                         BinaryFormatter bf = new BinaryFormatter ();
119 #if NET_2_0
120                         Assert.AreEqual (FormatterAssemblyStyle.Simple, bf.AssemblyFormat, "AssemblyFormat");
121 #else
122                         Assert.AreEqual (FormatterAssemblyStyle.Full, bf.AssemblyFormat, "AssemblyFormat");
123 #endif
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");
129                 }
130
131                 [Test]
132                 public void Constructor ()
133                 {
134                         SurrogateSelector ss = new SurrogateSelector ();
135                         BinaryFormatter bf = new BinaryFormatter (ss, new StreamingContext (StreamingContextStates.CrossMachine));
136 #if NET_2_0
137                         Assert.AreEqual (FormatterAssemblyStyle.Simple, bf.AssemblyFormat, "AssemblyFormat");
138 #else
139                         Assert.AreEqual (FormatterAssemblyStyle.Full, bf.AssemblyFormat, "AssemblyFormat");
140 #endif
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");
146                 }
147
148                 public Stream GetSerializedStream ()
149                 {
150                         SerializationTest test = new SerializationTest (true, Int32.MinValue);
151                         BinaryFormatter bf = new BinaryFormatter ();
152                         MemoryStream ms = new MemoryStream ();
153                         bf.Serialize (ms, test);
154                         ms.Position = 0;
155                         return ms;
156                 }
157
158                 [Test]
159                 public void SerializationRoundtrip ()
160                 {
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");
166                 }
167
168                 [Test]
169                 public void SerializationUnsafeRoundtrip ()
170                 {
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");
176                 }
177                 
178                 [Test]
179                 public void NestedObjectReference ()
180                 {
181                         MemoryStream ms = new MemoryStream();
182                         BinaryFormatter bf = new BinaryFormatter();
183
184                         bf.Serialize(ms, new ThisObjectReference());
185                         bf.Serialize(ms, new NewObjectReference());
186                         ms.Position = 0;
187                         Assert.AreEqual (0, ThisObjectReference.Count, "#1");
188
189                         bf.Deserialize(ms);
190                         Assert.AreEqual (2, ThisObjectReference.Count, "#2");
191                         Assert.AreEqual (0, NewObjectReference.Count, "#3");
192                         try
193                         {
194                                 bf.Deserialize(ms);
195                         }
196                         catch (SerializationException e)
197                         {
198                         }
199                         Assert.AreEqual (101, NewObjectReference.Count, "#4");
200                 }
201
202                 [Test]
203                 public void DateTimeArray ()
204                 {
205                         DateTime [] e = new DateTime [6];
206                         string [] names = new string [6];
207
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;
214
215                         BinaryFormatter bf = new BinaryFormatter ();
216                         MemoryStream ms = new MemoryStream ();
217
218                         bf.Serialize (ms, e);
219
220                         ms.Position = 0;
221                         DateTime [] a = (DateTime []) bf.Deserialize (ms);
222
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]);
226                 }
227         }
228 }