diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-08-21 16:24:51 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-08-22 13:45:12 +0200 |
commit | d8fb698f07141c1e48cab3404b4641dc6d8532f7 (patch) | |
tree | a1bae579b499b3ef9e6f1129dcff81918d6f0d1a /server/sonar-process/src/test/java | |
parent | c20afce399c3e73ee946d4cb7a0a4578bbbe7348 (diff) | |
download | sonarqube-d8fb698f07141c1e48cab3404b4641dc6d8532f7.tar.gz sonarqube-d8fb698f07141c1e48cab3404b4641dc6d8532f7.zip |
SONAR-9283 harden Props against non trimmed strings
Diffstat (limited to 'server/sonar-process/src/test/java')
-rw-r--r-- | server/sonar-process/src/test/java/org/sonar/process/PropsTest.java | 101 |
1 files changed, 70 insertions, 31 deletions
diff --git a/server/sonar-process/src/test/java/org/sonar/process/PropsTest.java b/server/sonar-process/src/test/java/org/sonar/process/PropsTest.java index 29e6acd4159..8c7d21a6567 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/PropsTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/PropsTest.java @@ -19,47 +19,83 @@ */ package org.sonar.process; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; - +import com.tngtech.java.junit.dataprovider.DataProvider; +import com.tngtech.java.junit.dataprovider.DataProviderRunner; +import com.tngtech.java.junit.dataprovider.UseDataProvider; import java.io.File; import java.io.IOException; import java.util.Properties; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; +@RunWith(DataProviderRunner.class) public class PropsTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); + @Rule + public ExpectedException expectedException = ExpectedException.none(); @Test - public void value() { + @UseDataProvider("beforeAndAfterBlanks") + public void value(String blankBefore, String blankAfter) { Properties p = new Properties(); - p.setProperty("foo", "bar"); + p.setProperty("foo", blankBefore + "bar" + blankAfter); + p.setProperty("blank", blankBefore + blankAfter); Props props = new Props(p); assertThat(props.value("foo")).isEqualTo("bar"); assertThat(props.value("foo", "default value")).isEqualTo("bar"); + assertThat(props.value("blank")).isNull(); + assertThat(props.value("blank", "default value")).isEqualTo("default value"); assertThat(props.value("unknown")).isNull(); assertThat(props.value("unknown", "default value")).isEqualTo("default value"); + } + + @Test + @UseDataProvider("beforeAndAfterBlanks") + public void nonNullValue(String blankBefore, String blankAfter) { + Properties p = new Properties(); + p.setProperty("foo", blankBefore + "bar" + blankAfter); + Props props = new Props(p); assertThat(props.nonNullValue("foo")).isEqualTo("bar"); - try { - props.nonNullValue("other"); - fail(); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessage("Missing property: other"); - } } @Test - public void valueAsInt() { + public void nonNullValue_throws_IAE_on_non_existing_key() { + Props props = new Props(new Properties()); + + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Missing property: other"); + + props.nonNullValue("other"); + } + + @Test + @UseDataProvider("beforeAndAfterBlanks") + public void nonNullValue_throws_IAE_on_existing_key_with_blank_value(String blankBefore, String blankAfter) { Properties p = new Properties(); - p.setProperty("foo", "33"); - p.setProperty("blank", ""); + p.setProperty("blank", blankBefore + blankAfter); + Props props = new Props(p); + + expectedException.expect(IllegalArgumentException.class); + + props.nonNullValue("blank"); + } + + @Test + @UseDataProvider("beforeAndAfterBlanks") + public void valueAsInt(String blankBefore, String blankAfter) { + Properties p = new Properties(); + p.setProperty("foo", blankBefore + "33" + blankAfter); + p.setProperty("blank", blankBefore + blankAfter); Props props = new Props(p); assertThat(props.valueAsInt("foo")).isEqualTo(33); @@ -71,9 +107,10 @@ public class PropsTest { } @Test - public void valueAsInt_not_integer() { + @UseDataProvider("beforeAndAfterBlanks") + public void valueAsInt_not_integer(String blankBefore, String blankAfter) { Properties p = new Properties(); - p.setProperty("foo", "bar"); + p.setProperty("foo", blankBefore + "bar" + blankAfter); Props props = new Props(p); try { @@ -85,28 +122,20 @@ public class PropsTest { } @Test - public void booleanOf() { + @UseDataProvider("beforeAndAfterBlanks") + public void booleanOf(String blankBefore, String blankAfter) { Properties p = new Properties(); - p.setProperty("foo", "True"); - p.setProperty("bar", "false"); + p.setProperty("foo", blankBefore + "True" + blankAfter); + p.setProperty("bar", blankBefore + "false" + blankAfter); Props props = new Props(p); assertThat(props.valueAsBoolean("foo")).isTrue(); assertThat(props.valueAsBoolean("bar")).isFalse(); + assertThat(props.valueAsBoolean("foo", false)).isTrue(); + assertThat(props.valueAsBoolean("bar", true)).isFalse(); assertThat(props.valueAsBoolean("unknown")).isFalse(); - } - - @Test - public void booleanOf_default_value() { - Properties p = new Properties(); - p.setProperty("foo", "true"); - p.setProperty("bar", "false"); - Props props = new Props(p); - assertThat(props.valueAsBoolean("unset", false)).isFalse(); assertThat(props.valueAsBoolean("unset", true)).isTrue(); - assertThat(props.valueAsBoolean("foo", false)).isTrue(); - assertThat(props.valueAsBoolean("bar", true)).isFalse(); } @Test @@ -162,4 +191,14 @@ public class PropsTest { assertThat(e).hasMessage("Property other_path is not set"); } } + + @DataProvider + public static Object[][] beforeAndAfterBlanks() { + return new Object[][] { + {"", ""}, + {" ", ""}, + {"", " "}, + {" ", " "}, + }; + } } |