2 // IsolatedStorageFileStreamCas.cs - CAS unit tests for
3 // System.IO.IsolatedStorage.IsolatedStorageFileStream
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using NUnit.Framework;
34 using System.IO.IsolatedStorage;
35 using System.Reflection;
36 using System.Security;
37 using System.Security.Permissions;
38 using System.Threading;
41 using Microsoft.Win32.SafeHandles;
44 namespace MonoCasTests.System.IO.IsolatedStorageTest {
48 public class IsolatedStorageFileStreamCas {
50 private const int timeout = 30000;
51 private string message;
53 static ManualResetEvent reset;
56 public void FixtureSetUp ()
58 reset = new ManualResetEvent (false);
62 public void FixtureTearDown ()
70 if (!SecurityManager.SecurityEnabled)
71 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
75 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
76 [ExpectedException (typeof (SecurityException))]
77 public void DenyUnrestricted ()
79 IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("deny", FileMode.Create);
83 [IsolatedStorageFilePermission (SecurityAction.Deny, Unrestricted = true)]
84 [ExpectedException (typeof (SecurityException))]
85 public void DenyIsolatedStorageFilePermission ()
87 IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("deny", FileMode.Create);
90 private void Read (string filename)
92 byte[] buffer = new byte[8];
93 using (IsolatedStorageFileStream read = new IsolatedStorageFileStream (filename, FileMode.Open, FileAccess.Read)) {
94 Assert.AreEqual (8, read.Length, "Length");
95 Assert.AreEqual (0, read.Position, "Position");
96 Assert.IsTrue (read.CanRead, "read.CanRead");
97 Assert.IsTrue (read.CanSeek, "read.CanSeek");
98 Assert.IsFalse (read.CanWrite, "read.CanWrite");
99 Assert.IsFalse (read.IsAsync, "read.IsAync");
100 Assert.AreEqual (buffer.Length, read.ReadByte (), "ReadByte");
101 read.Seek (0, SeekOrigin.Begin);
102 Assert.AreEqual (buffer.Length, read.Read (buffer, 0, buffer.Length), "Read");
107 private void Write (string filename)
109 byte[] buffer = new byte[8];
110 using (IsolatedStorageFileStream write = new IsolatedStorageFileStream (filename, FileMode.Create, FileAccess.Write)) {
111 Assert.IsFalse (write.CanRead, "write.CanRead");
112 Assert.IsTrue (write.CanSeek, "write.CanSeek");
113 Assert.IsTrue (write.CanWrite, "write.CanWrite");
114 Assert.IsFalse (write.IsAsync, "write.IsAync");
115 write.Write (buffer, 0, buffer.Length);
117 write.WriteByte ((byte)buffer.Length);
125 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
126 [ExpectedException (typeof (FileNotFoundException))]
127 public void ReadUnexistingFile ()
129 string filename = "cas-doesnt-exists";
133 catch (FileNotFoundException fnfe) {
134 // check that we do not leak the full path to the missing file
135 // as we do not have the FileIOPermission's PathDiscovery rights
136 Assert.AreEqual (filename, fnfe.FileName, "FileName");
142 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
143 [ExpectedException (typeof (DirectoryNotFoundException))]
144 public void ReadInUnexistingDirectory ()
146 string filename = Path.Combine ("unexistingdir", "filename");
150 catch (DirectoryNotFoundException dnf) {
151 // check that we do not leak the full path to the missing file
152 // as we do not have the FileIOPermission's PathDiscovery rights
153 Assert.IsTrue (dnf.Message.IndexOf (filename) >= 0, "filename");
154 Assert.IsFalse (dnf.Message.IndexOf ("\\" + filename) >= 0, "fullpath");
160 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
161 [ExpectedException (typeof (UnauthorizedAccessException))]
162 public void ReadDirectoryAsFile ()
164 string dirname = "this-is-a-dir";
165 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
167 string[] dirs = isf.GetDirectoryNames (dirname);
168 if (dirs.Length == 0) {
169 isf.CreateDirectory (dirname);
173 catch (UnauthorizedAccessException uae) {
174 // check that we do not leak the full path to the missing file
175 // as we do not have the FileIOPermission's PathDiscovery rights
176 Assert.IsTrue (uae.Message.IndexOf (dirname) >= 0, "dirname");
177 Assert.IsFalse (uae.Message.IndexOf ("\\" + dirname) >= 0, "fullpath");
179 isf.DeleteDirectory (dirname);
181 catch (IsolatedStorageException) {
182 // this isn't where we want ot fail!
183 // and 1.x isn't always cooperative
190 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
191 public void ReWrite ()
193 Write ("cas-rewrite");
194 Write ("cas-rewrite");
198 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
199 public void WriteThenRead ()
206 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
207 [ExpectedException (typeof (DirectoryNotFoundException))]
208 public void WriteInUnexistingDirectory ()
210 string filename = Path.Combine ("unexistingdir", "filename");
214 catch (DirectoryNotFoundException dnf) {
215 // check that we do not leak the full path to the missing file
216 // as we do not have the FileIOPermission's PathDiscovery rights
217 Assert.IsTrue (dnf.Message.IndexOf (filename) >= 0, "filename");
218 Assert.IsFalse (dnf.Message.IndexOf ("\\" + filename) >= 0, "fullpath");
224 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
225 [ExpectedException (typeof (IsolatedStorageException))]
226 public void Handle ()
228 IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("cas-Handle", FileMode.Create);
229 IntPtr p = isfs.Handle;
230 // Note: The SecurityException for UnmanagedCode cannot be tested here because it's a LinkDemand
234 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
235 [ExpectedException (typeof (IsolatedStorageException))]
236 public void SafeFileHandle ()
238 IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("cas-SafeFileHandle", FileMode.Create);
239 SafeFileHandle sfh = isfs.SafeFileHandle;
240 // Note: The SecurityException for UnmanagedCode cannot be tested here because it's a LinkDemand
244 // we use reflection to call IsolatedStorageFileStream as the Handle and SafeFileHandle
245 // properties are protected by LinkDemand (which will be converted into full demand,
246 // i.e. a stack walk) when reflection is used (i.e. it gets testable).
249 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
250 [ExpectedException (typeof (SecurityException))]
251 public void Handle_UnmanagedCode ()
253 IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("cas-Handle-Unmanaged", FileMode.Create);
255 MethodInfo mi = typeof (IsolatedStorageFileStream).GetProperty ("Handle").GetGetMethod ();
256 mi.Invoke (isfs, null);
264 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
265 [ExpectedException (typeof (SecurityException))]
266 public void SafeFileHandle_UnmanagedCode ()
268 IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("cas-SafeFileHandle-Unmanaged", FileMode.Create);
270 MethodInfo mi = typeof (IsolatedStorageFileStream).GetProperty ("SafeFileHandle").GetGetMethod ();
271 mi.Invoke (isfs, null);
279 // async tests (for stack propagation)
281 private void ReadCallback (IAsyncResult ar)
283 IsolatedStorageFileStream s = (IsolatedStorageFileStream)ar.AsyncState;
286 // can we do something bad here ?
287 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
288 message = "Expected a SecurityException";
290 catch (SecurityException) {
294 catch (Exception e) {
295 message = e.ToString ();
300 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
301 public void AsyncRead ()
303 IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("cas-AsyncRead", FileMode.Create);
304 message = "AsyncRead";
306 IAsyncResult r = isfs.BeginRead (new byte[0], 0, 0, new AsyncCallback (ReadCallback), isfs);
307 Assert.IsNotNull (r, "IAsyncResult");
308 if (!reset.WaitOne (timeout, true))
309 Assert.Ignore ("Timeout");
310 Assert.IsNull (message, message);
314 private void WriteCallback (IAsyncResult ar)
316 IsolatedStorageFileStream s = (IsolatedStorageFileStream)ar.AsyncState;
319 // can we do something bad here ?
320 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
321 message = "Expected a SecurityException";
323 catch (SecurityException) {
327 catch (Exception e) {
328 message = e.ToString ();
333 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
334 public void AsyncWrite ()
336 IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("cas-AsyncWrite", FileMode.Create);
337 message = "AsyncWrite";
339 IAsyncResult r = isfs.BeginWrite (new byte[0], 0, 0, new AsyncCallback (WriteCallback), isfs);
340 Assert.IsNotNull (r, "IAsyncResult");
341 if (!reset.WaitOne (timeout, true))
342 Assert.Ignore ("Timeout");
343 Assert.IsNull (message, message);