Merge pull request #303 from ermshiperete/5278
[mono.git] / mcs / class / corlib / Test / System.Runtime.Serialization.Formatters.Binary / BinaryFormatterCas.cs
1 //
2 // BinaryFormatterCas.cs - CAS 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.Reflection;
33 using System.Runtime.Serialization.Formatters.Binary;
34 using System.Security;
35 using System.Security.Permissions;
36
37 using MonoTests.System.Runtime.Serialization.Formatters.Binary;
38
39 using NUnit.Framework;
40
41 namespace MonoCasTests.System.Runtime.Serialization.Formatters.Binary {
42
43         [TestFixture]
44         [Category ("CAS")]
45         public class BinaryFormatterCas {
46
47                 private BinaryFormatterTest unit;
48                 private Stream stream;
49
50                 [TestFixtureSetUp]
51                 public void FixtureSetUp ()
52                 {
53                         // executes at full trust
54                         unit = new BinaryFormatterTest ();
55                         stream = unit.GetSerializedStream ();
56                 }
57
58                 [SetUp]
59                 public virtual void SetUp ()
60                 {
61                         if (!SecurityManager.SecurityEnabled)
62                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
63                         stream.Position = 0;
64                 }
65
66                 [Test]
67                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
68                 public void ReuseUnitTest ()
69                 {
70                         unit.Constructor_Default ();
71                         unit.Constructor ();
72                 }
73
74                 [Test]
75                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
76                 [ExpectedException (typeof (SecurityException))]
77                 public void Serialization_Deny_Unrestricted ()
78                 {
79                         unit.GetSerializedStream ();
80                 }
81
82                 [Test]
83                 [SecurityPermission (SecurityAction.PermitOnly, SerializationFormatter = true)]
84                 public void Serialization_PermitOnly_SerializationFormatter ()
85                 {
86                         Assert.IsNotNull (unit.GetSerializedStream ());
87                 }
88
89                 [Test]
90                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
91                 [ExpectedException (typeof (SecurityException))]
92                 public void Deserialization_Deny_Unrestricted ()
93                 {
94                         BinaryFormatter bf = new BinaryFormatter ();
95                         SerializationTest clone = (SerializationTest) bf.Deserialize (stream);
96                         Assert.AreEqual (Int32.MinValue, clone.Integer, "Integer");
97                         Assert.IsFalse (clone.Boolean, "Boolean");
98                 }
99
100                 [Test]
101                 [SecurityPermission (SecurityAction.PermitOnly, SerializationFormatter = true)]
102                 public void Deserialization_PermitOnly_SerializationFormatter ()
103                 {
104                         BinaryFormatter bf = new BinaryFormatter ();
105                         SerializationTest clone = (SerializationTest) bf.Deserialize (stream);
106                         Assert.AreEqual (Int32.MinValue, clone.Integer, "Integer");
107                         Assert.IsFalse (clone.Boolean, "Boolean");
108                 }
109
110                 [Test]
111                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
112                 // no SecurityException here because a LinkDemand is used
113                 public void UnsafeDeserialization_Deny_Unrestricted ()
114                 {
115                         BinaryFormatter bf = new BinaryFormatter ();
116                         SerializationTest clone = (SerializationTest) bf.UnsafeDeserialize (stream, null);
117                         Assert.AreEqual (Int32.MinValue, clone.Integer, "Integer");
118                         Assert.IsFalse (clone.Boolean, "Boolean");
119                 }
120
121                 [Test]
122                 [SecurityPermission (SecurityAction.PermitOnly, SerializationFormatter = true)]
123                 public void UnsafeDeserialization_PermitOnly_SerializationFormatter ()
124                 {
125                         BinaryFormatter bf = new BinaryFormatter ();
126                         SerializationTest clone = (SerializationTest) bf.UnsafeDeserialize (stream, null);
127                         Assert.AreEqual (Int32.MinValue, clone.Integer, "Integer");
128                         Assert.IsFalse (clone.Boolean, "Boolean");
129                 }
130
131                 [Test]
132                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
133                 public void LinkDemand_Deny_Unrestricted ()
134                 {
135                         ConstructorInfo ci = typeof (BinaryFormatter).GetConstructor (new Type[0]);
136                         Assert.IsNotNull (ci, "default .ctor()");
137                         Assert.IsNotNull (ci.Invoke (null), "invoke");
138                 }
139         }
140 }