001: // -*- Java -*-
002: //
003: // Copyright (c) 2005, Matthew J. Rutherford <rutherfo@cs.colorado.edu>
004: // Copyright (c) 2005, University of Colorado at Boulder
005: // All rights reserved.
006: //
007: // Redistribution and use in source and binary forms, with or without
008: // modification, are permitted provided that the following conditions are
009: // met:
010: //
011: // * Redistributions of source code must retain the above copyright
012: // notice, this list of conditions and the following disclaimer.
013: //
014: // * Redistributions in binary form must reproduce the above copyright
015: // notice, this list of conditions and the following disclaimer in the
016: // documentation and/or other materials provided with the distribution.
017: //
018: // * Neither the name of the University of Colorado at Boulder nor the
019: // names of its contributors may be used to endorse or promote
020: // products derived from this software without specific prior written
021: // permission.
022: //
023: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
024: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
025: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
026: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
027: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
028: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
029: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
030: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
031: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
032: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
033: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
034: //
035: package org.xbill.DNS;
036:
037: import java.io.IOException;
038: import java.net.InetAddress;
039: import java.net.UnknownHostException;
040: import java.util.Arrays;
041: import java.util.Date;
042: import junit.framework.TestCase;
043:
044: public class RecordTest extends TestCase {
045: private static class SubRecord extends Record {
046: public SubRecord() {
047: }
048:
049: public SubRecord(Name name, int type, int dclass, long ttl) {
050: super (name, type, dclass, ttl);
051: }
052:
053: public Record getObject() {
054: return null;
055: }
056:
057: public void rrFromWire(DNSInput in) throws IOException {
058: }
059:
060: public String rrToString() {
061: return "{SubRecord: rrToString}";
062: }
063:
064: public void rdataFromString(Tokenizer t, Name origin)
065: throws IOException {
066: }
067:
068: public void rrToWire(DNSOutput out, Compression c,
069: boolean canonical) {
070: }
071:
072: // makes it callable by test code
073: public static byte[] byteArrayFromString(String in)
074: throws TextParseException {
075: return Record.byteArrayFromString(in);
076: }
077:
078: // make it callable by test code
079: public static String byteArrayToString(byte[] in, boolean quote) {
080: return Record.byteArrayToString(in, quote);
081: }
082:
083: // make it callable by test code
084: public static String unknownToString(byte[] in) {
085: return Record.unknownToString(in);
086: }
087:
088: public Object clone() throws CloneNotSupportedException {
089: throw new CloneNotSupportedException();
090: }
091: }
092:
093: public void test_ctor_0arg() {
094: SubRecord sr = new SubRecord();
095: assertNull(sr.getName());
096: assertEquals(0, sr.getType());
097: assertEquals(0, sr.getTTL());
098: assertEquals(0, sr.getDClass());
099: }
100:
101: public void test_ctor_4arg() throws TextParseException {
102: Name n = Name.fromString("my.name.");
103: int t = Type.A;
104: int d = DClass.IN;
105: long ttl = 0xABCDEL;
106:
107: SubRecord r = new SubRecord(n, t, d, ttl);
108: assertEquals(n, r.getName());
109: assertEquals(t, r.getType());
110: assertEquals(d, r.getDClass());
111: assertEquals(ttl, r.getTTL());
112: }
113:
114: public void test_ctor_4arg_invalid() throws TextParseException {
115: Name n = Name.fromString("my.name.");
116: Name r = Name.fromString("my.relative.name");
117: int t = Type.A;
118: int d = DClass.IN;
119: long ttl = 0xABCDEL;
120:
121: try {
122: new SubRecord(r, t, d, ttl);
123: fail("RelativeNameException not thrown");
124: } catch (RelativeNameException e) {
125: }
126:
127: try {
128: new SubRecord(n, -1, d, ttl);
129: fail("InvalidTypeException not thrown");
130: } catch (InvalidTypeException e) {
131: }
132:
133: try {
134: new SubRecord(n, t, -1, ttl);
135: fail("InvalidDClassException not thrown");
136: } catch (InvalidDClassException e) {
137: }
138:
139: try {
140: new SubRecord(n, t, d, -1);
141: fail("InvalidTTLException not thrown");
142: } catch (InvalidTTLException e) {
143: }
144: }
145:
146: public void test_newRecord_3arg() throws TextParseException {
147: Name n = Name.fromString("my.name.");
148: Name r = Name.fromString("my.relative.name");
149: int t = Type.A;
150: int d = DClass.IN;
151:
152: Record rec = Record.newRecord(n, t, d);
153: assertTrue(rec instanceof EmptyRecord);
154: assertEquals(n, rec.getName());
155: assertEquals(t, rec.getType());
156: assertEquals(d, rec.getDClass());
157: assertEquals(0, rec.getTTL());
158:
159: try {
160: Record.newRecord(r, t, d);
161: fail("RelativeNameException not thrown");
162: } catch (RelativeNameException e) {
163: }
164: }
165:
166: public void test_newRecord_4arg() throws TextParseException {
167: Name n = Name.fromString("my.name.");
168: Name r = Name.fromString("my.relative.name");
169: int t = Type.A;
170: int d = DClass.IN;
171: int ttl = 0xDBE8;
172:
173: Record rec = Record.newRecord(n, t, d, ttl);
174: assertTrue(rec instanceof EmptyRecord);
175: assertEquals(n, rec.getName());
176: assertEquals(t, rec.getType());
177: assertEquals(d, rec.getDClass());
178: assertEquals(ttl, rec.getTTL());
179:
180: try {
181: Record.newRecord(r, t, d, ttl);
182: fail("RelativeNameException not thrown");
183: } catch (RelativeNameException e) {
184: }
185: }
186:
187: public void test_newRecord_5arg() throws TextParseException,
188: UnknownHostException {
189: Name n = Name.fromString("my.name.");
190: int t = Type.A;
191: int d = DClass.IN;
192: int ttl = 0xDBE8;
193: byte[] data = new byte[] { (byte) 123, (byte) 232, (byte) 0,
194: (byte) 255 };
195: InetAddress exp = InetAddress.getByName("123.232.0.255");
196:
197: Record rec = Record.newRecord(n, t, d, ttl, data);
198: assertTrue(rec instanceof ARecord);
199: assertEquals(n, rec.getName());
200: assertEquals(t, rec.getType());
201: assertEquals(d, rec.getDClass());
202: assertEquals(ttl, rec.getTTL());
203: assertEquals(exp, ((ARecord) rec).getAddress());
204: }
205:
206: public void test_newRecord_6arg() throws TextParseException,
207: UnknownHostException {
208: Name n = Name.fromString("my.name.");
209: int t = Type.A;
210: int d = DClass.IN;
211: int ttl = 0xDBE8;
212: byte[] data = new byte[] { (byte) 123, (byte) 232, (byte) 0,
213: (byte) 255 };
214: InetAddress exp = InetAddress.getByName("123.232.0.255");
215:
216: Record rec = Record.newRecord(n, t, d, ttl, 0, null);
217: assertTrue(rec instanceof EmptyRecord);
218: assertEquals(n, rec.getName());
219: assertEquals(t, rec.getType());
220: assertEquals(d, rec.getDClass());
221: assertEquals(ttl, rec.getTTL());
222:
223: rec = Record.newRecord(n, t, d, ttl, data.length, data);
224: assertTrue(rec instanceof ARecord);
225: assertEquals(n, rec.getName());
226: assertEquals(t, rec.getType());
227: assertEquals(d, rec.getDClass());
228: assertEquals(ttl, rec.getTTL());
229: assertEquals(exp, ((ARecord) rec).getAddress());
230:
231: rec = Record.newRecord(n, Type.NIMLOC, d, ttl, data.length,
232: data);
233: assertTrue(rec instanceof UNKRecord);
234: assertEquals(n, rec.getName());
235: assertEquals(Type.NIMLOC, rec.getType());
236: assertEquals(d, rec.getDClass());
237: assertEquals(ttl, rec.getTTL());
238: assertTrue(Arrays.equals(data, ((UNKRecord) rec).getData()));
239: }
240:
241: public void test_newRecord_6arg_invalid() throws TextParseException {
242: Name n = Name.fromString("my.name.");
243: Name r = Name.fromString("my.relative.name");
244: int t = Type.A;
245: int d = DClass.IN;
246: int ttl = 0xDBE8;
247: byte[] data = new byte[] { (byte) 123, (byte) 232, (byte) 0,
248: (byte) 255 };
249:
250: assertNull(Record.newRecord(n, t, d, ttl, 0, new byte[0]));
251: assertNull(Record.newRecord(n, t, d, ttl, 1, new byte[0]));
252: assertNull(Record
253: .newRecord(n, t, d, ttl, data.length + 1, data));
254: assertNull(Record.newRecord(n, t, d, ttl, 5, new byte[] {
255: data[0], data[1], data[2], data[3], 0 }));
256: try {
257: Record.newRecord(r, t, d, ttl, 0, null);
258: fail("RelativeNameException not thrown");
259: } catch (RelativeNameException e) {
260: }
261:
262: }
263:
264: public void test_fromWire() throws IOException, TextParseException,
265: UnknownHostException
266:
267: {
268: Name n = Name.fromString("my.name.");
269: int t = Type.A;
270: int d = DClass.IN;
271: int ttl = 0xDBE8;
272: byte[] data = new byte[] { (byte) 123, (byte) 232, (byte) 0,
273: (byte) 255 };
274: InetAddress exp = InetAddress.getByName("123.232.0.255");
275:
276: DNSOutput out = new DNSOutput();
277: n.toWire(out, null);
278: out.writeU16(t);
279: out.writeU16(d);
280: out.writeU32(ttl);
281: out.writeU16(data.length);
282: out.writeByteArray(data);
283:
284: DNSInput in = new DNSInput(out.toByteArray());
285:
286: Record rec = Record.fromWire(in, Section.ANSWER, false);
287: assertTrue(rec instanceof ARecord);
288: assertEquals(n, rec.getName());
289: assertEquals(t, rec.getType());
290: assertEquals(d, rec.getDClass());
291: assertEquals(ttl, rec.getTTL());
292: assertEquals(exp, ((ARecord) rec).getAddress());
293:
294: in = new DNSInput(out.toByteArray());
295: rec = Record.fromWire(in, Section.QUESTION, false);
296: assertTrue(rec instanceof EmptyRecord);
297: assertEquals(n, rec.getName());
298: assertEquals(t, rec.getType());
299: assertEquals(d, rec.getDClass());
300: assertEquals(0, rec.getTTL());
301:
302: in = new DNSInput(out.toByteArray());
303: rec = Record.fromWire(in, Section.QUESTION);
304: assertTrue(rec instanceof EmptyRecord);
305: assertEquals(n, rec.getName());
306: assertEquals(t, rec.getType());
307: assertEquals(d, rec.getDClass());
308: assertEquals(0, rec.getTTL());
309:
310: rec = Record.fromWire(out.toByteArray(), Section.QUESTION);
311: assertTrue(rec instanceof EmptyRecord);
312: assertEquals(n, rec.getName());
313: assertEquals(t, rec.getType());
314: assertEquals(d, rec.getDClass());
315: assertEquals(0, rec.getTTL());
316:
317: out = new DNSOutput();
318: n.toWire(out, null);
319: out.writeU16(t);
320: out.writeU16(d);
321: out.writeU32(ttl);
322: out.writeU16(0);
323:
324: in = new DNSInput(out.toByteArray());
325:
326: rec = Record.fromWire(in, Section.ANSWER, true);
327: assertTrue(rec instanceof EmptyRecord);
328: assertEquals(n, rec.getName());
329: assertEquals(t, rec.getType());
330: assertEquals(d, rec.getDClass());
331: assertEquals(ttl, rec.getTTL());
332:
333: }
334:
335: public void test_toWire() throws IOException, TextParseException,
336: UnknownHostException
337:
338: {
339: Name n = Name.fromString("my.name.");
340: int t = Type.A;
341: int d = DClass.IN;
342: int ttl = 0xDBE8;
343: byte[] data = new byte[] { (byte) 123, (byte) 232, (byte) 0,
344: (byte) 255 };
345:
346: // a non-QUESTION
347: DNSOutput out = new DNSOutput();
348: n.toWire(out, null);
349: out.writeU16(t);
350: out.writeU16(d);
351: out.writeU32(ttl);
352: out.writeU16(data.length);
353: out.writeByteArray(data);
354:
355: byte[] exp = out.toByteArray();
356:
357: Record rec = Record.newRecord(n, t, d, ttl, data.length, data);
358:
359: out = new DNSOutput();
360:
361: rec.toWire(out, Section.ANSWER, null);
362:
363: byte[] after = out.toByteArray();
364:
365: assertTrue(Arrays.equals(exp, after));
366:
367: // an equivalent call
368: after = rec.toWire(Section.ANSWER);
369: assertTrue(Arrays.equals(exp, after));
370:
371: // a QUESTION entry
372: out = new DNSOutput();
373: n.toWire(out, null);
374: out.writeU16(t);
375: out.writeU16(d);
376:
377: exp = out.toByteArray();
378: out = new DNSOutput();
379: rec.toWire(out, Section.QUESTION, null);
380: after = out.toByteArray();
381:
382: assertTrue(Arrays.equals(exp, after));
383:
384: }
385:
386: public void test_toWireCanonical() throws IOException,
387: TextParseException, UnknownHostException
388:
389: {
390: Name n = Name.fromString("My.Name.");
391: int t = Type.A;
392: int d = DClass.IN;
393: int ttl = 0xDBE8;
394: byte[] data = new byte[] { (byte) 123, (byte) 232, (byte) 0,
395: (byte) 255 };
396:
397: DNSOutput out = new DNSOutput();
398: n.toWireCanonical(out);
399: out.writeU16(t);
400: out.writeU16(d);
401: out.writeU32(ttl);
402: out.writeU16(data.length);
403: out.writeByteArray(data);
404:
405: byte[] exp = out.toByteArray();
406:
407: Record rec = Record.newRecord(n, t, d, ttl, data.length, data);
408:
409: byte[] after = rec.toWireCanonical();
410: assertTrue(Arrays.equals(exp, after));
411: }
412:
413: public void test_rdataToWireCanonical() throws IOException,
414: TextParseException, UnknownHostException
415:
416: {
417: Name n = Name.fromString("My.Name.");
418: Name n2 = Name.fromString("My.Second.Name.");
419: int t = Type.NS;
420: int d = DClass.IN;
421: int ttl = 0xABE99;
422: DNSOutput out = new DNSOutput();
423: n2.toWire(out, null);
424: byte[] data = out.toByteArray();
425:
426: out = new DNSOutput();
427: n2.toWireCanonical(out);
428: byte[] exp = out.toByteArray();
429:
430: Record rec = Record.newRecord(n, t, d, ttl, data.length, data);
431: assertTrue(rec instanceof NSRecord);
432:
433: byte[] after = rec.rdataToWireCanonical();
434:
435: assertTrue(Arrays.equals(exp, after));
436: }
437:
438: public void test_rdataToString() throws IOException,
439: TextParseException, UnknownHostException
440:
441: {
442: Name n = Name.fromString("My.Name.");
443: Name n2 = Name.fromString("My.Second.Name.");
444: int t = Type.NS;
445: int d = DClass.IN;
446: int ttl = 0xABE99;
447: DNSOutput out = new DNSOutput();
448: n2.toWire(out, null);
449: byte[] data = out.toByteArray();
450:
451: Record rec = Record.newRecord(n, t, d, ttl, data.length, data);
452: assertTrue(rec instanceof NSRecord);
453: assertEquals(rec.rrToString(), rec.rdataToString());
454: }
455:
456: public void test_toString() throws TextParseException {
457: Name n = Name.fromString("My.N.");
458: Name n2 = Name.fromString("My.Second.Name.");
459: int t = Type.NS;
460: int d = DClass.IN;
461: int ttl = 0xABE99;
462: DNSOutput o = new DNSOutput();
463: n2.toWire(o, null);
464: byte[] data = o.toByteArray();
465:
466: Record rec = Record.newRecord(n, t, d, ttl, data.length, data);
467: String out = rec.toString();
468:
469: assertFalse(out.indexOf(n.toString()) == -1);
470: assertFalse(out.indexOf(n2.toString()) == -1);
471: assertFalse(out.indexOf("NS") == -1);
472: assertFalse(out.indexOf("IN") == -1);
473: assertFalse(out.indexOf(ttl + "") == -1);
474:
475: Options.set("BINDTTL");
476:
477: out = rec.toString();
478: assertFalse(out.indexOf(n.toString()) == -1);
479: assertFalse(out.indexOf(n2.toString()) == -1);
480: assertFalse(out.indexOf("NS") == -1);
481: assertFalse(out.indexOf("IN") == -1);
482: assertFalse(out.indexOf(TTL.format(ttl)) == -1);
483:
484: Options.set("noPrintIN");
485: out = rec.toString();
486: assertFalse(out.indexOf(n.toString()) == -1);
487: assertFalse(out.indexOf(n2.toString()) == -1);
488: assertFalse(out.indexOf("NS") == -1);
489: assertTrue(out.indexOf("IN") == -1);
490: assertFalse(out.indexOf(TTL.format(ttl)) == -1);
491: }
492:
493: public void test_byteArrayFromString() throws TextParseException {
494: String in = "the 98 \" \' quick 0xAB brown";
495: byte[] out = SubRecord.byteArrayFromString(in);
496: assertTrue(Arrays.equals(in.getBytes(), out));
497:
498: in = " \\031Aa\\;\\\"\\\\~\\127\\255";
499: byte[] exp = new byte[] { ' ', 0x1F, 'A', 'a', ';', '"', '\\',
500: 0x7E, 0x7F, (byte) 0xFF };
501: out = SubRecord.byteArrayFromString(in);
502: assertTrue(Arrays.equals(exp, out));
503: }
504:
505: public void test_byteArrayFromString_invalid() {
506: StringBuffer b = new StringBuffer();
507: for (int i = 0; i < 257; ++i) {
508: b.append('A');
509: }
510: try {
511: SubRecord.byteArrayFromString(b.toString());
512: fail("TextParseException not thrown");
513: } catch (TextParseException e) {
514: }
515:
516: try {
517: SubRecord.byteArrayFromString("\\256");
518: fail("TextParseException not thrown");
519: } catch (TextParseException e) {
520: }
521: try {
522: SubRecord.byteArrayFromString("\\25a");
523: fail("TextParseException not thrown");
524: } catch (TextParseException e) {
525: }
526: try {
527: SubRecord.byteArrayFromString("\\25");
528: fail("TextParseException not thrown");
529: } catch (TextParseException e) {
530: }
531:
532: b.append("\\233");
533: try {
534: SubRecord.byteArrayFromString(b.toString());
535: fail("TextParseException not thrown");
536: } catch (TextParseException e) {
537: }
538:
539: }
540:
541: public void test_byteArrayToString() {
542: byte[] in = new byte[] { ' ', 0x1F, 'A', 'a', ';', '"', '\\',
543: 0x7E, 0x7F, (byte) 0xFF };
544: String exp = "\" \\031Aa\\;\\\"\\\\~\\127\\255\"";
545: assertEquals(exp, SubRecord.byteArrayToString(in, true));
546: }
547:
548: public void test_unknownToString() {
549: byte[] data = new byte[] { (byte) 0x12, (byte) 0x34,
550: (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC,
551: (byte) 0xDE, (byte) 0xFF };
552: String out = SubRecord.unknownToString(data);
553:
554: assertFalse(out.indexOf("" + data.length) == -1);
555: assertFalse(out.indexOf("123456789ABCDEFF") == -1);
556: }
557:
558: public void test_fromString() throws IOException,
559: TextParseException {
560: Name n = Name.fromString("My.N.");
561: Name n2 = Name.fromString("My.Second.Name.");
562: int t = Type.A;
563: int d = DClass.IN;
564: int ttl = 0xABE99;
565: String sa = "191.234.43.10";
566: InetAddress addr = InetAddress.getByName(sa);
567: byte[] b = new byte[] { (byte) 191, (byte) 234, (byte) 43,
568: (byte) 10 };
569:
570: Tokenizer st = new Tokenizer(sa);
571: Record rec = Record.fromString(n, t, d, ttl, st, n2);
572: assertTrue(rec instanceof ARecord);
573: assertEquals(n, rec.getName());
574: assertEquals(t, rec.getType());
575: assertEquals(d, rec.getDClass());
576: assertEquals(ttl, rec.getTTL());
577: assertEquals(addr, ((ARecord) rec).getAddress());
578:
579: String unkData = SubRecord.unknownToString(b);
580: st = new Tokenizer(unkData);
581: rec = Record.fromString(n, t, d, ttl, st, n2);
582: assertTrue(rec instanceof ARecord);
583: assertEquals(n, rec.getName());
584: assertEquals(t, rec.getType());
585: assertEquals(d, rec.getDClass());
586: assertEquals(ttl, rec.getTTL());
587: assertEquals(addr, ((ARecord) rec).getAddress());
588: }
589:
590: public void test_fromString_invalid() throws IOException,
591: TextParseException {
592: Name n = Name.fromString("My.N.");
593: Name rel = Name.fromString("My.R");
594: Name n2 = Name.fromString("My.Second.Name.");
595: int t = Type.A;
596: int d = DClass.IN;
597: int ttl = 0xABE99;
598: InetAddress addr = InetAddress.getByName("191.234.43.10");
599:
600: Tokenizer st = new Tokenizer("191.234.43.10");
601:
602: try {
603: Record.fromString(rel, t, d, ttl, st, n2);
604: fail("RelativeNameException not thrown");
605: } catch (RelativeNameException e) {
606: }
607:
608: st = new Tokenizer("191.234.43.10 another_token");
609: try {
610: Record.fromString(n, t, d, ttl, st, n2);
611: fail("TextParseException not thrown");
612: } catch (TextParseException e) {
613: }
614:
615: st = new Tokenizer("\\# 100 ABCDE");
616: try {
617: Record.fromString(n, t, d, ttl, st, n2);
618: fail("TextParseException not thrown");
619: } catch (TextParseException e) {
620: }
621:
622: try {
623: Record.fromString(n, t, d, ttl, "\\# 100", n2);
624: fail("TextParseException not thrown");
625: } catch (TextParseException e) {
626: }
627: }
628:
629: public void test_getRRsetType() throws TextParseException {
630: Name n = Name.fromString("My.N.");
631:
632: Record r = Record.newRecord(n, Type.A, DClass.IN, 0);
633: assertEquals(Type.A, r.getRRsetType());
634:
635: r = new RRSIGRecord(n, DClass.IN, 0, Type.A, 1, 0, new Date(),
636: new Date(), 10, n, new byte[0]);
637: assertEquals(Type.A, r.getRRsetType());
638: }
639:
640: public void test_sameRRset() throws TextParseException {
641: Name n = Name.fromString("My.N.");
642: Name m = Name.fromString("My.M.");
643:
644: Record r1 = Record.newRecord(n, Type.A, DClass.IN, 0);
645: Record r2 = new RRSIGRecord(n, DClass.IN, 0, Type.A, 1, 0,
646: new Date(), new Date(), 10, n, new byte[0]);
647: assertTrue(r1.sameRRset(r2));
648: assertTrue(r2.sameRRset(r1));
649:
650: r1 = Record.newRecord(n, Type.A, DClass.HS, 0);
651: r2 = new RRSIGRecord(n, DClass.IN, 0, Type.A, 1, 0, new Date(),
652: new Date(), 10, n, new byte[0]);
653: assertFalse(r1.sameRRset(r2));
654: assertFalse(r2.sameRRset(r1));
655:
656: r1 = Record.newRecord(n, Type.A, DClass.IN, 0);
657: r2 = new RRSIGRecord(m, DClass.IN, 0, Type.A, 1, 0, new Date(),
658: new Date(), 10, n, new byte[0]);
659: assertFalse(r1.sameRRset(r2));
660: assertFalse(r2.sameRRset(r1));
661: }
662:
663: public void test_equals() throws TextParseException {
664: Name n = Name.fromString("My.N.");
665: Name n2 = Name.fromString("my.n.");
666: Name m = Name.fromString("My.M.");
667:
668: Record r1 = Record.newRecord(n, Type.A, DClass.IN, 0);
669:
670: assertFalse(r1.equals(null));
671: assertFalse(r1.equals(new Object()));
672:
673: Record r2 = Record.newRecord(n, Type.A, DClass.IN, 0);
674: assertEquals(r1, r2);
675: assertEquals(r2, r1);
676:
677: r2 = Record.newRecord(n2, Type.A, DClass.IN, 0);
678: assertEquals(r1, r2);
679: assertEquals(r2, r1);
680:
681: r2 = Record.newRecord(n2, Type.A, DClass.IN, 0xABCDE);
682: assertEquals(r1, r2);
683: assertEquals(r2, r1);
684:
685: r2 = Record.newRecord(m, Type.A, DClass.IN, 0xABCDE);
686: assertFalse(r1.equals(r2));
687: assertFalse(r2.equals(r1));
688:
689: r2 = Record.newRecord(n2, Type.MX, DClass.IN, 0xABCDE);
690: assertFalse(r1.equals(r2));
691: assertFalse(r2.equals(r1));
692:
693: r2 = Record.newRecord(n2, Type.A, DClass.CHAOS, 0xABCDE);
694: assertFalse(r1.equals(r2));
695: assertFalse(r2.equals(r1));
696:
697: byte[] d1 = new byte[] { 23, 12, 9, (byte) 129 };
698: byte[] d2 = new byte[] { (byte) 220, 1, (byte) 131, (byte) 212 };
699:
700: r1 = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d1);
701: r2 = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d1);
702:
703: assertEquals(r1, r2);
704: assertEquals(r2, r1);
705:
706: r2 = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d2);
707:
708: assertFalse(r1.equals(r2));
709: assertFalse(r2.equals(r1));
710: }
711:
712: public void test_hashCode() throws TextParseException {
713: Name n = Name.fromString("My.N.");
714: Name n2 = Name.fromString("my.n.");
715: Name m = Name.fromString("My.M.");
716: byte[] d1 = new byte[] { 23, 12, 9, (byte) 129 };
717: byte[] d2 = new byte[] { (byte) 220, 1, (byte) 131, (byte) 212 };
718:
719: Record r1 = Record
720: .newRecord(n, Type.A, DClass.IN, 0xABCDE9, d1);
721:
722: // same record has same hash code
723: Record r2 = Record
724: .newRecord(n, Type.A, DClass.IN, 0xABCDE9, d1);
725: assertEquals(r1.hashCode(), r2.hashCode());
726:
727: // case of names should not matter
728: r2 = Record.newRecord(n2, Type.A, DClass.IN, 0xABCDE9, d1);
729: assertEquals(r1.hashCode(), r2.hashCode());
730:
731: // different names
732: r2 = Record.newRecord(m, Type.A, DClass.IN, 0xABCDE9, d1);
733: assertFalse(r1.hashCode() == r2.hashCode());
734:
735: // different class
736: r2 = Record.newRecord(n, Type.A, DClass.CHAOS, 0xABCDE9, d1);
737: assertFalse(r1.hashCode() == r2.hashCode());
738:
739: // different TTL does not matter
740: r2 = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE, d1);
741: assertEquals(r1.hashCode(), r2.hashCode());
742:
743: // different data
744: r2 = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d2);
745: assertFalse(r1.hashCode() == r2.hashCode());
746: }
747:
748: public void test_cloneRecord() throws TextParseException {
749: Name n = Name.fromString("My.N.");
750: byte[] d = new byte[] { 23, 12, 9, (byte) 129 };
751: Record r = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d);
752:
753: Record r2 = r.cloneRecord();
754:
755: assertNotSame(r, r2);
756: assertEquals(r, r2);
757:
758: r = new SubRecord(n, Type.A, DClass.IN, 0xABCDE9);
759:
760: try {
761: r.cloneRecord();
762: fail("IllegalStateException not thrown");
763: } catch (IllegalStateException e) {
764: }
765: }
766:
767: public void test_withName() throws TextParseException {
768: Name n = Name.fromString("My.N.");
769: Name m = Name.fromString("My.M.Name.");
770: Name rel = Name.fromString("My.Relative.Name");
771: byte[] d = new byte[] { 23, 12, 9, (byte) 129 };
772: Record r = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d);
773:
774: Record r1 = r.withName(m);
775:
776: assertEquals(m, r1.getName());
777: assertEquals(Type.A, r1.getType());
778: assertEquals(DClass.IN, r1.getDClass());
779: assertEquals(0xABCDE9, r1.getTTL());
780: assertEquals(((ARecord) r).getAddress(), ((ARecord) r1)
781: .getAddress());
782:
783: try {
784: r.withName(rel);
785: fail("RelativeNameException not thrown");
786: } catch (RelativeNameException e) {
787: }
788: }
789:
790: public void test_withDClass() throws TextParseException {
791: Name n = Name.fromString("My.N.");
792: byte[] d = new byte[] { 23, 12, 9, (byte) 129 };
793: Record r = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d);
794:
795: Record r1 = r.withDClass(DClass.HESIOD, 0x9876);
796:
797: assertEquals(n, r1.getName());
798: assertEquals(Type.A, r1.getType());
799: assertEquals(DClass.HESIOD, r1.getDClass());
800: assertEquals(0x9876, r1.getTTL());
801: assertEquals(((ARecord) r).getAddress(), ((ARecord) r1)
802: .getAddress());
803: }
804:
805: public void test_setTTL() throws TextParseException,
806: UnknownHostException {
807: Name n = Name.fromString("My.N.");
808: byte[] d = new byte[] { 23, 12, 9, (byte) 129 };
809: InetAddress exp = InetAddress.getByName("23.12.9.129");
810: Record r = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d);
811:
812: assertEquals(0xABCDE9, r.getTTL());
813:
814: r.setTTL(0x9876);
815:
816: assertEquals(n, r.getName());
817: assertEquals(Type.A, r.getType());
818: assertEquals(DClass.IN, r.getDClass());
819: assertEquals(0x9876, r.getTTL());
820: assertEquals(exp, ((ARecord) r).getAddress());
821: }
822:
823: public void test_compareTo() throws TextParseException {
824: Name n = Name.fromString("My.N.");
825: Name n2 = Name.fromString("my.n.");
826: Name m = Name.fromString("My.M.");
827: byte[] d = new byte[] { 23, 12, 9, (byte) 129 };
828: byte[] d2 = new byte[] { 23, 12, 9, (byte) 128 };
829: Record r1 = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d);
830: Record r2 = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d);
831:
832: assertEquals(0, r1.compareTo(r1));
833:
834: assertEquals(0, r1.compareTo(r2));
835: assertEquals(0, r2.compareTo(r1));
836:
837: // name comparison should be canonical
838: r2 = Record.newRecord(n2, Type.A, DClass.IN, 0xABCDE9, d);
839: assertEquals(0, r1.compareTo(r2));
840: assertEquals(0, r2.compareTo(r1));
841:
842: // different name
843: r2 = Record.newRecord(m, Type.A, DClass.IN, 0xABCDE9, d);
844: assertEquals(n.compareTo(m), r1.compareTo(r2));
845: assertEquals(m.compareTo(n), r2.compareTo(r1));
846:
847: // different DClass
848: r2 = Record.newRecord(n, Type.A, DClass.CHAOS, 0xABCDE9, d);
849: assertEquals(DClass.IN - DClass.CHAOS, r1.compareTo(r2));
850: assertEquals(DClass.CHAOS - DClass.IN, r2.compareTo(r1));
851:
852: // different Type
853: r2 = Record.newRecord(n, Type.NS, DClass.IN, 0xABCDE9, m
854: .toWire());
855: assertEquals(Type.A - Type.NS, r1.compareTo(r2));
856: assertEquals(Type.NS - Type.A, r2.compareTo(r1));
857:
858: // different data (same length)
859: r2 = Record.newRecord(n, Type.A, DClass.IN, 0xABCDE9, d2);
860: assertEquals(1, r1.compareTo(r2));
861: assertEquals(-1, r2.compareTo(r1));
862:
863: // different data (one a prefix of the other)
864: m = Name.fromString("My.N.L.");
865: r1 = Record.newRecord(n, Type.NS, DClass.IN, 0xABCDE9, n
866: .toWire());
867: r2 = Record.newRecord(n, Type.NS, DClass.IN, 0xABCDE9, m
868: .toWire());
869: assertEquals(-1, r1.compareTo(r2));
870: assertEquals(1, r2.compareTo(r1));
871: }
872:
873: public void test_getAdditionalName() throws TextParseException {
874: Name n = Name.fromString("My.N.");
875: Record r = new SubRecord(n, Type.A, DClass.IN, 0xABCDE9);
876:
877: assertNull(r.getAdditionalName());
878: }
879:
880: public void test_checkU8() {
881: try {
882: Record.checkU8("field", -1);
883: fail("IllegalArgumentException not thrown");
884: } catch (IllegalArgumentException e) {
885: }
886: assertEquals(0, Record.checkU8("field", 0));
887: assertEquals(0x9D, Record.checkU8("field", 0x9D));
888: assertEquals(0xFF, Record.checkU8("field", 0xFF));
889: try {
890: Record.checkU8("field", 0x100);
891: fail("IllegalArgumentException not thrown");
892: } catch (IllegalArgumentException e) {
893: }
894: }
895:
896: public void test_checkU16() {
897: try {
898: Record.checkU16("field", -1);
899: fail("IllegalArgumentException not thrown");
900: } catch (IllegalArgumentException e) {
901: }
902: assertEquals(0, Record.checkU16("field", 0));
903: assertEquals(0x9DA1, Record.checkU16("field", 0x9DA1));
904: assertEquals(0xFFFF, Record.checkU16("field", 0xFFFF));
905: try {
906: Record.checkU16("field", 0x10000);
907: fail("IllegalArgumentException not thrown");
908: } catch (IllegalArgumentException e) {
909: }
910: }
911:
912: public void test_checkU32() {
913: try {
914: Record.checkU32("field", -1);
915: fail("IllegalArgumentException not thrown");
916: } catch (IllegalArgumentException e) {
917: }
918: assertEquals(0, Record.checkU32("field", 0));
919: assertEquals(0x9DA1F02DL, Record.checkU32("field", 0x9DA1F02DL));
920: assertEquals(0xFFFFFFFFL, Record.checkU32("field", 0xFFFFFFFFL));
921: try {
922: Record.checkU32("field", 0x100000000L);
923: fail("IllegalArgumentException not thrown");
924: } catch (IllegalArgumentException e) {
925: }
926: }
927:
928: public void test_checkName() throws TextParseException {
929: Name n = Name.fromString("My.N.");
930: Name m = Name.fromString("My.m");
931:
932: assertEquals(n, Record.checkName("field", n));
933:
934: try {
935: Record.checkName("field", m);
936: fail("RelativeNameException not thrown");
937: } catch (RelativeNameException e) {
938: }
939: }
940: }
|