hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r583201 - in /jakarta/httpcomponents/httpclient/trunk/module-client/src: main/java/org/apache/http/conn/util/ test/java/org/apache/http/client/ test/java/org/apache/http/conn/ test/java/org/apache/http/conn/util/
Date Tue, 09 Oct 2007 15:48:13 GMT
Author: olegk
Date: Tue Oct  9 08:48:12 2007
New Revision: 583201

URL: http://svn.apache.org/viewvc?rev=583201&view=rev
Log:
Added utility class to test whether a string represents a valid IPv4 or IPv6 address

Added:
    jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/util/
    jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/util/InetAddressUtils.java
  (with props)
    jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/util/
    jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/util/TestAllUtil.java
  (with props)
    jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/util/TestInetAddressUtils.java
  (with props)
Modified:
    jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/TestAll.java
    jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/TestAllConn.java

Added: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/util/InetAddressUtils.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/util/InetAddressUtils.java?rev=583201&view=auto
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/util/InetAddressUtils.java
(added)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/util/InetAddressUtils.java
Tue Oct  9 08:48:12 2007
@@ -0,0 +1,66 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ * 
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.conn.util;
+
+import java.util.regex.Pattern;
+
+public class InetAddressUtils {
+
+    private static final Pattern IPV4_PATTERN = 
+        Pattern.compile(
+                "^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");
+
+    private static final Pattern IPV6_STD_PATTERN = 
+        Pattern.compile(
+                "^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$");
+
+    private static final Pattern IPV6_HEX_COMPRESSED_PATTERN = 
+        Pattern.compile(
+                "^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)$");
+
+    public static boolean isIPv4Address(final String input) {
+        return IPV4_PATTERN.matcher(input).matches();
+    }
+
+    public static boolean isIPv6StdAddress(final String input) {
+        return IPV6_STD_PATTERN.matcher(input).matches();
+    }
+    
+    public static boolean isIPv6HexCompressedAddress(final String input) {
+        return IPV6_HEX_COMPRESSED_PATTERN.matcher(input).matches();
+    }
+    
+    public static boolean isIPv6Address(final String input) {
+        return isIPv6StdAddress(input) || isIPv6HexCompressedAddress(input); 
+    }
+
+}

Propchange: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/util/InetAddressUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/util/InetAddressUtils.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/util/InetAddressUtils.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/TestAll.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/TestAll.java?rev=583201&r1=583200&r2=583201&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/TestAll.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/client/TestAll.java
Tue Oct  9 08:48:12 2007
@@ -36,7 +36,6 @@
 
 import org.apache.http.client.protocol.TestAllProtocol;
 import org.apache.http.conn.TestAllConn;
-import org.apache.http.conn.ssl.TestAllSSL;
 import org.apache.http.cookie.TestAllCookie;
 import org.apache.http.impl.client.TestAllHttpClientImpl;
 import org.apache.http.impl.conn.TestAllConnImpl;
@@ -55,7 +54,6 @@
         suite.addTest(TestAllHttpClientImpl.suite());
         suite.addTest(TestAllConn.suite());
         suite.addTest(TestAllConnImpl.suite());
-        suite.addTest(TestAllSSL.suite());        
         suite.addTest(TestAllProtocol.suite());        
         return suite;
     }

Modified: jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/TestAllConn.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/TestAllConn.java?rev=583201&r1=583200&r2=583201&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/TestAllConn.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/TestAllConn.java
Tue Oct  9 08:48:12 2007
@@ -30,6 +30,9 @@
 
 package org.apache.http.conn;
 
+import org.apache.http.conn.ssl.TestAllSSL;
+import org.apache.http.conn.util.TestAllUtil;
+
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
@@ -49,6 +52,8 @@
         suite.addTest(TestScheme.suite());
         suite.addTest(TestParams.suite());
         suite.addTest(TestExceptions.suite());
+        suite.addTest(TestAllSSL.suite());
+        suite.addTest(TestAllUtil.suite());
 
         return suite;
     }

Added: jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/util/TestAllUtil.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/util/TestAllUtil.java?rev=583201&view=auto
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/util/TestAllUtil.java
(added)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/util/TestAllUtil.java
Tue Oct  9 08:48:12 2007
@@ -0,0 +1,56 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.conn.util;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class TestAllUtil extends TestCase {
+
+    public TestAllUtil(String testName) {
+        super(testName);
+    }
+
+    public static Test suite() {
+        TestSuite suite = new TestSuite();
+
+        suite.addTest(TestInetAddressUtils.suite());
+
+        return suite;
+    }
+
+    public static void main(String args[]) {
+        String[] testCaseName = { TestAllUtil.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+}

Propchange: jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/util/TestAllUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/util/TestAllUtil.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/util/TestAllUtil.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/util/TestInetAddressUtils.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/util/TestInetAddressUtils.java?rev=583201&view=auto
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/util/TestInetAddressUtils.java
(added)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/util/TestInetAddressUtils.java
Tue Oct  9 08:48:12 2007
@@ -0,0 +1,82 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ * 
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.conn.util;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Unit tests for InetAddressUtils.
+ */
+public class TestInetAddressUtils extends TestCase {
+
+    public TestInetAddressUtils(String testName) {
+        super(testName);
+    }
+
+    public static void main(String args[]) {
+        String[] testCaseName = { TestInetAddressUtils.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestInetAddressUtils.class);
+    }
+    
+    public void testValidIPv4Address() {
+        assertTrue(InetAddressUtils.isIPv4Address("127.0.0.1"));
+        assertTrue(InetAddressUtils.isIPv4Address("192.168.0.0"));
+        assertTrue(InetAddressUtils.isIPv4Address("255.255.255.255"));
+    }
+
+    public void testInvalidIPv4Address() {
+        assertFalse(InetAddressUtils.isIPv4Address(" 127.0.0.1 "));  // Blanks not allowed
+        assertFalse(InetAddressUtils.isIPv4Address("g.ar.ba.ge"));
+        assertFalse(InetAddressUtils.isIPv4Address("192.168.0"));
+        assertFalse(InetAddressUtils.isIPv4Address("256.255.255.255"));
+    }
+
+    public void testValidIPv6Address() {
+        assertTrue(InetAddressUtils.isIPv6StdAddress("2001:0db8:0000:0000:0000:0000:1428:57ab"));
+        assertTrue(InetAddressUtils.isIPv6StdAddress("2001:db8:0:0:0:0:1428:57ab"));
+        assertTrue(InetAddressUtils.isIPv6HexCompressedAddress("2001:0db8:0:0::1428:57ab"));
+        assertTrue(InetAddressUtils.isIPv6HexCompressedAddress("2001:0db8::1428:57ab"));
+        assertTrue(InetAddressUtils.isIPv6HexCompressedAddress("2001:db8::1428:57ab"));
+    }
+
+    public void testInvalidIPv6Address() {
+        assertFalse(InetAddressUtils.isIPv6Address("2001:0db8:0000:garb:age0:0000:1428:57ab"));
+        assertFalse(InetAddressUtils.isIPv6Address("2001:0gb8:0000:0000:0000:0000:1428:57ab"));
+    }
+    
+}

Propchange: jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/util/TestInetAddressUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/util/TestInetAddressUtils.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/conn/util/TestInetAddressUtils.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message