1 // FileStreamSafeHandleTests.cs - Test Cases for System.IO.FileStream class 2 // 3 // Authors: 4 // Marcos Henrich (marcos.henrich@xamarin.com) 5 // 6 // Copyright 2015 Xamarin Inc (http://www.xamarin.com). 7 // 8 9 using System; 10 using System.IO; 11 using System.Security.AccessControl; 12 using System.Threading; 13 using NUnit.Framework; 14 15 namespace MonoTests.System.IO 16 { 17 [TestFixture] 18 public class FileStreamWithClosedSafeHandleTests 19 { 20 #if !MOBILE GetFileStreamWithClosedHandle()21 private FileStream GetFileStreamWithClosedHandle () 22 { 23 var fs1 = new FileStream ("test2", FileMode.OpenOrCreate); 24 var fs2 = new FileStream (fs1.SafeFileHandle, FileAccess.Read); 25 fs1.Close (); 26 27 return fs2; 28 } 29 30 [Test] 31 [ExpectedException (typeof (ObjectDisposedException))] GetLength()32 public void GetLength () 33 { 34 var fs = GetFileStreamWithClosedHandle (); 35 36 var l = fs.Length; 37 } 38 39 [Test] 40 [ExpectedException (typeof (ObjectDisposedException))] GetPosition()41 public void GetPosition () 42 { 43 var fs = GetFileStreamWithClosedHandle (); 44 45 var p = fs.Position; 46 } 47 48 [Test] 49 [ExpectedException (typeof (ObjectDisposedException))] SetPosition()50 public void SetPosition () 51 { 52 var fs = GetFileStreamWithClosedHandle (); 53 54 fs.Position = 3; 55 } 56 57 [Test] 58 [ExpectedException (typeof (ObjectDisposedException))] GetAccessControl()59 public void GetAccessControl () 60 { 61 var fs = GetFileStreamWithClosedHandle (); 62 63 fs.GetAccessControl (); 64 } 65 66 [Test] 67 [ExpectedException (typeof (ObjectDisposedException))] SetAccessControl()68 public void SetAccessControl () 69 { 70 var fs = GetFileStreamWithClosedHandle (); 71 72 fs.SetAccessControl (new FileSecurity ()); 73 } 74 75 [Test] 76 [ExpectedException (typeof (ObjectDisposedException))] Flush()77 public void Flush () 78 { 79 var fs = GetFileStreamWithClosedHandle (); 80 81 fs.Flush (false); 82 } 83 84 [Test] 85 [ExpectedException (typeof (ObjectDisposedException))] SetLength()86 public void SetLength () 87 { 88 var fs = GetFileStreamWithClosedHandle (); 89 90 fs.SetLength (20); 91 } 92 93 [Test] 94 [ExpectedException (typeof (ObjectDisposedException))] Read()95 public void Read () 96 { 97 var fs = GetFileStreamWithClosedHandle (); 98 99 fs.Read (new byte [2], 0, 1); 100 } 101 102 [Test] 103 [ExpectedException (typeof (ObjectDisposedException))] Seek()104 public void Seek () 105 { 106 var fs = GetFileStreamWithClosedHandle (); 107 108 fs.Seek (0, SeekOrigin.Begin); 109 } 110 111 [Test] 112 [ExpectedException (typeof (ObjectDisposedException))] Write()113 public void Write () 114 { 115 var fs = GetFileStreamWithClosedHandle (); 116 117 fs.Write (new byte [2], 0, 1); 118 } 119 120 [Test] 121 [ExpectedException (typeof (ObjectDisposedException))] ReadByte()122 public void ReadByte () 123 { 124 var fs = GetFileStreamWithClosedHandle (); 125 126 fs.ReadByte (); 127 } 128 129 [Test] 130 [ExpectedException (typeof (ObjectDisposedException))] WriteByte()131 public void WriteByte () 132 { 133 var fs = GetFileStreamWithClosedHandle (); 134 135 fs.WriteByte (0); 136 } 137 138 [Test] 139 [ExpectedException (typeof (ObjectDisposedException))] Lock()140 public void Lock () 141 { 142 var fs = GetFileStreamWithClosedHandle (); 143 144 fs.Lock (0, 1); 145 } 146 147 [Test] 148 [ExpectedException (typeof (ObjectDisposedException))] UnLock()149 public void UnLock () 150 { 151 var fs = GetFileStreamWithClosedHandle (); 152 153 fs.Unlock (0, 1); 154 } 155 156 [Test] 157 [ExpectedException (typeof (ObjectDisposedException))] ReadAsync()158 public void ReadAsync () 159 { 160 var fs = GetFileStreamWithClosedHandle (); 161 162 fs.ReadAsync (new byte [2], 0, 1); 163 } 164 165 [Test] 166 [ExpectedException (typeof (ObjectDisposedException))] WriteAsync()167 public void WriteAsync () 168 { 169 var fs = GetFileStreamWithClosedHandle (); 170 171 fs.WriteAsync (new byte [2], 0, 1); 172 } 173 174 [Test] 175 [ExpectedException (typeof (ObjectDisposedException))] FlushAsync()176 public void FlushAsync () 177 { 178 var fs = GetFileStreamWithClosedHandle (); 179 180 fs.FlushAsync (new CancellationToken ()); 181 } 182 #endif 183 } 184 } 185