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