1 //
2 // KeyContainerPermissionAttributeTest.cs -
3 //	NUnit Test Cases for KeyContainerPermissionAttributeTest
4 //
5 // Author:
6 //	Sebastien Pouliot  <sebastien@ximian.com>
7 //
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 
31 using NUnit.Framework;
32 using System;
33 using System.Security;
34 using System.Security.Permissions;
35 
36 namespace MonoTests.System.Security.Permissions {
37 
38 	[TestFixture]
39 	public class KeyContainerPermissionAttributeTest {
40 
41 		[Test]
42 		[Category ("NotWorking")]
Default()43 		public void Default ()
44 		{
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");
52 
53 			Assert.IsFalse (a.Unrestricted, "Unrestricted");
54 			Assert.AreEqual (a.ToString (), a.TypeId.ToString (), "TypeId");
55 
56 			KeyContainerPermission perm = (KeyContainerPermission)a.CreatePermission ();
57 			Assert.AreEqual (KeyContainerPermissionFlags.NoFlags, perm.Flags, "perm.Flags");
58 			Assert.IsFalse (perm.IsUnrestricted (), "perm.Unrestricted");
59 		}
60 
61 		[Test]
Action()62 		public void Action ()
63 		{
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");
82 		}
83 
84 		[Test]
Action_Invalid()85 		public void Action_Invalid ()
86 		{
87 			KeyContainerPermissionAttribute a = new KeyContainerPermissionAttribute ((SecurityAction)Int32.MinValue);
88 			// no validation in attribute
89 		}
90 
Empty()91 		private KeyContainerPermissionAttribute Empty ()
92 		{
93 			return new KeyContainerPermissionAttribute (SecurityAction.Assert);
94 		}
95 
96 		[Test]
KeyContainerName()97 		public void KeyContainerName ()
98 		{
99 			KeyContainerPermissionAttribute a = Empty ();
100 			a.KeyContainerName = "mono";
101 			Assert.AreEqual ("mono", a.KeyContainerName, "KeyContainerName-1");
102 			a.KeyContainerName = null;
103 			Assert.IsNull (a.KeyContainerName, "KeyContainerName-2");
104 			a.KeyContainerName = String.Empty;
105 			Assert.AreEqual (String.Empty, a.KeyContainerName, "KeyContainerName-3");
106 			Assert.AreEqual (KeyContainerPermissionFlags.NoFlags, a.Flags, "Flags");
107 		}
108 
109 		[Test]
KeySpec()110 		public void KeySpec ()
111 		{
112 			KeyContainerPermissionAttribute a = Empty ();
113 			a.KeySpec = Int32.MinValue;
114 			Assert.AreEqual (Int32.MinValue, a.KeySpec, "KeySpec-1");
115 			a.KeySpec = 0;
116 			Assert.AreEqual (0, a.KeySpec, "KeySpec-2");
117 			a.KeySpec = Int32.MaxValue;
118 			Assert.AreEqual (Int32.MaxValue, a.KeySpec, "KeySpec-3");
119 			Assert.AreEqual (KeyContainerPermissionFlags.NoFlags, a.Flags, "Flags");
120 		}
121 
122 		[Test]
KeyStore()123 		public void KeyStore ()
124 		{
125 			KeyContainerPermissionAttribute a = Empty ();
126 			a.KeyStore = "mono";
127 			Assert.AreEqual ("mono", a.KeyStore, "KeyStore-1");
128 			a.KeyStore = null;
129 			Assert.IsNull (a.KeyStore, "KeyStore-2");
130 			a.KeyStore = String.Empty;
131 			Assert.AreEqual (String.Empty, a.KeyStore, "KeyStore-3");
132 			Assert.AreEqual (KeyContainerPermissionFlags.NoFlags, a.Flags, "Flags");
133 		}
134 
135 		[Test]
ProviderName()136 		public void ProviderName ()
137 		{
138 			KeyContainerPermissionAttribute a = Empty ();
139 			a.ProviderName = "mono";
140 			Assert.AreEqual ("mono", a.ProviderName, "ProviderName-1");
141 			a.ProviderName = null;
142 			Assert.IsNull (a.ProviderName, "ProviderName-2");
143 			a.ProviderName = String.Empty;
144 			Assert.AreEqual (String.Empty, a.ProviderName, "ProviderName-3");
145 			Assert.AreEqual (KeyContainerPermissionFlags.NoFlags, a.Flags, "Flags");
146 		}
147 
148 		[Test]
ProviderType()149 		public void ProviderType ()
150 		{
151 			KeyContainerPermissionAttribute a = Empty ();
152 			a.ProviderType = Int32.MinValue;
153 			Assert.AreEqual (Int32.MinValue, a.ProviderType, "ProviderType-1");
154 			a.ProviderType = 0;
155 			Assert.AreEqual (0, a.ProviderType, "ProviderType-2");
156 			a.ProviderType = Int32.MaxValue;
157 			Assert.AreEqual (Int32.MaxValue, a.ProviderType, "ProviderType-3");
158 			Assert.AreEqual (KeyContainerPermissionFlags.NoFlags, a.Flags, "Flags");
159 		}
160 
161 		[Test]
Unrestricted()162 		public void Unrestricted ()
163 		{
164 			KeyContainerPermissionAttribute a = Empty ();
165 			a.Unrestricted = true;
166 			Assert.AreEqual (KeyContainerPermissionFlags.NoFlags, a.Flags, "Flags");
167 
168 			KeyContainerPermission perm = (KeyContainerPermission)a.CreatePermission ();
169 			Assert.AreEqual (KeyContainerPermissionFlags.AllFlags, perm.Flags, "CreatePermission.Flags");
170 		}
171 
172 		[Test]
Flags()173 		public void Flags ()
174 		{
175 			KeyContainerPermissionAttribute a = Empty ();
176 			a.Flags = KeyContainerPermissionFlags.AllFlags;
177 			Assert.IsFalse (a.Unrestricted, "Unrestricted");
178 			Assert.AreEqual (KeyContainerPermissionFlags.AllFlags, a.Flags, "Flags");
179 		}
180 
181 		[Test]
Flags_Invalid()182 		public void Flags_Invalid ()
183 		{
184 			KeyContainerPermissionAttribute a = Empty ();
185 			a.Flags = ((KeyContainerPermissionFlags)Int32.MinValue);
186 			// no validations for flags
187 		}
188 
189 		[Test]
Attributes()190 		public void Attributes ()
191 		{
192 			Type t = typeof (KeyContainerPermissionAttribute);
193 			Assert.IsTrue (t.IsSerializable, "IsSerializable");
194 
195 			object [] attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), false);
196 			Assert.AreEqual (1, attrs.Length, "AttributeUsage");
197 			AttributeUsageAttribute aua = (AttributeUsageAttribute)attrs [0];
198 			Assert.IsTrue (aua.AllowMultiple, "AllowMultiple");
199 			Assert.IsFalse (aua.Inherited, "Inherited");
200 			AttributeTargets at = (AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method);
201 			Assert.AreEqual (at, aua.ValidOn, "ValidOn");
202 		}
203 	}
204 }
205 
206