diff options
author | Marek Safar <marek.safar@gmail.com> | 2017-10-02 15:46:59 +0300 |
---|---|---|
committer | Marek Safar <marek.safar@gmail.com> | 2017-10-02 15:46:59 +0300 |
commit | 524e42629b39f500fb8ad5da467ece5256c38f31 (patch) | |
tree | b60c4f5fdecb34aec2132f6338efed00179fafce /src/System.IO.FileSystem/tests | |
parent | eed24cf38a93e59638405a4c92f610dca02be1ec (diff) | |
parent | 1525832151f05d790d7cd311ec87597e77fbf9fd (diff) |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'src/System.IO.FileSystem/tests')
7 files changed, 160 insertions, 214 deletions
diff --git a/src/System.IO.FileSystem/tests/Directory/Delete.cs b/src/System.IO.FileSystem/tests/Directory/Delete.cs index a2adcf855a..b7b9cb7176 100644 --- a/src/System.IO.FileSystem/tests/Directory/Delete.cs +++ b/src/System.IO.FileSystem/tests/Directory/Delete.cs @@ -208,6 +208,9 @@ namespace System.IO.Tests [Trait(XunitConstants.Category, XunitConstants.RequiresElevation)] public void Unix_NotFoundDirectory_ReadOnlyVolume() { + if (PlatformDetection.IsRedHat69) + return; // [ActiveIssue(https://github.com/dotnet/corefx/issues/21920)] + ReadOnly_FileSystemHelper(readOnlyDirectory => { Assert.Throws<DirectoryNotFoundException>(() => Delete(Path.Combine(readOnlyDirectory, "DoesNotExist"))); @@ -251,8 +254,10 @@ namespace System.IO.Tests } [Fact] + [ActiveIssue(24242)] [PlatformSpecific(TestPlatforms.Windows)] [OuterLoop("This test is very slow.")] + [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "Desktop does not have the fix for #22596")] public void RecursiveDelete_DeepNesting() { // Create a 2000 level deep directory and recursively delete from the root. diff --git a/src/System.IO.FileSystem/tests/File/Delete.cs b/src/System.IO.FileSystem/tests/File/Delete.cs index 2c74a91b33..f6f7ade21f 100644 --- a/src/System.IO.FileSystem/tests/File/Delete.cs +++ b/src/System.IO.FileSystem/tests/File/Delete.cs @@ -129,6 +129,9 @@ namespace System.IO.Tests [Trait(XunitConstants.Category, XunitConstants.RequiresElevation)] public void Unix_NonExistentPath_ReadOnlyVolume() { + if (PlatformDetection.IsRedHat69) + return; // [ActiveIssue(https://github.com/dotnet/corefx/issues/21920)] + ReadOnly_FileSystemHelper(readOnlyDirectory => { Delete(Path.Combine(readOnlyDirectory, "DoesNotExist")); @@ -141,6 +144,9 @@ namespace System.IO.Tests [Trait(XunitConstants.Category, XunitConstants.RequiresElevation)] public void Unix_ExistingDirectory_ReadOnlyVolume() { + if (PlatformDetection.IsRedHat69) + return; // [ActiveIssue(https://github.com/dotnet/corefx/issues/21920)] + ReadOnly_FileSystemHelper(readOnlyDirectory => { Assert.Throws<IOException>(() => Delete(Path.Combine(readOnlyDirectory, "subdir"))); diff --git a/src/System.IO.FileSystem/tests/FileStream/ReadWriteSpan.netcoreapp.cs b/src/System.IO.FileSystem/tests/FileStream/ReadWriteSpan.netcoreapp.cs index 3a67c5c9d0..272b19769e 100644 --- a/src/System.IO.FileSystem/tests/FileStream/ReadWriteSpan.netcoreapp.cs +++ b/src/System.IO.FileSystem/tests/FileStream/ReadWriteSpan.netcoreapp.cs @@ -3,6 +3,8 @@ // See the LICENSE file in the project root for more information. using System.Linq; +using System.Threading; +using System.Threading.Tasks; using Xunit; namespace System.IO.Tests @@ -123,6 +125,119 @@ namespace System.IO.Tests Assert.Equal(TestBuffer, buffer); } } + + [Fact] + public void DisposedStream_ReadWriteAsync_Throws() + { + var fs = CreateFileStream(GetTestFilePath(), FileMode.Create); + fs.Dispose(); + Assert.Throws<ObjectDisposedException>(() => { fs.ReadAsync(new Memory<byte>(new byte[1])); }); + Assert.Throws<ObjectDisposedException>(() => { fs.WriteAsync(new ReadOnlyMemory<byte>(new byte[1])); }); + } + + [Fact] + public async Task EmptyFile_ReadAsync_Succeeds() + { + using (var fs = CreateFileStream(GetTestFilePath(), FileMode.Create)) + { + // use a recognizable pattern + var buffer = (byte[])TestBuffer.Clone(); + + Assert.Equal(0, await fs.ReadAsync(Memory<byte>.Empty)); + Assert.Equal(0, await fs.ReadAsync(new Memory<byte>(buffer, 0, 1))); + Assert.Equal(TestBuffer, buffer); + + Assert.Equal(0, await fs.ReadAsync(new Memory<byte>(buffer, 0, buffer.Length))); + Assert.Equal(TestBuffer, buffer); + + Assert.Equal(0, await fs.ReadAsync(new Memory<byte>(buffer, buffer.Length - 1, 1))); + Assert.Equal(TestBuffer, buffer); + + Assert.Equal(0, await fs.ReadAsync(new Memory<byte>(buffer, buffer.Length / 2, buffer.Length - buffer.Length / 2))); + Assert.Equal(TestBuffer, buffer); + } + } + + [Fact] + public async Task NonEmptyFile_ReadAsync_GetsExpectedData() + { + string fileName = GetTestFilePath(); + File.WriteAllBytes(fileName, TestBuffer); + + using (var fs = CreateFileStream(fileName, FileMode.Open)) + { + var buffer = new byte[TestBuffer.Length]; + Assert.Equal(TestBuffer.Length, await fs.ReadAsync(new Memory<byte>(buffer, 0, buffer.Length))); + Assert.Equal(TestBuffer, buffer); + + // Larger than needed buffer, read into beginning, rest remains untouched + fs.Position = 0; + buffer = new byte[TestBuffer.Length * 2]; + Assert.Equal(TestBuffer.Length, await fs.ReadAsync(new Memory<byte>(buffer))); + Assert.Equal(TestBuffer, buffer.Take(TestBuffer.Length)); + Assert.Equal(new byte[buffer.Length - TestBuffer.Length], buffer.Skip(TestBuffer.Length)); + + // Larger than needed buffer, read into middle, beginning and end remain untouched + fs.Position = 0; + buffer = new byte[TestBuffer.Length * 2]; + Assert.Equal(TestBuffer.Length, await fs.ReadAsync(new Memory<byte>(buffer, 2, buffer.Length - 2))); + Assert.Equal(TestBuffer, buffer.Skip(2).Take(TestBuffer.Length)); + Assert.Equal(new byte[2], buffer.Take(2)); + Assert.Equal(new byte[buffer.Length - TestBuffer.Length - 2], buffer.Skip(2 + TestBuffer.Length)); + } + } + + [Fact] + public void ReadOnly_WriteAsync_Throws() + { + string fileName = GetTestFilePath(); + File.WriteAllBytes(fileName, TestBuffer); + + using (var fs = CreateFileStream(fileName, FileMode.Open, FileAccess.Read)) + { + Assert.Throws<NotSupportedException>(() => { fs.WriteAsync(new ReadOnlyMemory<byte>(new byte[1])); }); + fs.Dispose(); + Assert.Throws<ObjectDisposedException>(() => { fs.WriteAsync(new ReadOnlyMemory<byte>(new byte[1])); }); // Disposed checking happens first + } + } + + [Fact] + public void WriteOnly_ReadAsync_Throws() + { + using (var fs = CreateFileStream(GetTestFilePath(), FileMode.Create, FileAccess.Write)) + { + Assert.Throws<NotSupportedException>(() => { fs.ReadAsync(new Memory<byte>(new byte[1])); }); + fs.Dispose(); + Assert.Throws<ObjectDisposedException>(() => { fs.ReadAsync(new Memory<byte>(new byte[1])); });// Disposed checking happens first + } + } + + [Fact] + public async Task EmptyWriteAsync_NoDataWritten() + { + using (var fs = CreateFileStream(GetTestFilePath(), FileMode.Create)) + { + await fs.WriteAsync(Memory<byte>.Empty); + Assert.Equal(0, fs.Length); + Assert.Equal(0, fs.Position); + } + } + + [Fact] + public async Task NonEmptyWriteAsync_WritesExpectedData() + { + using (var fs = CreateFileStream(GetTestFilePath(), FileMode.Create)) + { + await fs.WriteAsync(new Memory<byte>(TestBuffer)); + Assert.Equal(TestBuffer.Length, fs.Length); + Assert.Equal(TestBuffer.Length, fs.Position); + + fs.Position = 0; + var buffer = new byte[TestBuffer.Length]; + Assert.Equal(TestBuffer.Length, await fs.ReadAsync(new Memory<byte>(buffer))); + Assert.Equal(TestBuffer, buffer); + } + } } public class Sync_FileStream_ReadWrite_Span : FileStream_ReadWrite_Span @@ -160,6 +275,25 @@ namespace System.IO.Tests Assert.True(fs.ReadArrayInvoked); } } + + [Fact] + public async Task CallMemoryReadWriteAsyncOnDerivedFileStream_ArrayMethodsUsed() + { + using (var fs = (DerivedFileStream)CreateFileStream(GetTestFilePath(), FileMode.Create, FileAccess.ReadWrite)) + { + Assert.False(fs.WriteAsyncArrayInvoked); + Assert.False(fs.ReadAsyncArrayInvoked); + + await fs.WriteAsync(new ReadOnlyMemory<byte>(new byte[1])); + Assert.True(fs.WriteAsyncArrayInvoked); + Assert.False(fs.ReadAsyncArrayInvoked); + + fs.Position = 0; + await fs.ReadAsync(new Memory<byte>(new byte[1])); + Assert.True(fs.WriteAsyncArrayInvoked); + Assert.True(fs.ReadAsyncArrayInvoked); + } + } } public sealed class Async_DerivedFileStream_ReadWrite_Span : Async_FileStream_ReadWrite_Span @@ -171,6 +305,7 @@ namespace System.IO.Tests internal sealed class DerivedFileStream : FileStream { public bool ReadArrayInvoked = false, WriteArrayInvoked = false; + public bool ReadAsyncArrayInvoked = false, WriteAsyncArrayInvoked = false; public DerivedFileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options) : base(path, mode, access, share, bufferSize, options) @@ -188,5 +323,17 @@ namespace System.IO.Tests WriteArrayInvoked = true; base.Write(array, offset, count); } + + public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) + { + ReadAsyncArrayInvoked = true; + return base.ReadAsync(buffer, offset, count, cancellationToken); + } + + public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) + { + WriteAsyncArrayInvoked = true; + return base.WriteAsync(buffer, offset, count, cancellationToken); + } } } diff --git a/src/System.IO.FileSystem/tests/FileSystemTest.cs b/src/System.IO.FileSystem/tests/FileSystemTest.cs index 7fcae98914..4e593f27b8 100644 --- a/src/System.IO.FileSystem/tests/FileSystemTest.cs +++ b/src/System.IO.FileSystem/tests/FileSystemTest.cs @@ -61,7 +61,7 @@ namespace System.IO.Tests protected string GetNamedPipeServerStreamName() { - if (PlatformDetection.IsWinRT) + if (PlatformDetection.IsInAppContainer) { return @"LOCAL\" + Guid.NewGuid().ToString("N"); } diff --git a/src/System.IO.FileSystem/tests/PortedCommon/IOServices.cs b/src/System.IO.FileSystem/tests/PortedCommon/IOServices.cs index 41d8b5f1fe..6b45e65b5a 100644 --- a/src/System.IO.FileSystem/tests/PortedCommon/IOServices.cs +++ b/src/System.IO.FileSystem/tests/PortedCommon/IOServices.cs @@ -228,7 +228,7 @@ internal class IOServices public static bool IsDriveNTFS(string drive) { - if (PlatformDetection.IsWinRT) + if (PlatformDetection.IsInAppContainer) { // we cannot determine filesystem so assume NTFS return true; diff --git a/src/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj b/src/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj index 6a75704f5a..1ecc4c4585 100644 --- a/src/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj +++ b/src/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj @@ -54,9 +54,6 @@ <Compile Include="File\ReadWriteAllTextAsync.cs" /> <Compile Include="FileStream\ReadWriteSpan.netcoreapp.cs" /> </ItemGroup> - <ItemGroup Condition="'$(TargetGroup)' == 'uapaot' or '$(TargetGroup)' == 'uap'"> - <Compile Include="WinRT_BrokeredFunctions.cs" /> - </ItemGroup> <ItemGroup> <!-- Rewritten --> <Compile Include="DirectoryInfo\GetSetAttributes.cs" /> diff --git a/src/System.IO.FileSystem/tests/WinRT_BrokeredFunctions.cs b/src/System.IO.FileSystem/tests/WinRT_BrokeredFunctions.cs deleted file mode 100644 index cb20e27349..0000000000 --- a/src/System.IO.FileSystem/tests/WinRT_BrokeredFunctions.cs +++ /dev/null @@ -1,209 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Collections.Generic; -using System.Linq; -using System.Runtime.InteropServices; -using Xunit; -using Windows.Storage; - -namespace System.IO.Tests -{ - [PlatformSpecific(TestPlatforms.Windows)] - [SkipOnTargetFramework(~(TargetFrameworkMonikers.Uap | TargetFrameworkMonikers.UapAot))] - public partial class WinRT_BrokeredFunctions : FileSystemTest - { - private static string s_musicFolder = StorageLibrary.GetLibraryAsync(KnownLibraryId.Music).AsTask().Result.SaveFolder.Path; - - [Fact] - public void CopyFile_ToBrokeredLocation() - { - string testFile = GetTestFilePath(); - File.Create(testFile).Dispose(); - - string destination = Path.Combine(s_musicFolder, "CopyToBrokeredLocation_" + Path.GetRandomFileName()); - try - { - Assert.False(File.Exists(destination), "destination shouldn't exist before copying"); - File.Copy(testFile, destination); - Assert.True(File.Exists(testFile), "testFile should exist after copying"); - Assert.True(File.Exists(destination), "destination should exist after copying"); - } - finally - { - File.Delete(destination); - } - } - - [Fact] - public void CreateDirectory() - { - string testFolder = Path.Combine(s_musicFolder, "CreateDirectory_" + Path.GetRandomFileName()); - try - { - Assert.False(Directory.Exists(testFolder), "destination shouldn't exist"); - Directory.CreateDirectory(testFolder); - Assert.True(Directory.Exists(testFolder), "destination should exist"); - } - finally - { - Directory.Delete(testFolder); - } - } - - [Fact] - public void DeleteFile() - { - string testFile = Path.Combine(s_musicFolder, "DeleteFile_" + Path.GetRandomFileName()); - CreateFileInBrokeredLocation(testFile); - Assert.True(File.Exists(testFile), "testFile should exist before deleting"); - File.Delete(testFile); - Assert.False(File.Exists(testFile), "testFile shouldn't exist after deleting"); - } - - [Fact] - public void FindFirstFile() - { - string subFolder = Path.Combine(s_musicFolder, "FindFirstFile_SubFolder_" + Path.GetRandomFileName()); - Directory.CreateDirectory(subFolder); - string testFile = null; - - try - { - testFile = Path.Combine(subFolder, "FindFirstFile_SubFile_" + Path.GetRandomFileName()); - CreateFileInBrokeredLocation(testFile); - Assert.True(File.Exists(testFile), "testFile should exist"); - } - finally - { - Directory.Delete(subFolder, true); - } - Assert.False(File.Exists(testFile), "testFile shouldn't exist after a recursive delete"); - Assert.False(Directory.Exists(subFolder), "subFolder shouldn't exist after a recursive delete"); - } - - [Fact] - [ActiveIssue(23444)] - public void GetFileAttributesEx() - { - string destination = Path.Combine(s_musicFolder, "GetFileAttributesEx_" + Path.GetRandomFileName()); - CreateFileInBrokeredLocation(destination); - try - { - FileAttributes attr = File.GetAttributes(destination); - Assert.False((attr & FileAttributes.ReadOnly) == 0, "new file in brokered location should not be readonly"); - } - finally - { - File.Delete(destination); - } - } - - [Fact] - public void MoveFile() - { - string testFile = GetTestFilePath(); - File.Create(testFile).Dispose(); - - string destination = Path.Combine(s_musicFolder, "MoveFile_" + Path.GetRandomFileName()); - try - { - Assert.False(File.Exists(destination), "destination shouldn't exist before moving"); - File.Move(testFile, destination); - Assert.False(File.Exists(testFile), "testFile shouldn't exist after moving"); - Assert.True(File.Exists(destination), "destination should exist after moving"); - } - finally - { - File.Delete(destination); - } - } - - [Fact] - public void RemoveDirectory() - { - string testFolder = Path.Combine(s_musicFolder, "CreateDirectory_" + Path.GetRandomFileName()); - Assert.False(Directory.Exists(testFolder), "destination shouldn't exist"); - Directory.CreateDirectory(testFolder); - Assert.True(Directory.Exists(testFolder), "destination should exist"); - Directory.Delete(testFolder); - Assert.False(Directory.Exists(testFolder), "destination shouldn't exist"); - } - - [Fact] - public void ReplaceFile() - { - string testFile = GetTestFilePath(); - File.Create(testFile).Dispose(); - - string destination = Path.Combine(s_musicFolder, "ReplaceFile_" + Path.GetRandomFileName()); - File.Copy(testFile, destination); - - // Need to be on the same drive - Assert.Equal(testFile[0], destination[0]); - - string destinationBackup = Path.Combine(s_musicFolder, "ReplaceFile_" + Path.GetRandomFileName()); - try - { - Assert.True(File.Exists(destination), "destination should exist before replacing"); - Assert.False(File.Exists(destinationBackup), "destination shouldn't exist before replacing"); - File.Replace(testFile, destination, destinationBackup); - Assert.False(File.Exists(testFile), "testFile shouldn't exist after replacing"); - Assert.True(File.Exists(destination), "destination should exist after replacing"); - Assert.True(File.Exists(destinationBackup), "destinationBackup should exist after replacing"); - } - finally - { - File.Delete(destination); - File.Delete(destinationBackup); - } - } - - [Fact] - [ActiveIssue(23444)] - public void SetFileAttributes() - { - string destination = Path.Combine(s_musicFolder, "SetFileAttributes_" + Path.GetRandomFileName()); - CreateFileInBrokeredLocation(destination); - FileAttributes attr = File.GetAttributes(destination); - try - { - Assert.False(((attr & FileAttributes.ReadOnly) > 0), "new file in brokered location should not be readonly"); - File.SetAttributes(destination, attr | FileAttributes.ReadOnly); - Assert.True(((File.GetAttributes(destination) & FileAttributes.ReadOnly) > 0), "file in brokered location should be readonly after setting FileAttributes"); - } - finally - { - File.SetAttributes(destination, attr); - Assert.False(((File.GetAttributes(destination) & FileAttributes.ReadOnly) > 0), "file in brokered location should NOT be readonly after setting FileAttributes"); - File.Delete(destination); - } - } - - private void CreateFileInBrokeredLocation(string path) - { - // Temporary hack until FileStream is updated to support brokering - string testFile = GetTestFilePath(); - File.WriteAllText(testFile, "CoreFX test file"); - File.Copy(testFile, path); - } - - // Temporarily blocking until the CoreCLR change is made [Fact] - public void WriteReadAllText() - { - string destination = Path.Combine(s_musicFolder, "WriteReadAllText_" + Path.GetRandomFileName()); - string content = "WriteReadAllText"; - File.WriteAllText(destination, content); - try - { - Assert.True(File.Exists(destination)); - Assert.Equal(content, File.ReadAllText(destination)); - } - finally - { - File.Delete(destination); - } - } - } -} |