public MFTestResults FileAccess_ReadWrite() { // 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.ReadWrite)) { 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("Write after read"); fs.Seek(0, SeekOrigin.End); if (!FileStreamHelper.Write(fs, 500)) { result = MFTestResults.Fail; } } // 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 FileAccess_Read() { // Clean up incase file exists if (File.Exists(fileName)) { File.Delete(fileName); } MFTestResults result = MFTestResults.Pass; try { // From MSDN: // Read access to the file. Data can be read from the file. // Combine with Write 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.Read)) { Log.Comment("Should be able to read"); if (!FileStreamHelper.VerifyRead(fs)) { result = MFTestResults.Fail; } Log.Comment("Shouldn't be able to write"); try { fs.Write(new byte[] { 1, 2, 3 }, 0, 3); result = MFTestResults.Fail; Log.Exception("Expected NotSupportedException"); } catch (NotSupportedException) { /* pass case */ } } } catch (Exception ex) { Log.Exception("Unexpected exception: " + ex.Message); result = MFTestResults.Fail; } return(result); }
public MFTestResults Flush_Write() { Log.Comment("This test case requires the previous test case to write a properly formatted 5k file"); if (!FileStreamHelper.VerifyFile(sourceFile, 5000)) { return(MFTestResults.Skip); } MFTestResults result = MFTestResults.Pass; try { // read bytes from source file byte[] data = File.ReadAllBytes(sourceFile); using (FileStream fs = new FileStream(fileName, FileMode.CreateNew)) { fs.Write(data, 0, data.Length); 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 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 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); }
public MFTestResults VanillaWrite() { if (File.Exists(fileName)) { File.Delete(fileName); } MFTestResults result = MFTestResults.Pass; try { using (FileStream fs = new FileStream(fileName, FileMode.CreateNew)) { Log.Comment("Write 256 bytes of data"); if (!TestWrite(fs, 256)) { result = MFTestResults.Fail; } Log.Comment("Write middle of buffer"); if (!TestWrite(fs, 256, 100, 100)) { result = MFTestResults.Fail; } // 1000 - 256 - 100 = 644 Log.Comment("Write start of buffer"); if (!TestWrite(fs, 1000, 644, 0)) { result = MFTestResults.Fail; } Log.Comment("Write end of buffer"); if (!TestWrite(fs, 1000, 900, 100)) { result = MFTestResults.Fail; } Log.Comment("Rewind and verify all bytes written"); fs.Seek(0, SeekOrigin.Begin); if (!FileStreamHelper.VerifyRead(fs)) { 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); }