Files
dotnet-runtime/src/libraries/System.Net.WebSockets.Client/tests/CloseTest.cs
T
Stephen Toub 43996b5017 Fix two WebSocket issues on Unix
This fixes two issues in our managed ClientWebSocket implementation, both of which stemmed from a misunderstanding on my part of how things were supposed to work (or not work):
- I thought that if the WebSocket was already in a CloseSent state and then received a close frame that it should transition to Closed.  Instead, it's supposed to transition to CloseReceived.  Similarly, I thought a WebSocket that was already in a CloseReceived state and then sent a close frame was supposed to transition to Closed.  Instead, it's supposed to transition to CloseSent.  The WebSocket is only supposed to transition to Closed when the connection is closed.
- I thought that ReceiveAsync could only be used concurrently with a SendAsync.  Turns out we need to support it being used concurrently with a Close{Output}Async.

This commit fixes both.


Commit migrated from https://github.com/dotnet/corefx/commit/b729fffc30a36fb765a149596d518658ea069938
2016-07-16 21:42:13 -04:00

314 lines
16 KiB
C#

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Net.Test.Common;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace System.Net.WebSockets.Client.Tests
{
public class CloseTest : ClientWebSocketTestBase
{
public CloseTest(ITestOutputHelper output) : base(output) { }
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseAsync_ServerInitiatedClose_Success(Uri server)
{
const string closeWebSocketMetaCommand = ".close";
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);
_output.WriteLine("SendAsync starting.");
await cws.SendAsync(
WebSocketData.GetBufferFromText(closeWebSocketMetaCommand),
WebSocketMessageType.Text,
true,
cts.Token);
_output.WriteLine("SendAsync done.");
var recvBuffer = new byte[256];
_output.WriteLine("ReceiveAsync starting.");
WebSocketReceiveResult recvResult = await cws.ReceiveAsync(new ArraySegment<byte>(recvBuffer), cts.Token);
_output.WriteLine("ReceiveAsync done.");
// Verify received server-initiated close message.
Assert.Equal(WebSocketCloseStatus.NormalClosure, recvResult.CloseStatus);
Assert.Equal(closeWebSocketMetaCommand, recvResult.CloseStatusDescription);
// Verify current websocket state as CloseReceived which indicates only partial close.
Assert.Equal(WebSocketState.CloseReceived, cws.State);
Assert.Equal(WebSocketCloseStatus.NormalClosure, cws.CloseStatus);
Assert.Equal(closeWebSocketMetaCommand, cws.CloseStatusDescription);
// Send back close message to acknowledge server-initiated close.
_output.WriteLine("CloseAsync starting.");
await cws.CloseAsync(WebSocketCloseStatus.InvalidMessageType, string.Empty, cts.Token);
_output.WriteLine("CloseAsync done.");
Assert.Equal(WebSocketState.Closed, cws.State);
// Verify that there is no follow-up echo close message back from the server by
// making sure the close code and message are the same as from the first server close message.
Assert.Equal(WebSocketCloseStatus.NormalClosure, cws.CloseStatus);
Assert.Equal(closeWebSocketMetaCommand, cws.CloseStatusDescription);
}
}
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseAsync_ClientInitiatedClose_Success(Uri server)
{
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);
Assert.Equal(WebSocketState.Open, cws.State);
var closeStatus = WebSocketCloseStatus.InvalidMessageType;
string closeDescription = "CloseAsync_InvalidMessageType";
await cws.CloseAsync(closeStatus, closeDescription, cts.Token);
Assert.Equal(WebSocketState.Closed, cws.State);
Assert.Equal(closeStatus, cws.CloseStatus);
Assert.Equal(closeDescription, cws.CloseStatusDescription);
}
}
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseAsync_CloseDescriptionIsMaxLength_Success(Uri server)
{
string closeDescription = new string('C', CloseDescriptionMaxLength);
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);
await cws.CloseAsync(WebSocketCloseStatus.NormalClosure, closeDescription, cts.Token);
}
}
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseAsync_CloseDescriptionIsMaxLengthPlusOne_ThrowsArgumentException(Uri server)
{
string closeDescription = new string('C', CloseDescriptionMaxLength + 1);
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);
string expectedInnerMessage = ResourceHelper.GetExceptionMessage(
"net_WebSockets_InvalidCloseStatusDescription",
closeDescription,
CloseDescriptionMaxLength);
var expectedException = new ArgumentException(expectedInnerMessage, "statusDescription");
string expectedMessage = expectedException.Message;
Assert.Throws<ArgumentException>(() =>
{ Task t = cws.CloseAsync(WebSocketCloseStatus.NormalClosure, closeDescription, cts.Token); });
Assert.Equal(WebSocketState.Open, cws.State);
}
}
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseAsync_CloseDescriptionHasUnicode_Success(Uri server)
{
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);
var closeStatus = WebSocketCloseStatus.InvalidMessageType;
string closeDescription = "CloseAsync_Containing\u016Cnicode.";
await cws.CloseAsync(closeStatus, closeDescription, cts.Token);
Assert.Equal(closeStatus, cws.CloseStatus);
Assert.Equal(closeDescription, cws.CloseStatusDescription);
}
}
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseAsync_CloseDescriptionIsNull_Success(Uri server)
{
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);
var closeStatus = WebSocketCloseStatus.NormalClosure;
string closeDescription = null;
await cws.CloseAsync(closeStatus, closeDescription, cts.Token);
Assert.Equal(closeStatus, cws.CloseStatus);
Assert.Equal(true, String.IsNullOrEmpty(cws.CloseStatusDescription));
}
}
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseOutputAsync_ClientInitiated_CanReceive_CanClose(Uri server)
{
string message = "Hello WebSockets!";
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);
var closeStatus = WebSocketCloseStatus.InvalidPayloadData;
string closeDescription = "CloseOutputAsync_Client_InvalidPayloadData";
await cws.SendAsync(WebSocketData.GetBufferFromText(message), WebSocketMessageType.Text, true, cts.Token);
// Need a short delay as per WebSocket rfc6455 section 5.5.1 there isn't a requirement to receive any
// data fragments after a close has been sent. The delay allows the received data fragment to be
// available before calling close. The WinRT MessageWebSocket implementation doesn't allow receiving
// after a call to Close.
await Task.Delay(100);
await cws.CloseOutputAsync(closeStatus, closeDescription, cts.Token);
// Should be able to receive the message echoed by the server.
var recvBuffer = new byte[100];
var segmentRecv = new ArraySegment<byte>(recvBuffer);
WebSocketReceiveResult recvResult = await cws.ReceiveAsync(segmentRecv, cts.Token);
Assert.Equal(message.Length, recvResult.Count);
segmentRecv = new ArraySegment<byte>(segmentRecv.Array, 0, recvResult.Count);
Assert.Equal(message, WebSocketData.GetTextFromBuffer(segmentRecv));
Assert.Equal(null, recvResult.CloseStatus);
Assert.Equal(null, recvResult.CloseStatusDescription);
await cws.CloseAsync(closeStatus, closeDescription, cts.Token);
Assert.Equal(closeStatus, cws.CloseStatus);
Assert.Equal(closeDescription, cws.CloseStatusDescription);
}
}
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseOutputAsync_ServerInitiated_CanSend(Uri server)
{
string message = "Hello WebSockets!";
var expectedCloseStatus = WebSocketCloseStatus.NormalClosure;
var expectedCloseDescription = ".shutdown";
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);
await cws.SendAsync(
WebSocketData.GetBufferFromText(".shutdown"),
WebSocketMessageType.Text,
true,
cts.Token);
// Should be able to receive a shutdown message.
var recvBuffer = new byte[100];
var segmentRecv = new ArraySegment<byte>(recvBuffer);
WebSocketReceiveResult recvResult = await cws.ReceiveAsync(segmentRecv, cts.Token);
Assert.Equal(0, recvResult.Count);
Assert.Equal(expectedCloseStatus, recvResult.CloseStatus);
Assert.Equal(expectedCloseDescription, recvResult.CloseStatusDescription);
// Verify WebSocket state
Assert.Equal(expectedCloseStatus, cws.CloseStatus);
Assert.Equal(expectedCloseDescription, cws.CloseStatusDescription);
Assert.Equal(WebSocketState.CloseReceived, cws.State);
// Should be able to send.
await cws.SendAsync(WebSocketData.GetBufferFromText(message), WebSocketMessageType.Text, true, cts.Token);
// Cannot change the close status/description with the final close.
var closeStatus = WebSocketCloseStatus.InvalidPayloadData;
var closeDescription = "CloseOutputAsync_Client_Description";
await cws.CloseAsync(closeStatus, closeDescription, cts.Token);
Assert.Equal(expectedCloseStatus, cws.CloseStatus);
Assert.Equal(expectedCloseDescription, cws.CloseStatusDescription);
Assert.Equal(WebSocketState.Closed, cws.State);
}
}
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseOutputAsync_CloseDescriptionIsNull_Success(Uri server)
{
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);
var closeStatus = WebSocketCloseStatus.NormalClosure;
string closeDescription = null;
await cws.CloseOutputAsync(closeStatus, closeDescription, cts.Token);
}
}
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseOutputAsync_DuringConcurrentReceiveAsync_ExpectedStates(Uri server)
{
var receiveBuffer = new byte[1024];
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
// Issue a receive but don't wait for it.
var t = cws.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), CancellationToken.None);
Assert.False(t.IsCompleted);
Assert.Equal(WebSocketState.Open, cws.State);
// Send a close frame. After this completes, the state could be CloseSent if we haven't
// yet received the server response close frame, or it could be CloseReceived if we have.
await cws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
Assert.True(
cws.State == WebSocketState.CloseSent || cws.State == WebSocketState.CloseReceived,
$"Expected CloseSent or CloseReceived, got {cws.State}");
// Then wait for the receive. After this completes, the state is most likely CloseReceived,
// however there is a race condition between the our realizing that the send has completed
// and a fast server sending back a close frame, such that we could end up noticing the
// receive completion before we notice the send completion.
WebSocketReceiveResult r = await t;
Assert.Equal(WebSocketMessageType.Close, r.MessageType);
Assert.True(
cws.State == WebSocketState.CloseSent || cws.State == WebSocketState.CloseReceived,
$"Expected CloseSent or CloseReceived, got {cws.State}");
// Then close
await cws.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
Assert.Equal(WebSocketState.Closed, cws.State);
// Another close should fail
await Assert.ThrowsAsync<WebSocketException>(() => cws.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None));
}
}
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task CloseAsync_DuringConcurrentReceiveAsync_ExpectedStates(Uri server)
{
var receiveBuffer = new byte[1024];
using (ClientWebSocket cws = await WebSocketHelper.GetConnectedWebSocket(server, TimeOutMilliseconds, _output))
{
var t = cws.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), CancellationToken.None);
Assert.False(t.IsCompleted);
await cws.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
// There is a race condition in the above. If the ReceiveAsync receives the sent close message from the server,
// then it will complete successfully and the socket will close successfully. If the CloseAsync receive the sent
// close message from the server, then the receive async will end up getting aborted along with the socket.
try
{
await t;
Assert.Equal(WebSocketState.Closed, cws.State);
}
catch (WebSocketException)
{
Assert.Equal(WebSocketState.Aborted, cws.State);
}
}
}
}
}