2 // KeyContainerPermissionAttributeTest.cs -
3 // NUnit Test Cases for KeyContainerPermissionAttributeTest
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 using NUnit
.Framework
;
34 using System
.Security
;
35 using System
.Security
.Permissions
;
37 namespace MonoTests
.System
.Security
.Permissions
{
40 public class KeyContainerPermissionAttributeTest
{
43 public void Default ()
45 KeyContainerPermissionAttribute a
= new KeyContainerPermissionAttribute (SecurityAction
.Assert
);
46 Assert
.AreEqual (KeyContainerPermissionFlags
.NoFlags
, a
.Flags
, "Flags");
47 Assert
.IsNull (a
.KeyContainerName
, "KeyContainerName");
48 Assert
.AreEqual (-1, a
.KeySpec
, "KeySpec");
49 Assert
.IsNull (a
.KeyStore
, "KeyStore");
50 Assert
.IsNull (a
.ProviderName
, "ProviderName");
51 Assert
.AreEqual (-1, a
.ProviderType
, "ProviderType");
53 Assert
.IsFalse (a
.Unrestricted
, "Unrestricted");
54 Assert
.AreEqual (a
.ToString (), a
.TypeId
.ToString (), "TypeId");
56 KeyContainerPermission perm
= (KeyContainerPermission
)a
.CreatePermission ();
57 Assert
.AreEqual (KeyContainerPermissionFlags
.NoFlags
, perm
.Flags
, "perm.Flags");
58 Assert
.IsFalse (perm
.IsUnrestricted (), "perm.Unrestricted");
64 KeyContainerPermissionAttribute a
= new KeyContainerPermissionAttribute (SecurityAction
.Assert
);
65 Assert
.AreEqual (SecurityAction
.Assert
, a
.Action
, "Action=Assert");
66 a
.Action
= SecurityAction
.Demand
;
67 Assert
.AreEqual (SecurityAction
.Demand
, a
.Action
, "Action=Demand");
68 a
.Action
= SecurityAction
.Deny
;
69 Assert
.AreEqual (SecurityAction
.Deny
, a
.Action
, "Action=Deny");
70 a
.Action
= SecurityAction
.InheritanceDemand
;
71 Assert
.AreEqual (SecurityAction
.InheritanceDemand
, a
.Action
, "Action=InheritanceDemand");
72 a
.Action
= SecurityAction
.LinkDemand
;
73 Assert
.AreEqual (SecurityAction
.LinkDemand
, a
.Action
, "Action=LinkDemand");
74 a
.Action
= SecurityAction
.PermitOnly
;
75 Assert
.AreEqual (SecurityAction
.PermitOnly
, a
.Action
, "Action=PermitOnly");
76 a
.Action
= SecurityAction
.RequestMinimum
;
77 Assert
.AreEqual (SecurityAction
.RequestMinimum
, a
.Action
, "Action=RequestMinimum");
78 a
.Action
= SecurityAction
.RequestOptional
;
79 Assert
.AreEqual (SecurityAction
.RequestOptional
, a
.Action
, "Action=RequestOptional");
80 a
.Action
= SecurityAction
.RequestRefuse
;
81 Assert
.AreEqual (SecurityAction
.RequestRefuse
, a
.Action
, "Action=RequestRefuse");
83 a
.Action
= SecurityAction
.DemandChoice
;
84 Assert
.AreEqual (SecurityAction
.DemandChoice
, a
.Action
, "Action=DemandChoice");
85 a
.Action
= SecurityAction
.InheritanceDemandChoice
;
86 Assert
.AreEqual (SecurityAction
.InheritanceDemandChoice
, a
.Action
, "Action=InheritanceDemandChoice");
87 a
.Action
= SecurityAction
.LinkDemandChoice
;
88 Assert
.AreEqual (SecurityAction
.LinkDemandChoice
, a
.Action
, "Action=LinkDemandChoice");
93 public void Action_Invalid ()
95 KeyContainerPermissionAttribute a
= new KeyContainerPermissionAttribute ((SecurityAction
)Int32
.MinValue
);
96 // no validation in attribute
99 private KeyContainerPermissionAttribute
Empty ()
101 return new KeyContainerPermissionAttribute (SecurityAction
.Assert
);
105 public void KeyContainerName ()
107 KeyContainerPermissionAttribute a
= Empty ();
108 a
.KeyContainerName
= "mono";
109 Assert
.AreEqual ("mono", a
.KeyContainerName
, "KeyContainerName-1");
110 a
.KeyContainerName
= null;
111 Assert
.IsNull (a
.KeyContainerName
, "KeyContainerName-2");
112 a
.KeyContainerName
= String
.Empty
;
113 Assert
.AreEqual (String
.Empty
, a
.KeyContainerName
, "KeyContainerName-3");
114 Assert
.AreEqual (KeyContainerPermissionFlags
.NoFlags
, a
.Flags
, "Flags");
118 public void KeySpec ()
120 KeyContainerPermissionAttribute a
= Empty ();
121 a
.KeySpec
= Int32
.MinValue
;
122 Assert
.AreEqual (Int32
.MinValue
, a
.KeySpec
, "KeySpec-1");
124 Assert
.AreEqual (0, a
.KeySpec
, "KeySpec-2");
125 a
.KeySpec
= Int32
.MaxValue
;
126 Assert
.AreEqual (Int32
.MaxValue
, a
.KeySpec
, "KeySpec-3");
127 Assert
.AreEqual (KeyContainerPermissionFlags
.NoFlags
, a
.Flags
, "Flags");
131 public void KeyStore ()
133 KeyContainerPermissionAttribute a
= Empty ();
135 Assert
.AreEqual ("mono", a
.KeyStore
, "KeyStore-1");
137 Assert
.IsNull (a
.KeyStore
, "KeyStore-2");
138 a
.KeyStore
= String
.Empty
;
139 Assert
.AreEqual (String
.Empty
, a
.KeyStore
, "KeyStore-3");
140 Assert
.AreEqual (KeyContainerPermissionFlags
.NoFlags
, a
.Flags
, "Flags");
144 public void ProviderName ()
146 KeyContainerPermissionAttribute a
= Empty ();
147 a
.ProviderName
= "mono";
148 Assert
.AreEqual ("mono", a
.ProviderName
, "ProviderName-1");
149 a
.ProviderName
= null;
150 Assert
.IsNull (a
.ProviderName
, "ProviderName-2");
151 a
.ProviderName
= String
.Empty
;
152 Assert
.AreEqual (String
.Empty
, a
.ProviderName
, "ProviderName-3");
153 Assert
.AreEqual (KeyContainerPermissionFlags
.NoFlags
, a
.Flags
, "Flags");
157 public void ProviderType ()
159 KeyContainerPermissionAttribute a
= Empty ();
160 a
.ProviderType
= Int32
.MinValue
;
161 Assert
.AreEqual (Int32
.MinValue
, a
.ProviderType
, "ProviderType-1");
163 Assert
.AreEqual (0, a
.ProviderType
, "ProviderType-2");
164 a
.ProviderType
= Int32
.MaxValue
;
165 Assert
.AreEqual (Int32
.MaxValue
, a
.ProviderType
, "ProviderType-3");
166 Assert
.AreEqual (KeyContainerPermissionFlags
.NoFlags
, a
.Flags
, "Flags");
170 public void Unrestricted ()
172 KeyContainerPermissionAttribute a
= Empty ();
173 a
.Unrestricted
= true;
174 Assert
.AreEqual (KeyContainerPermissionFlags
.NoFlags
, a
.Flags
, "Flags");
176 KeyContainerPermission perm
= (KeyContainerPermission
)a
.CreatePermission ();
177 Assert
.AreEqual (KeyContainerPermissionFlags
.AllFlags
, perm
.Flags
, "CreatePermission.Flags");
183 KeyContainerPermissionAttribute a
= Empty ();
184 a
.Flags
= KeyContainerPermissionFlags
.AllFlags
;
185 Assert
.IsFalse (a
.Unrestricted
, "Unrestricted");
186 Assert
.AreEqual (KeyContainerPermissionFlags
.AllFlags
, a
.Flags
, "Flags");
190 public void Flags_Invalid ()
192 KeyContainerPermissionAttribute a
= Empty ();
193 a
.Flags
= ((KeyContainerPermissionFlags
)Int32
.MinValue
);
194 // no validations for flags
198 public void Attributes ()
200 Type t
= typeof (KeyContainerPermissionAttribute
);
201 Assert
.IsTrue (t
.IsSerializable
, "IsSerializable");
203 object [] attrs
= t
.GetCustomAttributes (typeof (AttributeUsageAttribute
), false);
204 Assert
.AreEqual (1, attrs
.Length
, "AttributeUsage");
205 AttributeUsageAttribute aua
= (AttributeUsageAttribute
)attrs
[0];
206 Assert
.IsTrue (aua
.AllowMultiple
, "AllowMultiple");
207 Assert
.IsFalse (aua
.Inherited
, "Inherited");
208 AttributeTargets at
= (AttributeTargets
.Assembly
| AttributeTargets
.Class
| AttributeTargets
.Struct
| AttributeTargets
.Constructor
| AttributeTargets
.Method
);
209 Assert
.AreEqual (at
, aua
.ValidOn
, "ValidOn");