diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/LocalHostPermitHandlerTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/LocalHostPermitHandlerTest.java new file mode 100644 index 00000000000..98695722a36 --- /dev/null +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/LocalHostPermitHandlerTest.java @@ -0,0 +1,40 @@ +package org.apache.dubbo.qos.server.handler; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +import java.net.InetAddress; +import java.net.InetSocketAddress; + +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class LocalHostPermitHandlerTest { + @Test + public void testHandlerAdded() throws Exception { + ChannelHandlerContext context = mock(ChannelHandlerContext.class); + Channel channel = mock(Channel.class); + when(context.channel()).thenReturn(channel); + InetAddress addr = mock(InetAddress.class); + when(addr.isLoopbackAddress()).thenReturn(false); + InetSocketAddress address = new InetSocketAddress(addr, 12345); + when(channel.remoteAddress()).thenReturn(address); + ChannelFuture future = mock(ChannelFuture.class); + when(context.writeAndFlush(any(ByteBuf.class))).thenReturn(future); + LocalHostPermitHandler handler = new LocalHostPermitHandler(false); + handler.handlerAdded(context); + ArgumentCaptor captor = ArgumentCaptor.forClass(ByteBuf.class); + verify(context).writeAndFlush(captor.capture()); + assertThat(new String(captor.getValue().array()), containsString("Foreign Ip Not Permitted")); + verify(future).addListener(ChannelFutureListener.CLOSE); + } +} diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/QosProcessHandlerTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/QosProcessHandlerTest.java new file mode 100644 index 00000000000..5199c70acd4 --- /dev/null +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/QosProcessHandlerTest.java @@ -0,0 +1,51 @@ +package org.apache.dubbo.qos.server.handler; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelPipeline; +import io.netty.handler.codec.LineBasedFrameDecoder; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpServerCodec; +import io.netty.handler.codec.string.StringDecoder; +import io.netty.handler.codec.string.StringEncoder; +import org.junit.Test; +import org.mockito.Mockito; + +import java.util.Collections; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; + +public class QosProcessHandlerTest { + @Test + public void testDecodeHttp() throws Exception { + ByteBuf buf = Unpooled.wrappedBuffer(new byte[] {'G'}); + ChannelHandlerContext context = Mockito.mock(ChannelHandlerContext.class); + ChannelPipeline pipeline = Mockito.mock(ChannelPipeline.class); + Mockito.when(context.pipeline()).thenReturn(pipeline); + QosProcessHandler handler = new QosProcessHandler("welcome", false); + handler.decode(context, buf, Collections.emptyList()); + verify(pipeline).addLast(any(HttpServerCodec.class)); + verify(pipeline).addLast(any(HttpObjectAggregator.class)); + verify(pipeline).addLast(any(HttpProcessHandler.class)); + verify(pipeline).remove(handler); + } + + @Test + public void testDecodeTelnet() throws Exception { + ByteBuf buf = Unpooled.wrappedBuffer(new byte[] {'A'}); + ChannelHandlerContext context = Mockito.mock(ChannelHandlerContext.class); + ChannelPipeline pipeline = Mockito.mock(ChannelPipeline.class); + Mockito.when(context.pipeline()).thenReturn(pipeline); + QosProcessHandler handler = new QosProcessHandler("welcome", false); + handler.decode(context, buf, Collections.emptyList()); + verify(pipeline).addLast(any(LineBasedFrameDecoder.class)); + verify(pipeline).addLast(any(StringDecoder.class)); + verify(pipeline).addLast(any(StringEncoder.class)); + verify(pipeline).addLast(any(TelnetProcessHandler.class)); + verify(pipeline).remove(handler); + } + + +} diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/TelnetProcessHandlerTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/TelnetProcessHandlerTest.java new file mode 100644 index 00000000000..ab3174ef68d --- /dev/null +++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/TelnetProcessHandlerTest.java @@ -0,0 +1,56 @@ +package org.apache.dubbo.qos.server.handler; + +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; + +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class TelnetProcessHandlerTest { + @Test + public void testPrompt() throws Exception { + ChannelHandlerContext context = mock(ChannelHandlerContext.class); + TelnetProcessHandler handler = new TelnetProcessHandler(); + handler.channelRead0(context, ""); + verify(context).writeAndFlush(QosProcessHandler.prompt); + } + + @Test + public void testBye() throws Exception { + ChannelHandlerContext context = mock(ChannelHandlerContext.class); + TelnetProcessHandler handler = new TelnetProcessHandler(); + ChannelFuture future = mock(ChannelFuture.class); + when(context.writeAndFlush("BYE!\n")).thenReturn(future); + handler.channelRead0(context, "quit"); + verify(future).addListener(ChannelFutureListener.CLOSE); + } + + @Test + public void testUnknownCommand() throws Exception { + ChannelHandlerContext context = mock(ChannelHandlerContext.class); + TelnetProcessHandler handler = new TelnetProcessHandler(); + handler.channelRead0(context, "unknown"); + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(context, Mockito.atLeastOnce()).writeAndFlush(captor.capture()); + assertThat(captor.getAllValues(), contains("unknown :no such command", "\r\ndubbo>")); + } + + @Test + public void testGreeting() throws Exception { + ChannelHandlerContext context = mock(ChannelHandlerContext.class); + TelnetProcessHandler handler = new TelnetProcessHandler(); + handler.channelRead0(context, "greeting"); + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(context).writeAndFlush(captor.capture()); + assertThat(captor.getValue(), containsString("greeting")); + assertThat(captor.getValue(), containsString("dubbo>")); + } +}