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.util.Arrays;
039: import junit.framework.TestCase;
040:
041: public class U16NameBaseTest extends TestCase {
042: private void assertEquals(byte[] exp, byte[] act) {
043: assertTrue(java.util.Arrays.equals(exp, act));
044: }
045:
046: private static class TestClass extends U16NameBase {
047: public TestClass() {
048: }
049:
050: public TestClass(Name name, int type, int dclass, long ttl) {
051: super (name, type, dclass, ttl);
052: }
053:
054: public TestClass(Name name, int type, int dclass, long ttl,
055: int u16Field, String u16Description, Name nameField,
056: String nameDescription) {
057: super (name, type, dclass, ttl, u16Field, u16Description,
058: nameField, nameDescription);
059: }
060:
061: public int getU16Field() {
062: return super .getU16Field();
063: }
064:
065: public Name getNameField() {
066: return super .getNameField();
067: }
068:
069: public Record getObject() {
070: return null;
071: }
072: }
073:
074: public void test_ctor_0arg() {
075: TestClass tc = new TestClass();
076: assertNull(tc.getName());
077: assertEquals(0, tc.getType());
078: assertEquals(0, tc.getDClass());
079: assertEquals(0, tc.getTTL());
080: assertEquals(0, tc.getU16Field());
081: assertNull(tc.getNameField());
082: }
083:
084: public void test_ctor_4arg() throws TextParseException {
085: Name n = Name.fromString("My.Name.");
086:
087: TestClass tc = new TestClass(n, Type.MX, DClass.IN, 0xBCDA);
088:
089: assertSame(n, tc.getName());
090: assertEquals(Type.MX, tc.getType());
091: assertEquals(DClass.IN, tc.getDClass());
092: assertEquals(0xBCDA, tc.getTTL());
093: assertEquals(0, tc.getU16Field());
094: assertNull(tc.getNameField());
095: }
096:
097: public void test_ctor_8arg() throws TextParseException {
098: Name n = Name.fromString("My.Name.");
099: Name m = Name.fromString("My.Other.Name.");
100:
101: TestClass tc = new TestClass(n, Type.MX, DClass.IN, 0xB12FL,
102: 0x1F2B, "u16 description", m, "name description");
103:
104: assertSame(n, tc.getName());
105: assertEquals(Type.MX, tc.getType());
106: assertEquals(DClass.IN, tc.getDClass());
107: assertEquals(0xB12FL, tc.getTTL());
108: assertEquals(0x1F2B, tc.getU16Field());
109: assertEquals(m, tc.getNameField());
110:
111: // an invalid u16 value
112: try {
113: new TestClass(n, Type.MX, DClass.IN, 0xB12FL, 0x10000,
114: "u16 description", m, "name description");
115: fail("IllegalArgumentException not thrown");
116: } catch (IllegalArgumentException e) {
117: }
118:
119: // a relative name
120: Name rel = Name.fromString("My.relative.Name");
121: try {
122: new TestClass(n, Type.MX, DClass.IN, 0xB12FL, 0x1F2B,
123: "u16 description", rel, "name description");
124: fail("RelativeNameException not thrown");
125: } catch (RelativeNameException e) {
126: }
127:
128: }
129:
130: public void test_rrFromWire() throws IOException {
131: byte[] raw = new byte[] { (byte) 0xBC, (byte) 0x1F, 2, 'M',
132: 'y', 6, 's', 'i', 'N', 'g', 'l', 'E', 4, 'n', 'A', 'm',
133: 'E', 0 };
134: DNSInput in = new DNSInput(raw);
135:
136: TestClass tc = new TestClass();
137: tc.rrFromWire(in);
138:
139: Name exp = Name.fromString("My.single.name.");
140: assertEquals(0xBC1FL, tc.getU16Field());
141: assertEquals(exp, tc.getNameField());
142: }
143:
144: public void test_rdataFromString() throws IOException {
145: Name exp = Name.fromString("My.Single.Name.");
146:
147: Tokenizer t = new Tokenizer(0x19A2 + " My.Single.Name.");
148: TestClass tc = new TestClass();
149: tc.rdataFromString(t, null);
150:
151: assertEquals(0x19A2, tc.getU16Field());
152: assertEquals(exp, tc.getNameField());
153:
154: t = new Tokenizer("10 My.Relative.Name");
155: tc = new TestClass();
156: try {
157: tc.rdataFromString(t, null);
158: fail("RelativeNameException not thrown");
159: } catch (RelativeNameException e) {
160: }
161: }
162:
163: public void test_rrToString() throws IOException,
164: TextParseException {
165: Name n = Name.fromString("My.Name.");
166: Name m = Name.fromString("My.Other.Name.");
167:
168: TestClass tc = new TestClass(n, Type.MX, DClass.IN, 0xB12FL,
169: 0x1F2B, "u16 description", m, "name description");
170:
171: String out = tc.rrToString();
172: String exp = 0x1F2B + " My.Other.Name.";
173:
174: assertEquals(exp, out);
175: }
176:
177: public void test_rrToWire() throws IOException, TextParseException {
178: Name n = Name.fromString("My.Name.");
179: Name m = Name.fromString("M.O.n.");
180:
181: TestClass tc = new TestClass(n, Type.MX, DClass.IN, 0xB12FL,
182: 0x1F2B, "u16 description", m, "name description");
183:
184: // canonical
185: DNSOutput dout = new DNSOutput();
186: tc.rrToWire(dout, null, true);
187: byte[] out = dout.toByteArray();
188: byte[] exp = new byte[] { 0x1F, 0x2B, 1, 'm', 1, 'o', 1, 'n', 0 };
189: assertTrue(Arrays.equals(exp, out));
190:
191: // case sensitive
192: dout = new DNSOutput();
193: tc.rrToWire(dout, null, false);
194: out = dout.toByteArray();
195: exp = new byte[] { 0x1F, 0x2B, 1, 'M', 1, 'O', 1, 'n', 0 };
196: assertTrue(Arrays.equals(exp, out));
197: }
198: }
|