1 // 2 // StreamTest.cs 3 // 4 // Authors: 5 // Marek Safar <marek.safar@gmail.com> 6 // 7 // Copyright 2011 Xamarin, Inc (http://www.xamarin.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 System; 30 using System.IO; 31 32 using NUnit.Framework; 33 34 namespace MonoTests.System.IO 35 { 36 [TestFixture] 37 public class StreamTest 38 { 39 class MockStream : Stream 40 { 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; 45 MockStream(bool canRead, bool canSeek, bool canWrite)46 public MockStream (bool canRead, bool canSeek, bool canWrite) 47 { 48 this.canRead = canRead; 49 this.canSeek = canSeek; 50 this.canWrite = canWrite; 51 } 52 53 public override bool CanRead { 54 get { 55 return canRead; 56 } 57 } 58 59 public override bool CanSeek { 60 get { 61 return canSeek; 62 } 63 } 64 65 public override bool CanWrite { 66 get { 67 return canWrite; 68 } 69 } 70 Flush()71 public override void Flush () 72 { 73 if (OnFlush != null) 74 OnFlush (); 75 } 76 77 public override long Length { 78 get { throw new NotImplementedException (); } 79 } 80 81 public override long Position { 82 get { 83 throw new NotImplementedException (); 84 } 85 set { 86 throw new NotImplementedException (); 87 } 88 } 89 Read(byte[] buffer, int offset, int count)90 public override int Read (byte[] buffer, int offset, int count) 91 { 92 if (OnRead != null) 93 return OnRead (buffer, offset, count); 94 95 return -1; 96 } 97 Seek(long offset, SeekOrigin origin)98 public override long Seek (long offset, SeekOrigin origin) 99 { 100 throw new NotImplementedException (); 101 } 102 SetLength(long value)103 public override void SetLength (long value) 104 { 105 throw new NotImplementedException (); 106 } 107 Write(byte[] buffer, int offset, int count)108 public override void Write (byte[] buffer, int offset, int count) 109 { 110 if (OnWrite != null) 111 OnWrite (buffer, offset, count); 112 } 113 } 114 115 [Test] SynchronizedTest()116 public void SynchronizedTest () 117 { 118 var s = Stream.Synchronized (new MemoryStream ()); 119 s.Close (); 120 Assert.IsFalse (s.CanRead, "#1"); 121 } 122 123 [Test] CopyAsync()124 public void CopyAsync () 125 { 126 var ms = new MockStream (true, false, true); 127 int counter = 4; 128 ms.OnRead += delegate { return --counter; }; 129 var memory = new MemoryStream (); 130 Assert.IsTrue (ms.CopyToAsync (ms).Wait (1000)); 131 } 132 133 [Test] FlushAsync()134 public void FlushAsync () 135 { 136 bool called = false; 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"); 142 } 143 144 [Test] FlushAsync_Exception()145 public void FlushAsync_Exception () 146 { 147 var ms = new MockStream (false, false, false); 148 ms.OnFlush += () => { throw new ApplicationException (); }; 149 var t = ms.FlushAsync (); 150 try { 151 t.Wait (1000); 152 Assert.Fail (); 153 } catch (AggregateException) { 154 } 155 } 156 157 [Test] ReadAsync()158 public void ReadAsync () 159 { 160 bool called = false; 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); 165 166 Assert.IsTrue (t.Wait (1000), "#1"); 167 Assert.IsTrue (called, "#2"); 168 Assert.AreEqual (2, t.Result, "#3"); 169 } 170 171 [Test] ReadAsync_Exception()172 public void ReadAsync_Exception () 173 { 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); 178 179 try { 180 t.Wait (1000); 181 Assert.Fail (); 182 } catch (AggregateException) { 183 } 184 } 185 186 [Test] WriteAsync()187 public void WriteAsync () 188 { 189 bool called = false; 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); 194 195 Assert.IsTrue (t.Wait (1000), "#1"); 196 Assert.IsTrue (called, "#2"); 197 } 198 199 [Test] WriteAsync_Exception()200 public void WriteAsync_Exception () 201 { 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); 206 207 try { 208 t.Wait (1000); 209 Assert.Fail (); 210 } catch (AggregateException) { 211 } 212 } 213 } 214 } 215