Skip to content

Commit

Permalink
added unit tests about websocket masked encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
kerryjiang committed Aug 30, 2024
1 parent 0091922 commit 8892e9f
Showing 1 changed file with 76 additions and 1 deletion.
77 changes: 76 additions & 1 deletion test/SuperSocket.Tests/WebSocket/WebSocketBasicTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using SuperSocket;
using SuperSocket.Command;
using SuperSocket.WebSocket.Server;
using SuperSocket.ProtoBase;
Expand Down Expand Up @@ -42,6 +41,82 @@ public WebSocketBasicTest(ITestOutputHelper outputHelper)

}

private string GetTestMessage(int messageSize, bool nonAsciiText)
{
var sb = new StringBuilder();

while (sb.Length < messageSize)
{
if (nonAsciiText)
sb.Append("这是一段长文本这是一段长文本这是");
else
sb.Append(Guid.NewGuid().ToString());
}

return sb.ToString(0, messageSize);
}

[Theory]
[InlineData(125, false, 128)]
[InlineData(125, true, 128)]
[InlineData(126, false, 128)]
[InlineData(126, true, 128)]
[InlineData(127, false, 128)]
[InlineData(127, true, 128)]
[InlineData(128, false, 128)]
[InlineData(128, true, 128)]
[InlineData(129, false, 128)]
[InlineData(129, true, 128)]
[InlineData(130, false, 128)]
[InlineData(130, true, 128)]
[InlineData(8192, false, 1024)]
[InlineData(8192, true, 1024)]
[InlineData(65535, false, 1024)]
[InlineData(65535, true, 1024)]
[InlineData(65536, false, 1024)]
[InlineData(65536, true, 1024)]
[InlineData(65537, false, 1024)]
[InlineData(65537, true, 1024)]
[InlineData(6, true, 8)]
[InlineData(7, true, 8)]
[InlineData(8, true, 8)]
[InlineData(9, true, 8)]
[InlineData(10, true, 8)]
[InlineData(16, true, 8)]
[InlineData(17, true, 8)]
public void TestWebSocketMaskEncoder(int messageLength, bool nonAsciiText, int fragmentSize)
{
var websocketEncoder = new WebSocketMaskedEncoder(ArrayPool<byte>.Shared, new int[] { fragmentSize });

var package = new WebSocketPackage();
package.OpCode = OpCode.Text;
package.Message = GetTestMessage(messageLength, nonAsciiText);

var writter = new ArrayBufferWriter<byte>();
websocketEncoder.Encode(writter, package);

var filter = new WebSocketDataPipelineFilter(null, true);

var reader = new SequenceReader<byte>(new ReadOnlySequence<byte>(writter.WrittenMemory));

var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10));

var decodedPakcage = default(WebSocketPackage);

while (true)
{
Assert.False(cancellationTokenSource.IsCancellationRequested);

decodedPakcage = filter.Filter(ref reader);

if (decodedPakcage != null)
break;
}

Assert.Equal(package.OpCode, decodedPakcage.OpCode);
Assert.Equal(package.Message, decodedPakcage.Message);
}

[Theory]
[InlineData(typeof(RegularHostConfigurator))]
public async Task TestCustomWebSocketSession(Type hostConfiguratorType)
Expand Down

0 comments on commit 8892e9f

Please sign in to comment.