HttpResponseCas.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. //
  2. // HttpResponseCas.cs - CAS unit tests for System.Web.HttpResponse
  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.Collections;
  31. using System.IO;
  32. using System.Reflection;
  33. using System.Security;
  34. using System.Security.Permissions;
  35. using System.Text;
  36. using System.Web;
  37. using System.Web.Caching;
  38. namespace MonoCasTests.System.Web {
  39. [TestFixture]
  40. [Category ("CAS")]
  41. public class HttpResponseCas : AspNetHostingMinimal {
  42. private StringWriter writer;
  43. private String fname;
  44. private FileStream fs;
  45. private IntPtr handle;
  46. [TestFixtureSetUp]
  47. public void FixtureSetUp ()
  48. {
  49. // running at full-trust
  50. writer = new StringWriter ();
  51. }
  52. [SetUp]
  53. public override void SetUp ()
  54. {
  55. // running at full-trust too
  56. base.SetUp ();
  57. fname = Path.GetTempFileName ();
  58. fs = new FileStream (fname, FileMode.Open, FileAccess.Read);
  59. handle = fs.Handle;
  60. }
  61. [TearDown]
  62. public void TearDown ()
  63. {
  64. try {
  65. if (fs != null)
  66. fs.Close ();
  67. handle = IntPtr.Zero;
  68. if (File.Exists (fname))
  69. File.Delete (fname);
  70. }
  71. catch {
  72. }
  73. }
  74. [Test]
  75. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  76. public void Properties_Deny_Unrestricted ()
  77. {
  78. HttpResponse response = new HttpResponse (writer);
  79. response.Buffer = false;
  80. Assert.IsFalse (response.Buffer, "Buffer");
  81. response.BufferOutput = false;
  82. Assert.IsFalse (response.BufferOutput, "BufferOutput");
  83. Assert.IsNotNull (response.Cache, "Cache");
  84. response.CacheControl = "public";
  85. Assert.AreEqual ("public", response.CacheControl, "CacheControl");
  86. response.ContentEncoding = Encoding.UTF8;
  87. Assert.AreEqual (Encoding.UTF8, response.ContentEncoding, "ContentEncoding");
  88. response.ContentType = String.Empty;
  89. Assert.AreEqual (String.Empty, response.ContentType, "ContentType");
  90. response.Charset = Encoding.UTF8.WebName;
  91. Assert.AreEqual (Encoding.UTF8.WebName, response.Charset, "Charset");
  92. Assert.IsNotNull (response.Cookies, "Cookies");
  93. try {
  94. response.Expires = 2;
  95. }
  96. catch (NullReferenceException) {
  97. // ms
  98. }
  99. Assert.IsTrue (response.Expires > 0, "Expires");
  100. response.ExpiresAbsolute = DateTime.MinValue;
  101. Assert.AreEqual (DateTime.MinValue, response.ExpiresAbsolute, "ExpiresAbsolute");
  102. Assert.IsTrue (response.IsClientConnected, "IsClientConnected");
  103. Assert.IsNotNull (response.Output, "Ouput");
  104. response.RedirectLocation = String.Empty;
  105. Assert.AreEqual (String.Empty, response.RedirectLocation, "RedirectLocation");
  106. response.Status = "501 Not Ok";
  107. Assert.AreEqual ("501 Not Ok", response.Status, "Status");
  108. response.StatusCode = 501;
  109. Assert.AreEqual (501, response.StatusCode, "StatusCode");
  110. response.StatusDescription = "Not Ok";
  111. Assert.AreEqual ("Not Ok", response.StatusDescription, "StatusDescription");
  112. response.SuppressContent = false;
  113. Assert.IsFalse (response.SuppressContent, "SuppressContent");
  114. #if NET_2_0
  115. response.HeaderEncoding = Encoding.UTF8;
  116. Assert.AreEqual (Encoding.UTF8, response.HeaderEncoding, "HeaderEncoding");
  117. Assert.IsFalse (response.IsRequestBeingRedirected, "IsRequestBeingRedirected");
  118. #endif
  119. }
  120. [Test]
  121. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  122. #if ONLY_1_1
  123. [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
  124. #endif
  125. public void Filter_Deny_Unrestricted ()
  126. {
  127. HttpResponse response = new HttpResponse (writer);
  128. try {
  129. response.Filter = new MemoryStream ();
  130. }
  131. catch (HttpException) {
  132. // ms
  133. }
  134. Assert.IsNull (response.Filter, "Filter");
  135. }
  136. [Test]
  137. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  138. #if ONLY_1_1
  139. [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
  140. #endif
  141. public void OutputStream_Deny_Unrestricted ()
  142. {
  143. HttpResponse response = new HttpResponse (writer);
  144. try {
  145. Assert.IsNotNull (response.OutputStream, "OutputStream");
  146. }
  147. catch (HttpException) {
  148. // ms 2.0
  149. }
  150. }
  151. private string Callback (HttpContext context)
  152. {
  153. return string.Empty;
  154. }
  155. [Test]
  156. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  157. public void Methods_Deny_Unrestricted ()
  158. {
  159. HttpResponse response = new HttpResponse (writer);
  160. response.AddCacheItemDependencies (new ArrayList ());
  161. response.AddCacheItemDependency (String.Empty);
  162. response.AddFileDependencies (new ArrayList ());
  163. response.AddFileDependency (fname);
  164. #if NET_2_0
  165. response.AddCacheDependency (new CacheDependency[0]);
  166. response.AddCacheItemDependencies (new string [0]);
  167. response.AddFileDependencies (new string [0]);
  168. #endif
  169. try {
  170. response.AppendCookie (new HttpCookie ("mono"));
  171. }
  172. catch (NullReferenceException) {
  173. // ms
  174. }
  175. try {
  176. Assert.IsNull (response.ApplyAppPathModifier (null), "ApplyAppPathModifier");
  177. }
  178. catch (NullReferenceException) {
  179. // ms
  180. }
  181. try {
  182. response.Clear ();
  183. }
  184. catch (NullReferenceException) {
  185. // ms
  186. }
  187. try {
  188. response.ClearContent ();
  189. }
  190. catch (NullReferenceException) {
  191. // ms
  192. }
  193. try {
  194. response.ClearHeaders ();
  195. }
  196. catch (NullReferenceException) {
  197. // ms
  198. }
  199. try {
  200. response.Redirect ("http://www.mono-project.com");
  201. }
  202. catch (NullReferenceException) {
  203. // ms
  204. }
  205. try {
  206. response.Redirect ("http://www.mono-project.com", false);
  207. }
  208. catch (NullReferenceException) {
  209. // ms
  210. }
  211. try {
  212. response.SetCookie (new HttpCookie ("mono"));
  213. }
  214. catch (NullReferenceException) {
  215. // ms
  216. }
  217. response.Write (String.Empty);
  218. response.Write (Char.MinValue);
  219. response.Write (new char[0], 0, 0);
  220. response.Write (this);
  221. #if NET_2_0
  222. response.WriteSubstitution (new HttpResponseSubstitutionCallback (Callback));
  223. #endif
  224. response.Flush ();
  225. response.Close ();
  226. try {
  227. response.End ();
  228. }
  229. catch (NullReferenceException) {
  230. // ms
  231. }
  232. }
  233. [Test]
  234. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  235. #if ONLY_1_1
  236. [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
  237. #endif
  238. public void AppendHeader_Deny_Unrestricted ()
  239. {
  240. HttpResponse response = new HttpResponse (writer);
  241. response.AppendHeader ("monkey", "mono");
  242. }
  243. [Test]
  244. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  245. #if ONLY_1_1
  246. [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
  247. #endif
  248. public void AddHeader_Deny_Unrestricted ()
  249. {
  250. HttpResponse response = new HttpResponse (writer);
  251. try {
  252. response.AddHeader (String.Empty, String.Empty);
  253. }
  254. catch (HttpException) {
  255. // ms 2.0
  256. }
  257. }
  258. [Test]
  259. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  260. #if ONLY_1_1
  261. [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
  262. #endif
  263. public void BinaryWrite_Deny_Unrestricted ()
  264. {
  265. HttpResponse response = new HttpResponse (writer);
  266. try {
  267. response.BinaryWrite (new byte[0]);
  268. }
  269. catch (HttpException) {
  270. // ms
  271. }
  272. }
  273. [Test]
  274. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  275. #if ONLY_1_1
  276. [Category ("NotDotNet")] // triggers a TypeInitializationException in HttpRuntime
  277. #endif
  278. public void Pics_Deny_Unrestricted ()
  279. {
  280. HttpResponse response = new HttpResponse (writer);
  281. try {
  282. response.Pics (String.Empty);
  283. }
  284. catch (HttpException) {
  285. // ms
  286. }
  287. }
  288. [Test]
  289. [AspNetHostingPermission (SecurityAction.Deny, Level = AspNetHostingPermissionLevel.Medium)]
  290. [ExpectedException (typeof (SecurityException))]
  291. public void AppendToLog_Deny_Medium ()
  292. {
  293. HttpResponse response = new HttpResponse (writer);
  294. response.AppendToLog ("mono");
  295. }
  296. [Test]
  297. [AspNetHostingPermission (SecurityAction.PermitOnly, Level = AspNetHostingPermissionLevel.Medium)]
  298. public void AppendToLog_PermitOnly_Medium ()
  299. {
  300. HttpResponse response = new HttpResponse (writer);
  301. response.AppendToLog ("mono");
  302. }
  303. [Test]
  304. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  305. [ExpectedException (typeof (SecurityException))]
  306. public void TransmitFile_Deny_FileIOPermission ()
  307. {
  308. HttpResponse response = new HttpResponse (writer);
  309. response.TransmitFile (fname);
  310. }
  311. [Test]
  312. [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  313. public void TransmitFile_PermitOnly_FileIOPermission ()
  314. {
  315. HttpResponse response = new HttpResponse (writer);
  316. response.TransmitFile (fname);
  317. }
  318. [Test]
  319. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  320. [ExpectedException (typeof (SecurityException))]
  321. public void WriteFile_String_Deny_FileIOPermission ()
  322. {
  323. HttpResponse response = new HttpResponse (writer);
  324. response.WriteFile (fname);
  325. }
  326. [Test]
  327. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  328. [ExpectedException (typeof (SecurityException))]
  329. public void WriteFile_StringBool_Deny_FileIOPermission ()
  330. {
  331. HttpResponse response = new HttpResponse (writer);
  332. response.WriteFile (fname, false);
  333. }
  334. [Test]
  335. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  336. [ExpectedException (typeof (SecurityException))]
  337. public void WriteFile_StringIntInt_Deny_FileIOPermission ()
  338. {
  339. HttpResponse response = new HttpResponse (writer);
  340. response.WriteFile (fname, 0, 1);
  341. }
  342. [Test]
  343. [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  344. public void WriteFile_PermitOnly_FileIOPermission ()
  345. {
  346. HttpResponse response = new HttpResponse (writer);
  347. response.WriteFile (fname);
  348. response.WriteFile (fname, false);
  349. response.WriteFile (fname, 0, 0);
  350. }
  351. [Test]
  352. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  353. [ExpectedException (typeof (SecurityException))]
  354. public void WriteFile_Deny_UnmanagedCode ()
  355. {
  356. HttpResponse response = new HttpResponse (writer);
  357. response.WriteFile (handle, 0, 1);
  358. }
  359. [Test]
  360. [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
  361. public void WriteFile_PermitOnly_UnmanagedCode ()
  362. {
  363. HttpResponse response = new HttpResponse (writer);
  364. response.WriteFile (handle, 0, 1);
  365. }
  366. // LinkDemand
  367. public override object CreateControl (SecurityAction action, AspNetHostingPermissionLevel level)
  368. {
  369. ConstructorInfo ci = this.Type.GetConstructor (new Type[1] { typeof (TextWriter) });
  370. Assert.IsNotNull (ci, ".ctor(TextWriter)");
  371. return ci.Invoke (new object[1] { writer });
  372. }
  373. public override Type Type {
  374. get { return typeof (HttpResponse); }
  375. }
  376. }
  377. }