2 // SqlClientPermissionAttributeTest.cs -
3 // NUnit Test Cases for SqlClientPermissionAttribute
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.
30 using NUnit
.Framework
;
33 using System
.Data
.SqlClient
;
34 using System
.Security
;
35 using System
.Security
.Permissions
;
37 namespace MonoTests
.System
.Data
.SqlClient
{
40 public class SqlClientPermissionAttributeTest
{
43 public void Default ()
45 SqlClientPermissionAttribute a
= new SqlClientPermissionAttribute (SecurityAction
.Assert
);
46 Assert
.AreEqual (a
.ToString (), a
.TypeId
.ToString (), "TypeId");
47 Assert
.IsFalse (a
.Unrestricted
, "Unrestricted");
48 Assert
.IsFalse (a
.AllowBlankPassword
, "AllowBlankPassword");
49 Assert
.AreEqual (String
.Empty
, a
.ConnectionString
, "ConnectionString");
50 Assert
.AreEqual (KeyRestrictionBehavior
.AllowOnly
, a
.KeyRestrictionBehavior
, "KeyRestrictionBehavior");
51 Assert
.AreEqual (String
.Empty
, a
.KeyRestrictions
, "KeyRestrictions");
53 Assert
.IsFalse (a
.ShouldSerializeConnectionString (), "ShouldSerializeConnectionString");
54 Assert
.IsFalse (a
.ShouldSerializeKeyRestrictions (), "ShouldSerializeConnectionString");
56 SqlClientPermission sp
= (SqlClientPermission
)a
.CreatePermission ();
57 Assert
.IsFalse (sp
.IsUnrestricted (), "IsUnrestricted");
63 SqlClientPermissionAttribute a
= new SqlClientPermissionAttribute (SecurityAction
.Assert
);
64 Assert
.AreEqual (SecurityAction
.Assert
, a
.Action
, "Action=Assert");
65 a
.Action
= SecurityAction
.Demand
;
66 Assert
.AreEqual (SecurityAction
.Demand
, a
.Action
, "Action=Demand");
67 a
.Action
= SecurityAction
.Deny
;
68 Assert
.AreEqual (SecurityAction
.Deny
, a
.Action
, "Action=Deny");
69 a
.Action
= SecurityAction
.InheritanceDemand
;
70 Assert
.AreEqual (SecurityAction
.InheritanceDemand
, a
.Action
, "Action=InheritanceDemand");
71 a
.Action
= SecurityAction
.LinkDemand
;
72 Assert
.AreEqual (SecurityAction
.LinkDemand
, a
.Action
, "Action=LinkDemand");
73 a
.Action
= SecurityAction
.PermitOnly
;
74 Assert
.AreEqual (SecurityAction
.PermitOnly
, a
.Action
, "Action=PermitOnly");
75 a
.Action
= SecurityAction
.RequestMinimum
;
76 Assert
.AreEqual (SecurityAction
.RequestMinimum
, a
.Action
, "Action=RequestMinimum");
77 a
.Action
= SecurityAction
.RequestOptional
;
78 Assert
.AreEqual (SecurityAction
.RequestOptional
, a
.Action
, "Action=RequestOptional");
79 a
.Action
= SecurityAction
.RequestRefuse
;
80 Assert
.AreEqual (SecurityAction
.RequestRefuse
, a
.Action
, "Action=RequestRefuse");
82 a
.Action
= SecurityAction
.DemandChoice
;
83 Assert
.AreEqual (SecurityAction
.DemandChoice
, a
.Action
, "Action=DemandChoice");
84 a
.Action
= SecurityAction
.InheritanceDemandChoice
;
85 Assert
.AreEqual (SecurityAction
.InheritanceDemandChoice
, a
.Action
, "Action=InheritanceDemandChoice");
86 a
.Action
= SecurityAction
.LinkDemandChoice
;
87 Assert
.AreEqual (SecurityAction
.LinkDemandChoice
, a
.Action
, "Action=LinkDemandChoice");
92 public void Action_Invalid ()
94 SqlClientPermissionAttribute a
= new SqlClientPermissionAttribute ((SecurityAction
)Int32
.MinValue
);
95 // no validation in attribute
99 public void Unrestricted ()
101 SqlClientPermissionAttribute a
= new SqlClientPermissionAttribute (SecurityAction
.Assert
);
102 a
.Unrestricted
= true;
103 SqlClientPermission scp
= (SqlClientPermission
)a
.CreatePermission ();
104 Assert
.IsTrue (scp
.IsUnrestricted (), "IsUnrestricted");
105 Assert
.IsFalse (a
.AllowBlankPassword
, "AllowBlankPassword");
106 Assert
.AreEqual (String
.Empty
, a
.ConnectionString
, "ConnectionString");
107 Assert
.AreEqual (KeyRestrictionBehavior
.AllowOnly
, a
.KeyRestrictionBehavior
, "KeyRestrictionBehavior");
108 Assert
.AreEqual (String
.Empty
, a
.KeyRestrictions
, "KeyRestrictions");
110 a
.Unrestricted
= false;
111 scp
= (SqlClientPermission
)a
.CreatePermission ();
112 Assert
.IsFalse (scp
.IsUnrestricted (), "!IsUnrestricted");
116 public void AllowBlankPassword ()
118 SqlClientPermissionAttribute a
= new SqlClientPermissionAttribute (SecurityAction
.Assert
);
119 Assert
.IsFalse (a
.AllowBlankPassword
, "Default");
120 a
.AllowBlankPassword
= true;
121 Assert
.IsTrue (a
.AllowBlankPassword
, "True");
122 a
.AllowBlankPassword
= false;
123 Assert
.IsFalse (a
.AllowBlankPassword
, "False");
127 public void ConnectionString ()
129 SqlClientPermissionAttribute a
= new SqlClientPermissionAttribute (SecurityAction
.Assert
);
130 a
.ConnectionString
= String
.Empty
;
131 Assert
.AreEqual (String
.Empty
, a
.ConnectionString
, "Empty");
132 a
.ConnectionString
= "Mono";
133 Assert
.AreEqual ("Mono", a
.ConnectionString
, "Mono");
134 a
.ConnectionString
= null;
135 Assert
.AreEqual (String
.Empty
, a
.ConnectionString
, "Empty(null)");
139 public void KeyRestrictionBehavior_All ()
141 SqlClientPermissionAttribute a
= new SqlClientPermissionAttribute (SecurityAction
.Assert
);
142 a
.KeyRestrictionBehavior
= KeyRestrictionBehavior
.AllowOnly
;
143 Assert
.AreEqual (KeyRestrictionBehavior
.AllowOnly
, a
.KeyRestrictionBehavior
, "AllowOnly");
144 a
.KeyRestrictionBehavior
= KeyRestrictionBehavior
.PreventUsage
;
145 Assert
.AreEqual (KeyRestrictionBehavior
.PreventUsage
, a
.KeyRestrictionBehavior
, "PreventUsage");
149 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
150 public void KeyRestrictionBehavior_Invalid ()
152 SqlClientPermissionAttribute a
= new SqlClientPermissionAttribute (SecurityAction
.Assert
);
153 a
.KeyRestrictionBehavior
= (KeyRestrictionBehavior
)Int32
.MinValue
;
157 public void KeyRestriction ()
159 SqlClientPermissionAttribute a
= new SqlClientPermissionAttribute (SecurityAction
.Assert
);
160 a
.KeyRestrictions
= String
.Empty
;
161 Assert
.AreEqual (String
.Empty
, a
.KeyRestrictions
, "Empty");
162 a
.KeyRestrictions
= "Mono";
163 Assert
.AreEqual ("Mono", a
.KeyRestrictions
, "Mono");
164 a
.KeyRestrictions
= null;
165 Assert
.AreEqual (String
.Empty
, a
.KeyRestrictions
, "Empty(null)");
169 public void Attributes ()
171 Type t
= typeof (SqlClientPermissionAttribute
);
172 Assert
.IsTrue (t
.IsSerializable
, "IsSerializable");
174 object [] attrs
= t
.GetCustomAttributes (typeof (AttributeUsageAttribute
), false);
175 Assert
.AreEqual (1, attrs
.Length
, "AttributeUsage");
176 AttributeUsageAttribute aua
= (AttributeUsageAttribute
)attrs
[0];
177 Assert
.IsTrue (aua
.AllowMultiple
, "AllowMultiple");
178 Assert
.IsFalse (aua
.Inherited
, "Inherited");
179 AttributeTargets at
= AttributeTargets
.Assembly
| AttributeTargets
.Class
| AttributeTargets
.Struct
| AttributeTargets
.Constructor
| AttributeTargets
.Method
;
180 Assert
.AreEqual (at
, aua
.ValidOn
, "ValidOn");