未验证 提交 2abd4878 编写于 作者: A Adam Sitnik 提交者: GitHub

More file stream options tests (#53982)

* add more tests

* StreamWriter and StreamReader require FileStreamOptions with necessary access

* allow for bufferSize == 0

* extend the test

* refactor the tests

* Revert "allow for bufferSize == 0"

This reverts commit f1259be18a3446c1b8939883f484cc28347c74cf.
上级 aca3754a
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
namespace System.IO.Tests
......@@ -134,5 +134,59 @@ public void BufferSize()
Assert.Throws<ArgumentOutOfRangeException>(() => new FileStreamOptions { BufferSize = -1 });
}
[Theory]
[InlineData(FileMode.Create, FileAccess.Write, FileOptions.None)]
[InlineData(FileMode.Create, FileAccess.Write, FileOptions.Asynchronous)]
[InlineData(FileMode.Open, FileAccess.Read, FileOptions.None)]
[InlineData(FileMode.Open, FileAccess.Read, FileOptions.Asynchronous)]
[InlineData(FileMode.Create, FileAccess.ReadWrite, FileOptions.None)]
[InlineData(FileMode.Create, FileAccess.ReadWrite, FileOptions.Asynchronous)]
public void SettingsArePropagated(FileMode mode, FileAccess access, FileOptions fileOptions)
{
string filePath = GetTestFilePath();
if (mode == FileMode.Open)
{
File.Create(filePath).Dispose();
}
bool canRead = (access & FileAccess.Read) != 0;
bool canWrite = (access & FileAccess.Write) != 0;
bool isAsync = (fileOptions & FileOptions.Asynchronous) != 0;
var options = new FileStreamOptions
{
Mode = mode,
Access = access,
Options = fileOptions
};
Validate(new FileStream(filePath, options), filePath, isAsync, canRead, canWrite);
Validate(File.Open(filePath, options), filePath, isAsync, canRead, canWrite);
Validate(new FileInfo(filePath).Open(options), filePath, isAsync, canRead, canWrite);
if (canWrite)
{
Validate((FileStream)new StreamWriter(filePath, options).BaseStream, filePath, isAsync, canRead, canWrite);
Validate((FileStream)new StreamWriter(filePath, Encoding.UTF8, options).BaseStream, filePath, isAsync, canRead, canWrite);
}
if (canRead)
{
Validate((FileStream)new StreamReader(filePath, options).BaseStream, filePath, isAsync, canRead, canWrite);
Validate((FileStream)new StreamReader(filePath, Encoding.UTF8, false, options).BaseStream, filePath, isAsync, canRead, canWrite);
}
static void Validate(FileStream fs, string expectedPath, bool expectedAsync, bool expectedCanRead, bool expectedCanWrite)
{
using (fs)
{
Assert.Equal(expectedPath, fs.Name);
Assert.Equal(expectedAsync, fs.IsAsync);
Assert.Equal(expectedCanRead, fs.CanRead);
Assert.Equal(expectedCanWrite, fs.CanWrite);
}
}
}
}
}
......@@ -47,6 +47,16 @@ public static void NegativeBufferSize_ThrowsArgumentOutOfRangeException()
AssertExtensions.Throws<ArgumentOutOfRangeException>("bufferSize", () => new StreamReader("path", Encoding.UTF8, true, 0));
}
[Fact]
public static void LackOfReadAccess_ThrowsArgumentException()
{
var noReadAccess = new FileStreamOptions { Access = FileAccess.Write };
AssertExtensions.Throws<ArgumentException>("options", () => new StreamReader("path", noReadAccess));
AssertExtensions.Throws<ArgumentException>("options", () => new StreamReader("path", Encoding.UTF8, false, noReadAccess));
AssertExtensions.Throws<ArgumentException>("options", () => new StreamReader("path", Encoding.UTF8, true, noReadAccess));
}
[Theory]
[InlineData(true)]
[InlineData(false)]
......
......@@ -46,6 +46,15 @@ public static void NegativeBufferSize_ThrowsArgumentOutOfRangeException()
AssertExtensions.Throws<ArgumentOutOfRangeException>("bufferSize", () => new StreamWriter("path", true, Encoding.UTF8, 0));
}
[Fact]
public static void LackOfWriteAccess_ThrowsArgumentException()
{
var readOptions = new FileStreamOptions { Access = FileAccess.Read };
AssertExtensions.Throws<ArgumentException>("options", () => new StreamWriter("path", readOptions));
AssertExtensions.Throws<ArgumentException>("options", () => new StreamWriter("path", Encoding.UTF8, readOptions));
}
[Fact]
public static void CreateStreamWriter()
{
......
......@@ -211,8 +211,15 @@ public StreamReader(string path, Encoding encoding, bool detectEncodingFromByteO
private static Stream ValidateArgsAndOpenPath(string path, Encoding encoding, FileStreamOptions options)
{
ValidateArgs(path, encoding);
if (options == null)
if (options is null)
{
throw new ArgumentNullException(nameof(options));
}
if ((options.Access & FileAccess.Read) == 0)
{
throw new ArgumentException(SR.Argument_StreamNotReadable, nameof(options));
}
return new FileStream(path, options);
}
......
......@@ -167,8 +167,15 @@ public StreamWriter(string path, Encoding encoding, FileStreamOptions options)
private static Stream ValidateArgsAndOpenPath(string path, Encoding encoding, FileStreamOptions options)
{
ValidateArgs(path, encoding);
if (options == null)
if (options is null)
{
throw new ArgumentNullException(nameof(options));
}
if ((options.Access & FileAccess.Write) == 0)
{
throw new ArgumentException(SR.Argument_StreamNotWritable, nameof(options));
}
return new FileStream(path, options);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册