Esempio n. 1
0
        public async Task TlsWrite(int[] frameLengths, bool isClient, SslProtocols serverProtocol, SslProtocols clientProtocol)
        {
            this.Output.WriteLine($"frameLengths: {string.Join(", ", frameLengths)}");
            this.Output.WriteLine($"isClient: {isClient}");
            this.Output.WriteLine($"serverProtocol: {serverProtocol}");
            this.Output.WriteLine($"clientProtocol: {clientProtocol}");

            var writeStrategy = new AsIsWriteStrategy();

            this.Output.WriteLine($"writeStrategy: {writeStrategy}");

            var executor = new SingleThreadEventExecutor("test executor", TimeSpan.FromMilliseconds(10));

            try
            {
                var writeTasks = new List <Task>();
                var pair       = await SetupStreamAndChannelAsync(isClient, executor, writeStrategy, serverProtocol, clientProtocol, writeTasks);

                EmbeddedChannel ch           = pair.Item1;
                SslStream       driverStream = pair.Item2;

                int         randomSeed     = Environment.TickCount;
                var         random         = new Random(randomSeed);
                IByteBuffer expectedBuffer = Unpooled.Buffer(16 * 1024);
                foreach (IEnumerable <int> lengths in frameLengths.Split(x => x < 0))
                {
                    ch.WriteOutbound(lengths.Select(len =>
                    {
                        var data = new byte[len];
                        random.NextBytes(data);
                        expectedBuffer.WriteBytes(data);
                        return((object)Unpooled.WrappedBuffer(data));
                    }).ToArray());
                }

                IByteBuffer finalReadBuffer = Unpooled.Buffer(16 * 1024);
                var         readBuffer      = new byte[16 * 1024 * 10];
                await ReadOutboundAsync(
                    async() =>
                {
                    int read = await driverStream.ReadAsync(readBuffer, 0, readBuffer.Length);
                    return(Unpooled.WrappedBuffer(readBuffer, 0, read));
                },
                    expectedBuffer.ReadableBytes, finalReadBuffer, TestTimeout);

                bool isEqual = ByteBufferUtil.Equals(expectedBuffer, finalReadBuffer);
                if (!isEqual)
                {
                    Assert.True(isEqual, $"---Expected:\n{ByteBufferUtil.PrettyHexDump(expectedBuffer)}\n---Actual:\n{ByteBufferUtil.PrettyHexDump(finalReadBuffer)}");
                }
                driverStream.Dispose();
                Assert.False(ch.Finish());
            }
            finally
            {
                await executor.ShutdownGracefullyAsync(TimeSpan.Zero, TimeSpan.Zero);
            }
        }
Esempio n. 2
0
        public async Task TlsWrite(int[] frameLengths, bool isClient, SslProtocols protocol, string targetHost)
        {
            this.Output.WriteLine("frameLengths: " + string.Join(", ", frameLengths));
            this.Output.WriteLine($"protocol: {protocol}");
            this.Output.WriteLine($"targetHost: {targetHost}");

            var writeStrategy = new AsIsWriteStrategy();
            var executor      = new DefaultEventExecutor();

            try
            {
                var writeTasks = new List <Task>();
                var pair       = await SetupStreamAndChannelAsync(isClient, executor, writeStrategy, protocol, writeTasks, targetHost);

                EmbeddedChannel ch           = pair.Item1;
                SslStream       driverStream = pair.Item2;

                int         randomSeed     = Environment.TickCount;
                var         random         = new Random(randomSeed);
                IByteBuffer expectedBuffer = Unpooled.Buffer(16 * 1024);
                foreach (IEnumerable <int> lengths in frameLengths.Split(x => x < 0))
                {
                    ch.WriteOutbound(lengths.Select(len =>
                    {
                        var data = new byte[len];
                        random.NextBytes(data);
                        expectedBuffer.WriteBytes(data);
                        return((object)Unpooled.WrappedBuffer(data));
                    }).ToArray());
                }

                IByteBuffer finalReadBuffer = Unpooled.Buffer(16 * 1024);
                var         readBuffer      = new byte[16 * 1024 * 10];
                await ReadOutboundAsync(
                    async() =>
                {
                    int read = await driverStream.ReadAsync(readBuffer, 0, readBuffer.Length);
                    return(Unpooled.WrappedBuffer(readBuffer, 0, read));
                },
                    expectedBuffer.ReadableBytes, finalReadBuffer, TestTimeout);

                Assert.True(ByteBufferUtil.Equals(expectedBuffer, finalReadBuffer), $"---Expected:\n{ByteBufferUtil.PrettyHexDump(expectedBuffer)}\n---Actual:\n{ByteBufferUtil.PrettyHexDump(finalReadBuffer)}");

                if (!isClient)
                {
                    // check if snihandler got replaced with tls handler
                    Assert.Null(ch.Pipeline.Get <SniHandler>());
                    Assert.NotNull(ch.Pipeline.Get <TlsHandler>());
                }

                driverStream.Dispose();
                Assert.False(ch.Finish());
            }
            finally
            {
                await executor.ShutdownGracefullyAsync(TimeSpan.Zero, TimeSpan.Zero);
            }
        }
Esempio n. 3
0
        public async Task TlsWrite(int[] frameLengths, bool isClient, SslProtocols protocol)
        {
            this.Output.WriteLine("frameLengths: " + string.Join(", ", frameLengths));

            var writeStrategy = new AsIsWriteStrategy();

            var writeTasks = new List <Task>();
            var pair       = await SetupStreamAndChannelAsync(isClient, writeStrategy, protocol, writeTasks);

            EmbeddedChannel ch           = pair.Item1;
            SslStream       driverStream = pair.Item2;

            int         randomSeed     = Environment.TickCount;
            var         random         = new Random(randomSeed);
            IByteBuffer expectedBuffer = Unpooled.Buffer(16 * 1024);

            foreach (IEnumerable <int> lengths in frameLengths.Split(x => x < 0))
            {
                ch.WriteOutbound(lengths.Select(len =>
                {
                    var data = new byte[len];
                    random.NextBytes(data);
                    expectedBuffer.WriteBytes(data);
                    return((object)Unpooled.WrappedBuffer(data));
                }).ToArray());
            }

            IByteBuffer finalReadBuffer = Unpooled.Buffer(16 * 1024);
            var         readBuffer      = new byte[16 * 1024 * 10];

            await ReadOutboundAsync(
                () =>
            {
                int read = driverStream.Read(readBuffer, 0, readBuffer.Length);
                return(Unpooled.WrappedBuffer(readBuffer, 0, read));
            },
                expectedBuffer.ReadableBytes, finalReadBuffer, TestTimeout);

            Assert.True(ByteBufferUtil.Equals(expectedBuffer, finalReadBuffer), $"---Expected:\n{ByteBufferUtil.PrettyHexDump(expectedBuffer)}\n---Actual:\n{ByteBufferUtil.PrettyHexDump(finalReadBuffer)}");
        }
Esempio n. 4
0
        public async Task TlsWrite(int[] frameLengths, bool isClient, SslProtocols protocol)
        {
            this.Output.WriteLine("frameLengths: " + string.Join(", ", frameLengths));

            var writeStrategy = new AsIsWriteStrategy();

            var writeTasks = new List<Task>();
            var pair = await SetupStreamAndChannelAsync(isClient, writeStrategy, protocol, writeTasks);
            EmbeddedChannel ch = pair.Item1;
            SslStream driverStream = pair.Item2;

            int randomSeed = Environment.TickCount;
            var random = new Random(randomSeed);
            IByteBuffer expectedBuffer = Unpooled.Buffer(16 * 1024);
            foreach (IEnumerable<int> lengths in frameLengths.Split(x => x < 0))
            {
                ch.WriteOutbound(lengths.Select(len =>
                {
                    var data = new byte[len];
                    random.NextBytes(data);
                    expectedBuffer.WriteBytes(data);
                    return (object)Unpooled.WrappedBuffer(data);
                }).ToArray());
            }

            IByteBuffer finalReadBuffer = Unpooled.Buffer(16 * 1024);
            var readBuffer = new byte[16 * 1024 * 10];
            await ReadOutboundAsync(
                () =>
                {
                    int read = driverStream.Read(readBuffer, 0, readBuffer.Length);
                    return Unpooled.WrappedBuffer(readBuffer, 0, read);
                },
                expectedBuffer.ReadableBytes, finalReadBuffer, TestTimeout);
            Assert.True(ByteBufferUtil.Equals(expectedBuffer, finalReadBuffer), $"---Expected:\n{ByteBufferUtil.PrettyHexDump(expectedBuffer)}\n---Actual:\n{ByteBufferUtil.PrettyHexDump(finalReadBuffer)}");
        }