PageCas.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. //
  2. // PageCas.cs - CAS unit tests for System.Web.UI.Page
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2005 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.IO;
  31. using System.Security;
  32. using System.Security.Permissions;
  33. using System.Text;
  34. using System.Web;
  35. using System.Web.UI;
  36. using System.Web.UI.HtmlControls;
  37. namespace MonoCasTests.System.Web.UI {
  38. [TestFixture]
  39. [Category ("CAS")]
  40. public class PageCas : AspNetHostingMinimal {
  41. private Control control;
  42. private Page page;
  43. [TestFixtureSetUp]
  44. public void FixtureSetUp ()
  45. {
  46. control = new Control ();
  47. control.ID = "mono";
  48. page = new Page ();
  49. }
  50. [Test]
  51. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  52. public void Properties_Deny_Unrestricted ()
  53. {
  54. Page p = new Page ();
  55. Assert.IsNull (p.Application, "Application");
  56. p.ClientTarget = "mono";
  57. Assert.AreEqual ("mono", p.ClientTarget, "ClientTarget");
  58. p.EnableViewState = true;
  59. Assert.IsTrue (p.EnableViewState, "EnableViewState");
  60. p.ErrorPage = "error.html";
  61. Assert.AreEqual ("error.html", p.ErrorPage, "ErrorPage");
  62. p.ID = "mono";
  63. Assert.AreEqual ("mono", p.ID, "ID");
  64. Assert.IsFalse (p.IsPostBack, "IsPostBack");
  65. Assert.IsFalse (p.IsReusable, "IsReusable");
  66. p.SmartNavigation = false;
  67. Assert.IsFalse (p.SmartNavigation, "SmartNavigation");
  68. Assert.IsNotNull (p.Validators, "Validators");
  69. p.ViewStateUserKey = "mono";
  70. Assert.AreEqual ("mono", p.ViewStateUserKey, "ViewStateUserKey");
  71. p.Visible = true;
  72. Assert.IsTrue (p.Visible, "Visible");
  73. #if NET_2_0
  74. Assert.IsNotNull (p.ClientScript, "ClientScript");
  75. // p.CodePage = 0;
  76. // Assert.AreEqual (0, p.CodePage, "CodePage");
  77. // p.ContentType = "mono";
  78. // Assert.AreEqual ("mono", p.ContentType, "ContentType");
  79. Assert.IsNotNull (p.Culture, "Culture");
  80. Assert.IsNull (p.Form, "Form");
  81. Assert.IsNull (p.Header, "Header");
  82. Assert.IsFalse (p.IsCallback, "IsCallback");
  83. Assert.IsFalse (p.IsCrossPagePostBack, "IsCrossPagePostBack");
  84. Assert.IsTrue (p.LCID != 0, "LCID");
  85. p.MasterPageFile = String.Empty;
  86. Assert.AreEqual (String.Empty, p.MasterPageFile, "MasterPageFile");
  87. Assert.IsNull (p.Master, "Master");
  88. Assert.IsNull (p.PageAdapter, "PageAdapter");
  89. Assert.IsNull (p.PreviousPage, "PreviousPage");
  90. // p.ResponseEncoding = Encoding.UTF8.WebName;
  91. // Assert.AreEqual (Encoding.UTF8.WebName, p.ResponseEncoding, "ResponseEncoding");
  92. p.UICulture = "en-us";
  93. Assert.IsNotNull (p.UICulture, "UICulture");
  94. #endif
  95. }
  96. [Test]
  97. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  98. [ExpectedException (typeof (HttpException))]
  99. public void Cache_Deny_Unrestricted ()
  100. {
  101. Assert.IsNotNull (new Page ().Cache, "Cache");
  102. }
  103. [Test]
  104. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  105. [ExpectedException (typeof (HttpException))]
  106. public void IsValid_Deny_Unrestricted ()
  107. {
  108. Assert.IsFalse (new Page ().IsValid, "IsValid");
  109. }
  110. [Test]
  111. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  112. [ExpectedException (typeof (HttpException))]
  113. public void Request_Deny_Unrestricted ()
  114. {
  115. Assert.IsNotNull (new Page ().Request, "Request");
  116. }
  117. [Test]
  118. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  119. [ExpectedException (typeof (HttpException))]
  120. public void Response_Deny_Unrestricted ()
  121. {
  122. Assert.IsNotNull (new Page ().Response, "Response");
  123. }
  124. [Test]
  125. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  126. [ExpectedException (typeof (NullReferenceException))]
  127. public void Server_Deny_Unrestricted ()
  128. {
  129. Assert.IsNotNull (new Page ().Server, "Server");
  130. }
  131. [Test]
  132. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  133. [ExpectedException (typeof (HttpException))]
  134. public void Session_Deny_Unrestricted ()
  135. {
  136. Assert.IsNotNull (new Page ().Session, "Session");
  137. }
  138. [Test]
  139. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  140. [ExpectedException (typeof (NullReferenceException))]
  141. public void Trace_Deny_Unrestricted ()
  142. {
  143. Assert.IsNotNull (new Page ().Trace, "Trace");
  144. }
  145. [Test]
  146. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  147. [ExpectedException (typeof (NullReferenceException))]
  148. public void User_Deny_Unrestricted ()
  149. {
  150. Assert.IsNotNull (new Page ().User, "User");
  151. }
  152. #if NET_2_0
  153. [Test]
  154. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  155. [ExpectedException (typeof (HttpException))]
  156. public void Buffer_set_Deny_Unrestricted ()
  157. {
  158. page.Buffer = true;
  159. }
  160. [Test]
  161. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  162. [ExpectedException (typeof (HttpException))]
  163. public void Buffer_get_Deny_Unrestricted ()
  164. {
  165. Assert.IsTrue (page.Buffer, "Buffer");
  166. }
  167. [Test]
  168. [SecurityPermission (SecurityAction.Deny, ControlThread = true)]
  169. [ExpectedException (typeof (SecurityException))]
  170. public void Culture_Deny_ControlThread ()
  171. {
  172. page.Culture = "fr-ca";
  173. }
  174. [Test]
  175. [SecurityPermission (SecurityAction.PermitOnly, ControlThread = true)]
  176. public void Culture_PermitOnly_ControlThread ()
  177. {
  178. page.Culture = "fr-ca";
  179. }
  180. [Test]
  181. [SecurityPermission (SecurityAction.Deny, ControlThread = true)]
  182. [ExpectedException (typeof (SecurityException))]
  183. public void LCID_Deny_ControlThread ()
  184. {
  185. page.LCID = 0x409;
  186. }
  187. [Test]
  188. [SecurityPermission (SecurityAction.PermitOnly, ControlThread = true)]
  189. public void LCID_PermitOnly_ControlThread ()
  190. {
  191. page.LCID = 0x409;
  192. }
  193. [Test]
  194. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  195. [ExpectedException (typeof (NullReferenceException))]
  196. public void TraceEnabled_set_Deny_Unrestricted ()
  197. {
  198. page.TraceEnabled = false;
  199. }
  200. [Test]
  201. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  202. [ExpectedException (typeof (NullReferenceException))]
  203. public void TraceEnabled_get_Deny_Unrestricted ()
  204. {
  205. Assert.IsFalse (page.TraceEnabled, "TraceEnabled");
  206. }
  207. [Test]
  208. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  209. [ExpectedException (typeof (NullReferenceException))]
  210. public void TraceModeValue_set_Deny_Unrestricted ()
  211. {
  212. page.TraceModeValue = TraceMode.Default;
  213. }
  214. [Test]
  215. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  216. [ExpectedException (typeof (NullReferenceException))]
  217. public void TraceModeValue_get_Deny_Unrestricted ()
  218. {
  219. Assert.AreEqual (TraceMode.Default, page.TraceModeValue, "TraceModeValue");
  220. }
  221. #endif
  222. [Test]
  223. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  224. public void Methods_Deny_Unrestricted ()
  225. {
  226. Page p = new Page ();
  227. p.DesignerInitialize ();
  228. Assert.IsNotNull (p.GetPostBackClientEvent (control, "mono"), "GetPostBackClientEvent");
  229. Assert.IsNotNull (p.GetPostBackClientHyperlink (control, "mono"), "GetPostBackClientHyperlink");
  230. Assert.IsNotNull (p.GetPostBackEventReference (control), "GetPostBackEventReference(control)");
  231. Assert.IsNotNull (p.GetPostBackEventReference (control, "mono"), "GetPostBackEventReference(control,string)");
  232. Assert.AreEqual (0, p.GetTypeHashCode (), "GetTypeHashCode");
  233. Assert.IsFalse (p.IsClientScriptBlockRegistered ("mono"), "IsClientScriptBlockRegistered");
  234. Assert.IsFalse (p.IsStartupScriptRegistered ("mono"), "IsStartupScriptRegistered");
  235. p.RegisterArrayDeclaration ("arrayname", "value");
  236. p.RegisterClientScriptBlock ("key", "script");
  237. p.RegisterHiddenField ("name", "hidden");
  238. p.RegisterOnSubmitStatement ("key", "script");
  239. p.RegisterRequiresPostBack (new HtmlTextArea ());
  240. p.RegisterRequiresRaiseEvent (new HtmlAnchor ());
  241. p.RegisterStartupScript ("key", "script");
  242. p.Validate ();
  243. p.VerifyRenderingInServerForm (control);
  244. #if NET_2_0
  245. p.Controls.Add (control);
  246. Assert.IsNotNull (p.FindControl ("mono"), "FindControl");
  247. p.RegisterRequiresControlState (control);
  248. Assert.IsTrue (p.RequiresControlState (control), "RequiresControlState");
  249. p.UnregisterRequiresControlState (control);
  250. Assert.IsNotNull (p.GetValidators (String.Empty), "GetValidators");
  251. p.Validate (String.Empty);
  252. #endif
  253. }
  254. [Test]
  255. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  256. public void MapPath_Deny_Unrestricted ()
  257. {
  258. try {
  259. new Page ().MapPath ("/");
  260. }
  261. catch (NullReferenceException) {
  262. // ms 1.x + 2.0
  263. }
  264. catch (HttpException) {
  265. // mono
  266. }
  267. }
  268. [Test]
  269. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  270. #if NET_2_0
  271. [ExpectedException (typeof (SecurityException))]
  272. #else
  273. [ExpectedException (typeof (NullReferenceException))]
  274. #endif
  275. public void ProcessRequest_Deny_Unrestricted ()
  276. {
  277. new Page ().ProcessRequest (new HttpContext (null));
  278. }
  279. [Test]
  280. [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  281. #if NET_2_0
  282. [ExpectedException (typeof (HttpException))]
  283. #else
  284. // indirect for HttpApplicationState | HttpStaticObjectsCollection
  285. [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
  286. [ExpectedException (typeof (NullReferenceException))]
  287. #endif
  288. public void ProcessRequest_PermitOnly_FileIOPermission ()
  289. {
  290. new Page ().ProcessRequest (new HttpContext (null));
  291. }
  292. #if NET_2_0
  293. private void Handler (object sender, EventArgs e)
  294. {
  295. }
  296. [Test]
  297. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  298. public void Events_Deny_Unrestricted ()
  299. {
  300. Page p = new Page ();
  301. p.InitComplete += new EventHandler (Handler);
  302. p.LoadComplete += new EventHandler (Handler);
  303. p.PreInit += new EventHandler (Handler);
  304. p.PreLoad += new EventHandler (Handler);
  305. p.PreRenderComplete += new EventHandler (Handler);
  306. p.SaveStateComplete += new EventHandler (Handler);
  307. p.InitComplete -= new EventHandler (Handler);
  308. p.LoadComplete -= new EventHandler (Handler);
  309. p.PreInit -= new EventHandler (Handler);
  310. p.PreLoad -= new EventHandler (Handler);
  311. p.PreRenderComplete -= new EventHandler (Handler);
  312. p.SaveStateComplete -= new EventHandler (Handler);
  313. }
  314. #endif
  315. // LinkDemand
  316. public override Type Type {
  317. get { return typeof (Page); }
  318. }
  319. }
  320. }