public async Task SendMultipleDataRemoteCommand() { using (MockWebSocket ws = new MockWebSocket()) using (StreamDemuxer demuxer = new StreamDemuxer(ws)) { List <byte> sentBuffer = new List <byte>(); ws.MessageSent += (sender, args) => { sentBuffer.AddRange(args.Data.Buffer); }; demuxer.Start(); byte channelIndex = 12; var stream = demuxer.GetStream(channelIndex, channelIndex); var b = GenerateRandomBuffer(100, 0xEF); stream.Write(b, 0, b.Length); b = GenerateRandomBuffer(200, 0xAB); stream.Write(b, 0, b.Length); // Send 300 bytes in 2 messages, expect 1 (channel index) * 2 + 300 (payload) = 302 bytes Assert.True( await WaitForAsync(() => sentBuffer.Count == 302).ConfigureAwait(false), $"Demuxer error: expect to send 302 bytes, but actually send {sentBuffer.Count} bytes."); Assert.True(sentBuffer[0] == channelIndex, "The first sent byte is not channel index!"); Assert.True(sentBuffer[1] == 0xEF, "The first part of payload incorrect!"); Assert.True(sentBuffer[101] == channelIndex, "The second message first byte is not channel index!"); Assert.True(sentBuffer[102] == 0xAB, "The second part of payload incorrect!"); } }
public void SendDataRaw() { var rcv = new MockWebSocket(); var msg = new UniumSocket.Message(); msg.Socket = new UniumSocket(rcv); // no id rcv.Expect = @"{""data"":{""x"":1,""y"":2,""z"":3}}"; msg.Reply(@"{""x"":1,""y"":2,""z"":3}"); // id msg.id = "id"; rcv.Expect = @"{""id"":""id"",""data"":{""x"":1,""y"":2,""z"":3}}"; msg.Reply(@"{""x"":1,""y"":2,""z"":3}"); // null rcv.Expect = @"{""id"":""id"",""data"":null}"; msg.Reply(null as string); rcv.Expect = @"{""id"":""id"",""data"":null}"; msg.Reply(""); }
private const int LengthOfStringQuotes = 2; // "" public WebSocketMessageHandlerTests(IJsonRpcMessageFormatter formatter, ITestOutputHelper logger) : base(logger) { this.socket = new MockWebSocket(); this.formatter = formatter; this.handler = new WebSocketMessageHandler(this.socket, this.formatter, BufferSize); }
public void SendMessage() { var rcv = new MockWebSocket(); var msg = new UniumSocket.Message(); msg.Socket = new UniumSocket(rcv); // no id rcv.Expect = @"{""info"":""hello""}"; msg.id = null; msg.Info("hello"); // with id rcv.Expect = @"{""id"":""msg"",""info"":""hello""}"; msg.id = "msg"; msg.Info("hello"); // JSON unfriendly rcv.Expect = @"{""id"":""\"",x:{}\"""",""info"":""\"",x:{}\""""}"; msg.id = @""",x:{}"""; msg.Info(@""",x:{}"""); // no message rcv.Expect = @"{""info"":null}"; msg.id = null; msg.Info(null); }
public async Task ReceiveDataPortForward() { using (MockWebSocket ws = new MockWebSocket()) using (StreamDemuxer demuxer = new StreamDemuxer(ws, StreamType.PortForward)) { demuxer.Start(); List <byte> receivedBuffer = new List <byte>(); byte channelIndex = 12; var stream = demuxer.GetStream(channelIndex, channelIndex); // Receive 600 bytes in 3 messages. Exclude 1 channel index byte per message, and 2 port bytes in the first message. // expect 600 - 3 - 2 = 595 bytes payload. int expectedCount = 595; var t = Task.Run(async() => { await ws.InvokeReceiveAsync( new ArraySegment <byte>(GenerateRandomBuffer(100, channelIndex, 0xB1, true)), WebSocketMessageType.Binary, true).ConfigureAwait(false); await ws.InvokeReceiveAsync( new ArraySegment <byte>(GenerateRandomBuffer(200, channelIndex, 0xB2, false)), WebSocketMessageType.Binary, true).ConfigureAwait(false); await ws.InvokeReceiveAsync( new ArraySegment <byte>(GenerateRandomBuffer(300, channelIndex, 0xB3, false)), WebSocketMessageType.Binary, true).ConfigureAwait(false); await WaitForAsync(() => receivedBuffer.Count == expectedCount).ConfigureAwait(false); await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "normal", CancellationToken.None).ConfigureAwait(false); }); var buffer = new byte[50]; while (true) { var cRead = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false); if (cRead == 0) { break; } for (int i = 0; i < cRead; i++) { receivedBuffer.Add(buffer[i]); } } await t.ConfigureAwait(false); Assert.True( receivedBuffer.Count == expectedCount, $"Demuxer error: expect to receive {expectedCount} bytes, but actually got {receivedBuffer.Count} bytes."); Assert.True(receivedBuffer[0] == 0xB1, "The first payload incorrect!"); Assert.True(receivedBuffer[96] == 0xB1, "The first payload incorrect!"); Assert.True(receivedBuffer[97] == 0xB2, "The second payload incorrect!"); Assert.True(receivedBuffer[295] == 0xB2, "The second payload incorrect!"); Assert.True(receivedBuffer[296] == 0xB3, "The third payload incorrect!"); Assert.True(receivedBuffer[594] == 0xB3, "The third payload incorrect!"); } }
public async Task UsePipeReader_WebSocket() { var expectedBuffer = new byte[] { 4, 5, 6 }; var webSocket = new MockWebSocket(); webSocket.EnqueueRead(expectedBuffer); var pipeReader = webSocket.UsePipeReader(cancellationToken: this.TimeoutToken); var readResult = await pipeReader.ReadAsync(this.TimeoutToken); Assert.Equal(expectedBuffer, readResult.Buffer.First.Span.ToArray()); pipeReader.AdvanceTo(readResult.Buffer.End); }
public async Task UsePipeWriter_WebSocket() { var expectedBuffer = new byte[] { 4, 5, 6 }; var webSocket = new MockWebSocket(); var pipeWriter = webSocket.UsePipeWriter(cancellationToken: this.TimeoutToken); await pipeWriter.WriteAsync(expectedBuffer, this.TimeoutToken); pipeWriter.Complete(); await pipeWriter.WaitForReaderCompletionAsync(); var message = webSocket.WrittenQueue.Dequeue(); Assert.Equal(expectedBuffer, message.Buffer.ToArray()); }
public async Task UsePipeWriter_WebSocket() { var expectedBuffer = new byte[] { 4, 5, 6 }; var webSocket = new MockWebSocket(); var pipeWriter = webSocket.UsePipeWriter(cancellationToken: this.TimeoutToken); await pipeWriter.WriteAsync(expectedBuffer, this.TimeoutToken); pipeWriter.Complete(); #pragma warning disable CS0618 // Type or member is obsolete await pipeWriter.WaitForReaderCompletionAsync(); #pragma warning restore CS0618 // Type or member is obsolete var message = webSocket.WrittenQueue.Dequeue(); Assert.Equal(expectedBuffer, message.Buffer.ToArray()); }
public async Task ReceiveDataRemoteCommand() { using (MockWebSocket ws = new MockWebSocket()) { StreamDemuxer demuxer = new StreamDemuxer(ws); Task.Run(() => demuxer.Start()); List <byte> receivedBuffer = new List <byte>(); byte channelIndex = 12; var stream = demuxer.GetStream(channelIndex, channelIndex); // Receive 600 bytes in 3 messages. Exclude 1 channel index byte per message, expect 597 bytes payload. int expectedCount = 597; var t = Task.Run(async() => { await ws.InvokeReceiveAsync(new ArraySegment <byte>(GenerateRandomBuffer(100, channelIndex, 0xAA, false)), WebSocketMessageType.Binary, true); await ws.InvokeReceiveAsync(new ArraySegment <byte>(GenerateRandomBuffer(200, channelIndex, 0xAB, false)), WebSocketMessageType.Binary, true); await ws.InvokeReceiveAsync(new ArraySegment <byte>(GenerateRandomBuffer(300, channelIndex, 0xAC, false)), WebSocketMessageType.Binary, true); await WaitForAsync(() => receivedBuffer.Count == expectedCount); await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "normal", CancellationToken.None); }); var buffer = new byte[50]; while (true) { var cRead = await stream.ReadAsync(buffer, 0, buffer.Length); if (cRead == 0) { break; } for (int i = 0; i < cRead; i++) { receivedBuffer.Add(buffer[i]); } } await t; Assert.True(receivedBuffer.Count == expectedCount, $"Demuxer error: expect to receive {expectedCount} bytes, but actually got {receivedBuffer.Count} bytes."); Assert.True(receivedBuffer[0] == 0xAA, "The first payload incorrect!"); Assert.True(receivedBuffer[98] == 0xAA, "The first payload incorrect!"); Assert.True(receivedBuffer[99] == 0xAB, "The second payload incorrect!"); Assert.True(receivedBuffer[297] == 0xAB, "The second payload incorrect!"); Assert.True(receivedBuffer[298] == 0xAC, "The third payload incorrect!"); Assert.True(receivedBuffer[596] == 0xAC, "The third payload incorrect!"); } }
public void SendDataObject() { var rcv = new MockWebSocket(); var msg = new UniumSocket.Message(); msg.Socket = new UniumSocket(rcv); int n = 5; // no id rcv.Expect = @"{""data"":5}"; msg.Reply(n); // id msg.id = "id"; rcv.Expect = @"{""id"":""id"",""data"":5}"; msg.Reply(n); // objects msg.id = null; rcv.Expect = @"{""data"":{""name"":""name"",""value"":7}}"; msg.Reply(new { name = "name", value = 7 }); var o = new { name = "name", child = new { c = "a" } }; rcv.Expect = @"{""data"":{""name"":""name"",""child"":{""c"":""a""}}}"; msg.Reply(o); // rcv.Expect = @"{""data"":{""name"":""name"",""child"":{""c"":""a""}}}"; // msg.Reply( o, true ); rcv.Expect = @"{""data"":null}"; msg.Reply(null as object); // value types float f = 1.0f; rcv.Expect = @"{""data"":1}"; msg.Reply(f); }
public async Task UsePipe_WebSocket() { var expectedBuffer = new byte[] { 4, 5, 6 }; var webSocket = new MockWebSocket(); webSocket.EnqueueRead(expectedBuffer); var pipe = webSocket.UsePipe(cancellationToken: this.TimeoutToken); var readResult = await pipe.Input.ReadAsync(this.TimeoutToken); Assert.Equal(expectedBuffer, readResult.Buffer.First.Span.ToArray()); pipe.Input.AdvanceTo(readResult.Buffer.End); await pipe.Output.WriteAsync(expectedBuffer, this.TimeoutToken); pipe.Output.Complete(); await pipe.Output.WaitForReaderCompletionAsync(); var message = webSocket.WrittenQueue.Dequeue(); Assert.Equal(expectedBuffer, message.Buffer.ToArray()); }
public async Task SendDataRemoteCommand() { using (MockWebSocket ws = new MockWebSocket()) using (StreamDemuxer demuxer = new StreamDemuxer(ws)) { List <byte> sentBuffer = new List <byte>(); ws.MessageSent += (sender, args) => { sentBuffer.AddRange(args.Data.Buffer); }; demuxer.Start(); byte channelIndex = 12; var stream = demuxer.GetStream(channelIndex, channelIndex); var b = GenerateRandomBuffer(100, 0xEF); stream.Write(b, 0, b.Length); // Send 100 bytes, expect 1 (channel index) + 100 (payload) = 101 bytes Assert.True(await WaitForAsync(() => sentBuffer.Count == 101), $"Demuxer error: expect to send 101 bytes, but actually send {sentBuffer.Count} bytes."); Assert.True(sentBuffer[0] == channelIndex, "The first sent byte is not channel index!"); Assert.True(sentBuffer[1] == 0xEF, "Incorrect payload!"); } }
public void SendErrors() { var rcv = new MockWebSocket(); var msg = new UniumSocket.Message(); msg.Socket = new UniumSocket(rcv); // no id rcv.Expect = @"{""error"":""Bad Request""}"; msg.Error(ResponseCode.BadRequest); // general errors rcv.Expect = @"{""id"":""err"",""error"":""Bad Request""}"; msg.id = "err"; msg.Error(ResponseCode.BadRequest); rcv.Expect = @"{""id"":""err"",""error"":""Not Found""}"; msg.id = "err"; msg.Error(ResponseCode.NotFound); rcv.Expect = @"{""id"":""err"",""error"":""Internal Server Error""}"; msg.id = "err"; msg.Error(ResponseCode.InternalServerError); // json in id rcv.Expect = @"{""id"":"":{}'"",""error"":""Bad Request""}"; msg.id = ":{}'"; msg.Error(ResponseCode.BadRequest); rcv.Expect = @"{""id"":""\""name\"":\""ok\"""",""error"":""Bad Request""}"; msg.id = @"""name"":""ok"""; msg.Error(ResponseCode.BadRequest); }
public WebSocketStreamTests(ITestOutputHelper logger) : base(logger) { this.socket = new MockWebSocket(); this.stream = this.socket.AsStream(); }
public WebSocketMessageHandlerTests(ITestOutputHelper logger) : base(logger) { this.socket = new MockWebSocket(); this.handler = new WebSocketMessageHandler(this.socket, BufferSize); }
public async Task ReceiveDataPortForwardMultipleStream() { using (MockWebSocket ws = new MockWebSocket()) using (StreamDemuxer demuxer = new StreamDemuxer(ws, StreamType.PortForward)) { demuxer.Start(); List <byte> receivedBuffer1 = new List <byte>(); byte channelIndex1 = 1; var stream1 = demuxer.GetStream(channelIndex1, channelIndex1); List <byte> receivedBuffer2 = new List <byte>(); byte channelIndex2 = 2; var stream2 = demuxer.GetStream(channelIndex2, channelIndex2); // stream 1: receive 100 + 300 = 400 bytes, exclude 1 channel index per message, exclude port bytes in the first message, // expect 400 - 1 x 2 - 2 = 396 bytes. int expectedCount1 = 396; // stream 2: receive 200 bytes, exclude 1 channel index per message, exclude port bytes in the first message, // expect 200 - 1 - 2 = 197 bytes. int expectedCount2 = 197; var t1 = Task.Run(async() => { // Simulate WebSocket received remote data to multiple streams await ws.InvokeReceiveAsync( new ArraySegment <byte>(GenerateRandomBuffer(100, channelIndex1, 0xE1, true)), WebSocketMessageType.Binary, true).ConfigureAwait(false); await ws.InvokeReceiveAsync( new ArraySegment <byte>(GenerateRandomBuffer(200, channelIndex2, 0xE2, true)), WebSocketMessageType.Binary, true).ConfigureAwait(false); await ws.InvokeReceiveAsync( new ArraySegment <byte>(GenerateRandomBuffer(300, channelIndex1, 0xE3, false)), WebSocketMessageType.Binary, true).ConfigureAwait(false); await WaitForAsync(() => receivedBuffer1.Count == expectedCount1).ConfigureAwait(false); await WaitForAsync(() => receivedBuffer2.Count == expectedCount2).ConfigureAwait(false); await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "normal", CancellationToken.None).ConfigureAwait(false); }); var t2 = Task.Run(async() => { var buffer = new byte[50]; while (true) { var cRead = await stream1.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false); if (cRead == 0) { break; } for (int i = 0; i < cRead; i++) { receivedBuffer1.Add(buffer[i]); } } }); var t3 = Task.Run(async() => { var buffer = new byte[50]; while (true) { var cRead = await stream2.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false); if (cRead == 0) { break; } for (int i = 0; i < cRead; i++) { receivedBuffer2.Add(buffer[i]); } } }); await Task.WhenAll(t1, t2, t3).ConfigureAwait(false); Assert.True( receivedBuffer1.Count == expectedCount1, $"Demuxer error: expect to receive {expectedCount1} bytes, but actually got {receivedBuffer1.Count} bytes."); Assert.True( receivedBuffer2.Count == expectedCount2, $"Demuxer error: expect to receive {expectedCount2} bytes, but actually got {receivedBuffer2.Count} bytes."); Assert.True(receivedBuffer1[0] == 0xE1, "The first payload incorrect!"); Assert.True(receivedBuffer1[96] == 0xE1, "The first payload incorrect!"); Assert.True(receivedBuffer1[97] == 0xE3, "The second payload incorrect!"); Assert.True(receivedBuffer1[395] == 0xE3, "The second payload incorrect!"); Assert.True(receivedBuffer2[0] == 0xE2, "The first payload incorrect!"); Assert.True(receivedBuffer2[196] == 0xE2, "The first payload incorrect!"); } }