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 [Category ("NotWorking")]
44 public void Default ()
46 KeyContainerPermissionAttribute a = new KeyContainerPermissionAttribute (SecurityAction.Assert);
47 Assert.AreEqual (KeyContainerPermissionFlags.NoFlags, a.Flags, "Flags");
48 Assert.IsNull (a.KeyContainerName, "KeyContainerName");
49 Assert.AreEqual (-1, a.KeySpec, "KeySpec");
50 Assert.IsNull (a.KeyStore, "KeyStore");
51 Assert.IsNull (a.ProviderName, "ProviderName");
52 Assert.AreEqual (-1, a.ProviderType, "ProviderType");
54 Assert.IsFalse (a.Unrestricted, "Unrestricted");
55 Assert.AreEqual (a.ToString (), a.TypeId.ToString (), "TypeId");
57 KeyContainerPermission perm = (KeyContainerPermission)a.CreatePermission ();
58 Assert.AreEqual (KeyContainerPermissionFlags.NoFlags, perm.Flags, "perm.Flags");
59 Assert.IsFalse (perm.IsUnrestricted (), "perm.Unrestricted");
65 KeyContainerPermissionAttribute a = new KeyContainerPermissionAttribute (SecurityAction.Assert);
66 Assert.AreEqual (SecurityAction.Assert, a.Action, "Action=Assert");
67 a.Action = SecurityAction.Demand;
68 Assert.AreEqual (SecurityAction.Demand, a.Action, "Action=Demand");
69 a.Action = SecurityAction.Deny;
70 Assert.AreEqual (SecurityAction.Deny, a.Action, "Action=Deny");
71 a.Action = SecurityAction.InheritanceDemand;
72 Assert.AreEqual (SecurityAction.InheritanceDemand, a.Action, "Action=InheritanceDemand");
73 a.Action = SecurityAction.LinkDemand;
74 Assert.AreEqual (SecurityAction.LinkDemand, a.Action, "Action=LinkDemand");
75 a.Action = SecurityAction.PermitOnly;
76 Assert.AreEqual (SecurityAction.PermitOnly, a.Action, "Action=PermitOnly");
77 a.Action = SecurityAction.RequestMinimum;
78 Assert.AreEqual (SecurityAction.RequestMinimum, a.Action, "Action=RequestMinimum");
79 a.Action = SecurityAction.RequestOptional;
80 Assert.AreEqual (SecurityAction.RequestOptional, a.Action, "Action=RequestOptional");
81 a.Action = SecurityAction.RequestRefuse;
82 Assert.AreEqual (SecurityAction.RequestRefuse, a.Action, "Action=RequestRefuse");
84 a.Action = SecurityAction.DemandChoice;
85 Assert.AreEqual (SecurityAction.DemandChoice, a.Action, "Action=DemandChoice");
86 a.Action = SecurityAction.InheritanceDemandChoice;
87 Assert.AreEqual (SecurityAction.InheritanceDemandChoice, a.Action, "Action=InheritanceDemandChoice");
88 a.Action = SecurityAction.LinkDemandChoice;
89 Assert.AreEqual (SecurityAction.LinkDemandChoice, a.Action, "Action=LinkDemandChoice");
94 public void Action_Invalid ()
96 KeyContainerPermissionAttribute a = new KeyContainerPermissionAttribute ((SecurityAction)Int32.MinValue);
97 // no validation in attribute
100 private KeyContainerPermissionAttribute Empty ()
102 return new KeyContainerPermissionAttribute (SecurityAction.Assert);
106 public void KeyContainerName ()
108 KeyContainerPermissionAttribute a = Empty ();
109 a.KeyContainerName = "mono";
110 Assert.AreEqual ("mono", a.KeyContainerName, "KeyContainerName-1");
111 a.KeyContainerName = null;
112 Assert.IsNull (a.KeyContainerName, "KeyContainerName-2");
113 a.KeyContainerName = String.Empty;
114 Assert.AreEqual (String.Empty, a.KeyContainerName, "KeyContainerName-3");
115 Assert.AreEqual (KeyContainerPermissionFlags.NoFlags, a.Flags, "Flags");
119 public void KeySpec ()
121 KeyContainerPermissionAttribute a = Empty ();
122 a.KeySpec = Int32.MinValue;
123 Assert.AreEqual (Int32.MinValue, a.KeySpec, "KeySpec-1");
125 Assert.AreEqual (0, a.KeySpec, "KeySpec-2");
126 a.KeySpec = Int32.MaxValue;
127 Assert.AreEqual (Int32.MaxValue, a.KeySpec, "KeySpec-3");
128 Assert.AreEqual (KeyContainerPermissionFlags.NoFlags, a.Flags, "Flags");
132 public void KeyStore ()
134 KeyContainerPermissionAttribute a = Empty ();
136 Assert.AreEqual ("mono", a.KeyStore, "KeyStore-1");
138 Assert.IsNull (a.KeyStore, "KeyStore-2");
139 a.KeyStore = String.Empty;
140 Assert.AreEqual (String.Empty, a.KeyStore, "KeyStore-3");
141 Assert.AreEqual (KeyContainerPermissionFlags.NoFlags, a.Flags, "Flags");
145 public void ProviderName ()
147 KeyContainerPermissionAttribute a = Empty ();
148 a.ProviderName = "mono";
149 Assert.AreEqual ("mono", a.ProviderName, "ProviderName-1");
150 a.ProviderName = null;
151 Assert.IsNull (a.ProviderName, "ProviderName-2");
152 a.ProviderName = String.Empty;
153 Assert.AreEqual (String.Empty, a.ProviderName, "ProviderName-3");
154 Assert.AreEqual (KeyContainerPermissionFlags.NoFlags, a.Flags, "Flags");
158 public void ProviderType ()
160 KeyContainerPermissionAttribute a = Empty ();
161 a.ProviderType = Int32.MinValue;
162 Assert.AreEqual (Int32.MinValue, a.ProviderType, "ProviderType-1");
164 Assert.AreEqual (0, a.ProviderType, "ProviderType-2");
165 a.ProviderType = Int32.MaxValue;
166 Assert.AreEqual (Int32.MaxValue, a.ProviderType, "ProviderType-3");
167 Assert.AreEqual (KeyContainerPermissionFlags.NoFlags, a.Flags, "Flags");
171 public void Unrestricted ()
173 KeyContainerPermissionAttribute a = Empty ();
174 a.Unrestricted = true;
175 Assert.AreEqual (KeyContainerPermissionFlags.NoFlags, a.Flags, "Flags");
177 KeyContainerPermission perm = (KeyContainerPermission)a.CreatePermission ();
178 Assert.AreEqual (KeyContainerPermissionFlags.AllFlags, perm.Flags, "CreatePermission.Flags");
184 KeyContainerPermissionAttribute a = Empty ();
185 a.Flags = KeyContainerPermissionFlags.AllFlags;
186 Assert.IsFalse (a.Unrestricted, "Unrestricted");
187 Assert.AreEqual (KeyContainerPermissionFlags.AllFlags, a.Flags, "Flags");
191 public void Flags_Invalid ()
193 KeyContainerPermissionAttribute a = Empty ();
194 a.Flags = ((KeyContainerPermissionFlags)Int32.MinValue);
195 // no validations for flags
199 public void Attributes ()
201 Type t = typeof (KeyContainerPermissionAttribute);
202 Assert.IsTrue (t.IsSerializable, "IsSerializable");
204 object [] attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), false);
205 Assert.AreEqual (1, attrs.Length, "AttributeUsage");
206 AttributeUsageAttribute aua = (AttributeUsageAttribute)attrs [0];
207 Assert.IsTrue (aua.AllowMultiple, "AllowMultiple");
208 Assert.IsFalse (aua.Inherited, "Inherited");
209 AttributeTargets at = (AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method);
210 Assert.AreEqual (at, aua.ValidOn, "ValidOn");