3
// Copyright (c) 2005, Matthew J. Rutherford <rutherfo@cs.colorado.edu>
4
// Copyright (c) 2005, University of Colorado at Boulder
5
// All rights reserved.
7
// Redistribution and use in source and binary forms, with or without
8
// modification, are permitted provided that the following conditions are
11
// * Redistributions of source code must retain the above copyright
12
// notice, this list of conditions and the following disclaimer.
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.
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
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.
35
package org.xbill.DNS;
37
import java.net.InetAddress;
38
import java.net.UnknownHostException;
39
import java.util.Arrays;
40
import junit.framework.TestCase;
42
public class AddressTest extends TestCase
44
private void assertEquals( byte[] exp, byte[] act )
46
assertTrue(Arrays.equals(exp, act));
49
private void assertEquals( int[] exp, int[] act )
51
assertEquals( exp.length, act.length );
52
for( int i=0; i<exp.length; ++i ){
53
assertEquals( "i=" + i, exp[i], act[i] );
57
public void test_toByteArray_invalid()
60
Address.toByteArray("doesn't matter", 3);
61
fail("IllegalArgumentException not thrown");
63
catch( IllegalArgumentException e ){
67
public void test_toByteArray_IPv4()
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);
73
exp = new byte[] { 0, 0, 0, 0 };
74
ret = Address.toByteArray("0.0.0.0", Address.IPv4);
75
assertEquals(exp, ret);
77
exp = new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF };
78
ret = Address.toByteArray("255.255.255.255", Address.IPv4);
81
public void test_toByteArray_IPv4_invalid()
83
assertNull(Address.toByteArray("A.B.C.D", Address.IPv4));
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));
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));
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));
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));
105
assertNull(Address.toByteArray("120.121.90.10.10", Address.IPv4));
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));
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));
117
assertNull(Address.toByteArray("", Address.IPv4));
120
public void test_toByteArray_IPv6()
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);
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);
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);
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);
152
ret = Address.toByteArray("2001:0db8::08d3:1319:8a2e:0370:7334", Address.IPv6);
153
assertEquals(exp, ret);
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);
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);
171
ret = Address.toByteArray("2001:0db8:85a3:08d3:1319:8a2e::", Address.IPv6);
172
assertEquals(exp, ret);
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);
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);
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);
202
public void test_toByteArray_IPv6_invalid()
205
assertNull(Address.toByteArray("2001:0db8:85a3:08d3:1319:8a2e:0370", Address.IPv6));
207
assertNull(Address.toByteArray("2001:0db8:85a3:08d3:1319:8a2e:0370:193A:BCdE", Address.IPv6));
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));
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));
223
public void test_toArray()
225
int[] exp = new int[] { 1, 2, 3, 4 };
226
int[] ret = Address.toArray("1.2.3.4", Address.IPv4);
227
assertEquals( exp, ret );
229
exp = new int[] { 0, 0, 0, 0 };
230
ret = Address.toArray("0.0.0.0", Address.IPv4);
231
assertEquals( exp, ret );
233
exp = new int[] { 255, 255, 255, 255 };
234
ret = Address.toArray("255.255.255.255", Address.IPv4);
235
assertEquals( exp, ret );
238
public void test_toArray_invalid()
240
assertNull(Address.toArray("128.121.1", Address.IPv4));
242
assertNull(Address.toArray(""));
245
public void test_isDottedQuad()
247
assertTrue(Address.isDottedQuad("1.2.3.4"));
248
assertFalse(Address.isDottedQuad("256.2.3.4"));
251
public void test_toDottedQuad()
253
assertEquals("128.176.201.1",
254
Address.toDottedQuad(new byte[] { (byte)128, (byte)176, (byte)201, (byte)1 }));
256
assertEquals("200.1.255.128",
257
Address.toDottedQuad(new int[] { 200, 1, 255, 128 }));
260
public void test_addressLength()
262
assertEquals(4, Address.addressLength(Address.IPv4));
263
assertEquals(16, Address.addressLength(Address.IPv6));
266
Address.addressLength(3);
267
fail("IllegalArgumentException not thrown");
269
catch( IllegalArgumentException e ){
273
public void test_getByName() throws UnknownHostException
275
InetAddress out = Address.getByName("128.145.198.231");
276
assertEquals("128.145.198.231", out.getHostAddress());
278
out = Address.getByName("serl.cs.colorado.edu");
279
assertEquals("serl.cs.colorado.edu", out.getCanonicalHostName());
280
assertEquals("128.138.207.163", out.getHostAddress());
283
public void test_getByName_invalid() throws UnknownHostException
286
Address.getByName("bogushost.com");
287
fail("UnknownHostException not thrown");
289
catch( UnknownHostException e ){
292
Address.getByName("");
293
fail("UnknownHostException not thrown");
295
catch( UnknownHostException e ){
299
public void test_getAllByName() throws UnknownHostException
301
InetAddress[] out = Address.getAllByName("128.145.198.231");
302
assertEquals(1, out.length);
303
assertEquals("128.145.198.231", out[0].getHostAddress());
305
out = Address.getAllByName("serl.cs.colorado.edu");
306
assertEquals(1, out.length);
307
assertEquals("serl.cs.colorado.edu", out[0].getCanonicalHostName());
308
assertEquals("128.138.207.163", out[0].getHostAddress());
310
out = Address.getAllByName("cnn.com");
311
assertTrue(out.length > 1);
312
for( int i=0; i<out.length; ++i){
313
assertTrue(out[i].getHostName().endsWith("cnn.com"));
317
public void test_getAllByName_invalid() throws UnknownHostException
320
Address.getAllByName("bogushost.com");
321
fail("UnknownHostException not thrown");
323
catch( UnknownHostException e ){
326
Address.getAllByName("");
327
fail("UnknownHostException not thrown");
329
catch( UnknownHostException e ){
333
public void test_familyOf() throws UnknownHostException
335
assertEquals(Address.IPv4, Address.familyOf(InetAddress.getByName("192.168.0.1")));
336
assertEquals(Address.IPv6, Address.familyOf(InetAddress.getByName("1:2:3:4:5:6:7:8")));
338
Address.familyOf(null);
339
fail("IllegalArgumentException not thrown");
341
catch( IllegalArgumentException e ){
345
public void test_getHostName() throws UnknownHostException
347
String out = Address.getHostName(InetAddress.getByName("128.138.207.163"));
348
assertEquals("serl.cs.colorado.edu.", out);
351
Address.getHostName(InetAddress.getByName("192.168.1.1"));
352
fail("UnknownHostException not thrown");
354
catch( UnknownHostException e ){