SocketPermissionAttributeTest.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //
  2. // SocketPermissionAttributeTest.cs - NUnit Test Cases for SocketPermissionAttribute
  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. using System.Text.RegularExpressions;
  35. namespace MonoTests.System.Net {
  36. [TestFixture]
  37. public class SocketPermissionAttributeTest {
  38. [Test]
  39. public void Default ()
  40. {
  41. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  42. Assert.AreEqual (a.ToString (), a.TypeId.ToString (), "TypeId");
  43. Assert.IsFalse (a.Unrestricted, "Unrestricted");
  44. Assert.IsNull (a.Access, "Access");
  45. Assert.IsNull (a.Host, "Host");
  46. Assert.IsNull (a.Port, "Port");
  47. Assert.IsNull (a.Transport, "Transport");
  48. a.Access = "connect";
  49. a.Host = String.Empty;
  50. a.Port = "80";
  51. a.Transport = "tcp";
  52. SocketPermission sp = (SocketPermission) a.CreatePermission ();
  53. Assert.IsFalse (sp.IsUnrestricted (), "IsUnrestricted");
  54. }
  55. [Test]
  56. public void Action ()
  57. {
  58. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  59. Assert.AreEqual (SecurityAction.Assert, a.Action, "Action=Assert");
  60. a.Action = SecurityAction.Demand;
  61. Assert.AreEqual (SecurityAction.Demand, a.Action, "Action=Demand");
  62. a.Action = SecurityAction.Deny;
  63. Assert.AreEqual (SecurityAction.Deny, a.Action, "Action=Deny");
  64. a.Action = SecurityAction.InheritanceDemand;
  65. Assert.AreEqual (SecurityAction.InheritanceDemand, a.Action, "Action=InheritanceDemand");
  66. a.Action = SecurityAction.LinkDemand;
  67. Assert.AreEqual (SecurityAction.LinkDemand, a.Action, "Action=LinkDemand");
  68. a.Action = SecurityAction.PermitOnly;
  69. Assert.AreEqual (SecurityAction.PermitOnly, a.Action, "Action=PermitOnly");
  70. a.Action = SecurityAction.RequestMinimum;
  71. Assert.AreEqual (SecurityAction.RequestMinimum, a.Action, "Action=RequestMinimum");
  72. a.Action = SecurityAction.RequestOptional;
  73. Assert.AreEqual (SecurityAction.RequestOptional, a.Action, "Action=RequestOptional");
  74. a.Action = SecurityAction.RequestRefuse;
  75. Assert.AreEqual (SecurityAction.RequestRefuse, a.Action, "Action=RequestRefuse");
  76. }
  77. [Test]
  78. public void Action_Invalid ()
  79. {
  80. SocketPermissionAttribute a = new SocketPermissionAttribute ((SecurityAction)Int32.MinValue);
  81. // no validation in attribute
  82. }
  83. [Test]
  84. public void Unrestricted ()
  85. {
  86. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  87. a.Access = "connect";
  88. a.Host = String.Empty;
  89. a.Port = "80";
  90. a.Transport = "tcp";
  91. a.Unrestricted = true;
  92. SocketPermission wp = (SocketPermission)a.CreatePermission ();
  93. Assert.IsTrue (wp.IsUnrestricted (), "IsUnrestricted");
  94. a.Unrestricted = false;
  95. wp = (SocketPermission)a.CreatePermission ();
  96. Assert.IsFalse (wp.IsUnrestricted (), "!IsUnrestricted");
  97. }
  98. [Test]
  99. [ExpectedException (typeof (ArgumentException))]
  100. public void Access_Null_CreatePermission ()
  101. {
  102. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  103. a.Access = null; // legal (assign)
  104. SocketPermission sp = (SocketPermission) a.CreatePermission ();
  105. }
  106. [Test]
  107. public void Access_Null ()
  108. {
  109. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  110. a.Access = null; // legal
  111. Assert.IsNull (a.Access, "Access");
  112. }
  113. [Test]
  114. [ExpectedException (typeof (ArgumentException))]
  115. public void Access_Dual ()
  116. {
  117. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  118. a.Access = "/";
  119. a.Access = "\\";
  120. }
  121. [Test]
  122. [ExpectedException (typeof (ArgumentException))]
  123. public void Access_Dual_Null ()
  124. {
  125. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  126. a.Access = "/";
  127. a.Access = null;
  128. }
  129. [Test]
  130. public void Access ()
  131. {
  132. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  133. a.Access = "/";
  134. }
  135. [Test]
  136. [ExpectedException (typeof (ArgumentException))]
  137. public void Host_Null_CreatePermission ()
  138. {
  139. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  140. a.Access = String.Empty;
  141. a.Host = null; // legal (assign)
  142. SocketPermission sp = (SocketPermission)a.CreatePermission ();
  143. }
  144. [Test]
  145. public void Host_Null ()
  146. {
  147. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  148. a.Host = null; // legal
  149. Assert.IsNull (a.Host, "Host");
  150. }
  151. [Test]
  152. [ExpectedException (typeof (ArgumentException))]
  153. public void Host_Dual ()
  154. {
  155. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  156. a.Host = "/";
  157. a.Host = "\\";
  158. }
  159. [Test]
  160. [ExpectedException (typeof (ArgumentException))]
  161. public void Host_Dual_Null ()
  162. {
  163. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  164. a.Host = "/";
  165. a.Host = null;
  166. }
  167. [Test]
  168. public void Host ()
  169. {
  170. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  171. a.Host = "www.mono-project.com";
  172. }
  173. [Test]
  174. [ExpectedException (typeof (ArgumentException))]
  175. public void Port_Null_CreatePermission ()
  176. {
  177. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  178. a.Access = String.Empty;
  179. a.Host = String.Empty;
  180. a.Port = null; // legal (assign)
  181. SocketPermission sp = (SocketPermission)a.CreatePermission ();
  182. }
  183. [Test]
  184. public void Port_Null ()
  185. {
  186. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  187. a.Port = null; // legal
  188. Assert.IsNull (a.Port, "Port");
  189. }
  190. [Test]
  191. [ExpectedException (typeof (ArgumentException))]
  192. public void Port_Dual ()
  193. {
  194. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  195. a.Port = "/";
  196. a.Port = "\\";
  197. }
  198. [Test]
  199. [ExpectedException (typeof (ArgumentException))]
  200. public void Port_Dual_Null ()
  201. {
  202. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  203. a.Port = "/";
  204. a.Port = null;
  205. }
  206. [Test]
  207. public void Port ()
  208. {
  209. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  210. a.Port = "80";
  211. }
  212. [Test]
  213. [ExpectedException (typeof (ArgumentException))]
  214. public void Transport_Null_CreatePermission ()
  215. {
  216. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  217. a.Access = String.Empty;
  218. a.Host = String.Empty;
  219. a.Port = String.Empty;
  220. a.Transport = null; // legal (assign)
  221. SocketPermission sp = (SocketPermission)a.CreatePermission ();
  222. }
  223. [Test]
  224. public void Transport_Null ()
  225. {
  226. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  227. a.Transport = null; // legal
  228. Assert.IsNull (a.Transport, "Transport");
  229. }
  230. [Test]
  231. [ExpectedException (typeof (ArgumentException))]
  232. public void Transport_Dual ()
  233. {
  234. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  235. a.Transport = "/";
  236. a.Transport = "\\";
  237. }
  238. [Test]
  239. [ExpectedException (typeof (ArgumentException))]
  240. public void Transport_Dual_Null ()
  241. {
  242. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  243. a.Transport = "/";
  244. a.Transport = null;
  245. }
  246. [Test]
  247. public void Transport ()
  248. {
  249. SocketPermissionAttribute a = new SocketPermissionAttribute (SecurityAction.Assert);
  250. a.Transport = "http";
  251. }
  252. [Test]
  253. public void Attributes ()
  254. {
  255. Type t = typeof (SocketPermissionAttribute);
  256. Assert.IsTrue (t.IsSerializable, "IsSerializable");
  257. object [] attrs = t.GetCustomAttributes (typeof (AttributeUsageAttribute), false);
  258. Assert.AreEqual (1, attrs.Length, "AttributeUsage");
  259. AttributeUsageAttribute aua = (AttributeUsageAttribute)attrs [0];
  260. Assert.IsTrue (aua.AllowMultiple, "AllowMultiple");
  261. Assert.IsFalse (aua.Inherited, "Inherited");
  262. AttributeTargets at = (AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method);
  263. Assert.AreEqual (at, aua.ValidOn, "ValidOn");
  264. }
  265. }
  266. }
  267. #endif