FileStreamCas.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // FileStreamCas.cs -CAS unit tests for System.IO.FileStream
  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.Reflection;
  32. using System.Security;
  33. using System.Security.Permissions;
  34. using System.Threading;
  35. namespace MonoCasTests.System.IO {
  36. [TestFixture]
  37. [Category ("CAS")]
  38. public class FileStreamCas {
  39. private MonoTests.System.IO.FileStreamTest fst;
  40. private const int timeout = 30000;
  41. private string message;
  42. private string readfile;
  43. private string writefile;
  44. static ManualResetEvent reset;
  45. [TestFixtureSetUp]
  46. public void FixtureSetUp ()
  47. {
  48. // this occurs with a "clean" stack (full trust)
  49. fst = new MonoTests.System.IO.FileStreamTest ();
  50. reset = new ManualResetEvent (false);
  51. readfile = Path.GetTempFileName ();
  52. writefile = Path.GetTempFileName ();
  53. }
  54. [TestFixtureTearDown]
  55. public void FixtureTearDown ()
  56. {
  57. reset.Close ();
  58. if (File.Exists (readfile))
  59. File.Delete (readfile);
  60. if (File.Exists (writefile))
  61. File.Delete (writefile);
  62. }
  63. [SetUp]
  64. public void SetUp ()
  65. {
  66. if (!SecurityManager.SecurityEnabled)
  67. Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
  68. }
  69. // Partial Trust Tests - i.e. call "normal" unit with reduced privileges
  70. [Test]
  71. [ExpectedException (typeof (SecurityException))]
  72. public void PartialTrust_DenyUnrestricted_Failure ()
  73. {
  74. try {
  75. // SetUp/TearDown requires FileIOPermission
  76. fst.SetUp ();
  77. // so does the call but that's the test ;-)
  78. CallRestricted (fst);
  79. }
  80. finally {
  81. fst.TearDown ();
  82. }
  83. }
  84. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  85. private void CallRestricted (MonoTests.System.IO.FileStreamTest fst)
  86. {
  87. fst.TestDefaultProperties ();
  88. }
  89. [Test]
  90. [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  91. public void PartialTrust_PermitOnly_FileIOPermission_Success ()
  92. {
  93. fst.SetUp ();
  94. fst.TestCtr ();
  95. fst.CtorAccess1Read2Read ();
  96. fst.Write ();
  97. fst.Length ();
  98. fst.Flush ();
  99. fst.TestDefaultProperties ();
  100. fst.TestLock ();
  101. fst.Seek ();
  102. fst.TestSeek ();
  103. fst.TestClose ();
  104. fst.PositionAfterSetLength ();
  105. fst.ReadBytePastEndOfStream ();
  106. fst.TearDown ();
  107. }
  108. [Test]
  109. [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
  110. [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
  111. public void PartialTrust_PermitOnly_FileIOPermissionUnmanagedCode_Success ()
  112. {
  113. fst.SetUp ();
  114. fst.TestFlushNotOwningHandle ();
  115. fst.TearDown ();
  116. }
  117. // test Demand by denying the required permissions
  118. [Test]
  119. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  120. [ExpectedException (typeof (SecurityException))]
  121. public void Ctor_IntPtrFileAccess ()
  122. {
  123. new FileStream (IntPtr.Zero, FileAccess.Read);
  124. }
  125. [Test]
  126. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  127. [ExpectedException (typeof (SecurityException))]
  128. public void Ctor_IntPtrFileAccessBool ()
  129. {
  130. new FileStream (IntPtr.Zero, FileAccess.Read, false);
  131. }
  132. [Test]
  133. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  134. [ExpectedException (typeof (SecurityException))]
  135. public void Ctor_IntPtrFileAccessBoolInt ()
  136. {
  137. new FileStream (IntPtr.Zero, FileAccess.Read, false, 0);
  138. }
  139. [Test]
  140. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  141. [ExpectedException (typeof (SecurityException))]
  142. public void Ctor_IntPtrFileAccessBoolIntBool ()
  143. {
  144. new FileStream (IntPtr.Zero, FileAccess.Read, false, 0, false);
  145. }
  146. // we use reflection to call FileStream as the Handle property is protected
  147. // by a LinkDemand (which will be converted into full demand, i.e. a stack
  148. // walk) when reflection is used (i.e. it gets testable).
  149. [Test]
  150. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  151. [ExpectedException (typeof (SecurityException))]
  152. public void Handle ()
  153. {
  154. FileStream fs = File.OpenWrite (Path.GetTempFileName ());
  155. try {
  156. MethodInfo mi = typeof (FileStream).GetProperty ("Handle").GetGetMethod ();
  157. mi.Invoke (fs, null);
  158. }
  159. finally {
  160. fs.Close ();
  161. }
  162. }
  163. // async tests (for stack propagation)
  164. private void ReadCallback (IAsyncResult ar)
  165. {
  166. FileStream s = (FileStream)ar.AsyncState;
  167. s.EndRead (ar);
  168. try {
  169. // can we do something bad here ?
  170. Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
  171. message = "Expected a SecurityException";
  172. }
  173. catch (SecurityException) {
  174. message = null;
  175. reset.Set ();
  176. }
  177. catch (Exception e) {
  178. message = e.ToString ();
  179. }
  180. }
  181. [Test]
  182. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  183. public void AsyncRead ()
  184. {
  185. FileStream fs = new FileStream (readfile, FileMode.OpenOrCreate);
  186. message = "AsyncRead";
  187. reset.Reset ();
  188. IAsyncResult r = fs.BeginRead (new byte[0], 0, 0, new AsyncCallback (ReadCallback), fs);
  189. Assert.IsNotNull (r, "IAsyncResult");
  190. if (!reset.WaitOne (timeout, true))
  191. Assert.Ignore ("Timeout");
  192. Assert.IsNull (message, message);
  193. fs.Close ();
  194. }
  195. private void WriteCallback (IAsyncResult ar)
  196. {
  197. FileStream s = (FileStream)ar.AsyncState;
  198. s.EndWrite (ar);
  199. try {
  200. // can we do something bad here ?
  201. Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
  202. message = "Expected a SecurityException";
  203. }
  204. catch (SecurityException) {
  205. message = null;
  206. reset.Set ();
  207. }
  208. catch (Exception e) {
  209. message = e.ToString ();
  210. }
  211. }
  212. [Test]
  213. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  214. public void AsyncWrite ()
  215. {
  216. FileStream fs = new FileStream (writefile, FileMode.OpenOrCreate);
  217. message = "AsyncWrite";
  218. reset.Reset ();
  219. IAsyncResult r = fs.BeginWrite (new byte[0], 0, 0, new AsyncCallback (WriteCallback), fs);
  220. Assert.IsNotNull (r, "IAsyncResult");
  221. if (!reset.WaitOne (timeout, true))
  222. Assert.Ignore ("Timeout");
  223. Assert.IsNull (message, message);
  224. fs.Close ();
  225. }
  226. }
  227. }