Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JuditKnoll committed Nov 27, 2023
1 parent 21277fe commit 014046a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ void testConstructorThrowCheck16() {
assertCTBugInLine(10);
}

@Test
void testConstructorThrowCheck17() {
performAnalysis("constructorthrow/ConstructorThrowTest17.class");
assertNumOfCTBugs(1);
assertCTBugInLine(9);
}

@Test
void testGoodConstructorThrowCheck1() {
performAnalysis("constructorthrow/ConstructorThrowNegativeTest1.class");
Expand Down Expand Up @@ -203,6 +210,18 @@ void testGoodConstructorThrowCheck13() {
assertNumOfCTBugs(0);
}

@Test
void testGoodConstructorThrowCheck14() {
performAnalysis("constructorthrow/ConstructorThrowNegativeTest14.class");
assertNumOfCTBugs(0);
}

@Test
void testGoodConstructorThrowCheck15() {
performAnalysis("constructorthrow/ConstructorThrowNegativeTest15.class");
assertNumOfCTBugs(0);
}

private void assertNumOfCTBugs(int num) {
final BugInstanceMatcher bugTypeMatcher = new BugInstanceMatcherBuilder()
.bugType("CT_CONSTRUCTOR_THROW").build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package constructorthrow;

/**
* The Exception is thrown before the java.lang.Object constructor exits, so this is compliant, since in this case
* the JVM will not execute an object's finalizer.
* @see <a href="https://github.com/spotbugs/spotbugs/issues/2710">GitHub issue</a>
*/
public class ConstructorThrowNegativeTest14 {
public ConstructorThrowNegativeTest14() {
this(verify());
}

private ConstructorThrowNegativeTest14(boolean secure) {
// secure is always true
// Constructor without any checks
}

private static boolean verify() {
// Returns true if data entered is valid, else throws an Exception
// Assume that the attacker just enters invalid data, so this method always throws the exception
throw new RuntimeException("Invalid data!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package constructorthrow;

/**
* The isInitialized flag set by the constructor, and the exception is thrown by not the constructor depending on its value.
*/
public class ConstructorThrowNegativeTest15 {
private volatile boolean isInitialized = false;

public ConstructorThrowNegativeTest15() {
if (!verify()) {
return;
}

this.isInitialized = true;
}

private boolean verify() {
return false;
}

public void doSomething () {
if (!this.isInitialized) {
throw new RuntimeException();
}
// do something
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package constructorthrow;

/**
* The constructor throws an Exception conditionally.
*/
public class ConstructorThrowTest17 {
public ConstructorThrowTest17() {
if (!verify()) {
throw new RuntimeException();
}
}

private boolean verify() {
return false;
}
}

0 comments on commit 014046a

Please sign in to comment.