~ubuntu-branches/ubuntu/wily/dnsjava/wily-proposed

« back to all changes in this revision

Viewing changes to tests/org/xbill/DNS/AddressTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Thierry Carrez
  • Date: 2009-07-21 15:17:03 UTC
  • Revision ID: james.westby@ubuntu.com-20090721151703-6v0107p1s3h7gv1c
Tags: upstream-2.0.6
ImportĀ upstreamĀ versionĀ 2.0.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
{
 
44
    private void assertEquals( byte[] exp, byte[] act )
 
45
    {
 
46
        assertTrue(Arrays.equals(exp, act));
 
47
    }
 
48
 
 
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
 
 
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
 
 
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
 
 
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
 
 
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
 
 
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
 
 
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
 
 
238
    public void test_toArray_invalid()
 
239
    {
 
240
        assertNull(Address.toArray("128.121.1", Address.IPv4));
 
241
 
 
242
        assertNull(Address.toArray(""));
 
243
    }
 
244
 
 
245
    public void test_isDottedQuad()
 
246
    {
 
247
        assertTrue(Address.isDottedQuad("1.2.3.4"));
 
248
        assertFalse(Address.isDottedQuad("256.2.3.4"));
 
249
    }
 
250
 
 
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
 
 
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
 
 
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("serl.cs.colorado.edu");
 
279
        assertEquals("serl.cs.colorado.edu", out.getCanonicalHostName());
 
280
        assertEquals("128.138.207.163", out.getHostAddress());
 
281
    }
 
282
 
 
283
    public void test_getByName_invalid() throws UnknownHostException
 
284
    {
 
285
        try {
 
286
            Address.getByName("bogushost.com");
 
287
            fail("UnknownHostException not thrown");
 
288
        }
 
289
        catch( UnknownHostException e ){
 
290
        }
 
291
        try {
 
292
            Address.getByName("");
 
293
            fail("UnknownHostException not thrown");
 
294
        }
 
295
        catch( UnknownHostException e ){
 
296
        }
 
297
    }
 
298
 
 
299
    public void test_getAllByName() throws UnknownHostException
 
300
    {
 
301
        InetAddress[] out = Address.getAllByName("128.145.198.231");
 
302
        assertEquals(1, out.length);
 
303
        assertEquals("128.145.198.231", out[0].getHostAddress());
 
304
 
 
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());
 
309
 
 
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"));
 
314
        }
 
315
    }
 
316
 
 
317
    public void test_getAllByName_invalid() throws UnknownHostException
 
318
    {
 
319
        try {
 
320
            Address.getAllByName("bogushost.com");
 
321
            fail("UnknownHostException not thrown");
 
322
        }
 
323
        catch( UnknownHostException e ){
 
324
        }
 
325
        try {
 
326
            Address.getAllByName("");
 
327
            fail("UnknownHostException not thrown");
 
328
        }
 
329
        catch( UnknownHostException e ){
 
330
        }
 
331
    }
 
332
 
 
333
    public void test_familyOf() throws UnknownHostException
 
334
    {
 
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")));
 
337
        try {
 
338
            Address.familyOf(null);
 
339
            fail("IllegalArgumentException not thrown");
 
340
        }
 
341
        catch( IllegalArgumentException e ){
 
342
        }
 
343
    }
 
344
 
 
345
    public void test_getHostName() throws UnknownHostException
 
346
    {
 
347
        String out = Address.getHostName(InetAddress.getByName("128.138.207.163"));
 
348
        assertEquals("serl.cs.colorado.edu.", out);
 
349
 
 
350
        try {
 
351
            Address.getHostName(InetAddress.getByName("192.168.1.1"));
 
352
            fail("UnknownHostException not thrown");
 
353
        }
 
354
        catch( UnknownHostException e ){
 
355
        }
 
356
    }
 
357
}