Merge pull request #4503 from BrzVlad/fix-appdomain-unload
[mono.git] / mcs / class / System.Data / Test / System.Data.OleDb / OleDbPermissionTest.cs
1 //\r
2 // OleDbPermissionTest.cs - NUnit Test Cases for OleDbPermission\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 #if !NO_OLEDB\r
30 \r
31 using NUnit.Framework;\r
32 using System;\r
33 using System.Data;\r
34 using System.Data.Common;\r
35 using System.Data.OleDb;\r
36 using System.Security;\r
37 using System.Security.Permissions;\r
38 \r
39 namespace MonoTests.System.Data.OleDb {\r
40 \r
41         // NOTE: Most tests are are located in the base class, DBDataPermission\r
42 \r
43         [TestFixture]\r
44         public class OleDbPermissionTest {\r
45 \r
46                 private void Check (string msg, OleDbPermission perm, bool blank, bool unrestricted, int count)\r
47                 {\r
48                         Assert.AreEqual (blank, perm.AllowBlankPassword, msg + ".AllowBlankPassword");\r
49                         Assert.AreEqual (unrestricted, perm.IsUnrestricted (), msg + ".IsUnrestricted");\r
50                         if (count == 0)\r
51                                 Assert.IsNull (perm.ToXml ().Children, msg + ".Count != 0");\r
52                         else\r
53                                 Assert.AreEqual (count, perm.ToXml ().Children.Count, msg + ".Count");\r
54                         Assert.AreEqual (String.Empty, perm.Provider, "Provider");\r
55                 }\r
56 \r
57                 [Test]\r
58                 [ExpectedException (typeof (ArgumentOutOfRangeException))]\r
59                 public void PermissionState_Invalid ()\r
60                 {\r
61                         PermissionState ps = (PermissionState)Int32.MinValue;\r
62                         OleDbPermission perm = new OleDbPermission (ps);\r
63                 }\r
64 \r
65                 [Test]\r
66                 public void None ()\r
67                 {\r
68                         OleDbPermission perm = new OleDbPermission (PermissionState.None);\r
69                         Check ("None-1", perm, false, false, 0);\r
70                         perm.AllowBlankPassword = true;\r
71                         Check ("None-2", perm, true, false, 0);\r
72 \r
73                         OleDbPermission copy = (OleDbPermission)perm.Copy ();\r
74                         Check ("Copy_None-1", copy, true, false, 0);\r
75                         copy.AllowBlankPassword = false;\r
76                         Check ("Copy_None-2", copy, false, false, 0);\r
77                 }\r
78 \r
79                 [Test]\r
80                 public void None_Childs ()\r
81                 {\r
82                         OleDbPermission perm = new OleDbPermission (PermissionState.None);\r
83                         perm.Add ("data source=localhost;", String.Empty, KeyRestrictionBehavior.AllowOnly);\r
84                         perm.Add ("data source=127.0.0.1;", "password=;", KeyRestrictionBehavior.PreventUsage);\r
85 \r
86                         Check ("None-Childs-1", perm, false, false, 2);\r
87                         perm.AllowBlankPassword = true;\r
88                         Check ("None-Childs-2", perm, true, false, 2);\r
89 \r
90                         OleDbPermission copy = (OleDbPermission)perm.Copy ();\r
91                         Check ("Copy_None-Childs-1", copy, true, false, 2);\r
92                         copy.AllowBlankPassword = false;\r
93                         Check ("Copy_None-Childs-2", copy, false, false, 2);\r
94                 }\r
95 \r
96                 [Test]\r
97                 public void Unrestricted ()\r
98                 {\r
99                         OleDbPermission perm = new OleDbPermission (PermissionState.Unrestricted);\r
100                         Check ("Unrestricted-1", perm, false, true, 0);\r
101                         perm.AllowBlankPassword = true;\r
102                         Check ("Unrestricted-2", perm, true, true, 0);\r
103 \r
104                         OleDbPermission copy = (OleDbPermission)perm.Copy ();\r
105                         // note: Unrestricted is always created with default values (so AllowBlankPassword is false)\r
106                         Check ("Copy_Unrestricted-1", copy, false, true, 0);\r
107                         copy.AllowBlankPassword = true;\r
108                         Check ("Copy_Unrestricted-2", copy, true, true, 0);\r
109                 }\r
110 \r
111                 [Test]\r
112                 public void Unrestricted_Add ()\r
113                 {\r
114                         OleDbPermission perm = new OleDbPermission (PermissionState.Unrestricted);\r
115                         Check ("Unrestricted-NoChild", perm, false, true, 0);\r
116                         perm.Add ("data source=localhost;", String.Empty, KeyRestrictionBehavior.AllowOnly);\r
117                         // note: Lost unrestricted state when children was added\r
118                         Check ("Unrestricted-WithChild", perm, false, false, 1);\r
119                 }\r
120 \r
121                 [Test]\r
122                 public void Provider ()\r
123                 {\r
124                         OleDbPermission perm = new OleDbPermission (PermissionState.None);\r
125                         perm.Provider = String.Empty;\r
126                         Assert.AreEqual (String.Empty, perm.Provider, "Empty");\r
127                         perm.Provider = "Mono";\r
128                         Assert.AreEqual ("Mono", perm.Provider, "Mono");\r
129                         perm.Provider = null;\r
130                         Assert.AreEqual (String.Empty, perm.Provider, "Empty(null)");\r
131                 }\r
132         }\r
133 }\r
134 \r
135 #endif