-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🦺 Remove Non-Null Invariant from Findings
See [CodeTF specification update](pixee/codemodder-specs#38)
- Loading branch information
Showing
9 changed files
with
184 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.codemodder.codetf; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Describes a detected finding that served as input to the codemod. | ||
* | ||
* <p>When a codemod is able to fix a finding, it should create a {@link FixedFinding} instance. If | ||
* a codemod would typically fix findings of this type but cannot, it can create an {@link | ||
* UnfixedFinding} instance to explain why. | ||
* | ||
* <p>Findings typically have some ID specified in the detector results. | ||
*/ | ||
public abstract sealed class Finding permits FixedFinding, UnfixedFinding { | ||
|
||
private final String id; | ||
private final DetectorRule rule; | ||
|
||
Finding(final String id, final DetectorRule rule) { | ||
this.id = id; | ||
this.rule = Objects.requireNonNull(rule); | ||
} | ||
|
||
Finding(final DetectorRule rule) { | ||
this(null, rule); | ||
} | ||
|
||
/** | ||
* @return the ID of the finding, or {@code null} if the finding has no ID | ||
*/ | ||
public String getId() { | ||
return id; | ||
} | ||
|
||
/** | ||
* @return the rule that detected the finding | ||
*/ | ||
public DetectorRule getRule() { | ||
return rule; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
final Finding finding = (Finding) o; | ||
return Objects.equals(id, finding.id) && rule.equals(finding.rule); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = Objects.hashCode(id); | ||
result = 31 * result + rule.hashCode(); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/test/java/io/codemodder/codetf/EqualsAndHashcodeTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.codemodder.codetf; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public interface EqualsAndHashcodeTests<T> { | ||
|
||
/** | ||
* @return a new instance of the class under test | ||
*/ | ||
T createInstance(); | ||
|
||
/** | ||
* @return a new instance of the class under test that is equal to the instance returned by {@link | ||
* #createInstance()} | ||
*/ | ||
default T createEqualInstance() { | ||
return createInstance(); | ||
} | ||
|
||
/** | ||
* @return a new instance of the class under test that is different from the instance returned by | ||
* {@link #createInstance()} | ||
*/ | ||
T createDifferentInstance(); | ||
|
||
@Test | ||
default void testEquals() { | ||
final T instance = createInstance(); | ||
final T equalInstance = createEqualInstance(); | ||
final T differentInstance = createDifferentInstance(); | ||
|
||
assertEquals(instance, equalInstance, "Instances should be equal"); | ||
assertNotEquals(instance, differentInstance, "Instances should not be equal"); | ||
assertNotEquals(equalInstance, differentInstance, "Instances should not be equal"); | ||
} | ||
|
||
@Test | ||
default void testHashCode() { | ||
final T instance = createInstance(); | ||
final T equalInstance = createEqualInstance(); | ||
final T differentInstance = createDifferentInstance(); | ||
|
||
assertEquals(instance.hashCode(), equalInstance.hashCode(), "Hash codes should be equal"); | ||
assertNotEquals( | ||
instance.hashCode(), differentInstance.hashCode(), "Hash codes should not be equal"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.codemodder.codetf; | ||
|
||
/** Unit tests for {@link FixedFinding}. */ | ||
final class FixedFindingTest implements EqualsAndHashcodeTests<FixedFinding> { | ||
|
||
@Override | ||
public FixedFinding createInstance() { | ||
final var rule = | ||
new DetectorRule( | ||
"xxe", | ||
"XML External Entities", | ||
"https://owasp.org/www-community/vulnerabilities/XML_External_Entity_(XXE)_Processing"); | ||
return new FixedFinding(rule); | ||
} | ||
|
||
@Override | ||
public FixedFinding createDifferentInstance() { | ||
return new FixedFinding( | ||
"sql-injection/foo", | ||
new DetectorRule( | ||
"sqli", | ||
"SQL Injection", | ||
"https://owasp.org/www-community/vulnerabilities/SQL_Injection")); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/test/java/io/codemodder/codetf/UnfixedFindingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.codemodder.codetf; | ||
|
||
/** Unit tests for {@link UnfixedFinding}. */ | ||
final class UnfixedFindingTest implements EqualsAndHashcodeTests<UnfixedFinding> { | ||
|
||
@Override | ||
public UnfixedFinding createInstance() { | ||
final var rule = | ||
new DetectorRule( | ||
"xxe", | ||
"XML External Entities", | ||
"https://owasp.org/www-community/vulnerabilities/XML_External_Entity_(XXE)_Processing"); | ||
return new UnfixedFinding(rule, "src/main/java/com/acme/Foo.java", 42, "This is bad"); | ||
} | ||
|
||
@Override | ||
public UnfixedFinding createDifferentInstance() { | ||
final var rule = | ||
new DetectorRule( | ||
"sqli", | ||
"SQL Injection", | ||
"https://owasp.org/www-community/vulnerabilities/SQL_Injection"); | ||
return new UnfixedFinding( | ||
"sql-injection/foo", rule, "src/main/java/com/acme/Bar.java", 84, "This is also bad"); | ||
} | ||
} |