AvoidUsingHardCodedIP now checks for IPv6 addresses as well

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@5595 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch
2007-10-27 17:44:43 +00:00
parent 68b3ffb069
commit d7972f80d1
4 changed files with 90 additions and 3 deletions

View File

@ -1,6 +1,6 @@
????, 2007 - 4.1:
New rules:
Basic ruleset: AvoidUsingHardCodedIPRule,CheckResultSet
Basic ruleset: AvoidUsingHardCodedIP,CheckResultSet
Controversial ruleset: AvoidFinalLocalVariable,AvoidUsingShortType,AvoidUsingVolatile,AvoidUsingNativeCode,AvoidAccessibilityAlteration
Design ruleset: ClassWithOnlyPrivateConstructorsShouldBeFinal,EmptyMethodInAbstractClassShouldBeAbstract
Imports ruleset: TooManyStaticImports

View File

@ -61,4 +61,43 @@ public class Foo {
]]></code>
</test-code>
<test-code>
<description><![CDATA[
Not an IPv6 address string
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
String B1 = "2001:0db8:0000:0000:0000:0000:1428:57ab testing";
String B2 = "::1 testing";
String B3 = ":::1";
String B4 = "::1:";
String B5 = "0";
String B6 = "0000000000000";
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
IPv6 addresses
]]></description>
<expected-problems>11</expected-problems>
<code><![CDATA[
public class Foo {
String A1 = "2001:0db8:0000:0000:0000:0000:1428:57ab";
String A2 = "2001:0db8:0000:0000:0000::1428:57ab";
String A3 = "2001:0db8:0:0:0:0:1428:57ab";
String A4 = "2001:0db8:0:0::1428:57ab";
String A5 = "2001:0db8::1428:57ab";
String A6 = "2001:db8::1428:57ab";
String A7 = "::1";
String A8 = "::ffff:12.34.56.78";
String A9 = "::ffff:0c22:384e";
String A10 = "0:0:0:0:0:ffff:0c22:384e";
String A11 = "2001:0db8:0000:0000:0000:0000:12.34.56.78";
}
]]></code>
</test-code>
</test-data>

View File

@ -1014,8 +1014,8 @@ public class Test {
</rule>
<rule name="AvoidUsingHardCodedIP"
message="Do not hard code IP, even 127.0.0.1 !"
class="net.sourceforge.pmd.rules.GenericLiteralCheckerRule"
message="Do not hard code IPv4 or IPv6 addresses, even 127.0.0.1 !"
class="net.sourceforge.pmd.rules.basic.AvoidUsingHardCodedIP"
externalInfoUrl="http://pmd.sourceforge.net/rules/basic.html#AvoidUsingHardCodedIP">
<description>
<![CDATA[

View File

@ -0,0 +1,48 @@
package net.sourceforge.pmd.rules.basic;
import java.net.InetAddress;
import java.util.regex.Pattern;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.ast.ASTLiteral;
public class AvoidUsingHardCodedIP extends AbstractRule {
private static final String IPv4_REGEXP = "^\"[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\"$";
private static final String IPv6_REGEXP = "^\"[0-9a-fA-F:]+:[0-9a-fA-F]+\"$";
private static final String IPv4_MAPPED_IPv6_REGEXP = "^\"[0-9a-fA-F:]+:[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\"$";
private static final Pattern IPv4_PATTERM = Pattern.compile(IPv4_REGEXP);
private static final Pattern IPv6_PATTERM = Pattern.compile(IPv6_REGEXP);
private static final Pattern IPv4_MAPPED_IPv6_PATTERM = Pattern.compile(IPv4_MAPPED_IPv6_REGEXP);
/**
* This method checks if the Literal matches the pattern. If it does, a violation is logged.
*/
public Object visit(ASTLiteral node, Object data) {
String image = node.getImage();
if (image == null || image.length() < 3 || image.charAt(0) != '"' ||
image.charAt(image.length()-1) != '"') {
return data;
}
/* Tests before calls to matches() ensure that the literal is '"[0-9:].*"' */
char c = image.charAt(1);
if ((Character.isDigit(c) || c == ':') &&
(IPv4_PATTERM.matcher(image).matches() ||
IPv6_PATTERM.matcher(image).matches() ||
IPv4_MAPPED_IPv6_PATTERM.matcher(image).matches())) {
try {
// as patterns are not 100% accurate, test address
InetAddress.getByName(image.substring(1, image.length()-1));
// no error creating address object, pattern must be valid
addViolation(data, node);
} catch (Exception e) {
// ignored: invalid format
}
}
return data;
}
}