Skip to content

Commit 0acbafe

Browse files
authored
Add initial set of conformance tests for Stream (#43834)
* Fix invalid assert in StreamBuffer This can be hit by calling code and thus shouldn't be an assert. As an assert it prevents testing. * Add ConnectedStreams.CreateUnidirectional * Add initial set of Stream conformance tests These primarily focus on "connected streams", ones that can be arranged to communicate with each other in a producer/consumer pattern or as a bidirectional communication mechanism, e.g. NetworkStream, PipeStream, SslStream wrapped around a NetworkStream, FileStream created around pipes, etc. Later we can add more tests focused on standalone streams, e.g. FileStream created for an on-disk file, MemoryStream, etc. * Add ConnectedStreams tests These are currently helpers used by many other tests. At some point they could become public API as well. * Fix NetworkStream argument names Technically a breaking change, but the current divergence from the base Stream class is a bug, and bringing them into sync means argument exceptions that emerge from the derived type make sense when used via the base type, as well as then being able to use shared validation logic across all streams (subsequent to these changes). * Add stream conformance tests for NetworkStream * Add stream conformance tests for QuicStream * Fix several CryptoStream behaviors 1. Flushing a stream that wraps another stream for writing should always flush that underlying stream, even if no additional data was written as part of the flush. 2. Argument validation should validate buffers are not null rather than null ref'ing on a null buffer. 3. Checks for the CryptoStream mode should come after argument validation. * Add stream conformance tests for CryptoStream * Fix FileStream argument names Technically a breaking change, but the current divergence from the base Stream class is a bug, and bringing them into sync means argument exceptions that emerge from the derived type make sense when used via the base type, as well as then being able to use shared validation logic across all streams (subsequent to these changes). * Fix BufferedStream argument names Technically a breaking change, but the current divergence from the base Stream class is a bug, and bringing them into sync means argument exceptions that emerge from the derived type make sense when used via the base type, as well as then being able to use shared validation logic across all streams (subsequent to these changes). * Add stream conformance tests for FileStream Specifically when used in a connected fashion, wrapped around an anonymous or named pipe. * Add stream conformance tests for BufferedStream Specifically when used in a connected fashion, wrapped around some other connected stream. * Add stream conformance tests for SslStream and NegotiateStream * Fix PipeStream.Flush to not fail on readable streams Consumers may expect Stream.Flush to be a nop if the stream is readable only, but PipeStream.Flush is throwing in that case. Stop doing that. * Add stream conformance tests for PipeStream * Fix several BrotliStream behaviors 1. When passed a null buffer, it's throwing an exception with the argument name "array", even though the parameter's name is "buffer". 2. Even if there's no data written as part of the Flush{Async} call on a writeable stream, it should be calling flush on the wrapped stream. * Fix several DeflateStream (and friends) issues 1. DeflateStream.Flush{Async} when writing needs to always flush the underlying stream, even if there's no data written as part of the flush call itself. 2. Several byte[] array arguments should be byte[] buffer. Technically a breaking change, but the current divergence from the base Stream class is a bug, and bringing them into sync means argument exceptions that emerge from the derived type make sense when used via the base type, as well as then being able to use shared validation logic across all streams (subsequent to these changes). 3. DeflateStream.EndRead/Write needs to do additional state validation. 4. Not a bug, but simplify ReadAsync to match the sync Read implementation flow. * Add stream conformance tests for Deflate/ZLib/GZip/BrotliStream * Fix PipeReader/WriterStream argument validation * Add stream conformance tests for PipeReader/WriterStream * Remove erroneous asserts from Stream.ReadWriteTask * Address PR feedback * Fix a few tests in CI
1 parent cf276ce commit 0acbafe

File tree

91 files changed

+4293
-5878
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+4293
-5878
lines changed

src/libraries/Common/src/System/Net/StreamBuffer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,6 @@ public void Reset()
343343
{
344344
if (_hasWaiter != 0)
345345
{
346-
Debug.Fail("Concurrent use is not supported");
347346
throw new InvalidOperationException("Concurrent use is not supported");
348347
}
349348

src/libraries/Common/tests/Common.Tests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@
102102
Link="Common\System\Threading\Tasks\TaskToApm.cs" />
103103
<Compile Include="$(CoreLibSharedDir)System\IO\PathInternal.cs"
104104
Link="System\IO\PathInternal.cs" />
105+
<Compile Include="System\IO\ConnectedStreams.cs" Link="System\IO\ConnectedStreams.cs" />
106+
<Compile Include="Tests\System\IO\ConnectedStreamsTests.cs" />
107+
<Compile Include="Tests\System\IO\StreamConformanceTests.cs" />
108+
<Compile Include="$(CommonPath)System\Net\ArrayBuffer.cs" Link="Common\System\Net\ArrayBuffer.cs" />
109+
<Compile Include="$(CommonPath)System\Net\StreamBuffer.cs" Link="Common\System\Net\StreamBuffer.cs" />
110+
<Compile Include="$(CommonTestPath)System\IO\CallTrackingStream.cs" Link="Common\System\IO\CallTrackingStream.cs" />
105111
</ItemGroup>
106112
<ItemGroup Condition="'$(TargetsWindows)'=='true'">
107113
<Compile Include="$(CoreLibSharedDir)System\IO\PathInternal.Windows.cs"

src/libraries/Common/tests/System/IO/Compression/CompressionStreamTestBase.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Collections.Generic;
5+
using System.IO.Tests;
6+
using System.Threading.Tasks;
57

68
namespace System.IO.Compression
79
{
8-
public abstract class CompressionTestBase
10+
public abstract class CompressionTestBase : WrappingConnectedStreamConformanceTests
911
{
1012
public static IEnumerable<object[]> UncompressedTestFiles()
1113
{
@@ -36,8 +38,22 @@ public abstract class CompressionStreamTestBase : CompressionTestBase
3638
public abstract Stream CreateStream(Stream stream, CompressionLevel level);
3739
public abstract Stream CreateStream(Stream stream, CompressionLevel level, bool leaveOpen);
3840
public abstract Stream BaseStream(Stream stream);
39-
public virtual bool FlushCompletes { get => true; }
40-
public virtual bool FlushNoOps { get => false; }
4141
public virtual int BufferSize { get => 8192; }
42+
43+
protected override Task<StreamPair> CreateConnectedStreamsAsync()
44+
{
45+
(Stream stream1, Stream stream2) = ConnectedStreams.CreateBidirectional(4 * 1024, 16 * 1024);
46+
return Task.FromResult<StreamPair>((CreateStream(stream1, CompressionMode.Compress), CreateStream(stream2, CompressionMode.Decompress)));
47+
}
48+
49+
protected override Task<StreamPair> CreateWrappedConnectedStreamsAsync(StreamPair wrapped, bool leaveOpen) =>
50+
Task.FromResult<StreamPair>((CreateStream(wrapped.Stream1, CompressionMode.Compress, leaveOpen), CreateStream(wrapped.Stream2, CompressionMode.Decompress, leaveOpen)));
51+
52+
protected override int BufferedSize => 16 * 1024 + BufferSize;
53+
protected override bool UsableAfterCanceledReads => false;
54+
protected override Type UnsupportedReadWriteExceptionType => typeof(InvalidOperationException);
55+
protected override bool WrappedUsableAfterClose => false;
56+
protected override bool FlushRequiredToWriteData => true;
57+
protected override bool FlushGuaranteesAllDataWritten => false;
4258
}
4359
}

src/libraries/Common/tests/System/IO/Compression/CompressionStreamUnitTestBase.cs

Lines changed: 24 additions & 890 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)