2 // FileStreamCas.cs -CAS unit tests for System.IO.FileStream
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using NUnit.Framework;
33 using System.Reflection;
34 using System.Security;
35 using System.Security.Permissions;
36 using System.Threading;
38 namespace MonoCasTests.System.IO {
42 public class FileStreamCas {
44 private MonoTests.System.IO.FileStreamTest fst;
45 private const int timeout = 30000;
46 private string message;
47 private string readfile;
48 private string writefile;
50 static ManualResetEvent reset;
53 public void FixtureSetUp ()
55 // this occurs with a "clean" stack (full trust)
56 fst = new MonoTests.System.IO.FileStreamTest ();
57 reset = new ManualResetEvent (false);
58 readfile = Path.GetTempFileName ();
59 writefile = Path.GetTempFileName ();
63 public void FixtureTearDown ()
66 if (File.Exists (readfile))
67 File.Delete (readfile);
68 if (File.Exists (writefile))
69 File.Delete (writefile);
75 if (!SecurityManager.SecurityEnabled)
76 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
79 // Partial Trust Tests - i.e. call "normal" unit with reduced privileges
82 [ExpectedException (typeof (SecurityException))]
83 public void PartialTrust_DenyUnrestricted_Failure ()
86 // SetUp/TearDown requires FileIOPermission
88 // so does the call but that's the test ;-)
96 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
97 private void CallRestricted (MonoTests.System.IO.FileStreamTest fst)
99 fst.TestDefaultProperties ();
103 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
104 public void PartialTrust_PermitOnly_FileIOPermission_Success ()
108 fst.CtorAccess1Read2Read ();
112 fst.TestDefaultProperties ();
117 fst.PositionAfterSetLength ();
118 fst.ReadBytePastEndOfStream ();
123 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
124 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
125 public void PartialTrust_PermitOnly_FileIOPermissionUnmanagedCode_Success ()
128 fst.TestFlushNotOwningHandle ();
132 // test Demand by denying the required permissions
135 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
136 [ExpectedException (typeof (SecurityException))]
137 public void Ctor_IntPtrFileAccess ()
139 new FileStream (IntPtr.Zero, FileAccess.Read);
143 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
144 [ExpectedException (typeof (SecurityException))]
145 public void Ctor_IntPtrFileAccessBool ()
147 new FileStream (IntPtr.Zero, FileAccess.Read, false);
151 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
152 [ExpectedException (typeof (SecurityException))]
153 public void Ctor_IntPtrFileAccessBoolInt ()
155 new FileStream (IntPtr.Zero, FileAccess.Read, false, 0);
159 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
160 [ExpectedException (typeof (SecurityException))]
161 public void Ctor_IntPtrFileAccessBoolIntBool ()
163 new FileStream (IntPtr.Zero, FileAccess.Read, false, 0, false);
166 // we use reflection to call FileStream as the Handle property is protected
167 // by a LinkDemand (which will be converted into full demand, i.e. a stack
168 // walk) when reflection is used (i.e. it gets testable).
171 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
172 [ExpectedException (typeof (SecurityException))]
173 public void Handle ()
175 FileStream fs = File.OpenWrite (Path.GetTempFileName ());
177 MethodInfo mi = typeof (FileStream).GetProperty ("Handle").GetGetMethod ();
178 mi.Invoke (fs, null);
185 // async tests (for stack propagation)
187 private void ReadCallback (IAsyncResult ar)
189 FileStream s = (FileStream)ar.AsyncState;
192 // can we do something bad here ?
193 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
194 message = "Expected a SecurityException";
196 catch (SecurityException) {
200 catch (Exception e) {
201 message = e.ToString ();
206 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
207 public void AsyncRead ()
209 FileStream fs = new FileStream (readfile, FileMode.OpenOrCreate);
210 message = "AsyncRead";
212 IAsyncResult r = fs.BeginRead (new byte[0], 0, 0, new AsyncCallback (ReadCallback), fs);
213 Assert.IsNotNull (r, "IAsyncResult");
214 if (!reset.WaitOne (timeout, true))
215 Assert.Ignore ("Timeout");
216 Assert.IsNull (message, message);
220 private void WriteCallback (IAsyncResult ar)
222 FileStream s = (FileStream)ar.AsyncState;
225 // can we do something bad here ?
226 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
227 message = "Expected a SecurityException";
229 catch (SecurityException) {
233 catch (Exception e) {
234 message = e.ToString ();
239 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
240 public void AsyncWrite ()
242 FileStream fs = new FileStream (writefile, FileMode.OpenOrCreate);
243 message = "AsyncWrite";
245 IAsyncResult r = fs.BeginWrite (new byte[0], 0, 0, new AsyncCallback (WriteCallback), fs);
246 Assert.IsNotNull (r, "IAsyncResult");
247 if (!reset.WaitOne (timeout, true))
248 Assert.Ignore ("Timeout");
249 Assert.IsNull (message, message);