View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.conn.util;
29  
30  import org.junit.Assert;
31  import org.junit.Test;
32  
33  /**
34   * Unit tests for InetAddressUtils.
35   */
36  public class TestInetAddressUtils {
37  
38      @Test
39      public void testValidIPv4Address() {
40          Assert.assertTrue(InetAddressUtils.isIPv4Address("127.0.0.1"));
41          Assert.assertTrue(InetAddressUtils.isIPv4Address("192.168.0.0"));
42          Assert.assertTrue(InetAddressUtils.isIPv4Address("255.255.255.255"));
43      }
44  
45      @Test
46      public void testInvalidIPv4Address() {
47          Assert.assertFalse(InetAddressUtils.isIPv4Address(" 127.0.0.1 "));  // Blanks not allowed
48          Assert.assertFalse(InetAddressUtils.isIPv4Address("g.ar.ba.ge"));
49          Assert.assertFalse(InetAddressUtils.isIPv4Address("192.168.0"));
50          Assert.assertFalse(InetAddressUtils.isIPv4Address("256.255.255.255"));
51      }
52  
53      @Test
54      public void testValidIPv6Address() {
55          Assert.assertTrue(InetAddressUtils.isIPv6StdAddress("2001:0db8:0000:0000:0000:0000:1428:57ab"));
56          Assert.assertTrue(InetAddressUtils.isIPv6StdAddress("2001:db8:0:0:0:0:1428:57ab"));
57          Assert.assertTrue(InetAddressUtils.isIPv6StdAddress("0:0:0:0:0:0:0:0"));
58          Assert.assertTrue(InetAddressUtils.isIPv6StdAddress("0:0:0:0:0:0:0:1"));
59          Assert.assertTrue(InetAddressUtils.isIPv6HexCompressedAddress("2001:0db8:0:0::1428:57ab"));
60          Assert.assertTrue(InetAddressUtils.isIPv6HexCompressedAddress("2001:0db8::1428:57ab"));
61          Assert.assertTrue(InetAddressUtils.isIPv6HexCompressedAddress("2001:db8::1428:57ab"));
62          Assert.assertTrue(InetAddressUtils.isIPv6HexCompressedAddress("::1"));
63          Assert.assertTrue(InetAddressUtils.isIPv6HexCompressedAddress("::")); // http://tools.ietf.org/html/rfc4291#section-2.2
64      }
65  
66      @Test
67      public void testInvalidIPv6Address() {
68          Assert.assertFalse(InetAddressUtils.isIPv6Address("2001:0db8:0000:garb:age0:0000:1428:57ab"));
69          Assert.assertFalse(InetAddressUtils.isIPv6Address("2001:0gb8:0000:0000:0000:0000:1428:57ab"));
70          Assert.assertFalse(InetAddressUtils.isIPv6StdAddress("0:0:0:0:0:0:0:0:0")); // Too many
71          Assert.assertFalse(InetAddressUtils.isIPv6StdAddress("0:0:0:0:0:0:0")); // Too few
72          Assert.assertFalse(InetAddressUtils.isIPv6HexCompressedAddress(":1"));
73          Assert.assertFalse(InetAddressUtils.isIPv6Address("2001:0db8::0000::57ab")); // Cannot have two contractions
74          Assert.assertFalse(InetAddressUtils.isIPv6HexCompressedAddress("1:2:3:4:5:6:7::9")); // too many fields before ::
75          Assert.assertFalse(InetAddressUtils.isIPv6HexCompressedAddress("1::3:4:5:6:7:8:9")); // too many fields after ::
76          Assert.assertFalse(InetAddressUtils.isIPv6HexCompressedAddress("::3:4:5:6:7:8:9")); // too many fields after ::
77          Assert.assertFalse(InetAddressUtils.isIPv6Address("")); // empty
78      }
79  
80      @Test
81      // Test HTTPCLIENT-1319
82      public void testInvalidIPv6AddressIncorrectGroupCount() {
83          Assert.assertFalse(InetAddressUtils.isIPv6HexCompressedAddress("1:2::4:5:6:7:8:9")); // too many fields in total
84          Assert.assertFalse(InetAddressUtils.isIPv6HexCompressedAddress("1:2:3:4:5:6::8:9")); // too many fields in total
85      }
86  
87      @Test
88      public void testValidIPv4MappedIPv6Address() {
89          Assert.assertTrue(InetAddressUtils.isIPv4MappedIPv64Address("::FFFF:1.2.3.4"));
90          Assert.assertTrue(InetAddressUtils.isIPv4MappedIPv64Address("::ffff:255.255.255.255"));
91      }
92  
93      @Test
94      public void testInValidIPv4MappedIPv6Address() {
95          Assert.assertFalse(InetAddressUtils.isIPv4MappedIPv64Address("2001:0db8:0000:0000:0000:0000:1428:57ab"));
96          Assert.assertFalse(InetAddressUtils.isIPv4MappedIPv64Address("::ffff:1:2:3:4"));
97      }
98  
99  }