More file stream options tests (#53982)
authorAdam Sitnik <adam.sitnik@gmail.com>
Sat, 26 Jun 2021 21:20:32 +0000 (23:20 +0200)
committerGitHub <noreply@github.com>
Sat, 26 Jun 2021 21:20:32 +0000 (17:20 -0400)
* 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.

src/libraries/System.IO.FileSystem/tests/FileStream/FileStreamOptions.cs
src/libraries/System.IO/tests/StreamReader/StreamReader.StringCtorTests.cs
src/libraries/System.IO/tests/StreamWriter/StreamWriter.StringCtorTests.cs
src/libraries/System.Private.CoreLib/src/System/IO/StreamReader.cs
src/libraries/System.Private.CoreLib/src/System/IO/StreamWriter.cs

index d078deb..9be492d 100644 (file)
@@ -1,8 +1,8 @@
 // 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 @@ namespace System.IO.Tests
 
             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);
+                }
+            }
+        }
     }
 }
index d4c7ac4..2e19dd2 100644 (file)
@@ -47,6 +47,16 @@ namespace System.IO.Tests
             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)]
index 95041cd..841ff59 100644 (file)
@@ -47,6 +47,15 @@ namespace System.IO.Tests
         }
 
         [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()
         {
             string testfile = Path.GetTempFileName();
index af35711..3b3e03a 100644 (file)
@@ -211,8 +211,15 @@ namespace System.IO
         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);
         }
index 96ac62f..7ba3626 100644 (file)
@@ -167,8 +167,15 @@ namespace System.IO
         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);
         }