]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3644 Add checks for invalid regexps
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Tue, 10 Sep 2013 14:40:13 +0000 (16:40 +0200)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Tue, 10 Sep 2013 14:41:24 +0000 (16:41 +0200)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/PatternDecoder.java
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/PatternsInitializer.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/pattern/PatternsInitializerTest.java

index 70c18e1b9c444e979d29b451e8aec0b54f5b8aad..03542237fb7d6e66d95d03217c5687d79515d1c4 100644 (file)
@@ -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);
index 742943335c6b83746243d65ccf2c4a5c0b910404..35bb21ad6095092bc951cb229a0b9225581a0381 100644 (file)
@@ -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);
     }
index fe4df90f3ccf0ef73f3a142f92a2317a5dc8c2e8..c87f4696b8e817a15bc2ec37d3c60b660f3c1575 100644 (file)
 
 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";