2 // StreamCas.cs - CAS unit tests for System.IO.Stream
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using NUnit.Framework;
33 using System.Security;
34 using System.Security.Permissions;
35 using System.Threading;
37 namespace MonoCasTests.System.IO {
39 // System.IO.Stream is an abstract class, so we use our own inherited
40 // class for the tests
42 public class NonAbstractStream : Stream {
47 public override bool CanRead {
51 public override bool CanSeek {
55 public override bool CanWrite {
59 public override void Flush ()
63 public override long Length {
64 get { return _length; }
67 public override long Position {
72 public override int Read (byte[] buffer, int offset, int count)
78 public override long Seek (long offset, SeekOrigin origin)
84 public override void SetLength (long value)
89 public override void Write (byte[] buffer, int offset, int count)
97 public class StreamCas {
99 private const int timeout = 30000;
100 private string message;
102 static ManualResetEvent reset;
105 public void FixtureSetUp ()
107 reset = new ManualResetEvent (false);
110 [TestFixtureTearDown]
111 public void FixtureTearDown ()
119 if (!SecurityManager.SecurityEnabled)
120 Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
123 // async tests (for stack propagation)
125 private void ReadCallback (IAsyncResult ar)
127 NonAbstractStream s = (NonAbstractStream) ar.AsyncState;
130 // can we do something bad here ?
131 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
132 message = "Expected a SecurityException";
134 catch (SecurityException) {
138 catch (Exception e) {
139 message = e.ToString ();
144 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
145 public void AsyncRead ()
147 NonAbstractStream s = new NonAbstractStream ();
148 message = "AsyncRead";
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);
157 private void WriteCallback (IAsyncResult ar)
159 NonAbstractStream s = (NonAbstractStream)ar.AsyncState;
162 // can we do something bad here ?
163 Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
164 message = "Expected a SecurityException";
166 catch (SecurityException) {
170 catch (Exception e) {
171 message = e.ToString ();
176 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
177 public void AsyncWrite ()
179 NonAbstractStream s = new NonAbstractStream ();
180 message = "AsyncWrite";
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);