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;
40 using Microsoft.Win32.SafeHandles;
43 namespace MonoCasTests.System.IO.IsolatedStorage {
47 public class IsolatedStorageFileStreamCas {
52 if (!SecurityManager.SecurityEnabled)
53 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
57 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
58 [ExpectedException (typeof (SecurityException))]
59 public void DenyUnrestricted ()
61 IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("deny", FileMode.Create);
65 [IsolatedStorageFilePermission (SecurityAction.Deny, Unrestricted = true)]
66 [ExpectedException (typeof (SecurityException))]
67 public void DenyIsolatedStorageFilePermission ()
69 IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("deny", FileMode.Create);
72 private void Read (string filename)
74 byte[] buffer = new byte[8];
75 using (IsolatedStorageFileStream read = new IsolatedStorageFileStream (filename, FileMode.Open, FileAccess.Read)) {
76 Assert.AreEqual (8, read.Length, "Length");
77 Assert.AreEqual (0, read.Position, "Position");
78 Assert.IsTrue (read.CanRead, "read.CanRead");
79 Assert.IsTrue (read.CanSeek, "read.CanSeek");
80 Assert.IsFalse (read.CanWrite, "read.CanWrite");
81 Assert.IsFalse (read.IsAsync, "read.IsAync");
82 Assert.AreEqual (buffer.Length, read.ReadByte (), "ReadByte");
83 read.Seek (0, SeekOrigin.Begin);
84 Assert.AreEqual (buffer.Length, read.Read (buffer, 0, buffer.Length), "Read");
89 private void Write (string filename)
91 byte[] buffer = new byte[8];
92 using (IsolatedStorageFileStream write = new IsolatedStorageFileStream (filename, FileMode.Create, FileAccess.Write)) {
93 Assert.IsFalse (write.CanRead, "write.CanRead");
94 Assert.IsTrue (write.CanSeek, "write.CanSeek");
95 Assert.IsTrue (write.CanWrite, "write.CanWrite");
96 Assert.IsFalse (write.IsAsync, "write.IsAync");
97 write.Write (buffer, 0, buffer.Length);
99 write.WriteByte ((byte)buffer.Length);
107 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
108 [ExpectedException (typeof (FileNotFoundException))]
109 public void ReadUnexistingFile ()
111 string filename = "cas-doesnt-exists";
115 catch (FileNotFoundException fnfe) {
116 // check that we do not leak the full path to the missing file
117 // as we do not have the FileIOPermission's PathDiscovery rights
118 Assert.AreEqual (filename, fnfe.FileName, "FileName");
124 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
125 [ExpectedException (typeof (DirectoryNotFoundException))]
126 public void ReadInUnexistingDirectory ()
128 string filename = Path.Combine ("unexistingdir", "filename");
132 catch (DirectoryNotFoundException dnf) {
133 // check that we do not leak the full path to the missing file
134 // as we do not have the FileIOPermission's PathDiscovery rights
135 Assert.IsTrue (dnf.Message.IndexOf (filename) >= 0, "filename");
136 Assert.IsFalse (dnf.Message.IndexOf ("\\" + filename) >= 0, "fullpath");
142 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
143 [ExpectedException (typeof (UnauthorizedAccessException))]
144 public void ReadDirectoryAsFile ()
146 string dirname = "this-is-a-dir";
147 IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
149 string[] dirs = isf.GetDirectoryNames (dirname);
150 if (dirs.Length == 0) {
151 isf.CreateDirectory (dirname);
155 catch (UnauthorizedAccessException uae) {
156 // check that we do not leak the full path to the missing file
157 // as we do not have the FileIOPermission's PathDiscovery rights
158 Assert.IsTrue (uae.Message.IndexOf (dirname) >= 0, "dirname");
159 Assert.IsFalse (uae.Message.IndexOf ("\\" + dirname) >= 0, "fullpath");
161 isf.DeleteDirectory (dirname);
163 catch (IsolatedStorageException) {
164 // this isn't where we want ot fail!
165 // and 1.x isn't always cooperative
172 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
173 public void ReWrite ()
175 Write ("cas-rewrite");
176 Write ("cas-rewrite");
180 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
181 public void WriteThenRead ()
188 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
189 [ExpectedException (typeof (DirectoryNotFoundException))]
190 public void WriteInUnexistingDirectory ()
192 string filename = Path.Combine ("unexistingdir", "filename");
196 catch (DirectoryNotFoundException dnf) {
197 // check that we do not leak the full path to the missing file
198 // as we do not have the FileIOPermission's PathDiscovery rights
199 Assert.IsTrue (dnf.Message.IndexOf (filename) >= 0, "filename");
200 Assert.IsFalse (dnf.Message.IndexOf ("\\" + filename) >= 0, "fullpath");
206 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
207 [ExpectedException (typeof (IsolatedStorageException))]
208 public void Handle ()
210 IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("cas-Handle", FileMode.Create);
211 IntPtr p = isfs.Handle;
212 // Note: The SecurityException for UnmanagedCode cannot be tested here because it's a LinkDemand
216 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
217 [ExpectedException (typeof (IsolatedStorageException))]
218 public void SafeFileHandle ()
220 IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("cas-SafeFileHandle", FileMode.Create);
221 SafeFileHandle sfh = isfs.SafeFileHandle;
222 // Note: The SecurityException for UnmanagedCode cannot be tested here because it's a LinkDemand
226 // we use reflection to call IsolatedStorageFileStream as the Handle and SafeFileHandle
227 // properties are protected by LinkDemand (which will be converted into full demand,
228 // i.e. a stack walk) when reflection is used (i.e. it gets testable).
231 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
232 [ExpectedException (typeof (SecurityException))]
233 public void Handle_UnmanagedCode ()
235 IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("cas-Handle-Unmanaged", FileMode.Create);
237 MethodInfo mi = typeof (IsolatedStorageFileStream).GetProperty ("Handle").GetGetMethod ();
238 mi.Invoke (isfs, null);
246 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
247 [ExpectedException (typeof (SecurityException))]
248 public void SafeFileHandle_UnmanagedCode ()
250 IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("cas-SafeFileHandle-Unmanaged", FileMode.Create);
252 MethodInfo mi = typeof (IsolatedStorageFileStream).GetProperty ("SafeFileHandle").GetGetMethod ();
253 mi.Invoke (isfs, null);