2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Data / Test / System.Data.Common / DBDataPermissionTest.cs
1 //
2 // DBDataPermissionTest.cs - NUnit Test Cases for DBDataPermission
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.Security;
34 using System.Security.Permissions;
35 using System.IO;
36 namespace MonoTests.System.Data.Common {
37
38         public class NonAbstractDBDataPermission : DBDataPermission {
39
40 #if !NET_2_0
41                 public NonAbstractDBDataPermission () 
42                         : base ()
43                 {
44                 }
45
46                 public NonAbstractDBDataPermission (DBDataPermission permission, bool allowBlankPassword)
47                         : base (permission)
48                 {
49                         AllowBlankPassword = allowBlankPassword;
50                 }
51 #else
52                 // make Copy and CreateInstance work :)
53                 public NonAbstractDBDataPermission () 
54                         : base (PermissionState.None)
55                 {
56                 }
57 #endif
58                 public NonAbstractDBDataPermission (PermissionState state)
59                         : base (state)
60                 {
61                 }
62
63                 public NonAbstractDBDataPermission (DBDataPermission permission)
64                         : base (permission)
65                 {
66                 }
67
68                 public NonAbstractDBDataPermission (DBDataPermissionAttribute permissionAttribute)
69                         : base (permissionAttribute)
70                 {
71                 }
72 #if NET_2_0
73                 public NonAbstractDBDataPermission (DbConnectionOptions connectionOptions)
74                         : base (connectionOptions)
75                 {
76                 }
77 #endif
78
79                 public new void Clear ()
80                 {
81                         base.Clear ();
82                 }
83
84                 public new DBDataPermission CreateInstance ()
85                 {
86                         return base.CreateInstance ();
87                 }
88         }
89
90         [TestFixture]
91         public class DBDataPermissionTest {
92
93                 private const string defaultConnectString = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;";
94                 private const string defaultConnectString2 = "Data Source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=Northwind;";
95
96                 private void Check (string msg, NonAbstractDBDataPermission dbdp, bool blank, bool unrestricted, int count)
97                 {
98                         Assert.AreEqual (blank, dbdp.AllowBlankPassword, msg + ".AllowBlankPassword");
99                         Assert.AreEqual (unrestricted, dbdp.IsUnrestricted (), msg + ".IsUnrestricted");
100                         if (count == 0)
101                                 Assert.IsNull (dbdp.ToXml ().Children, msg + ".Count != 0");
102                         else
103                                 Assert.AreEqual (count, dbdp.ToXml ().Children.Count, msg + ".Count");
104                 }
105
106 #if !NET_2_0
107                 [Test]
108                 public void Constructor_Empty ()
109                 {
110                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission ();
111                         Check ("Empty", dbdp, false, false, 0);
112                 }
113 #elif !NET_1_1
114                 [Test]
115                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
116                 public void Constructor_PermissionStateBoolean_Invalid ()
117                 {
118                         PermissionState ps = (PermissionState) Int32.MinValue;
119                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (ps, false);
120                 }
121
122                 [Test]
123                 public void Constructor_PermissionStateBoolean_None ()
124                 {
125                         PermissionState ps = PermissionState.None;
126                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (ps, false);
127                         Check ("None,False", dbdp, false, false, 0);
128
129                         dbdp = new NonAbstractDBDataPermission (ps, true);
130                         Check ("None,True", dbdp, true, false, 0);
131                 }
132
133                 [Test]
134                 public void Constructor_PermissionStateBoolean_Unrestricted ()
135                 {
136                         PermissionState ps = PermissionState.Unrestricted;
137                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (ps, false);
138                         Check ("Unrestricted,False", dbdp, false, true, 0);
139
140                         dbdp = new NonAbstractDBDataPermission (ps, true);
141                         Check ("Unrestricted,True", dbdp, true, true, 0);
142                 }
143 #endif
144                 [Test]
145                 [ExpectedException (typeof (ArgumentNullException))]
146                 public void Constructor_DBDataPermission_Null ()
147                 {
148                         DBDataPermission p = null;
149                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (p);
150                 }
151
152                 [Test]
153                 public void Constructor_DBDataPermission ()
154                 {
155                         DBDataPermission p = new NonAbstractDBDataPermission (PermissionState.None);
156                         p.AllowBlankPassword = true;
157                         p.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
158
159                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (p);
160                         Check ("DBDataPermission", dbdp, true, false, 1);
161                 }
162
163                 [Test]
164                 [ExpectedException (typeof (ArgumentNullException))]
165                 public void Constructor_DBDataPermissionAttribute_Null ()
166                 {
167                         DBDataPermissionAttribute a = null;
168                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (a);
169                 }
170
171                 [Test]
172                 public void Constructor_DBDataPermissionAttribute ()
173                 {
174                         DBDataPermissionAttribute a = new NonAbstractDBDataPermissionAttribute (SecurityAction.Assert);
175                         a.AllowBlankPassword = true;
176
177                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (a);
178                         Check ("DBDataPermissionAttribute-0", dbdp, true, false, 0);
179
180                         a.Unrestricted = true;
181                         dbdp = new NonAbstractDBDataPermission (a);
182                         Check ("DBDataPermissionAttribute-1", dbdp, false, true, 0);
183                         // Unrestricted "bypass" the AllowBlankPassword (so it report false)
184
185                         a.ConnectionString = defaultConnectString;
186                         dbdp = new NonAbstractDBDataPermission (a);
187                         Check ("DBDataPermissionAttribute-2", dbdp, false, true, 0);
188                         // Unrestricted "bypass" the ConnectionString (so it report 0 childs)
189
190                         a.Unrestricted = false;
191                         dbdp = new NonAbstractDBDataPermission (a);
192                         Check ("DBDataPermissionAttribute-3", dbdp, true, false, 1);
193                 }
194 #if NET_2_0
195                 [Test]
196 //              [ExpectedException (typeof (ArgumentNullException))]
197                 public void Constructor_DbConnectionOptions_Null ()
198                 {
199                         DbConnectionOptions o = null;
200                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (o);
201                         Check ("DbConnectionOptions_Null", dbdp, false, false, 0);
202                 }
203
204                 [Test]
205                 public void Constructor_DbConnectionOptions ()
206                 {
207                         DbConnectionOptions o = new DbConnectionOptions (defaultConnectString);
208                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (o);
209                         Check ("DbConnectionOptions", dbdp, false, false, 1);
210                 }
211 #endif
212                 [Test]
213 #if NET_2_0
214                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
215 #else
216                 [ExpectedException (typeof (ArgumentException))]
217 #endif
218                 public void Constructor_PermissionState_Invalid ()
219                 {
220                         PermissionState ps = (PermissionState) Int32.MinValue;
221                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (ps);
222                 }
223
224                 [Test]
225                 public void Constructor_PermissionState_None ()
226                 {
227                         PermissionState ps = PermissionState.None;
228                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (ps);
229                         Check ("PermissionState_None-1", dbdp, false, false, 0);
230                         dbdp.AllowBlankPassword = true;
231                         Check ("PermissionState_None-1", dbdp, true, false, 0);
232                 }
233
234                 [Test]
235                 public void Constructor_PermissionState_Unrestricted ()
236                 {
237                         PermissionState ps = PermissionState.Unrestricted;
238                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (ps);
239                         Check ("PermissionState_Unrestricted-1", dbdp, false, true, 0);
240                         dbdp.AllowBlankPassword = true;
241                         Check ("PermissionState_Unrestricted-2", dbdp, true, true, 0);
242                 }
243
244                 [Test]
245                 public void AllowBlankPassword ()
246                 {
247                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.None);
248                         Assert.IsFalse (dbdp.AllowBlankPassword, "Default");
249                         dbdp.AllowBlankPassword = true;
250                         Assert.IsTrue (dbdp.AllowBlankPassword, "True");
251                         dbdp.Clear ();
252                         // clear the connection list - not the permission itself
253                         Assert.IsTrue (dbdp.AllowBlankPassword, "Clear");
254                         dbdp.AllowBlankPassword = false;
255                         Assert.IsFalse (dbdp.AllowBlankPassword, "False");
256                 }
257
258                 [Test]
259                 public void Add ()
260                 {
261                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.None);
262                         dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
263                         Assert.AreEqual (1, dbdp.ToXml ().Children.Count, "Count");
264
265                         NonAbstractDBDataPermission copy = (NonAbstractDBDataPermission)dbdp.Copy ();
266                         Assert.AreEqual (1, copy.ToXml ().Children.Count, "Copy.Count");
267
268                         dbdp.Clear ();
269                         Assert.IsNull (dbdp.ToXml ().Children, "Clear");
270                         Assert.AreEqual (1, copy.ToXml ().Children.Count, "Copy.Count-2");
271                 }
272
273                 [Test]
274                 public void Add_Duplicates ()
275                 {
276                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.None);
277                         dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
278                         dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
279                         // no exception but a single element is kept
280                         Assert.AreEqual (1, dbdp.ToXml ().Children.Count, "Count");
281                         dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.PreventUsage);
282
283                         dbdp.Clear ();
284                         Assert.IsNull (dbdp.ToXml ().Children, "Clear");
285                 }
286
287                 [Test]
288                 public void Add_Differents ()
289                 {
290                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.None);
291                         dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
292                         string connectString = "Data Source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=Northwind;";
293                         dbdp.Add (connectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
294                         Assert.AreEqual (2, dbdp.ToXml ().Children.Count, "Count");
295
296                         dbdp.Clear ();
297                         Assert.IsNull (dbdp.ToXml ().Children, "Clear");
298                 }
299
300                 [Test]
301                 public void Add_Unrestricted ()
302                 {
303                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
304                         Assert.IsTrue (dbdp.IsUnrestricted (), "IsUnrestricted-1");
305                         // we lose unrestricted by adding an element
306                         dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
307                         Assert.IsFalse (dbdp.IsUnrestricted (), "IsUnrestricted-2");
308                         // removing the element doesn't regain unrestricted status
309                         dbdp.Clear ();
310                         Assert.IsFalse (dbdp.IsUnrestricted (), "IsUnrestricted-3");
311                 }
312
313                 [Test]
314                 public void CreateInstance ()
315                 {
316                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
317                         Assert.AreEqual (typeof (NonAbstractDBDataPermission), dbdp.CreateInstance ().GetType (), "Same type"); 
318                 }
319
320
321                 [Test]
322                 public void Intersect_Null ()
323                 {
324                         NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
325                         // No intersection with null
326                         Assert.IsNull (elp.Intersect (null), "None N null");
327                 }
328
329                 [Test]
330                 public void Intersect ()
331                 {
332                         NonAbstractDBDataPermission dbdp1 = new NonAbstractDBDataPermission (PermissionState.None);
333                         NonAbstractDBDataPermission dbdp2 = new NonAbstractDBDataPermission (PermissionState.None);
334                         
335                         // 1. None N None
336                         NonAbstractDBDataPermission result = (NonAbstractDBDataPermission) dbdp1.Intersect (dbdp2);
337                         Assert.IsNull (result, "Empty N Empty");
338                         
339                         // 2. None N Entry
340                         dbdp2.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
341                         result = (NonAbstractDBDataPermission)dbdp1.Intersect (dbdp2);
342                         Assert.IsNull (result, "Empty N Entry");
343
344                         // 3. Entry N None
345                         result = (NonAbstractDBDataPermission)dbdp2.Intersect (dbdp1);
346                         Assert.IsNull (result, "Entry N Empty");
347
348                         // 4. Unrestricted N None
349                         NonAbstractDBDataPermission unr = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
350                         result = (NonAbstractDBDataPermission)unr.Intersect (dbdp1);
351                         Check ("(Unrestricted N None)", result, false, false, 0);
352
353                         // 5. None N Unrestricted
354                         result = (NonAbstractDBDataPermission)dbdp1.Intersect (unr);
355                         Check ("(None N Unrestricted)", result, false, false, 0);
356
357                         // 6. Unrestricted N Unrestricted
358                         result = (NonAbstractDBDataPermission)unr.Intersect (unr);
359                         Check ("(Unrestricted N Unrestricted)", result, false, true, 0);
360
361                         // 7. Unrestricted N Entry
362                         result = (NonAbstractDBDataPermission)unr.Intersect (dbdp2);
363                         Check ("(Unrestricted N Entry)", result, false, false, 1);
364
365                         // 8. Entry N Unrestricted
366                         result = (NonAbstractDBDataPermission)dbdp2.Intersect (unr);
367                         Check ("(Entry N Unrestricted)", result, false, false, 1);
368
369                         // 9. Entry2 N Entry2
370                         result = (NonAbstractDBDataPermission)dbdp2.Intersect (dbdp2);
371                         Check ("(Entry2 N Entry2)", result, false, false, 1);
372
373                         // 10. Entry1 N Entry 2
374                         dbdp1.Add (defaultConnectString2, String.Empty, KeyRestrictionBehavior.PreventUsage);
375                         result = (NonAbstractDBDataPermission)dbdp1.Intersect (dbdp2);
376                         Assert.IsNull (result, "(Entry1 N Entry2)");
377
378                         // 11. Entry2 N Entry 1
379                         result = (NonAbstractDBDataPermission)dbdp2.Intersect (dbdp1);
380                         Assert.IsNull (result, "(Entry2 N Entry1)");
381                 }
382
383                 [Test]
384                 public void Intersect_AllowBlankPassword ()
385                 {
386                         NonAbstractDBDataPermission ptrue = new NonAbstractDBDataPermission (PermissionState.None);
387                         ptrue.AllowBlankPassword = true;
388                         ptrue.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
389                         NonAbstractDBDataPermission pfalse = new NonAbstractDBDataPermission (PermissionState.None);
390                         pfalse.AllowBlankPassword = false;
391                         pfalse.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
392
393                         NonAbstractDBDataPermission intersect = (NonAbstractDBDataPermission)ptrue.Intersect (ptrue);
394                         Check ("true N true", intersect, true, false, 1);
395                         intersect = (NonAbstractDBDataPermission)ptrue.Intersect (pfalse);
396                         Check ("true N false", intersect, false, false, 1);
397                         intersect = (NonAbstractDBDataPermission)pfalse.Intersect (ptrue);
398                         Check ("false N true", intersect, false, false, 1);
399                         intersect = (NonAbstractDBDataPermission)pfalse.Intersect (pfalse);
400                         Check ("false N false", intersect, false, false, 1);
401                 }
402
403                 [Test]
404                 [ExpectedException (typeof (ArgumentException))]
405                 public void Intersect_BadPermission ()
406                 {
407                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
408                         dbdp.Intersect (new SecurityPermission (SecurityPermissionFlag.Assertion));
409                 }
410
411                 [Test]
412                 public void IsSubset_Null ()
413                 {
414                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.None);
415                         Assert.IsTrue (dbdp.IsSubsetOf (null), "Empty-null");
416
417                         dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
418                         Assert.IsFalse (dbdp.IsSubsetOf (null), "Element-null");
419
420                         dbdp = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
421                         Assert.IsFalse (dbdp.IsSubsetOf (null), "Unrestricted-null");
422                 }
423
424                 [Test]
425                 public void IsSubset ()
426                 {
427                         NonAbstractDBDataPermission empty = new NonAbstractDBDataPermission (PermissionState.None);
428                         Assert.IsTrue (empty.IsSubsetOf (empty), "Empty-Empty");
429
430                         NonAbstractDBDataPermission dbdp1 = new NonAbstractDBDataPermission (PermissionState.None);
431                         dbdp1.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
432                         Assert.IsTrue (empty.IsSubsetOf (dbdp1), "Empty-1");
433                         Assert.IsFalse (dbdp1.IsSubsetOf (empty), "1-Empty");
434                         Assert.IsTrue (dbdp1.IsSubsetOf (dbdp1), "1-1");
435
436                         NonAbstractDBDataPermission dbdp2 = (NonAbstractDBDataPermission)dbdp1.Copy ();
437                         dbdp2.Add (defaultConnectString2, String.Empty, KeyRestrictionBehavior.AllowOnly);
438                         Assert.IsTrue (dbdp1.IsSubsetOf (dbdp2), "1-2");
439                         Assert.IsFalse (dbdp2.IsSubsetOf (dbdp1), "2-1");
440                         Assert.IsTrue (dbdp2.IsSubsetOf (dbdp2), "2-2");
441
442                         NonAbstractDBDataPermission dbdp3 = new NonAbstractDBDataPermission (PermissionState.None);
443                         dbdp3.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.PreventUsage);
444                         Assert.IsTrue (dbdp3.IsSubsetOf (dbdp1), "3-1");
445                         Assert.IsTrue (dbdp1.IsSubsetOf (dbdp3), "1-3");
446                         Assert.IsTrue (dbdp3.IsSubsetOf (dbdp3), "3-3");
447
448                         NonAbstractDBDataPermission unr = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
449                         Assert.IsTrue (dbdp1.IsSubsetOf (unr), "1-unrestricted");
450                         Assert.IsFalse (unr.IsSubsetOf (dbdp1), "unrestricted-1");
451                         Assert.IsTrue (dbdp2.IsSubsetOf (unr), "2-unrestricted");
452                         Assert.IsFalse (unr.IsSubsetOf (dbdp2), "unrestricted-2");
453                         Assert.IsTrue (dbdp3.IsSubsetOf (unr), "3-unrestricted");
454                         Assert.IsFalse (unr.IsSubsetOf (dbdp3), "unrestricted-3");
455                         Assert.IsTrue (unr.IsSubsetOf (unr), "unrestricted-unrestricted");
456                 }
457
458                 [Test]
459                 public void IsSubsetOf_AllowBlankPassword ()
460                 {
461                         NonAbstractDBDataPermission ptrue = new NonAbstractDBDataPermission (PermissionState.None);
462                         ptrue.AllowBlankPassword = true;
463                         ptrue.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
464                         NonAbstractDBDataPermission pfalse = new NonAbstractDBDataPermission (PermissionState.None);
465                         pfalse.AllowBlankPassword = false;
466                         pfalse.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
467
468                         Assert.IsTrue (ptrue.IsSubsetOf (ptrue), "true subsetof true");
469                         Assert.IsFalse (ptrue.IsSubsetOf (pfalse), "true subsetof false");
470                         Assert.IsTrue (pfalse.IsSubsetOf (ptrue), "false subsetof true");
471                         Assert.IsTrue (pfalse.IsSubsetOf (pfalse), "false subsetof false");
472                 }
473
474                 [Test]
475                 public void IsSubsetOf_BothEmpty_KeyRestrictionBehavior ()
476                 {
477                         NonAbstractDBDataPermission pAllow = new NonAbstractDBDataPermission (PermissionState.None);
478                         pAllow.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
479                         NonAbstractDBDataPermission pPrevent = new NonAbstractDBDataPermission (PermissionState.None);
480                         pPrevent.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.PreventUsage);
481
482                         Assert.IsTrue (pAllow.IsSubsetOf (pAllow), "BothEmpty - pAllow subsetof pAllow");
483                         Assert.IsTrue (pAllow.IsSubsetOf (pPrevent), "BothEmpty - pAllow subsetof pPrevent");
484                         Assert.IsTrue (pPrevent.IsSubsetOf (pAllow), "BothEmpty - pPrevent subsetof pAllow");
485                         Assert.IsTrue (pPrevent.IsSubsetOf (pPrevent), "BothEmpty - pPrevent subsetof pPrevent");
486                 }
487
488                 [Test]
489                 public void IsSubsetOf_EmptyAllow_Prevent_KeyRestrictionBehavior ()
490                 {
491                         NonAbstractDBDataPermission pAllow = new NonAbstractDBDataPermission (PermissionState.None);
492                         pAllow.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
493                         NonAbstractDBDataPermission pPrevent = new NonAbstractDBDataPermission (PermissionState.None);
494                         pPrevent.Add (defaultConnectString, "password=;", KeyRestrictionBehavior.PreventUsage);
495
496                         Assert.IsTrue (pAllow.IsSubsetOf (pAllow), "EmptyAllow_Prevent - pAllow subsetof pAllow");
497                         Assert.IsTrue (pAllow.IsSubsetOf (pPrevent), "EmptyAllow_Prevent - pAllow subsetof pPrevent");
498                         Assert.IsTrue (pPrevent.IsSubsetOf (pAllow), "EmptyAllow_Prevent - pPrevent subsetof pAllow");
499                         Assert.IsTrue (pPrevent.IsSubsetOf (pPrevent), "EmptyAllow_Prevent - pPrevent subsetof pPrevent");
500                 }
501
502                 [Test]
503                 public void IsSubsetOf_Allow_EmptyPrevent_KeyRestrictionBehavior ()
504                 {
505                         NonAbstractDBDataPermission pAllow = new NonAbstractDBDataPermission (PermissionState.None);
506                         pAllow.Add (defaultConnectString, "data source=;", KeyRestrictionBehavior.AllowOnly);
507                         NonAbstractDBDataPermission pPrevent = new NonAbstractDBDataPermission (PermissionState.None);
508                         pPrevent.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.PreventUsage);
509
510                         Assert.IsTrue (pAllow.IsSubsetOf (pAllow), "Allow_EmptyPrevent - pAllow subsetof pAllow");
511                         Assert.IsTrue (pAllow.IsSubsetOf (pPrevent), "Allow_EmptyPrevent - pAllow subsetof pPrevent");
512                         Assert.IsTrue (pPrevent.IsSubsetOf (pAllow), "Allow_EmptyPrevent - pPrevent subsetof pAllow");
513                         Assert.IsTrue (pPrevent.IsSubsetOf (pPrevent), "Allow_EmptyPrevent - pPrevent subsetof pPrevent");
514                 }
515
516                 [Test]
517                 public void IsSubsetOf_AllowPreventSame_KeyRestrictionBehavior ()
518                 {
519                         NonAbstractDBDataPermission pAllow = new NonAbstractDBDataPermission (PermissionState.None);
520                         pAllow.Add (defaultConnectString, "password=;", KeyRestrictionBehavior.AllowOnly);
521                         NonAbstractDBDataPermission pPrevent = new NonAbstractDBDataPermission (PermissionState.None);
522                         pPrevent.Add (defaultConnectString, "password=;", KeyRestrictionBehavior.PreventUsage);
523
524                         Assert.IsTrue (pAllow.IsSubsetOf (pAllow), "AllowPreventSame - pAllow subsetof pAllow");
525                         Assert.IsTrue (pAllow.IsSubsetOf (pPrevent), "AllowPreventSame - pAllow subsetof pPrevent");
526                         Assert.IsTrue (pPrevent.IsSubsetOf (pAllow), "AllowPreventSame - pPrevent subsetof pAllow");
527                         Assert.IsTrue (pPrevent.IsSubsetOf (pPrevent), "AllowPreventSame - pPrevent subsetof pPrevent");
528                 }
529
530                 [Test]
531                 public void IsSubsetOf_AllowPreventDifferent_KeyRestrictionBehavior ()
532                 {
533                         NonAbstractDBDataPermission pAllow1 = new NonAbstractDBDataPermission (PermissionState.None);
534                         pAllow1.Add (defaultConnectString, "security=;", KeyRestrictionBehavior.AllowOnly);
535                         NonAbstractDBDataPermission pAllow2 = new NonAbstractDBDataPermission (PermissionState.None);
536                         pAllow2.Add (defaultConnectString, "password=;", KeyRestrictionBehavior.AllowOnly);
537                         NonAbstractDBDataPermission pPrevent1 = new NonAbstractDBDataPermission (PermissionState.None);
538                         pPrevent1.Add (defaultConnectString, "security=;", KeyRestrictionBehavior.PreventUsage);
539                         NonAbstractDBDataPermission pPrevent2 = new NonAbstractDBDataPermission (PermissionState.None);
540                         pPrevent2.Add (defaultConnectString, "password=;", KeyRestrictionBehavior.PreventUsage);
541
542                         Assert.IsTrue (pAllow1.IsSubsetOf (pAllow1), "AllowPreventDifferent - pAllow subsetof pAllow");
543                         Assert.IsTrue (pAllow1.IsSubsetOf (pPrevent2), "AllowPreventDifferent - pAllow subsetof pPrevent");
544                         Assert.IsTrue (pPrevent1.IsSubsetOf (pAllow2), "AllowPreventDifferent - pPrevent subsetof pAllow");
545                         Assert.IsTrue (pPrevent1.IsSubsetOf (pPrevent2), "AllowPreventDifferent - pPrevent subsetof pPrevent");
546                 }
547
548                 [Test]
549                 [ExpectedException (typeof (ArgumentException))]
550                 public void IsSubsetOf_BadPermission ()
551                 {
552                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
553                         dbdp.IsSubsetOf (new SecurityPermission (SecurityPermissionFlag.Assertion));
554                 }
555
556                 [Test]
557                 public void Union_Null ()
558                 {
559                         NonAbstractDBDataPermission dbdp = new NonAbstractDBDataPermission (PermissionState.None);
560                         NonAbstractDBDataPermission union = (NonAbstractDBDataPermission) dbdp.Union (null);
561                         Check ("Empty U null", dbdp, false, false, 0);
562
563                         dbdp.AllowBlankPassword = true;
564                         dbdp.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
565                         union = (NonAbstractDBDataPermission) dbdp.Union (null);
566                         Check ("Element U null", dbdp, true, false, 1);
567
568                         dbdp = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
569                         union = (NonAbstractDBDataPermission) dbdp.Union (null);
570                         Check ("Unrestricted U null", dbdp, false, true, 0);
571                 }
572
573                 [Test]
574                 public void Union ()
575                 {
576                         NonAbstractDBDataPermission empty = new NonAbstractDBDataPermission (PermissionState.None);
577                         NonAbstractDBDataPermission union = (NonAbstractDBDataPermission) empty.Union (empty);
578                         Assert.IsNotNull (union, "Empty U Empty");
579
580                         NonAbstractDBDataPermission dbdp1 = new NonAbstractDBDataPermission (PermissionState.None);
581                         dbdp1.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
582                         union = (NonAbstractDBDataPermission) empty.Union (dbdp1);
583                         Check ("Empty U 1", union, false, false, 1);
584
585                         NonAbstractDBDataPermission dbdp2 = (NonAbstractDBDataPermission)dbdp1.Copy ();
586                         dbdp2.Add (defaultConnectString2, String.Empty, KeyRestrictionBehavior.AllowOnly);
587                         union = (NonAbstractDBDataPermission) dbdp1.Union (dbdp2);
588                         Check ("1 U 2", union, false, false, 2);
589
590                         NonAbstractDBDataPermission dbdp3 = new NonAbstractDBDataPermission (PermissionState.None);
591                         dbdp3.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.PreventUsage);
592                         union = (NonAbstractDBDataPermission) dbdp2.Union (dbdp3);
593                         Check ("2 U 3", union, false, false, 2);
594
595                         NonAbstractDBDataPermission dbdp4 = new NonAbstractDBDataPermission (PermissionState.None);
596                         dbdp4.Add (defaultConnectString, "Data Source=;", KeyRestrictionBehavior.PreventUsage);
597                         union = (NonAbstractDBDataPermission) dbdp3.Union (dbdp4);
598                         Check ("3 U 4", union, false, false, 1);
599
600                         NonAbstractDBDataPermission unr = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
601                         union = (NonAbstractDBDataPermission) unr.Union (empty);
602                         Check ("unrestricted U empty", union, false, true, 0);
603
604                         union = (NonAbstractDBDataPermission)unr.Union (dbdp4);
605                         Check ("unrestricted U 4", union, false, true, 0);
606                 }
607
608                 [Test]
609                 public void Union_AllowBlankPassword ()
610                 {
611                         NonAbstractDBDataPermission ptrue = new NonAbstractDBDataPermission (PermissionState.None);
612                         ptrue.AllowBlankPassword = true;
613                         ptrue.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
614                         NonAbstractDBDataPermission pfalse = new NonAbstractDBDataPermission (PermissionState.None);
615                         pfalse.AllowBlankPassword = false;
616                         pfalse.Add (defaultConnectString, String.Empty, KeyRestrictionBehavior.AllowOnly);
617
618                         NonAbstractDBDataPermission union = (NonAbstractDBDataPermission) ptrue.Union (ptrue);
619                         Check ("true U true", union, true, false, 1);
620                         union = (NonAbstractDBDataPermission)ptrue.Union (pfalse);
621                         Check ("true U false", union, true, false, 1);
622                         union = (NonAbstractDBDataPermission)pfalse.Union (ptrue);
623                         Check ("false U true", union, true, false, 1);
624                         union = (NonAbstractDBDataPermission)pfalse.Union (pfalse);
625                         Check ("false U false", union, false, false, 1);
626                 }
627
628                 [Test]
629                 [ExpectedException (typeof (ArgumentException))]
630                 public void Union_BadPermission ()
631                 {
632                         NonAbstractDBDataPermission dbdp1 = new NonAbstractDBDataPermission (PermissionState.Unrestricted);
633                         dbdp1.Union (new SecurityPermission (SecurityPermissionFlag.Assertion));
634                 }
635
636                 [Test]
637                 [ExpectedException (typeof (ArgumentNullException))]
638                 public void FromXml_Null ()
639                 {
640                         NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
641                         elp.FromXml (null);
642                 }
643
644                 [Test]
645                 [ExpectedException (typeof (ArgumentException))]
646                 public void FromXml_WrongTag ()
647                 {
648                         NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
649                         SecurityElement se = elp.ToXml ();
650                         se.Tag = "IMono";
651                         elp.FromXml (se);
652                 }
653
654                 [Test]
655                 [ExpectedException (typeof (ArgumentException))]
656                 public void FromXml_WrongTagCase ()
657                 {
658                         NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
659                         SecurityElement se = elp.ToXml ();
660                         se.Tag = "IPERMISSION"; // instead of IPermission
661                         elp.FromXml (se);
662                 }
663
664                 [Test]
665                 public void FromXml_WrongClass ()
666                 {
667                         NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
668                         SecurityElement se = elp.ToXml ();
669
670                         SecurityElement w = new SecurityElement (se.Tag);
671                         w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
672                         w.AddAttribute ("version", se.Attribute ("version"));
673                         elp.FromXml (w);
674                         // doesn't care of the class name at that stage
675                         // anyway the class has already be created so...
676                 }
677
678                 [Test]
679                 public void FromXml_NoClass ()
680                 {
681                         NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
682                         SecurityElement se = elp.ToXml ();
683
684                         SecurityElement w = new SecurityElement (se.Tag);
685                         w.AddAttribute ("version", se.Attribute ("version"));
686                         elp.FromXml (w);
687                         // doesn't even care of the class attribute presence
688                 }
689
690                 [Test]
691                 [ExpectedException (typeof (ArgumentException))]
692                 public void FromXml_WrongVersion ()
693                 {
694                         NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
695                         SecurityElement se = elp.ToXml ();
696                         se.Attributes.Remove ("version");
697                         se.Attributes.Add ("version", "2");
698                         elp.FromXml (se);
699                 }
700
701                 [Test]
702                 public void FromXml_NoVersion ()
703                 {
704                         NonAbstractDBDataPermission elp = new NonAbstractDBDataPermission (PermissionState.None);
705                         SecurityElement se = elp.ToXml ();
706
707                         SecurityElement w = new SecurityElement (se.Tag);
708                         w.AddAttribute ("class", se.Attribute ("class"));
709                         elp.FromXml (w);
710                 }
711         }
712 }