Merge pull request #601 from knocte/sock_improvements
[mono.git] / mcs / class / System.Data / Test / System.Data.SqlClient / SqlClientPermissionTest.cs
1 //\r
2 // SqlClientPermissionTest.cs - NUnit Test Cases for SqlClientPermission\r
3 //\r
4 // Author:\r
5 //      Sebastien Pouliot  <sebastien@ximian.com>\r
6 //\r
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)\r
8 //\r
9 // Permission is hereby granted, free of charge, to any person obtaining\r
10 // a copy of this software and associated documentation files (the\r
11 // "Software"), to deal in the Software without restriction, including\r
12 // without limitation the rights to use, copy, modify, merge, publish,\r
13 // distribute, sublicense, and/or sell copies of the Software, and to\r
14 // permit persons to whom the Software is furnished to do so, subject to\r
15 // the following conditions:\r
16 // \r
17 // The above copyright notice and this permission notice shall be\r
18 // included in all copies or substantial portions of the Software.\r
19 // \r
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
27 //\r
28 \r
29 using NUnit.Framework;\r
30 using System;\r
31 using System.Data;\r
32 using System.Data.Common;\r
33 using System.Data.SqlClient;\r
34 using System.Security;\r
35 using System.Security.Permissions;\r
36 \r
37 namespace MonoTests.System.Data.SqlClient {\r
38 \r
39         // NOTE: Most tests are are located in the base class, DBDataPermission\r
40 \r
41         [TestFixture]\r
42 #if MOBILE\r
43         [Ignore ("CAS is not supported and parts will be linked away")]\r
44 #endif\r
45         public class SqlClientPermissionTest {\r
46 \r
47                 private void Check (string msg, DBDataPermission dbdp, bool blank, bool unrestricted, int count)\r
48                 {\r
49                         Assert.AreEqual (blank, dbdp.AllowBlankPassword, msg + ".AllowBlankPassword");\r
50                         Assert.AreEqual (unrestricted, dbdp.IsUnrestricted (), msg + ".IsUnrestricted");\r
51                         if (count == 0)\r
52                                 Assert.IsNull (dbdp.ToXml ().Children, msg + ".Count != 0");\r
53                         else\r
54                                 Assert.AreEqual (count, dbdp.ToXml ().Children.Count, msg + ".Count");\r
55                 }\r
56 \r
57                 [Test]\r
58 #if NET_2_0\r
59                 [ExpectedException (typeof (ArgumentOutOfRangeException))]\r
60 #else\r
61                 [ExpectedException (typeof (ArgumentException))]\r
62 #endif\r
63                 public void PermissionState_Invalid ()\r
64                 {\r
65                         PermissionState ps = (PermissionState)Int32.MinValue;\r
66                         SqlClientPermission perm = new SqlClientPermission (ps);\r
67                 }\r
68 \r
69                 [Test]\r
70                 public void None ()\r
71                 {\r
72                         SqlClientPermission perm = new SqlClientPermission (PermissionState.None);\r
73                         Check ("None-1", perm, false, false, 0);\r
74                         perm.AllowBlankPassword = true;\r
75                         Check ("None-2", perm, true, false, 0);\r
76 \r
77                         SqlClientPermission copy = (SqlClientPermission)perm.Copy ();\r
78                         Check ("Copy_None-1", copy, true, false, 0);\r
79                         copy.AllowBlankPassword = false;\r
80                         Check ("Copy_None-2", copy, false, false, 0);\r
81                 }\r
82 \r
83                 [Test]\r
84                 public void None_Childs ()\r
85                 {\r
86                         SqlClientPermission perm = new SqlClientPermission (PermissionState.None);\r
87                         perm.Add ("data source=localhost;", String.Empty, KeyRestrictionBehavior.AllowOnly);\r
88                         perm.Add ("data source=127.0.0.1;", "password=;", KeyRestrictionBehavior.PreventUsage);\r
89 \r
90                         Check ("None-Childs-1", perm, false, false, 2);\r
91                         perm.AllowBlankPassword = true;\r
92                         Check ("None-Childs-2", perm, true, false, 2);\r
93 \r
94                         SqlClientPermission copy = (SqlClientPermission)perm.Copy ();\r
95                         Check ("Copy_None-Childs-1", copy, true, false, 2);\r
96                         copy.AllowBlankPassword = false;\r
97                         Check ("Copy_None-Childs-2", copy, false, false, 2);\r
98                 }\r
99 \r
100                 [Test]\r
101                 public void Unrestricted ()\r
102                 {\r
103                         SqlClientPermission perm = new SqlClientPermission (PermissionState.Unrestricted);\r
104                         Check ("Unrestricted-1", perm, false, true, 0);\r
105                         perm.AllowBlankPassword = true;\r
106                         Check ("Unrestricted-2", perm, true, true, 0);\r
107 \r
108                         SqlClientPermission copy = (SqlClientPermission)perm.Copy ();\r
109                         // note: Unrestricted is always created with default values (so AllowBlankPassword is false)\r
110                         Check ("Copy_Unrestricted-1", copy, false, true, 0);\r
111                         copy.AllowBlankPassword = true;\r
112                         Check ("Copy_Unrestricted-2", copy, true, true, 0);\r
113                 }\r
114 \r
115                 [Test]\r
116                 public void Unrestricted_Add ()\r
117                 {\r
118                         SqlClientPermission perm = new SqlClientPermission (PermissionState.Unrestricted);\r
119                         Check ("Unrestricted-NoChild", perm, false, true, 0);\r
120                         perm.Add ("data source=localhost;", String.Empty, KeyRestrictionBehavior.AllowOnly);\r
121                         // note: Lost unrestricted state when children was added\r
122                         Check ("Unrestricted-WithChild", perm, false, false, 1);\r
123                 }\r
124         }\r
125 }\r