5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright 2011 Xamarin, Inc (http://www.xamarin.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.
32 using NUnit.Framework;
34 namespace MonoTests.System.IO
37 public class StreamTest
39 class MockStream : Stream
41 bool canRead, canSeek, canWrite;
42 public event Action OnFlush;
43 public event Func<byte[], int, int, int> OnRead;
44 public event Action<byte[], int, int> OnWrite;
46 public MockStream (bool canRead, bool canSeek, bool canWrite)
48 this.canRead = canRead;
49 this.canSeek = canSeek;
50 this.canWrite = canWrite;
53 public override bool CanRead {
59 public override bool CanSeek {
65 public override bool CanWrite {
71 public override void Flush ()
77 public override long Length {
78 get { throw new NotImplementedException (); }
81 public override long Position {
83 throw new NotImplementedException ();
86 throw new NotImplementedException ();
90 public override int Read (byte[] buffer, int offset, int count)
93 return OnRead (buffer, offset, count);
98 public override long Seek (long offset, SeekOrigin origin)
100 throw new NotImplementedException ();
103 public override void SetLength (long value)
105 throw new NotImplementedException ();
108 public override void Write (byte[] buffer, int offset, int count)
111 OnWrite (buffer, offset, count);
116 public void SynchronizedTest ()
118 var s = Stream.Synchronized (new MemoryStream ());
120 Assert.IsFalse (s.CanRead, "#1");
124 public void CopyAsync ()
126 var ms = new MockStream (true, false, true);
128 ms.OnRead += delegate { return --counter; };
129 var memory = new MemoryStream ();
130 Assert.IsTrue (ms.CopyToAsync (ms).Wait (1000));
134 public void FlushAsync ()
137 var ms = new MockStream (false, false, false);
138 ms.OnFlush += () => { called = true; };
139 var t = ms.FlushAsync ();
140 Assert.IsTrue (t.Wait (1000), "#1");
141 Assert.IsTrue (called, "#2");
145 public void FlushAsync_Exception ()
147 var ms = new MockStream (false, false, false);
148 ms.OnFlush += () => { throw new ApplicationException (); };
149 var t = ms.FlushAsync ();
153 } catch (AggregateException) {
158 public void ReadAsync ()
161 var buffer = new byte[4];
162 var ms = new MockStream (true, false, false);
163 ms.OnRead += (b, p, c) => { called = true; return 2; };
164 var t = ms.ReadAsync (buffer, 0, 4);
166 Assert.IsTrue (t.Wait (1000), "#1");
167 Assert.IsTrue (called, "#2");
168 Assert.AreEqual (2, t.Result, "#3");
172 public void ReadAsync_Exception ()
174 var buffer = new byte[4];
175 var ms = new MockStream (true, false, false);
176 ms.OnRead += (b, p, c) => { throw new ApplicationException (); };
177 var t = ms.ReadAsync (buffer, 0, 4);
182 } catch (AggregateException) {
187 public void WriteAsync ()
190 var buffer = new byte[4];
191 var ms = new MockStream (false, false, true);
192 ms.OnWrite += (b, p, c) => { called = true; };
193 var t = ms.WriteAsync (buffer, 0, 4);
195 Assert.IsTrue (t.Wait (1000), "#1");
196 Assert.IsTrue (called, "#2");
200 public void WriteAsync_Exception ()
202 var buffer = new byte[4];
203 var ms = new MockStream (false, false, true);
204 ms.OnWrite += (b, p, c) => { throw new ApplicationException (); };
205 var t = ms.WriteAsync (buffer, 0, 4);
210 } catch (AggregateException) {