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
{
41 [Ignore ("CAS is not supported and parts will be linked away")]
43 public class SqlClientPermissionAttributeTest
{
46 public void Default ()
48 SqlClientPermissionAttribute a
= new SqlClientPermissionAttribute (SecurityAction
.Assert
);
49 Assert
.AreEqual (a
.ToString (), a
.TypeId
.ToString (), "TypeId");
50 Assert
.IsFalse (a
.Unrestricted
, "Unrestricted");
51 Assert
.IsFalse (a
.AllowBlankPassword
, "AllowBlankPassword");
52 Assert
.AreEqual (String
.Empty
, a
.ConnectionString
, "ConnectionString");
53 Assert
.AreEqual (KeyRestrictionBehavior
.AllowOnly
, a
.KeyRestrictionBehavior
, "KeyRestrictionBehavior");
54 Assert
.AreEqual (String
.Empty
, a
.KeyRestrictions
, "KeyRestrictions");
56 Assert
.IsFalse (a
.ShouldSerializeConnectionString (), "ShouldSerializeConnectionString");
57 Assert
.IsFalse (a
.ShouldSerializeKeyRestrictions (), "ShouldSerializeConnectionString");
59 SqlClientPermission sp
= (SqlClientPermission
)a
.CreatePermission ();
60 Assert
.IsFalse (sp
.IsUnrestricted (), "IsUnrestricted");
66 SqlClientPermissionAttribute a
= new SqlClientPermissionAttribute (SecurityAction
.Assert
);
67 Assert
.AreEqual (SecurityAction
.Assert
, a
.Action
, "Action=Assert");
68 a
.Action
= SecurityAction
.Demand
;
69 Assert
.AreEqual (SecurityAction
.Demand
, a
.Action
, "Action=Demand");
70 a
.Action
= SecurityAction
.Deny
;
71 Assert
.AreEqual (SecurityAction
.Deny
, a
.Action
, "Action=Deny");
72 a
.Action
= SecurityAction
.InheritanceDemand
;
73 Assert
.AreEqual (SecurityAction
.InheritanceDemand
, a
.Action
, "Action=InheritanceDemand");
74 a
.Action
= SecurityAction
.LinkDemand
;
75 Assert
.AreEqual (SecurityAction
.LinkDemand
, a
.Action
, "Action=LinkDemand");
76 a
.Action
= SecurityAction
.PermitOnly
;
77 Assert
.AreEqual (SecurityAction
.PermitOnly
, a
.Action
, "Action=PermitOnly");
78 a
.Action
= SecurityAction
.RequestMinimum
;
79 Assert
.AreEqual (SecurityAction
.RequestMinimum
, a
.Action
, "Action=RequestMinimum");
80 a
.Action
= SecurityAction
.RequestOptional
;
81 Assert
.AreEqual (SecurityAction
.RequestOptional
, a
.Action
, "Action=RequestOptional");
82 a
.Action
= SecurityAction
.RequestRefuse
;
83 Assert
.AreEqual (SecurityAction
.RequestRefuse
, a
.Action
, "Action=RequestRefuse");
87 public void Action_Invalid ()
89 SqlClientPermissionAttribute a
= new SqlClientPermissionAttribute ((SecurityAction
)Int32
.MinValue
);
90 // no validation in attribute
94 public void Unrestricted ()
96 SqlClientPermissionAttribute a
= new SqlClientPermissionAttribute (SecurityAction
.Assert
);
97 a
.Unrestricted
= true;
98 SqlClientPermission scp
= (SqlClientPermission
)a
.CreatePermission ();
99 Assert
.IsTrue (scp
.IsUnrestricted (), "IsUnrestricted");
100 Assert
.IsFalse (a
.AllowBlankPassword
, "AllowBlankPassword");
101 Assert
.AreEqual (String
.Empty
, a
.ConnectionString
, "ConnectionString");
102 Assert
.AreEqual (KeyRestrictionBehavior
.AllowOnly
, a
.KeyRestrictionBehavior
, "KeyRestrictionBehavior");
103 Assert
.AreEqual (String
.Empty
, a
.KeyRestrictions
, "KeyRestrictions");
105 a
.Unrestricted
= false;
106 scp
= (SqlClientPermission
)a
.CreatePermission ();
107 Assert
.IsFalse (scp
.IsUnrestricted (), "!IsUnrestricted");
111 public void AllowBlankPassword ()
113 SqlClientPermissionAttribute a
= new SqlClientPermissionAttribute (SecurityAction
.Assert
);
114 Assert
.IsFalse (a
.AllowBlankPassword
, "Default");
115 a
.AllowBlankPassword
= true;
116 Assert
.IsTrue (a
.AllowBlankPassword
, "True");
117 a
.AllowBlankPassword
= false;
118 Assert
.IsFalse (a
.AllowBlankPassword
, "False");
122 public void ConnectionString ()
124 SqlClientPermissionAttribute a
= new SqlClientPermissionAttribute (SecurityAction
.Assert
);
125 a
.ConnectionString
= String
.Empty
;
126 Assert
.AreEqual (String
.Empty
, a
.ConnectionString
, "Empty");
127 a
.ConnectionString
= "Mono";
128 Assert
.AreEqual ("Mono", a
.ConnectionString
, "Mono");
129 a
.ConnectionString
= null;
130 Assert
.AreEqual (String
.Empty
, a
.ConnectionString
, "Empty(null)");
134 public void KeyRestrictionBehavior_All ()
136 SqlClientPermissionAttribute a
= new SqlClientPermissionAttribute (SecurityAction
.Assert
);
137 a
.KeyRestrictionBehavior
= KeyRestrictionBehavior
.AllowOnly
;
138 Assert
.AreEqual (KeyRestrictionBehavior
.AllowOnly
, a
.KeyRestrictionBehavior
, "AllowOnly");
139 a
.KeyRestrictionBehavior
= KeyRestrictionBehavior
.PreventUsage
;
140 Assert
.AreEqual (KeyRestrictionBehavior
.PreventUsage
, a
.KeyRestrictionBehavior
, "PreventUsage");
145 [ExpectedException (typeof (ArgumentOutOfRangeException
))]
147 [ExpectedException (typeof (ArgumentException
))]
149 public void KeyRestrictionBehavior_Invalid ()
151 SqlClientPermissionAttribute a
= new SqlClientPermissionAttribute (SecurityAction
.Assert
);
152 a
.KeyRestrictionBehavior
= (KeyRestrictionBehavior
)Int32
.MinValue
;
156 public void KeyRestriction ()
158 SqlClientPermissionAttribute a
= new SqlClientPermissionAttribute (SecurityAction
.Assert
);
159 a
.KeyRestrictions
= String
.Empty
;
160 Assert
.AreEqual (String
.Empty
, a
.KeyRestrictions
, "Empty");
161 a
.KeyRestrictions
= "Mono";
162 Assert
.AreEqual ("Mono", a
.KeyRestrictions
, "Mono");
163 a
.KeyRestrictions
= null;
164 Assert
.AreEqual (String
.Empty
, a
.KeyRestrictions
, "Empty(null)");
168 public void Attributes ()
170 Type t
= typeof (SqlClientPermissionAttribute
);
171 Assert
.IsTrue (t
.IsSerializable
, "IsSerializable");
173 object [] attrs
= t
.GetCustomAttributes (typeof (AttributeUsageAttribute
), false);
174 Assert
.AreEqual (1, attrs
.Length
, "AttributeUsage");
175 AttributeUsageAttribute aua
= (AttributeUsageAttribute
)attrs
[0];
176 Assert
.IsTrue (aua
.AllowMultiple
, "AllowMultiple");
177 Assert
.IsFalse (aua
.Inherited
, "Inherited");
178 AttributeTargets at
= AttributeTargets
.Assembly
| AttributeTargets
.Class
| AttributeTargets
.Struct
| AttributeTargets
.Constructor
| AttributeTargets
.Method
;
179 Assert
.AreEqual (at
, aua
.ValidOn
, "ValidOn");