Files
dotnet-runtime/src/libraries/System.IO.FileSystem/tests/FileStream/SafeFileHandle.cs
T
Stephen Toub cdf97700fa Fix System.IO.FileSystem product source and tests for Linux
Prior to this set of changes, 110 of the FileSystem tests were failing on Linux.  After this change, all allowed tests pass.

Primary product fixes:
- Using rmdir/unlink for directory/file deletion rather than using remove
- Disallowing FileStreams to be opened for directories
- Supporting copying files when only target directory (not target name) is specified
- Overhauling directory enumeration to ensure to match Win32 behaviors related to search pattern normalization, input validation, supporting re-iteration through an enumerable, outputting partial paths based on user-supplied path, using a SafeHandle to ensure resources are cleaned up
- Fixing FileSystemObject.LastWriteTime to correctly return the LastWriteTime rather than LastAccessTime
- Fixing FileAttributes.ReadOnly handling due to inappropriate caching
- Validating inputs to FileSystemObject.Attributes, and make sure that changes to attributes are visible afterwards
- Adding support for getting CreationTime if it's supported by the OS
- Marking links with FileAttributes.ReparsePoint
- Working around current runtime bug related to exception handling and finally blocks getting executed twice

Primary test fixes:
- Suppressing tests as platform-specific that dependend on Win32-isms like drive letters, alternate data streams, etc.
- Changing a bunch of '\' usage to instead use Path.DirectorySeparatorChar or Path.Combine
- Adapting to behavioral differences between Windows and Unix, e.g. whether a directory can be deleted if it's readonly, invalid path characters, max path lengths, file sharing differences, etc.

These changes should all accrue to OSX as well, but we'll need to separately do an exercise to clean this library and tests up there for anything OSX-specific.


Commit migrated from https://github.com/dotnet/corefx/commit/dd48c9dc1f0a7142a005ff1d4c162e10facda306
2015-04-10 13:13:34 -04:00

127 lines
4.3 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Win32.SafeHandles;
using System;
using System.IO;
using Xunit;
namespace System.IO.FileSystem.Tests
{
public class FileStream_SafeFileHandle : FileSystemTest
{
[Fact]
public void HandleNotNull()
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
Assert.NotNull(fs.SafeFileHandle);
}
}
[Fact]
public void DisposeClosesHandle()
{
using (FileStream fs = new FileStream(GetTestFilePath(), FileMode.Create))
{
SafeFileHandle handle = fs.SafeFileHandle;
if (!handle.IsInvalid)
{
fs.Dispose();
Assert.True(handle.IsClosed);
}
}
}
[Fact]
public void AccessFlushesFileClosesHandle()
{
string fileName = GetTestFilePath();
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete))
using (FileStream fsr = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete))
{
// write will be buffered
fs.Write(TestBuffer, 0, TestBuffer.Length);
// other handle doesn't yet see the change
Assert.Equal(0, fsr.Length);
SafeFileHandle handle = fs.SafeFileHandle;
// expect the handle to be flushed
Assert.Equal(TestBuffer.Length, fsr.Length);
}
}
[Fact]
public void ThrowWhenHandlePositionIsChanged()
{
string fileName = GetTestFilePath();
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
// write some data to move the position, flush to ensure OS position is updated
fs.Write(TestBuffer, 0, TestBuffer.Length);
fs.Flush();
if (fs.SafeFileHandle.IsInvalid)
{
// nothing to test
return;
}
using (FileStream fsr = new FileStream(fs.SafeFileHandle, FileAccess.Read, TestBuffer.Length))
{
Assert.Equal(TestBuffer.Length, fs.Position);
Assert.Equal(TestBuffer.Length, fsr.Position);
// Operations on original filestream will fail if data is in buffer and position changes.
// Put data in FS write buffer and update position from FSR
fs.WriteByte(0);
fsr.Position = 0;
Assert.Throws<IOException>(() => fs.Position);
fs.WriteByte(0);
fsr.Position++;
Assert.Throws<IOException>(() => fs.Read(new byte[1], 0, 1));
fs.WriteByte(0);
fsr.Position++;
if (Interop.IsWindows) // TODO: [ActiveIssue(812)]: Remove guard when async I/O is properly implemented on Unix
{
Assert.Throws<IOException>(() => FSAssert.CompletesSynchronously(fs.ReadAsync(new byte[1], 0, 1)));
}
else
{
Assert.ThrowsAsync<IOException>(() => fs.ReadAsync(new byte[1], 0, 1)).Wait();
}
fs.WriteByte(0);
fsr.Position++;
Assert.Throws<IOException>(() => fs.ReadByte());
fs.WriteByte(0);
fsr.Position++;
Assert.Throws<IOException>(() => fs.Seek(0, SeekOrigin.End));
fs.WriteByte(0);
fsr.Position++;
Assert.Throws<IOException>(() => fs.SetLength(2));
fs.WriteByte(0);
fsr.Position++;
Assert.Throws<IOException>(() => fs.Flush());
fs.WriteByte(0);
fsr.Position++;
Assert.Throws<IOException>(() => fs.Dispose());
}
}
}
}
}