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 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_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 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 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);
     }
 }
Exemplo n.º 7
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);
        }
 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);
     }
 }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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);
        }
        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);
        }
Exemplo n.º 12
0
        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);
        }
Exemplo n.º 13
0
        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);
        }
Exemplo n.º 14
0
        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);
        }
Exemplo n.º 15
0
        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);
        }
Exemplo n.º 16
0
        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_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);
        }
Exemplo n.º 18
0
        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);
        }
Exemplo n.º 19
0
        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);
        }