diff --git a/codec-dns/src/main/java/io/netty/handler/codec/dns/DatagramDnsQueryDecoder.java b/codec-dns/src/main/java/io/netty/handler/codec/dns/DatagramDnsQueryDecoder.java index 3fdd8dabdb..0c9a02ae5f 100644 --- a/codec-dns/src/main/java/io/netty/handler/codec/dns/DatagramDnsQueryDecoder.java +++ b/codec-dns/src/main/java/io/netty/handler/codec/dns/DatagramDnsQueryDecoder.java @@ -15,11 +15,9 @@ */ package io.netty.handler.codec.dns; -import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.socket.DatagramPacket; -import io.netty.handler.codec.CorruptedFrameException; import io.netty.handler.codec.MessageToMessageDecoder; import io.netty.util.internal.UnstableApi; @@ -51,71 +49,13 @@ public class DatagramDnsQueryDecoder extends MessageToMessageDecoder> 15 == 1) { - throw new CorruptedFrameException("not a query"); - } - final DnsQuery query = - new DatagramDnsQuery( - packet.sender(), - packet.recipient(), - id, - DnsOpCode.valueOf((byte) (flags >> 11 & 0xf))); - query.setRecursionDesired((flags >> 8 & 1) == 1); - query.setZ(flags >> 4 & 0x7); - return query; - } - - private void decodeQuestions(DnsQuery query, ByteBuf buf, int questionCount) throws Exception { - for (int i = questionCount; i > 0; i--) { - query.addRecord(DnsSection.QUESTION, recordDecoder.decodeQuestion(buf)); - } - } - - private void decodeRecords( - DnsQuery query, DnsSection section, ByteBuf buf, int count) throws Exception { - for (int i = count; i > 0; i--) { - final DnsRecord r = recordDecoder.decodeRecord(buf); - if (r == null) { - // Truncated response - break; - } - - query.addRecord(section, r); - } + ctx.fireChannelRead(query); } } diff --git a/codec-dns/src/test/java/io/netty/handler/codec/dns/DnsQueryTest.java b/codec-dns/src/test/java/io/netty/handler/codec/dns/DnsQueryTest.java index 8002ff2ae3..272a62be3e 100644 --- a/codec-dns/src/test/java/io/netty/handler/codec/dns/DnsQueryTest.java +++ b/codec-dns/src/test/java/io/netty/handler/codec/dns/DnsQueryTest.java @@ -27,15 +27,19 @@ import java.util.List; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; public class DnsQueryTest { @Test - public void writeQueryTest() throws Exception { + public void testEncodeAndDecodeQuery() { InetSocketAddress addr = SocketUtils.socketAddress("8.8.8.8", 53); - EmbeddedChannel embedder = new EmbeddedChannel(new DatagramDnsQueryEncoder()); + EmbeddedChannel writeChannel = new EmbeddedChannel(new DatagramDnsQueryEncoder()); + EmbeddedChannel readChannel = new EmbeddedChannel(new DatagramDnsQueryDecoder()); + List queries = new ArrayList<>(5); queries.add(new DatagramDnsQuery(null, addr, 1).setRecord( DnsSection.QUESTION, @@ -59,12 +63,17 @@ public class DnsQueryTest { assertThat(query.count(DnsSection.AUTHORITY), is(0)); assertThat(query.count(DnsSection.ADDITIONAL), is(0)); - embedder.writeOutbound(query); + assertTrue(writeChannel.writeOutbound(query)); - DatagramPacket packet = embedder.readOutbound(); + DatagramPacket packet = writeChannel.readOutbound(); assertTrue(packet.content().isReadable()); - packet.release(); - assertNull(embedder.readOutbound()); + assertTrue(readChannel.writeInbound(packet)); + assertEquals(query, readChannel.readInbound()); + assertNull(writeChannel.readOutbound()); + assertNull(readChannel.readInbound()); } + + assertFalse(writeChannel.finish()); + assertFalse(readChannel.finish()); } }