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 junit.framework.TestCase;
042:
043: public class A6RecordTest extends TestCase {
044: Name m_an, m_an2, m_rn;
045: InetAddress m_addr;
046: String m_addr_string, m_addr_string_canonical;
047: byte[] m_addr_bytes;
048: int m_prefix_bits;
049: long m_ttl;
050:
051: protected void setUp() throws TextParseException,
052: UnknownHostException {
053: m_an = Name.fromString("My.Absolute.Name.");
054: m_an2 = Name.fromString("My.Second.Absolute.Name.");
055: m_rn = Name.fromString("My.Relative.Name");
056: m_addr_string = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334";
057: m_addr_string_canonical = "2001:db8:85a3:8d3:1319:8a2e:370:7334";
058: m_addr = InetAddress.getByName(m_addr_string);
059: m_addr_bytes = m_addr.getAddress();
060: m_ttl = 0x13579;
061: m_prefix_bits = 9;
062: }
063:
064: public void test_ctor_0arg() {
065: A6Record ar = new A6Record();
066: assertNull(ar.getName());
067: assertEquals(0, ar.getType());
068: assertEquals(0, ar.getDClass());
069: assertEquals(0, ar.getTTL());
070: }
071:
072: public void test_getObject() {
073: A6Record ar = new A6Record();
074: Record r = ar.getObject();
075: assertTrue(r instanceof A6Record);
076: }
077:
078: public void test_ctor_6arg() {
079: A6Record ar = new A6Record(m_an, DClass.IN, m_ttl,
080: m_prefix_bits, m_addr, null);
081: assertEquals(m_an, ar.getName());
082: assertEquals(Type.A6, ar.getType());
083: assertEquals(DClass.IN, ar.getDClass());
084: assertEquals(m_ttl, ar.getTTL());
085: assertEquals(m_prefix_bits, ar.getPrefixBits());
086: assertEquals(m_addr, ar.getSuffix());
087: assertNull(ar.getPrefix());
088:
089: // with the prefix name
090: ar = new A6Record(m_an, DClass.IN, m_ttl, m_prefix_bits,
091: m_addr, m_an2);
092: assertEquals(m_an, ar.getName());
093: assertEquals(Type.A6, ar.getType());
094: assertEquals(DClass.IN, ar.getDClass());
095: assertEquals(m_ttl, ar.getTTL());
096: assertEquals(m_prefix_bits, ar.getPrefixBits());
097: assertEquals(m_addr, ar.getSuffix());
098: assertEquals(m_an2, ar.getPrefix());
099:
100: // a relative name
101: try {
102: new A6Record(m_rn, DClass.IN, m_ttl, m_prefix_bits, m_addr,
103: null);
104: fail("RelativeNameException not thrown");
105: } catch (RelativeNameException e) {
106: }
107:
108: // a relative prefix name
109: try {
110: new A6Record(m_an, DClass.IN, m_ttl, m_prefix_bits, m_addr,
111: m_rn);
112: fail("RelativeNameException not thrown");
113: } catch (RelativeNameException e) {
114: }
115:
116: // invalid prefix bits
117: try {
118: new A6Record(m_rn, DClass.IN, m_ttl, 0x100, m_addr, null);
119: fail("IllegalArgumentException not thrown");
120: } catch (RelativeNameException e) {
121: }
122:
123: // an IPv4 address
124: try {
125: new A6Record(m_an, DClass.IN, m_ttl, m_prefix_bits,
126: InetAddress.getByName("192.168.0.1"), null);
127: fail("IllegalArgumentException not thrown");
128: } catch (IllegalArgumentException e) {
129: } catch (UnknownHostException e) {
130: fail(e.getMessage());
131: }
132: }
133:
134: public void test_rrFromWire() throws CloneNotSupportedException,
135: IOException, UnknownHostException {
136: // record with no prefix
137: DNSOutput dout = new DNSOutput();
138: dout.writeU8(0);
139: dout.writeByteArray(m_addr_bytes);
140:
141: DNSInput din = new DNSInput(dout.toByteArray());
142: A6Record ar = new A6Record();
143: ar.rrFromWire(din);
144: assertEquals(0, ar.getPrefixBits());
145: assertEquals(m_addr, ar.getSuffix());
146: assertNull(ar.getPrefix());
147:
148: // record with 9 bit prefix (should result in 15 bytes of the address)
149: dout = new DNSOutput();
150: dout.writeU8(9);
151: dout.writeByteArray(m_addr_bytes, 1, 15);
152: dout.writeByteArray(m_an2.toWire());
153:
154: din = new DNSInput(dout.toByteArray());
155: ar = new A6Record();
156: ar.rrFromWire(din);
157: assertEquals(9, ar.getPrefixBits());
158:
159: byte[] addr_bytes = (byte[]) m_addr_bytes.clone();
160: addr_bytes[0] = 0;
161: InetAddress exp = InetAddress.getByAddress(addr_bytes);
162: assertEquals(exp, ar.getSuffix());
163: assertEquals(m_an2, ar.getPrefix());
164: }
165:
166: public void test_rdataFromString()
167: throws CloneNotSupportedException, IOException,
168: UnknownHostException {
169: // record with no prefix
170: Tokenizer t = new Tokenizer("0 " + m_addr_string);
171: A6Record ar = new A6Record();
172: ar.rdataFromString(t, null);
173: assertEquals(0, ar.getPrefixBits());
174: assertEquals(m_addr, ar.getSuffix());
175: assertNull(ar.getPrefix());
176:
177: // record with 9 bit prefix. In contrast to the rrFromWire method,
178: // rdataFromString expects the entire 128 bits to be represented
179: // in the string
180: t = new Tokenizer("9 " + m_addr_string + " " + m_an2);
181: ar = new A6Record();
182: ar.rdataFromString(t, null);
183: assertEquals(9, ar.getPrefixBits());
184: assertEquals(m_addr, ar.getSuffix());
185: assertEquals(m_an2, ar.getPrefix());
186:
187: // record with invalid prefixBits
188: t = new Tokenizer("129");
189: ar = new A6Record();
190: try {
191: ar.rdataFromString(t, null);
192: fail("TextParseException not thrown");
193: } catch (TextParseException e) {
194: }
195:
196: // record with invalid ipv6 address
197: t = new Tokenizer("0 " + m_addr_string.substring(4));
198: ar = new A6Record();
199: try {
200: ar.rdataFromString(t, null);
201: fail("TextParseException not thrown");
202: } catch (TextParseException e) {
203: }
204: }
205:
206: public void test_rrToString() {
207: A6Record ar = new A6Record(m_an, DClass.IN, m_ttl,
208: m_prefix_bits, m_addr, m_an2);
209: String exp = "" + m_prefix_bits + " " + m_addr_string_canonical
210: + " " + m_an2;
211: String out = ar.rrToString();
212: assertEquals(exp, out);
213: }
214:
215: public void test_rrToWire() {
216: // canonical form
217: A6Record ar = new A6Record(m_an, DClass.IN, m_ttl,
218: m_prefix_bits, m_addr, m_an2);
219: DNSOutput dout = new DNSOutput();
220: dout.writeU8(m_prefix_bits);
221: dout.writeByteArray(m_addr_bytes, 1, 15);
222: dout.writeByteArray(m_an2.toWireCanonical());
223:
224: byte[] exp = dout.toByteArray();
225:
226: dout = new DNSOutput();
227: ar.rrToWire(dout, null, true);
228:
229: assertTrue(Arrays.equals(exp, dout.toByteArray()));
230:
231: // case sensitiveform
232: dout = new DNSOutput();
233: dout.writeU8(m_prefix_bits);
234: dout.writeByteArray(m_addr_bytes, 1, 15);
235: dout.writeByteArray(m_an2.toWire());
236:
237: exp = dout.toByteArray();
238:
239: dout = new DNSOutput();
240: ar.rrToWire(dout, null, false);
241: assertTrue(Arrays.equals(exp, dout.toByteArray()));
242: }
243: }
|