[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / corlib / Test / System.Security.Permissions / StrongNameIdentityPermissionTest.cs
1 //
2 // StrongNameIdentityPermissionTest.cs -
3 //      NUnit Test Cases for StrongNameIdentityPermission
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using NUnit.Framework;
31 using System;
32 using System.Security;
33 using System.Security.Permissions;
34
35 namespace MonoTests.System.Security.Permissions {
36
37         [TestFixture]
38         public class StrongNameIdentityPermissionTest {
39
40                 static byte[] ecma = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
41
42                 private StrongNameIdentityPermission GetUnion ()
43                 {
44                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
45                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
46                         StrongNamePublicKeyBlob blob2 = new StrongNamePublicKeyBlob (new byte[16]);
47                         StrongNameIdentityPermission diffPk = new StrongNameIdentityPermission (blob2, "mono", new Version (1, 2, 3, 4));
48                         return (StrongNameIdentityPermission)snip.Union (diffPk);
49                 }
50
51                 [Test]
52                 public void PermissionStateNone ()
53                 {
54                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
55                         Assert.AreEqual (String.Empty, snip.Name, "Name");
56                         Assert.IsNull (snip.PublicKey, "PublicKey");
57                         Assert.AreEqual ("0.0", snip.Version.ToString (), "Version");
58
59                         SecurityElement se = snip.ToXml ();
60                         Assert.IsNull (se.Attribute ("Name"), "Xml-Name");
61                         Assert.IsNull (se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
62                         Assert.IsNull (se.Attribute ("PublicKeyBlob"), "Xml-PublicKeyBlob");
63
64                         // because Name == String.Empty, which is illegal using the other constructor
65                         StrongNameIdentityPermission copy = (StrongNameIdentityPermission) snip.Copy ();
66                         Assert.AreEqual (String.Empty, copy.Name, "Copy-Name");
67                         // Strangely once copied the Name becomes equals to String.Empty in 2.0 [FDBK19351]
68                         Assert.IsNull (se.Attribute ("AssemblyVersion"), "Copy-Version");
69                         Assert.IsNull (copy.PublicKey, "Copy-PublicKey");
70                 }
71                 [Test]
72                 public void PermissionStateUnrestricted ()
73                 {
74                         // In 2.0 Unrestricted are permitted for identity permissions
75                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.Unrestricted);
76                         Assert.AreEqual (String.Empty, snip.Name, "Name");
77                         Assert.IsNull (snip.PublicKey, "PublicKey");
78                         Assert.AreEqual ("0.0", snip.Version.ToString (), "Version");
79                         SecurityElement se = snip.ToXml ();
80                         Assert.IsNull (se.Attribute ("Name"), "Xml-Name");
81                         Assert.IsNull (se.Attribute ("PublicKeyBlob"), "Xml-PublicKeyBlob");
82                         Assert.IsNull (se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
83                         StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy ();
84                         Assert.IsTrue (snip.Equals (copy), "snip Equals copy");
85                         Assert.IsTrue (copy.Equals (snip), "copy Equals snip");
86                         // and they aren't equals to None
87                         Assert.IsFalse (snip.Equals (new StrongNameIdentityPermission (PermissionState.None)), "Not Equals None");
88                 }
89                 [Test]
90                 [ExpectedException (typeof (ArgumentException))]
91                 public void PermissionStateInvalid ()
92                 {
93                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission ((PermissionState)2);
94                 }
95
96                 [Test]
97                 [ExpectedException (typeof (ArgumentNullException))]
98                 public void StrongNameIdentityPermission_BlobNull ()
99                 {
100                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (null, "mono", new Version (0,0));
101                 }
102
103                 [Test]
104                 public void StrongNameIdentityPermission_NameNull ()
105                 {
106                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
107                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, null, new Version (1, 2));
108                         Assert.IsNull (snip.Name, "Name");
109                         Assert.AreEqual ("00000000000000000400000000000000", snip.PublicKey.ToString (), "PublicKey");
110                         Assert.AreEqual ("1.2", snip.Version.ToString (), "Version");
111
112                         SecurityElement se = snip.ToXml ();
113                         Assert.IsNull (se.Attribute ("Name"), "Xml-Name");
114                         Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Xml-PublicKeyBlob");
115                         Assert.AreEqual ("1.2", se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
116
117                         StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy ();
118                         se = copy.ToXml ();
119                         Assert.IsNull (se.Attribute ("Name"), "Copy-Name");
120                         Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Copy-PublicKeyBlob");
121                         Assert.AreEqual ("1.2", se.Attribute ("AssemblyVersion"), "Copy-AssemblyVersion");
122                 }
123
124                 [Test]
125                 [ExpectedException (typeof (ArgumentException))]
126                 public void StrongNameIdentityPermission_NameEmpty ()
127                 {
128                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
129                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, String.Empty, new Version (1, 2));
130                 }
131
132                 [Test]
133                 public void StrongNameIdentityPermission_VersionNull ()
134                 {
135                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
136                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", null);
137                         Assert.AreEqual ("mono", snip.Name, "Name");
138                         Assert.AreEqual ("00000000000000000400000000000000", snip.PublicKey.ToString (), "PublicKey");
139                         Assert.IsNull (snip.Version, "Version");
140
141                         SecurityElement se = snip.ToXml ();
142                         Assert.AreEqual ("mono", se.Attribute ("Name"), "Xml-Name");
143                         Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Xml-PublicKeyBlob");
144                         Assert.IsNull (se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
145
146                         StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy ();
147                         Assert.AreEqual ("mono", se.Attribute ("Name"), "Copy-Name");
148                         Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Copy-PublicKeyBlob");
149                         Assert.IsNull (se.Attribute ("AssemblyVersion"), "Copy-AssemblyVersion");
150                 }
151
152                 [Test]
153                 public void StrongNameIdentityPermission_All ()
154                 {
155                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
156                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
157                         Assert.AreEqual ("mono", snip.Name, "Name");
158                         Assert.AreEqual ("00000000000000000400000000000000", snip.PublicKey.ToString (), "PublicKey");
159                         Assert.AreEqual ("1.2.3.4", snip.Version.ToString (), "Version");
160
161                         SecurityElement se = snip.ToXml ();
162                         Assert.AreEqual ("mono", se.Attribute ("Name"), "Xml-Name");
163                         Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Xml-PublicKeyBlob");
164                         Assert.AreEqual ("1.2.3.4", se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
165
166                         StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy ();
167                         Assert.AreEqual ("mono", se.Attribute ("Name"), "Copy-Name");
168                         Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Copy-PublicKeyBlob");
169                         Assert.AreEqual ("1.2.3.4", se.Attribute ("AssemblyVersion"), "Copy-AssemblyVersion");
170                 }
171
172                 [Test]
173                 public void Name ()
174                 {
175                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
176                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
177                         Assert.AreEqual ("mono", snip.Name, "Name-1");
178                         snip.Name = null;
179                         Assert.IsNull (snip.Name, "Name-2");
180                         snip.Name = "mono";
181                         Assert.AreEqual ("mono", snip.Name, "Name-3");
182                 }
183
184                 [Test]
185                 [ExpectedException (typeof (ArgumentException))]
186                 public void Name_Empty ()
187                 {
188                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
189                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
190                         snip.Name = String.Empty;
191                 }
192
193                 [Test]
194                 [ExpectedException (typeof (NotSupportedException))]
195                 public void Name_Multiple_Get ()
196                 {
197                         StrongNameIdentityPermission union = GetUnion ();
198                         string s = union.Name;
199                 }
200
201                 [Test]
202                 public void Name_Multiple_Set ()
203                 {
204                         StrongNameIdentityPermission union = GetUnion ();
205                         union.Name = "oops";
206                         Assert.AreEqual ("oops", union.Name, "Name");
207                         Assert.IsNull (union.PublicKey, "PublicKey");
208                         Assert.AreEqual ("0.0", union.Version.ToString (), "Version");
209                 }
210
211                 [Test]
212                 [ExpectedException (typeof (ArgumentNullException))]
213                 public void PublicKey_Null ()
214                 {
215                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
216                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
217                         snip.PublicKey = null;
218                 }
219                 [Test]
220                 [ExpectedException (typeof (NotSupportedException))]
221                 public void PublicKey_Multiple_Get ()
222                 {
223                         StrongNameIdentityPermission union = GetUnion ();
224                         StrongNamePublicKeyBlob snpkb = union.PublicKey;
225                 }
226
227                 [Test]
228                 public void PublicKey_Multiple_Set ()
229                 {
230                         StrongNameIdentityPermission union = GetUnion ();
231                         union.PublicKey = new StrongNamePublicKeyBlob (ecma);
232                         Assert.AreEqual (String.Empty, union.Name, "Name");
233                         Assert.IsNotNull (union.PublicKey, "PublicKey");
234                         Assert.AreEqual ("0.0", union.Version.ToString (), "Version");
235                 }
236                 [Test]
237                 public void Version ()
238                 {
239                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
240                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
241                         Assert.AreEqual ("1.2.3.4", snip.Version.ToString (), "Version-1");
242                         snip.Version = null;
243                         Assert.IsNull (snip.Version, "Version-2");
244                         snip.Version = new Version (1, 2, 3);
245                         Assert.AreEqual ("1.2.3", snip.Version.ToString (), "Version-3");
246                 }
247                 [Test]
248                 [ExpectedException (typeof (NotSupportedException))]
249                 public void Version_Multiple_Get ()
250                 {
251                         StrongNameIdentityPermission union = GetUnion ();
252                         Version v = union.Version;
253                 }
254
255                 [Test]
256                 public void Version_Multiple_Set ()
257                 {
258                         StrongNameIdentityPermission union = GetUnion ();
259                         union.Version = new Version ("1.2.3.4");
260                         Assert.AreEqual (String.Empty, union.Name, "Name");
261                         Assert.IsNull (union.PublicKey, "PublicKey");
262                         Assert.AreEqual ("1.2.3.4", union.Version.ToString (), "Version");
263                 }
264                 [Test]
265                 public void Copy_NameEmpty ()
266                 {
267                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
268                         snip.PublicKey = new StrongNamePublicKeyBlob (ecma);
269                         snip.Version = new Version ("1.2.3.4");
270
271                         // because Name == String.Empty, which is illegal using the other constructor
272                         // but (somewhat) required to copy the teo other informations
273                         StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy ();
274                         Assert.IsTrue (copy.Equals (snip), "Equals");
275                 }
276
277                 private void Compare (StrongNameIdentityPermission p1, StrongNameIdentityPermission p2, string prefix)
278                 {
279                         Assert.AreEqual (p1.Name, p2.Name, prefix + ".Name");
280                         Assert.AreEqual (p1.PublicKey, p2.PublicKey, prefix + ".PublicKey");
281                         Assert.AreEqual (p1.Version, p2.Version, prefix + ".Version");
282                         Assert.IsFalse (Object.ReferenceEquals (p1, p2), "ReferenceEquals");
283                 }
284
285                 [Test]
286                 public void Intersect ()
287                 {
288                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
289                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
290
291                         StrongNameIdentityPermission intersect = (StrongNameIdentityPermission)snip.Intersect (null);
292                         Assert.IsNull (intersect, "snip N null");
293
294                         StrongNameIdentityPermission empty = new StrongNameIdentityPermission (PermissionState.None);
295                         intersect = (StrongNameIdentityPermission)snip.Intersect (empty);
296                         Assert.IsNull (intersect, "snip N empty");
297                         intersect = (StrongNameIdentityPermission)snip.Intersect (snip);
298                         Compare (snip, intersect, "snip U snip");
299
300                         StrongNameIdentityPermission samePk = new StrongNameIdentityPermission (blob, "novell", new Version (1, 2));
301                         intersect = (StrongNameIdentityPermission)snip.Intersect (samePk);
302                         Assert.IsNull (intersect, "(snip N samePk)");
303                         // strange, I would have expected a SNIP with the same public key...
304                 }
305
306                 [Test]
307                 [ExpectedException (typeof (ArgumentException))]
308                 public void Intersect_DifferentPermissions ()
309                 {
310                         StrongNameIdentityPermission a = new StrongNameIdentityPermission (PermissionState.None);
311                         SecurityPermission b = new SecurityPermission (PermissionState.None);
312                         Assert.IsNull (a.Intersect (b));
313                 }
314
315                 [Test]
316                 public void IsSubsetOf ()
317                 {
318                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
319                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
320                         Assert.IsFalse (snip.IsSubsetOf (null), "snip.IsSubsetOf (null)");
321
322                         StrongNameIdentityPermission empty = new StrongNameIdentityPermission (PermissionState.None);
323                         Assert.IsTrue (empty.IsSubsetOf (null), "empty.IsSubsetOf (null)");
324                 }
325
326                 [Test]
327                 public void IsSubsetOf_Corlib ()
328                 {
329                         Version fx11 = new Version (1, 0, 5000, 0);
330                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
331                         StrongNameIdentityPermission granted = new StrongNameIdentityPermission (blob, "mscorlib", fx11);
332
333                         StrongNameIdentityPermission demanded = new StrongNameIdentityPermission (blob, null, null);
334                         Assert.IsTrue (demanded.IsSubsetOf (granted), "pk");
335
336                         demanded = new StrongNameIdentityPermission (blob, "mscorlib", null);
337                         Assert.IsTrue (demanded.IsSubsetOf (granted), "pk+name");
338
339                         demanded = new StrongNameIdentityPermission (blob, null, fx11);
340                         Assert.IsTrue (demanded.IsSubsetOf (granted), "pk+v");
341
342                         demanded = new StrongNameIdentityPermission (blob, "mscorlib", fx11);
343                         Assert.IsTrue (demanded.IsSubsetOf (granted), "pk+name+v");
344                 }
345
346                 [Test]
347                 [ExpectedException (typeof (ArgumentException))]
348                 public void IsSubsetOf_DifferentPermissions ()
349                 {
350                         StrongNameIdentityPermission a = new StrongNameIdentityPermission (PermissionState.None);
351                         SecurityPermission b = new SecurityPermission (PermissionState.None);
352                         a.IsSubsetOf (b);
353                 }
354
355                 [Test]
356                 public void Union ()
357                 {
358                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
359                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
360
361                         StrongNameIdentityPermission union = (StrongNameIdentityPermission)snip.Union (null);
362                         Compare (snip, union, "snip U null");
363
364                         StrongNameIdentityPermission empty = new StrongNameIdentityPermission (PermissionState.None);
365                         union = (StrongNameIdentityPermission)snip.Union (empty);
366                         Compare (snip, union, "snip U empty");
367
368                         union = (StrongNameIdentityPermission)snip.Union (snip);
369                         Compare (snip, union, "snip U snip");
370
371                         // note: can't be tested with PermissionState.Unrestricted
372
373                         StrongNameIdentityPermission samePk = new StrongNameIdentityPermission (blob, null, null);
374                         union = (StrongNameIdentityPermission)snip.Union (samePk);
375                         Assert.IsTrue (snip.IsSubsetOf (union), "snip.IsSubsetOf (union)");
376
377                         union = (StrongNameIdentityPermission)samePk.Union (snip);
378                         Assert.IsTrue (samePk.IsSubsetOf (union), "snip.IsSubsetOf (union)");
379                 }
380
381                 [Test]
382                 public void Union_DifferentPk ()
383                 {
384                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
385                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
386                         StrongNamePublicKeyBlob blob2 = new StrongNamePublicKeyBlob (new byte [16]);
387                         StrongNameIdentityPermission diffPk = new StrongNameIdentityPermission (blob2, "mono", new Version (1, 2, 3, 4));
388                         StrongNameIdentityPermission result = (StrongNameIdentityPermission) snip.Union (diffPk);
389                         Assert.IsNotNull (result, "DifferentPk");
390                         // new XML format is used to contain more than one site
391                         SecurityElement se = result.ToXml ();
392                         Assert.AreEqual (2, se.Children.Count, "Childs");
393                         Assert.AreEqual ("00000000000000000400000000000000", (se.Children [0] as SecurityElement).Attribute ("PublicKeyBlob"), "Blob#1");
394                         Assert.AreEqual ("00000000000000000000000000000000", (se.Children [1] as SecurityElement).Attribute ("PublicKeyBlob"), "Blob#2");
395                         // strangely it is still versioned as 'version="1"'.
396                         Assert.AreEqual ("1", se.Attribute ("version"), "Version");
397                 }
398
399                 [Test]
400                 public void Union_SamePublicKey_DifferentName ()
401                 {
402                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
403                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
404                         StrongNameIdentityPermission diffName = new StrongNameIdentityPermission (blob, "novell", null);
405                         StrongNameIdentityPermission result = (StrongNameIdentityPermission) snip.Union (diffName);
406                         Assert.IsNotNull (result, "DifferentName");
407                         // new XML format is used to contain more than one site
408                         SecurityElement se = result.ToXml ();
409                         Assert.AreEqual (2, se.Children.Count, "Childs");
410                         Assert.AreEqual ("mono", (se.Children [0] as SecurityElement).Attribute ("Name"), "Name#1");
411                         Assert.AreEqual ("novell", (se.Children [1] as SecurityElement).Attribute ("Name"), "Name#2");
412                         // strangely it is still versioned as 'version="1"'.
413                         Assert.AreEqual ("1", se.Attribute ("version"), "Version");
414                 }
415
416                 [Test]
417                 public void Union_SamePublicKey_DifferentVersion ()
418                 {
419                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
420                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
421                         StrongNameIdentityPermission diffVersion = new StrongNameIdentityPermission (blob, null, new Version (1, 2));
422                         StrongNameIdentityPermission result = (StrongNameIdentityPermission) snip.Union (diffVersion);
423                         Assert.IsNotNull (result, "DifferentVersion");
424                         // new XML format is used to contain more than one site
425                         SecurityElement se = result.ToXml ();
426                         Assert.AreEqual (2, se.Children.Count, "Childs");
427                         Assert.AreEqual ("1.2.3.4", (se.Children [0] as SecurityElement).Attribute ("AssemblyVersion"), "AssemblyVersion#1");
428                         Assert.AreEqual ("1.2", (se.Children [1] as SecurityElement).Attribute ("AssemblyVersion"), "AssemblyVersion#2");
429                         // strangely it is still versioned as 'version="1"'.
430                         Assert.AreEqual ("1", se.Attribute ("version"), "Version");
431                 }
432
433                 [Test]
434                 [ExpectedException (typeof (ArgumentException))]
435                 public void Union_DifferentPermissions ()
436                 {
437                         StrongNameIdentityPermission a = new StrongNameIdentityPermission (PermissionState.None);
438                         SecurityPermission b = new SecurityPermission (PermissionState.None);
439                         a.Union (b);
440                 }
441
442                 [Test]
443                 [ExpectedException (typeof (ArgumentNullException))]
444                 public void FromXml_Null ()
445                 {
446                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
447                         snip.FromXml (null);
448                 }
449
450                 [Test]
451                 [ExpectedException (typeof (ArgumentException))]
452                 public void FromXml_WrongTag ()
453                 {
454                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
455                         SecurityElement se = snip.ToXml ();
456                         se.Tag = "IMono";
457                         snip.FromXml (se);
458                 }
459
460                 [Test]
461                 [ExpectedException (typeof (ArgumentException))]
462                 public void FromXml_WrongTagCase ()
463                 {
464                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
465                         SecurityElement se = snip.ToXml ();
466                         se.Tag = "IPERMISSION"; // instead of IPermission
467                         snip.FromXml (se);
468                 }
469
470                 [Test]
471                 public void FromXml_WrongClass ()
472                 {
473                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
474                         SecurityElement se = snip.ToXml ();
475
476                         SecurityElement w = new SecurityElement (se.Tag);
477                         w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
478                         w.AddAttribute ("version", se.Attribute ("version"));
479                         snip.FromXml (w);
480                         // doesn't care of the class name at that stage
481                         // anyway the class has already be created so...
482                 }
483
484                 [Test]
485                 public void FromXml_NoClass ()
486                 {
487                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
488                         SecurityElement se = snip.ToXml ();
489
490                         SecurityElement w = new SecurityElement (se.Tag);
491                         w.AddAttribute ("version", se.Attribute ("version"));
492                         snip.FromXml (w);
493                         // doesn't even care of the class attribute presence
494                 }
495
496                 [Test]
497                 [ExpectedException (typeof (ArgumentException))]
498                 public void FromXml_WrongVersion ()
499                 {
500                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
501                         SecurityElement se = snip.ToXml ();
502                         se.Attributes.Remove ("version");
503                         se.Attributes.Add ("version", "2");
504                         snip.FromXml (se);
505                 }
506
507                 [Test]
508                 public void FromXml_NoVersion ()
509                 {
510                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
511                         SecurityElement se = snip.ToXml ();
512
513                         SecurityElement w = new SecurityElement (se.Tag);
514                         w.AddAttribute ("class", se.Attribute ("class"));
515                         snip.FromXml (w);
516                 }
517
518                 [Test]
519                 public void FromXml_NameEmpty ()
520                 {
521                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
522                         SecurityElement se = snip.ToXml ();
523                         snip.FromXml (se);
524
525                         snip.PublicKey = new StrongNamePublicKeyBlob (ecma);
526                         snip.Version = new Version ("1.2.3.4");
527                         se = snip.ToXml ();
528                         snip.FromXml (se);
529                 }
530         }
531 }