📄 recordtest.java
字号:
n2.toWire(o, null); byte[] data = o.toByteArray(); Record rec = Record.newRecord(n, t, d, ttl, data.length, data); String out = rec.toString(); assertFalse(out.indexOf(n.toString()) == -1); assertFalse(out.indexOf(n2.toString()) == -1); assertFalse(out.indexOf("NS") == -1); assertFalse(out.indexOf("IN") == -1); assertFalse(out.indexOf(ttl+"") == -1); Options.set("BINDTTL"); out = rec.toString(); assertFalse(out.indexOf(n.toString()) == -1); assertFalse(out.indexOf(n2.toString()) == -1); assertFalse(out.indexOf("NS") == -1); assertFalse(out.indexOf("IN") == -1); assertFalse(out.indexOf(TTL.format(ttl)) == -1); Options.set("noPrintIN"); out = rec.toString(); assertFalse(out.indexOf(n.toString()) == -1); assertFalse(out.indexOf(n2.toString()) == -1); assertFalse(out.indexOf("NS") == -1); assertTrue(out.indexOf("IN") == -1); assertFalse(out.indexOf(TTL.format(ttl)) == -1); } public void test_byteArrayFromString() throws TextParseException { String in = "the 98 \" \' quick 0xAB brown"; byte[] out = SubRecord.byteArrayFromString(in); assertTrue(Arrays.equals(in.getBytes(), out)); in = " \\031Aa\\;\\\"\\\\~\\127\\255"; byte[] exp = new byte[] { ' ', 0x1F, 'A', 'a', ';', '"', '\\', 0x7E, 0x7F, (byte)0xFF }; out = SubRecord.byteArrayFromString(in); assertTrue(Arrays.equals(exp, out)); } public void test_byteArrayFromString_invalid() { StringBuffer b = new StringBuffer(); for( int i=0; i<257; ++i){ b.append('A'); } try { SubRecord.byteArrayFromString(b.toString()); fail("TextParseException not thrown"); } catch( TextParseException e ){} try { SubRecord.byteArrayFromString("\\256"); fail("TextParseException not thrown"); } catch( TextParseException e ){} try { SubRecord.byteArrayFromString("\\25a"); fail("TextParseException not thrown"); } catch( TextParseException e ){} try { SubRecord.byteArrayFromString("\\25"); fail("TextParseException not thrown"); } catch( TextParseException e ){} b.append("\\233"); try { SubRecord.byteArrayFromString(b.toString()); fail("TextParseException not thrown"); } catch( TextParseException e ){} } public void test_byteArrayToString() { byte[] in = new byte[] { ' ', 0x1F, 'A', 'a', ';', '"', '\\', 0x7E, 0x7F, (byte)0xFF }; String exp = "\" \\031Aa\\;\\\"\\\\~\\127\\255\""; assertEquals(exp, SubRecord.byteArrayToString(in, true)); } public void test_unknownToString() { byte[] data = new byte[] { (byte)0x12, (byte)0x34, (byte)0x56, (byte)0x78, (byte)0x9A, (byte)0xBC, (byte)0xDE, (byte)0xFF }; String out = SubRecord.unknownToString(data); assertFalse(out.indexOf("" + data.length) == -1); assertFalse(out.indexOf("123456789ABCDEFF") == -1); } public void test_fromString() throws IOException, TextParseException { Name n = Name.fromString("My.N."); Name n2 = Name.fromString("My.Second.Name."); int t = Type.A; int d = DClass.IN; int ttl = 0xABE99; String sa = "191.234.43.10"; InetAddress addr = InetAddress.getByName(sa); byte[] b = new byte[] { (byte)191, (byte)234, (byte)43, (byte)10 }; Tokenizer st = new Tokenizer(sa); Record rec = Record.fromString(n, t, d, ttl, st, n2); assertTrue(rec instanceof ARecord); assertEquals(n, rec.getName()); assertEquals(t, rec.getType()); assertEquals(d, rec.getDClass()); assertEquals(ttl, rec.getTTL()); assertEquals(addr, ((ARecord)rec).getAddress()); String unkData = SubRecord.unknownToString(b); st = new Tokenizer(unkData); rec = Record.fromString(n, t, d, ttl, st, n2); assertTrue(rec instanceof ARecord); assertEquals(n, rec.getName()); assertEquals(t, rec.getType()); assertEquals(d, rec.getDClass()); assertEquals(ttl, rec.getTTL()); assertEquals(addr, ((ARecord)rec).getAddress()); } public void test_fromString_invalid() throws IOException, TextParseException { Name n = Name.fromString("My.N."); Name rel = Name.fromString("My.R"); Name n2 = Name.fromString("My.Second.Name."); int t = Type.A; int d = DClass.IN; int ttl = 0xABE99; InetAddress addr = InetAddress.getByName("191.234.43.10"); Tokenizer st = new Tokenizer("191.234.43.10"); try { Record.fromString(rel, t, d, ttl, st, n2); fail("RelativeNameException not thrown"); } catch( RelativeNameException e ){} st = new Tokenizer("191.234.43.10 another_token"); try { Record.fromString(n, t, d, ttl, st, n2); fail("TextParseException not thrown"); } catch( TextParseException e ){} st = new Tokenizer("\\# 100 ABCDE"); try { Record.fromString(n, t, d, ttl, st, n2); fail("TextParseException not thrown"); } catch( TextParseException e ){} try { Record.fromString(n, t, d, ttl, "\\# 100", n2); fail("TextParseException not thrown"); } catch( TextParseException e ){} } public void test_getRRsetType() throws TextParseException { Name n = Name.fromString("My.N."); Record r = Record.newRecord(n, Type.A, DClass.IN, 0); assertEquals(Type.A, r.getRRsetType()); r = new RRSIGRecord(n, DClass.IN, 0, Type.A, 1, 0, new Date(), new Date(), 10, n, new byte[ 0 ]); assertEquals(Type.A, r.getRRsetType()); } public void test_sameRRset() throws TextParseException { Name n = Name.fromString("My.N."); Name m = Name.fromString("My.M."); Record r1 = Record.newRecord(n, Type.A, DClass.IN, 0); Record r2 = new RRSIGRecord(n, DClass.IN, 0, Type.A, 1, 0, new Date(), new Date(), 10, n, new byte[ 0 ]); assertTrue(r1.sameRRset(r2)); assertTrue(r2.sameRRset(r1)); r1 = Record.newRecord(n, Type.A, DClass.HS, 0); r2 = new RRSIGRecord(n, DClass.IN, 0, Type.A, 1, 0, new Date(), new Date(), 10, n, new byte[ 0 ]); assertFalse(r1.sameRRset(r2)); assertFalse(r2.sameRRset(r1)); r1 = Record.newRecord(n, Type.A, DClass.IN, 0); r2 = new RRSIGRecord(m, DClass.IN, 0, Type.A, 1, 0, new Date(), new Date(), 10, n, new byte[ 0 ]); assertFalse(r1.sameRRset(r2)); assertFalse(r2.sameRRset(r1)); } public void test_equals() throws TextParseException { Name n = Name.fromString("My.N."); Name n2 = Name.fromString("my.n."); Name m = Name.fromString("My.M."); Record r1 = Record.newRecord(n, Type.A, DClass.IN, 0); assertFalse(r1.equals(null)); assertFalse(r1.equals(new Object())); Record r2 = Record.newRecord(n, Type.A, DClass.IN, 0); assertEquals(r1, r2); assertEquals(r2, r1); r2 = Record.newRecord(n2, Type.A, DClass.IN, 0); assertEquals(r1, r2); assertEquals(r2, r1); r2 = Record.newRecord(n2, Type.A, DClass.IN, 0xABCDE); assertEquals(r1, r2); assertEquals(r2, r1); r2 = Record.newRecord(m, Type.A, DClass.IN, 0xABCDE); assertFalse(r1.equals(r2)); assertFalse(r2.equals(r1)); r2 = Record.newRecord(n2, Type.MX, DClass.IN, 0xABCDE); assertFalse(r1.equals(r2)); assertFalse(r2.equals(r1)); r2 = Record.newRecord(n2, Type.A, DClass.CHAOS, 0xABCDE); assertFalse(r1.equals(r2)); assertFalse(r2.equals(r1)); byte[] d1 = new byte[] { 23, 12, 9, (byte)129 }; byte[] d2 = new byte[] { (byte)220, 1, (byte)131, (byte)212 }; r1 = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d1); r2 = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d1); assertEquals(r1, r2); assertEquals(r2, r1); r2 = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d2); assertFalse(r1.equals(r2)); assertFalse(r2.equals(r1)); } public void test_hashCode() throws TextParseException { Name n = Name.fromString("My.N."); Name n2 = Name.fromString("my.n."); Name m = Name.fromString("My.M."); byte[] d1 = new byte[] { 23, 12, 9, (byte)129 }; byte[] d2 = new byte[] { (byte)220, 1, (byte)131, (byte)212 }; Record r1 = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d1); // same record has same hash code Record r2 = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d1); assertEquals(r1.hashCode(), r2.hashCode()); // case of names should not matter r2 = Record.newRecord(n2, Type.A, DClass.IN, 0xABCDE9, d1); assertEquals(r1.hashCode(), r2.hashCode()); // different names r2 = Record.newRecord(m, Type.A, DClass.IN, 0xABCDE9, d1); assertFalse(r1.hashCode() == r2.hashCode()); // different class r2 = Record.newRecord(n, Type.A, DClass.CHAOS, 0xABCDE9, d1); assertFalse(r1.hashCode() == r2.hashCode()); // different TTL does not matter r2 = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE, d1); assertEquals(r1.hashCode(), r2.hashCode()); // different data r2 = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d2); assertFalse(r1.hashCode() == r2.hashCode()); } public void test_cloneRecord() throws TextParseException { Name n = Name.fromString("My.N."); byte[] d = new byte[] { 23, 12, 9, (byte)129 }; Record r = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d); Record r2 = r.cloneRecord(); assertNotSame(r, r2); assertEquals(r, r2); r = new SubRecord(n, Type.A, DClass.IN, 0xABCDE9); try { r.cloneRecord(); fail("IllegalStateException not thrown"); } catch( IllegalStateException e ){} } public void test_withName() throws TextParseException { Name n = Name.fromString("My.N."); Name m = Name.fromString("My.M.Name."); Name rel = Name.fromString("My.Relative.Name"); byte[] d = new byte[] { 23, 12, 9, (byte)129 }; Record r = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d); Record r1 = r.withName(m); assertEquals(m, r1.getName()); assertEquals(Type.A, r1.getType()); assertEquals(DClass.IN, r1.getDClass()); assertEquals(0xABCDE9, r1.getTTL()); assertEquals(((ARecord)r).getAddress(), ((ARecord)r1).getAddress()); try { r.withName(rel); fail("RelativeNameException not thrown"); } catch( RelativeNameException e ){} } public void test_withDClass() throws TextParseException { Name n = Name.fromString("My.N."); byte[] d = new byte[] { 23, 12, 9, (byte)129 }; Record r = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d); Record r1 = r.withDClass(DClass.HESIOD, 0x9876); assertEquals(n, r1.getName()); assertEquals(Type.A, r1.getType()); assertEquals(DClass.HESIOD, r1.getDClass()); assertEquals(0x9876, r1.getTTL()); assertEquals(((ARecord)r).getAddress(), ((ARecord)r1).getAddress()); } public void test_setTTL() throws TextParseException, UnknownHostException { Name n = Name.fromString("My.N."); byte[] d = new byte[] { 23, 12, 9, (byte)129 }; InetAddress exp = InetAddress.getByName("23.12.9.129"); Record r = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d); assertEquals(0xABCDE9, r.getTTL()); r.setTTL(0x9876); assertEquals(n, r.getName()); assertEquals(Type.A, r.getType()); assertEquals(DClass.IN, r.getDClass()); assertEquals(0x9876, r.getTTL()); assertEquals(exp, ((ARecord)r).getAddress()); } public void test_compareTo() throws TextParseException { Name n = Name.fromString("My.N."); Name n2 = Name.fromString("my.n."); Name m = Name.fromString("My.M."); byte[] d = new byte[] { 23, 12, 9, (byte)129 }; byte[] d2 = new byte[] { 23, 12, 9, (byte)128 }; Record r1 = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d); Record r2 = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d); assertEquals(0, r1.compareTo(r1)); assertEquals(0, r1.compareTo(r2)); assertEquals(0, r2.compareTo(r1)); // name comparison should be canonical r2 = Record.newRecord(n2, Type.A, DClass.IN, 0xABCDE9, d); assertEquals(0, r1.compareTo(r2)); assertEquals(0, r2.compareTo(r1)); // different name r2 = Record.newRecord(m, Type.A, DClass.IN, 0xABCDE9, d); assertEquals(n.compareTo(m), r1.compareTo(r2)); assertEquals(m.compareTo(n), r2.compareTo(r1)); // different DClass r2 = Record.newRecord(n, Type.A, DClass.CHAOS, 0xABCDE9, d); assertEquals(DClass.IN-DClass.CHAOS, r1.compareTo(r2)); assertEquals(DClass.CHAOS-DClass.IN, r2.compareTo(r1)); // different Type r2 = Record.newRecord(n, Type.NS, DClass.IN, 0xABCDE9, m.toWire()); assertEquals(Type.A-Type.NS, r1.compareTo(r2)); assertEquals(Type.NS-Type.A, r2.compareTo(r1)); // different data (same length) r2 = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d2); assertEquals(1, r1.compareTo(r2)); assertEquals(-1, r2.compareTo(r1)); // different data (one a prefix of the other) m = Name.fromString("My.N.L."); r1 = Record.newRecord(n, Type.NS, DClass.IN, 0xABCDE9, n.toWire()); r2 = Record.newRecord(n, Type.NS, DClass.IN, 0xABCDE9, m.toWire()); assertEquals(-1, r1.compareTo(r2)); assertEquals(1, r2.compareTo(r1)); } public void test_getAdditionalName() throws TextParseException { Name n = Name.fromString("My.N."); Record r = new SubRecord(n, Type.A, DClass.IN, 0xABCDE9); assertNull(r.getAdditionalName()); } public void test_checkU8() { try {Record.checkU8("field", -1); fail("IllegalArgumentException not thrown");} catch( IllegalArgumentException e ){} assertEquals(0, Record.checkU8("field", 0)); assertEquals(0x9D, Record.checkU8("field", 0x9D)); assertEquals(0xFF, Record.checkU8("field", 0xFF)); try {Record.checkU8("field", 0x100); fail("IllegalArgumentException not thrown");} catch( IllegalArgumentException e ){} } public void test_checkU16() { try {Record.checkU16("field", -1); fail("IllegalArgumentException not thrown");} catch( IllegalArgumentException e ){} assertEquals(0, Record.checkU16("field", 0)); assertEquals(0x9DA1, Record.checkU16("field", 0x9DA1)); assertEquals(0xFFFF, Record.checkU16("field", 0xFFFF)); try {Record.checkU16("field", 0x10000); fail("IllegalArgumentException not thrown");} catch( IllegalArgumentException e ){} } public void test_checkU32() { try {Record.checkU32("field", -1); fail("IllegalArgumentException not thrown");} catch( IllegalArgumentException e ){} assertEquals(0, Record.checkU32("field", 0)); assertEquals(0x9DA1F02DL, Record.checkU32("field", 0x9DA1F02DL)); assertEquals(0xFFFFFFFFL, Record.checkU32("field", 0xFFFFFFFFL)); try {Record.checkU32("field", 0x100000000L); fail("IllegalArgumentException not thrown");} catch( IllegalArgumentException e ){} } public void test_checkName() throws TextParseException { Name n = Name.fromString("My.N."); Name m = Name.fromString("My.m"); assertEquals(n, Record.checkName("field", n)); try { Record.checkName("field", m); fail("RelativeNameException not thrown"); } catch( RelativeNameException e ){} }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -