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 junit.framework.TestCase;
039:
040: public class SingleNameBaseTest extends TestCase {
041: private void assertEquals(byte[] exp, byte[] act) {
042: assertTrue(java.util.Arrays.equals(exp, act));
043: }
044:
045: private static class TestClass extends SingleNameBase {
046: public TestClass() {
047: }
048:
049: public TestClass(Name name, int type, int dclass, long ttl) {
050: super (name, type, dclass, ttl);
051: }
052:
053: public TestClass(Name name, int type, int dclass, long ttl,
054: Name singleName, String desc) {
055: super (name, type, dclass, ttl, singleName, desc);
056: }
057:
058: public Name getSingleName() {
059: return super .getSingleName();
060: }
061:
062: public Record getObject() {
063: return null;
064: }
065: }
066:
067: public void test_ctor() throws TextParseException {
068: TestClass tc = new TestClass();
069: assertNull(tc.getSingleName());
070:
071: Name n = Name.fromString("my.name.");
072: Name sn = Name.fromString("my.single.name.");
073:
074: tc = new TestClass(n, Type.A, DClass.IN, 100L);
075:
076: assertSame(n, tc.getName());
077: assertEquals(Type.A, tc.getType());
078: assertEquals(DClass.IN, tc.getDClass());
079: assertEquals(100L, tc.getTTL());
080:
081: tc = new TestClass(n, Type.A, DClass.IN, 100L, sn,
082: "The Description");
083:
084: assertSame(n, tc.getName());
085: assertEquals(Type.A, tc.getType());
086: assertEquals(DClass.IN, tc.getDClass());
087: assertEquals(100L, tc.getTTL());
088: assertSame(sn, tc.getSingleName());
089: }
090:
091: public void test_rrFromWire() throws IOException {
092: byte[] raw = new byte[] { 2, 'm', 'y', 6, 's', 'i', 'n', 'g',
093: 'l', 'e', 4, 'n', 'a', 'm', 'e', 0 };
094: DNSInput in = new DNSInput(raw);
095:
096: TestClass tc = new TestClass();
097: tc.rrFromWire(in);
098:
099: Name exp = Name.fromString("my.single.name.");
100: assertEquals(exp, tc.getSingleName());
101: }
102:
103: public void test_rdataFromString() throws IOException {
104: Name exp = Name.fromString("my.single.name.");
105:
106: Tokenizer t = new Tokenizer("my.single.name.");
107: TestClass tc = new TestClass();
108: tc.rdataFromString(t, null);
109: assertEquals(exp, tc.getSingleName());
110:
111: t = new Tokenizer("my.relative.name");
112: tc = new TestClass();
113: try {
114: tc.rdataFromString(t, null);
115: fail("RelativeNameException not thrown");
116: } catch (RelativeNameException e) {
117: }
118: }
119:
120: public void test_rrToString() throws IOException,
121: TextParseException {
122: Name exp = Name.fromString("my.single.name.");
123:
124: Tokenizer t = new Tokenizer("my.single.name.");
125: TestClass tc = new TestClass();
126: tc.rdataFromString(t, null);
127: assertEquals(exp, tc.getSingleName());
128:
129: String out = tc.rrToString();
130: assertEquals(out, exp.toString());
131: }
132:
133: public void test_rrToWire() throws IOException, TextParseException {
134: Name n = Name.fromString("my.name.");
135: Name sn = Name.fromString("My.Single.Name.");
136:
137: // non-canonical (case sensitive)
138: TestClass tc = new TestClass(n, Type.A, DClass.IN, 100L, sn,
139: "The Description");
140: byte[] exp = new byte[] { 2, 'M', 'y', 6, 'S', 'i', 'n', 'g',
141: 'l', 'e', 4, 'N', 'a', 'm', 'e', 0 };
142:
143: DNSOutput dout = new DNSOutput();
144: tc.rrToWire(dout, null, false);
145:
146: byte[] out = dout.toByteArray();
147: assertEquals(exp, out);
148:
149: // canonical (lowercase)
150: tc = new TestClass(n, Type.A, DClass.IN, 100L, sn,
151: "The Description");
152: exp = new byte[] { 2, 'm', 'y', 6, 's', 'i', 'n', 'g', 'l',
153: 'e', 4, 'n', 'a', 'm', 'e', 0 };
154:
155: dout = new DNSOutput();
156: tc.rrToWire(dout, null, true);
157:
158: out = dout.toByteArray();
159: assertEquals(exp, out);
160: }
161: }
|