/* * Copyright 2012 Future Systems * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.krakenapps.dns; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.nio.ByteBuffer; import org.junit.Ignore; import org.junit.Test; import org.krakenapps.dns.rr.A; import org.krakenapps.dns.rr.AAAA; import org.krakenapps.dns.rr.NS; import org.krakenapps.dns.rr.PTR; import org.krakenapps.dns.rr.SRV; public class DnsMessageCodecTest { @Test public void simpleQueryTest() { byte[] b = new byte[] { (byte) 0x1a, (byte) 0x5e, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x06, (byte) 0x6f, (byte) 0x66, (byte) 0x66, (byte) 0x69, (byte) 0x63, (byte) 0x65, (byte) 0x06, (byte) 0x6e, (byte) 0x63, (byte) 0x68, (byte) 0x6f, (byte) 0x76, (byte) 0x79, (byte) 0x03, (byte) 0x6e, (byte) 0x65, (byte) 0x74, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01 }; DnsMessage msg = DnsMessageCodec.decode(ByteBuffer.wrap(b)); assertTrue(msg.getFlags().isQuery()); assertEquals(1, msg.getQuestionCount()); assertEquals(0, msg.getAnswerCount()); assertEquals(0, msg.getAuthorityCount()); assertEquals(0, msg.getAdditionalCount()); assertEquals("office.nchovy.net", msg.getQuestions().get(0).getName()); } @Test public void decompressLabelTest() { byte[] b = new byte[] { (byte) 0x04, (byte) 0xd2, (byte) 0x81, (byte) 0x80, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a, (byte) 0x6b, (byte) 0x72, (byte) 0x61, (byte) 0x6b, (byte) 0x65, (byte) 0x6e, (byte) 0x61, (byte) 0x70, (byte) 0x70, (byte) 0x73, (byte) 0x03, (byte) 0x6f, (byte) 0x72, (byte) 0x67, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0xc0, (byte) 0x0c, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x10, (byte) 0xc7, (byte) 0x00, (byte) 0x04, (byte) 0x0e, (byte) 0x3f, (byte) 0xd9, (byte) 0x34 }; DnsMessage msg = DnsMessageCodec.decode(ByteBuffer.wrap(b)); assertEquals(1, msg.getQuestionCount()); assertEquals(1, msg.getAnswerCount()); A a = (A) msg.getAnswers().get(0); assertEquals("krakenapps.org", a.getName()); assertEquals(0x110c7, a.getTtl()); } @Test public void nsRecordTest() { // rr type 2 byte[] b = new byte[] { (byte) 0x04, (byte) 0xd2, (byte) 0x80, (byte) 0x80, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0x03, (byte) 0x05, (byte) 0x6e, (byte) 0x61, (byte) 0x76, (byte) 0x65, (byte) 0x72, (byte) 0x03, (byte) 0x63, (byte) 0x6f, (byte) 0x6d, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0xc0, (byte) 0x0c, (byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0xf8, (byte) 0xff, (byte) 0x00, (byte) 0x06, (byte) 0x03, (byte) 0x6e, (byte) 0x73, (byte) 0x32, (byte) 0xc0, (byte) 0x0c, (byte) 0xc0, (byte) 0x1b, (byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0xf8, (byte) 0xff, (byte) 0x00, (byte) 0x06, (byte) 0x03, (byte) 0x6e, (byte) 0x73, (byte) 0x33, (byte) 0xc0, (byte) 0x0c, (byte) 0xc0, (byte) 0x1b, (byte) 0x00, (byte) 0x02, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0xf8, (byte) 0xff, (byte) 0x00, (byte) 0x06, (byte) 0x03, (byte) 0x6e, (byte) 0x73, (byte) 0x31, (byte) 0xc0, (byte) 0x0c, (byte) 0xc0, (byte) 0x27, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0xc9, (byte) 0x1b, (byte) 0x00, (byte) 0x04, (byte) 0x3d, (byte) 0xf7, (byte) 0xca, (byte) 0x7b, (byte) 0xc0, (byte) 0x39, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0xf8, (byte) 0x03, (byte) 0x00, (byte) 0x04, (byte) 0xaf, (byte) 0x9e, (byte) 0x1e, (byte) 0x5a, (byte) 0xc0, (byte) 0x4b, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0xc9, (byte) 0x1b, (byte) 0x00, (byte) 0x04, (byte) 0x77, (byte) 0xcd, (byte) 0xf0, (byte) 0xa5 }; DnsMessage msg = DnsMessageCodec.decode(ByteBuffer.wrap(b)); assertEquals(1, msg.getQuestionCount()); assertEquals(3, msg.getAuthorityCount()); assertEquals(3, msg.getAdditionalCount()); NS ns1 = (NS) msg.getAuthorities().get(0); assertEquals("ns2.naver.com", ns1.getNsDomain()); NS ns2 = (NS) msg.getAuthorities().get(1); assertEquals("ns3.naver.com", ns2.getNsDomain()); NS ns3 = (NS) msg.getAuthorities().get(2); assertEquals("ns1.naver.com", ns3.getNsDomain()); A a1 = (A) msg.getAdditionals().get(0); assertEquals("ns2.naver.com", a1.getName()); assertEquals("61.247.202.123", a1.getAddress().getHostAddress()); A a2 = (A) msg.getAdditionals().get(1); assertEquals("ns3.naver.com", a2.getName()); assertEquals("175.158.30.90", a2.getAddress().getHostAddress()); A a3 = (A) msg.getAdditionals().get(2); assertEquals("ns1.naver.com", a3.getName()); assertEquals("119.205.240.165", a3.getAddress().getHostAddress()); } @Test public void parseTest() { byte[] b = new byte[] { (byte) 0xe9, (byte) 0xff, (byte) 0x81, (byte) 0x80, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x0e, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x08, (byte) 0x64, (byte) 0x6f, (byte) 0x77, (byte) 0x6e, (byte) 0x6c, (byte) 0x6f, (byte) 0x61, (byte) 0x64, (byte) 0x0d, (byte) 0x77, (byte) 0x69, (byte) 0x6e, (byte) 0x64, (byte) 0x6f, (byte) 0x77, (byte) 0x73, (byte) 0x75, (byte) 0x70, (byte) 0x64, (byte) 0x61, (byte) 0x74, (byte) 0x65, (byte) 0x03, (byte) 0x63, (byte) 0x6f, (byte) 0x6d, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0xc0, (byte) 0x0c, (byte) 0x00, (byte) 0x05, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x0b, (byte) 0x03, (byte) 0x00, (byte) 0x22, (byte) 0x08, (byte) 0x64, (byte) 0x6f, (byte) 0x77, (byte) 0x6e, (byte) 0x6c, (byte) 0x6f, (byte) 0x61, (byte) 0x64, (byte) 0x0d, (byte) 0x77, (byte) 0x69, (byte) 0x6e, (byte) 0x64, (byte) 0x6f, (byte) 0x77, (byte) 0x73, (byte) 0x75, (byte) 0x70, (byte) 0x64, (byte) 0x61, (byte) 0x74, (byte) 0x65, (byte) 0x05, (byte) 0x6e, (byte) 0x73, (byte) 0x61, (byte) 0x74, (byte) 0x63, (byte) 0x03, (byte) 0x6e, (byte) 0x65, (byte) 0x74, (byte) 0x00, (byte) 0xc0, (byte) 0x38, (byte) 0x00, (byte) 0x05, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x22, (byte) 0x00, (byte) 0x14, (byte) 0x04, (byte) 0x6d, (byte) 0x61, (byte) 0x69, (byte) 0x6e, (byte) 0x02, (byte) 0x64, (byte) 0x6c, (byte) 0x02, (byte) 0x77, (byte) 0x75, (byte) 0x06, (byte) 0x61, (byte) 0x6b, (byte) 0x61, (byte) 0x64, (byte) 0x6e, (byte) 0x73, (byte) 0xc0, (byte) 0x55, (byte) 0xc0, (byte) 0x66, (byte) 0x00, (byte) 0x05, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x88, (byte) 0x00, (byte) 0x07, (byte) 0x04, (byte) 0x69, (byte) 0x6e, (byte) 0x74, (byte) 0x6c, (byte) 0xc0, (byte) 0x6b, (byte) 0xc0, (byte) 0x86, (byte) 0x00, (byte) 0x05, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x88, (byte) 0x00, (byte) 0x0f, (byte) 0x02, (byte) 0x64, (byte) 0x6c, (byte) 0x02, (byte) 0x77, (byte) 0x75, (byte) 0x02, (byte) 0x6d, (byte) 0x73, (byte) 0x03, (byte) 0x67, (byte) 0x65, (byte) 0x6f, (byte) 0xc0, (byte) 0x71, (byte) 0xc0, (byte) 0x99, (byte) 0x00, (byte) 0x05, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x09, (byte) 0xfb, (byte) 0x00, (byte) 0x10, (byte) 0x03, (byte) 0x61, (byte) 0x32, (byte) 0x36, (byte) 0x02, (byte) 0x6d, (byte) 0x73, (byte) 0x06, (byte) 0x61, (byte) 0x6b, (byte) 0x61, (byte) 0x6d, (byte) 0x61, (byte) 0x69, (byte) 0xc0, (byte) 0x55, (byte) 0xc0, (byte) 0xb4, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0b, (byte) 0x00, (byte) 0x04, (byte) 0xad, (byte) 0xdf, (byte) 0xe8, (byte) 0x0a, (byte) 0xc0, (byte) 0xb4, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0b, (byte) 0x00, (byte) 0x04, (byte) 0xad, (byte) 0xdf, (byte) 0xe8, (byte) 0x28, (byte) 0xc0, (byte) 0xb4, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0b, (byte) 0x00, (byte) 0x04, (byte) 0xad, (byte) 0xdf, (byte) 0xe8, (byte) 0x30, (byte) 0xc0, (byte) 0xb4, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0b, (byte) 0x00, (byte) 0x04, (byte) 0xad, (byte) 0xdf, (byte) 0xe8, (byte) 0x31, (byte) 0xc0, (byte) 0xb4, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0b, (byte) 0x00, (byte) 0x04, (byte) 0xad, (byte) 0xdf, (byte) 0xe8, (byte) 0x33, (byte) 0xc0, (byte) 0xb4, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0b, (byte) 0x00, (byte) 0x04, (byte) 0xad, (byte) 0xdf, (byte) 0xe8, (byte) 0x3b, (byte) 0xc0, (byte) 0xb4, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0b, (byte) 0x00, (byte) 0x04, (byte) 0xad, (byte) 0xdf, (byte) 0xe8, (byte) 0x5b, (byte) 0xc0, (byte) 0xb4, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0b, (byte) 0x00, (byte) 0x04, (byte) 0xad, (byte) 0xdf, (byte) 0xe8, (byte) 0x61, (byte) 0xc0, (byte) 0xb4, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0b, (byte) 0x00, (byte) 0x04, (byte) 0xad, (byte) 0xdf, (byte) 0xe8, (byte) 0x08 }; DnsMessageCodec.decode(ByteBuffer.wrap(b)); } @Test public void srvQueryTest() { // rr type 33 byte[] b = new byte[] { (byte) 0x7e, (byte) 0xac, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x05, (byte) 0x5f, (byte) 0x6c, (byte) 0x64, (byte) 0x61, (byte) 0x70, (byte) 0x04, (byte) 0x5f, (byte) 0x74, (byte) 0x63, (byte) 0x70, (byte) 0x17, (byte) 0x44, (byte) 0x65, (byte) 0x66, (byte) 0x61, (byte) 0x75, (byte) 0x6c, (byte) 0x74, (byte) 0x2d, (byte) 0x46, (byte) 0x69, (byte) 0x72, (byte) 0x73, (byte) 0x74, (byte) 0x2d, (byte) 0x53, (byte) 0x69, (byte) 0x74, (byte) 0x65, (byte) 0x2d, (byte) 0x4e, (byte) 0x61, (byte) 0x6d, (byte) 0x65, (byte) 0x06, (byte) 0x5f, (byte) 0x73, (byte) 0x69, (byte) 0x74, (byte) 0x65, (byte) 0x73, (byte) 0x06, (byte) 0x6f, (byte) 0x66, (byte) 0x66, (byte) 0x69, (byte) 0x63, (byte) 0x65, (byte) 0x06, (byte) 0x6e, (byte) 0x63, (byte) 0x68, (byte) 0x6f, (byte) 0x76, (byte) 0x79, (byte) 0x03, (byte) 0x6e, (byte) 0x65, (byte) 0x74, (byte) 0x00, (byte) 0x00, (byte) 0x21, (byte) 0x00, (byte) 0x01 }; DnsMessage msg = DnsMessageCodec.decode(ByteBuffer.wrap(b)); assertTrue(msg.getFlags().isQuery()); assertEquals(1, msg.getQuestionCount()); assertEquals(0, msg.getAnswerCount()); assertEquals(0, msg.getAuthorityCount()); assertEquals(0, msg.getAdditionalCount()); SRV srv = (SRV) msg.getQuestions().get(0); assertEquals(33, srv.getType()); } @Test public void ptrQueryTest() { // rr type 12 byte[] b = new byte[] { (byte) 0xd7, (byte) 0x0b, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x31, (byte) 0x03, (byte) 0x31, (byte) 0x34, (byte) 0x30, (byte) 0x02, (byte) 0x34, (byte) 0x31, (byte) 0x02, (byte) 0x31, (byte) 0x30, (byte) 0x07, (byte) 0x69, (byte) 0x6e, (byte) 0x2d, (byte) 0x61, (byte) 0x64, (byte) 0x64, (byte) 0x72, (byte) 0x04, (byte) 0x61, (byte) 0x72, (byte) 0x70, (byte) 0x61, (byte) 0x00, (byte) 0x00, (byte) 0x0c, (byte) 0x00, (byte) 0x01 }; DnsMessage msg = DnsMessageCodec.decode(ByteBuffer.wrap(b)); assertTrue(msg.getFlags().isQuery()); assertEquals(1, msg.getQuestionCount()); assertEquals(0, msg.getAnswerCount()); assertEquals(0, msg.getAuthorityCount()); assertEquals(0, msg.getAdditionalCount()); PTR ptr = (PTR) msg.getQuestions().get(0); assertEquals("1.140.41.10.in-addr.arpa", ptr.getName()); } @Test public void aaaaQueryTest() { // rr type 28 byte[] b = new byte[] { (byte) 0xe3, (byte) 0xcd, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x6e, (byte) 0x74, (byte) 0x70, (byte) 0x06, (byte) 0x75, (byte) 0x62, (byte) 0x75, (byte) 0x6e, (byte) 0x74, (byte) 0x75, (byte) 0x03, (byte) 0x63, (byte) 0x6f, (byte) 0x6d, (byte) 0x00, (byte) 0x00, (byte) 0x1c, (byte) 0x00, (byte) 0x01 }; DnsMessage msg = DnsMessageCodec.decode(ByteBuffer.wrap(b)); assertTrue(msg.getFlags().isQuery()); assertEquals(1, msg.getQuestionCount()); assertEquals(0, msg.getAnswerCount()); assertEquals(0, msg.getAuthorityCount()); assertEquals(0, msg.getAdditionalCount()); AAAA aaaa = (AAAA) msg.getQuestions().get(0); assertEquals("ntp.ubuntu.com", aaaa.getName()); } @Ignore @Test public void optTest() { // rr type 41 byte[] b = new byte[] { (byte) 0xa0, (byte) 0x47, (byte) 0x81, (byte) 0x80, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x07, (byte) 0x65, (byte) 0x78, (byte) 0x74, (byte) 0x2d, (byte) 0x64, (byte) 0x6f, (byte) 0x63, (byte) 0x03, (byte) 0x6f, (byte) 0x72, (byte) 0x67, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x29, (byte) 0x10, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00 }; DnsMessage msg = DnsMessageCodec.decode(ByteBuffer.wrap(b)); System.out.println(msg); } }