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