1 // -*- Java -*-
2 //
3 // Copyright (c) 2005, Matthew J. Rutherford <rutherfo@cs.colorado.edu>
4 // Copyright (c) 2005, University of Colorado at Boulder
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are
9 // met:
10 //
11 // * Redistributions of source code must retain the above copyright
12 //   notice, this list of conditions and the following disclaimer.
13 //
14 // * Redistributions in binary form must reproduce the above copyright
15 //   notice, this list of conditions and the following disclaimer in the
16 //   documentation and/or other materials provided with the distribution.
17 //
18 // * Neither the name of the University of Colorado at Boulder nor the
19 //   names of its contributors may be used to endorse or promote
20 //   products derived from this software without specific prior written
21 //   permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 //
35 package org.xbill.DNS;
36 
37 import	java.net.InetAddress;
38 import	java.net.UnknownHostException;
39 import	java.util.Arrays;
40 import	junit.framework.TestCase;
41 
42 public class AddressTest extends TestCase
43 {
assertEquals( byte[] exp, byte[] act )44     private void assertEquals( byte[] exp, byte[] act )
45     {
46 	assertTrue(Arrays.equals(exp, act));
47     }
48 
assertEquals( int[] exp, int[] act )49     private void assertEquals( int[] exp, int[] act )
50     {
51 	assertEquals( exp.length, act.length );
52 	for( int i=0; i<exp.length; ++i ){
53 	    assertEquals( "i=" + i, exp[i], act[i] );
54 	}
55     }
56 
test_toByteArray_invalid()57     public void test_toByteArray_invalid()
58     {
59 	try {
60 	    Address.toByteArray("doesn't matter", 3);
61 	    fail("IllegalArgumentException not thrown");
62 	}
63 	catch( IllegalArgumentException e ){
64 	}
65     }
66 
test_toByteArray_IPv4()67     public void test_toByteArray_IPv4()
68     {
69 	byte[] exp = new byte[] { (byte)198, (byte)121, (byte)10, (byte)234 };
70 	byte[] ret = Address.toByteArray("198.121.10.234", Address.IPv4);
71 	assertEquals(exp, ret);
72 
73 	exp = new byte[] { 0, 0, 0, 0 };
74 	ret = Address.toByteArray("0.0.0.0", Address.IPv4);
75 	assertEquals(exp, ret);
76 
77 	exp = new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF };
78 	ret = Address.toByteArray("255.255.255.255", Address.IPv4);
79     }
80 
test_toByteArray_IPv4_invalid()81     public void test_toByteArray_IPv4_invalid()
82     {
83 	assertNull(Address.toByteArray("A.B.C.D", Address.IPv4));
84 
85 	assertNull(Address.toByteArray("128...", Address.IPv4));
86 	assertNull(Address.toByteArray("128.121", Address.IPv4));
87 	assertNull(Address.toByteArray("128.111.8", Address.IPv4));
88 	assertNull(Address.toByteArray("128.198.10.", Address.IPv4));
89 
90 	assertNull(Address.toByteArray("128.121.90..10", Address.IPv4));
91 	assertNull(Address.toByteArray("128.121..90.10", Address.IPv4));
92 	assertNull(Address.toByteArray("128..121.90.10", Address.IPv4));
93 	assertNull(Address.toByteArray(".128.121.90.10", Address.IPv4));
94 
95 	assertNull(Address.toByteArray("128.121.90.256", Address.IPv4));
96 	assertNull(Address.toByteArray("128.121.256.10", Address.IPv4));
97 	assertNull(Address.toByteArray("128.256.90.10", Address.IPv4));
98 	assertNull(Address.toByteArray("256.121.90.10", Address.IPv4));
99 
100 	assertNull(Address.toByteArray("128.121.90.-1", Address.IPv4));
101 	assertNull(Address.toByteArray("128.121.-1.10", Address.IPv4));
102 	assertNull(Address.toByteArray("128.-1.90.10", Address.IPv4));
103 	assertNull(Address.toByteArray("-1.121.90.10", Address.IPv4));
104 
105 	assertNull(Address.toByteArray("120.121.90.10.10", Address.IPv4));
106 
107 	assertNull(Address.toByteArray("120.121.90.010", Address.IPv4));
108 	assertNull(Address.toByteArray("120.121.090.10", Address.IPv4));
109 	assertNull(Address.toByteArray("120.021.90.10", Address.IPv4));
110 	assertNull(Address.toByteArray("020.121.90.10", Address.IPv4));
111 
112 	assertNull(Address.toByteArray("1120.121.90.10", Address.IPv4));
113 	assertNull(Address.toByteArray("120.2121.90.10", Address.IPv4));
114 	assertNull(Address.toByteArray("120.121.4190.10", Address.IPv4));
115 	assertNull(Address.toByteArray("120.121.190.1000", Address.IPv4));
116 
117 	assertNull(Address.toByteArray("", Address.IPv4));
118     }
119 
test_toByteArray_IPv6()120     public void test_toByteArray_IPv6()
121     {
122 	byte[] exp = new byte[] { (byte)32, (byte)1, (byte)13, (byte)184,
123 				  (byte)133, (byte)163, (byte)8, (byte)211,
124 				  (byte)19, (byte)25, (byte)138, (byte)46,
125 				  (byte)3, (byte)112, (byte)115, (byte)52 };
126 	byte[] ret = Address.toByteArray("2001:0db8:85a3:08d3:1319:8a2e:0370:7334", Address.IPv6);
127 	assertEquals(exp, ret);
128 	ret = Address.toByteArray("2001:db8:85a3:8d3:1319:8a2e:370:7334", Address.IPv6);
129 	assertEquals(exp, ret);
130 	ret = Address.toByteArray("2001:DB8:85A3:8D3:1319:8A2E:370:7334", Address.IPv6);
131 	assertEquals(exp, ret);
132 
133 	exp = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0,
134 			   0, 0, 0, 0, 0, 0, 0, 0 };
135 	ret = Address.toByteArray("0:0:0:0:0:0:0:0", Address.IPv6);
136 	assertEquals(exp, ret);
137 
138 	exp = new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF,
139 			   (byte)0xFF, (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 	ret = Address.toByteArray("FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", Address.IPv6);
143 	assertEquals(exp, ret);
144 
145 	exp = new byte[] { (byte)32, (byte)1, (byte)13, (byte)184,
146 			   (byte)0, (byte)0, (byte)8, (byte)211,
147 			   (byte)19, (byte)25, (byte)138, (byte)46,
148 			   (byte)3, (byte)112, (byte)115, (byte)52 };
149 	ret = Address.toByteArray("2001:0db8:0000:08d3:1319:8a2e:0370:7334", Address.IPv6);
150 	assertEquals(exp, ret);
151 
152 	ret = Address.toByteArray("2001:0db8::08d3:1319:8a2e:0370:7334", Address.IPv6);
153 	assertEquals(exp, ret);
154 
155 	exp = new byte[] { (byte)0, (byte)0, (byte)0, (byte)0,
156 			   (byte)133, (byte)163, (byte)8, (byte)211,
157 			   (byte)19, (byte)25, (byte)138, (byte)46,
158 			   (byte)3, (byte)112, (byte)115, (byte)52 };
159 	ret = Address.toByteArray("0000:0000:85a3:08d3:1319:8a2e:0370:7334", Address.IPv6);
160 	assertEquals(exp, ret);
161 	ret = Address.toByteArray("::85a3:08d3:1319:8a2e:0370:7334", Address.IPv6);
162 	assertEquals(exp, ret);
163 
164 	exp = new byte[] { (byte)32, (byte)1, (byte)13, (byte)184,
165 			   (byte)133, (byte)163, (byte)8, (byte)211,
166 			   (byte)19, (byte)25, (byte)138, (byte)46,
167 			   (byte)0, (byte)0, (byte)0, (byte)0 };
168 	ret = Address.toByteArray("2001:0db8:85a3:08d3:1319:8a2e:0:0", Address.IPv6);
169 	assertEquals(exp, ret);
170 
171 	ret = Address.toByteArray("2001:0db8:85a3:08d3:1319:8a2e::", Address.IPv6);
172 	assertEquals(exp, ret);
173 
174 	exp = new byte[] { (byte)32, (byte)1, (byte)13, (byte)184,
175 			   (byte)0, (byte)0, (byte)0, (byte)0,
176 			   (byte)0, (byte)0, (byte)0, (byte)0,
177 			   (byte)3, (byte)112, (byte)115, (byte)52 };
178 	ret = Address.toByteArray("2001:0db8:0000:0000:0000:0000:0370:7334", Address.IPv6);
179 	assertEquals(exp, ret);
180 	ret = Address.toByteArray("2001:0db8:0:0:0:0:0370:7334", Address.IPv6);
181 	assertEquals(exp, ret);
182 	ret = Address.toByteArray("2001:0db8::0:0370:7334", Address.IPv6);
183 	assertEquals(exp, ret);
184 	ret = Address.toByteArray("2001:db8::370:7334", Address.IPv6);
185 	assertEquals(exp, ret);
186 
187 	exp = new byte[] { (byte)32, (byte)1, (byte)13, (byte)184,
188 			   (byte)133, (byte)163, (byte)8, (byte)211,
189 			   (byte)19, (byte)25, (byte)138, (byte)46,
190 			   (byte)0xC0, (byte)0xA8, (byte)0x59, (byte)0x09 };
191 	ret = Address.toByteArray("2001:0db8:85a3:08d3:1319:8a2e:192.168.89.9", Address.IPv6);
192 	assertEquals(exp, ret);
193 
194 	exp = new byte[] { (byte)0, (byte)0, (byte)0, (byte)0,
195 			   (byte)0, (byte)0, (byte)0, (byte)0,
196 			   (byte)0, (byte)0, (byte)0, (byte)0,
197 			   (byte)0xC0, (byte)0xA8, (byte)0x59, (byte)0x09 };
198 	ret = Address.toByteArray("::192.168.89.9", Address.IPv6);
199 	assertEquals(exp, ret);
200     }
201 
test_toByteArray_IPv6_invalid()202     public void test_toByteArray_IPv6_invalid()
203     {
204 	// not enough groups
205 	assertNull(Address.toByteArray("2001:0db8:85a3:08d3:1319:8a2e:0370", Address.IPv6));
206 	// too many groups
207 	assertNull(Address.toByteArray("2001:0db8:85a3:08d3:1319:8a2e:0370:193A:BCdE", Address.IPv6));
208 	// invalid letter
209 	assertNull(Address.toByteArray("2001:0gb8:85a3:08d3:1319:8a2e:0370:9819", Address.IPv6));
210 	assertNull(Address.toByteArray("lmno:0bb8:85a3:08d3:1319:8a2e:0370:9819", Address.IPv6));
211 	assertNull(Address.toByteArray("11ab:0ab8:85a3:08d3:1319:8a2e:0370:qrst", Address.IPv6));
212 	// three consecutive colons
213 	assertNull(Address.toByteArray("11ab:0ab8:85a3:08d3:::", Address.IPv6));
214 	// IPv4 in the middle
215 	assertNull(Address.toByteArray("2001:0ab8:192.168.0.1:1319:8a2e:0370:9819", Address.IPv6));
216 	// invalid IPv4
217 	assertNull(Address.toByteArray("2001:0ab8:1212:AbAb:8a2e:345.12.22.1", Address.IPv6));
218 	// group with too many digits
219 	assertNull(Address.toByteArray("2001:0ab8:85a3:128d3:1319:8a2e:0370:9819", Address.IPv6));
220 
221     }
222 
test_toArray()223     public void test_toArray()
224     {
225 	int[] exp = new int[] { 1, 2, 3, 4 };
226 	int[] ret = Address.toArray("1.2.3.4", Address.IPv4);
227 	assertEquals( exp, ret );
228 
229 	exp = new int[] { 0, 0, 0, 0 };
230 	ret = Address.toArray("0.0.0.0", Address.IPv4);
231 	assertEquals( exp, ret );
232 
233 	exp = new int[] { 255, 255, 255, 255 };
234 	ret = Address.toArray("255.255.255.255", Address.IPv4);
235 	assertEquals( exp, ret );
236     }
237 
test_toArray_invalid()238     public void test_toArray_invalid()
239     {
240 	assertNull(Address.toArray("128.121.1", Address.IPv4));
241 
242 	assertNull(Address.toArray(""));
243     }
244 
test_isDottedQuad()245     public void test_isDottedQuad()
246     {
247 	assertTrue(Address.isDottedQuad("1.2.3.4"));
248 	assertFalse(Address.isDottedQuad("256.2.3.4"));
249     }
250 
test_toDottedQuad()251     public void test_toDottedQuad()
252     {
253 	assertEquals("128.176.201.1",
254 		     Address.toDottedQuad(new byte[] { (byte)128, (byte)176, (byte)201, (byte)1 }));
255 
256 	assertEquals("200.1.255.128",
257 		     Address.toDottedQuad(new int[] { 200, 1, 255, 128 }));
258     }
259 
test_addressLength()260     public void test_addressLength()
261     {
262 	assertEquals(4, Address.addressLength(Address.IPv4));
263 	assertEquals(16, Address.addressLength(Address.IPv6));
264 
265 	try {
266 	    Address.addressLength(3);
267 	    fail("IllegalArgumentException not thrown");
268 	}
269 	catch( IllegalArgumentException e ){
270 	}
271     }
272 
test_getByName()273     public void test_getByName() throws UnknownHostException
274     {
275 	InetAddress out = Address.getByName("128.145.198.231");
276 	assertEquals("128.145.198.231", out.getHostAddress());
277 
278 	out = Address.getByName("a.root-servers.net");
279 	assertEquals("198.41.0.4", out.getHostAddress());
280     }
281 
test_getByName_invalid()282     public void test_getByName_invalid() throws UnknownHostException
283     {
284 	try {
285 	    Address.getByName("example.invalid");
286 	    fail("UnknownHostException not thrown");
287 	}
288 	catch( UnknownHostException e ){
289 	}
290 	try {
291 	    Address.getByName("");
292 	    fail("UnknownHostException not thrown");
293 	}
294 	catch( UnknownHostException e ){
295 	}
296     }
297 
test_getAllByName()298     public void test_getAllByName() throws UnknownHostException
299     {
300 	InetAddress[] out = Address.getAllByName("128.145.198.231");
301 	assertEquals(1, out.length);
302 	assertEquals("128.145.198.231", out[0].getHostAddress());
303 
304 	out = Address.getAllByName("a.root-servers.net");
305 	assertTrue(out.length == 2);
306 	assertEquals("198.41.0.4", out[0].getHostAddress());
307 	assertEquals("2001:503:ba3e:0:0:0:2:30", out[1].getHostAddress());
308 
309 	out = Address.getAllByName("cnn.com");
310 	assertTrue(out.length > 1);
311 	for( int i=0; i<out.length; ++i){
312 	    assertTrue(out[i].getHostName().endsWith("cnn.com"));
313 	}
314     }
315 
test_getAllByName_invalid()316     public void test_getAllByName_invalid() throws UnknownHostException
317     {
318 	try {
319 	    Address.getAllByName("example.invalid");
320 	    fail("UnknownHostException not thrown");
321 	}
322 	catch( UnknownHostException e ){
323 	}
324 	try {
325 	    Address.getAllByName("");
326 	    fail("UnknownHostException not thrown");
327 	}
328 	catch( UnknownHostException e ){
329 	}
330     }
331 
test_familyOf()332     public void test_familyOf() throws UnknownHostException
333     {
334 	assertEquals(Address.IPv4, Address.familyOf(InetAddress.getByName("192.168.0.1")));
335 	assertEquals(Address.IPv6, Address.familyOf(InetAddress.getByName("1:2:3:4:5:6:7:8")));
336 	try {
337 	    Address.familyOf(null);
338 	    fail("IllegalArgumentException not thrown");
339 	}
340 	catch( IllegalArgumentException e ){
341 	}
342     }
343 
test_getHostName()344     public void test_getHostName() throws UnknownHostException
345     {
346 	String out = Address.getHostName(InetAddress.getByName("198.41.0.4"));
347 	assertEquals("a.root-servers.net.", out);
348 
349 	try {
350 	    Address.getHostName(InetAddress.getByName("192.168.1.1"));
351 	    fail("UnknownHostException not thrown");
352 	}
353 	catch( UnknownHostException e ){
354 	}
355     }
356 }
357