2 // UrlIdentityPermissionAttributeTest.cs -
3 // NUnit Test Cases for UrlIdentityPermissionAttribute
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using NUnit
.Framework
;
33 using System
.Security
;
34 using System
.Security
.Permissions
;
36 namespace MonoTests
.System
.Security
.Permissions
{
39 public class UrlIdentityPermissionAttributeTest
{
42 public void Default ()
44 UrlIdentityPermissionAttribute a
= new UrlIdentityPermissionAttribute (SecurityAction
.Assert
);
45 Assert
.AreEqual (a
.ToString (), a
.TypeId
.ToString (), "TypeId");
46 Assert
.IsFalse (a
.Unrestricted
, "Unrestricted");
47 Assert
.IsNull (a
.Url
, "Url");
52 [ExpectedException (typeof (NullReferenceException
))]
54 public void DefaultPermission ()
56 UrlIdentityPermissionAttribute a
= new UrlIdentityPermissionAttribute (SecurityAction
.Assert
);
57 Assert
.IsNull (a
.Url
, "Url");
58 // UrlIdentityPermission would throw a ArgumentNullException for a null URL ...
59 UrlIdentityPermission perm
= (UrlIdentityPermission
) a
.CreatePermission ();
60 // ... but this works ...
61 Assert
.IsNotNull (perm
, "CreatePermission(null url)");
62 // ... but this doesn't!
63 string url
= perm
.Url
;
65 Assert
.AreEqual (String
.Empty
, url
, "Url-2");
72 UrlIdentityPermissionAttribute a
= new UrlIdentityPermissionAttribute (SecurityAction
.Assert
);
73 Assert
.AreEqual (SecurityAction
.Assert
, a
.Action
, "Action=Assert");
74 a
.Action
= SecurityAction
.Demand
;
75 Assert
.AreEqual (SecurityAction
.Demand
, a
.Action
, "Action=Demand");
76 a
.Action
= SecurityAction
.Deny
;
77 Assert
.AreEqual (SecurityAction
.Deny
, a
.Action
, "Action=Deny");
78 a
.Action
= SecurityAction
.InheritanceDemand
;
79 Assert
.AreEqual (SecurityAction
.InheritanceDemand
, a
.Action
, "Action=InheritanceDemand");
80 a
.Action
= SecurityAction
.LinkDemand
;
81 Assert
.AreEqual (SecurityAction
.LinkDemand
, a
.Action
, "Action=LinkDemand");
82 a
.Action
= SecurityAction
.PermitOnly
;
83 Assert
.AreEqual (SecurityAction
.PermitOnly
, a
.Action
, "Action=PermitOnly");
84 a
.Action
= SecurityAction
.RequestMinimum
;
85 Assert
.AreEqual (SecurityAction
.RequestMinimum
, a
.Action
, "Action=RequestMinimum");
86 a
.Action
= SecurityAction
.RequestOptional
;
87 Assert
.AreEqual (SecurityAction
.RequestOptional
, a
.Action
, "Action=RequestOptional");
88 a
.Action
= SecurityAction
.RequestRefuse
;
89 Assert
.AreEqual (SecurityAction
.RequestRefuse
, a
.Action
, "Action=RequestRefuse");
91 a
.Action
= SecurityAction
.DemandChoice
;
92 Assert
.AreEqual (SecurityAction
.DemandChoice
, a
.Action
, "Action=DemandChoice");
93 a
.Action
= SecurityAction
.InheritanceDemandChoice
;
94 Assert
.AreEqual (SecurityAction
.InheritanceDemandChoice
, a
.Action
, "Action=InheritanceDemandChoice");
95 a
.Action
= SecurityAction
.LinkDemandChoice
;
96 Assert
.AreEqual (SecurityAction
.LinkDemandChoice
, a
.Action
, "Action=LinkDemandChoice");
101 public void Action_Invalid ()
103 UrlIdentityPermissionAttribute a
= new UrlIdentityPermissionAttribute ((SecurityAction
)Int32
.MinValue
);
104 // no validation in attribute
110 UrlIdentityPermissionAttribute a
= new UrlIdentityPermissionAttribute (SecurityAction
.Assert
);
112 Assert
.AreEqual ("mono", a
.Url
, "Url-1");
114 Assert
.IsNull (a
.Url
, "Url-2");
115 a
.Url
= "mono again";
116 Assert
.AreEqual ("mono again", a
.Url
, "Url-3");
120 [ExpectedException (typeof (ArgumentException
))]
121 public void Unrestricted ()
123 UrlIdentityPermissionAttribute a
= new UrlIdentityPermissionAttribute (SecurityAction
.Assert
);
124 a
.Unrestricted
= true;
125 IPermission perm
= a
.CreatePermission ();
129 public void Attributes ()
131 Type t
= typeof (UrlIdentityPermissionAttribute
);
132 Assert
.IsTrue (t
.IsSerializable
, "IsSerializable");
134 object[] attrs
= t
.GetCustomAttributes (typeof (AttributeUsageAttribute
), false);
135 Assert
.AreEqual (1, attrs
.Length
, "AttributeUsage");
136 AttributeUsageAttribute aua
= (AttributeUsageAttribute
)attrs
[0];
137 Assert
.IsTrue (aua
.AllowMultiple
, "AllowMultiple");
138 Assert
.IsFalse (aua
.Inherited
, "Inherited");
139 AttributeTargets at
= (AttributeTargets
.Assembly
| AttributeTargets
.Class
| AttributeTargets
.Struct
| AttributeTargets
.Constructor
| AttributeTargets
.Method
);
140 Assert
.AreEqual (at
, aua
.ValidOn
, "ValidOn");