public MFTestResults FileMode_OpenOrCreate()
        {
            // 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 a file if it exists; otherwise,
                // a new file should be created. If the file is opened with FileAccess.Read,
                // FileIOPermissionAccess..::.Read is required. If the file access is
                // FileAccess.Write then FileIOPermissionAccess..::.Write is required. If the file
                // is opened with FileAccess.ReadWrite, both FileIOPermissionAccess..::.Read and
                // FileIOPermissionAccess..::.Write are required. If the file access is
                // FileAccess.Append, then FileIOPermissionAccess..::.Append is required.

                Log.Comment("Non-existent file, should create file");
                using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate))
                {
                    if (!FileStreamHelper.WriteReadEmpty(fs))
                    {
                        result = MFTestResults.Fail;
                    }
                }

                Log.Comment("Open existing file");
                using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate))
                {
                    if (!FileStreamHelper.WriteReadVerify(fs))
                    {
                        result = MFTestResults.Fail;
                    }
                }

                if (!FileStreamHelper.VerifyFile(fileName, 600))
                {
                    result = MFTestResults.Fail;
                }
            }
            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 FileShare_ReadWrite()
        {
            // 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 reading or writing. If this
                // flag is not specified, any request to open the file for reading or
                // 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.ReadWrite))
                {
                    // 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");
                    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;
                    }

                    // 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;
                    }

                    // 300 bytes
                    Log.Comment("Try to ReadWrite");
                    using (FileStream fs2 = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                    {
                        if (!FileStreamHelper.WriteReadVerify(fs2))
                        {
                            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 + 300 + 500 = 2800
                Log.Comment("Verify file after close");
                if (!FileStreamHelper.VerifyFile(fileName, 2800))
                {
                    result = MFTestResults.Fail;
                }
            }
            catch (Exception ex)
            {
                Log.Exception("Unexpected exception: " + ex.Message);
                result = MFTestResults.Fail;
            }

            return(result);
        }