1 //
2 // SqlClientPermissionAttributeTest.cs -
3 //	NUnit Test Cases for SqlClientPermissionAttribute
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 using NUnit.Framework;
31 using System;
32 using System.Data;
33 using System.Data.SqlClient;
34 using System.Security;
35 using System.Security.Permissions;
36 
37 namespace MonoTests.System.Data.SqlClient {
38 
39 	[TestFixture]
40 #if MOBILE
41 	[Ignore ("CAS is not supported and parts will be linked away")]
42 #endif
43 	public class SqlClientPermissionAttributeTest {
44 
45 		[Test]
Default()46 		public void Default ()
47 		{
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");
55 			Assert.IsFalse (a.ShouldSerializeConnectionString (), "ShouldSerializeConnectionString");
56 			Assert.IsFalse (a.ShouldSerializeKeyRestrictions (), "ShouldSerializeConnectionString");
57 			SqlClientPermission sp = (SqlClientPermission)a.CreatePermission ();
58 			Assert.IsFalse (sp.IsUnrestricted (), "IsUnrestricted");
59 		}
60 
61 		[Test]
Action()62 		public void Action ()
63 		{
64 			SqlClientPermissionAttribute a = new SqlClientPermissionAttribute (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 			SqlClientPermissionAttribute a = new SqlClientPermissionAttribute ((SecurityAction)Int32.MinValue);
88 			// no validation in attribute
89 		}
90 
91 		[Test]
Unrestricted()92 		public void Unrestricted ()
93 		{
94 			SqlClientPermissionAttribute a = new SqlClientPermissionAttribute (SecurityAction.Assert);
95 			a.Unrestricted = true;
96 			SqlClientPermission scp = (SqlClientPermission)a.CreatePermission ();
97 			Assert.IsTrue (scp.IsUnrestricted (), "IsUnrestricted");
98 			Assert.IsFalse (a.AllowBlankPassword, "AllowBlankPassword");
99 			Assert.AreEqual (String.Empty, a.ConnectionString, "ConnectionString");
100 			Assert.AreEqual (KeyRestrictionBehavior.AllowOnly, a.KeyRestrictionBehavior, "KeyRestrictionBehavior");
101 			Assert.AreEqual (String.Empty, a.KeyRestrictions, "KeyRestrictions");
102 
103 			a.Unrestricted = false;
104 			scp = (SqlClientPermission)a.CreatePermission ();
105 			Assert.IsFalse (scp.IsUnrestricted (), "!IsUnrestricted");
106 		}
107 
108 		[Test]
AllowBlankPassword()109 		public void AllowBlankPassword ()
110 		{
111 			SqlClientPermissionAttribute a = new SqlClientPermissionAttribute (SecurityAction.Assert);
112 			Assert.IsFalse (a.AllowBlankPassword, "Default");
113 			a.AllowBlankPassword = true;
114 			Assert.IsTrue (a.AllowBlankPassword, "True");
115 			a.AllowBlankPassword = false;
116 			Assert.IsFalse (a.AllowBlankPassword, "False");
117 		}
118 
119 		[Test]
ConnectionString()120 		public void ConnectionString ()
121 		{
122 			SqlClientPermissionAttribute a = new SqlClientPermissionAttribute (SecurityAction.Assert);
123 			a.ConnectionString = String.Empty;
124 			Assert.AreEqual (String.Empty, a.ConnectionString, "Empty");
125 			a.ConnectionString = "Mono";
126 			Assert.AreEqual ("Mono", a.ConnectionString, "Mono");
127 			a.ConnectionString = null;
128 			Assert.AreEqual (String.Empty, a.ConnectionString, "Empty(null)");
129 		}
130 
131 		[Test]
KeyRestrictionBehavior_All()132 		public void KeyRestrictionBehavior_All ()
133 		{
134 			SqlClientPermissionAttribute a = new SqlClientPermissionAttribute (SecurityAction.Assert);
135 			a.KeyRestrictionBehavior = KeyRestrictionBehavior.AllowOnly;
136 			Assert.AreEqual (KeyRestrictionBehavior.AllowOnly, a.KeyRestrictionBehavior, "AllowOnly");
137 			a.KeyRestrictionBehavior = KeyRestrictionBehavior.PreventUsage;
138 			Assert.AreEqual (KeyRestrictionBehavior.PreventUsage, a.KeyRestrictionBehavior, "PreventUsage");
139 		}
140 
141 		[Test]
142 		[ExpectedException (typeof (ArgumentOutOfRangeException))]
KeyRestrictionBehavior_Invalid()143 		public void KeyRestrictionBehavior_Invalid ()
144 		{
145 			SqlClientPermissionAttribute a = new SqlClientPermissionAttribute (SecurityAction.Assert);
146 			a.KeyRestrictionBehavior = (KeyRestrictionBehavior)Int32.MinValue;
147 		}
148 
149 		[Test]
KeyRestriction()150 		public void KeyRestriction ()
151 		{
152 			SqlClientPermissionAttribute a = new SqlClientPermissionAttribute (SecurityAction.Assert);
153 			a.KeyRestrictions = String.Empty;
154 			Assert.AreEqual (String.Empty, a.KeyRestrictions, "Empty");
155 			a.KeyRestrictions = "Mono";
156 			Assert.AreEqual ("Mono", a.KeyRestrictions, "Mono");
157 			a.KeyRestrictions = null;
158 			Assert.AreEqual (String.Empty, a.KeyRestrictions, "Empty(null)");
159 		}
160 
161 		[Test]
Attributes()162 		public void Attributes ()
163 		{
164 			Type t = typeof (SqlClientPermissionAttribute);
165 			Assert.IsTrue (t.IsSerializable, "IsSerializable");
166 
167 			object [] attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), false);
168 			Assert.AreEqual (1, attrs.Length, "AttributeUsage");
169 			AttributeUsageAttribute aua = (AttributeUsageAttribute)attrs [0];
170 			Assert.IsTrue (aua.AllowMultiple, "AllowMultiple");
171 			Assert.IsFalse (aua.Inherited, "Inherited");
172 			AttributeTargets at = AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method;
173 			Assert.AreEqual (at, aua.ValidOn, "ValidOn");
174 		}
175 	}
176 }
177