1 //
2 // HashMembershipConditionTest.cs -
3 //	NUnit Test Cases for HashMembershipCondition
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.Reflection;
33 using System.Security;
34 using System.Security.Cryptography;
35 using System.Security.Policy;
36 
37 namespace MonoTests.System.Security.Policy {
38 
39 	[TestFixture]
40 	public class HashMembershipConditionTest {
41 
42 		static Hash hashEvidence;
43 		static MD5 md5;
44 		static SHA1 sha1;
45 		static byte[] digestMd5;
46 		static byte[] digestSha1;
47 
48 		[TestFixtureSetUp]
FixtureSetUp()49 		public void FixtureSetUp ()
50 		{
51 			Assembly a = Assembly.GetExecutingAssembly ();
52 			hashEvidence = new Hash (a);
53 
54 			md5 = MD5.Create ();
55 			digestMd5 = hashEvidence.GenerateHash (md5);
56 
57 			sha1 = SHA1.Create ();
58 			digestSha1 = hashEvidence.GenerateHash (sha1);
59 		}
60 
61 		[Test]
Constructor_MD5()62 		public void Constructor_MD5 ()
63 		{
64 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
65 			Assert.IsNotNull (hash);
66 			Assert.AreEqual (md5, hash.HashAlgorithm, "HashAlgorithm");
67 			Assert.AreEqual (BitConverter.ToString (digestMd5), BitConverter.ToString (hash.HashValue), "HashValue");
68 		}
69 
70 		[Test]
Constructor_SHA1()71 		public void Constructor_SHA1 ()
72 		{
73 			HashMembershipCondition hash = new HashMembershipCondition (sha1, digestSha1);
74 			Assert.IsNotNull (hash);
75 			Assert.AreEqual (sha1, hash.HashAlgorithm, "HashAlgorithm");
76 			Assert.AreEqual (BitConverter.ToString (digestSha1), BitConverter.ToString (hash.HashValue), "HashValue");
77 		}
78 
79 		[Test]
HashValue()80 		public void HashValue ()
81 		{
82 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
83 			// we can't change the instance data by getting a reference inside it
84 			byte[] value = hash.HashValue;
85 			value [0] ^= 0xFF;
86 			Assert.IsFalse (value [0] == hash.HashValue [0], "reference");
87 			Assert.AreEqual (BitConverter.ToString (digestMd5), BitConverter.ToString (hash.HashValue), "HashValue");
88 			// and we can't change the instance data by keeping a reference to what we supply
89 			hash.HashValue = value;
90 			byte old_value = value [0];
91 			value [0] += 42;
92 			Assert.IsFalse (value [0] == hash.HashValue [0], "reference-2");
93 			Assert.AreEqual (old_value, hash.HashValue [0], "HashValue[0]");
94 		}
95 
96 		[Test]
Check()97 		public void Check ()
98 		{
99 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
100 			Evidence e = null;
101 			Assert.IsFalse (hash.Check (e), "Check (null)");
102 			e = new Evidence ();
103 			Assert.IsFalse (hash.Check (e), "Check (empty)");
104 			e.AddHost (new Zone (SecurityZone.MyComputer));
105 			Assert.IsFalse (hash.Check (e), "Check (zone)");
106 			e.AddAssembly (hashEvidence);
107 			Assert.IsFalse (hash.Check (e), "Check (hash-assembly)");
108 
109 			e = new Evidence ();
110 			e.AddHost (hashEvidence);
111 			Assert.IsTrue (hash.Check (e), "Check (MD5-host)");
112 
113 			hash = new HashMembershipCondition (sha1, digestSha1);
114 			Assert.IsTrue (hash.Check (e), "Check (SHA1-host)");
115 		}
116 
117 		[Test]
Copy()118 		public void Copy ()
119 		{
120 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
121 			HashMembershipCondition copy = (HashMembershipCondition)hash.Copy ();
122 			Assert.AreEqual (hash, copy, "Equals");
123 			Assert.IsFalse (Object.ReferenceEquals (hash, copy), "ReferenceEquals");
124 		}
125 
126 		[Test]
Equals()127 		public void Equals ()
128 		{
129 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
130 			Assert.IsFalse (hash.Equals (null), "Equals(null)");
131 			Assert.IsFalse (hash.Equals (new object ()), "Equals (object)");
132 
133 			HashMembershipCondition h2 = new HashMembershipCondition (md5, digestMd5);
134 			Assert.IsTrue (hash.Equals (h2), "Equals(h2)");
135 			Assert.IsTrue (h2.Equals (hash), "Equals(hash)");
136 
137 			// same assembly but different algorithm / value
138 			hash = new HashMembershipCondition (sha1, digestSha1);
139 			Assert.IsFalse (hash.Equals (h2), "Equals(h2)");
140 			Assert.IsFalse (h2.Equals (hash), "Equals(hash)");
141 		}
142 
143 		[Test]
144 		[ExpectedException (typeof (ArgumentNullException))]
FromXml_Null()145 		public void FromXml_Null ()
146 		{
147 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
148 			hash.FromXml (null);
149 		}
150 
151 		[Test]
FromXml()152 		public void FromXml ()
153 		{
154 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
155 			SecurityElement se = hash.ToXml ();
156 			hash.FromXml (se);
157 		}
158 
159 		[Test]
160 		[ExpectedException (typeof (ArgumentException))]
FromXml_InvalidTag()161 		public void FromXml_InvalidTag ()
162 		{
163 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
164 			SecurityElement se = hash.ToXml ();
165 			se.Tag = "IMonoship";
166 			hash.FromXml (se);
167 		}
168 
169 		[Test]
170 		[ExpectedException (typeof (ArgumentException))]
FromXml_WrongTagCase()171 		public void FromXml_WrongTagCase ()
172 		{
173 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
174 			SecurityElement se = hash.ToXml ();
175 			se.Tag = "IMEMBERSHIPCONDITION"; // instehash of IMembershipCondition
176 			hash.FromXml (se);
177 		}
178 
179 		[Test]
FromXml_InvalidClass()180 		public void FromXml_InvalidClass ()
181 		{
182 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
183 			SecurityElement se = hash.ToXml ();
184 			se.Attributes ["class"] = "Hello world";
185 			hash.FromXml (se);
186 		}
187 
188 		[Test]
FromXml_NoClass()189 		public void FromXml_NoClass ()
190 		{
191 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
192 			SecurityElement se = hash.ToXml ();
193 
194 			SecurityElement w = new SecurityElement (se.Tag);
195 			w.AddAttribute ("version", se.Attribute ("version"));
196 			hash.FromXml (w);
197 			// doesn't even care of the class attribute presence
198 		}
199 
200 		[Test]
FromXml_InvalidVersion()201 		public void FromXml_InvalidVersion ()
202 		{
203 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
204 			SecurityElement se = hash.ToXml ();
205 
206 			SecurityElement w = new SecurityElement (se.Tag);
207 			w.AddAttribute ("class", se.Attribute ("class"));
208 			w.AddAttribute ("version", "2");
209 			hash.FromXml (w);
210 			// doesn't seems to care about the version number!
211 		}
212 
213 		[Test]
FromXml_NoVersion()214 		public void FromXml_NoVersion ()
215 		{
216 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
217 			SecurityElement se = hash.ToXml ();
218 
219 			SecurityElement w = new SecurityElement (se.Tag);
220 			w.AddAttribute ("class", se.Attribute ("class"));
221 			hash.FromXml (w);
222 		}
223 
224 		[Test]
FromXml_Empty_HashAlgorithm()225 		public void FromXml_Empty_HashAlgorithm ()
226 		{
227 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
228 			SecurityElement se = hash.ToXml ();
229 
230 			SecurityElement w = new SecurityElement (se.Tag);
231 			w.AddAttribute ("class", se.Attribute ("class"));
232 			w.AddAttribute ("version", "1");
233 			hash.FromXml (w);
234 			// this is accepted - but doesn't include a hash algorithm or value
235 			// both would throw ArgumentNullException from the constructor
236 
237 			Assert.IsTrue ((hash.HashAlgorithm is SHA1Managed), "HashAlgorithm");
238 		}
239 
240 		[Test]
241 		[ExpectedException (typeof (ArgumentException))]
FromXml_Empty_HashValue()242 		public void FromXml_Empty_HashValue()
243 		{
244 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
245 			SecurityElement se = hash.ToXml ();
246 
247 			SecurityElement w = new SecurityElement (se.Tag);
248 			w.AddAttribute ("class", se.Attribute ("class"));
249 			w.AddAttribute ("version", "1");
250 			hash.FromXml (w);
251 			// this is accepted - but doesn't include a hash algorithm or value
252 			// both would throw ArgumentNullException from the constructor
253 			byte[] value = hash.HashValue;
254 		}
255 
256 		[Test]
257 		[ExpectedException (typeof (ArgumentException))]
FromXml_Empty_ToString()258 		public void FromXml_Empty_ToString ()
259 		{
260 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
261 			SecurityElement se = hash.ToXml ();
262 
263 			SecurityElement w = new SecurityElement (se.Tag);
264 			w.AddAttribute ("class", se.Attribute ("class"));
265 			w.AddAttribute ("version", "1");
266 			hash.FromXml (w);
267 			// this is accepted - but doesn't include a hash algorithm or value
268 			// both would throw ArgumentNullException from the constructor
269 			string s = hash.ToString ();
270 		}
271 
272 		[Test]
273 #if MOBILE
274 		[Ignore]
275 #endif
276 		[ExpectedException (typeof (ArgumentNullException))]
FromXml_SecurityElementNull()277 		public void FromXml_SecurityElementNull ()
278 		{
279 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
280 			hash.FromXml (null, PolicyLevel.CreateAppDomainLevel ());
281 		}
282 
283 		[Test]
FromXml_PolicyLevelNull()284 		public void FromXml_PolicyLevelNull ()
285 		{
286 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
287 			SecurityElement se = hash.ToXml ();
288 			hash.FromXml (se, null);
289 		}
290 
291 		[Test]
GetHashCode_()292 		public void GetHashCode_ ()
293 		{
294 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
295 			HashMembershipCondition copy = (HashMembershipCondition)hash.Copy ();
296 			Assert.AreEqual (hash.GetHashCode (), copy.GetHashCode ());
297 		}
298 
299 		[Test]
ToString_()300 		public void ToString_ ()
301 		{
302 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
303 			Assert.IsTrue (hash.ToString ().StartsWith ("Hash - System.Security.Cryptography.MD5"), "MD5");
304 
305 			hash = new HashMembershipCondition (sha1, digestSha1);
306 			Assert.IsTrue (hash.ToString ().StartsWith ("Hash - System.Security.Cryptography.SHA1"), "SHA1");
307 		}
308 
309 		[Test]
310 #if MOBILE
311 		[Ignore]
312 #endif
ToXml()313 		public void ToXml ()
314 		{
315 			HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
316 			SecurityElement se = hash.ToXml ();
317 			Assert.AreEqual ("IMembershipCondition", se.Tag, "Tag");
318 			Assert.IsTrue (se.Attribute ("class").StartsWith ("System.Security.Policy.HashMembershipCondition"), "class");
319 			Assert.AreEqual ("1", se.Attribute ("version"), "version");
320 			Assert.AreEqual (se.ToString (), hash.ToXml (null).ToString (), "ToXml(null)");
321 			Assert.AreEqual (se.ToString (), hash.ToXml (PolicyLevel.CreateAppDomainLevel ()).ToString (), "ToXml(PolicyLevel)");
322 		}
323 	}
324 }
325