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