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