Skip to content

Commit

Permalink
Add SslStream test for zero-byte reads
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stephentoub committed May 29, 2020
1 parent 45b0d0b commit 9fd0c2b
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,38 @@ public async Task SslStream_StreamToStream_Successive_ClientWrite_WithZeroBytes_
}
}

[Fact]
public async Task SslStream_StreamToStream_ZeroByteRead_SucceedsWhenDataAvailable()
{
using var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
using var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(new IPEndPoint(IPAddress.Loopback, 0));
listener.Listen(1);
client.Connect(listener.LocalEndPoint);
using Socket server = listener.Accept();

using var clientSslStream = new SslStream(new NetworkStream(client, ownsSocket: true), leaveInnerStreamOpen: false, AllowAnyServerCertificate);
using var serverSslStream = new SslStream(new NetworkStream(server, ownsSocket: true));
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)
{
count += await clientSslStream.ReadAsync(readBytes.AsMemory(count));
}
Assert.Equal("hello", Encoding.UTF8.GetString(readBytes));
}
}

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

0 comments on commit 9fd0c2b

Please sign in to comment.