[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / corlib / Test / System.Security.Policy / HashMembershipConditionTest.cs
blobb09232900c1c97527230162bbb0f3fb803b9d714
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.
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;
37 namespace MonoTests.System.Security.Policy {
39 [TestFixture]
40 public class HashMembershipConditionTest {
42 static Hash hashEvidence;
43 static MD5 md5;
44 static SHA1 sha1;
45 static byte[] digestMd5;
46 static byte[] digestSha1;
48 [TestFixtureSetUp]
49 public void FixtureSetUp ()
51 Assembly a = Assembly.GetExecutingAssembly ();
52 hashEvidence = new Hash (a);
54 md5 = MD5.Create ();
55 digestMd5 = hashEvidence.GenerateHash (md5);
57 sha1 = SHA1.Create ();
58 digestSha1 = hashEvidence.GenerateHash (sha1);
61 [Test]
62 public void Constructor_MD5 ()
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");
70 [Test]
71 public void Constructor_SHA1 ()
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");
79 [Test]
80 public void HashValue ()
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]");
96 [Test]
97 public void Check ()
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)");
109 e = new Evidence ();
110 e.AddHost (hashEvidence);
111 Assert.IsTrue (hash.Check (e), "Check (MD5-host)");
113 hash = new HashMembershipCondition (sha1, digestSha1);
114 Assert.IsTrue (hash.Check (e), "Check (SHA1-host)");
117 [Test]
118 public void Copy ()
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");
126 [Test]
127 public void Equals ()
129 HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
130 Assert.IsFalse (hash.Equals (null), "Equals(null)");
131 Assert.IsFalse (hash.Equals (new object ()), "Equals (object)");
133 HashMembershipCondition h2 = new HashMembershipCondition (md5, digestMd5);
134 Assert.IsTrue (hash.Equals (h2), "Equals(h2)");
135 Assert.IsTrue (h2.Equals (hash), "Equals(hash)");
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)");
143 [Test]
144 [ExpectedException (typeof (ArgumentNullException))]
145 public void FromXml_Null ()
147 HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
148 hash.FromXml (null);
151 [Test]
152 public void FromXml ()
154 HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
155 SecurityElement se = hash.ToXml ();
156 hash.FromXml (se);
159 [Test]
160 [ExpectedException (typeof (ArgumentException))]
161 public void FromXml_InvalidTag ()
163 HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
164 SecurityElement se = hash.ToXml ();
165 se.Tag = "IMonoship";
166 hash.FromXml (se);
169 [Test]
170 [ExpectedException (typeof (ArgumentException))]
171 public void FromXml_WrongTagCase ()
173 HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
174 SecurityElement se = hash.ToXml ();
175 se.Tag = "IMEMBERSHIPCONDITION"; // instehash of IMembershipCondition
176 hash.FromXml (se);
179 [Test]
180 public void FromXml_InvalidClass ()
182 HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
183 SecurityElement se = hash.ToXml ();
184 se.Attributes ["class"] = "Hello world";
185 hash.FromXml (se);
188 [Test]
189 public void FromXml_NoClass ()
191 HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
192 SecurityElement se = hash.ToXml ();
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
200 [Test]
201 public void FromXml_InvalidVersion ()
203 HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
204 SecurityElement se = hash.ToXml ();
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!
213 [Test]
214 public void FromXml_NoVersion ()
216 HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
217 SecurityElement se = hash.ToXml ();
219 SecurityElement w = new SecurityElement (se.Tag);
220 w.AddAttribute ("class", se.Attribute ("class"));
221 hash.FromXml (w);
224 [Test]
225 public void FromXml_Empty_HashAlgorithm ()
227 HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
228 SecurityElement se = hash.ToXml ();
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
237 Assert.IsTrue ((hash.HashAlgorithm is SHA1Managed), "HashAlgorithm");
240 [Test]
241 [ExpectedException (typeof (ArgumentException))]
242 public void FromXml_Empty_HashValue()
244 HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
245 SecurityElement se = hash.ToXml ();
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;
256 [Test]
257 [ExpectedException (typeof (ArgumentException))]
258 public void FromXml_Empty_ToString ()
260 HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
261 SecurityElement se = hash.ToXml ();
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 ();
272 [Test]
273 #if MOBILE
274 [Ignore]
275 #endif
276 [ExpectedException (typeof (ArgumentNullException))]
277 public void FromXml_SecurityElementNull ()
279 HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
280 hash.FromXml (null, PolicyLevel.CreateAppDomainLevel ());
283 [Test]
284 public void FromXml_PolicyLevelNull ()
286 HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
287 SecurityElement se = hash.ToXml ();
288 hash.FromXml (se, null);
291 [Test]
292 public void GetHashCode_ ()
294 HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
295 HashMembershipCondition copy = (HashMembershipCondition)hash.Copy ();
296 Assert.AreEqual (hash.GetHashCode (), copy.GetHashCode ());
299 [Test]
300 public void ToString_ ()
302 HashMembershipCondition hash = new HashMembershipCondition (md5, digestMd5);
303 Assert.IsTrue (hash.ToString ().StartsWith ("Hash - System.Security.Cryptography.MD5"), "MD5");
305 hash = new HashMembershipCondition (sha1, digestSha1);
306 Assert.IsTrue (hash.ToString ().StartsWith ("Hash - System.Security.Cryptography.SHA1"), "SHA1");
309 [Test]
310 #if MOBILE
311 [Ignore]
312 #endif
313 public void ToXml ()
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)");