📄 recordtest.java
字号:
// -*- Java -*-//// Copyright (c) 2005, Matthew J. Rutherford <rutherfo@cs.colorado.edu>// Copyright (c) 2005, University of Colorado at Boulder// All rights reserved.// // Redistribution and use in source and binary forms, with or without// modification, are permitted provided that the following conditions are// met:// // * Redistributions of source code must retain the above copyright// notice, this list of conditions and the following disclaimer.// // * Redistributions in binary form must reproduce the above copyright// notice, this list of conditions and the following disclaimer in the// documentation and/or other materials provided with the distribution.// // * Neither the name of the University of Colorado at Boulder nor the// names of its contributors may be used to endorse or promote// products derived from this software without specific prior written// permission.// // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.//package org.xbill.DNS;import java.io.IOException;import java.net.InetAddress;import java.net.UnknownHostException;import java.util.Arrays;import java.util.Date;import junit.framework.TestCase;public class RecordTest extends TestCase{ private static class SubRecord extends Record { public SubRecord(){} public SubRecord(Name name, int type, int dclass, long ttl) { super(name, type, dclass, ttl); } public Record getObject() { return null; } public void rrFromWire(DNSInput in) throws IOException {} public String rrToString() { return "{SubRecord: rrToString}"; } public void rdataFromString(Tokenizer t, Name origin) throws IOException {} public void rrToWire(DNSOutput out, Compression c, boolean canonical) {} // makes it callable by test code public static byte[] byteArrayFromString(String in) throws TextParseException { return Record.byteArrayFromString(in); } // make it callable by test code public static String byteArrayToString(byte[] in, boolean quote) { return Record.byteArrayToString(in, quote); } // make it callable by test code public static String unknownToString(byte[] in) { return Record.unknownToString(in); } public Object clone() throws CloneNotSupportedException { throw new CloneNotSupportedException(); } } public void test_ctor_0arg() { SubRecord sr = new SubRecord(); assertNull(sr.getName()); assertEquals(0, sr.getType()); assertEquals(0, sr.getTTL()); assertEquals(0, sr.getDClass()); } public void test_ctor_4arg() throws TextParseException { Name n = Name.fromString("my.name."); int t = Type.A; int d = DClass.IN; long ttl = 0xABCDEL; SubRecord r = new SubRecord(n, t, d, ttl); assertEquals(n, r.getName()); assertEquals(t, r.getType()); assertEquals(d, r.getDClass()); assertEquals(ttl, r.getTTL()); } public void test_ctor_4arg_invalid() throws TextParseException { Name n = Name.fromString("my.name."); Name r = Name.fromString("my.relative.name"); int t = Type.A; int d = DClass.IN; long ttl = 0xABCDEL; try { new SubRecord(r, t, d, ttl); fail("RelativeNameException not thrown"); } catch( RelativeNameException e ){} try { new SubRecord(n, -1, d, ttl); fail("InvalidTypeException not thrown"); } catch( InvalidTypeException e ){} try { new SubRecord(n, t, -1, ttl); fail("InvalidDClassException not thrown"); } catch( InvalidDClassException e ){} try { new SubRecord(n, t, d, -1); fail("InvalidTTLException not thrown"); } catch( InvalidTTLException e ){} } public void test_newRecord_3arg() throws TextParseException { Name n = Name.fromString("my.name."); Name r = Name.fromString("my.relative.name"); int t = Type.A; int d = DClass.IN; Record rec = Record.newRecord(n, t, d); assertTrue(rec instanceof EmptyRecord); assertEquals(n, rec.getName()); assertEquals(t, rec.getType()); assertEquals(d, rec.getDClass()); assertEquals(0, rec.getTTL()); try { Record.newRecord(r, t, d); fail("RelativeNameException not thrown"); } catch( RelativeNameException e ){} } public void test_newRecord_4arg() throws TextParseException { Name n = Name.fromString("my.name."); Name r = Name.fromString("my.relative.name"); int t = Type.A; int d = DClass.IN; int ttl = 0xDBE8; Record rec = Record.newRecord(n, t, d, ttl); assertTrue(rec instanceof EmptyRecord); assertEquals(n, rec.getName()); assertEquals(t, rec.getType()); assertEquals(d, rec.getDClass()); assertEquals(ttl, rec.getTTL()); try { Record.newRecord(r, t, d, ttl); fail("RelativeNameException not thrown"); } catch( RelativeNameException e ){} } public void test_newRecord_5arg() throws TextParseException, UnknownHostException { Name n = Name.fromString("my.name."); int t = Type.A; int d = DClass.IN; int ttl = 0xDBE8; byte[] data = new byte[] { (byte)123, (byte)232, (byte)0, (byte)255 }; InetAddress exp = InetAddress.getByName("123.232.0.255"); Record rec = Record.newRecord(n, t, d, ttl, data); assertTrue(rec instanceof ARecord); assertEquals(n, rec.getName()); assertEquals(t, rec.getType()); assertEquals(d, rec.getDClass()); assertEquals(ttl, rec.getTTL()); assertEquals(exp, ((ARecord)rec).getAddress()); } public void test_newRecord_6arg() throws TextParseException, UnknownHostException { Name n = Name.fromString("my.name."); int t = Type.A; int d = DClass.IN; int ttl = 0xDBE8; byte[] data = new byte[] { (byte)123, (byte)232, (byte)0, (byte)255 }; InetAddress exp = InetAddress.getByName("123.232.0.255"); Record rec = Record.newRecord(n, t, d, ttl, 0, null); assertTrue(rec instanceof EmptyRecord); assertEquals(n, rec.getName()); assertEquals(t, rec.getType()); assertEquals(d, rec.getDClass()); assertEquals(ttl, rec.getTTL()); rec = Record.newRecord(n, t, d, ttl, data.length, data); assertTrue(rec instanceof ARecord); assertEquals(n, rec.getName()); assertEquals(t, rec.getType()); assertEquals(d, rec.getDClass()); assertEquals(ttl, rec.getTTL()); assertEquals(exp, ((ARecord)rec).getAddress()); rec = Record.newRecord(n, Type.NIMLOC, d, ttl, data.length, data); assertTrue(rec instanceof UNKRecord); assertEquals(n, rec.getName()); assertEquals(Type.NIMLOC, rec.getType()); assertEquals(d, rec.getDClass()); assertEquals(ttl, rec.getTTL()); assertTrue(Arrays.equals(data, ((UNKRecord)rec).getData())); } public void test_newRecord_6arg_invalid() throws TextParseException { Name n = Name.fromString("my.name."); Name r = Name.fromString("my.relative.name"); int t = Type.A; int d = DClass.IN; int ttl = 0xDBE8; byte[] data = new byte[] { (byte)123, (byte)232, (byte)0, (byte)255 }; assertNull(Record.newRecord(n, t, d, ttl, 0, new byte[ 0 ])); assertNull(Record.newRecord(n, t, d, ttl, 1, new byte[ 0 ])); assertNull(Record.newRecord(n, t, d, ttl, data.length+1, data)); assertNull(Record.newRecord(n, t, d, ttl, 5, new byte[] { data[0], data[1], data[2], data[3], 0 })); try { Record.newRecord(r, t, d, ttl, 0, null); fail("RelativeNameException not thrown"); } catch( RelativeNameException e ){} } public void test_fromWire() throws IOException, TextParseException, UnknownHostException { Name n = Name.fromString("my.name."); int t = Type.A; int d = DClass.IN; int ttl = 0xDBE8; byte[] data = new byte[] { (byte)123, (byte)232, (byte)0, (byte)255 }; InetAddress exp = InetAddress.getByName("123.232.0.255"); DNSOutput out = new DNSOutput(); n.toWire(out, null); out.writeU16(t); out.writeU16(d); out.writeU32(ttl); out.writeU16(data.length); out.writeByteArray(data); DNSInput in = new DNSInput(out.toByteArray()); Record rec = Record.fromWire(in, Section.ANSWER, false); assertTrue(rec instanceof ARecord); assertEquals(n, rec.getName()); assertEquals(t, rec.getType()); assertEquals(d, rec.getDClass()); assertEquals(ttl, rec.getTTL()); assertEquals(exp, ((ARecord)rec).getAddress()); in = new DNSInput(out.toByteArray()); rec = Record.fromWire(in, Section.QUESTION, false); assertTrue(rec instanceof EmptyRecord); assertEquals(n, rec.getName()); assertEquals(t, rec.getType()); assertEquals(d, rec.getDClass()); assertEquals(0, rec.getTTL()); in = new DNSInput(out.toByteArray()); rec = Record.fromWire(in, Section.QUESTION); assertTrue(rec instanceof EmptyRecord); assertEquals(n, rec.getName()); assertEquals(t, rec.getType()); assertEquals(d, rec.getDClass()); assertEquals(0, rec.getTTL()); rec = Record.fromWire(out.toByteArray(), Section.QUESTION); assertTrue(rec instanceof EmptyRecord); assertEquals(n, rec.getName()); assertEquals(t, rec.getType()); assertEquals(d, rec.getDClass()); assertEquals(0, rec.getTTL()); out = new DNSOutput(); n.toWire(out, null); out.writeU16(t); out.writeU16(d); out.writeU32(ttl); out.writeU16(0); in = new DNSInput(out.toByteArray()); rec = Record.fromWire(in, Section.ANSWER, true); assertTrue(rec instanceof EmptyRecord); assertEquals(n, rec.getName()); assertEquals(t, rec.getType()); assertEquals(d, rec.getDClass()); assertEquals(ttl, rec.getTTL()); } public void test_toWire() throws IOException, TextParseException, UnknownHostException { Name n = Name.fromString("my.name."); int t = Type.A; int d = DClass.IN; int ttl = 0xDBE8; byte[] data = new byte[] { (byte)123, (byte)232, (byte)0, (byte)255 }; // a non-QUESTION DNSOutput out = new DNSOutput(); n.toWire(out, null); out.writeU16(t); out.writeU16(d); out.writeU32(ttl); out.writeU16(data.length); out.writeByteArray(data); byte[] exp = out.toByteArray(); Record rec = Record.newRecord(n, t, d, ttl, data.length, data); out = new DNSOutput(); rec.toWire(out, Section.ANSWER, null); byte[] after = out.toByteArray(); assertTrue(Arrays.equals(exp, after)); // an equivalent call after = rec.toWire(Section.ANSWER); assertTrue(Arrays.equals(exp, after)); // a QUESTION entry out = new DNSOutput(); n.toWire(out, null); out.writeU16(t); out.writeU16(d); exp = out.toByteArray(); out = new DNSOutput(); rec.toWire(out, Section.QUESTION, null); after = out.toByteArray(); assertTrue(Arrays.equals(exp, after)); } public void test_toWireCanonical() throws IOException, TextParseException, UnknownHostException { Name n = Name.fromString("My.Name."); int t = Type.A; int d = DClass.IN; int ttl = 0xDBE8; byte[] data = new byte[] { (byte)123, (byte)232, (byte)0, (byte)255 }; DNSOutput out = new DNSOutput(); n.toWireCanonical(out); out.writeU16(t); out.writeU16(d); out.writeU32(ttl); out.writeU16(data.length); out.writeByteArray(data); byte[] exp = out.toByteArray(); Record rec = Record.newRecord(n, t, d, ttl, data.length, data); byte[] after = rec.toWireCanonical(); assertTrue(Arrays.equals(exp, after)); } public void test_rdataToWireCanonical() throws IOException, TextParseException, UnknownHostException { Name n = Name.fromString("My.Name."); Name n2 = Name.fromString("My.Second.Name."); int t = Type.NS; int d = DClass.IN; int ttl = 0xABE99; DNSOutput out = new DNSOutput(); n2.toWire(out, null); byte[] data = out.toByteArray(); out = new DNSOutput(); n2.toWireCanonical(out); byte[] exp = out.toByteArray(); Record rec = Record.newRecord(n, t, d, ttl, data.length, data); assertTrue(rec instanceof NSRecord); byte[] after = rec.rdataToWireCanonical(); assertTrue(Arrays.equals(exp, after)); } public void test_rdataToString() throws IOException, TextParseException, UnknownHostException { Name n = Name.fromString("My.Name."); Name n2 = Name.fromString("My.Second.Name."); int t = Type.NS; int d = DClass.IN; int ttl = 0xABE99; DNSOutput out = new DNSOutput(); n2.toWire(out, null); byte[] data = out.toByteArray(); Record rec = Record.newRecord(n, t, d, ttl, data.length, data); assertTrue(rec instanceof NSRecord); assertEquals(rec.rrToString(), rec.rdataToString()); } public void test_toString() throws TextParseException { Name n = Name.fromString("My.N."); Name n2 = Name.fromString("My.Second.Name."); int t = Type.NS; int d = DClass.IN; int ttl = 0xABE99; DNSOutput o = new DNSOutput();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -