2 // StrongNameMembershipConditionTest.cs -
3 // NUnit Test Cases for StrongNameMembershipCondition
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005 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
.Collections
;
33 using System
.Reflection
;
34 using System
.Security
;
35 using System
.Security
.Permissions
;
36 using System
.Security
.Policy
;
38 namespace MonoTests
.System
.Security
.Policy
{
41 public class StrongNameMembershipConditionTest
{
43 static Evidence allEmpty
;
44 static Evidence hostEcmaCorlibVersion
;
45 static Evidence hostEcmaVersion
;
46 static Evidence hostMsSystemSecurityVersion
;
47 static Evidence hostMsVersion
;
48 static Evidence hostOther
;
49 static Evidence assemblyEcmaCorlibVersion
;
50 static Evidence assemblyEcmaVersion
;
51 static Evidence assemblyMsSystemSecurityVersion
;
52 static Evidence assemblyMsVersion
;
53 static Evidence assemblyOther
;
54 static object wrongEvidence
;
57 static Version version
;
58 static StrongNamePublicKeyBlob blob
;
59 static StrongNamePublicKeyBlob ms
;
60 private static byte[] _msFinalKey
= new byte[160] {
61 0x00, 0x24, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00, 0x06, 0x02, 0x00, 0x00,
62 0x00, 0x24, 0x00, 0x00, 0x52, 0x53, 0x41, 0x31, 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00,
63 0x07, 0xD1, 0xFA, 0x57, 0xC4, 0xAE, 0xD9, 0xF0, 0xA3, 0x2E, 0x84, 0xAA, 0x0F, 0xAE, 0xFD, 0x0D,
64 0xE9, 0xE8, 0xFD, 0x6A, 0xEC, 0x8F, 0x87, 0xFB, 0x03, 0x76, 0x6C, 0x83, 0x4C, 0x99, 0x92, 0x1E,
65 0xB2, 0x3B, 0xE7, 0x9A, 0xD9, 0xD5, 0xDC, 0xC1, 0xDD, 0x9A, 0xD2, 0x36, 0x13, 0x21, 0x02, 0x90,
66 0x0B, 0x72, 0x3C, 0xF9, 0x80, 0x95, 0x7F, 0xC4, 0xE1, 0x77, 0x10, 0x8F, 0xC6, 0x07, 0x77, 0x4F,
67 0x29, 0xE8, 0x32, 0x0E, 0x92, 0xEA, 0x05, 0xEC, 0xE4, 0xE8, 0x21, 0xC0, 0xA5, 0xEF, 0xE8, 0xF1,
68 0x64, 0x5C, 0x4C, 0x0C, 0x93, 0xC1, 0xAB, 0x99, 0x28, 0x5D, 0x62, 0x2C, 0xAA, 0x65, 0x2C, 0x1D,
69 0xFA, 0xD6, 0x3D, 0x74, 0x5D, 0x6F, 0x2D, 0xE5, 0xF1, 0x7E, 0x5E, 0xAF, 0x0F, 0xC4, 0x96, 0x3D,
70 0x26, 0x1C, 0x8A, 0x12, 0x43, 0x65, 0x18, 0x20, 0x6D, 0xC0, 0x93, 0x34, 0x4D, 0x5A, 0xD2, 0x93 };
72 private Evidence
CreateHostEvidence (object o
)
74 Evidence e
= new Evidence ();
79 private Evidence
CreateAssemblyEvidence (object o
)
81 Evidence e
= new Evidence ();
87 public void FixtureSetUp ()
89 wrongEvidence
= new Site ("test");
90 allEmpty
= new Evidence ();
92 AssemblyName an
= typeof (int).Assembly
.GetName ();
95 blob
= new StrongNamePublicKeyBlob (an
.GetPublicKey ());
97 ms
= new StrongNamePublicKeyBlob (_msFinalKey
);
99 hostEcmaCorlibVersion
= CreateHostEvidence (new StrongName (blob
, name
, version
));
100 hostEcmaVersion
= CreateHostEvidence (new StrongName (blob
, " ", version
));
101 hostMsSystemSecurityVersion
= CreateHostEvidence (new StrongName (ms
, "System.Security", version
));
102 hostMsVersion
= CreateHostEvidence (new StrongName (ms
, " ", version
));
103 hostOther
= CreateHostEvidence (wrongEvidence
);
105 assemblyEcmaCorlibVersion
= CreateAssemblyEvidence (new StrongName (blob
, name
, version
));
106 assemblyEcmaVersion
= CreateAssemblyEvidence (new StrongName (blob
, " ", version
));
107 assemblyMsSystemSecurityVersion
= CreateAssemblyEvidence (new StrongName (ms
, "System.Security", version
));
108 assemblyMsVersion
= CreateAssemblyEvidence (new StrongName (ms
, " ", version
));
109 assemblyOther
= CreateAssemblyEvidence (wrongEvidence
);
113 [ExpectedException (typeof (ArgumentNullException
))]
114 public void StrongNameMembershipCondition_NullBlob ()
116 StrongNameMembershipCondition snmc
= new StrongNameMembershipCondition (null, name
, version
);
119 private void Common (StrongNameMembershipCondition snmc
)
121 Assert
.IsFalse (snmc
.Check (allEmpty
), "Check(allEmpty)");
122 Assert
.IsFalse (snmc
.Check (hostOther
), "Check(hostOther)");
123 Assert
.IsFalse (snmc
.Check (assemblyEcmaCorlibVersion
), "Check(assemblyEcmaCorlibVersion)");
124 Assert
.IsFalse (snmc
.Check (assemblyEcmaVersion
), "Check(assemblyEcmaVersion)");
125 Assert
.IsFalse (snmc
.Check (assemblyMsSystemSecurityVersion
), "Check(assemblyMsSystemSecurityVersion)");
126 Assert
.IsFalse (snmc
.Check (assemblyMsVersion
), "Check(assemblyMsVersion)");
127 Assert
.IsFalse (snmc
.Check (assemblyOther
), "Check(assemblyOther)");
129 StrongNameMembershipCondition copy
= (StrongNameMembershipCondition
)snmc
.Copy ();
130 Assert
.IsTrue (copy
.Equals (snmc
), "copy.Equals (snmc)");
131 Assert
.IsTrue (snmc
.Equals (copy
), "snmc.Equals (copy)");
134 bool original
= ((snmc
.Name
== null) && (snmc
.Version
== null));
135 Assert
.AreEqual (original
, copy
.Equals (snmc
), "bad.Equals (snmc)");
136 Assert
.AreEqual (original
, snmc
.Equals (copy
), "snmc.Equals (bad)");
138 SecurityElement se
= snmc
.ToXml ();
140 Assert
.AreEqual (snmc
.PublicKey
, copy
.PublicKey
, "PublicKey");
141 Assert
.AreEqual (snmc
.Name
, copy
.Name
, "Name");
142 Assert
.AreEqual (snmc
.Version
, copy
.Version
, "Version");
143 Assert
.AreEqual (snmc
.GetHashCode (), copy
.GetHashCode (), "GetHashCode ()");
144 Assert
.AreEqual (snmc
.ToString (), copy
.ToString (), "ToString ()");
145 Assert
.IsTrue (copy
.Equals (snmc
), "xml.Equals (snmc)");
146 Assert
.IsTrue (snmc
.Equals (copy
), "snmc.Equals (xml)");
150 public void StrongNameMembershipCondition_NullName ()
152 StrongNameMembershipCondition snmc
= new StrongNameMembershipCondition (blob
, null, version
);
153 Assert
.AreEqual (blob
, snmc
.PublicKey
, "PublicKey");
154 Assert
.IsNull (snmc
.Name
, "Name");
155 Assert
.AreEqual (version
, snmc
.Version
, "Version");
156 Assert
.AreEqual (blob
.GetHashCode (), snmc
.GetHashCode (), "GetHashCode ()");
157 Assert
.IsTrue (snmc
.ToString ().StartsWith ("StrongName - 00000000000000000400000000000000 version = "), "ToString ()");
159 Assert
.IsTrue (snmc
.Check (hostEcmaCorlibVersion
), "Check(hostEcmaCorlibVersion)");
160 Assert
.IsTrue (snmc
.Check (hostEcmaVersion
), "Check(hostEcmaVersion)");
161 Assert
.IsFalse (snmc
.Check (hostMsSystemSecurityVersion
), "Check(hostMsSystemSecurityVersion)");
162 Assert
.IsFalse (snmc
.Check (hostMsVersion
), "Check(hostMsVersion)");
167 public void StrongNameMembershipCondition_NullVersion ()
169 StrongNameMembershipCondition snmc
= new StrongNameMembershipCondition (blob
, name
, null);
170 Assert
.AreEqual (blob
, snmc
.PublicKey
, "PublicKey");
171 Assert
.AreEqual (name
, snmc
.Name
, "Name");
172 Assert
.IsNull (snmc
.Version
, "Version");
173 Assert
.AreEqual (blob
.GetHashCode (), snmc
.GetHashCode (), "GetHashCode ()");
174 Assert
.AreEqual ("StrongName - 00000000000000000400000000000000 name = mscorlib", snmc
.ToString (), "ToString ()");
176 Assert
.IsTrue (snmc
.Check (hostEcmaCorlibVersion
), "Check(hostEcmaCorlibVersion)");
177 Assert
.IsFalse (snmc
.Check (hostEcmaVersion
), "Check(hostEcmaVersion)");
178 Assert
.IsFalse (snmc
.Check (hostMsSystemSecurityVersion
), "Check(hostMsSystemSecurityVersion)");
179 Assert
.IsFalse (snmc
.Check (hostMsVersion
), "Check(hostMsVersion)");
184 public void StrongNameMembershipCondition_NullNameVersion ()
186 StrongNameMembershipCondition snmc
= new StrongNameMembershipCondition (blob
, null, null);
187 Assert
.AreEqual (blob
, snmc
.PublicKey
, "PublicKey");
188 Assert
.IsNull (snmc
.Name
, "Name");
189 Assert
.IsNull (snmc
.Version
, "Version");
190 Assert
.AreEqual (blob
.GetHashCode (), snmc
.GetHashCode (), "GetHashCode ()");
191 Assert
.AreEqual ("StrongName - 00000000000000000400000000000000", snmc
.ToString (), "ToString ()");
193 Assert
.IsTrue (snmc
.Check (hostEcmaCorlibVersion
), "Check(hostEcmaCorlibVersion)");
194 Assert
.IsTrue (snmc
.Check (hostEcmaVersion
), "Check(hostEcmaVersion)");
195 Assert
.IsFalse (snmc
.Check (hostMsSystemSecurityVersion
), "Check(hostMsSystemSecurityVersion)");
196 Assert
.IsFalse (snmc
.Check (hostMsVersion
), "Check(hostMsVersion)");
201 public void StrongNameMembershipCondition_Mscorlib ()
203 StrongNameMembershipCondition snmc
= new StrongNameMembershipCondition (blob
, name
, version
);
204 Assert
.AreEqual (blob
, snmc
.PublicKey
, "PublicKey");
205 Assert
.AreEqual ("mscorlib", snmc
.Name
, "Name");
206 Assert
.AreEqual (version
, snmc
.Version
, "Version");
207 Assert
.AreEqual (blob
.GetHashCode (), snmc
.GetHashCode (), "GetHashCode ()");
208 Assert
.IsTrue (snmc
.ToString ().StartsWith ("StrongName - 00000000000000000400000000000000 name = mscorlib version = "), "ToString ()");
210 Assert
.IsTrue (snmc
.Check (hostEcmaCorlibVersion
), "Check(hostEcmaCorlibVersion)");
211 Assert
.IsFalse (snmc
.Check (hostEcmaVersion
), "Check(hostEcmaVersion)");
212 Assert
.IsFalse (snmc
.Check (hostMsSystemSecurityVersion
), "Check(hostMsSystemSecurityVersion)");
213 Assert
.IsFalse (snmc
.Check (hostMsVersion
), "Check(hostMsVersion)");
218 public void StrongNameMembershipCondition_MsKey ()
220 StrongNameMembershipCondition snmc
= new StrongNameMembershipCondition (ms
, null, null);
221 Assert
.AreEqual (ms
, snmc
.PublicKey
, "PublicKey");
222 Assert
.IsNull (snmc
.Name
, "Name");
223 Assert
.IsNull (snmc
.Version
, "Version");
224 Assert
.AreEqual (ms
.GetHashCode (), snmc
.GetHashCode (), "GetHashCode ()");
226 Assert
.IsFalse (snmc
.Check (hostEcmaCorlibVersion
), "Check(hostEcmaCorlibVersion)");
227 Assert
.IsFalse (snmc
.Check (hostEcmaVersion
), "Check(hostEcmaVersion)");
228 Assert
.IsTrue (snmc
.Check (hostMsSystemSecurityVersion
), "Check(hostMsSystemSecurityVersion)");
229 Assert
.IsTrue (snmc
.Check (hostMsVersion
), "Check(hostMsVersion)");
234 public void Name_Null ()
236 StrongNameMembershipCondition snmc
= new StrongNameMembershipCondition (blob
, name
, version
);
241 [ExpectedException (typeof (ArgumentNullException
))]
242 public void PublicKey_Null ()
244 StrongNameMembershipCondition snmc
= new StrongNameMembershipCondition (blob
, name
, version
);
245 snmc
.PublicKey
= null;
249 public void Version_Null ()
251 StrongNameMembershipCondition snmc
= new StrongNameMembershipCondition (blob
, name
, version
);
256 [ExpectedException (typeof (ArgumentNullException
))]
257 public void FromXml_Null ()
259 StrongNameMembershipCondition snmc
= new StrongNameMembershipCondition (blob
, name
, version
);
264 [ExpectedException (typeof (ArgumentException
))]
265 public void FromXml_InvalidTag ()
267 StrongNameMembershipCondition snmc
= new StrongNameMembershipCondition (blob
, name
, version
);
268 SecurityElement se
= snmc
.ToXml ();
269 se
.Tag
= "IMonoship";
274 public void FromXml_InvalidClass ()
276 StrongNameMembershipCondition snmc
= new StrongNameMembershipCondition (blob
, name
, version
);
277 SecurityElement se
= snmc
.ToXml ();
278 se
.Attributes
["class"] = "Hello world";
283 public void FromXml_NoClass ()
285 StrongNameMembershipCondition snmc
= new StrongNameMembershipCondition (blob
, name
, version
);
286 SecurityElement se
= snmc
.ToXml ();
288 SecurityElement w
= new SecurityElement (se
.Tag
);
289 w
.AddAttribute ("version", se
.Attribute ("version"));
291 // doesn't even care of the class attribute presence
295 public void FromXml_InvalidVersion ()
297 StrongNameMembershipCondition snmc
= new StrongNameMembershipCondition (blob
, name
, version
);
298 SecurityElement se
= snmc
.ToXml ();
300 SecurityElement w
= new SecurityElement (se
.Tag
);
301 w
.AddAttribute ("class", se
.Attribute ("class"));
302 w
.AddAttribute ("version", "2");
303 w
.AddAttribute ("PublicKeyBlob", se
.Attribute ("PublicKeyBlob"));
308 public void FromXml_NoVersion ()
310 StrongNameMembershipCondition snmc
= new StrongNameMembershipCondition (blob
, name
, version
);
311 SecurityElement se
= snmc
.ToXml ();
313 SecurityElement w
= new SecurityElement (se
.Tag
);
314 w
.AddAttribute ("class", se
.Attribute ("class"));
319 public void FromXml_PolicyLevel ()
321 StrongNameMembershipCondition snmc
= new StrongNameMembershipCondition (blob
, name
, version
);
322 SecurityElement se
= snmc
.ToXml ();
323 // is it accepted for all policy levels ?
324 IEnumerator e
= SecurityManager
.PolicyHierarchy ();
325 while (e
.MoveNext ()) {
326 PolicyLevel pl
= e
.Current
as PolicyLevel
;
327 StrongNameMembershipCondition spl
= new StrongNameMembershipCondition (blob
, name
, version
);
328 spl
.FromXml (se
, pl
);
329 Assert
.IsTrue (spl
.Equals (snmc
), "FromXml(PolicyLevel='" + pl
.Label
+ "')");
335 public void ToXml_Null ()
337 StrongNameMembershipCondition snmc
= new StrongNameMembershipCondition (blob
, name
, version
);
338 // no ArgumentNullException here
339 SecurityElement se
= snmc
.ToXml (null);
340 Assert
.IsNotNull (se
, "ToXml(null)");
344 public void ToXml_PolicyLevel ()
346 StrongNameMembershipCondition snmc
= new StrongNameMembershipCondition (blob
, name
, version
);
347 SecurityElement se
= snmc
.ToXml ();
348 string s
= snmc
.ToXml ().ToString ();
349 // is it accepted for all policy levels ?
350 IEnumerator e
= SecurityManager
.PolicyHierarchy ();
351 while (e
.MoveNext ()) {
352 PolicyLevel pl
= e
.Current
as PolicyLevel
;
353 StrongNameMembershipCondition spl
= new StrongNameMembershipCondition (blob
, name
, version
);
354 spl
.FromXml (se
, pl
);
355 Assert
.AreEqual (s
, spl
.ToXml (pl
).ToString (), "ToXml(PolicyLevel='" + pl
.Label
+ "')");