[corlib] FileStream::Name needs to include fully qualified normalized name. Fixes...
[mono.git] / mcs / class / corlib / Test / System.IO / StreamCas.cs
1 //
2 // StreamCas.cs - CAS unit tests for System.IO.Stream
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.Security;
34 using System.Security.Permissions;
35 using System.Threading;
36
37 namespace MonoCasTests.System.IO {
38
39         // System.IO.Stream is an abstract class, so we use our own inherited
40         // class for the tests
41
42         public class NonAbstractStream : Stream {
43
44                 private long _pos;
45                 private long _length;
46
47                 public override bool CanRead {
48                         get { return true; }
49                 }
50
51                 public override bool CanSeek {
52                         get { return true; }
53                 }
54
55                 public override bool CanWrite {
56                         get { return true; }
57                 }
58
59                 public override void Flush ()
60                 {
61                 }
62
63                 public override long Length {
64                         get { return _length; }
65                 }
66
67                 public override long Position {
68                         get { return _pos; }
69                         set { _pos = value; }
70                 }
71
72                 public override int Read (byte[] buffer, int offset, int count)
73                 {
74                         _pos += count;
75                         return count;
76                 }
77
78                 public override long Seek (long offset, SeekOrigin origin)
79                 {
80                         _pos = offset;
81                         return _pos;
82                 }
83
84                 public override void SetLength (long value)
85                 {
86                         _length = value;
87                 }
88
89                 public override void Write (byte[] buffer, int offset, int count)
90                 {
91                         _pos += count;
92                 }
93         }
94
95         [TestFixture]
96         [Category ("CAS")]
97         public class StreamCas {
98
99                 private const int timeout = 30000;
100                 private string message;
101
102                 static ManualResetEvent reset;
103
104                 [TestFixtureSetUp]
105                 public void FixtureSetUp ()
106                 {
107                         reset = new ManualResetEvent (false);
108                 }
109
110                 [TestFixtureTearDown]
111                 public void FixtureTearDown ()
112                 {
113                         reset.Close ();
114                 }
115
116                 [SetUp]
117                 public void SetUp ()
118                 {
119                         if (!SecurityManager.SecurityEnabled)
120                                 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
121                 }
122
123                 // async tests (for stack propagation)
124
125                 private void ReadCallback (IAsyncResult ar)
126                 {
127                         NonAbstractStream s = (NonAbstractStream) ar.AsyncState;
128                         s.EndRead (ar);
129                         try {
130                                 // can we do something bad here ?
131                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
132                                 message = "Expected a SecurityException";
133                         }
134                         catch (SecurityException) {
135                                 message = null;
136                                 reset.Set ();
137                         }
138                         catch (Exception e) {
139                                 message = e.ToString ();
140                         }
141                 }
142
143                 [Test]
144                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
145                 public void AsyncRead ()
146                 {
147                         NonAbstractStream s = new NonAbstractStream ();
148                         message = "AsyncRead";
149                         reset.Reset ();
150                         IAsyncResult r = s.BeginRead (null, 0, 0, new AsyncCallback (ReadCallback), s);
151                         Assert.IsNotNull (r, "IAsyncResult");
152                         if (!reset.WaitOne (timeout, true))
153                                 Assert.Ignore ("Timeout");
154                         Assert.IsNull (message, message);
155                 }
156
157                 private void WriteCallback (IAsyncResult ar)
158                 {
159                         NonAbstractStream s = (NonAbstractStream)ar.AsyncState;
160                         s.EndWrite (ar);
161                         try {
162                                 // can we do something bad here ?
163                                 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
164                                 message = "Expected a SecurityException";
165                         }
166                         catch (SecurityException) {
167                                 message = null;
168                                 reset.Set ();
169                         }
170                         catch (Exception e) {
171                                 message = e.ToString ();
172                         }
173                 }
174
175                 [Test]
176                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
177                 public void AsyncWrite ()
178                 {
179                         NonAbstractStream s = new NonAbstractStream ();
180                         message = "AsyncWrite";
181                         reset.Reset ();
182                         IAsyncResult r = s.BeginWrite (null, 0, 0, new AsyncCallback (WriteCallback), s);
183                         Assert.IsNotNull (r, "IAsyncResult");
184                         if (!reset.WaitOne (timeout, true))
185                                 Assert.Ignore ("Timeout");
186                         Assert.IsNull (message, message);
187                 }
188         }
189 }