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.net.InetAddress;
038: import java.net.UnknownHostException;
039: import java.util.Arrays;
040: import junit.framework.TestCase;
041:
042: public class AddressTest extends TestCase {
043: private void assertEquals(byte[] exp, byte[] act) {
044: assertTrue(Arrays.equals(exp, act));
045: }
046:
047: private void assertEquals(int[] exp, int[] act) {
048: assertEquals(exp.length, act.length);
049: for (int i = 0; i < exp.length; ++i) {
050: assertEquals("i=" + i, exp[i], act[i]);
051: }
052: }
053:
054: public void test_toByteArray_invalid() {
055: try {
056: Address.toByteArray("doesn't matter", 3);
057: fail("IllegalArgumentException not thrown");
058: } catch (IllegalArgumentException e) {
059: }
060: }
061:
062: public void test_toByteArray_IPv4() {
063: byte[] exp = new byte[] { (byte) 198, (byte) 121, (byte) 10,
064: (byte) 234 };
065: byte[] ret = Address
066: .toByteArray("198.121.10.234", Address.IPv4);
067: assertEquals(exp, ret);
068:
069: exp = new byte[] { 0, 0, 0, 0 };
070: ret = Address.toByteArray("0.0.0.0", Address.IPv4);
071: assertEquals(exp, ret);
072:
073: exp = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
074: (byte) 0xFF };
075: ret = Address.toByteArray("255.255.255.255", Address.IPv4);
076: }
077:
078: public void test_toByteArray_IPv4_invalid() {
079: assertNull(Address.toByteArray("A.B.C.D", Address.IPv4));
080:
081: assertNull(Address.toByteArray("128...", Address.IPv4));
082: assertNull(Address.toByteArray("128.121", Address.IPv4));
083: assertNull(Address.toByteArray("128.111.8", Address.IPv4));
084: assertNull(Address.toByteArray("128.198.10.", Address.IPv4));
085:
086: assertNull(Address.toByteArray("128.121.90..10", Address.IPv4));
087: assertNull(Address.toByteArray("128.121..90.10", Address.IPv4));
088: assertNull(Address.toByteArray("128..121.90.10", Address.IPv4));
089: assertNull(Address.toByteArray(".128.121.90.10", Address.IPv4));
090:
091: assertNull(Address.toByteArray("128.121.90.256", Address.IPv4));
092: assertNull(Address.toByteArray("128.121.256.10", Address.IPv4));
093: assertNull(Address.toByteArray("128.256.90.10", Address.IPv4));
094: assertNull(Address.toByteArray("256.121.90.10", Address.IPv4));
095:
096: assertNull(Address.toByteArray("128.121.90.-1", Address.IPv4));
097: assertNull(Address.toByteArray("128.121.-1.10", Address.IPv4));
098: assertNull(Address.toByteArray("128.-1.90.10", Address.IPv4));
099: assertNull(Address.toByteArray("-1.121.90.10", Address.IPv4));
100:
101: assertNull(Address
102: .toByteArray("120.121.90.10.10", Address.IPv4));
103:
104: assertNull(Address.toByteArray("120.121.90.010", Address.IPv4));
105: assertNull(Address.toByteArray("120.121.090.10", Address.IPv4));
106: assertNull(Address.toByteArray("120.021.90.10", Address.IPv4));
107: assertNull(Address.toByteArray("020.121.90.10", Address.IPv4));
108:
109: assertNull(Address.toByteArray("1120.121.90.10", Address.IPv4));
110: assertNull(Address.toByteArray("120.2121.90.10", Address.IPv4));
111: assertNull(Address.toByteArray("120.121.4190.10", Address.IPv4));
112: assertNull(Address
113: .toByteArray("120.121.190.1000", Address.IPv4));
114:
115: assertNull(Address.toByteArray("", Address.IPv4));
116: }
117:
118: public void test_toByteArray_IPv6() {
119: byte[] exp = new byte[] { (byte) 32, (byte) 1, (byte) 13,
120: (byte) 184, (byte) 133, (byte) 163, (byte) 8,
121: (byte) 211, (byte) 19, (byte) 25, (byte) 138,
122: (byte) 46, (byte) 3, (byte) 112, (byte) 115, (byte) 52 };
123: byte[] ret = Address
124: .toByteArray("2001:0db8:85a3:08d3:1319:8a2e:0370:7334",
125: Address.IPv6);
126: assertEquals(exp, ret);
127: ret = Address.toByteArray(
128: "2001:db8:85a3:8d3:1319:8a2e:370:7334", Address.IPv6);
129: assertEquals(exp, ret);
130: ret = Address.toByteArray(
131: "2001:DB8:85A3:8D3:1319:8A2E:370:7334", Address.IPv6);
132: assertEquals(exp, ret);
133:
134: exp = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
135: 0 };
136: ret = Address.toByteArray("0:0:0:0:0:0:0:0", Address.IPv6);
137: assertEquals(exp, ret);
138:
139: exp = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
140: (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
141: (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
142: (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
143: (byte) 0xFF };
144: ret = Address
145: .toByteArray("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
146: Address.IPv6);
147: assertEquals(exp, ret);
148:
149: exp = new byte[] { (byte) 32, (byte) 1, (byte) 13, (byte) 184,
150: (byte) 0, (byte) 0, (byte) 8, (byte) 211, (byte) 19,
151: (byte) 25, (byte) 138, (byte) 46, (byte) 3, (byte) 112,
152: (byte) 115, (byte) 52 };
153: ret = Address
154: .toByteArray("2001:0db8:0000:08d3:1319:8a2e:0370:7334",
155: Address.IPv6);
156: assertEquals(exp, ret);
157:
158: ret = Address.toByteArray(
159: "2001:0db8::08d3:1319:8a2e:0370:7334", Address.IPv6);
160: assertEquals(exp, ret);
161:
162: exp = new byte[] { (byte) 0, (byte) 0, (byte) 0, (byte) 0,
163: (byte) 133, (byte) 163, (byte) 8, (byte) 211,
164: (byte) 19, (byte) 25, (byte) 138, (byte) 46, (byte) 3,
165: (byte) 112, (byte) 115, (byte) 52 };
166: ret = Address
167: .toByteArray("0000:0000:85a3:08d3:1319:8a2e:0370:7334",
168: Address.IPv6);
169: assertEquals(exp, ret);
170: ret = Address.toByteArray("::85a3:08d3:1319:8a2e:0370:7334",
171: Address.IPv6);
172: assertEquals(exp, ret);
173:
174: exp = new byte[] { (byte) 32, (byte) 1, (byte) 13, (byte) 184,
175: (byte) 133, (byte) 163, (byte) 8, (byte) 211,
176: (byte) 19, (byte) 25, (byte) 138, (byte) 46, (byte) 0,
177: (byte) 0, (byte) 0, (byte) 0 };
178: ret = Address.toByteArray("2001:0db8:85a3:08d3:1319:8a2e:0:0",
179: Address.IPv6);
180: assertEquals(exp, ret);
181:
182: ret = Address.toByteArray("2001:0db8:85a3:08d3:1319:8a2e::",
183: Address.IPv6);
184: assertEquals(exp, ret);
185:
186: exp = new byte[] { (byte) 32, (byte) 1, (byte) 13, (byte) 184,
187: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
188: (byte) 0, (byte) 0, (byte) 0, (byte) 3, (byte) 112,
189: (byte) 115, (byte) 52 };
190: ret = Address
191: .toByteArray("2001:0db8:0000:0000:0000:0000:0370:7334",
192: Address.IPv6);
193: assertEquals(exp, ret);
194: ret = Address.toByteArray("2001:0db8:0:0:0:0:0370:7334",
195: Address.IPv6);
196: assertEquals(exp, ret);
197: ret = Address.toByteArray("2001:0db8::0:0370:7334",
198: Address.IPv6);
199: assertEquals(exp, ret);
200: ret = Address.toByteArray("2001:db8::370:7334", Address.IPv6);
201: assertEquals(exp, ret);
202:
203: exp = new byte[] { (byte) 32, (byte) 1, (byte) 13, (byte) 184,
204: (byte) 133, (byte) 163, (byte) 8, (byte) 211,
205: (byte) 19, (byte) 25, (byte) 138, (byte) 46,
206: (byte) 0xC0, (byte) 0xA8, (byte) 0x59, (byte) 0x09 };
207: ret = Address.toByteArray(
208: "2001:0db8:85a3:08d3:1319:8a2e:192.168.89.9",
209: Address.IPv6);
210: assertEquals(exp, ret);
211:
212: exp = new byte[] { (byte) 0, (byte) 0, (byte) 0, (byte) 0,
213: (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
214: (byte) 0, (byte) 0, (byte) 0, (byte) 0xC0, (byte) 0xA8,
215: (byte) 0x59, (byte) 0x09 };
216: ret = Address.toByteArray("::192.168.89.9", Address.IPv6);
217: assertEquals(exp, ret);
218: }
219:
220: public void test_toByteArray_IPv6_invalid() {
221: // not enough groups
222: assertNull(Address.toByteArray(
223: "2001:0db8:85a3:08d3:1319:8a2e:0370", Address.IPv6));
224: // too many groups
225: assertNull(Address.toByteArray(
226: "2001:0db8:85a3:08d3:1319:8a2e:0370:193A:BCdE",
227: Address.IPv6));
228: // invalid letter
229: assertNull(Address
230: .toByteArray("2001:0gb8:85a3:08d3:1319:8a2e:0370:9819",
231: Address.IPv6));
232: assertNull(Address
233: .toByteArray("lmno:0bb8:85a3:08d3:1319:8a2e:0370:9819",
234: Address.IPv6));
235: assertNull(Address
236: .toByteArray("11ab:0ab8:85a3:08d3:1319:8a2e:0370:qrst",
237: Address.IPv6));
238: // three consecutive colons
239: assertNull(Address.toByteArray("11ab:0ab8:85a3:08d3:::",
240: Address.IPv6));
241: // IPv4 in the middle
242: assertNull(Address.toByteArray(
243: "2001:0ab8:192.168.0.1:1319:8a2e:0370:9819",
244: Address.IPv6));
245: // invalid IPv4
246: assertNull(Address.toByteArray(
247: "2001:0ab8:1212:AbAb:8a2e:345.12.22.1", Address.IPv6));
248: // group with too many digits
249: assertNull(Address.toByteArray(
250: "2001:0ab8:85a3:128d3:1319:8a2e:0370:9819",
251: Address.IPv6));
252:
253: }
254:
255: public void test_toArray() {
256: int[] exp = new int[] { 1, 2, 3, 4 };
257: int[] ret = Address.toArray("1.2.3.4", Address.IPv4);
258: assertEquals(exp, ret);
259:
260: exp = new int[] { 0, 0, 0, 0 };
261: ret = Address.toArray("0.0.0.0", Address.IPv4);
262: assertEquals(exp, ret);
263:
264: exp = new int[] { 255, 255, 255, 255 };
265: ret = Address.toArray("255.255.255.255", Address.IPv4);
266: assertEquals(exp, ret);
267: }
268:
269: public void test_toArray_invalid() {
270: assertNull(Address.toArray("128.121.1", Address.IPv4));
271:
272: assertNull(Address.toArray(""));
273: }
274:
275: public void test_isDottedQuad() {
276: assertTrue(Address.isDottedQuad("1.2.3.4"));
277: assertFalse(Address.isDottedQuad("256.2.3.4"));
278: }
279:
280: public void test_toDottedQuad() {
281: assertEquals("128.176.201.1", Address.toDottedQuad(new byte[] {
282: (byte) 128, (byte) 176, (byte) 201, (byte) 1 }));
283:
284: assertEquals("200.1.255.128", Address.toDottedQuad(new int[] {
285: 200, 1, 255, 128 }));
286: }
287:
288: public void test_addressLength() {
289: assertEquals(4, Address.addressLength(Address.IPv4));
290: assertEquals(16, Address.addressLength(Address.IPv6));
291:
292: try {
293: Address.addressLength(3);
294: fail("IllegalArgumentException not thrown");
295: } catch (IllegalArgumentException e) {
296: }
297: }
298:
299: public void test_getByName() throws UnknownHostException {
300: InetAddress out = Address.getByName("128.145.198.231");
301: assertEquals("128.145.198.231", out.getHostAddress());
302:
303: out = Address.getByName("serl.cs.colorado.edu");
304: assertEquals("serl.cs.colorado.edu", out.getCanonicalHostName());
305: assertEquals("128.138.207.163", out.getHostAddress());
306: }
307:
308: public void test_getByName_invalid() throws UnknownHostException {
309: try {
310: Address.getByName("bogushost.com");
311: fail("UnknownHostException not thrown");
312: } catch (UnknownHostException e) {
313: }
314: try {
315: Address.getByName("");
316: fail("UnknownHostException not thrown");
317: } catch (UnknownHostException e) {
318: }
319: }
320:
321: public void test_getAllByName() throws UnknownHostException {
322: InetAddress[] out = Address.getAllByName("128.145.198.231");
323: assertEquals(1, out.length);
324: assertEquals("128.145.198.231", out[0].getHostAddress());
325:
326: out = Address.getAllByName("serl.cs.colorado.edu");
327: assertEquals(1, out.length);
328: assertEquals("serl.cs.colorado.edu", out[0]
329: .getCanonicalHostName());
330: assertEquals("128.138.207.163", out[0].getHostAddress());
331:
332: out = Address.getAllByName("cnn.com");
333: assertTrue(out.length > 1);
334: for (int i = 0; i < out.length; ++i) {
335: assertTrue(out[i].getHostName().endsWith("cnn.com"));
336: }
337: }
338:
339: public void test_getAllByName_invalid() throws UnknownHostException {
340: try {
341: Address.getAllByName("bogushost.com");
342: fail("UnknownHostException not thrown");
343: } catch (UnknownHostException e) {
344: }
345: try {
346: Address.getAllByName("");
347: fail("UnknownHostException not thrown");
348: } catch (UnknownHostException e) {
349: }
350: }
351:
352: public void test_familyOf() throws UnknownHostException {
353: assertEquals(Address.IPv4, Address.familyOf(InetAddress
354: .getByName("192.168.0.1")));
355: assertEquals(Address.IPv6, Address.familyOf(InetAddress
356: .getByName("1:2:3:4:5:6:7:8")));
357: try {
358: Address.familyOf(null);
359: fail("IllegalArgumentException not thrown");
360: } catch (IllegalArgumentException e) {
361: }
362: }
363:
364: public void test_getHostName() throws UnknownHostException {
365: String out = Address.getHostName(InetAddress
366: .getByName("128.138.207.163"));
367: assertEquals("serl.cs.colorado.edu.", out);
368:
369: try {
370: Address.getHostName(InetAddress.getByName("192.168.1.1"));
371: fail("UnknownHostException not thrown");
372: } catch (UnknownHostException e) {
373: }
374: }
375: }
|