2005-02-24 Sebastien Pouliot <sebastien@ximian.com>
[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 #if NET_2_0
43                 private StrongNameIdentityPermission GetUnion ()
44                 {
45                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
46                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
47                         StrongNamePublicKeyBlob blob2 = new StrongNamePublicKeyBlob (new byte[16]);
48                         StrongNameIdentityPermission diffPk = new StrongNameIdentityPermission (blob2, "mono", new Version (1, 2, 3, 4));
49                         return (StrongNameIdentityPermission)snip.Union (diffPk);
50                 }
51 #endif
52
53                 [Test]
54                 public void PermissionStateNone ()
55                 {
56                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
57                         Assert.AreEqual (String.Empty, snip.Name, "Name");
58                         Assert.IsNull (snip.PublicKey, "PublicKey");
59                         Assert.AreEqual ("0.0", snip.Version.ToString (), "Version");
60
61                         SecurityElement se = snip.ToXml ();
62 #if NET_2_0
63                         Assert.IsNull (se.Attribute ("Name"), "Xml-Name");
64                         Assert.IsNull (se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
65 #else
66                         Assert.AreEqual (String.Empty, se.Attribute ("Name"), "Xml-Name");
67                         Assert.AreEqual ("0.0", se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
68 #endif
69                         Assert.IsNull (se.Attribute ("PublicKeyBlob"), "Xml-PublicKeyBlob");
70
71                         // because Name == String.Empty, which is illegal using the other constructor
72                         StrongNameIdentityPermission copy = (StrongNameIdentityPermission) snip.Copy ();
73                         Assert.AreEqual (String.Empty, copy.Name, "Copy-Name");
74 #if NET_2_0
75                         // Strangely once copied the Name becomes equals to String.Empty in 2.0 [FDBK19351]
76                         Assert.IsNull (se.Attribute ("AssemblyVersion"), "Copy-Version");
77 #else
78                         Assert.AreEqual ("0.0", copy.Version.ToString (), "Copy-Version");
79 #endif
80                         Assert.IsNull (copy.PublicKey, "Copy-PublicKey");
81                 }
82 #if NET_2_0
83                 [Test]
84                 public void PermissionStateUnrestricted ()
85                 {
86                         // In 2.0 Unrestricted are permitted for identity permissions
87                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.Unrestricted);
88                         Assert.AreEqual (String.Empty, snip.Name, "Name");
89                         Assert.IsNull (snip.PublicKey, "PublicKey");
90                         Assert.AreEqual ("0.0", snip.Version.ToString (), "Version");
91                         SecurityElement se = snip.ToXml ();
92                         Assert.IsNull (se.Attribute ("Name"), "Xml-Name");
93                         Assert.IsNull (se.Attribute ("PublicKeyBlob"), "Xml-PublicKeyBlob");
94                         Assert.IsNull (se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
95                         StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy ();
96                         Assert.IsTrue (snip.Equals (copy), "snip Equals copy");
97                         Assert.IsTrue (copy.Equals (snip), "copy Equals snip");
98                         // and they aren't equals to None
99                         Assert.IsFalse (snip.Equals (new StrongNameIdentityPermission (PermissionState.None)), "Not Equals None");
100                 }
101 #else
102                 [Test]
103                 [ExpectedException (typeof (ArgumentException))]
104                 public void PermissionStateUnrestricted ()
105                 {
106                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.Unrestricted);
107                 }
108 #endif
109                 [Test]
110                 [ExpectedException (typeof (ArgumentException))]
111                 public void PermissionStateInvalid ()
112                 {
113                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission ((PermissionState)2);
114                 }
115
116                 [Test]
117                 [ExpectedException (typeof (ArgumentNullException))]
118                 public void StrongNameIdentityPermission_BlobNull ()
119                 {
120                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (null, "mono", new Version (0,0));
121                 }
122
123                 [Test]
124                 public void StrongNameIdentityPermission_NameNull ()
125                 {
126                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
127                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, null, new Version (1, 2));
128                         Assert.IsNull (snip.Name, "Name");
129                         Assert.AreEqual ("00000000000000000400000000000000", snip.PublicKey.ToString (), "PublicKey");
130                         Assert.AreEqual ("1.2", snip.Version.ToString (), "Version");
131
132                         SecurityElement se = snip.ToXml ();
133                         Assert.IsNull (se.Attribute ("Name"), "Xml-Name");
134                         Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Xml-PublicKeyBlob");
135                         Assert.AreEqual ("1.2", se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
136
137                         StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy ();
138                         se = copy.ToXml ();
139                         Assert.IsNull (se.Attribute ("Name"), "Copy-Name");
140                         Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Copy-PublicKeyBlob");
141                         Assert.AreEqual ("1.2", se.Attribute ("AssemblyVersion"), "Copy-AssemblyVersion");
142                 }
143
144                 [Test]
145 #if NET_2_0
146                 [ExpectedException (typeof (ArgumentException))]
147 #endif
148                 public void StrongNameIdentityPermission_NameEmpty ()
149                 {
150                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
151                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, String.Empty, new Version (1, 2));
152                 }
153
154                 [Test]
155                 public void StrongNameIdentityPermission_VersionNull ()
156                 {
157                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
158                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", null);
159                         Assert.AreEqual ("mono", snip.Name, "Name");
160                         Assert.AreEqual ("00000000000000000400000000000000", snip.PublicKey.ToString (), "PublicKey");
161                         Assert.IsNull (snip.Version, "Version");
162
163                         SecurityElement se = snip.ToXml ();
164                         Assert.AreEqual ("mono", se.Attribute ("Name"), "Xml-Name");
165                         Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Xml-PublicKeyBlob");
166                         Assert.IsNull (se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
167
168                         StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy ();
169                         Assert.AreEqual ("mono", se.Attribute ("Name"), "Copy-Name");
170                         Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Copy-PublicKeyBlob");
171                         Assert.IsNull (se.Attribute ("AssemblyVersion"), "Copy-AssemblyVersion");
172                 }
173
174                 [Test]
175                 public void StrongNameIdentityPermission_All ()
176                 {
177                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
178                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
179                         Assert.AreEqual ("mono", snip.Name, "Name");
180                         Assert.AreEqual ("00000000000000000400000000000000", snip.PublicKey.ToString (), "PublicKey");
181                         Assert.AreEqual ("1.2.3.4", snip.Version.ToString (), "Version");
182
183                         SecurityElement se = snip.ToXml ();
184                         Assert.AreEqual ("mono", se.Attribute ("Name"), "Xml-Name");
185                         Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Xml-PublicKeyBlob");
186                         Assert.AreEqual ("1.2.3.4", se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
187
188                         StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy ();
189                         Assert.AreEqual ("mono", se.Attribute ("Name"), "Copy-Name");
190                         Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Copy-PublicKeyBlob");
191                         Assert.AreEqual ("1.2.3.4", se.Attribute ("AssemblyVersion"), "Copy-AssemblyVersion");
192                 }
193
194                 [Test]
195                 public void Name ()
196                 {
197                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
198                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
199                         Assert.AreEqual ("mono", snip.Name, "Name-1");
200                         snip.Name = null;
201                         Assert.IsNull (snip.Name, "Name-2");
202                         snip.Name = "mono";
203                         Assert.AreEqual ("mono", snip.Name, "Name-3");
204                 }
205
206                 [Test]
207 #if NET_2_0
208                 [ExpectedException (typeof (ArgumentException))]
209 #endif
210                 public void Name_Empty ()
211                 {
212                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
213                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
214                         snip.Name = String.Empty;
215 #if !NET_2_0
216                         Assert.AreEqual (String.Empty, snip.Name, "Name");
217 #endif
218                 }
219
220 #if NET_2_0
221                 [Test]
222                 [ExpectedException (typeof (NotSupportedException))]
223                 public void Name_Multiple_Get ()
224                 {
225                         StrongNameIdentityPermission union = GetUnion ();
226                         string s = union.Name;
227                 }
228
229                 [Test]
230                 public void Name_Multiple_Set ()
231                 {
232                         StrongNameIdentityPermission union = GetUnion ();
233                         union.Name = "oops";
234                         Assert.AreEqual ("oops", union.Name, "Name");
235                         Assert.IsNull (union.PublicKey, "PublicKey");
236                         Assert.AreEqual ("0.0", union.Version.ToString (), "Version");
237                 }
238 #endif
239
240                 [Test]
241                 [ExpectedException (typeof (ArgumentNullException))]
242                 public void PublicKey_Null ()
243                 {
244                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
245                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
246                         snip.PublicKey = null;
247                 }
248 #if NET_2_0
249                 [Test]
250                 [ExpectedException (typeof (NotSupportedException))]
251                 public void PublicKey_Multiple_Get ()
252                 {
253                         StrongNameIdentityPermission union = GetUnion ();
254                         StrongNamePublicKeyBlob snpkb = union.PublicKey;
255                 }
256
257                 [Test]
258                 public void PublicKey_Multiple_Set ()
259                 {
260                         StrongNameIdentityPermission union = GetUnion ();
261                         union.PublicKey = new StrongNamePublicKeyBlob (ecma);
262                         Assert.AreEqual (String.Empty, union.Name, "Name");
263                         Assert.IsNotNull (union.PublicKey, "PublicKey");
264                         Assert.AreEqual ("0.0", union.Version.ToString (), "Version");
265                 }
266 #endif
267                 [Test]
268                 public void Version ()
269                 {
270                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
271                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
272                         Assert.AreEqual ("1.2.3.4", snip.Version.ToString (), "Version-1");
273                         snip.Version = null;
274                         Assert.IsNull (snip.Version, "Version-2");
275                         snip.Version = new Version (1, 2, 3);
276                         Assert.AreEqual ("1.2.3", snip.Version.ToString (), "Version-3");
277                 }
278 #if NET_2_0
279                 [Test]
280                 [ExpectedException (typeof (NotSupportedException))]
281                 public void Version_Multiple_Get ()
282                 {
283                         StrongNameIdentityPermission union = GetUnion ();
284                         Version v = union.Version;
285                 }
286
287                 [Test]
288                 public void Version_Multiple_Set ()
289                 {
290                         StrongNameIdentityPermission union = GetUnion ();
291                         union.Version = new Version ("1.2.3.4");
292                         Assert.AreEqual (String.Empty, union.Name, "Name");
293                         Assert.IsNull (union.PublicKey, "PublicKey");
294                         Assert.AreEqual ("1.2.3.4", union.Version.ToString (), "Version");
295                 }
296 #endif
297                 [Test]
298                 public void Copy_NameEmpty ()
299                 {
300                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
301                         snip.PublicKey = new StrongNamePublicKeyBlob (ecma);
302                         snip.Version = new Version ("1.2.3.4");
303
304                         // because Name == String.Empty, which is illegal using the other constructor
305                         // but (somewhat) required to copy the teo other informations
306                         StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy ();
307 #if NET_2_0
308                         Assert.IsTrue (copy.Equals (snip), "Equals");
309 #else
310                         Assert.AreEqual (copy.ToXml ().ToString (), snip.ToXml ().ToString (), "Equals-XML");
311 #endif
312                 }
313
314                 private void Compare (StrongNameIdentityPermission p1, StrongNameIdentityPermission p2, string prefix)
315                 {
316                         Assert.AreEqual (p1.Name, p2.Name, prefix + ".Name");
317                         Assert.AreEqual (p1.PublicKey, p2.PublicKey, prefix + ".PublicKey");
318                         Assert.AreEqual (p1.Version, p2.Version, prefix + ".Version");
319                         Assert.IsFalse (Object.ReferenceEquals (p1, p2), "ReferenceEquals");
320                 }
321
322                 [Test]
323                 public void Intersect ()
324                 {
325                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
326                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
327
328                         StrongNameIdentityPermission intersect = (StrongNameIdentityPermission)snip.Intersect (null);
329                         Assert.IsNull (intersect, "snip N null");
330
331                         StrongNameIdentityPermission empty = new StrongNameIdentityPermission (PermissionState.None);
332                         intersect = (StrongNameIdentityPermission)snip.Intersect (empty);
333 #if NET_2_0
334                         Assert.IsNull (intersect, "snip N empty");
335 #else
336                         Compare (empty, intersect, "snip U empty");
337 #endif
338                         intersect = (StrongNameIdentityPermission)snip.Intersect (snip);
339                         Compare (snip, intersect, "snip U snip");
340
341                         StrongNameIdentityPermission samePk = new StrongNameIdentityPermission (blob, "novell", new Version (1, 2));
342                         intersect = (StrongNameIdentityPermission)snip.Intersect (samePk);
343                         Assert.IsNull (intersect, "(snip N samePk)");
344                         // strange, I would have expected a SNIP with the same public key...
345                 }
346
347                 [Test]
348 #if NET_2_0
349                 [ExpectedException (typeof (ArgumentException))]
350 #endif
351                 public void Intersect_DifferentPermissions ()
352                 {
353                         StrongNameIdentityPermission a = new StrongNameIdentityPermission (PermissionState.None);
354                         SecurityPermission b = new SecurityPermission (PermissionState.None);
355                         Assert.IsNull (a.Intersect (b));
356                 }
357
358                 [Test]
359                 public void IsSubsetOf ()
360                 {
361                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
362                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
363                         Assert.IsFalse (snip.IsSubsetOf (null), "snip.IsSubsetOf (null)");
364
365                         StrongNameIdentityPermission empty = new StrongNameIdentityPermission (PermissionState.None);
366                         Assert.IsTrue (empty.IsSubsetOf (null), "empty.IsSubsetOf (null)");
367                 }
368
369                 [Test]
370                 public void IsSubsetOf_Corlib ()
371                 {
372                         Version fx11 = new Version (1, 0, 5000, 0);
373                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
374                         StrongNameIdentityPermission granted = new StrongNameIdentityPermission (blob, "mscorlib", fx11);
375
376                         StrongNameIdentityPermission demanded = new StrongNameIdentityPermission (blob, null, null);
377                         Assert.IsTrue (demanded.IsSubsetOf (granted), "pk");
378
379                         demanded = new StrongNameIdentityPermission (blob, "mscorlib", null);
380                         Assert.IsTrue (demanded.IsSubsetOf (granted), "pk+name");
381
382                         demanded = new StrongNameIdentityPermission (blob, null, fx11);
383                         Assert.IsTrue (demanded.IsSubsetOf (granted), "pk+v");
384
385                         demanded = new StrongNameIdentityPermission (blob, "mscorlib", fx11);
386                         Assert.IsTrue (demanded.IsSubsetOf (granted), "pk+name+v");
387                 }
388
389                 [Test]
390                 [ExpectedException (typeof (ArgumentException))]
391                 public void IsSubsetOf_DifferentPermissions ()
392                 {
393                         StrongNameIdentityPermission a = new StrongNameIdentityPermission (PermissionState.None);
394                         SecurityPermission b = new SecurityPermission (PermissionState.None);
395                         a.IsSubsetOf (b);
396                 }
397
398                 [Test]
399                 public void Union ()
400                 {
401                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
402                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
403
404                         StrongNameIdentityPermission union = (StrongNameIdentityPermission)snip.Union (null);
405                         Compare (snip, union, "snip U null");
406
407                         StrongNameIdentityPermission empty = new StrongNameIdentityPermission (PermissionState.None);
408                         union = (StrongNameIdentityPermission)snip.Union (empty);
409                         Compare (snip, union, "snip U empty");
410
411                         union = (StrongNameIdentityPermission)snip.Union (snip);
412                         Compare (snip, union, "snip U snip");
413
414                         // note: can't be tested with PermissionState.Unrestricted
415
416                         StrongNameIdentityPermission samePk = new StrongNameIdentityPermission (blob, null, null);
417                         union = (StrongNameIdentityPermission)snip.Union (samePk);
418 #if !NET_2_0
419                         // can't compare the properties with multiple entries
420                         Compare (snip, union, "snip U samePk");
421 #endif
422                         Assert.IsTrue (snip.IsSubsetOf (union), "snip.IsSubsetOf (union)");
423
424                         union = (StrongNameIdentityPermission)samePk.Union (snip);
425 #if !NET_2_0
426                         // can't compare the properties with multiple entries
427                         Compare (snip, union, "samePk U snip");
428 #endif
429                         Assert.IsTrue (samePk.IsSubsetOf (union), "snip.IsSubsetOf (union)");
430                 }
431
432                 [Test]
433                 public void Union_DifferentPk ()
434                 {
435                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
436                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
437                         StrongNamePublicKeyBlob blob2 = new StrongNamePublicKeyBlob (new byte [16]);
438                         StrongNameIdentityPermission diffPk = new StrongNameIdentityPermission (blob2, "mono", new Version (1, 2, 3, 4));
439                         StrongNameIdentityPermission result = (StrongNameIdentityPermission) snip.Union (diffPk);
440 #if NET_2_0
441                         Assert.IsNotNull (result, "DifferentPk");
442                         // new XML format is used to contain more than one site
443                         SecurityElement se = result.ToXml ();
444                         Assert.AreEqual (2, se.Children.Count, "Childs");
445                         Assert.AreEqual ("00000000000000000400000000000000", (se.Children [0] as SecurityElement).Attribute ("PublicKeyBlob"), "Blob#1");
446                         Assert.AreEqual ("00000000000000000000000000000000", (se.Children [1] as SecurityElement).Attribute ("PublicKeyBlob"), "Blob#2");
447                         // strangely it is still versioned as 'version="1"'.
448                         Assert.AreEqual ("1", se.Attribute ("version"), "Version");
449 #else
450                         Assert.IsNull (result, "DifferentPk");
451 #endif
452                 }
453
454                 [Test]
455                 public void Union_SamePublicKey_DifferentName ()
456                 {
457                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
458                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
459                         StrongNameIdentityPermission diffName = new StrongNameIdentityPermission (blob, "novell", null);
460                         StrongNameIdentityPermission result = (StrongNameIdentityPermission) snip.Union (diffName);
461 #if NET_2_0
462                         Assert.IsNotNull (result, "DifferentName");
463                         // new XML format is used to contain more than one site
464                         SecurityElement se = result.ToXml ();
465                         Assert.AreEqual (2, se.Children.Count, "Childs");
466                         Assert.AreEqual ("mono", (se.Children [0] as SecurityElement).Attribute ("Name"), "Name#1");
467                         Assert.AreEqual ("novell", (se.Children [1] as SecurityElement).Attribute ("Name"), "Name#2");
468                         // strangely it is still versioned as 'version="1"'.
469                         Assert.AreEqual ("1", se.Attribute ("version"), "Version");
470 #else
471                         Assert.IsNull (result, "DifferentName");
472 #endif
473                 }
474
475                 [Test]
476                 public void Union_SamePublicKey_DifferentVersion ()
477                 {
478                         StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
479                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
480                         StrongNameIdentityPermission diffVersion = new StrongNameIdentityPermission (blob, null, new Version (1, 2));
481                         StrongNameIdentityPermission result = (StrongNameIdentityPermission) snip.Union (diffVersion);
482 #if NET_2_0
483                         Assert.IsNotNull (result, "DifferentVersion");
484                         // new XML format is used to contain more than one site
485                         SecurityElement se = result.ToXml ();
486                         Assert.AreEqual (2, se.Children.Count, "Childs");
487                         Assert.AreEqual ("1.2.3.4", (se.Children [0] as SecurityElement).Attribute ("AssemblyVersion"), "AssemblyVersion#1");
488                         Assert.AreEqual ("1.2", (se.Children [1] as SecurityElement).Attribute ("AssemblyVersion"), "AssemblyVersion#2");
489                         // strangely it is still versioned as 'version="1"'.
490                         Assert.AreEqual ("1", se.Attribute ("version"), "Version");
491 #else
492                         Assert.IsNull (result, "DifferentVersion");
493 #endif
494                 }
495
496                 [Test]
497                 [ExpectedException (typeof (ArgumentException))]
498                 public void Union_DifferentPermissions ()
499                 {
500                         StrongNameIdentityPermission a = new StrongNameIdentityPermission (PermissionState.None);
501                         SecurityPermission b = new SecurityPermission (PermissionState.None);
502                         a.Union (b);
503                 }
504
505                 [Test]
506                 [ExpectedException (typeof (ArgumentNullException))]
507                 public void FromXml_Null ()
508                 {
509                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
510                         snip.FromXml (null);
511                 }
512
513                 [Test]
514                 [ExpectedException (typeof (ArgumentException))]
515                 public void FromXml_WrongTag ()
516                 {
517                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
518                         SecurityElement se = snip.ToXml ();
519                         se.Tag = "IMono";
520                         snip.FromXml (se);
521                 }
522
523                 [Test]
524                 [ExpectedException (typeof (ArgumentException))]
525                 public void FromXml_WrongTagCase ()
526                 {
527                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
528                         SecurityElement se = snip.ToXml ();
529                         se.Tag = "IPERMISSION"; // instead of IPermission
530                         snip.FromXml (se);
531                 }
532
533                 [Test]
534                 public void FromXml_WrongClass ()
535                 {
536                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
537                         SecurityElement se = snip.ToXml ();
538
539                         SecurityElement w = new SecurityElement (se.Tag);
540                         w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
541                         w.AddAttribute ("version", se.Attribute ("version"));
542                         snip.FromXml (w);
543                         // doesn't care of the class name at that stage
544                         // anyway the class has already be created so...
545                 }
546
547                 [Test]
548                 public void FromXml_NoClass ()
549                 {
550                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
551                         SecurityElement se = snip.ToXml ();
552
553                         SecurityElement w = new SecurityElement (se.Tag);
554                         w.AddAttribute ("version", se.Attribute ("version"));
555                         snip.FromXml (w);
556                         // doesn't even care of the class attribute presence
557                 }
558
559                 [Test]
560                 [ExpectedException (typeof (ArgumentException))]
561                 public void FromXml_WrongVersion ()
562                 {
563                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
564                         SecurityElement se = snip.ToXml ();
565                         se.Attributes.Remove ("version");
566                         se.Attributes.Add ("version", "2");
567                         snip.FromXml (se);
568                 }
569
570                 [Test]
571                 public void FromXml_NoVersion ()
572                 {
573                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
574                         SecurityElement se = snip.ToXml ();
575
576                         SecurityElement w = new SecurityElement (se.Tag);
577                         w.AddAttribute ("class", se.Attribute ("class"));
578                         snip.FromXml (w);
579                 }
580
581                 [Test]
582                 public void FromXml_NameEmpty ()
583                 {
584                         StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
585                         SecurityElement se = snip.ToXml ();
586                         snip.FromXml (se);
587
588                         snip.PublicKey = new StrongNamePublicKeyBlob (ecma);
589                         snip.Version = new Version ("1.2.3.4");
590                         se = snip.ToXml ();
591                         snip.FromXml (se);
592                 }
593         }
594 }