Adjust corlib unit tests to reduce failures when executed on the NET_2_1 profile
[mono.git] / mcs / class / corlib / Test / System.IO.IsolatedStorage / IsolatedStorageFileStreamTest.cs
1 //
2 // IsolatedStorageFileStreamTest.cs 
3 //      - Unit Tests for abstract IsolatedStorageFileStream class
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 System;
31 using System.IO;
32 using System.IO.IsolatedStorage;
33 #if NET_2_0
34 using Microsoft.Win32.SafeHandles;
35 #endif
36
37 using NUnit.Framework;
38
39 namespace MonoTests.System.IO.IsolatedStorageTest {
40
41         [TestFixture]
42         public class IsolatedStorageFileStreamTest {
43
44                 private void CheckCommonDetails (string prefix, IsolatedStorageFileStream isfs, bool read, bool write)
45                 {
46                         Assert.AreEqual (read, isfs.CanRead, prefix + ".CanRead");
47                         Assert.IsTrue (isfs.CanSeek, prefix + ".CanSeek");
48                         Assert.AreEqual (write, isfs.CanWrite, prefix + ".CanWrite");
49                         Assert.IsFalse (isfs.IsAsync, prefix + ".IsAsync");
50                         Assert.AreEqual (0, isfs.Length, prefix + ".Length");
51                         Assert.AreEqual ("[Unknown]", isfs.Name, prefix + ".Name");
52                         Assert.AreEqual (0, isfs.Position, prefix + ".Position");
53 #if NET_2_0_NOTYET
54                         Assert.IsFalse (isfs.CanTimeout, prefix + ".CanTimeout");
55 #endif
56                 }
57
58                 [Test]
59                 [ExpectedException (typeof (ArgumentNullException))]
60                 public void Constructor_StringNullMode ()
61                 {
62                         IsolatedStorageFileStream isfs = new IsolatedStorageFileStream (null, FileMode.CreateNew);
63                 }
64
65                 [Test]
66                 [ExpectedException (typeof (ArgumentException))] // Mono's FileStream throw an ArgumentOutOfRangeException
67                 public void Constructor_StringModeBad ()
68                 {
69                         IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("badmode", (FileMode)Int32.MinValue);
70                 }
71
72                 [Test]
73                 public void Constructor_StringMode ()
74                 {
75                         string test = "string-filemode";
76                         IsolatedStorageFileStream isfs = new IsolatedStorageFileStream (test, FileMode.Create);
77                         CheckCommonDetails (test, isfs, true, true);
78                 }
79
80                 [Test]
81                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
82                 public void Constructor_StringModeAccessBad ()
83                 {
84                         IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("badaccess", FileMode.Create, (FileAccess)Int32.MinValue);
85                 }
86
87                 [Test]
88                 public void Constructor_StringModeAccess ()
89                 {
90                         string test = "string-filemode-fileaccess";
91                         IsolatedStorageFileStream isfs = new IsolatedStorageFileStream (test, FileMode.Create, FileAccess.ReadWrite);
92                         CheckCommonDetails (test, isfs, true, true);
93                 }
94
95                 [Test]
96                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
97                 public void Constructor_StringModeAccessShareBad ()
98                 {
99                         IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("badshare", FileMode.Create, FileAccess.Read, (FileShare)Int32.MinValue);
100                 }
101
102                 [Test]
103                 public void Constructor_StringModeAccessShare ()
104                 {
105                         string test = "string-filemode-fileaccess-fileshare";
106                         IsolatedStorageFileStream isfs = new IsolatedStorageFileStream (test, FileMode.Create, FileAccess.Write, FileShare.Read);
107                         CheckCommonDetails (test, isfs, false, true);
108                 }
109
110                 [Test]
111                 [ExpectedException (typeof (IsolatedStorageException))]
112                 public void Handle ()
113                 {
114                         IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("handle", FileMode.Create);
115                         IntPtr p = isfs.Handle;
116                 }
117
118                 [Test]
119                 public void RootPath ()
120                 {
121                         new IsolatedStorageFileStream ("/rootpath", FileMode.Create);
122                 }
123 #if NET_2_0_NOTYET
124                 [Test]
125                 [ExpectedException (typeof (IsolatedStorageException))]
126                 public void SafeFileHandle_ ()
127                 {
128                         IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("safeFileHandle", FileMode.Create);
129                         SafeFileHandle sfh = isfs.SafeFileHandle;
130                 }
131
132                 [Test]
133                 [ExpectedException (typeof (InvalidOperationException))]
134                 public void ReadTimeOut ()
135                 {
136                         IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("readTimeout", FileMode.Create);
137                         int t = isfs.ReadTimeout;
138                 }
139
140                 [Test]
141                 [ExpectedException (typeof (InvalidOperationException))]
142                 public void WriteTimeOut ()
143                 {
144                         IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("writeTimeout", FileMode.Create);
145                         int t = isfs.WriteTimeout;
146                 }
147 #endif
148
149 #if NET_4_0
150                 [Test]
151                 public void Constructor_StorageInvalid ()
152                 {
153                         IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
154
155                         isf.Close ();
156                         try {
157                                 new IsolatedStorageFileStream ("file", FileMode.Create, isf);
158                         } catch (InvalidOperationException) {
159                         }
160
161                         isf.Dispose ();
162                         try {
163                                 new IsolatedStorageFileStream ("file", FileMode.Create, isf);
164                         } catch (InvalidOperationException) {
165                         }
166
167                         // Re-open and then remove the storage
168                         isf = IsolatedStorageFile.GetUserStoreForAssembly ();
169                         isf.Remove ();
170
171                         try {
172                                 new IsolatedStorageFileStream ("file", FileMode.Create, isf);
173                         } catch (InvalidOperationException) {
174                         }
175                 }
176 #endif
177         }
178 }