**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / Test / System.Security.Permissions / StrongNameIdentityPermissionTest.cs
blob06c37ade008293b21159c0a77a77f0e0d7ad65ba
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.
30 using NUnit.Framework;
31 using System;
32 using System.Security;
33 using System.Security.Permissions;
35 namespace MonoTests.System.Security.Permissions {
37 [TestFixture]
38 public class StrongNameIdentityPermissionTest {
40 static byte[] ecma = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
42 [Test]
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 ();
51 Assert.AreEqual (String.Empty, se.Attribute ("Name"), "Xml-Name");
52 Assert.IsNull (se.Attribute ("PublicKeyBlob"), "Xml-PublicKeyBlob");
53 Assert.AreEqual ("0.0", se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
55 // because Name == String.Empty, which is illegal using the other constructor
56 StrongNameIdentityPermission copy = (StrongNameIdentityPermission) snip.Copy ();
57 Assert.AreEqual (String.Empty, copy.Name, "Copy-Name");
58 Assert.IsNull (copy.PublicKey, "Copy-PublicKey");
59 Assert.AreEqual ("0.0", copy.Version.ToString (), "Copy-Version");
62 [Test]
63 [ExpectedException (typeof (ArgumentException))]
64 public void PermissionStateUnrestricted ()
66 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.Unrestricted);
69 [Test]
70 [ExpectedException (typeof (ArgumentException))]
71 public void PermissionStateInvalid ()
73 StrongNameIdentityPermission snip = new StrongNameIdentityPermission ((PermissionState)2);
76 [Test]
77 [ExpectedException (typeof (ArgumentNullException))]
78 public void StrongNameIdentityPermission_BlobNull ()
80 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (null, "mono", new Version (0,0));
83 [Test]
84 public void StrongNameIdentityPermission_NameNull ()
86 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
87 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, null, new Version (1, 2));
88 Assert.IsNull (snip.Name, "Name");
89 Assert.AreEqual ("00000000000000000400000000000000", snip.PublicKey.ToString (), "PublicKey");
90 Assert.AreEqual ("1.2", snip.Version.ToString (), "Version");
92 SecurityElement se = snip.ToXml ();
93 Assert.IsNull (se.Attribute ("Name"), "Xml-Name");
94 Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Xml-PublicKeyBlob");
95 Assert.AreEqual ("1.2", se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
97 StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy ();
98 Assert.IsNull (se.Attribute ("Name"), "Copy-Name");
99 Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Copy-PublicKeyBlob");
100 Assert.AreEqual ("1.2", se.Attribute ("AssemblyVersion"), "Copy-AssemblyVersion");
103 [Test]
104 #if NET_2_0
105 [ExpectedException (typeof (ArgumentException))]
106 #endif
107 public void StrongNameIdentityPermission_NameEmpty ()
109 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
110 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, String.Empty, new Version (1, 2));
111 #if !NET_2_0
112 // TODO
113 #endif
116 [Test]
117 public void StrongNameIdentityPermission_VersionNull ()
119 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
120 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", null);
121 Assert.AreEqual ("mono", snip.Name, "Name");
122 Assert.AreEqual ("00000000000000000400000000000000", snip.PublicKey.ToString (), "PublicKey");
123 Assert.IsNull (snip.Version, "Version");
125 SecurityElement se = snip.ToXml ();
126 Assert.AreEqual ("mono", se.Attribute ("Name"), "Xml-Name");
127 Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Xml-PublicKeyBlob");
128 Assert.IsNull (se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
130 StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy ();
131 Assert.AreEqual ("mono", se.Attribute ("Name"), "Copy-Name");
132 Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Copy-PublicKeyBlob");
133 Assert.IsNull (se.Attribute ("AssemblyVersion"), "Copy-AssemblyVersion");
136 [Test]
137 public void StrongNameIdentityPermission_All ()
139 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
140 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
141 Assert.AreEqual ("mono", snip.Name, "Name");
142 Assert.AreEqual ("00000000000000000400000000000000", snip.PublicKey.ToString (), "PublicKey");
143 Assert.AreEqual ("1.2.3.4", snip.Version.ToString (), "Version");
145 SecurityElement se = snip.ToXml ();
146 Assert.AreEqual ("mono", se.Attribute ("Name"), "Xml-Name");
147 Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Xml-PublicKeyBlob");
148 Assert.AreEqual ("1.2.3.4", se.Attribute ("AssemblyVersion"), "Xml-AssemblyVersion");
150 StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy ();
151 Assert.AreEqual ("mono", se.Attribute ("Name"), "Copy-Name");
152 Assert.AreEqual ("00000000000000000400000000000000", se.Attribute ("PublicKeyBlob"), "Copy-PublicKeyBlob");
153 Assert.AreEqual ("1.2.3.4", se.Attribute ("AssemblyVersion"), "Copy-AssemblyVersion");
156 [Test]
157 public void Name ()
159 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
160 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
161 Assert.AreEqual ("mono", snip.Name, "Name-1");
162 snip.Name = null;
163 Assert.IsNull (snip.Name, "Name-2");
164 snip.Name = "mono";
165 Assert.AreEqual ("mono", snip.Name, "Name-3");
168 [Test]
169 #if NET_2_0
170 [ExpectedException (typeof (ArgumentException))]
171 #endif
172 public void Name_Empty ()
174 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
175 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
176 snip.Name = String.Empty;
177 #if !NET_2_0
178 Assert.AreEqual (String.Empty, snip.Name, "Name");
179 #endif
182 [Test]
183 [ExpectedException (typeof (ArgumentNullException))]
184 public void PublicKey_Null ()
186 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
187 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
188 snip.PublicKey = null;
191 [Test]
192 public void Version ()
194 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
195 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
196 Assert.AreEqual ("1.2.3.4", snip.Version.ToString (), "Version-1");
197 snip.Version = null;
198 Assert.IsNull (snip.Version, "Version-2");
199 snip.Version = new Version (1, 2, 3);
200 Assert.AreEqual ("1.2.3", snip.Version.ToString (), "Version-3");
203 [Test]
204 #if NET_2_0
205 [ExpectedException (typeof (ArgumentException))]
206 #endif
207 public void Copy_NameEmpty ()
209 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
210 snip.PublicKey = new StrongNamePublicKeyBlob (ecma);
211 snip.Version = new Version ("1.2.3.4");
213 // because Name == String.Empty, which is illegal using the other constructor
214 // but (somewhat) required to copy the teo other informations
215 StrongNameIdentityPermission copy = (StrongNameIdentityPermission)snip.Copy ();
216 #if !NET_2_0
217 // TODO
218 #endif
221 private void Compare (StrongNameIdentityPermission p1, StrongNameIdentityPermission p2, string prefix)
223 Assert.AreEqual (p1.Name, p2.Name, prefix + ".Name");
224 Assert.AreEqual (p1.PublicKey, p2.PublicKey, prefix + ".PublicKey");
225 Assert.AreEqual (p1.Version, p2.Version, prefix + ".Version");
226 Assert.IsFalse (Object.ReferenceEquals (p1, p2), "ReferenceEquals");
229 [Test]
230 public void Intersect ()
232 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
233 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
235 StrongNameIdentityPermission intersect = (StrongNameIdentityPermission)snip.Intersect (null);
236 Assert.IsNull (intersect, "snip N null");
238 StrongNameIdentityPermission empty = new StrongNameIdentityPermission (PermissionState.None);
239 intersect = (StrongNameIdentityPermission)snip.Intersect (empty);
240 Compare (empty, intersect, "snip U empty");
242 intersect = (StrongNameIdentityPermission)snip.Intersect (snip);
243 Compare (snip, intersect, "snip U snip");
245 StrongNameIdentityPermission samePk = new StrongNameIdentityPermission (blob, "novell", new Version (1, 2));
246 intersect = (StrongNameIdentityPermission)snip.Intersect (samePk);
247 Assert.IsNull (intersect, "(snip N samePk)");
248 // strange, I would have expected a SNIP with the same public key...
251 [Test]
252 public void Intersect_DifferentPermissions ()
254 StrongNameIdentityPermission a = new StrongNameIdentityPermission (PermissionState.None);
255 SecurityPermission b = new SecurityPermission (PermissionState.None);
256 Assert.IsNull (a.Intersect (b));
259 [Test]
260 public void IsSubsetOf ()
262 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
263 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
264 Assert.IsFalse (snip.IsSubsetOf (null), "snip.IsSubsetOf (null)");
266 StrongNameIdentityPermission empty = new StrongNameIdentityPermission (PermissionState.None);
267 Assert.IsTrue (empty.IsSubsetOf (null), "empty.IsSubsetOf (null)");
270 [Test]
271 [ExpectedException (typeof (ArgumentException))]
272 public void IsSubsetOf_DifferentPermissions ()
274 StrongNameIdentityPermission a = new StrongNameIdentityPermission (PermissionState.None);
275 SecurityPermission b = new SecurityPermission (PermissionState.None);
276 a.IsSubsetOf (b);
279 [Test]
280 public void Union ()
282 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
283 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
285 StrongNameIdentityPermission union = (StrongNameIdentityPermission)snip.Union (null);
286 Compare (snip, union, "snip U null");
288 StrongNameIdentityPermission empty = new StrongNameIdentityPermission (PermissionState.None);
289 union = (StrongNameIdentityPermission)snip.Union (empty);
290 Compare (snip, union, "snip U empty");
292 union = (StrongNameIdentityPermission)snip.Union (snip);
293 Compare (snip, union, "snip U snip");
295 // note: can't be tested with PermissionState.Unrestricted
297 StrongNameIdentityPermission samePk = new StrongNameIdentityPermission (blob, null, null);
298 union = (StrongNameIdentityPermission)snip.Union (samePk);
299 Compare (snip, union, "snip U samePk");
300 Assert.IsTrue (snip.IsSubsetOf (union), "snip.IsSubsetOf (union)");
302 union = (StrongNameIdentityPermission)samePk.Union (snip);
303 Compare (snip, union, "samePk U snip");
304 Assert.IsTrue (samePk.IsSubsetOf (union), "snip.IsSubsetOf (union)");
307 [Test]
308 #if NET_2_0
309 [ExpectedException (typeof (ArgumentException))]
310 #endif
311 public void Union_DifferentPk ()
313 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
314 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
315 StrongNamePublicKeyBlob blob2 = new StrongNamePublicKeyBlob (new byte [16]);
316 StrongNameIdentityPermission diffPk = new StrongNameIdentityPermission (blob2, "mono", new Version (1, 2, 3, 4));
317 StrongNameIdentityPermission result = (StrongNameIdentityPermission) snip.Union (diffPk);
318 #if !NET_2_0
319 // TODO
320 #endif
323 [Test]
324 #if NET_2_0
325 [ExpectedException (typeof (ArgumentException))]
326 #endif
327 public void Union_SamePublicKey_DifferentName ()
329 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
330 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
331 StrongNameIdentityPermission diffName = new StrongNameIdentityPermission (blob, "novell", null);
332 StrongNameIdentityPermission result = (StrongNameIdentityPermission) snip.Union (diffName);
333 #if !NET_2_0
334 // TODO
335 #endif
338 [Test]
339 #if NET_2_0
340 [ExpectedException (typeof (ArgumentException))]
341 #endif
342 public void Union_SamePublicKey_DifferentVersion ()
344 StrongNamePublicKeyBlob blob = new StrongNamePublicKeyBlob (ecma);
345 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (blob, "mono", new Version (1, 2, 3, 4));
346 StrongNameIdentityPermission diffVersion = new StrongNameIdentityPermission (blob, null, new Version (1, 2));
347 StrongNameIdentityPermission result = (StrongNameIdentityPermission) snip.Union (diffVersion);
348 #if !NET_2_0
349 // TODO
350 #endif
353 [Test]
354 [ExpectedException (typeof (ArgumentException))]
355 public void Union_DifferentPermissions ()
357 StrongNameIdentityPermission a = new StrongNameIdentityPermission (PermissionState.None);
358 SecurityPermission b = new SecurityPermission (PermissionState.None);
359 a.Union (b);
362 [Test]
363 [ExpectedException (typeof (ArgumentNullException))]
364 public void FromXml_Null ()
366 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
367 snip.FromXml (null);
370 [Test]
371 [ExpectedException (typeof (ArgumentException))]
372 public void FromXml_WrongTag ()
374 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
375 SecurityElement se = snip.ToXml ();
376 se.Tag = "IMono";
377 snip.FromXml (se);
380 [Test]
381 [ExpectedException (typeof (ArgumentException))]
382 public void FromXml_WrongTagCase ()
384 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
385 SecurityElement se = snip.ToXml ();
386 se.Tag = "IPERMISSION"; // instead of IPermission
387 snip.FromXml (se);
390 [Test]
391 public void FromXml_WrongClass ()
393 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
394 SecurityElement se = snip.ToXml ();
396 SecurityElement w = new SecurityElement (se.Tag);
397 w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
398 w.AddAttribute ("version", se.Attribute ("version"));
399 snip.FromXml (w);
400 // doesn't care of the class name at that stage
401 // anyway the class has already be created so...
404 [Test]
405 public void FromXml_NoClass ()
407 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
408 SecurityElement se = snip.ToXml ();
410 SecurityElement w = new SecurityElement (se.Tag);
411 w.AddAttribute ("version", se.Attribute ("version"));
412 snip.FromXml (w);
413 // doesn't even care of the class attribute presence
416 [Test]
417 [ExpectedException (typeof (ArgumentException))]
418 public void FromXml_WrongVersion ()
420 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
421 SecurityElement se = snip.ToXml ();
422 se.Attributes.Remove ("version");
423 se.Attributes.Add ("version", "2");
424 snip.FromXml (se);
427 [Test]
428 public void FromXml_NoVersion ()
430 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
431 SecurityElement se = snip.ToXml ();
433 SecurityElement w = new SecurityElement (se.Tag);
434 w.AddAttribute ("class", se.Attribute ("class"));
435 snip.FromXml (w);
438 [Test]
439 public void FromXml_NameEmpty ()
441 StrongNameIdentityPermission snip = new StrongNameIdentityPermission (PermissionState.None);
442 SecurityElement se = snip.ToXml ();
443 snip.FromXml (se);
445 snip.PublicKey = new StrongNamePublicKeyBlob (ecma);
446 snip.Version = new Version ("1.2.3.4");
447 se = snip.ToXml ();
448 snip.FromXml (se);