DatagramDnsResponseDecoder should rethrow as CorruptedFrameException (#10714)
Motivation: DatagramDnsResponseDecoder should rethrow as CorruptedFrameException if an IndexOutOfBoundsException happens. Modifications: - Catch IndexOutOfBoundsException and rethrow as CorruptedFrameException - Add a testcase Result: Less noise in the logs
This commit is contained in:
parent
ad8fe88abd
commit
ee4164ab8f
@ -18,6 +18,7 @@ package io.netty.handler.codec.dns;
|
|||||||
import io.netty.channel.ChannelHandler;
|
import io.netty.channel.ChannelHandler;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import io.netty.channel.socket.DatagramPacket;
|
import io.netty.channel.socket.DatagramPacket;
|
||||||
|
import io.netty.handler.codec.CorruptedFrameException;
|
||||||
import io.netty.handler.codec.MessageToMessageDecoder;
|
import io.netty.handler.codec.MessageToMessageDecoder;
|
||||||
import io.netty.util.internal.UnstableApi;
|
import io.netty.util.internal.UnstableApi;
|
||||||
|
|
||||||
@ -55,7 +56,11 @@ public class DatagramDnsResponseDecoder extends MessageToMessageDecoder<Datagram
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void decode(ChannelHandlerContext ctx, DatagramPacket packet, List<Object> out) throws Exception {
|
protected void decode(ChannelHandlerContext ctx, DatagramPacket packet, List<Object> out) throws Exception {
|
||||||
|
try {
|
||||||
out.add(decodeResponse(ctx, packet));
|
out.add(decodeResponse(ctx, packet));
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
throw new CorruptedFrameException("Unable to decode response", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected DnsResponse decodeResponse(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
|
protected DnsResponse decodeResponse(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
|
||||||
|
@ -29,6 +29,8 @@ import java.net.InetSocketAddress;
|
|||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.hamcrest.Matchers.*;
|
import static org.hamcrest.Matchers.*;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
public class DnsResponseTest {
|
public class DnsResponseTest {
|
||||||
|
|
||||||
@ -90,6 +92,7 @@ public class DnsResponseTest {
|
|||||||
|
|
||||||
envelope.release();
|
envelope.release();
|
||||||
}
|
}
|
||||||
|
assertFalse(embedder.finish());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
@ -100,6 +103,22 @@ public class DnsResponseTest {
|
|||||||
EmbeddedChannel embedder = new EmbeddedChannel(new DatagramDnsResponseDecoder());
|
EmbeddedChannel embedder = new EmbeddedChannel(new DatagramDnsResponseDecoder());
|
||||||
ByteBuf packet = embedder.alloc().buffer(512).writeBytes(malformedLoopPacket);
|
ByteBuf packet = embedder.alloc().buffer(512).writeBytes(malformedLoopPacket);
|
||||||
exception.expect(CorruptedFrameException.class);
|
exception.expect(CorruptedFrameException.class);
|
||||||
|
try {
|
||||||
embedder.writeInbound(new DatagramPacket(packet, null, new InetSocketAddress(0)));
|
embedder.writeInbound(new DatagramPacket(packet, null, new InetSocketAddress(0)));
|
||||||
|
} finally {
|
||||||
|
assertFalse(embedder.finish());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void readIncompleteResponseTest() {
|
||||||
|
EmbeddedChannel embedder = new EmbeddedChannel(new DatagramDnsResponseDecoder());
|
||||||
|
ByteBuf packet = embedder.alloc().buffer(512);
|
||||||
|
exception.expect(CorruptedFrameException.class);
|
||||||
|
try {
|
||||||
|
embedder.writeInbound(new DatagramPacket(packet, null, new InetSocketAddress(0)));
|
||||||
|
} finally {
|
||||||
|
assertFalse(embedder.finish());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user