Normalize line endings.
[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         {
43                 private int integer;
44                 [NonSerialized]
45                 private bool boolean;
46
47                 public SerializationTest (bool b, int i)
48                 {
49                         boolean = b;
50                         integer = i;
51                 }
52
53                 public bool Boolean {
54                         get { return boolean; }
55                         set { boolean = value; }
56                 }
57
58                 public int Integer {
59                         get { return integer; }
60                         set { integer = value; }
61                 }
62         }
63
64         class SurrogateSelector: ISurrogateSelector
65         {
66                 public void ChainSelector (ISurrogateSelector selector)
67                 {
68                 }
69
70                 public ISurrogateSelector GetNextSelector ()
71                 {
72                         return null;
73                 }
74
75                 public ISerializationSurrogate GetSurrogate (Type type, StreamingContext context, out ISurrogateSelector selector)
76                 {
77                         selector = null;
78                         return null;
79                 }
80         }
81
82         [Serializable]
83         sealed class ThisObjectReference : IObjectReference
84         {
85                 internal static int Count;
86
87                 internal ThisObjectReference()
88                 {
89                 }
90
91                 public object GetRealObject(StreamingContext context)
92                 {
93                         Count++;
94                         return this;
95                 }
96         }
97
98         [Serializable]
99         sealed class NewObjectReference : IObjectReference
100         {
101                 internal static int Count;
102
103                 internal NewObjectReference()
104                 {
105                 }
106
107                 public object GetRealObject(StreamingContext context)
108                 {
109                         Count++;
110                         return new NewObjectReference();
111                 }
112         }
113
114         [Serializable]
115         class Foo
116         {
117                 private int privateFoo;
118                 protected int familyFoo;
119                 protected internal int familyANDAssemFoo;
120                 public int publicFoo;
121                 internal int assemblyFoo;
122
123                 public int PrivateFoo {
124                         get { return privateFoo; }
125                 }
126
127                 public int FamilyFoo {
128                         get { return familyFoo; }
129                 }
130
131                 public int FamilyANDAssemFoo {
132                         get { return familyANDAssemFoo; }
133                 }
134
135                 public int PublicFoo {
136                         get { return publicFoo; }
137                 }
138
139                 public int AssemblyFoo {
140                         get { return assemblyFoo; }
141                 }
142
143                 public virtual void Init ()
144                 {
145                         privateFoo = 1;
146                         familyFoo = 2;
147                         familyANDAssemFoo = 4;
148                         publicFoo = 8;
149                         assemblyFoo = 16;
150                 }
151         }
152
153         [Serializable]
154         class Bar : Foo
155         {
156                 private int privateBar;
157                 protected int familyBar;
158                 protected internal int familyANDAssemBar;
159                 public int publicBar;
160                 internal int assemblyBar;
161
162                 public int PrivateBar {
163                         get { return privateBar; }
164                 }
165
166                 public int FamilyBar {
167                         get { return familyBar; }
168                 }
169
170                 public int FamilyANDAssemBar {
171                         get { return familyANDAssemBar; }
172                 }
173
174                 public int PublicBar {
175                         get { return publicBar; }
176                 }
177
178                 public int AssemblyBar {
179                         get { return assemblyBar; }
180                 }
181
182                 public override void Init ()
183                 {
184                         privateBar = 1;
185                         familyBar = 2;
186                         familyANDAssemBar = 4;
187                         publicBar = 8;
188                         assemblyBar = 16;
189
190                         base.Init ();
191                 }
192         }
193
194         [TestFixture]
195         public class BinaryFormatterTest
196         {
197                 [Test]
198                 public void Constructor_Default ()
199                 {
200                         BinaryFormatter bf = new BinaryFormatter ();
201 #if NET_2_0
202                         Assert.AreEqual (FormatterAssemblyStyle.Simple, bf.AssemblyFormat, "AssemblyFormat");
203 #else
204                         Assert.AreEqual (FormatterAssemblyStyle.Full, bf.AssemblyFormat, "AssemblyFormat");
205 #endif
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");
211                 }
212
213                 [Test]
214                 public void Constructor ()
215                 {
216                         SurrogateSelector ss = new SurrogateSelector ();
217                         BinaryFormatter bf = new BinaryFormatter (ss, new StreamingContext (StreamingContextStates.CrossMachine));
218 #if NET_2_0
219                         Assert.AreEqual (FormatterAssemblyStyle.Simple, bf.AssemblyFormat, "AssemblyFormat");
220 #else
221                         Assert.AreEqual (FormatterAssemblyStyle.Full, bf.AssemblyFormat, "AssemblyFormat");
222 #endif
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");
228                 }
229
230                 [Test]
231                 public void Inheritance ()
232                 {
233                         MemoryStream ms = new MemoryStream ();
234                         BinaryFormatter bf = new BinaryFormatter ();
235
236                         Bar bar = new Bar ();
237                         bar.Init ();
238
239                         bf.Serialize (ms, bar);
240                         ms.Position = 0;
241
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");
253                 }
254
255                 [Test]
256                 public void SerializationRoundtrip ()
257                 {
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");
263                 }
264
265                 [Test]
266                 public void SerializationUnsafeRoundtrip ()
267                 {
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");
273                 }
274                 
275                 [Test]
276                 public void NestedObjectReference ()
277                 {
278                         MemoryStream ms = new MemoryStream();
279                         BinaryFormatter bf = new BinaryFormatter();
280
281                         bf.Serialize(ms, new ThisObjectReference());
282                         bf.Serialize(ms, new NewObjectReference());
283                         ms.Position = 0;
284                         Assert.AreEqual (0, ThisObjectReference.Count, "#1");
285
286                         bf.Deserialize(ms);
287                         Assert.AreEqual (2, ThisObjectReference.Count, "#2");
288                         Assert.AreEqual (0, NewObjectReference.Count, "#3");
289                         try {
290                                 bf.Deserialize(ms);
291                         } catch (SerializationException) {
292                         }
293                         Assert.AreEqual (101, NewObjectReference.Count, "#4");
294                 }
295
296                 [Test]
297                 public void DateTimeArray ()
298                 {
299                         DateTime [] e = new DateTime [6];
300                         string [] names = new string [6];
301
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;
308
309                         BinaryFormatter bf = new BinaryFormatter ();
310                         MemoryStream ms = new MemoryStream ();
311
312                         bf.Serialize (ms, e);
313
314                         ms.Position = 0;
315                         DateTime [] a = (DateTime []) bf.Deserialize (ms);
316
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]);
320                 }
321
322                 public Stream GetSerializedStream ()
323                 {
324                         SerializationTest test = new SerializationTest (true, Int32.MinValue);
325                         BinaryFormatter bf = new BinaryFormatter ();
326                         MemoryStream ms = new MemoryStream ();
327                         bf.Serialize (ms, test);
328                         ms.Position = 0;
329                         return ms;
330                 }
331         }
332 }