mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-14 12:32:15 +00:00
Prefer assertThrows to try/fail (#7114)
This commit is contained in:
1 parent
db6ce9a01e
commit
3c2f9f641e
19 files changed
+165
-247
No files matched your search
@@ -26,8 +26,8 @@ package hudson.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.thoughtworks.xstream.security.InputManipulationException;
|
||||
import java.time.Instant;
|
||||
@@ -82,16 +82,12 @@ public class RobustCollectionConverterTest {
|
||||
|
||||
xstream2.setCollectionUpdateLimit(3);
|
||||
final String xml = xstream2.toXML(set);
|
||||
try {
|
||||
xstream2.fromXML(xml);
|
||||
fail("Thrown " + CriticalXStreamException.class.getName() + " expected");
|
||||
} catch (final CriticalXStreamException e) {
|
||||
Throwable cause = e.getCause();
|
||||
assertNotNull("A non-null cause of CriticalXStreamException is expected", cause);
|
||||
assertTrue("Cause of CriticalXStreamException is expected to be InputManipulationException", cause instanceof InputManipulationException);
|
||||
InputManipulationException ime = (InputManipulationException) cause;
|
||||
assertTrue("Limit expected in message", ime.getMessage().contains("exceeds 3 seconds"));
|
||||
}
|
||||
CriticalXStreamException e = assertThrows(CriticalXStreamException.class, () -> xstream2.fromXML(xml));
|
||||
Throwable cause = e.getCause();
|
||||
assertNotNull("A non-null cause of CriticalXStreamException is expected", cause);
|
||||
assertTrue("Cause of CriticalXStreamException is expected to be InputManipulationException", cause instanceof InputManipulationException);
|
||||
InputManipulationException ime = (InputManipulationException) cause;
|
||||
assertTrue("Limit expected in message", ime.getMessage().contains("exceeds 3 seconds"));
|
||||
}
|
||||
|
||||
@Test(timeout = 30 * 1000)
|
||||
@@ -106,16 +102,12 @@ public class RobustCollectionConverterTest {
|
||||
Set<Object> set = preparePayload();
|
||||
|
||||
final String xml = xstream2.toXML(set);
|
||||
try {
|
||||
xstream2.fromXML(xml);
|
||||
fail("Thrown " + CriticalXStreamException.class.getName() + " expected");
|
||||
} catch (final CriticalXStreamException e) {
|
||||
Throwable cause = e.getCause();
|
||||
assertNotNull("A non-null cause of CriticalXStreamException is expected", cause);
|
||||
assertTrue("Cause of CriticalXStreamException is expected to be InputManipulationException", cause instanceof InputManipulationException);
|
||||
InputManipulationException ime = (InputManipulationException) cause;
|
||||
assertTrue("Limit expected in message", ime.getMessage().contains("exceeds 4 seconds"));
|
||||
}
|
||||
CriticalXStreamException e = assertThrows(CriticalXStreamException.class, () -> xstream2.fromXML(xml));
|
||||
Throwable cause = e.getCause();
|
||||
assertNotNull("A non-null cause of CriticalXStreamException is expected", cause);
|
||||
assertTrue("Cause of CriticalXStreamException is expected to be InputManipulationException", cause instanceof InputManipulationException);
|
||||
InputManipulationException ime = (InputManipulationException) cause;
|
||||
assertTrue("Limit expected in message", ime.getMessage().contains("exceeds 4 seconds"));
|
||||
} finally {
|
||||
if (currentValue == null) {
|
||||
System.clearProperty(XStream2.COLLECTION_UPDATE_LIMIT_PROPERTY_NAME);
|
||||
@@ -134,16 +126,12 @@ public class RobustCollectionConverterTest {
|
||||
Set<Object> set = preparePayload();
|
||||
|
||||
final String xml = xstream2.toXML(set);
|
||||
try {
|
||||
xstream2.fromXML(xml);
|
||||
fail("Thrown " + CriticalXStreamException.class.getName() + " expected");
|
||||
} catch (final CriticalXStreamException e) {
|
||||
Throwable cause = e.getCause();
|
||||
assertNotNull("A non-null cause of CriticalXStreamException is expected", cause);
|
||||
assertTrue("Cause of CriticalXStreamException is expected to be InputManipulationException", cause instanceof InputManipulationException);
|
||||
InputManipulationException ime = (InputManipulationException) cause;
|
||||
assertTrue("Limit expected in message", ime.getMessage().contains("exceeds 5 seconds"));
|
||||
}
|
||||
CriticalXStreamException e = assertThrows(CriticalXStreamException.class, () -> xstream2.fromXML(xml));
|
||||
Throwable cause = e.getCause();
|
||||
assertNotNull("A non-null cause of CriticalXStreamException is expected", cause);
|
||||
assertTrue("Cause of CriticalXStreamException is expected to be InputManipulationException", cause instanceof InputManipulationException);
|
||||
InputManipulationException ime = (InputManipulationException) cause;
|
||||
assertTrue("Limit expected in message", ime.getMessage().contains("exceeds 5 seconds"));
|
||||
}
|
||||
|
||||
// Inspired by https://github.com/x-stream/xstream/commit/e8e88621ba1c85ac3b8620337dd672e0c0c3a846#diff-9fde4ecf1bb4dc9850c031cb161960d2e61e069b386fa0b3db0d57e0e9f5baa
|
||||
|
||||
@@ -25,8 +25,8 @@
|
||||
package hudson.util;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.thoughtworks.xstream.security.InputManipulationException;
|
||||
import java.util.HashMap;
|
||||
@@ -51,16 +51,12 @@ public class RobustMapConverterTest {
|
||||
|
||||
xstream2.setCollectionUpdateLimit(3);
|
||||
final String xml = xstream2.toXML(map);
|
||||
try {
|
||||
xstream2.fromXML(xml);
|
||||
fail("Thrown " + CriticalXStreamException.class.getName() + " expected");
|
||||
} catch (final CriticalXStreamException e) {
|
||||
Throwable cause = e.getCause();
|
||||
assertNotNull("A non-null cause of CriticalXStreamException is expected", cause);
|
||||
assertTrue("Cause of CriticalXStreamException is expected to be InputManipulationException", cause instanceof InputManipulationException);
|
||||
InputManipulationException ime = (InputManipulationException) cause;
|
||||
assertTrue("Limit expected in message", ime.getMessage().contains("exceeds 3 seconds"));
|
||||
}
|
||||
CriticalXStreamException e = assertThrows(CriticalXStreamException.class, () -> xstream2.fromXML(xml));
|
||||
Throwable cause = e.getCause();
|
||||
assertNotNull("A non-null cause of CriticalXStreamException is expected", cause);
|
||||
assertTrue("Cause of CriticalXStreamException is expected to be InputManipulationException", cause instanceof InputManipulationException);
|
||||
InputManipulationException ime = (InputManipulationException) cause;
|
||||
assertTrue("Limit expected in message", ime.getMessage().contains("exceeds 3 seconds"));
|
||||
}
|
||||
|
||||
@Test(timeout = 30 * 1000)
|
||||
@@ -75,16 +71,12 @@ public class RobustMapConverterTest {
|
||||
Map<Object, Object> map = preparePayload();
|
||||
|
||||
final String xml = xstream2.toXML(map);
|
||||
try {
|
||||
xstream2.fromXML(xml);
|
||||
fail("Thrown " + CriticalXStreamException.class.getName() + " expected");
|
||||
} catch (final CriticalXStreamException e) {
|
||||
Throwable cause = e.getCause();
|
||||
assertNotNull("A non-null cause of CriticalXStreamException is expected", cause);
|
||||
assertTrue("Cause of CriticalXStreamException is expected to be InputManipulationException", cause instanceof InputManipulationException);
|
||||
InputManipulationException ime = (InputManipulationException) cause;
|
||||
assertTrue("Limit expected in message", ime.getMessage().contains("exceeds 4 seconds"));
|
||||
}
|
||||
CriticalXStreamException e = assertThrows(CriticalXStreamException.class, () -> xstream2.fromXML(xml));
|
||||
Throwable cause = e.getCause();
|
||||
assertNotNull("A non-null cause of CriticalXStreamException is expected", cause);
|
||||
assertTrue("Cause of CriticalXStreamException is expected to be InputManipulationException", cause instanceof InputManipulationException);
|
||||
InputManipulationException ime = (InputManipulationException) cause;
|
||||
assertTrue("Limit expected in message", ime.getMessage().contains("exceeds 4 seconds"));
|
||||
} finally {
|
||||
if (currentValue == null) {
|
||||
System.clearProperty(XStream2.COLLECTION_UPDATE_LIMIT_PROPERTY_NAME);
|
||||
@@ -103,16 +95,12 @@ public class RobustMapConverterTest {
|
||||
Map<Object, Object> map = preparePayload();
|
||||
|
||||
final String xml = xstream2.toXML(map);
|
||||
try {
|
||||
xstream2.fromXML(xml);
|
||||
fail("Thrown " + CriticalXStreamException.class.getName() + " expected");
|
||||
} catch (final CriticalXStreamException e) {
|
||||
Throwable cause = e.getCause();
|
||||
assertNotNull("A non-null cause of CriticalXStreamException is expected", cause);
|
||||
assertTrue("Cause of CriticalXStreamException is expected to be InputManipulationException", cause instanceof InputManipulationException);
|
||||
InputManipulationException ime = (InputManipulationException) cause;
|
||||
assertTrue("Limit expected in message", ime.getMessage().contains("exceeds 5 seconds"));
|
||||
}
|
||||
CriticalXStreamException e = assertThrows(CriticalXStreamException.class, () -> xstream2.fromXML(xml));
|
||||
Throwable cause = e.getCause();
|
||||
assertNotNull("A non-null cause of CriticalXStreamException is expected", cause);
|
||||
assertTrue("Cause of CriticalXStreamException is expected to be InputManipulationException", cause instanceof InputManipulationException);
|
||||
InputManipulationException ime = (InputManipulationException) cause;
|
||||
assertTrue("Limit expected in message", ime.getMessage().contains("exceeds 5 seconds"));
|
||||
}
|
||||
|
||||
// Inspired by https://github.com/x-stream/xstream/commit/e8e88621ba1c85ac3b8620337dd672e0c0c3a846#diff-9fde4ecf1bb4dc9850c031cb161960d2e61e069b386fa0b3db0d57e0e9f5baa
|
||||
|
||||
@@ -30,7 +30,6 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import com.thoughtworks.xstream.converters.ConversionException;
|
||||
@@ -194,16 +193,12 @@ public class RobustReflectionConverterTest {
|
||||
|
||||
final String xml = xstream2.toXML(parentItem);
|
||||
|
||||
try {
|
||||
xstream2.fromXML(xml);
|
||||
fail("Thrown " + CriticalXStreamException.class.getName() + " expected");
|
||||
} catch (final CriticalXStreamException e) {
|
||||
Throwable cause = e.getCause();
|
||||
assertNotNull("A non-null cause of CriticalXStreamException is expected", cause);
|
||||
assertTrue("Cause of CriticalXStreamException is expected to be InputManipulationException", cause instanceof InputManipulationException);
|
||||
InputManipulationException ime = (InputManipulationException) cause;
|
||||
assertTrue("Limit expected in message", ime.getMessage().contains("exceeds 5 seconds"));
|
||||
}
|
||||
CriticalXStreamException e = assertThrows(CriticalXStreamException.class, () -> xstream2.fromXML(xml));
|
||||
Throwable cause = e.getCause();
|
||||
assertNotNull("A non-null cause of CriticalXStreamException is expected", cause);
|
||||
assertTrue("Cause of CriticalXStreamException is expected to be InputManipulationException", cause instanceof InputManipulationException);
|
||||
InputManipulationException ime = (InputManipulationException) cause;
|
||||
assertTrue("Limit expected in message", ime.getMessage().contains("exceeds 5 seconds"));
|
||||
}
|
||||
|
||||
@Test(timeout = 30 * 1000)
|
||||
@@ -216,12 +211,8 @@ public class RobustReflectionConverterTest {
|
||||
// Will use the ConverterImpl without going with RobustReflectionConverter
|
||||
final String xml = xstream2.toXML(customSet);
|
||||
|
||||
try {
|
||||
xstream2.fromXML(xml);
|
||||
fail("Thrown " + InputManipulationException.class.getName() + " expected");
|
||||
} catch (final InputManipulationException e) {
|
||||
assertTrue("Limit expected in message", e.getMessage().contains("exceeds 5 seconds"));
|
||||
}
|
||||
InputManipulationException e = assertThrows(InputManipulationException.class, () -> xstream2.fromXML(xml));
|
||||
assertTrue("Limit expected in message", e.getMessage().contains("exceeds 5 seconds"));
|
||||
}
|
||||
|
||||
@Test(timeout = 30 * 1000)
|
||||
@@ -239,18 +230,14 @@ public class RobustReflectionConverterTest {
|
||||
|
||||
final String xml = xstream2.toXML(parentObject);
|
||||
|
||||
try {
|
||||
xstream2.fromXML(xml);
|
||||
// Without the InputManipulationException catch in RobustReflectionConverter,
|
||||
// the parsing is continued despite the DoS prevention being triggered due to the robustness
|
||||
fail("Thrown " + CriticalXStreamException.class.getName() + " expected");
|
||||
} catch (final CriticalXStreamException e) {
|
||||
Throwable cause = e.getCause();
|
||||
assertNotNull("A non-null cause of CriticalXStreamException is expected", cause);
|
||||
assertTrue("Cause of CriticalXStreamException is expected to be InputManipulationException", cause instanceof InputManipulationException);
|
||||
InputManipulationException ime = (InputManipulationException) cause;
|
||||
assertTrue("Limit expected in message", ime.getMessage().contains("exceeds 5 seconds"));
|
||||
}
|
||||
// Without the InputManipulationException catch in RobustReflectionConverter,
|
||||
// the parsing is continued despite the DoS prevention being triggered due to the robustness
|
||||
CriticalXStreamException e = assertThrows(CriticalXStreamException.class, () -> xstream2.fromXML(xml));
|
||||
Throwable cause = e.getCause();
|
||||
assertNotNull("A non-null cause of CriticalXStreamException is expected", cause);
|
||||
assertTrue("Cause of CriticalXStreamException is expected to be InputManipulationException", cause instanceof InputManipulationException);
|
||||
InputManipulationException ime = (InputManipulationException) cause;
|
||||
assertTrue("Limit expected in message", ime.getMessage().contains("exceeds 5 seconds"));
|
||||
}
|
||||
|
||||
private Set<Object> preparePayload() {
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
package jenkins.model;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
|
||||
import hudson.model.Failure;
|
||||
import hudson.model.Messages;
|
||||
@@ -36,23 +36,21 @@ public class Security2424Test {
|
||||
@Test
|
||||
@Issue("SECURITY-2424")
|
||||
public void doesNotAcceptNameWithTrailingDot_regular() {
|
||||
try {
|
||||
Jenkins.checkGoodName("job.");
|
||||
fail("Names with dot should not be accepted");
|
||||
} catch (Failure e) {
|
||||
assertEquals(Messages.Hudson_TrailingDot(), e.getMessage());
|
||||
}
|
||||
Failure e = assertThrows(
|
||||
"Names with dot should not be accepted",
|
||||
Failure.class,
|
||||
() -> Jenkins.checkGoodName("job."));
|
||||
assertEquals(Messages.Hudson_TrailingDot(), e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Issue("SECURITY-2424")
|
||||
public void doesNotAcceptNameWithTrailingDot_withSpaces() {
|
||||
try {
|
||||
Jenkins.checkGoodName("job. ");
|
||||
fail("Names with dot should not be accepted");
|
||||
} catch (Failure e) {
|
||||
assertEquals(Messages.Hudson_TrailingDot(), e.getMessage());
|
||||
}
|
||||
Failure e = assertThrows(
|
||||
"Names with dot should not be accepted",
|
||||
Failure.class,
|
||||
() -> Jenkins.checkGoodName("job. "));
|
||||
assertEquals(Messages.Hudson_TrailingDot(), e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user