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 using Microsoft.Win32.SafeHandles;
34 
35 using NUnit.Framework;
36 
37 namespace MonoTests.System.IO.IsolatedStorageTest {
38 
39 	[TestFixture]
40 	public class IsolatedStorageFileStreamTest {
41 
CheckCommonDetails(string prefix, IsolatedStorageFileStream isfs, bool read, bool write)42 		private void CheckCommonDetails (string prefix, IsolatedStorageFileStream isfs, bool read, bool write)
43 		{
44 			Assert.AreEqual (read, isfs.CanRead, prefix + ".CanRead");
45 			Assert.IsTrue (isfs.CanSeek, prefix + ".CanSeek");
46 			Assert.AreEqual (write, isfs.CanWrite, prefix + ".CanWrite");
47 			Assert.IsFalse (isfs.IsAsync, prefix + ".IsAsync");
48 			Assert.AreEqual (0, isfs.Length, prefix + ".Length");
49 			Assert.AreEqual ("[Unknown]", isfs.Name, prefix + ".Name");
50 			Assert.AreEqual (0, isfs.Position, prefix + ".Position");
51 		}
52 
53 		[Test]
54 		[ExpectedException (typeof (ArgumentNullException))]
Constructor_StringNullMode()55 		public void Constructor_StringNullMode ()
56 		{
57 			IsolatedStorageFileStream isfs = new IsolatedStorageFileStream (null, FileMode.CreateNew);
58 		}
59 
60 		[Test]
61 		[ExpectedException (typeof (ArgumentException))] // Mono's FileStream throw an ArgumentOutOfRangeException
Constructor_StringModeBad()62 		public void Constructor_StringModeBad ()
63 		{
64 			IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("badmode", (FileMode)Int32.MinValue);
65 		}
66 
67 		[Test]
Constructor_StringMode()68 		public void Constructor_StringMode ()
69 		{
70 			string test = "string-filemode";
71 			using (var isfs = new IsolatedStorageFileStream (test, FileMode.Create)) {
72 				CheckCommonDetails (test, isfs, true, true);
73 			}
74 		}
75 
76 		[Test]
77 		[ExpectedException (typeof (ArgumentOutOfRangeException))]
Constructor_StringModeAccessBad()78 		public void Constructor_StringModeAccessBad ()
79 		{
80 			IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("badaccess", FileMode.Create, (FileAccess)Int32.MinValue);
81 		}
82 
83 		[Test]
Constructor_StringModeAccess()84 		public void Constructor_StringModeAccess ()
85 		{
86 			string test = "string-filemode-fileaccess";
87 			using (var isfs = new IsolatedStorageFileStream (test, FileMode.Create, FileAccess.ReadWrite)) {
88 				CheckCommonDetails (test, isfs, true, true);
89 			}
90 		}
91 
92 		[Test]
93 		[ExpectedException (typeof (ArgumentOutOfRangeException))]
Constructor_StringModeAccessShareBad()94 		public void Constructor_StringModeAccessShareBad ()
95 		{
96 			IsolatedStorageFileStream isfs = new IsolatedStorageFileStream ("badshare", FileMode.Create, FileAccess.Read, (FileShare)Int32.MinValue);
97 		}
98 
99 		[Test]
Constructor_StringModeAccessShare()100 		public void Constructor_StringModeAccessShare ()
101 		{
102 			string test = "string-filemode-fileaccess-fileshare";
103 			using (var isfs = new IsolatedStorageFileStream (test, FileMode.Create, FileAccess.Write, FileShare.Read)) {
104 				CheckCommonDetails (test, isfs, false, true);
105 			}
106 		}
107 
108 		[Test]
109 		[ExpectedException (typeof (IsolatedStorageException))]
Handle()110 		public void Handle ()
111 		{
112 			using (var isfs = new IsolatedStorageFileStream ("handle", FileMode.Create)) {
113 				IntPtr p = isfs.Handle;
114 			}
115 		}
116 
117 		[Test]
RootPath()118 		public void RootPath ()
119 		{
120 			new IsolatedStorageFileStream ("/rootpath", FileMode.Create).Close ();
121 		}
122 
123 		[Test]
Constructor_StorageInvalid()124 		public void Constructor_StorageInvalid ()
125 		{
126 			IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly ();
127 
128 			isf.Close ();
129 			try {
130 				new IsolatedStorageFileStream ("file", FileMode.Create, isf);
131 			} catch (InvalidOperationException) {
132 			}
133 
134 			isf.Dispose ();
135 			try {
136 				new IsolatedStorageFileStream ("file", FileMode.Create, isf);
137 			} catch (InvalidOperationException) {
138 			}
139 
140 			// Re-open and then remove the storage
141 			isf = IsolatedStorageFile.GetUserStoreForAssembly ();
142 			isf.Remove ();
143 
144 			try {
145 				new IsolatedStorageFileStream ("file", FileMode.Create, isf);
146 			} catch (InvalidOperationException) {
147 			}
148 		}
149 	}
150 }
151