Skip to content

Commit d1aa364

Browse files
author
Dave Wichers
committed
Additional work in progress. Changed testcase expected/actual results
map to use a String instead of int. For normal Benchmark scoring, the results key is the testcase number, now as a string representation of the test case number. For custom scoring, the results key is the name of the entire test case file. Still have to implement changes to each tool parser to be able to parse out either the test case number, or provide the entire filename, depending on the scoring approach.
1 parent 1b27b66 commit d1aa364

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+180
-163
lines changed

library/src/main/java/org/owasp/benchmarkutils/helpers/Category.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ public class Category implements Comparable<Category> {
3939
*/
4040
public Category(String id, String name, int cwe, boolean isInjection, String shortname) {
4141
this.id = id;
42+
if (name.contains("/") || name.contains("\\")) {
43+
System.out.println(
44+
"FATAL ERROR: CWE name from provided categories.xml file: '"
45+
+ name
46+
+ "' contains a path character, which breaks scorecard generation.");
47+
System.exit(-1);
48+
}
4249
this.name = name;
4350
this.CWE = cwe;
4451
this.isInjection = isInjection;

library/src/main/resources/categories.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
</category>
107107
<category>
108108
<id>reflecti</id>
109-
<name>Reflection</name>
109+
<name>Unsafe Reflection</name>
110110
<cwe>470</cwe>
111111
<isInjection>true</isInjection>
112112
<shortname>REFL</shortname>

plugin/src/main/java/org/owasp/benchmarkutils/score/BenchmarkScore.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -575,16 +575,16 @@ private static void process(
575575
private static void printExtraCWE(
576576
TestSuiteResults expectedResults, TestSuiteResults actualResults) {
577577
Set<Integer> expectedCWE = new HashSet<Integer>();
578-
for (int i : expectedResults.keySet()) {
579-
List<TestCaseResult> list = expectedResults.get(i);
578+
for (String testcase : expectedResults.keySet()) {
579+
List<TestCaseResult> list = expectedResults.get(testcase);
580580
for (TestCaseResult t : list) {
581581
expectedCWE.add(t.getCWE());
582582
}
583583
}
584584

585585
Set<Integer> actualCWE = new HashSet<Integer>();
586-
for (int i : actualResults.keySet()) {
587-
List<TestCaseResult> list = actualResults.get(i);
586+
for (String testcase : actualResults.keySet()) {
587+
List<TestCaseResult> list = actualResults.get(testcase);
588588
if (list != null) {
589589
for (TestCaseResult t : list) {
590590
actualCWE.add(t.getCWE());
@@ -682,8 +682,8 @@ public static int translateNameToCWE(String categoryName) {
682682
private static Map<String, TP_FN_TN_FP_Counts> calculateScores(TestSuiteResults actualResults) {
683683
Map<String, TP_FN_TN_FP_Counts> map = new TreeMap<String, TP_FN_TN_FP_Counts>();
684684

685-
for (Integer tn : actualResults.keySet()) {
686-
TestCaseResult tcr = actualResults.get(tn).get(0); // only one
685+
for (String testcase : actualResults.keySet()) {
686+
TestCaseResult tcr = actualResults.get(testcase).get(0); // only one
687687
String cat = Categories.getById(tcr.getCategory()).getName();
688688

689689
TP_FN_TN_FP_Counts c = map.get(cat);
@@ -763,17 +763,16 @@ private static TestSuiteResults analyze(
763763
}
764764

765765
boolean pass = false;
766-
for (int tc : expected.keySet()) {
767-
TestCaseResult exp = expected.get(tc).get(0); // always only one!
766+
for (String testcase : expected.keySet()) {
767+
TestCaseResult exp = expected.get(testcase).get(0); // always only one!
768768
List<TestCaseResult> act =
769-
rawToolResults.get(tc); // could be lots of results for this test
769+
rawToolResults.get(testcase); // could be lots of results for this test
770770

771771
pass = compare(exp, act, rawToolResults.getToolName());
772772

773773
// helpful in debugging
774-
// System.out.println( tc + ", " + exp.getCategory() + ", " + exp.isTruePositive() + ",
775-
// " +
776-
// exp.getCWE() + ", " + pass + "\n");
774+
// System.out.println( testcase + ", " + exp.getCategory() + ", " + exp.isTruePositive()
775+
// + "," + exp.getCWE() + ", " + pass + "\n");
777776

778777
// fill the result into the "expected" results in case we need it later
779778
exp.setPassed(pass);

plugin/src/main/java/org/owasp/benchmarkutils/score/TestCaseResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void setTestCaseName(String name) {
8181
/*
8282
* The name of the test case. E.g., BenchmarkTest00001
8383
*/
84-
public String getName() {
84+
public String getTestCaseName() {
8585
return testCaseName;
8686
}
8787

plugin/src/main/java/org/owasp/benchmarkutils/score/TestSuiteResults.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,15 @@ public static enum ToolType {
4848
// The name and version of the test suite these test results are for
4949
private String testSuiteName = "notSet";
5050
private String testSuiteVersion = "notSet";
51+
private boolean standardBenchmarkStyleScoring = true;
5152

5253
private String toolName = "Unknown Tool";
5354
private String toolVersion = null;
5455
private String time = "Unknown"; // Scan time. e.g., '0:17:29'
5556
public final boolean isCommercial;
5657
public final ToolType toolType;
57-
private Map<Integer, List<TestCaseResult>> testCaseResults =
58-
new TreeMap<Integer, List<TestCaseResult>>();
58+
private Map<String, List<TestCaseResult>> testCaseResults =
59+
new TreeMap<String, List<TestCaseResult>>();
5960

6061
// Used to track if this tool has been anonymized
6162
private boolean anonymous = false;
@@ -102,35 +103,47 @@ public boolean isCommercial() {
102103
}
103104

104105
/**
105-
* Add a test case result to the set of results for this tool.
106+
* Add a test case result to the set of results for this tool or expected results file.
106107
*
107108
* @param tcr The test case result to add.
108109
*/
109110
public void put(TestCaseResult tcr) {
110111

111-
// This warning message is added just in case. It can be caused by a buggy parser or
112-
// invalid results file.
113112
int testCaseNum = tcr.getNumber();
114-
if ((testCaseNum <= 0 || testCaseNum > 10000)
115-
&& testCaseNum != TestCaseResult.NOT_USING_TESTCASE_NUMBERS) {
116-
System.out.println(
117-
"WARNING: Did you really intend to add a test case result for test case: "
118-
+ testCaseNum);
113+
String testCaseKey;
114+
115+
// If we are using test case numbers, we add each result to that specific test case number
116+
if (this.standardBenchmarkStyleScoring
117+
&& (testCaseNum != TestCaseResult.NOT_USING_TESTCASE_NUMBERS)) {
118+
// This warning message is added just in case. It can be caused by a buggy parser or
119+
// invalid results file.
120+
if ((testCaseNum <= 0 || testCaseNum > 10000)) {
121+
System.out.println(
122+
"WARNING: Did you really intend to add a test case result for test case: "
123+
+ testCaseNum);
124+
}
125+
126+
testCaseKey = String.valueOf(testCaseNum);
127+
} else {
128+
// otherwise use test case names as the key, and we add each result by test case name
129+
testCaseKey = tcr.getTestCaseName();
130+
this.standardBenchmarkStyleScoring = false;
119131
}
120132

121133
// There is a list of results for each test case
122-
List<TestCaseResult> results = testCaseResults.get(testCaseNum);
134+
List<TestCaseResult> results = testCaseResults.get(testCaseKey);
123135
if (results == null) {
124136
// If there are no results yet for this test case, create a List.
125-
// Add this list for this test case to the set of results
137+
// Add this entry for this test case to the set of results
126138
results = new ArrayList<TestCaseResult>();
127-
testCaseResults.put(tcr.getNumber(), results);
139+
testCaseResults.put(testCaseKey, results);
128140
}
141+
129142
// Add this specific result to this test case's results
130143
results.add(tcr);
131144
}
132145

133-
public List<TestCaseResult> get(int tn) {
146+
public List<TestCaseResult> get(String tn) {
134147
return testCaseResults.get(tn);
135148
}
136149

@@ -139,7 +152,7 @@ public List<TestCaseResult> get(int tn) {
139152
*
140153
* @return The Set of Keys.
141154
*/
142-
public Set<Integer> keySet() {
155+
public Set<String> keySet() {
143156
return testCaseResults.keySet();
144157
}
145158

plugin/src/main/java/org/owasp/benchmarkutils/score/service/ExpectedResultsProvider.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static TestSuiteResults parse(ResultFile resultFile) throws IOException {
4949
try (final CSVParser parser = resultFile.csvRecords()) {
5050
setResultsMetadata(parser, tr);
5151

52-
String testCaseName = tr.getTestSuiteName() + BenchmarkScore.TEST;
52+
final String TESTCASENAME = tr.getTestSuiteName() + BenchmarkScore.TEST;
5353

5454
for (CSVRecord record : parser) {
5555
TestCaseResult tcr = new TestCaseResult();
@@ -70,9 +70,10 @@ public static TestSuiteResults parse(ResultFile resultFile) throws IOException {
7070
}
7171
if (record.get(TEST_NAME)
7272
.trim()
73-
.startsWith(tr.getTestSuiteName() + BenchmarkScore.TEST)) {
74-
tcr.setNumber(testNumber(record.get(TEST_NAME).trim(), testCaseName));
75-
} else tcr.setNumber(TestCaseResult.NOT_USING_TESTCASE_NUMBERS);
73+
.startsWith(tr.getTestSuiteName() + BenchmarkScore.TEST))
74+
tcr.setNumber(testNumber(record.get(TEST_NAME).trim(), TESTCASENAME));
75+
else tcr.setNumber(TestCaseResult.NOT_USING_TESTCASE_NUMBERS);
76+
7677
if (isExtendedResultsFile(parser)) {
7778
tcr.setSource(record.get(SOURCE).trim());
7879
tcr.setDataFlow(record.get(DATA_FLOW).trim());

plugin/src/main/java/org/owasp/benchmarkutils/score/service/ResultsFileCreator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public String createFor(TestSuiteResults actual) {
7272
}
7373

7474
private boolean isFullDetails(TestSuiteResults actual) {
75-
Iterator<Integer> iterator = actual.keySet().iterator();
75+
Iterator<String> iterator = actual.keySet().iterator();
7676

7777
return iterator.hasNext() && (actual.get(iterator.next()).get(0).getSource() != null);
7878
}
@@ -94,12 +94,12 @@ private void writeHeader(PrintStream ps, boolean fullDetails, String testSuiteVe
9494
}
9595

9696
private void appendRow(
97-
PrintStream ps, TestSuiteResults actual, Integer testNumber, boolean fullDetails) {
98-
TestCaseResult actualResult = actual.get(testNumber).get(0);
97+
PrintStream ps, TestSuiteResults actual, String testcaseID, boolean fullDetails) {
98+
TestCaseResult actualResult = actual.get(testcaseID).get(0);
9999
boolean isReal = actualResult.isTruePositive();
100100
boolean passed = actualResult.isPassed();
101101

102-
ps.print(actualResult.getName());
102+
ps.print(actualResult.getTestCaseName());
103103
ps.print(", " + actualResult.getCategory());
104104
ps.print(", " + actualResult.getCWE());
105105

plugin/src/main/java/org/owasp/benchmarkutils/tools/CalculateToolCodeBlocksSupport.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,8 @@ protected void run() {
371371
// 3. Calculate which codeblocks that tool seems to support and does not support
372372

373373
// 3a. Loop through all the results in theToolResults to calculate the initial
374-
// statistics
375-
// across all of them.
376-
for (int tc : theToolResults.keySet()) {
374+
// statistics across all of them.
375+
for (String tc : theToolResults.keySet()) {
377376
TestCaseResult theResult = theToolResults.get(tc).get(0); // Always only one.
378377
boolean passed = theResult.isPassed();
379378
CodeBlockSupportResults source = sourceCodeBlocksResults.get(theResult.getSource());
@@ -393,9 +392,8 @@ protected void run() {
393392
// If a TP passes, we 'assume' that all code block elements are supported
394393

395394
// However, sources can be false positives, but the dataflow introduces
396-
// taint, so we
397-
// only 'know' if a source is supported if the source is also a true
398-
// positive.
395+
// taint, so we only 'know' if a source is supported if the source is
396+
// also a true positive.
399397
if (source.truePositive) source.supported = true;
400398
source.numTPTestCasesPassed++;
401399
dataflow.supported = true;
@@ -434,7 +432,7 @@ protected void run() {
434432
// SOURCEs/DATAFLOWs: Calculate which sources cause TPs to NOT be detected.
435433
// Loop through them all again and calculate the number of TPs for each source that
436434
// pass/fail, ignoring any failures that are caused by unsupported SINKs.
437-
for (int tc : theToolResults.keySet()) {
435+
for (String tc : theToolResults.keySet()) {
438436
TestCaseResult theResult = theToolResults.get(tc).get(0); // Always only one.
439437
boolean passed = theResult.isPassed();
440438
CodeBlockSupportResults source = sourceCodeBlocksResults.get(theResult.getSource());
@@ -485,7 +483,7 @@ protected void run() {
485483
// Check Failures where the sink is 'supported' or hasn't been reported as a False
486484
// Positive and the dataflow is 'null'.
487485
boolean foundFPorFN = false;
488-
for (int tc : theToolResults.keySet()) {
486+
for (String tc : theToolResults.keySet()) {
489487
TestCaseResult theResult = theToolResults.get(tc).get(0); // Always only one.
490488
boolean passed = theResult.isPassed();
491489
CodeBlockSupportResults source = sourceCodeBlocksResults.get(theResult.getSource());
@@ -592,7 +590,7 @@ protected void run() {
592590
// Print out codeblock coordinates of the rest of the False Positives, ignoring all with
593591
// sinks or sources already known to cause FPs
594592
int FPCount = 1;
595-
for (int tc : theToolResults.keySet()) {
593+
for (String tc : theToolResults.keySet()) {
596594
TestCaseResult theResult = theToolResults.get(tc).get(0); // Always only one.
597595
boolean passed = theResult.isPassed();
598596
CodeBlockSupportResults source = sourceCodeBlocksResults.get(theResult.getSource());

plugin/src/test/java/org/owasp/benchmarkutils/score/parsers/AcunetixReaderTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ void readerHandlesGivenResultFile() throws Exception {
5757

5858
assertEquals(2, result.getTotalResults());
5959

60-
assertEquals(CweNumber.COMMAND_INJECTION, result.get(1).get(0).getCWE());
61-
assertEquals(CweNumber.XSS, result.get(2).get(0).getCWE());
60+
assertEquals(CweNumber.COMMAND_INJECTION, result.get("1").get(0).getCWE());
61+
assertEquals(CweNumber.XSS, result.get("2").get(0).getCWE());
6262

6363
// For Acunetix WVS
6464
reader = new AcunetixReader();
@@ -70,7 +70,7 @@ void readerHandlesGivenResultFile() throws Exception {
7070

7171
assertEquals(2, result.getTotalResults());
7272

73-
assertEquals(CweNumber.LDAP_INJECTION, result.get(44).get(0).getCWE());
74-
assertEquals(CweNumber.SQL_INJECTION, result.get(2629).get(0).getCWE());
73+
assertEquals(CweNumber.LDAP_INJECTION, result.get("44").get(0).getCWE());
74+
assertEquals(CweNumber.SQL_INJECTION, result.get("2629").get(0).getCWE());
7575
}
7676
}

plugin/src/test/java/org/owasp/benchmarkutils/score/parsers/ArachniReaderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void readerHandlesGivenResultFile() throws Exception {
5454

5555
assertEquals(2, result.getTotalResults());
5656

57-
assertEquals(CweNumber.XSS, result.get(1).get(0).getCWE());
58-
assertEquals(CweNumber.XSS, result.get(2).get(0).getCWE());
57+
assertEquals(CweNumber.XSS, result.get("1").get(0).getCWE());
58+
assertEquals(CweNumber.XSS, result.get("2").get(0).getCWE());
5959
}
6060
}

plugin/src/test/java/org/owasp/benchmarkutils/score/parsers/BearerReaderTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ void readerHandlesGivenResultFileInV1_30() throws Exception {
5555

5656
assertEquals(3, result.getTotalResults());
5757

58-
assertEquals(CweNumber.COMMAND_INJECTION, result.get(7).get(0).getCWE());
59-
assertEquals(CweNumber.WEAK_HASH_ALGO, result.get(5).get(0).getCWE());
60-
assertEquals(CweNumber.WEAK_CRYPTO_ALGO, result.get(35).get(0).getCWE());
58+
assertEquals(CweNumber.COMMAND_INJECTION, result.get("7").get(0).getCWE());
59+
assertEquals(CweNumber.WEAK_HASH_ALGO, result.get("5").get(0).getCWE());
60+
assertEquals(CweNumber.WEAK_CRYPTO_ALGO, result.get("35").get(0).getCWE());
6161
}
6262
}

plugin/src/test/java/org/owasp/benchmarkutils/score/parsers/BurpReaderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void readerHandlesGivenResultFile() throws Exception {
5454

5555
assertEquals(2, result.getTotalResults());
5656

57-
assertEquals(CweNumber.COMMAND_INJECTION, result.get(1).get(0).getCWE());
58-
assertEquals(CweNumber.SQL_INJECTION, result.get(2).get(0).getCWE());
57+
assertEquals(CweNumber.COMMAND_INJECTION, result.get("1").get(0).getCWE());
58+
assertEquals(CweNumber.SQL_INJECTION, result.get("2").get(0).getCWE());
5959
}
6060
}

plugin/src/test/java/org/owasp/benchmarkutils/score/parsers/CASTAIPReaderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void readerHandlesGivenResultFile() throws Exception {
5454

5555
assertEquals(2, result.getTotalResults());
5656

57-
assertEquals(CweNumber.COMMAND_INJECTION, result.get(1).get(0).getCWE());
58-
assertEquals(CweNumber.SQL_INJECTION, result.get(2).get(0).getCWE());
57+
assertEquals(CweNumber.COMMAND_INJECTION, result.get("1").get(0).getCWE());
58+
assertEquals(CweNumber.SQL_INJECTION, result.get("2").get(0).getCWE());
5959
}
6060
}

plugin/src/test/java/org/owasp/benchmarkutils/score/parsers/CheckmarxIASTReaderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void readerHandlesGivenResultFile() throws Exception {
5454

5555
assertEquals(2, result.getTotalResults());
5656

57-
assertEquals(CweNumber.SQL_INJECTION, result.get(1).get(0).getCWE());
58-
assertEquals(CweNumber.PATH_TRAVERSAL, result.get(2).get(0).getCWE());
57+
assertEquals(CweNumber.SQL_INJECTION, result.get("1").get(0).getCWE());
58+
assertEquals(CweNumber.PATH_TRAVERSAL, result.get("2").get(0).getCWE());
5959
}
6060
}

plugin/src/test/java/org/owasp/benchmarkutils/score/parsers/CheckmarxReaderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void readerHandlesGivenResultFile() throws Exception {
5454

5555
assertEquals(2, result.getTotalResults());
5656

57-
assertEquals(CweNumber.XSS, result.get(1).get(0).getCWE());
58-
assertEquals(CweNumber.SQL_INJECTION, result.get(2).get(0).getCWE());
57+
assertEquals(CweNumber.XSS, result.get("1").get(0).getCWE());
58+
assertEquals(CweNumber.SQL_INJECTION, result.get("2").get(0).getCWE());
5959
}
6060
}

plugin/src/test/java/org/owasp/benchmarkutils/score/parsers/CoverityReaderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void readerHandlesGivenResultFile() throws Exception {
5454

5555
assertEquals(2, result.getTotalResults());
5656

57-
assertEquals(CweNumber.PATH_TRAVERSAL, result.get(1).get(0).getCWE());
58-
assertEquals(CweNumber.SQL_INJECTION, result.get(2).get(0).getCWE());
57+
assertEquals(CweNumber.PATH_TRAVERSAL, result.get("1").get(0).getCWE());
58+
assertEquals(CweNumber.SQL_INJECTION, result.get("2").get(0).getCWE());
5959
}
6060
}

plugin/src/test/java/org/owasp/benchmarkutils/score/parsers/DatadogReaderTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ void readerHandlesGivenResultFile() throws Exception {
5555

5656
assertEquals(4, result.getTotalResults());
5757

58-
assertEquals(CweNumber.COMMAND_INJECTION, result.get(1609).get(0).getCWE());
59-
assertEquals(CweNumber.PATH_TRAVERSAL, result.get(2).get(0).getCWE());
60-
assertEquals(CweNumber.WEAK_HASH_ALGO, result.get(1).get(0).getCWE());
61-
assertEquals(CweNumber.TRUST_BOUNDARY_VIOLATION, result.get(4).get(0).getCWE());
58+
assertEquals(CweNumber.COMMAND_INJECTION, result.get("1609").get(0).getCWE());
59+
assertEquals(CweNumber.PATH_TRAVERSAL, result.get("2").get(0).getCWE());
60+
assertEquals(CweNumber.WEAK_HASH_ALGO, result.get("1").get(0).getCWE());
61+
assertEquals(CweNumber.TRUST_BOUNDARY_VIOLATION, result.get("4").get(0).getCWE());
6262
}
6363
}

0 commit comments

Comments
 (0)