DnsPermissionTest.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. #if !MOBILE
  29. using NUnit.Framework;
  30. using System;
  31. using System.Net;
  32. using System.Security;
  33. using System.Security.Permissions;
  34. namespace MonoTests.System.Net {
  35. [TestFixture]
  36. public class DnsPermissionTest {
  37. [Test]
  38. public void PermissionState_None ()
  39. {
  40. PermissionState ps = PermissionState.None;
  41. DnsPermission dp = new DnsPermission (ps);
  42. Assert.IsFalse (dp.IsUnrestricted (), "IsUnrestricted");
  43. SecurityElement se = dp.ToXml ();
  44. // only class and version are present
  45. Assert.AreEqual (2, se.Attributes.Count, "Xml-Attributes#");
  46. Assert.IsNull (se.Children, "Xml-Children");
  47. DnsPermission copy = (DnsPermission)dp.Copy ();
  48. Assert.IsFalse (Object.ReferenceEquals (dp, copy), "ReferenceEquals");
  49. Assert.AreEqual (dp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
  50. }
  51. [Test]
  52. public void PermissionState_Unrestricted ()
  53. {
  54. PermissionState ps = PermissionState.Unrestricted;
  55. DnsPermission dp = new DnsPermission (ps);
  56. Assert.IsTrue (dp.IsUnrestricted (), "IsUnrestricted");
  57. SecurityElement se = dp.ToXml ();
  58. Assert.AreEqual ("true", se.Attribute ("Unrestricted"), "Xml-Unrestricted");
  59. Assert.AreEqual (3, se.Attributes.Count, "Xml-Attributes#");
  60. Assert.IsNull (se.Children, "Xml-Children");
  61. DnsPermission copy = (DnsPermission)dp.Copy ();
  62. Assert.IsFalse (Object.ReferenceEquals (dp, copy), "ReferenceEquals");
  63. Assert.AreEqual (dp.IsUnrestricted (), copy.IsUnrestricted (), "IsUnrestricted ()");
  64. }
  65. [Test]
  66. public void PermissionState_Bad ()
  67. {
  68. PermissionState ps = (PermissionState)Int32.MinValue;
  69. DnsPermission dp = new DnsPermission (ps);
  70. // no ArgumentException here
  71. Assert.IsFalse (dp.IsUnrestricted ());
  72. }
  73. [Test]
  74. public void Intersect ()
  75. {
  76. DnsPermission dpn = new DnsPermission (PermissionState.None);
  77. Assert.IsNull (dpn.Intersect (null), "None N null");
  78. Assert.IsNull (dpn.Intersect (dpn), "None N None");
  79. DnsPermission dpu = new DnsPermission (PermissionState.Unrestricted);
  80. Assert.IsNull (dpu.Intersect (null), "Unrestricted N null");
  81. DnsPermission result = (DnsPermission) dpu.Intersect (dpu);
  82. Assert.IsTrue (result.IsUnrestricted (), "Unrestricted N Unrestricted");
  83. Assert.IsNull (dpn.Intersect (dpu), "None N Unrestricted");
  84. Assert.IsNull (dpu.Intersect (dpn), "Unrestricted N None");
  85. }
  86. [Test]
  87. [ExpectedException (typeof (ArgumentException))]
  88. public void Intersect_BadPermission ()
  89. {
  90. DnsPermission dp = new DnsPermission (PermissionState.None);
  91. dp.Intersect (new SecurityPermission (PermissionState.Unrestricted));
  92. }
  93. [Test]
  94. public void IsSubset ()
  95. {
  96. DnsPermission dpn = new DnsPermission (PermissionState.None);
  97. DnsPermission dpu = new DnsPermission (PermissionState.Unrestricted);
  98. Assert.IsTrue (dpn.IsSubsetOf (null), "None IsSubsetOf null");
  99. Assert.IsFalse (dpu.IsSubsetOf (null), "Unrestricted IsSubsetOf null");
  100. Assert.IsTrue (dpn.IsSubsetOf (dpn), "None IsSubsetOf None");
  101. Assert.IsTrue (dpu.IsSubsetOf (dpu), "Unrestricted IsSubsetOf Unrestricted");
  102. Assert.IsTrue (dpn.IsSubsetOf (dpu), "None IsSubsetOf Unrestricted");
  103. Assert.IsFalse (dpu.IsSubsetOf (dpn), "Unrestricted IsSubsetOf None");
  104. }
  105. [Test]
  106. [ExpectedException (typeof (ArgumentException))]
  107. public void IsSubset_BadPermission ()
  108. {
  109. DnsPermission dp = new DnsPermission (PermissionState.None);
  110. dp.IsSubsetOf (new SecurityPermission (PermissionState.Unrestricted));
  111. }
  112. [Test]
  113. public void Union ()
  114. {
  115. DnsPermission dpn = new DnsPermission (PermissionState.None);
  116. DnsPermission dpu = new DnsPermission (PermissionState.Unrestricted);
  117. DnsPermission result = (DnsPermission) dpn.Union (null);
  118. Assert.IsFalse (result.IsUnrestricted (), "None U null");
  119. result = (DnsPermission) dpu.Union (null);
  120. Assert.IsTrue (result.IsUnrestricted (), "Unrestricted U null");
  121. result = (DnsPermission) dpn.Union (dpn);
  122. Assert.IsFalse (result.IsUnrestricted (), "None U None");
  123. result = (DnsPermission) dpu.Union (dpu);
  124. Assert.IsTrue (result.IsUnrestricted (), "Unrestricted U Unrestricted");
  125. result = (DnsPermission) dpn.Union (dpu);
  126. Assert.IsTrue (result.IsUnrestricted (), "None U Unrestricted");
  127. result = (DnsPermission) dpu.Union (dpn);
  128. Assert.IsTrue (result.IsUnrestricted (), "Unrestricted U None");
  129. }
  130. [Test]
  131. [ExpectedException (typeof (ArgumentException))]
  132. public void Union_BadPermission ()
  133. {
  134. DnsPermission dp = new DnsPermission (PermissionState.None);
  135. dp.Union (new SecurityPermission (PermissionState.Unrestricted));
  136. }
  137. [Test]
  138. [ExpectedException (typeof (ArgumentNullException))]
  139. public void FromXml_Null ()
  140. {
  141. DnsPermission dp = new DnsPermission (PermissionState.None);
  142. dp.FromXml (null);
  143. }
  144. [Test]
  145. [ExpectedException (typeof (ArgumentException))]
  146. public void FromXml_WrongTag ()
  147. {
  148. DnsPermission dp = new DnsPermission (PermissionState.None);
  149. SecurityElement se = dp.ToXml ();
  150. se.Tag = "IMono";
  151. dp.FromXml (se);
  152. // note: normally IPermission classes (in corlib) DO care about the
  153. // IPermission tag
  154. }
  155. [Test]
  156. [ExpectedException (typeof (ArgumentException))]
  157. public void FromXml_WrongTagCase ()
  158. {
  159. DnsPermission dp = new DnsPermission (PermissionState.None);
  160. SecurityElement se = dp.ToXml ();
  161. se.Tag = "IPERMISSION"; // instead of IPermission
  162. dp.FromXml (se);
  163. // note: normally IPermission classes (in corlib) DO care about the
  164. // IPermission tag
  165. }
  166. [Test]
  167. public void FromXml_WrongClass ()
  168. {
  169. DnsPermission dp = new DnsPermission (PermissionState.None);
  170. SecurityElement se = dp.ToXml ();
  171. SecurityElement w = new SecurityElement (se.Tag);
  172. w.AddAttribute ("class", "Wrong" + se.Attribute ("class"));
  173. w.AddAttribute ("version", se.Attribute ("version"));
  174. dp.FromXml (w);
  175. // doesn't care of the class name at that stage
  176. // anyway the class has already be created so...
  177. }
  178. [Test]
  179. [ExpectedException (typeof (ArgumentException))]
  180. public void FromXml_NoClass ()
  181. {
  182. DnsPermission dp = new DnsPermission (PermissionState.None);
  183. SecurityElement se = dp.ToXml ();
  184. SecurityElement w = new SecurityElement (se.Tag);
  185. w.AddAttribute ("version", se.Attribute ("version"));
  186. dp.FromXml (w);
  187. // note: normally IPermission classes (in corlib) DO NOT care about
  188. // attribute "class" name presence in the XML
  189. }
  190. [Test]
  191. [ExpectedException (typeof (ArgumentException))]
  192. public void FromXml_WrongVersion ()
  193. {
  194. DnsPermission dp = new DnsPermission (PermissionState.None);
  195. SecurityElement se = dp.ToXml ();
  196. se.Attributes.Remove ("version");
  197. se.Attributes.Add ("version", "2");
  198. dp.FromXml (se);
  199. }
  200. [Test]
  201. public void FromXml_NoVersion ()
  202. {
  203. DnsPermission dp = new DnsPermission (PermissionState.None);
  204. SecurityElement se = dp.ToXml ();
  205. SecurityElement w = new SecurityElement (se.Tag);
  206. w.AddAttribute ("class", se.Attribute ("class"));
  207. dp.FromXml (w);
  208. }
  209. }
  210. }
  211. #endif