/mcs/class/corlib/Test/System.Security.Policy/ApplicationDirectoryMembershipConditionTest.cs

https://github.com/iainlane/mono · C# · 224 lines · 167 code · 27 blank · 30 comment · 0 complexity · 1fc87918859ff8ba596b97c79cfbb986 MD5 · raw file

  1. //
  2. // ApplicationDirectoryMembershipConditionTest.cs -
  3. // NUnit Test Cases for ApplicationDirectoryMembershipCondition
  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. using NUnit.Framework;
  30. using System;
  31. using System.Reflection;
  32. using System.Security;
  33. using System.Security.Policy;
  34. namespace MonoTests.System.Security.Policy {
  35. [TestFixture]
  36. public class ApplicationDirectoryMembershipConditionTest {
  37. [Test]
  38. public void Constructor ()
  39. {
  40. ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
  41. Assert.IsNotNull (ad);
  42. }
  43. [Test]
  44. public void Check ()
  45. {
  46. ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
  47. Evidence e = null;
  48. Assert.IsFalse (ad.Check (e), "Check (null)");
  49. e = new Evidence ();
  50. Assert.IsFalse (ad.Check (e), "Check (empty)");
  51. e.AddHost (new Zone (SecurityZone.MyComputer));
  52. Assert.IsFalse (ad.Check (e), "Check (zone)");
  53. string codebase = Assembly.GetExecutingAssembly ().CodeBase;
  54. Url u = new Url (codebase);
  55. ApplicationDirectory adir = new ApplicationDirectory (codebase);
  56. e.AddHost (u);
  57. Assert.IsFalse (ad.Check (e), "Check (url-host)"); // not enough
  58. e.AddAssembly (adir);
  59. Assert.IsFalse (ad.Check (e), "Check (url-host+adir-assembly)");
  60. e = new Evidence ();
  61. e.AddHost (adir);
  62. Assert.IsFalse (ad.Check (e), "Check (adir-host)"); // not enough
  63. e.AddAssembly (u);
  64. Assert.IsFalse (ad.Check (e), "Check (url-assembly+adir-host)");
  65. e = new Evidence ();
  66. e.AddHost (u);
  67. e.AddHost (adir);
  68. Assert.IsTrue (ad.Check (e), "Check (url+adir host)"); // both!!
  69. }
  70. [Test]
  71. public void Copy ()
  72. {
  73. ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
  74. ApplicationDirectoryMembershipCondition copy = (ApplicationDirectoryMembershipCondition)ad.Copy ();
  75. Assert.AreEqual (ad, copy, "Equals");
  76. Assert.IsFalse (Object.ReferenceEquals (ad, copy), "ReferenceEquals");
  77. }
  78. [Test]
  79. public void Equals ()
  80. {
  81. ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
  82. Assert.IsFalse (ad.Equals (null), "Equals(null)");
  83. ApplicationDirectoryMembershipCondition g2 = new ApplicationDirectoryMembershipCondition ();
  84. Assert.IsTrue (ad.Equals (g2), "Equals(g2)");
  85. Assert.IsTrue (g2.Equals (ad), "Equals(ad)");
  86. Assert.IsFalse (ad.Equals (new object ()), "Equals (object)");
  87. }
  88. [Test]
  89. [ExpectedException (typeof (ArgumentNullException))]
  90. public void FromXml_Null ()
  91. {
  92. ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
  93. ad.FromXml (null);
  94. }
  95. [Test]
  96. public void FromXml ()
  97. {
  98. ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
  99. SecurityElement se = ad.ToXml ();
  100. ad.FromXml (se);
  101. }
  102. [Test]
  103. [ExpectedException (typeof (ArgumentException))]
  104. public void FromXml_InvalidTag ()
  105. {
  106. ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
  107. SecurityElement se = ad.ToXml ();
  108. se.Tag = "IMonoship";
  109. ad.FromXml (se);
  110. }
  111. [Test]
  112. [ExpectedException (typeof (ArgumentException))]
  113. public void FromXml_WrongTagCase ()
  114. {
  115. ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
  116. SecurityElement se = ad.ToXml ();
  117. se.Tag = "IMEMBERSHIPCONDITION"; // instead of IMembershipCondition
  118. ad.FromXml (se);
  119. }
  120. [Test]
  121. public void FromXml_InvalidClass ()
  122. {
  123. ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
  124. SecurityElement se = ad.ToXml ();
  125. se.Attributes ["class"] = "Hello world";
  126. ad.FromXml (se);
  127. }
  128. [Test]
  129. public void FromXml_NoClass ()
  130. {
  131. ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
  132. SecurityElement se = ad.ToXml ();
  133. SecurityElement w = new SecurityElement (se.Tag);
  134. w.AddAttribute ("version", se.Attribute ("version"));
  135. ad.FromXml (w);
  136. // doesn't even care of the class attribute presence
  137. }
  138. [Test]
  139. public void FromXml_InvalidVersion ()
  140. {
  141. ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
  142. SecurityElement se = ad.ToXml ();
  143. SecurityElement w = new SecurityElement (se.Tag);
  144. w.AddAttribute ("class", se.Attribute ("class"));
  145. w.AddAttribute ("version", "2");
  146. ad.FromXml (w);
  147. // doesn't seems to care about the version number!
  148. }
  149. [Test]
  150. public void FromXml_NoVersion ()
  151. {
  152. ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
  153. SecurityElement se = ad.ToXml ();
  154. SecurityElement w = new SecurityElement (se.Tag);
  155. w.AddAttribute ("class", se.Attribute ("class"));
  156. ad.FromXml (w);
  157. }
  158. [Test]
  159. [ExpectedException (typeof (ArgumentNullException))]
  160. public void FromXml_SecurityElementNull ()
  161. {
  162. ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
  163. ad.FromXml (null, PolicyLevel.CreateAppDomainLevel ());
  164. }
  165. [Test]
  166. public void FromXml_PolicyLevelNull ()
  167. {
  168. ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
  169. SecurityElement se = ad.ToXml ();
  170. ad.FromXml (se, null);
  171. }
  172. [Test]
  173. public void GetHashCode_ ()
  174. {
  175. ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
  176. ApplicationDirectoryMembershipCondition copy = (ApplicationDirectoryMembershipCondition)ad.Copy ();
  177. Assert.AreEqual (ad.GetHashCode (), copy.GetHashCode ());
  178. }
  179. [Test]
  180. public void ToString_ ()
  181. {
  182. ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
  183. Assert.AreEqual ("ApplicationDirectory", ad.ToString ());
  184. }
  185. [Test]
  186. public void ToXml ()
  187. {
  188. ApplicationDirectoryMembershipCondition ad = new ApplicationDirectoryMembershipCondition ();
  189. SecurityElement se = ad.ToXml ();
  190. Assert.AreEqual ("IMembershipCondition", se.Tag, "Tag");
  191. Assert.IsTrue (se.Attribute ("class").StartsWith ("System.Security.Policy.ApplicationDirectoryMembershipCondition"), "class");
  192. Assert.AreEqual ("1", se.Attribute ("version"), "version");
  193. Assert.AreEqual (se.ToString (), ad.ToXml (null).ToString (), "ToXml(null)");
  194. Assert.AreEqual (se.ToString (), ad.ToXml (PolicyLevel.CreateAppDomainLevel ()).ToString (), "ToXml(PolicyLevel)");
  195. }
  196. }
  197. }