DnsPermissionTest.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // DnsPermissionTest.cs - NUnit Test Cases for DnsPermission
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using NUnit.Framework;
  29. using System;
  30. using System.Net;
  31. using System.Security;
  32. using System.Security.Permissions;
  33. namespace MonoTests.System.Net {
  34. [TestFixture]
  35. public class DnsPermissionTest {
  36. [Test]
  37. public void PermissionState_None ()
  38. {
  39. PermissionState ps = PermissionState.None;
  40. DnsPermission dp = new DnsPermission (ps);
  41. Assert.IsFalse (dp.IsUnrestricted (), "IsUnrestricted");
  42. SecurityElement se = dp.ToXml ();
  43. // only class and version are present
  44. Assert.AreEqual (2, se.Attributes.Count, "Xml-Attributes#");
  45. Assert.IsNull (se.Children, "Xml-Children");
  46. DnsPermission copy = (DnsPermission)dp.Copy ();
  47. Assert.IsFalse (Object.ReferenceEquals (dp, copy), "ReferenceEquals");
  48. Assert.AreEqual (dp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
  49. }
  50. [Test]
  51. public void PermissionState_Unrestricted ()
  52. {
  53. PermissionState ps = PermissionState.Unrestricted;
  54. DnsPermission dp = new DnsPermission (ps);
  55. Assert.IsTrue (dp.IsUnrestricted (), "IsUnrestricted");
  56. SecurityElement se = dp.ToXml ();
  57. Assert.AreEqual ("true", se.Attribute ("Unrestricted"), "Xml-Unrestricted");
  58. Assert.AreEqual (3, se.Attributes.Count, "Xml-Attributes#");
  59. Assert.IsNull (se.Children, "Xml-Children");
  60. DnsPermission copy = (DnsPermission)dp.Copy ();
  61. Assert.IsFalse (Object.ReferenceEquals (dp, copy), "ReferenceEquals");
  62. Assert.AreEqual (dp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
  63. }
  64. [Test]
  65. public void PermissionState_Bad ()
  66. {
  67. PermissionState ps = (PermissionState)Int32.MinValue;
  68. DnsPermission dp = new DnsPermission (ps);
  69. // no ArgumentException here
  70. Assert.IsFalse (dp.IsUnrestricted ());
  71. }
  72. [Test]
  73. public void Intersect ()
  74. {
  75. DnsPermission dpn = new DnsPermission (PermissionState.None);
  76. Assert.IsNull (dpn.Intersect (null), "None N null");
  77. Assert.IsNull (dpn.Intersect (dpn), "None N None");
  78. DnsPermission dpu = new DnsPermission (PermissionState.Unrestricted);
  79. Assert.IsNull (dpu.Intersect (null), "Unrestricted N null");
  80. DnsPermission result = (DnsPermission) dpu.Intersect (dpu);
  81. Assert.IsTrue (result.IsUnrestricted (), "Unrestricted N Unrestricted");
  82. Assert.IsNull (dpn.Intersect (dpu), "None N Unrestricted");
  83. Assert.IsNull (dpu.Intersect (dpn), "Unrestricted N None");
  84. }
  85. [Test]
  86. [ExpectedException (typeof (ArgumentException))]
  87. public void Intersect_BadPermission ()
  88. {
  89. DnsPermission dp = new DnsPermission (PermissionState.None);
  90. dp.Intersect (new SecurityPermission (PermissionState.Unrestricted));
  91. }
  92. [Test]
  93. public void IsSubset ()
  94. {
  95. DnsPermission dpn = new DnsPermission (PermissionState.None);
  96. DnsPermission dpu = new DnsPermission (PermissionState.Unrestricted);
  97. Assert.IsTrue (dpn.IsSubsetOf (null), "None IsSubsetOf null");
  98. Assert.IsFalse (dpu.IsSubsetOf (null), "Unrestricted IsSubsetOf null");
  99. Assert.IsTrue (dpn.IsSubsetOf (dpn), "None IsSubsetOf None");
  100. Assert.IsTrue (dpu.IsSubsetOf (dpu), "Unrestricted IsSubsetOf Unrestricted");
  101. Assert.IsTrue (dpn.IsSubsetOf (dpu), "None IsSubsetOf Unrestricted");
  102. Assert.IsFalse (dpu.IsSubsetOf (dpn), "Unrestricted IsSubsetOf None");
  103. }
  104. [Test]
  105. [ExpectedException (typeof (ArgumentException))]
  106. public void IsSubset_BadPermission ()
  107. {
  108. DnsPermission dp = new DnsPermission (PermissionState.None);
  109. dp.IsSubsetOf (new SecurityPermission (PermissionState.Unrestricted));
  110. }
  111. [Test]
  112. public void Union ()
  113. {
  114. DnsPermission dpn = new DnsPermission (PermissionState.None);
  115. DnsPermission dpu = new DnsPermission (PermissionState.Unrestricted);
  116. DnsPermission result = (DnsPermission) dpn.Union (null);
  117. Assert.IsFalse (result.IsUnrestricted (), "None U null");
  118. result = (DnsPermission) dpu.Union (null);
  119. Assert.IsTrue (result.IsUnrestricted (), "Unrestricted U null");
  120. result = (DnsPermission) dpn.Union (dpn);
  121. Assert.IsFalse (result.IsUnrestricted (), "None U None");
  122. result = (DnsPermission) dpu.Union (dpu);
  123. Assert.IsTrue (result.IsUnrestricted (), "Unrestricted U Unrestricted");
  124. result = (DnsPermission) dpn.Union (dpu);
  125. Assert.IsTrue (result.IsUnrestricted (), "None U Unrestricted");
  126. result = (DnsPermission) dpu.Union (dpn);
  127. Assert.IsTrue (result.IsUnrestricted (), "Unrestricted U None");
  128. }
  129. [Test]
  130. [ExpectedException (typeof (ArgumentException))]
  131. public void Union_BadPermission ()
  132. {
  133. DnsPermission dp = new DnsPermission (PermissionState.None);
  134. dp.Union (new SecurityPermission (PermissionState.Unrestricted));
  135. }
  136. [Test]
  137. [ExpectedException (typeof (ArgumentNullException))]
  138. public void FromXml_Null ()
  139. {
  140. DnsPermission dp = new DnsPermission (PermissionState.None);
  141. dp.FromXml (null);
  142. }
  143. [Test]
  144. [ExpectedException (typeof (ArgumentException))]
  145. public void FromXml_WrongTag ()
  146. {
  147. DnsPermission dp = new DnsPermission (PermissionState.None);
  148. SecurityElement se = dp.ToXml ();
  149. se.Tag = "IMono";
  150. dp.FromXml (se);
  151. // note: normally IPermission classes (in corlib) DO care about the
  152. // IPermission tag
  153. }
  154. [Test]
  155. [ExpectedException (typeof (ArgumentException))]
  156. public void FromXml_WrongTagCase ()
  157. {
  158. DnsPermission dp = new DnsPermission (PermissionState.None);
  159. SecurityElement se = dp.ToXml ();
  160. se.Tag = "IPERMISSION"; // instead of IPermission
  161. dp.FromXml (se);
  162. // note: normally IPermission classes (in corlib) DO care about the
  163. // IPermission tag
  164. }
  165. [Test]
  166. public void FromXml_WrongClass ()
  167. {
  168. DnsPermission dp = new DnsPermission (PermissionState.None);
  169. SecurityElement se = dp.ToXml ();
  170. SecurityElement w = new SecurityElement (se.Tag);
  171. w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
  172. w.AddAttribute ("version", se.Attribute ("version"));
  173. dp.FromXml (w);
  174. // doesn't care of the class name at that stage
  175. // anyway the class has already be created so...
  176. }
  177. [Test]
  178. [ExpectedException (typeof (ArgumentException))]
  179. public void FromXml_NoClass ()
  180. {
  181. DnsPermission dp = new DnsPermission (PermissionState.None);
  182. SecurityElement se = dp.ToXml ();
  183. SecurityElement w = new SecurityElement (se.Tag);
  184. w.AddAttribute ("version", se.Attribute ("version"));
  185. dp.FromXml (w);
  186. // note: normally IPermission classes (in corlib) DO NOT care about
  187. // attribute "class" name presence in the XML
  188. }
  189. [Test]
  190. [ExpectedException (typeof (ArgumentException))]
  191. public void FromXml_WrongVersion ()
  192. {
  193. DnsPermission dp = new DnsPermission (PermissionState.None);
  194. SecurityElement se = dp.ToXml ();
  195. se.Attributes.Remove ("version");
  196. se.Attributes.Add ("version", "2");
  197. dp.FromXml (se);
  198. }
  199. [Test]
  200. public void FromXml_NoVersion ()
  201. {
  202. DnsPermission dp = new DnsPermission (PermissionState.None);
  203. SecurityElement se = dp.ToXml ();
  204. SecurityElement w = new SecurityElement (se.Tag);
  205. w.AddAttribute ("class", se.Attribute ("class"));
  206. dp.FromXml (w);
  207. }
  208. }
  209. }