commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bspeak...@apache.org
Subject svn commit: r585762 - in /commons/proper/validator/trunk/src: main/java/org/apache/commons/validator/routines/InetAddressValidator.java test/java/org/apache/commons/validator/routines/InetAddressValidatorTest.java
Date Thu, 18 Oct 2007 00:11:37 GMT
Author: bspeakmon
Date: Wed Oct 17 17:11:37 2007
New Revision: 585762

URL: http://svn.apache.org/viewvc?rev=585762&view=rev
Log:
- first crack at generic IP address validator

Added:
    commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/InetAddressValidator.java
    commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/InetAddressValidatorTest.java

Added: commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/InetAddressValidator.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/InetAddressValidator.java?rev=585762&view=auto
==============================================================================
--- commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/InetAddressValidator.java
(added)
+++ commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/InetAddressValidator.java
Wed Oct 17 17:11:37 2007
@@ -0,0 +1,104 @@
+/*
+ * 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.
+ */
+
+package org.apache.commons.validator.routines;
+
+import java.io.Serializable;
+
+/**
+ * <p><b>InetAddress</b> validation and conversion routines (<code>java.net.InetAddress</code>).</p>
+ *
+ * <p>This class provides methods to validate a candidate IP address.
+ *
+ * <p>
+ * This class is a Singleton; you can retrieve the instance via the {@link #getInstance()}
method.
+ * </p>
+ *
+ * @version $Revision$
+ * @since Validator 1.4
+ */
+public class InetAddressValidator implements Serializable {
+
+    private static final String IPV4_REGEX =
+            "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";
+
+    /**
+     * Singleton instance of this class.
+     */
+    private static final InetAddressValidator VALIDATOR = new InetAddressValidator();
+
+    /** IPv4 RegexValidator */
+    private RegexValidator ipv4Validator = new RegexValidator(IPV4_REGEX);
+
+    /**
+     * Returns the singleton instance of this validator.
+     * @return the singleton instance of this validator
+     */
+    public static InetAddressValidator getInstance() {
+        return VALIDATOR;
+    }
+
+    /**
+     * Checks if the specified string is a valid IP address.
+     * @param inetAddress the string to validate
+     * @return true if the string validates as an IP address
+     */
+    public boolean isValid(String inetAddress) {
+        return isValidInet4Address(inetAddress);
+    }
+
+    /**
+     * Validates an IPv4 address. Returns true if valid.
+     * @param inet4Address the IPv4 address to validate
+     * @return true if the argument contains a valid IPv4 address
+     */
+    public boolean isValidInet4Address(String inet4Address) {
+        // verify that address conforms to generic IPv4 format
+        String[] groups = ipv4Validator.match(inet4Address);
+
+        if (groups == null) return false;
+
+        // verify that address subgroups are legal
+        int addrAccumulator = 0;
+        for (int i = 0; i <= 3; i++) {
+            String ipSegment = groups[i];
+            if (ipSegment == null || ipSegment.length() <= 0) {
+                return false;
+            }
+
+            int iIpSegment = 0;
+
+            try {
+                iIpSegment = Integer.parseInt(ipSegment);
+            } catch(NumberFormatException e) {
+                return false;
+            }
+
+            addrAccumulator += iIpSegment;
+
+            if (iIpSegment > 255) {
+                return false;
+            }
+
+        }
+
+        // verify that at least one bit of the address was set
+        if (addrAccumulator == 0) return false;
+
+        return true;
+    }
+}

Added: commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/InetAddressValidatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/InetAddressValidatorTest.java?rev=585762&view=auto
==============================================================================
--- commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/InetAddressValidatorTest.java
(added)
+++ commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/InetAddressValidatorTest.java
Wed Oct 17 17:11:37 2007
@@ -0,0 +1,101 @@
+/*
+ * 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.
+ */
+
+package org.apache.commons.validator.routines;
+
+import junit.framework.TestCase;
+
+/**
+ * Test cases for InetAddressValidator.
+ *
+ * @version $Revision$
+ */
+public class InetAddressValidatorTest extends TestCase {
+
+    private InetAddressValidator validator;
+
+    /**
+     * Command-line test method.
+     * @param args
+     */
+    public static void main(String[] args) {
+        junit.textui.TestRunner.run(InetAddressValidatorTest.class);
+    }
+
+    /**
+     * Constructor.
+     * @param name
+     */
+    public InetAddressValidatorTest(String name) {
+        super(name);
+    }
+
+    protected void setUp() {
+        validator = new InetAddressValidator();
+    }
+
+    /**
+     * Test IPs that point to real, well-known hosts (without actually looking them up).
+     */
+    public void testInetAddressesFromTheWild() {
+        assertTrue("www.apache.org IP should be valid",       validator.isValid("140.211.11.130"));
+        assertTrue("www.l.google.com IP should be valid",     validator.isValid("72.14.253.103"));
+        assertTrue("fsf.org IP should be valid",              validator.isValid("199.232.41.5"));
+        assertTrue("appscs.ign.com IP should be valid",       validator.isValid("216.35.123.87"));
+    }
+
+    /**
+     * Test valid and invalid IPs from each address class.
+     */
+    public void testInetAddressesByClass() {
+        assertTrue("class A IP should be valid",              validator.isValid("24.25.231.12"));
+        assertFalse("illegal class A IP should be invalid",   validator.isValid("2.41.32.324"));
+
+        assertTrue("class B IP should be valid",              validator.isValid("135.14.44.12"));
+        assertFalse("illegal class B IP should be invalid",   validator.isValid("154.123.441.123"));
+
+        assertTrue("class C IP should be valid",              validator.isValid("213.25.224.32"));
+        assertFalse("illegal class C IP should be invalid",   validator.isValid("201.543.23.11"));
+
+        assertTrue("class D IP should be valid",              validator.isValid("229.35.159.6"));
+        assertFalse("illegal class D IP should be invalid",   validator.isValid("231.54.11.987"));
+
+        assertTrue("class E IP should be valid",              validator.isValid("248.85.24.92"));
+        assertFalse("illegal class E IP should be invalid",   validator.isValid("250.21.323.48"));
+    }
+
+    /**
+     * Test reserved IPs.
+     */
+    public void testReservedInetAddresses() {
+        assertTrue("localhost IP should be valid",            validator.isValid("127.0.0.1"));
+        assertTrue("broadcast IP should be valid",            validator.isValid("255.255.255.255"));
+
+        assertFalse("empty IP shouldn't be valid",            validator.isValid("0.0.0.0"));
+    }
+
+    /**
+     * Test obviously broken IPs.
+     */
+    public void testBrokenInetAddresses() {
+        assertFalse("IP with characters should be invalid",   validator.isValid("124.14.32.abc"));
+        assertFalse("IP with three groups should be invalid", validator.isValid("23.64.12"));
+        assertFalse("IP with five groups should be invalid",  validator.isValid("26.34.23.77.234"));
+    }
+}
+
+



Mime
View raw message