2009-12-02 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / Test / System.Runtime.Serialization.Formatters.Binary / BinaryFormatterCas.cs
blob25aef89dad7c203e3ef3c15686e1e7dd056ac49f
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.
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;
37 using MonoTests.System.Runtime.Serialization.Formatters.Binary;
39 using NUnit.Framework;
41 namespace MonoCasTests.System.Runtime.Serialization.Formatters.Binary {
43 [TestFixture]
44 [Category ("CAS")]
45 public class BinaryFormatterCas {
47 private BinaryFormatterTest unit;
48 private Stream stream;
50 [TestFixtureSetUp]
51 public void FixtureSetUp ()
53 // executes at full trust
54 unit = new BinaryFormatterTest ();
55 stream = unit.GetSerializedStream ();
58 [SetUp]
59 public virtual void SetUp ()
61 if (!SecurityManager.SecurityEnabled)
62 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
63 stream.Position = 0;
66 [Test]
67 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
68 public void ReuseUnitTest ()
70 unit.Constructor_Default ();
71 unit.Constructor ();
74 [Test]
75 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
76 [ExpectedException (typeof (SecurityException))]
77 public void Serialization_Deny_Unrestricted ()
79 unit.GetSerializedStream ();
82 [Test]
83 [SecurityPermission (SecurityAction.PermitOnly, SerializationFormatter = true)]
84 public void Serialization_PermitOnly_SerializationFormatter ()
86 Assert.IsNotNull (unit.GetSerializedStream ());
89 [Test]
90 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
91 [ExpectedException (typeof (SecurityException))]
92 public void Deserialization_Deny_Unrestricted ()
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");
100 [Test]
101 [SecurityPermission (SecurityAction.PermitOnly, SerializationFormatter = true)]
102 public void Deserialization_PermitOnly_SerializationFormatter ()
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");
110 [Test]
111 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
112 // no SecurityException here because a LinkDemand is used
113 public void UnsafeDeserialization_Deny_Unrestricted ()
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");
121 [Test]
122 [SecurityPermission (SecurityAction.PermitOnly, SerializationFormatter = true)]
123 public void UnsafeDeserialization_PermitOnly_SerializationFormatter ()
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");
131 [Test]
132 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
133 public void LinkDemand_Deny_Unrestricted ()
135 ConstructorInfo ci = typeof (BinaryFormatter).GetConstructor (new Type[0]);
136 Assert.IsNotNull (ci, "default .ctor()");
137 Assert.IsNotNull (ci.Invoke (null), "invoke");