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 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_Open()
        {
            // 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.
                // The ability to open the file is dependent on the value specified
                // by FileAccess. A System.IO..::.FileNotFoundException is thrown
                // if the file does not exist.

                try
                {
                    Log.Comment("Non-existent file open should throw");
                    using (FileStream fs = new FileStream(fileName, FileMode.Open)) { }
                    result = MFTestResults.Fail;
                    Log.Exception("Expected IOException");
                }
                catch (IOException) { /* pass case */ }

                // Create file
                File.Create(fileName).Close();

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

                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_CreateNew()
        {
            // 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.
                // This requires FileIOPermissionAccess..::.Write. If the file
                // already exists, an IOException is thrown.

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

                if (!FileStreamHelper.VerifyFile(fileName, 300))
                {
                    result = MFTestResults.Fail;
                }

                try
                {
                    Log.Comment("Exists file CreateNew");
                    using (FileStream fs = new FileStream(fileName, FileMode.CreateNew)) { }
                    result = MFTestResults.Fail;
                    Log.Exception("Expected IOException");
                }
                catch (IOException) { /* pass case */ }
            }
            catch (Exception ex)
            {
                Log.Exception("Unexpected exception: " + ex.Message);
                result = MFTestResults.Fail;
            }

            return(result);
        }
 private void OpenRead()
 {
     try
     {
         threadResult &= FileStreamHelper.VerifyFile(fileName);
     }
     catch (IOException)
     {
         if (!restricted)
         {
             threadResult = false;
             Log.Exception("Unexpected IOException");
         }
     }
     catch (Exception ex)
     {
         threadResult = false;
         Log.Exception("Unexpected Exception " + ex.Message);
     }
 }
示例#6
0
        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 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 FileMode_Append()
        {
            // Clean up incase file exists
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            MFTestResults result = MFTestResults.Pass;

            try
            {
                // From MSDN:
                // Opens the file if it exists and seeks to the end of the file,
                // or creates a new file. FileMode.Append can only be used in
                // conjunction with FileAccess.Write. Attempting to seek to a position
                // before the end of the file will throw an IOException and any attempt
                // to read fails and throws an NotSupportedException.

                Log.Comment("Non-existent file, should create file");
                using (FileStream fs = new FileStream(fileName, FileMode.Append))
                {
                    fs.WriteByte((byte)0);
                    fs.WriteByte((byte)1);
                    fs.WriteByte((byte)2);
                }

                Log.Comment("Existing file, should seek to end on open");
                using (FileStream fs = new FileStream(fileName, FileMode.Append))
                {
                    if (fs.Position != 3)
                    {
                        result = MFTestResults.Fail;
                        Log.Exception("Expected postion 3, but got " + fs.Position);
                    }

                    Log.Comment("Try to Seek before append");
                    try
                    {
                        fs.Seek(1, SeekOrigin.Begin);
                        result = MFTestResults.Fail;
                        Log.Exception("Unexpectedly able to seek");
                    }
                    catch (IOException) { /* pass case */ }

                    Log.Comment("Try to read");
                    try
                    {
                        byte[] buff = new byte[1];
                        fs.Read(buff, 0, buff.Length);
                        result = MFTestResults.Fail;
                        Log.Exception("Unexpectedly able to read");
                    }
                    catch (NotSupportedException) { /* pass case */ }

                    Log.Comment("append to file");
                    fs.WriteByte((byte)3);
                    fs.WriteByte((byte)4);
                    fs.WriteByte((byte)5);

                    Log.Comment("try to seek to start of append");
                    fs.Seek(-3, SeekOrigin.End);
                }

                if (!FileStreamHelper.VerifyFile(fileName, 6))
                {
                    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);
        }