1 //
2 // WebPermissionAttributeTest.cs - NUnit Test Cases for WebPermissionAttribute
3 //
4 // Author:
5 //	Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 
29 using NUnit.Framework;
30 using System;
31 using System.Net;
32 using System.Security;
33 using System.Security.Permissions;
34 using System.Text.RegularExpressions;
35 
36 namespace MonoTests.System.Net {
37 
38 	[TestFixture]
39 #if MOBILE
40 	[Ignore ("CAS is not supported and parts will be linked away")]
41 #endif
42 	public class WebPermissionAttributeTest {
43 
44 		[Test]
Default()45 		public void Default ()
46 		{
47 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
48 			Assert.AreEqual (a.ToString (), a.TypeId.ToString (), "TypeId");
49 			Assert.IsFalse (a.Unrestricted, "Unrestricted");
50 
51 			WebPermission wp = (WebPermission)a.CreatePermission ();
52 			Assert.IsFalse (wp.IsUnrestricted (), "IsUnrestricted");
53 		}
54 
55 		[Test]
Action()56 		public void Action ()
57 		{
58 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
59 			Assert.AreEqual (SecurityAction.Assert, a.Action, "Action=Assert");
60 			a.Action = SecurityAction.Demand;
61 			Assert.AreEqual (SecurityAction.Demand, a.Action, "Action=Demand");
62 			a.Action = SecurityAction.Deny;
63 			Assert.AreEqual (SecurityAction.Deny, a.Action, "Action=Deny");
64 			a.Action = SecurityAction.InheritanceDemand;
65 			Assert.AreEqual (SecurityAction.InheritanceDemand, a.Action, "Action=InheritanceDemand");
66 			a.Action = SecurityAction.LinkDemand;
67 			Assert.AreEqual (SecurityAction.LinkDemand, a.Action, "Action=LinkDemand");
68 			a.Action = SecurityAction.PermitOnly;
69 			Assert.AreEqual (SecurityAction.PermitOnly, a.Action, "Action=PermitOnly");
70 			a.Action = SecurityAction.RequestMinimum;
71 			Assert.AreEqual (SecurityAction.RequestMinimum, a.Action, "Action=RequestMinimum");
72 			a.Action = SecurityAction.RequestOptional;
73 			Assert.AreEqual (SecurityAction.RequestOptional, a.Action, "Action=RequestOptional");
74 			a.Action = SecurityAction.RequestRefuse;
75 			Assert.AreEqual (SecurityAction.RequestRefuse, a.Action, "Action=RequestRefuse");
76 		}
77 
78 		[Test]
Action_Invalid()79 		public void Action_Invalid ()
80 		{
81 			WebPermissionAttribute a = new WebPermissionAttribute ((SecurityAction)Int32.MinValue);
82 			// no validation in attribute
83 		}
84 
85 		[Test]
Unrestricted()86 		public void Unrestricted ()
87 		{
88 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
89 			a.Unrestricted = true;
90 			WebPermission wp = (WebPermission)a.CreatePermission ();
91 			Assert.IsTrue (wp.IsUnrestricted (), "IsUnrestricted");
92 
93 			a.Unrestricted = false;
94 			wp = (WebPermission)a.CreatePermission ();
95 			Assert.IsFalse (wp.IsUnrestricted (), "!IsUnrestricted");
96 		}
97 
98 		[Test]
Accept_Null()99 		public void Accept_Null ()
100 		{
101 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
102 			a.Accept = null; // legal
103 			Assert.IsNull (a.Accept, "Accept");
104 			Assert.IsNull (a.Connect, "Connect");
105 		}
106 
107 		[Test]
108 		[ExpectedException (typeof (ArgumentException))]
Accept_Dual()109 		public void Accept_Dual ()
110 		{
111 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
112 			a.Accept = "/";
113 			a.Accept = "\\";
114 		}
115 
116 		[Test]
117 		[ExpectedException (typeof (ArgumentException))]
Accept_Dual_Null()118 		public void Accept_Dual_Null ()
119 		{
120 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
121 			a.Accept = "/";
122 			a.Accept = null;
123 		}
124 
125 		[Test]
Accept()126 		public void Accept ()
127 		{
128 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
129 			a.Accept = "/";
130 		}
131 
132 		[Test]
133 		[ExpectedException (typeof (ArgumentNullException))]
AcceptPattern_Null()134 		public void AcceptPattern_Null ()
135 		{
136 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
137 			a.AcceptPattern = null;
138 		}
139 
140 		[Test]
141 		[ExpectedException (typeof (ArgumentException))]
AcceptPattern_Dual()142 		public void AcceptPattern_Dual ()
143 		{
144 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
145 			a.AcceptPattern = "/";
146 			a.AcceptPattern = "\\";
147 		}
148 
149 		[Test]
AcceptPattern()150 		public void AcceptPattern ()
151 		{
152 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
153 			a.AcceptPattern = "\b(?"; // invalid regex expression
154 			Assert.AreEqual ("\b(?", a.AcceptPattern, "AcceptPattern");
155 			Assert.IsNull (a.ConnectPattern, "ConnectPattern");
156 		}
157 
158 		[Test]
159 		[ExpectedException (typeof (ArgumentException))]
Accept_AcceptPattern()160 		public void Accept_AcceptPattern ()
161 		{
162 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
163 			a.Accept = "/";
164 			a.AcceptPattern = "\\";
165 		}
166 
167 		[Test]
168 		[ExpectedException (typeof (ArgumentException))]
AcceptPattern_Accept()169 		public void AcceptPattern_Accept ()
170 		{
171 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
172 			a.AcceptPattern = "/";
173 			a.Accept = "\\";
174 		}
175 
176 		[Test]
Connect_Null()177 		public void Connect_Null ()
178 		{
179 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
180 			a.Connect = null; // legal
181 			Assert.IsNull (a.Accept, "Accept");
182 			Assert.IsNull (a.Connect, "Connect");
183 		}
184 
185 		[Test]
186 		[ExpectedException (typeof (ArgumentException))]
Connect_Dual()187 		public void Connect_Dual ()
188 		{
189 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
190 			a.Connect = "/";
191 			a.Connect = "\\";
192 		}
193 
194 		[Test]
195 		[ExpectedException (typeof (ArgumentException))]
Connect_Dual_Null()196 		public void Connect_Dual_Null ()
197 		{
198 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
199 			a.Connect = "/";
200 			a.Connect = null;
201 		}
202 
203 		[Test]
Connect()204 		public void Connect ()
205 		{
206 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
207 			a.Connect = "/";
208 			Assert.IsNull (a.Accept, "Accept");
209 			Assert.AreEqual ("/", a.Connect, "Connect");
210 		}
211 
212 		[Test]
213 		[ExpectedException (typeof (ArgumentNullException))]
ConnectPattern_Null()214 		public void ConnectPattern_Null ()
215 		{
216 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
217 			a.ConnectPattern = null;
218 		}
219 
220 		[Test]
221 		[ExpectedException (typeof (ArgumentException))]
ConnectPattern_Dual()222 		public void ConnectPattern_Dual ()
223 		{
224 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
225 			a.ConnectPattern = "/";
226 			a.ConnectPattern = "\\";
227 		}
228 
229 		[Test]
ConnectPattern()230 		public void ConnectPattern ()
231 		{
232 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
233 			a.ConnectPattern = "\b(?"; // invalid regex expression
234 			Assert.IsNull (a.AcceptPattern, "AcceptPattern");
235 			Assert.AreEqual ("\b(?", a.ConnectPattern, "ConnectPattern");
236 		}
237 
238 		[Test]
239 		[ExpectedException (typeof (ArgumentException))]
Connect_ConnectPattern()240 		public void Connect_ConnectPattern ()
241 		{
242 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
243 			a.Connect = "/";
244 			a.ConnectPattern = "\\";
245 		}
246 
247 		[Test]
248 		[ExpectedException (typeof (ArgumentException))]
ConnectPattern_Accept()249 		public void ConnectPattern_Accept ()
250 		{
251 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
252 			a.ConnectPattern = "/";
253 			a.Connect = "\\";
254 		}
255 
256 		[Test]
CreatePermission_InvalidRegex()257 		public void CreatePermission_InvalidRegex ()
258 		{
259 			WebPermissionAttribute a = new WebPermissionAttribute (SecurityAction.Assert);
260 			a.AcceptPattern = "\b(?"; // invalid regex expression
261 			a.ConnectPattern = "\b(?"; // invalid regex expression
262 			WebPermission wp = (WebPermission) a.CreatePermission ();
263 			Assert.IsNotNull (wp, "CreatePermission");
264 		}
265 
266 		[Test]
Attributes()267 		public void Attributes ()
268 		{
269 			Type t = typeof (WebPermissionAttribute);
270 			Assert.IsTrue (t.IsSerializable, "IsSerializable");
271 
272 			object [] attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), false);
273 			Assert.AreEqual (1, attrs.Length, "AttributeUsage");
274 			AttributeUsageAttribute aua = (AttributeUsageAttribute)attrs [0];
275 			Assert.IsTrue (aua.AllowMultiple, "AllowMultiple");
276 			Assert.IsFalse (aua.Inherited, "Inherited");
277 			AttributeTargets at = (AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method);
278 			Assert.AreEqual (at, aua.ValidOn, "ValidOn");
279 		}
280 	}
281 }
282