2005-05-16 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / Test / System.IO.IsolatedStorage / IsolatedStorageFileStreamCas.cs
1 //
2 // IsolatedStorageFileStreamCas.cs - CAS unit tests for 
3 //      System.IO.IsolatedStorage.IsolatedStorageFileStream
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
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
30 using NUnit.Framework;
31
32 using System;
33 using System.IO;
34 using System.IO.IsolatedStorage;
35 using System.Reflection;
36 using System.Security;
37 using System.Security.Permissions;
38
39 #if NET_2_0
40 using Microsoft.Win32.SafeHandles;
41 #endif
42
43 namespace MonoCasTests.System.IO.IsolatedStorage {
44
45         [TestFixture]
46         [Category ("CAS")]
47         public class IsolatedStorageFileStreamCas {
48
49                 [SetUp]
50                 public void SetUp ()
51                 {
52                         if (!SecurityManager.SecurityEnabled)
53                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
54                 }
55
56                 [Test]
57                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
58                 [ExpectedException (typeof (SecurityException))]
59                 public void DenyUnrestricted ()
60                 {
61                         IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("deny", FileMode.Create);
62                 }
63
64                 [Test]
65                 [IsolatedStorageFilePermission (SecurityAction.Deny, Unrestricted = true)]
66                 [ExpectedException (typeof (SecurityException))]
67                 public void DenyIsolatedStorageFilePermission ()
68                 {
69                         IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("deny", FileMode.Create);
70                 }
71
72                 private void Read (string filename)
73                 {
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");
85                                 read.Close ();
86                         }
87                 }
88
89                 private void Write (string filename)
90                 {
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);
98                                 write.Position = 0;
99                                 write.WriteByte ((byte)buffer.Length);
100                                 write.SetLength (8);
101                                 write.Flush ();
102                                 write.Close ();
103                         }
104                 }
105
106                 [Test]
107                 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
108                 [ExpectedException (typeof (FileNotFoundException))]
109                 public void ReadUnexistingFile ()
110                 {
111                         string filename = "cas-doesnt-exists";
112                         try {
113                                 Read (filename);
114                         }
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");
119                                 throw;
120                         }
121                 }
122
123                 [Test]
124                 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
125                 [ExpectedException (typeof (DirectoryNotFoundException))]
126                 public void ReadInUnexistingDirectory ()
127                 {
128                         string filename = Path.Combine ("unexistingdir", "filename");
129                         try {
130                                 Read (filename);
131                         }
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");
137                                 throw;
138                         }
139                 }
140
141                 [Test]
142                 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
143                 [ExpectedException (typeof (UnauthorizedAccessException))]
144                 public void ReadDirectoryAsFile ()
145                 {
146                         string dirname = "this-is-a-dir";
147                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForDomain ();
148                         try {
149                                 string[] dirs = isf.GetDirectoryNames (dirname);
150                                 if (dirs.Length == 0) {
151                                         isf.CreateDirectory (dirname);
152                                 }
153                                 Read (dirname);
154                         }
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");
160                                 try {
161                                         isf.DeleteDirectory (dirname);
162                                 }
163                                 catch (IsolatedStorageException) {
164                                         // this isn't where we want ot fail!
165                                         // and 1.x isn't always cooperative
166                                 }
167                                 throw;
168                         }
169                 }
170
171                 [Test]
172                 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
173                 public void ReWrite ()
174                 {
175                         Write ("cas-rewrite");
176                         Write ("cas-rewrite");
177                 }
178
179                 [Test]
180                 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
181                 public void WriteThenRead ()
182                 {
183                         Write ("cas-rw");
184                         Read ("cas-rw");
185                 }
186
187                 [Test]
188                 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
189                 [ExpectedException (typeof (DirectoryNotFoundException))]
190                 public void WriteInUnexistingDirectory ()
191                 {
192                         string filename = Path.Combine ("unexistingdir", "filename");
193                         try {
194                                 Write (filename);
195                         }
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");
201                                 throw;
202                         }
203                 }
204
205                 [Test]
206                 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
207                 [ExpectedException (typeof (IsolatedStorageException))]
208                 public void Handle ()
209                 {
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
213                 }
214 #if NET_2_0
215                 [Test]
216                 [IsolatedStorageFilePermission (SecurityAction.PermitOnly, Unrestricted = true)]
217                 [ExpectedException (typeof (IsolatedStorageException))]
218                 public void SafeFileHandle ()
219                 {
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
223                 }
224 #endif
225
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).
229
230                 [Test]
231                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
232                 [ExpectedException (typeof (SecurityException))]
233                 public void Handle_UnmanagedCode ()
234                 {
235                         IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("cas-Handle-Unmanaged", FileMode.Create);
236                         try {
237                                 MethodInfo mi = typeof (IsolatedStorageFileStream).GetProperty ("Handle").GetGetMethod ();
238                                 mi.Invoke (isfs, null);
239                         }
240                         finally {
241                                 isfs.Close ();
242                         }
243                 }
244 #if NET_2_0
245                 [Test]
246                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
247                 [ExpectedException (typeof (SecurityException))]
248                 public void SafeFileHandle_UnmanagedCode ()
249                 {
250                         IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("cas-SafeFileHandle-Unmanaged", FileMode.Create);
251                         try {
252                                 MethodInfo mi = typeof (IsolatedStorageFileStream).GetProperty ("SafeFileHandle").GetGetMethod ();
253                                 mi.Invoke (isfs, null);
254                         }
255                         finally {
256                                 isfs.Close ();
257                         }
258                 }
259 #endif
260         }
261 }