2
0

SimpleWorkerRequestCas.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // SimpleWorkerRequestCas.cs
  3. // - CAS unit tests for System.Web.Hosting.SimpleWorkerRequest
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.IO;
  32. using System.Reflection;
  33. using System.Security;
  34. using System.Security.Permissions;
  35. using System.Web;
  36. using System.Web.Hosting;
  37. namespace MonoCasTests.System.Web.Hosting {
  38. [TestFixture]
  39. [Category ("CAS")]
  40. public class SimpleWorkerRequestCas : AspNetHostingMinimal {
  41. private StringWriter sw;
  42. private string cwd;
  43. private SimpleWorkerRequest swr;
  44. [TestFixtureSetUp]
  45. public void FixtureSetUp ()
  46. {
  47. // we're at full trust here
  48. sw = new StringWriter ();
  49. cwd = Environment.CurrentDirectory;
  50. swr = new SimpleWorkerRequest ("/", cwd, String.Empty, String.Empty, sw);
  51. }
  52. [Test]
  53. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  54. [ExpectedException (typeof (SecurityException))]
  55. public void Constructor3_Deny_UnmanagedCode ()
  56. {
  57. new SimpleWorkerRequest (null, null, null);
  58. }
  59. [Test]
  60. [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
  61. [Ignore ("I don't have a 'real' working case, inside NUnit, for this .ctor")]
  62. public void Constructor3_PermitOnly_UnmanagedCode ()
  63. {
  64. try {
  65. new SimpleWorkerRequest ("/", String.Empty, sw);
  66. }
  67. catch (NullReferenceException) {
  68. // we always seems to get a NRE from MS here (both 1.x and 2.0)
  69. }
  70. // note: on Mono a FileIOPermission is triggered later
  71. // in a call to HttpRuntime
  72. }
  73. [Test]
  74. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  75. [ExpectedException (typeof (SecurityException))]
  76. public void Constructor5_Deny_UnmanagedCode ()
  77. {
  78. new SimpleWorkerRequest (null, null, null, null, null);
  79. }
  80. [Test]
  81. [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
  82. public void Constructor5_PermitOnly_UnmanagedCode ()
  83. {
  84. new SimpleWorkerRequest (null, cwd, "/", String.Empty, sw);
  85. }
  86. [Test]
  87. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  88. public void Properties_Deny_Unrestricted ()
  89. {
  90. Assert.IsNull (swr.MachineConfigPath, "MachineConfigPath");
  91. Assert.IsNull (swr.MachineInstallDirectory, "MachineInstallDirectory");
  92. Assert.AreEqual ("/", swr.GetAppPath (), "GetAppPath");
  93. Assert.AreEqual ("/", swr.GetFilePath (), "GetFilePath");
  94. Assert.AreEqual ("GET", swr.GetHttpVerbName (), "GetHttpVerbName");
  95. Assert.AreEqual ("HTTP/1.0", swr.GetHttpVersion (), "GetHttpVersion");
  96. Assert.AreEqual ("127.0.0.1", swr.GetLocalAddress (), "GetLocalAddress");
  97. Assert.AreEqual (80, swr.GetLocalPort (), "GetLocalPort");
  98. Assert.AreEqual (String.Empty, swr.GetPathInfo (), "GetPathInfo");
  99. Assert.AreEqual (String.Empty, swr.GetQueryString (), "GetQueryString");
  100. Assert.AreEqual ("/", swr.GetRawUrl (), "GetRawUrl");
  101. Assert.AreEqual ("127.0.0.1", swr.GetRemoteAddress (), "GetRemoteAddress");
  102. Assert.AreEqual (0, swr.GetRemotePort (), "GetRemotePort");
  103. Assert.AreEqual (String.Empty, swr.GetServerVariable ("mono"), "GetServerVariable");
  104. Assert.IsNotNull (swr.GetUriPath (), "GetUriPath");
  105. Assert.AreEqual (IntPtr.Zero, swr.GetUserToken (), "GetUserToken");
  106. Assert.IsNull (swr.MapPath ("/"), "MapPath");
  107. }
  108. [Test]
  109. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  110. public void Methods_Deny_Unrestricted ()
  111. {
  112. swr.EndOfRequest ();
  113. swr.FlushResponse (true);
  114. swr.SendKnownResponseHeader (0, String.Empty);
  115. swr.SendResponseFromFile (IntPtr.Zero, 0, 0);
  116. swr.SendResponseFromFile (String.Empty, 0, 0);
  117. swr.SendResponseFromMemory (new byte[0], 0);
  118. swr.SendStatus (0, "hello?");
  119. swr.SendUnknownResponseHeader ("mono", "monkey");
  120. }
  121. [Test]
  122. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  123. [ExpectedException (typeof (SecurityException))]
  124. public void GetAppPathTranslated_Deny_FileIOPermission ()
  125. {
  126. // path discovery
  127. swr.GetAppPathTranslated ();
  128. }
  129. [Test]
  130. [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  131. public void GetAppPathTranslated_PermitOnly_FileIOPermission ()
  132. {
  133. Assert.IsNotNull (swr.GetAppPathTranslated (), "GetAppPathTranslated");
  134. }
  135. [Test]
  136. [FileIOPermission (SecurityAction.Deny, Unrestricted = true)]
  137. [ExpectedException (typeof (SecurityException))]
  138. public void GetFilePathTranslated_Deny_FileIOPermission ()
  139. {
  140. // path discovery
  141. swr.GetFilePathTranslated ();
  142. }
  143. [Test]
  144. [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  145. public void GetFilePathTranslated_PermitOnly_FileIOPermission ()
  146. {
  147. Assert.IsNotNull (swr.GetFilePathTranslated (), "GetFilePathTranslated");
  148. }
  149. // LinkDemand
  150. [SecurityPermission (SecurityAction.Assert, UnmanagedCode = true)]
  151. public override object CreateControl (SecurityAction action, AspNetHostingPermissionLevel level)
  152. {
  153. ConstructorInfo ci = this.Type.GetConstructor (new Type[5] { typeof (string), typeof (string), typeof (string), typeof (string), typeof (TextWriter) });
  154. Assert.IsNotNull (ci, ".ctor(string,string,TextWriter)");
  155. return ci.Invoke (new object[5] { null, cwd, "/", String.Empty, sw });
  156. }
  157. public override Type Type {
  158. get { return typeof (SimpleWorkerRequest); }
  159. }
  160. }
  161. }