2006-04-28 Robert Jordan <robertj@gmx.net>
[mono.git] / mcs / class / corlib / Test / System.IO / FileStreamCas.cs
1 //
2 // FileStreamCas.cs -CAS unit tests for System.IO.FileStream
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using NUnit.Framework;
30
31 using System;
32 using System.IO;
33 using System.Reflection;
34 using System.Security;
35 using System.Security.Permissions;
36 using System.Threading;
37
38 namespace MonoCasTests.System.IO {
39
40         [TestFixture]
41         [Category ("CAS")]
42         public class FileStreamCas {
43
44                 private MonoTests.System.IO.FileStreamTest fst;
45                 private const int timeout = 30000;
46                 private string message;
47                 private string readfile;
48                 private string writefile;
49
50                 static ManualResetEvent reset;
51
52                 [TestFixtureSetUp]
53                 public void FixtureSetUp ()
54                 {
55                         // this occurs with a "clean" stack (full trust)
56                         fst  = new MonoTests.System.IO.FileStreamTest ();
57                         reset = new ManualResetEvent (false);
58                         readfile = Path.GetTempFileName ();
59                         writefile = Path.GetTempFileName ();
60                 }
61
62                 [TestFixtureTearDown]
63                 public void FixtureTearDown ()
64                 {
65                         reset.Close ();
66                         if (File.Exists (readfile))
67                                 File.Delete (readfile);
68                         if (File.Exists (writefile))
69                                 File.Delete (writefile);
70                 }
71
72                 [SetUp]
73                 public void SetUp ()
74                 {
75                         if (!SecurityManager.SecurityEnabled)
76                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
77                 }
78
79                 // Partial Trust Tests - i.e. call "normal" unit with reduced privileges
80
81                 [Test]
82                 [ExpectedException (typeof (SecurityException))]
83                 public void PartialTrust_DenyUnrestricted_Failure ()
84                 {
85                         try {
86                                 // SetUp/TearDown requires FileIOPermission
87                                 fst.SetUp ();
88                                 // so does the call but that's the test ;-)
89                                 CallRestricted (fst);
90                         }
91                         finally {
92                                 fst.TearDown ();
93                         }
94                 }
95
96                 [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
97                 private void CallRestricted (MonoTests.System.IO.FileStreamTest fst)
98                 {
99                         fst.TestDefaultProperties ();
100                 }
101
102                 [Test]
103                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
104                 public void PartialTrust_PermitOnly_FileIOPermission_Success ()
105                 {
106                         fst.SetUp ();
107                         fst.TestCtr ();
108                         fst.CtorAccess1Read2Read ();
109                         fst.Write ();
110                         fst.Length ();
111                         fst.Flush ();
112                         fst.TestDefaultProperties ();
113                         fst.TestLock ();
114                         fst.Seek ();
115                         fst.TestSeek ();
116                         fst.TestClose ();
117                         fst.PositionAfterSetLength ();
118                         fst.ReadBytePastEndOfStream ();
119                         fst.TearDown ();
120                 }
121
122                 [Test]
123                 [FileIOPermission (SecurityAction.PermitOnly, Unrestricted = true)]
124                 [SecurityPermission (SecurityAction.PermitOnly, UnmanagedCode = true)]
125                 public void PartialTrust_PermitOnly_FileIOPermissionUnmanagedCode_Success ()
126                 {
127                         fst.SetUp ();
128                         fst.TestFlushNotOwningHandle ();
129                         fst.TearDown ();
130                 }
131
132                 // test Demand by denying the required permissions
133
134                 [Test]
135                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
136                 [ExpectedException (typeof (SecurityException))]
137                 public void Ctor_IntPtrFileAccess ()
138                 {
139                         new FileStream (IntPtr.Zero, FileAccess.Read);
140                 }
141
142                 [Test]
143                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
144                 [ExpectedException (typeof (SecurityException))]
145                 public void Ctor_IntPtrFileAccessBool ()
146                 {
147                         new FileStream (IntPtr.Zero, FileAccess.Read, false);
148                 }
149
150                 [Test]
151                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
152                 [ExpectedException (typeof (SecurityException))]
153                 public void Ctor_IntPtrFileAccessBoolInt ()
154                 {
155                         new FileStream (IntPtr.Zero, FileAccess.Read, false, 0);
156                 }
157
158                 [Test]
159                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
160                 [ExpectedException (typeof (SecurityException))]
161                 public void Ctor_IntPtrFileAccessBoolIntBool ()
162                 {
163                         new FileStream (IntPtr.Zero, FileAccess.Read, false, 0, false);
164                 }
165
166                 // we use reflection to call FileStream as the Handle property is protected
167                 // by a LinkDemand (which will be converted into full demand, i.e. a stack 
168                 // walk) when reflection is used (i.e. it gets testable).
169
170                 [Test]
171                 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
172                 [ExpectedException (typeof (SecurityException))]
173                 public void Handle ()
174                 {
175                         FileStream fs = File.OpenWrite (Path.GetTempFileName ());
176                         try {
177                                 MethodInfo mi = typeof (FileStream).GetProperty ("Handle").GetGetMethod ();
178                                 mi.Invoke (fs, null);
179                         }
180                         finally {
181                                 fs.Close ();
182                         }
183                 }
184
185                 // async tests (for stack propagation)
186
187                 private void ReadCallback (IAsyncResult ar)
188                 {
189                         FileStream s = (FileStream)ar.AsyncState;
190                         s.EndRead (ar);
191                         try {
192                                 // can we do something bad here ?
193                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
194                                 message = "Expected a SecurityException";
195                         }
196                         catch (SecurityException) {
197                                 message = null;
198                                 reset.Set ();
199                         }
200                         catch (Exception e) {
201                                 message = e.ToString ();
202                         }
203                 }
204
205                 [Test]
206                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
207                 public void AsyncRead ()
208                 {
209                         FileStream fs = new FileStream (readfile, FileMode.OpenOrCreate);
210                         message = "AsyncRead";
211                         reset.Reset ();
212                         IAsyncResult r = fs.BeginRead (new byte[0], 0, 0, new AsyncCallback (ReadCallback), fs);
213                         Assert.IsNotNull (r, "IAsyncResult");
214                         if (!reset.WaitOne (timeout, true))
215                                 Assert.Ignore ("Timeout");
216                         Assert.IsNull (message, message);
217                         fs.Close ();
218                 }
219
220                 private void WriteCallback (IAsyncResult ar)
221                 {
222                         FileStream s = (FileStream)ar.AsyncState;
223                         s.EndWrite (ar);
224                         try {
225                                 // can we do something bad here ?
226                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
227                                 message = "Expected a SecurityException";
228                         }
229                         catch (SecurityException) {
230                                 message = null;
231                                 reset.Set ();
232                         }
233                         catch (Exception e) {
234                                 message = e.ToString ();
235                         }
236                 }
237
238                 [Test]
239                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
240                 public void AsyncWrite ()
241                 {
242                         FileStream fs = new FileStream (writefile, FileMode.OpenOrCreate);
243                         message = "AsyncWrite";
244                         reset.Reset ();
245                         IAsyncResult r = fs.BeginWrite (new byte[0], 0, 0, new AsyncCallback (WriteCallback), fs);
246                         Assert.IsNotNull (r, "IAsyncResult");
247                         if (!reset.WaitOne (timeout, true))
248                                 Assert.Ignore ("Timeout");
249                         Assert.IsNull (message, message);
250                         fs.Close ();
251                 }
252         }
253 }