2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / Test / System.Security.Permissions / IsolatedStorageFilePermissionAttributeTest.cs
1 //
2 // IsolatedStorageFilePermissionAttributeTest.cs - NUnit Test Cases for IsolatedStorageFilePermissionAttribute
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 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 NUnit.Framework;
31 using System;
32 using System.Security;
33 using System.Security.Permissions;
34
35 namespace MonoTests.System.Security.Permissions {
36
37         [TestFixture]
38         public class IsolatedStorageFilePermissionAttributeTest {
39
40                 [Test]
41                 public void Default () 
42                 {
43                         IsolatedStorageFilePermissionAttribute a = new IsolatedStorageFilePermissionAttribute (SecurityAction.Assert);
44                         Assert.AreEqual (IsolatedStorageContainment.None, a.UsageAllowed, "UsageAllowed");
45                         Assert.AreEqual (0, a.UserQuota, "UserQuota");
46                         Assert.AreEqual (a.ToString (), a.TypeId.ToString (), "TypeId");
47                         Assert.IsFalse (a.Unrestricted, "Unrestricted");
48
49                         IsolatedStorageFilePermission perm = (IsolatedStorageFilePermission) a.CreatePermission ();
50                         Assert.AreEqual (IsolatedStorageContainment.None, perm.UsageAllowed, "CreatePermission-UsageAllowed");
51                         Assert.AreEqual (0, perm.UserQuota, "CreatePermission-UserQuota");
52                 }
53
54                 [Test]
55                 public void Action ()
56                 {
57                         IsolatedStorageFilePermissionAttribute a = new IsolatedStorageFilePermissionAttribute (SecurityAction.Assert);
58                         Assert.AreEqual (SecurityAction.Assert, a.Action, "Action=Assert");
59                         a.Action = SecurityAction.Demand;
60                         Assert.AreEqual (SecurityAction.Demand, a.Action, "Action=Demand");
61                         a.Action = SecurityAction.Deny;
62                         Assert.AreEqual (SecurityAction.Deny, a.Action, "Action=Deny");
63                         a.Action = SecurityAction.InheritanceDemand;
64                         Assert.AreEqual (SecurityAction.InheritanceDemand, a.Action, "Action=InheritanceDemand");
65                         a.Action = SecurityAction.LinkDemand;
66                         Assert.AreEqual (SecurityAction.LinkDemand, a.Action, "Action=LinkDemand");
67                         a.Action = SecurityAction.PermitOnly;
68                         Assert.AreEqual (SecurityAction.PermitOnly, a.Action, "Action=PermitOnly");
69                         a.Action = SecurityAction.RequestMinimum;
70                         Assert.AreEqual (SecurityAction.RequestMinimum, a.Action, "Action=RequestMinimum");
71                         a.Action = SecurityAction.RequestOptional;
72                         Assert.AreEqual (SecurityAction.RequestOptional, a.Action, "Action=RequestOptional");
73                         a.Action = SecurityAction.RequestRefuse;
74                         Assert.AreEqual (SecurityAction.RequestRefuse, a.Action, "Action=RequestRefuse");
75 #if NET_2_0
76                         a.Action = SecurityAction.DemandChoice;
77                         Assert.AreEqual (SecurityAction.DemandChoice, a.Action, "Action=DemandChoice");
78                         a.Action = SecurityAction.InheritanceDemandChoice;
79                         Assert.AreEqual (SecurityAction.InheritanceDemandChoice, a.Action, "Action=InheritanceDemandChoice");
80                         a.Action = SecurityAction.LinkDemandChoice;
81                         Assert.AreEqual (SecurityAction.LinkDemandChoice, a.Action, "Action=LinkDemandChoice");
82 #endif
83                 }
84
85                 [Test]
86                 public void Action_Invalid ()
87                 {
88                         IsolatedStorageFilePermissionAttribute a = new IsolatedStorageFilePermissionAttribute ((SecurityAction)Int32.MinValue);
89                         // no validation in attribute
90                 }
91
92                 [Test]
93                 public void UsageAllowed () 
94                 {
95                         IsolatedStorageFilePermissionAttribute a = new IsolatedStorageFilePermissionAttribute (SecurityAction.Assert);
96                         Assert.AreEqual (IsolatedStorageContainment.None, a.UsageAllowed, "UsageAllowed=None");
97                         a.UsageAllowed = IsolatedStorageContainment.AdministerIsolatedStorageByUser;
98                         Assert.AreEqual (IsolatedStorageContainment.AdministerIsolatedStorageByUser, a.UsageAllowed, "UsageAllowed=AdministerIsolatedStorageByUser");
99                         a.UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByRoamingUser;
100                         Assert.AreEqual (IsolatedStorageContainment.AssemblyIsolationByRoamingUser, a.UsageAllowed, "UsageAllowed=AssemblyIsolationByRoamingUser");
101                         a.UsageAllowed = IsolatedStorageContainment.AssemblyIsolationByUser;
102                         Assert.AreEqual (IsolatedStorageContainment.AssemblyIsolationByUser, a.UsageAllowed, "UsageAllowed=AssemblyIsolationByUser");
103                         a.UsageAllowed = IsolatedStorageContainment.DomainIsolationByRoamingUser;
104                         Assert.AreEqual (IsolatedStorageContainment.DomainIsolationByRoamingUser, a.UsageAllowed, "UsageAllowed=DomainIsolationByRoamingUser");
105                         a.UsageAllowed = IsolatedStorageContainment.DomainIsolationByUser;
106                         Assert.AreEqual (IsolatedStorageContainment.DomainIsolationByUser, a.UsageAllowed, "UsageAllowed=DomainIsolationByUser");
107                         a.UsageAllowed = IsolatedStorageContainment.UnrestrictedIsolatedStorage;
108                         Assert.AreEqual (IsolatedStorageContainment.UnrestrictedIsolatedStorage, a.UsageAllowed, "UsageAllowed=UnrestrictedIsolatedStorage");
109                 }
110
111                 [Test]
112                 public void UsageAllowed_Invalid ()
113                 {
114                         IsolatedStorageFilePermissionAttribute a = new IsolatedStorageFilePermissionAttribute (SecurityAction.Assert);
115                         a.UsageAllowed = (IsolatedStorageContainment)Int32.MinValue;
116                         // no validation in attribute
117                 }
118
119                 [Test]
120                 public void UserQuota () 
121                 {
122                         IsolatedStorageFilePermissionAttribute a = new IsolatedStorageFilePermissionAttribute (SecurityAction.Assert);
123                         Assert.AreEqual (0, a.UserQuota, "UserQuota=default");
124                         a.UserQuota = Int64.MinValue;
125                         Assert.AreEqual (Int64.MinValue, a.UserQuota, "UserQuota=MinValue");
126                         a.UserQuota = Int64.MaxValue;
127                         Assert.AreEqual (Int64.MaxValue, a.UserQuota, "UserQuota=MaxValue");
128                 }
129
130                 [Test]
131                 public void Unrestricted () 
132                 {
133                         IsolatedStorageFilePermissionAttribute a = new IsolatedStorageFilePermissionAttribute (SecurityAction.Assert);
134                         a.Unrestricted = true;
135
136                         IsolatedStorageFilePermission perm = (IsolatedStorageFilePermission) a.CreatePermission ();
137                         Assert.IsTrue (perm.IsUnrestricted (), "CreatePermission.IsUnrestricted");
138                         Assert.AreEqual (IsolatedStorageContainment.UnrestrictedIsolatedStorage, perm.UsageAllowed, "CreatePermission.UsageAllowed");
139                         Assert.AreEqual (Int64.MaxValue, perm.UserQuota, "CreatePermission.UserQuota");
140                 }
141
142                 [Test]
143                 public void Attributes ()
144                 {
145                         IsolatedStorageFilePermissionAttribute a = new IsolatedStorageFilePermissionAttribute (SecurityAction.Assert);
146                         Type t = typeof (IsolatedStorageFilePermissionAttribute);
147                         Assert.IsTrue (t.IsSerializable, "IsSerializable");
148
149                         object [] attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), false);
150                         Assert.AreEqual (1, attrs.Length, "AttributeUsage");
151                         AttributeUsageAttribute aua = (AttributeUsageAttribute)attrs [0];
152                         Assert.IsTrue (aua.AllowMultiple, "AllowMultiple");
153                         Assert.IsFalse (aua.Inherited, "Inherited");
154                         AttributeTargets at = (AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method);
155                         Assert.AreEqual (at, aua.ValidOn, "ValidOn");
156                 }
157         }
158 }