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

https://github.com/t-ashula/mono · C# · 236 lines · 172 code · 31 blank · 33 comment · 0 complexity · f1abe96b9a60267da43ae09e828b3411 MD5 · raw file

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