Ejemplo n.º 1
0
        public void PayloadLengthPart7bitsWitMaskTest()
        {
            var builder = new WebSocketFrameBuilder(ProxyConfig.MaxByteArrayLength);
            var data    = new byte[]
            {
                0b10000010, // FIN, Binary
                0b11111101, // Mask, 125bytes
                0b10101010, // Mask key
                0b10101010, // Mask key
                0b10101010, // Mask key
                0b10101010, // Mask key
            };

            builder.TryAddData(data, 0, data.Length, out var readSize, out _).IsFalse();
            readSize.Is(data.Length);

            var payload = Enumerable.Repeat((byte)'a', 125).ToArray();

            builder.TryAddData(payload, 0, payload.Length, out readSize, out var frame).IsTrue();
            readSize.Is(payload.Length);

            frame.Fin.IsTrue();
            frame.Opcode.Is(WebSocketOpcode.Binary);
            frame.Mask.IsTrue();
            frame.MaskKey.Is(new byte[] { 0b10101010, 0b10101010, 0b10101010, 0b10101010 });
        public void DecompressTest()
        {
            var source = "hogefugapiyo";

            byte[] destBytes;
            using (var stream = new MemoryStream())
            {
                using (var deflate = new DeflateStream(stream, CompressionMode.Compress, true))
                {
                    var sourceBytes = Encoding.UTF8.GetBytes(source);
                    deflate.Write(sourceBytes, 0, sourceBytes.Length);
                }
                destBytes = stream.ToArray();
            }

            var builder = new WebSocketFrameBuilder(ProxyConfig.MaxByteArrayLength);
            var data    = new byte[]
            {
                0b11000010,
                0b00001110,
            };

            builder.TryAddData(data, 0, data.Length, out var readSize, out _).IsFalse();
            readSize.Is(data.Length);

            builder.TryAddData(destBytes, 0, destBytes.Length, out readSize, out var frame).IsTrue();
            readSize.Is(destBytes.Length);

            var ex      = new PerMessageDeflateExtension();
            var message = ex.Decompress(new WebSocketMessage(null, frame));

            Encoding.UTF8.GetString(message.PayloadData).Is(source);
        }
Ejemplo n.º 3
0
        public void BuildMessageTest()
        {
            var frameBuilder = new WebSocketFrameBuilder(ProxyConfig.MaxByteArrayLength);

            var data1 = new byte[]
            {
                0b00000010, // notFIN, Binary
                0b00000001, // noMask, 1bytes
                0b11111111, // Payload
            };

            frameBuilder.TryAddData(data1, 0, data1.Length, out var readSize1, out var frame1).IsTrue();
            readSize1.Is(data1.Length);

            var data2 = new byte[]
            {
                0b00000000, // notFIN, Continuation
                0b00000001, // noMask, 1bytes
                0b00000000, // Payload
            };

            frameBuilder.TryAddData(data2, 0, data2.Length, out var readSize2, out var frame2).IsTrue();
            readSize2.Is(data2.Length);

            var data3 = new byte[]
            {
                0b10000000, // FIN, Continuation
                0b00000001, // noMask, 1bytes
                0b01010101, // Payload
            };

            frameBuilder.TryAddData(data3, 0, data3.Length, out var readSize3, out var frame3).IsTrue();
            readSize3.Is(data3.Length);

            var builder = new WebSocketMessageBuilder(null, ProxyConfig.MaxByteArrayLength);

            builder.TryCreateOrAdd(frame1, out _).IsFalse();
            builder.TryCreateOrAdd(frame2, out _).IsFalse();
            builder.TryCreateOrAdd(frame3, out var message).IsTrue();

            message.HandshakeSession.IsNull();
            message.Opcode.Is(WebSocketOpcode.Binary);
            message.PayloadData[0].Is(0b11111111);
            message.PayloadData[1].Is(0b00000000);
            message.PayloadData[2].Is(0b01010101);
        }
Ejemplo n.º 4
0
        public void PayloadLengthPart7bitsWithoutMaskTest()
        {
            var builder = new WebSocketFrameBuilder(ProxyConfig.MaxByteArrayLength);
            var data    = new byte[]
            {
                0b10000010,  // FIN, Binary
                0b00000000,  // noMask, 0bytes
            };

            builder.TryAddData(data, 0, data.Length, out var readSize, out var frame).IsTrue();
            readSize.Is(data.Length);

            frame.Fin.IsTrue();
            frame.Opcode.Is(WebSocketOpcode.Binary);
            frame.Mask.IsFalse();
            frame.PayloadLength.Is(0);
            frame.PayloadData.Length.Is(0);
        }