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 ();
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");
63 [ExpectedException (typeof (ArgumentException
))]
64 public void PermissionStateUnrestricted ()
66 StrongNameIdentityPermission snip
= new StrongNameIdentityPermission (PermissionState
.Unrestricted
);
70 [ExpectedException (typeof (ArgumentException
))]
71 public void PermissionStateInvalid ()
73 StrongNameIdentityPermission snip
= new StrongNameIdentityPermission ((PermissionState
)2);
77 [ExpectedException (typeof (ArgumentNullException
))]
78 public void StrongNameIdentityPermission_BlobNull ()
80 StrongNameIdentityPermission snip
= new StrongNameIdentityPermission (null, "mono", new Version (0,0));
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");
105 [ExpectedException (typeof (ArgumentException
))]
107 public void StrongNameIdentityPermission_NameEmpty ()
109 StrongNamePublicKeyBlob blob
= new StrongNamePublicKeyBlob (ecma
);
110 StrongNameIdentityPermission snip
= new StrongNameIdentityPermission (blob
, String
.Empty
, new Version (1, 2));
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");
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");
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");
163 Assert
.IsNull (snip
.Name
, "Name-2");
165 Assert
.AreEqual ("mono", snip
.Name
, "Name-3");
170 [ExpectedException (typeof (ArgumentException
))]
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
;
178 Assert
.AreEqual (String
.Empty
, snip
.Name
, "Name");
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;
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");
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");
205 [ExpectedException (typeof (ArgumentException
))]
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 ();
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");
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...
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
));
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)");
271 [ExpectedException (typeof (ArgumentException
))]
272 public void IsSubsetOf_DifferentPermissions ()
274 StrongNameIdentityPermission a
= new StrongNameIdentityPermission (PermissionState
.None
);
275 SecurityPermission b
= new SecurityPermission (PermissionState
.None
);
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)");
309 [ExpectedException (typeof (ArgumentException
))]
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
);
325 [ExpectedException (typeof (ArgumentException
))]
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
);
340 [ExpectedException (typeof (ArgumentException
))]
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
);
354 [ExpectedException (typeof (ArgumentException
))]
355 public void Union_DifferentPermissions ()
357 StrongNameIdentityPermission a
= new StrongNameIdentityPermission (PermissionState
.None
);
358 SecurityPermission b
= new SecurityPermission (PermissionState
.None
);
363 [ExpectedException (typeof (ArgumentNullException
))]
364 public void FromXml_Null ()
366 StrongNameIdentityPermission snip
= new StrongNameIdentityPermission (PermissionState
.None
);
371 [ExpectedException (typeof (ArgumentException
))]
372 public void FromXml_WrongTag ()
374 StrongNameIdentityPermission snip
= new StrongNameIdentityPermission (PermissionState
.None
);
375 SecurityElement se
= snip
.ToXml ();
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
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"));
400 // doesn't care of the class name at that stage
401 // anyway the class has already be created so...
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"));
413 // doesn't even care of the class attribute presence
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");
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"));
439 public void FromXml_NameEmpty ()
441 StrongNameIdentityPermission snip
= new StrongNameIdentityPermission (PermissionState
.None
);
442 SecurityElement se
= snip
.ToXml ();
445 snip
.PublicKey
= new StrongNamePublicKeyBlob (ecma
);
446 snip
.Version
= new Version ("1.2.3.4");