diff options
author | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2013-09-10 16:40:13 +0200 |
---|---|---|
committer | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2013-09-10 16:41:24 +0200 |
commit | 2bb0aa75fde62e2ce8c555b8394a604a3da82750 (patch) | |
tree | e43f59496d0245f4414564e5088fbe699bc8a0cd /plugins | |
parent | d4fb43b2fa63cfb1d48524d54c7391c582b95ca4 (diff) | |
download | sonarqube-2bb0aa75fde62e2ce8c555b8394a604a3da82750.tar.gz sonarqube-2bb0aa75fde62e2ce8c555b8394a604a3da82750.zip |
SONAR-3644 Add checks for invalid regexps
Diffstat (limited to 'plugins')
3 files changed, 35 insertions, 4 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/PatternDecoder.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/PatternDecoder.java index 70c18e1b9c4..03542237fb7 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/PatternDecoder.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/PatternDecoder.java @@ -66,6 +66,7 @@ public class PatternDecoder { checkDoubleRegexpLineConstraints(line, fields); pattern = new Pattern().setBeginBlockRegexp(fields[0]).setEndBlockRegexp(fields[1]); } else { + checkWholeFileRegexp(fields[0]); pattern = new Pattern().setAllFileRegexp(fields[0]); } @@ -84,7 +85,7 @@ public class PatternDecoder { } } - private void checkDoubleRegexpLineConstraints(String line, String[] fields) { + static void checkDoubleRegexpLineConstraints(String line, String[] fields) { if (!isRegexp(fields[0])) { throw new SonarException("Invalid format. The first field does not define a regular expression: " + line); } @@ -93,6 +94,12 @@ public class PatternDecoder { } } + static void checkWholeFileRegexp(String regexp) { + if (!isRegexp(regexp)) { + throw new SonarException("Invalid format. The field does not define a regular expression: " + regexp); + } + } + public static void decodeRangeOfLines(Pattern pattern, String field) { if (StringUtils.equals(field, "*")) { pattern.setCheckLines(false); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/PatternsInitializer.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/PatternsInitializer.java index 742943335c6..35bb21ad609 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/PatternsInitializer.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/PatternsInitializer.java @@ -104,6 +104,8 @@ public class PatternsInitializer implements BatchExtension { String propPrefix = Constants.PATTERNS_BLOCK_KEY + "." + id + "."; String beginBlockRegexp = settings.getString(propPrefix + Constants.BEGIN_BLOCK_REGEXP); String endBlockRegexp = settings.getString(propPrefix + Constants.END_BLOCK_REGEXP); + String[] fields = new String[] { beginBlockRegexp, endBlockRegexp }; + PatternDecoder.checkDoubleRegexpLineConstraints(StringUtils.join(fields, ","), fields); Pattern pattern = new Pattern().setBeginBlockRegexp(nullToEmpty(beginBlockRegexp)).setEndBlockRegexp(nullToEmpty(endBlockRegexp)); blockPatterns.add(pattern); } @@ -113,6 +115,7 @@ public class PatternsInitializer implements BatchExtension { for (String id : StringUtils.split(patternConf, ',')) { String propPrefix = Constants.PATTERNS_ALLFILE_KEY + "." + id + "."; String allFileRegexp = settings.getString(propPrefix + Constants.FILE_REGEXP); + PatternDecoder.checkWholeFileRegexp(allFileRegexp); Pattern pattern = new Pattern().setAllFileRegexp(nullToEmpty(allFileRegexp)); allFilePatterns.add(pattern); } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/pattern/PatternsInitializerTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/pattern/PatternsInitializerTest.java index fe4df90f3cc..c87f4696b8e 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/pattern/PatternsInitializerTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/pattern/PatternsInitializerTest.java @@ -20,15 +20,13 @@ package org.sonar.plugins.core.issue.ignore.pattern; -import org.sonar.api.utils.SonarException; - -import org.junit.Ignore; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import org.junit.Before; import org.junit.Test; import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.Settings; +import org.sonar.api.utils.SonarException; import org.sonar.plugins.core.issue.ignore.Constants; import org.sonar.plugins.core.issue.ignore.IgnoreIssuesConfiguration; @@ -142,6 +140,22 @@ public class PatternsInitializerTest { assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(0); } + @Test(expected = SonarException.class) + public void shouldLogInvalidStartBlockPattern() { + settings.setProperty(Constants.PATTERNS_BLOCK_KEY, "1"); + settings.setProperty(Constants.PATTERNS_BLOCK_KEY + ".1." + Constants.BEGIN_BLOCK_REGEXP, ""); + settings.setProperty(Constants.PATTERNS_BLOCK_KEY + ".1." + Constants.END_BLOCK_REGEXP, "// SONAR-ON"); + patternsInitializer.initPatterns(); + } + + @Test(expected = SonarException.class) + public void shouldLogInvalidEndBlockPattern() { + settings.setProperty(Constants.PATTERNS_BLOCK_KEY, "1"); + settings.setProperty(Constants.PATTERNS_BLOCK_KEY + ".1." + Constants.BEGIN_BLOCK_REGEXP, "// SONAR-OFF"); + settings.setProperty(Constants.PATTERNS_BLOCK_KEY + ".1." + Constants.END_BLOCK_REGEXP, ""); + patternsInitializer.initPatterns(); + } + @Test public void shouldReturnAllFilePattern() { settings.setProperty(Constants.PATTERNS_ALLFILE_KEY, "1,2"); @@ -157,6 +171,13 @@ public class PatternsInitializerTest { assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(2); } + @Test(expected = SonarException.class) + public void shouldLogInvalidAllFilePattern() { + settings.setProperty(Constants.PATTERNS_ALLFILE_KEY, "1"); + settings.setProperty(Constants.PATTERNS_ALLFILE_KEY + ".1." + Constants.FILE_REGEXP, ""); + patternsInitializer.initPatterns(); + } + @Test public void shouldConfigurePatternsForComponents() { String componentKey = "groupId:artifactId:org.foo.Bar"; |