public MFTestResults FileAccess_Write() { // Clean up incase file exists if (File.Exists(fileName)) { File.Delete(fileName); } MFTestResults result = MFTestResults.Pass; try { // From MSDN: // Write access to the file. Data can be written to the file. // Combine with Read for read/write access. Log.Comment("Create file for testing"); using (FileStream fs = new FileStream(fileName, FileMode.CreateNew)) { FileStreamHelper.WriteReadEmpty(fs); } using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Write)) { Log.Comment("Should be able to write"); if (!FileStreamHelper.Write(fs, 1000)) { result = MFTestResults.Fail; } if (!FileStreamHelper.Write(fs, 500)) { result = MFTestResults.Fail; } Log.Comment("Shouldn't be able to read"); try { fs.Seek(0, SeekOrigin.Begin); int data = fs.ReadByte(); result = MFTestResults.Fail; Log.Exception("Expected NotSupportedException"); } catch (NotSupportedException) { /* pass case */ } } // 300 bytes original + 1000 bytes + 500 bytes if (!FileStreamHelper.VerifyFile(fileName, 1800)) { result = MFTestResults.Fail; } } catch (Exception ex) { Log.Exception("Unexpected exception: " + ex.Message); result = MFTestResults.Fail; } return(result); }
public MFTestResults Flush_Closed() { // Clean up incase file exists if (File.Exists(fileName)) { File.Delete(fileName); } MFTestResults result = MFTestResults.Pass; try { FileStream fs = new FileStream(fileName, FileMode.Create); FileStreamHelper.Write(fs, 500); fs.Close(); try { fs.Flush(); } catch (ObjectDisposedException) { /* Pass Case */ } } catch (Exception ex) { // previous Bug# 21655 Log.Exception("Unexpected exception: " + ex.Message); result = MFTestResults.Fail; } return(result); }
private void OpenWrite() { try { using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)) { threadResult &= FileStreamHelper.Write(fs, 200); } } catch (IOException) { if (!restricted) { threadResult = false; Log.Exception("Unexpected IOException"); } } catch (Exception ex) { threadResult = false; Log.Exception("Unexpected Exception " + ex.Message); } }
public MFTestResults Flush_WriteByte() { // Clean up incase file exists if (File.Exists(fileName)) { File.Delete(fileName); } MFTestResults result = MFTestResults.Pass; try { Log.Comment("Verify flush of 5000k using WriteByte"); using (FileStream fs = new FileStream(sourceFile, FileMode.CreateNew)) { FileStreamHelper.Write(fs, 5000); fs.Flush(); if (fs.Length != 5000) { result = MFTestResults.Fail; Log.Comment("Expected 5000 bytes in file"); } fs.Seek(0, SeekOrigin.Begin); if (!FileStreamHelper.VerifyRead(fs)) { result = MFTestResults.Fail; } } } catch (Exception ex) { Log.Exception("Unexpected exception: " + ex.Message); result = MFTestResults.Fail; } return(result); }
public MFTestResults InvalidCases() { if (File.Exists(fileName)) { File.Delete(fileName); } FileStream fs = new FileStream(fileName, FileMode.Create); FileStreamHelper.Write(fs, 1000); fs.Seek(0, SeekOrigin.Begin); byte[] readbuff = new byte[1024]; MFTestResults result = MFTestResults.Pass; try { try { Log.Comment("Read to null buffer"); int read = fs.Read(null, 0, readbuff.Length); result = MFTestResults.Fail; Log.Exception("Expected ArgumentNullException, but got " + read + " + bytes"); } catch (ArgumentNullException) { /* pass case */ } try { Log.Comment("Read to negative offset"); int read = fs.Read(readbuff, -1, readbuff.Length); result = MFTestResults.Fail; Log.Exception("Expected ArgumentOutOfRangeException, but got " + read + " + bytes"); } catch (ArgumentOutOfRangeException) { /* pass case */ } try { Log.Comment("Read to out of range offset"); int read = fs.Read(readbuff, readbuff.Length + 1, readbuff.Length); result = MFTestResults.Fail; Log.Exception("Expected ArgumentException, but got " + read + " + bytes"); } catch (ArgumentException) { /* pass case */ } try { Log.Comment("Read negative count"); int read = fs.Read(readbuff, 0, -1); result = MFTestResults.Fail; Log.Exception("Expected ArgumentOutOfRangeException, but got " + read + " + bytes"); } catch (ArgumentOutOfRangeException) { /* pass case */ } try { Log.Comment("Read count larger then buffer"); int read = fs.Read(readbuff, 0, readbuff.Length + 1); result = MFTestResults.Fail; Log.Exception("Expected ArgumentException, but got " + read + " + bytes"); } catch (ArgumentException) { /* pass case */ } try { Log.Comment("Read closed stream"); fs.Close(); int read = fs.Read(readbuff, 0, readbuff.Length); result = MFTestResults.Fail; Log.Exception("Expected ObjectDisposedException, but got " + read + " + bytes"); } catch (ObjectDisposedException) { /* pass case */ } try { Log.Comment("Read disposed stream"); fs = new FileStream(fileName, FileMode.Open); fs.Dispose(); int read = fs.Read(readbuff, 0, readbuff.Length); result = MFTestResults.Fail; Log.Exception("Expected ObjectDisposedException, but got " + read + " + bytes"); } catch (ObjectDisposedException) { /* pass case */ } } catch (Exception ex) { Log.Exception("Unexpected exception: " + ex.Message); result = MFTestResults.Fail; } finally { if (fs != null) { fs.Dispose(); } } return(result); }
public MFTestResults VanillaRead() { if (File.Exists(fileName)) { File.Delete(fileName); } using (FileStream fs = new FileStream(fileName, FileMode.Create)) { FileStreamHelper.Write(fs, 1000); } MFTestResults result = MFTestResults.Pass; try { using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite)) { Log.Comment("Read 256 bytes of data"); if (!TestRead(fs, 256)) { result = MFTestResults.Fail; } Log.Comment("Request less bytes then buffer"); if (!TestRead(fs, 256, 100, 100)) { result = MFTestResults.Fail; } // 1000 - 256 - 100 = 644 Log.Comment("Request more bytes then file"); if (!TestRead(fs, 1000, 1000, 644)) { result = MFTestResults.Fail; } Log.Comment("Request bytes after EOF"); if (!TestRead(fs, 100, 100, 0)) { result = MFTestResults.Fail; } Log.Comment("Rewind and read entire file in one buffer larger then file"); fs.Seek(0, SeekOrigin.Begin); if (!TestRead(fs, 1001, 1001, 1000)) { result = MFTestResults.Fail; } Log.Comment("Verify Read validation with UTF8 string"); fs.SetLength(0); string test = "MFFramework Test"; fs.Write(UTF8Encoding.UTF8.GetBytes(test), 0, test.Length); fs.Flush(); fs.Seek(0, SeekOrigin.Begin); byte[] readbuff = new byte[20]; fs.Read(readbuff, 0, readbuff.Length); string testResult = new string(Encoding.UTF8.GetChars(readbuff)); if (test != testResult) { result = MFTestResults.Fail; Log.Comment("Exepected: " + test + ", but got: " + testResult); } } } catch (Exception ex) { Log.Exception("Unexpected exception: " + ex.Message); result = MFTestResults.Fail; } return(result); }
public MFTestResults InvalidCases() { if (File.Exists(fileName)) { File.Delete(fileName); } FileStream fs = new FileStream(fileName, FileMode.Create); FileStreamHelper.Write(fs, 1000); long seek; MFTestResults result = MFTestResults.Pass; try { try { Log.Comment("Seek -1 from Begin"); seek = fs.Seek(-1, SeekOrigin.Begin); result = MFTestResults.Fail; Log.Exception("Expected IOException, but got position " + seek); } catch (IOException) { /* pass case */ } try { Log.Comment("Seek -1001 from Current - at end from write"); seek = fs.Seek(-1001, SeekOrigin.Current); result = MFTestResults.Fail; Log.Exception("Expected IOException, but got position " + seek); } catch (IOException) { /* pass case */ } try { Log.Comment("Seek -1001 from End"); seek = fs.Seek(-1001, SeekOrigin.End); result = MFTestResults.Fail; Log.Exception("Expected IOException, but got position " + seek); } catch (IOException) { /* pass case */ } try { Log.Comment("Seek invalid -1 origin"); seek = fs.Seek(1, (SeekOrigin)(-1)); result = MFTestResults.Fail; Log.Exception("Expected ArgumentException, but got position " + seek); } catch (ArgumentException) { /* pass case */ } try { Log.Comment("Seek invalid 10 origin"); seek = fs.Seek(1, (SeekOrigin)10); result = MFTestResults.Fail; Log.Exception("Expected ArgumentException, but got position " + seek); } catch (ArgumentException) { /* pass case */ } try { Log.Comment("Seek with closed socket"); fs.Close(); seek = fs.Seek(0, SeekOrigin.Begin); result = MFTestResults.Fail; Log.Exception("Expected ObjectDisposedException, but got position " + seek); } catch (ObjectDisposedException) { /* pass case */ } try { Log.Comment("Seek with disposed socket"); fs.Dispose(); seek = fs.Seek(0, SeekOrigin.End); result = MFTestResults.Fail; Log.Exception("Expected ObjectDisposedException, but got position " + seek); } catch (ObjectDisposedException) { /* pass case */ } } catch (Exception ex) { Log.Exception("Unexpected exception: " + ex.Message); result = MFTestResults.Fail; } finally { if (fs != null) { fs.Dispose(); } } return(result); }
public MFTestResults ValidCases() { MFTestResults result = MFTestResults.Pass; try { if (File.Exists(fileName)) { File.Delete(fileName); } using (FileStream fs = new FileStream(fileName, FileMode.Create)) { FileStreamHelper.Write(fs, 1000); Log.Comment("Seek to beginning"); if (!TestSeek(fs, 0, SeekOrigin.Begin, 0)) { result = MFTestResults.Fail; } Log.Comment("Seek forward offset from begging"); if (!TestSeek(fs, 10, SeekOrigin.Begin, 0)) { result = MFTestResults.Fail; } Log.Comment("Seek backwards offset from current"); if (!TestSeek(fs, -5, SeekOrigin.Current, 5)) { result = MFTestResults.Fail; } Log.Comment("Seek forwards offset from current"); if (!TestSeek(fs, 20, SeekOrigin.Current, 25)) { result = MFTestResults.Fail; } Log.Comment("Seek to end"); if (!TestSeek(fs, 0, SeekOrigin.End, 1000)) { result = MFTestResults.Fail; } Log.Comment("Seek backwards offset from end"); if (!TestSeek(fs, -35, SeekOrigin.End, 965)) { result = MFTestResults.Fail; } Log.Comment("Seek past end relative to End"); if (!TestExtend(fs, 1, SeekOrigin.End, 1001, 1002)) { result = MFTestResults.Fail; } Log.Comment("Seek past end relative to Begin"); if (!TestExtend(fs, 1002, SeekOrigin.Begin, 1002, 1003)) { result = MFTestResults.Fail; } Log.Comment("Seek past end relative to Current"); if (!TestSeek(fs, 995, SeekOrigin.Begin, 995)) { result = MFTestResults.Fail; } if (!TestExtend(fs, 10, SeekOrigin.Current, 1005, 1006)) { result = MFTestResults.Fail; } // 1000 --123456 // verify 011001 Log.Comment("Verify proper bytes written at end (zero'd bytes from seek beyond end)"); byte[] buff = new byte[6]; byte[] verify = new byte[] { 0, 1, 1, 0, 0, 1 }; fs.Seek(-6, SeekOrigin.End); fs.Read(buff, 0, buff.Length); for (int i = 0; i < buff.Length; i++) { if (buff[i] != verify[i]) { result = MFTestResults.Fail; Log.Comment("Position " + i + ":" + buff[i] + " != " + verify[i]); } } } } catch (Exception ex) { Log.Exception("Unexpected exception: " + ex.Message); result = MFTestResults.Fail; } return(result); }
public MFTestResults FileMode_Truncate() { // Clean up incase file exists if (File.Exists(fileName)) { File.Delete(fileName); } MFTestResults result = MFTestResults.Pass; try { // From MSDN: // Specifies that the operating system should open an existing file. // Once opened, the file should be truncated so that its size is zero // bytes. This requires FileIOPermissionAccess..::.Write. Attempts to // read from a file opened with Truncate cause an exception. try { Log.Comment("Non-existent file truncate"); using (FileStream fs = new FileStream(fileName, FileMode.Truncate)) { } result = MFTestResults.Fail; Log.Exception("Expected IOException"); } catch (IOException) { /* pass case */ } Log.Comment("Create a new file and populate"); using (FileStream fs = new FileStream(fileName, FileMode.CreateNew)) { if (!FileStreamHelper.WriteReadVerify(fs)) { result = MFTestResults.Fail; } // add extra bytes, to make sure we really truncate FileStreamHelper.Write(fs, 50); } Log.Comment("Add Attribute, to verify Truncate behavior"); File.SetAttributes(fileName, FileAttributes.Hidden); Log.Comment("Open Truncate"); using (FileStream fs = new FileStream(fileName, FileMode.Truncate)) { if (!FileStreamHelper.WriteReadEmpty(fs)) { result = MFTestResults.Fail; } Log.Comment("Trying to read file while its open Truncate should fail"); try { using (FileStream fs2 = new FileStream(fileName, FileMode.Open, FileAccess.Read)) {} result = MFTestResults.Fail; Log.Exception("Unexpectedly able to read file"); } catch (IOException) { /* pass case */ } } Log.Comment("Verify hidden property is still set"); FileAttributes fa = File.GetAttributes(fileName); if ((fa & FileAttributes.Hidden) == 0) { result = MFTestResults.Fail; Log.Exception("Expected hidden attribute, but got " + fa); } if (!FileStreamHelper.VerifyFile(fileName, 300)) { result = MFTestResults.Fail; } } catch (Exception ex) { Log.Exception("Unexpected exception: " + ex.Message); result = MFTestResults.Fail; } return(result); }
public MFTestResults FileMode_Create() { // Clean up incase file exists if (File.Exists(fileName)) { File.Delete(fileName); } MFTestResults result = MFTestResults.Pass; try { // From MSDN: // Specifies that the operating system should create a new file. // If the file already exists, it will be overwritten. This requires // FileIOPermissionAccess..::.Write. System.IO.FileMode.Create is // equivalent to requesting that if the file does not exist, use // CreateNew; otherwise, use Truncate. Log.Comment("Non-existent file, should create file"); using (FileStream fs = new FileStream(fileName, FileMode.Create)) { if (!FileStreamHelper.WriteReadEmpty(fs)) { result = MFTestResults.Fail; } // add additional 50 bytes, to test that truncation really happens FileStreamHelper.Write(fs, 50); } Log.Comment("Add Attribute, to verify Truncate behavior"); File.SetAttributes(fileName, FileAttributes.Hidden); Log.Comment("Existing file, should treat as truncate"); using (FileStream fs = new FileStream(fileName, FileMode.Create)) { if (!FileStreamHelper.Write(fs, 300)) { result = MFTestResults.Fail; } Log.Comment("Trying to read file while its open Truncate should fail"); try { using (FileStream fs2 = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { } result = MFTestResults.Fail; Log.Exception("Unexpectedly able to read file"); } catch (IOException) { /* pass case */ } } Log.Comment("Verify hidden property is still set"); FileAttributes fa = File.GetAttributes(fileName); if ((fa & FileAttributes.Hidden) == 0) { result = MFTestResults.Fail; Log.Exception("Expected hidden attribute, but got " + fa); } // Verify the 300 bytes we wrote (not original 350) if (!FileStreamHelper.VerifyFile(fileName, 300)) { result = MFTestResults.Fail; } } catch (Exception ex) { Log.Exception("Unexpected exception: " + ex.Message); result = MFTestResults.Fail; } return(result); }
public MFTestResults FileShare_Write() { // Clean up incase file exists if (File.Exists(fileName)) { File.Delete(fileName); } MFTestResults result = MFTestResults.Pass; try { // From MSDN: // Allows subsequent opening of the file for writing. If this flag // is not specified, any request to open the file for writing (by // this process or another process) will fail until the file is // closed. However, even if this flag is specified, additional // permissions might still be needed to access the file. // 300 bytes Log.Comment("Create file for testing"); using (FileStream fs = new FileStream(fileName, FileMode.CreateNew)) { FileStreamHelper.WriteReadEmpty(fs); } using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.Write)) { // 1000 bytes Log.Comment("Should be able to write"); if (!FileStreamHelper.Write(fs, 1000)) { result = MFTestResults.Fail; } Log.Comment("Should be able to read"); fs.Seek(0, SeekOrigin.Begin); if (!FileStreamHelper.VerifyRead(fs)) { result = MFTestResults.Fail; } Log.Comment("Try to Read in same thread"); try { using (FileStream fs2 = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { } result = MFTestResults.Fail; Log.Exception("FileAccess.Read - Expected IOException"); } catch (IOException) { /* pass case */ } Log.Comment("Try to Read in another thread"); restricted = true; threadResult = true; Thread worker = new Thread(OpenRead); worker.Start(); worker.Join(); if (!threadResult) { result = MFTestResults.Fail; } // 500 bytes Log.Comment("Try to Write in same thread"); using (FileStream fs2 = new FileStream(fileName, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)) { FileStreamHelper.Write(fs2, 500); } // 200 bytes Log.Comment("Try to Write in another thread"); restricted = false; threadResult = true; worker = new Thread(OpenWrite); worker.Start(); worker.Join(); if (!threadResult) { result = MFTestResults.Fail; } // 500 bytes Log.Comment("Write after read other access attempt"); fs.Seek(0, SeekOrigin.End); if (!FileStreamHelper.Write(fs, 500)) { result = MFTestResults.Fail; } } // 300 + 1000 + 500 + 200 500 = 2500 Log.Comment("Verify file after close"); if (!FileStreamHelper.VerifyFile(fileName, 2500)) { result = MFTestResults.Fail; } } catch (Exception ex) { Log.Exception("Unexpected exception: " + ex.Message); result = MFTestResults.Fail; } return(result); }
public MFTestResults FileShare_Read() { // Clean up incase file exists if (File.Exists(fileName)) { File.Delete(fileName); } MFTestResults result = MFTestResults.Pass; try { // From MSDN: // Declines sharing of the current file. Any request to open the file // (by this process or another process) will fail until the file is closed. Log.Comment("Create file for testing"); using (FileStream fs = new FileStream(fileName, FileMode.CreateNew)) { FileStreamHelper.WriteReadEmpty(fs); } using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) { Log.Comment("Should be able to write"); if (!FileStreamHelper.Write(fs, 1000)) { result = MFTestResults.Fail; } Log.Comment("Should be able to read"); fs.Seek(0, SeekOrigin.Begin); if (!FileStreamHelper.VerifyRead(fs)) { result = MFTestResults.Fail; } Log.Comment("Try to Read in same thread"); if (!FileStreamHelper.VerifyFile(fileName)) { result = MFTestResults.Fail; } Log.Comment("Try to Read in another thread"); restricted = false; threadResult = true; Thread worker = new Thread(OpenRead); worker.Start(); worker.Join(); if (!threadResult) { result = MFTestResults.Fail; } Log.Comment("Try to Write in same thread"); try { using (FileStream fs2 = new FileStream(fileName, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)) { } result = MFTestResults.Fail; Log.Exception("Expected IOException"); } catch (IOException) { /* pass case */ } Log.Comment("Try to Write in another thread"); restricted = true; threadResult = true; worker = new Thread(OpenWrite); worker.Start(); worker.Join(); if (!threadResult) { result = MFTestResults.Fail; } Log.Comment("Write after read other access attempt"); fs.Seek(0, SeekOrigin.End); if (!FileStreamHelper.Write(fs, 500)) { result = MFTestResults.Fail; } } Log.Comment("Verify file after close - 300 bytes original + 1000 bytes + 500 bytes"); if (!FileStreamHelper.VerifyFile(fileName, 1800)) { result = MFTestResults.Fail; } } catch (Exception ex) { Log.Exception("Unexpected exception: " + ex.Message); result = MFTestResults.Fail; } return(result); }