Skip to content

Commit

Permalink
Add SslStream test for zero-byte reads (#37128)
Browse files Browse the repository at this point in the history
* Add SslStream test for zero-byte reads

We have higher-level tests for libraries like WebSockets that validate the behavior of zero-byte reads, but we don't currently have one in System.Net.Security's tests.  Adding one.

* Address PR feedback
  • Loading branch information
stephentoub committed May 31, 2020
1 parent 21783bc commit baedfec
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,34 @@ public async Task SslStream_StreamToStream_Successive_ClientWrite_WithZeroBytes_
}
}

[Fact]
public async Task SslStream_StreamToStream_ZeroByteRead_SucceedsWhenDataAvailable()
{
(NetworkStream clientStream, NetworkStream serverStream) = TestHelper.GetConnectedTcpStreams();
using var clientSslStream = new SslStream(clientStream, leaveInnerStreamOpen: false, AllowAnyServerCertificate);
using var serverSslStream = new SslStream(serverStream);
await DoHandshake(clientSslStream, serverSslStream);

for (int iter = 0; iter < 2; iter++)
{
ValueTask<int> zeroByteRead = clientSslStream.ReadAsync(Memory<byte>.Empty);
Assert.False(zeroByteRead.IsCompleted);

await serverSslStream.WriteAsync(Encoding.UTF8.GetBytes("hello"));
Assert.Equal(0, await zeroByteRead);

var readBytes = new byte[5];
int count = 0;
while (count < readBytes.Length)
{
int n = await clientSslStream.ReadAsync(readBytes.AsMemory(count));
Assert.InRange(n, 1, readBytes.Length - count);
count += n;
}
Assert.Equal("hello", Encoding.UTF8.GetString(readBytes));
}
}

[Theory]
[InlineData(false)]
[InlineData(true)]
Expand Down

0 comments on commit baedfec

Please sign in to comment.