Add Stream argument validation helpers and use throughout dotnet/runtime (#43968)
authorStephen Toub <stoub@microsoft.com>
Fri, 30 Oct 2020 09:50:49 +0000 (05:50 -0400)
committerGitHub <noreply@github.com>
Fri, 30 Oct 2020 09:50:49 +0000 (05:50 -0400)
commita112379a677ab36e0b417086ba74f7cc547118f3
tree0a1a74162ea443e1ef030c6bca3f24cdaeabf734
parentb5ea5cbe9fc2f57f4ba6fc63e822a01240c1caf7
Add Stream argument validation helpers and use throughout dotnet/runtime (#43968)

* Add Stream argument validation helpers and use throughout dotnet/runtime

* Delete unnecessary code in Stream

Stream.Null was using BlockingBegin/EndRead/Write, but the operations are nops (they don't even do argument validation), so the functionality was unnecessary complication.  We can just use TaskToApm instead, with already completed tasks, and then also delete the SynchronousAsyncResult that was used by these operations.

Also, the Begin/EndRead/Write methods were checking CanRead/Write and throwing if they return false, but this is a private sealed type with CanRead/Write hardcoded to return true! Delete.

Finally added consistency across Stream.Null to checking for cancellation.

* Clean up some style on Stream

Also consolidate a few resources and use a few existing ThrowHelpers where appropriate.

* Fix a few issues, and remove duplicative tests missed in conformance tests rollout

* Address PR feedback
77 files changed:
src/libraries/Common/src/System/IO/ReadOnlyMemoryStream.cs
src/libraries/Common/tests/System/IO/ConnectedStreams.cs
src/libraries/System.Console/src/System/IO/ConsoleStream.cs
src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLBytes.cs
src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/BrotliStream.cs
src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/dec/BrotliStream.Decompress.cs
src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/enc/BrotliStream.Compress.cs
src/libraries/System.IO.Compression/src/System.IO.Compression.csproj
src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/DeflateManagedStream.cs
src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateZLib/DeflateStream.cs
src/libraries/System.IO.Compression/src/System/IO/Compression/ZipArchiveEntry.cs
src/libraries/System.IO.Compression/src/System/IO/Compression/ZipCustomStreams.cs
src/libraries/System.IO.FileSystem/src/Resources/Strings.resx
src/libraries/System.IO.FileSystem/src/System.IO.FileSystem.csproj
src/libraries/System.IO.FileSystem/tests/FileStream/CopyToAsync.cs
src/libraries/System.IO.FileSystem/tests/FileStream/EndRead.cs [deleted file]
src/libraries/System.IO.FileSystem/tests/FileStream/EndWrite.cs [deleted file]
src/libraries/System.IO.FileSystem/tests/FileStream/Read.cs
src/libraries/System.IO.FileSystem/tests/FileStream/ReadAsync.cs
src/libraries/System.IO.FileSystem/tests/FileStream/Write.cs
src/libraries/System.IO.FileSystem/tests/FileStream/WriteAsync.cs
src/libraries/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj
src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/PipeReaderStream.cs
src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/PipeWriterStream.cs
src/libraries/System.IO.Pipes/src/Resources/Strings.resx
src/libraries/System.IO.Pipes/src/System.IO.Pipes.csproj
src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.cs
src/libraries/System.IO.UnmanagedMemoryStream/tests/UmsReadWrite.cs
src/libraries/System.IO/tests/BinaryWriter/BinaryWriter.WriteByteCharTests.cs
src/libraries/System.IO/tests/MemoryStream/MemoryStreamTests.cs
src/libraries/System.IO/tests/Stream/Stream.Methods.cs
src/libraries/System.IO/tests/Stream/Stream.NullTests.cs
src/libraries/System.IO/tests/Stream/Stream.cs
src/libraries/System.Net.Connections/src/System/Net/Connections/DuplexPipeStream.cs
src/libraries/System.Net.Http/src/System.Net.Http.csproj
src/libraries/System.Net.Http/src/System/Net/Http/EmptyReadStream.cs
src/libraries/System.Net.Http/src/System/Net/Http/HttpBaseStream.cs
src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs
src/libraries/System.Net.Http/src/System/Net/Http/MultipartContent.cs
src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ChunkedEncodingReadStream.cs
src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectionCloseReadStream.cs
src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ContentLengthReadStream.cs
src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Stream.cs
src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/RawConnectionStream.cs
src/libraries/System.Net.Http/tests/FunctionalTests/MultipartContentTest.cs
src/libraries/System.Net.Http/tests/UnitTests/System.Net.Http.Unit.Tests.csproj
src/libraries/System.Net.HttpListener/src/System/Net/HttpRequestStream.cs
src/libraries/System.Net.HttpListener/src/System/Net/HttpResponseStream.cs
src/libraries/System.Net.HttpListener/tests/HttpRequestStreamTests.cs
src/libraries/System.Net.HttpListener/tests/HttpResponseStreamTests.cs
src/libraries/System.Net.Mail/src/System/Net/Base64Stream.cs
src/libraries/System.Net.Mail/src/System/Net/Mime/EightBitStream.cs
src/libraries/System.Net.Mail/src/System/Net/Mime/QEncodedStream.cs
src/libraries/System.Net.Mail/src/System/Net/Mime/QuotedPrintableStream.cs
src/libraries/System.Net.Quic/src/System/Net/Quic/QuicStream.cs
src/libraries/System.Net.Requests/src/System/Net/RequestStream.cs
src/libraries/System.Net.Requests/tests/RequestStreamTest.cs
src/libraries/System.Net.Security/src/Resources/Strings.resx
src/libraries/System.Net.Security/src/System/Net/Security/NegotiateStream.cs
src/libraries/System.Net.Security/src/System/Net/Security/SslStream.Implementation.cs
src/libraries/System.Net.Security/src/System/Net/Security/SslStream.cs
src/libraries/System.Net.Security/tests/UnitTests/Fakes/FakeSslStream.Implementation.cs
src/libraries/System.Net.Sockets/src/System.Net.Sockets.csproj
src/libraries/System.Net.Sockets/src/System/Net/Sockets/NetworkStream.cs
src/libraries/System.Private.CoreLib/src/Resources/Strings.resx
src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems
src/libraries/System.Private.CoreLib/src/System/IO/BufferedStream.cs
src/libraries/System.Private.CoreLib/src/System/IO/FileStream.Windows.cs
src/libraries/System.Private.CoreLib/src/System/IO/FileStream.cs
src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs
src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs
src/libraries/System.Private.CoreLib/src/System/IO/UnmanagedMemoryStream.cs
src/libraries/System.Private.CoreLib/src/System/Text/TranscodingStream.cs
src/libraries/System.Private.CoreLib/src/System/ThrowHelper.cs
src/libraries/System.Runtime/ref/System.Runtime.cs
src/libraries/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/CryptoStream.cs
src/libraries/System.Security.Cryptography.Primitives/tests/CryptoStream.cs