예제 #1
0
        public void VerifyChannelHandlerCanBeReusedInPipeline()
        {
            _connection.Setup(x => x.IsServer).Returns(true);
            _handler = NewHandler();
            // Only read the connection preface...after preface is read internal state of Http2ConnectionHandler
            // is expected to change relative to the pipeline.
            IByteBuffer preface = Http2CodecUtil.ConnectionPrefaceBuf();

            _handler.ChannelRead(_ctx.Object, preface);
            _decoder.Verify(
                x => x.DecodeFrame(
                    It.IsAny <IChannelHandlerContext>(),
                    It.IsAny <IByteBuffer>(),
                    It.IsAny <List <object> >()),
                Times.Never());

            // Now remove and add the this.handler...this is setting up the test condition.
            _handler.HandlerRemoved(_ctx.Object);
            _handler.HandlerAdded(_ctx.Object);

            // Now verify we can continue as normal, reading connection preface plus more.
            IByteBuffer prefacePlusSome = AddSettingsHeader(Unpooled.Buffer().WriteBytes(Http2CodecUtil.ConnectionPrefaceBuf()));

            _handler.ChannelRead(_ctx.Object, prefacePlusSome);
            _decoder.Verify(
                x => x.DecodeFrame(
                    It.Is <IChannelHandlerContext>(v => v == _ctx.Object),
                    It.IsAny <IByteBuffer>(),
                    It.IsAny <List <object> >()),
                Times.AtLeastOnce);
        }
예제 #2
0
        public void ClientShouldveSentPrefaceAndSettingsFrameWhenUserEventIsTriggered()
        {
            _connection.Setup(x => x.IsServer).Returns(false);
            _channel.Setup(x => x.IsActive).Returns(false);
            _handler = NewHandler();
            _channel.Setup(x => x.IsActive).Returns(true);

            var evt = Http2ConnectionPrefaceAndSettingsFrameWrittenEvent.Instance;

            AtomicBoolean verified = new AtomicBoolean(false);

            _ctx
            .Setup(x => x.FireUserEventTriggered(It.Is <object>(v => ReferenceEquals(v, evt))))
            .Returns <object>(msg =>
            {
                Assert.Same(msg, evt);
                _ctx.Verify(x => x.WriteAsync(It.Is <object>(d => Http2CodecUtil.ConnectionPrefaceBuf().Equals((IByteBuffer)d))));
                _encoder.Verify(
                    x => x.WriteSettingsAsync(
                        It.Is <IChannelHandlerContext>(v => v == _ctx.Object),
                        It.IsAny <Http2Settings>(),
                        It.IsAny <IPromise>()));
                verified.Value = true;
                return(_ctx.Object);
            });

            _handler.ChannelActive(_ctx.Object);
            Assert.True(verified.Value);
        }
예제 #3
0
 public void ServerShouldNotSendClientPrefaceStringWhenActive()
 {
     _connection.Setup(x => x.IsServer).Returns(true);
     _channel.Setup(x => x.IsActive).Returns(false);
     _handler = NewHandler();
     _channel.Setup(x => x.IsActive).Returns(true);
     _handler.ChannelActive(_ctx.Object);
     _ctx.Verify(x => x.WriteAsync(It.Is <object>(d => Http2CodecUtil.ConnectionPrefaceBuf().Equals((IByteBuffer)d))), Times.Never());
 }
예제 #4
0
        private static void TestUpgrade(Http2ConnectionHandler handler, IChannelHandler multiplexer)
        {
            IFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Options, "*");

            request.Headers.Set(HttpHeaderNames.Host, "netty.io");
            request.Headers.Set(HttpHeaderNames.Connection, "Upgrade, HTTP2-Settings");
            request.Headers.Set(HttpHeaderNames.Upgrade, "h2c");
            request.Headers.Set((AsciiString)"HTTP2-Settings", "AAMAAABkAAQAAP__");

            var                     parent  = new Mock <IServerChannel>();
            EmbeddedChannel         channel = new EmbeddedChannel(parent.Object, DefaultChannelId.NewInstance(), false, true, new ChannelHandlerAdapter());
            IChannelHandlerContext  ctx     = channel.Pipeline.FirstContext();
            Http2ServerUpgradeCodec codec;

            if (multiplexer == null)
            {
                codec = new Http2ServerUpgradeCodec(handler);
            }
            else
            {
                codec = new Http2ServerUpgradeCodec((Http2FrameCodec)handler, multiplexer);
            }
            Assert.True(codec.PrepareUpgradeResponse(ctx, request, new DefaultHttpHeaders()));
            codec.UpgradeTo(ctx, request);
            // Flush the channel to ensure we write out all buffered data
            channel.Flush();

            channel.WriteInbound(Http2CodecUtil.ConnectionPrefaceBuf());
            Http2FrameInboundWriter writer = new Http2FrameInboundWriter(channel);

            writer.WriteInboundSettings(new Http2Settings());
            writer.WriteInboundRstStream(Http2CodecUtil.HttpUpgradeStreamId, Http2Error.Cancel);

            Assert.Same(handler, channel.Pipeline.Remove <Http2ConnectionHandler>());
            Assert.Null(channel.Pipeline.Get <Http2ConnectionHandler>());
            Assert.True(channel.Finish());

            // Check that the preface was send (a.k.a the settings frame)
            var settingsBuffer = channel.ReadOutbound <IByteBuffer>();

            Assert.NotNull(settingsBuffer);
            settingsBuffer.Release();

            var buf = channel.ReadOutbound <IByteBuffer>();

            Assert.NotNull(buf);
            buf.Release();

            Assert.Null(channel.ReadOutbound());
        }
예제 #5
0
        private void SetUp(Http2FrameCodecBuilder frameCodecBuilder, Http2Settings initialRemoteSettings)
        {
            // Some tests call this method twice. Once with JUnit's @Before and once directly to pass special settings.
            // This call ensures that in case of two consecutive calls to setUp(), the previous channel is shutdown and
            // ByteBufs are released correctly.
            Dispose0();

            _frameWriter = Http2TestUtil.MockedFrameWriter();

            var builder = frameCodecBuilder.FrameWriter(_frameWriter.Object);

            builder.FrameLogger     = new Http2FrameLogger(Common.Internal.Logging.InternalLogLevel.TRACE);
            builder.InitialSettings = initialRemoteSettings;
            _frameCodec             = frameCodecBuilder.Build();
            _inboundHandler         = new LastInboundHandler();

            _channel            = new EmbeddedChannel();
            _frameInboundWriter = new Http2FrameInboundWriter(_channel);
            //channel.Connect(new InetSocketAddress(0));
            _channel.Pipeline.AddLast(_frameCodec);
            _channel.Pipeline.AddLast(_inboundHandler);
            _channel.Pipeline.FireChannelActive();

            // Handshake
            _frameWriter.Verify(
                x => x.WriteSettingsAsync(
                    It.Is <IChannelHandlerContext>(v => v == _frameCodec._ctx),
                    It.IsAny <Http2Settings>(),
                    It.IsAny <IPromise>()));
            _frameWriter.VerifyNoOtherCalls();
            _channel.WriteInbound(Http2CodecUtil.ConnectionPrefaceBuf());

            _frameInboundWriter.WriteInboundSettings(initialRemoteSettings);
            _frameWriter.Verify(
                x => x.WriteSettingsAckAsync(
                    It.Is <IChannelHandlerContext>(v => v == _frameCodec._ctx),
                    It.IsAny <IPromise>()));
            _frameInboundWriter.WriteInboundSettingsAck();

            var settingsFrame = _inboundHandler.ReadInbound <IHttp2SettingsFrame>();

            Assert.NotNull(settingsFrame);
            var settingsAckFrame = _inboundHandler.ReadInbound <IHttp2SettingsAckFrame>();

            Assert.NotNull(settingsAckFrame);
        }
예제 #6
0
        public void ServerReceivingClientPrefaceStringFollowedByNonSettingsShouldHandleException()
        {
            _connection.Setup(x => x.IsServer).Returns(true);
            _handler = NewHandler();

            // Create a connection preface followed by a bunch of zeros (i.e. not a settings frame).
            IByteBuffer buf = Unpooled.Buffer().WriteBytes(Http2CodecUtil.ConnectionPrefaceBuf()).WriteZero(10);

            _handler.ChannelRead(_ctx.Object, buf);
            var captor = new ArgumentCaptor <IByteBuffer>();

            _frameWriter.Verify(
                x => x.WriteGoAwayAsync(
                    It.Is <IChannelHandlerContext>(v => v == _ctx.Object),
                    It.Is <int>(v => v == 0),
                    It.Is <Http2Error>(v => v == Http2Error.ProtocolError),
                    It.Is <IByteBuffer>(v => captor.Capture(v)),
                    It.Is <IPromise>(v => v == _promise)),
                Times.AtLeastOnce);
            Assert.Equal(0, captor.GetValue().ReferenceCount);
        }
예제 #7
0
        public void ServerReceivingValidClientPrefaceStringShouldContinueReadingFrames()
        {
            _connection.Setup(x => x.IsServer).Returns(true);
            _handler = NewHandler();
            IByteBuffer prefacePlusSome = AddSettingsHeader(Unpooled.Buffer().WriteBytes(Http2CodecUtil.ConnectionPrefaceBuf()));

            _handler.ChannelRead(_ctx.Object, prefacePlusSome);
            _decoder.Verify(
                x => x.DecodeFrame(
                    It.Is <IChannelHandlerContext>(v => v == _ctx.Object),
                    It.IsAny <IByteBuffer>(),
                    It.IsAny <List <object> >()),
                Times.AtLeastOnce);
        }