2 // StrongNameIdentityPermissionTest.cs -
3 // NUnit Test Cases for StrongNameIdentityPermission
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using NUnit.Framework;
32 using System.Security;
33 using System.Security.Permissions;
35 namespace MonoTests.System.Security.Permissions {
38 public class StrongNameIdentityPermissionTest {
40 static byte[] ecma = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
43 public void PermissionStateNone ()
45 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
46 Assert.AreEqual (String.Empty, snip.Name, "Name");
47 Assert.IsNull (snip.PublicKey, "PublicKey");
48 Assert.AreEqual ("0.0", snip.Version.ToString (), "Version");
50 SecurityElement se = snip.ToXml ();
52 Assert.IsNull (se.Attribute ("Name"), "Xml-Name");
53 Assert.IsNull (se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
55 Assert.AreEqual (String.Empty, se.Attribute ("Name"), "Xml-Name");
56 Assert.AreEqual ("0.0", se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
58 Assert.IsNull (se.Attribute ("PublicKeyBlob"), "Xml-PublicKeyBlob");
60 // because Name == String.Empty, which is illegal using the other constructor
61 StrongNameIdentityPermission copy = (StrongNameIdentityPermission) snip.Copy ();
62 Assert.AreEqual (String.Empty, copy.Name, "Copy-Name");
64 // Strangely once copied the Name becomes equals to String.Empty in 2.0 [FDBK19351]
65 Assert.IsNull (se.Attribute ("AssemblyVersion"), "Copy-Version");
67 Assert.AreEqual ("0.0", copy.Version.ToString (), "Copy-Version");
69 Assert.IsNull (copy.PublicKey, "Copy-PublicKey");
73 public void PermissionStateUnrestricted ()
75 // In 2.0 Unrestricted are permitted for identity permissions
76 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.Unrestricted);
77 Assert.AreEqual (String.Empty, snip.Name, "Name");
78 Assert.IsNull (snip.PublicKey, "PublicKey");
79 Assert.AreEqual ("0.0", snip.Version.ToString (), "Version");
80 SecurityElement se = snip.ToXml ();
81 Assert.IsNull (se.Attribute ("Name"), "Xml-Name");
82 Assert.IsNull (se.Attribute ("PublicKeyBlob"), "Xml-PublicKeyBlob");
83 Assert.IsNull (se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
84 StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy ();
85 // Strangely once copied the Name becomes equals to String.Empty in 2.0 [FDBK19351]
86 Assert.AreEqual (String.Empty, copy.Name, "Copy-Name");
87 Assert.IsNull (copy.PublicKey, "Copy-PublicKey");
88 Assert.IsNull (se.Attribute ("AssemblyVersion"), "Copy-Version");
89 // and they aren't equals to None
90 Assert.IsFalse (snip.Equals (new StrongNameIdentityPermission (PermissionState.None)));
94 [ExpectedException (typeof (ArgumentException))]
95 public void PermissionStateUnrestricted ()
97 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.Unrestricted);
101 [ExpectedException (typeof (ArgumentException))]
102 public void PermissionStateInvalid ()
104 StrongNameIdentityPermission snip = new StrongNameIdentityPermission ((PermissionState)2);
108 [ExpectedException (typeof (ArgumentNullException))]
109 public void StrongNameIdentityPermission_BlobNull ()
111 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (null, "mono", new Version (0,0));
115 public void StrongNameIdentityPermission_NameNull ()
117 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
118 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, null, new Version (1, 2));
119 Assert.IsNull (snip.Name, "Name");
120 Assert.AreEqual ("00000000000000000400000000000000", snip.PublicKey.ToString (), "PublicKey");
121 Assert.AreEqual ("1.2", snip.Version.ToString (), "Version");
123 SecurityElement se = snip.ToXml ();
124 Assert.IsNull (se.Attribute ("Name"), "Xml-Name");
125 Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Xml-PublicKeyBlob");
126 Assert.AreEqual ("1.2", se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
128 StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy ();
129 Assert.IsNull (se.Attribute ("Name"), "Copy-Name");
130 Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Copy-PublicKeyBlob");
131 Assert.AreEqual ("1.2", se.Attribute ("AssemblyVersion"), "Copy-AssemblyVersion");
136 [ExpectedException (typeof (ArgumentException))]
138 public void StrongNameIdentityPermission_NameEmpty ()
140 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
141 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, String.Empty, new Version (1, 2));
148 public void StrongNameIdentityPermission_VersionNull ()
150 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
151 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", null);
152 Assert.AreEqual ("mono", snip.Name, "Name");
153 Assert.AreEqual ("00000000000000000400000000000000", snip.PublicKey.ToString (), "PublicKey");
154 Assert.IsNull (snip.Version, "Version");
156 SecurityElement se = snip.ToXml ();
157 Assert.AreEqual ("mono", se.Attribute ("Name"), "Xml-Name");
158 Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Xml-PublicKeyBlob");
159 Assert.IsNull (se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
161 StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy ();
162 Assert.AreEqual ("mono", se.Attribute ("Name"), "Copy-Name");
163 Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Copy-PublicKeyBlob");
164 Assert.IsNull (se.Attribute ("AssemblyVersion"), "Copy-AssemblyVersion");
168 public void StrongNameIdentityPermission_All ()
170 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
171 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
172 Assert.AreEqual ("mono", snip.Name, "Name");
173 Assert.AreEqual ("00000000000000000400000000000000", snip.PublicKey.ToString (), "PublicKey");
174 Assert.AreEqual ("1.2.3.4", snip.Version.ToString (), "Version");
176 SecurityElement se = snip.ToXml ();
177 Assert.AreEqual ("mono", se.Attribute ("Name"), "Xml-Name");
178 Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Xml-PublicKeyBlob");
179 Assert.AreEqual ("1.2.3.4", se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
181 StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy ();
182 Assert.AreEqual ("mono", se.Attribute ("Name"), "Copy-Name");
183 Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Copy-PublicKeyBlob");
184 Assert.AreEqual ("1.2.3.4", se.Attribute ("AssemblyVersion"), "Copy-AssemblyVersion");
190 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
191 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
192 Assert.AreEqual ("mono", snip.Name, "Name-1");
194 Assert.IsNull (snip.Name, "Name-2");
196 Assert.AreEqual ("mono", snip.Name, "Name-3");
201 [ExpectedException (typeof (ArgumentException))]
203 public void Name_Empty ()
205 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
206 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
207 snip.Name = String.Empty;
209 Assert.AreEqual (String.Empty, snip.Name, "Name");
214 [ExpectedException (typeof (ArgumentNullException))]
215 public void PublicKey_Null ()
217 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
218 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
219 snip.PublicKey = null;
223 public void Version ()
225 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
226 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
227 Assert.AreEqual ("1.2.3.4", snip.Version.ToString (), "Version-1");
229 Assert.IsNull (snip.Version, "Version-2");
230 snip.Version = new Version (1, 2, 3);
231 Assert.AreEqual ("1.2.3", snip.Version.ToString (), "Version-3");
235 public void Copy_NameEmpty ()
237 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
238 snip.PublicKey = new StrongNamePublicKeyBlob (ecma);
239 snip.Version = new Version ("1.2.3.4");
241 // because Name == String.Empty, which is illegal using the other constructor
242 // but (somewhat) required to copy the two other informations
243 StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy ();
245 Assert.IsTrue (copy.Equals (snip), "Equals");
247 Assert.IsFalse (copy.Equals (snip), "Equals");
251 private void Compare (StrongNameIdentityPermission p1, StrongNameIdentityPermission p2, string prefix)
253 Assert.AreEqual (p1.Name, p2.Name, prefix + ".Name");
254 Assert.AreEqual (p1.PublicKey, p2.PublicKey, prefix + ".PublicKey");
255 Assert.AreEqual (p1.Version, p2.Version, prefix + ".Version");
256 Assert.IsFalse (Object.ReferenceEquals (p1, p2), "ReferenceEquals");
260 public void Intersect ()
262 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
263 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
265 StrongNameIdentityPermission intersect = (StrongNameIdentityPermission)snip.Intersect (null);
266 Assert.IsNull (intersect, "snip N null");
268 StrongNameIdentityPermission empty = new StrongNameIdentityPermission (PermissionState.None);
269 intersect = (StrongNameIdentityPermission)snip.Intersect (empty);
271 Assert.IsNull (intersect, "snip N empty");
273 Compare (empty, intersect, "snip U empty");
275 intersect = (StrongNameIdentityPermission)snip.Intersect (snip);
276 Compare (snip, intersect, "snip U snip");
278 StrongNameIdentityPermission samePk = new StrongNameIdentityPermission (blob, "novell", new Version (1, 2));
279 intersect = (StrongNameIdentityPermission)snip.Intersect (samePk);
280 Assert.IsNull (intersect, "(snip N samePk)");
281 // strange, I would have expected a SNIP with the same public key...
286 [ExpectedException (typeof (ArgumentException))]
288 public void Intersect_DifferentPermissions ()
290 StrongNameIdentityPermission a = new StrongNameIdentityPermission (PermissionState.None);
291 SecurityPermission b = new SecurityPermission (PermissionState.None);
292 Assert.IsNull (a.Intersect (b));
296 public void IsSubsetOf ()
298 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
299 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
300 Assert.IsFalse (snip.IsSubsetOf (null), "snip.IsSubsetOf (null)");
302 StrongNameIdentityPermission empty = new StrongNameIdentityPermission (PermissionState.None);
303 Assert.IsTrue (empty.IsSubsetOf (null), "empty.IsSubsetOf (null)");
307 [ExpectedException (typeof (ArgumentException))]
308 public void IsSubsetOf_DifferentPermissions ()
310 StrongNameIdentityPermission a = new StrongNameIdentityPermission (PermissionState.None);
311 SecurityPermission b = new SecurityPermission (PermissionState.None);
318 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
319 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
321 StrongNameIdentityPermission union = (StrongNameIdentityPermission)snip.Union (null);
322 Compare (snip, union, "snip U null");
324 StrongNameIdentityPermission empty = new StrongNameIdentityPermission (PermissionState.None);
325 union = (StrongNameIdentityPermission)snip.Union (empty);
326 Compare (snip, union, "snip U empty");
328 union = (StrongNameIdentityPermission)snip.Union (snip);
329 Compare (snip, union, "snip U snip");
331 // note: can't be tested with PermissionState.Unrestricted
333 StrongNameIdentityPermission samePk = new StrongNameIdentityPermission (blob, null, null);
334 union = (StrongNameIdentityPermission)snip.Union (samePk);
336 // can't compare the properties with multiple entries
337 Compare (snip, union, "snip U samePk");
339 Assert.IsTrue (snip.IsSubsetOf (union), "snip.IsSubsetOf (union)");
341 union = (StrongNameIdentityPermission)samePk.Union (snip);
343 // can't compare the properties with multiple entries
344 Compare (snip, union, "samePk U snip");
346 Assert.IsTrue (samePk.IsSubsetOf (union), "snip.IsSubsetOf (union)");
350 public void Union_DifferentPk ()
352 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
353 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
354 StrongNamePublicKeyBlob blob2 = new StrongNamePublicKeyBlob (new byte [16]);
355 StrongNameIdentityPermission diffPk = new StrongNameIdentityPermission (blob2, "mono", new Version (1, 2, 3, 4));
356 StrongNameIdentityPermission result = (StrongNameIdentityPermission) snip.Union (diffPk);
358 Assert.IsNotNull (result, "DifferentPk");
359 // new XML format is used to contain more than one site
360 SecurityElement se = result.ToXml ();
361 Assert.AreEqual (2, se.Children.Count, "Childs");
362 Assert.AreEqual ("00000000000000000400000000000000", (se.Children [0] as SecurityElement).Attribute ("PublicKeyBlob"), "Blob#1");
363 Assert.AreEqual ("00000000000000000000000000000000", (se.Children [1] as SecurityElement).Attribute ("PublicKeyBlob"), "Blob#2");
364 // strangely it is still versioned as 'version="1"'.
365 Assert.AreEqual ("1", se.Attribute ("version"), "Version");
370 public void Union_SamePublicKey_DifferentName ()
372 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
373 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
374 StrongNameIdentityPermission diffName = new StrongNameIdentityPermission (blob, "novell", null);
375 StrongNameIdentityPermission result = (StrongNameIdentityPermission) snip.Union (diffName);
377 Assert.IsNotNull (result, "DifferentName");
378 // new XML format is used to contain more than one site
379 SecurityElement se = result.ToXml ();
380 Assert.AreEqual (2, se.Children.Count, "Childs");
381 Assert.AreEqual ("mono", (se.Children [0] as SecurityElement).Attribute ("Name"), "Name#1");
382 Assert.AreEqual ("novell", (se.Children [1] as SecurityElement).Attribute ("Name"), "Name#2");
383 // strangely it is still versioned as 'version="1"'.
384 Assert.AreEqual ("1", se.Attribute ("version"), "Version");
389 public void Union_SamePublicKey_DifferentVersion ()
391 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
392 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
393 StrongNameIdentityPermission diffVersion = new StrongNameIdentityPermission (blob, null, new Version (1, 2));
394 StrongNameIdentityPermission result = (StrongNameIdentityPermission) snip.Union (diffVersion);
396 Assert.IsNotNull (result, "DifferentVersion");
397 // new XML format is used to contain more than one site
398 SecurityElement se = result.ToXml ();
399 Assert.AreEqual (2, se.Children.Count, "Childs");
400 Assert.AreEqual ("1.2.3.4", (se.Children [0] as SecurityElement).Attribute ("AssemblyVersion"), "AssemblyVersion#1");
401 Assert.AreEqual ("1.2", (se.Children [1] as SecurityElement).Attribute ("AssemblyVersion"), "AssemblyVersion#2");
402 // strangely it is still versioned as 'version="1"'.
403 Assert.AreEqual ("1", se.Attribute ("version"), "Version");
408 [ExpectedException (typeof (ArgumentException))]
409 public void Union_DifferentPermissions ()
411 StrongNameIdentityPermission a = new StrongNameIdentityPermission (PermissionState.None);
412 SecurityPermission b = new SecurityPermission (PermissionState.None);
417 [ExpectedException (typeof (ArgumentNullException))]
418 public void FromXml_Null ()
420 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
425 [ExpectedException (typeof (ArgumentException))]
426 public void FromXml_WrongTag ()
428 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
429 SecurityElement se = snip.ToXml ();
435 [ExpectedException (typeof (ArgumentException))]
436 public void FromXml_WrongTagCase ()
438 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
439 SecurityElement se = snip.ToXml ();
440 se.Tag = "IPERMISSION"; // instead of IPermission
445 public void FromXml_WrongClass ()
447 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
448 SecurityElement se = snip.ToXml ();
450 SecurityElement w = new SecurityElement (se.Tag);
451 w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
452 w.AddAttribute ("version", se.Attribute ("version"));
454 // doesn't care of the class name at that stage
455 // anyway the class has already be created so...
459 public void FromXml_NoClass ()
461 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
462 SecurityElement se = snip.ToXml ();
464 SecurityElement w = new SecurityElement (se.Tag);
465 w.AddAttribute ("version", se.Attribute ("version"));
467 // doesn't even care of the class attribute presence
471 [ExpectedException (typeof (ArgumentException))]
472 public void FromXml_WrongVersion ()
474 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
475 SecurityElement se = snip.ToXml ();
476 se.Attributes.Remove ("version");
477 se.Attributes.Add ("version", "2");
482 public void FromXml_NoVersion ()
484 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
485 SecurityElement se = snip.ToXml ();
487 SecurityElement w = new SecurityElement (se.Tag);
488 w.AddAttribute ("class", se.Attribute ("class"));
493 public void FromXml_NameEmpty ()
495 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
496 SecurityElement se = snip.ToXml ();
499 snip.PublicKey = new StrongNamePublicKeyBlob (ecma);
500 snip.Version = new Version ("1.2.3.4");