-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Extract overall stats table to seperate class * cleanup; disclaimer blocks * tests for OverallStatsTable * disclaimers * test cleanup
- Loading branch information
1 parent
faf3915
commit 3a7ea1a
Showing
13 changed files
with
980 additions
and
78 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
26 changes: 26 additions & 0 deletions
26
plugin/src/main/java/org/owasp/benchmarkutils/score/report/Formats.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 @@ | ||
/** | ||
* OWASP Benchmark Project | ||
* | ||
* <p>This file is part of the Open Web Application Security Project (OWASP) Benchmark Project For | ||
* details, please see <a | ||
* href="https://owasp.org/www-project-benchmark/">https://owasp.org/www-project-benchmark/</a>. | ||
* | ||
* <p>The OWASP Benchmark is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU General Public License as published by the Free Software Foundation, version 2. | ||
* | ||
* <p>The OWASP Benchmark is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | ||
* PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* @author Sascha Knoop | ||
* @created 2024 | ||
*/ | ||
package org.owasp.benchmarkutils.score.report; | ||
|
||
import java.text.DecimalFormat; | ||
|
||
public class Formats { | ||
|
||
public static final DecimalFormat twoDecimalPlacesPercentage = new DecimalFormat("#0.00%"); | ||
public static final DecimalFormat fourDecimalPlacesNumber = new DecimalFormat("#0.0000"); | ||
} |
110 changes: 110 additions & 0 deletions
110
plugin/src/main/java/org/owasp/benchmarkutils/score/report/html/HtmlStringBuilder.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,110 @@ | ||
/** | ||
* OWASP Benchmark Project | ||
* | ||
* <p>This file is part of the Open Web Application Security Project (OWASP) Benchmark Project For | ||
* details, please see <a | ||
* href="https://owasp.org/www-project-benchmark/">https://owasp.org/www-project-benchmark/</a>. | ||
* | ||
* <p>The OWASP Benchmark is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU General Public License as published by the Free Software Foundation, version 2. | ||
* | ||
* <p>The OWASP Benchmark is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | ||
* PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* @author Sascha Knoop | ||
* @created 2024 | ||
*/ | ||
package org.owasp.benchmarkutils.score.report.html; | ||
|
||
public class HtmlStringBuilder { | ||
|
||
private final StringBuilder sb = new StringBuilder(); | ||
|
||
public HtmlStringBuilder beginTable() { | ||
sb.append("<table>"); | ||
|
||
return this; | ||
} | ||
|
||
public HtmlStringBuilder beginTable(String cssClass) { | ||
if (cssClass == null) { | ||
return beginTable(); | ||
} | ||
|
||
sb.append("<table class=\"").append(cssClass).append("\">"); | ||
|
||
return this; | ||
} | ||
|
||
public HtmlStringBuilder beginTr() { | ||
sb.append("<tr>"); | ||
|
||
return this; | ||
} | ||
|
||
public HtmlStringBuilder beginTr(String cssClass) { | ||
if (cssClass == null) { | ||
return beginTr(); | ||
} | ||
|
||
sb.append("<tr class=\"").append(cssClass).append("\">"); | ||
|
||
return this; | ||
} | ||
|
||
public HtmlStringBuilder th(String content) { | ||
sb.append("<th>").append(content).append("</th>"); | ||
|
||
return this; | ||
} | ||
|
||
public HtmlStringBuilder th(String content, String cssClass) { | ||
if (cssClass == null) { | ||
return th(content); | ||
} | ||
|
||
sb.append("<th class=\"").append(cssClass).append("\">").append(content).append("</th>"); | ||
|
||
return this; | ||
} | ||
|
||
public HtmlStringBuilder endTr() { | ||
sb.append("</tr>"); | ||
|
||
return this; | ||
} | ||
|
||
public HtmlStringBuilder td(String content) { | ||
sb.append("<td>").append(content).append("</td>"); | ||
|
||
return this; | ||
} | ||
|
||
public HtmlStringBuilder td(String content, String cssClass) { | ||
if (cssClass == null) { | ||
return td(content); | ||
} | ||
|
||
sb.append("<td class=\"").append(cssClass).append("\">").append(content).append("</td>"); | ||
|
||
return this; | ||
} | ||
|
||
public HtmlStringBuilder endTable() { | ||
sb.append("</table>"); | ||
|
||
return this; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return sb.toString(); | ||
} | ||
|
||
public HtmlStringBuilder p(String content) { | ||
sb.append("<p>").append(content).append("</p>"); | ||
|
||
return this; | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
plugin/src/main/java/org/owasp/benchmarkutils/score/report/html/OverallStatsTable.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,129 @@ | ||
/** | ||
* OWASP Benchmark Project | ||
* | ||
* <p>This file is part of the Open Web Application Security Project (OWASP) Benchmark Project For | ||
* details, please see <a | ||
* href="https://owasp.org/www-project-benchmark/">https://owasp.org/www-project-benchmark/</a>. | ||
* | ||
* <p>The OWASP Benchmark is free software: you can redistribute it and/or modify it under the terms | ||
* of the GNU General Public License as published by the Free Software Foundation, version 2. | ||
* | ||
* <p>The OWASP Benchmark is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | ||
* PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* @author Sascha Knoop | ||
* @created 2024 | ||
*/ | ||
package org.owasp.benchmarkutils.score.report.html; | ||
|
||
import static org.owasp.benchmarkutils.score.report.Formats.fourDecimalPlacesNumber; | ||
import static org.owasp.benchmarkutils.score.report.Formats.twoDecimalPlacesPercentage; | ||
|
||
import java.util.Set; | ||
import org.owasp.benchmarkutils.score.Configuration; | ||
import org.owasp.benchmarkutils.score.Tool; | ||
import org.owasp.benchmarkutils.score.ToolResults; | ||
|
||
public class OverallStatsTable { | ||
|
||
private final Configuration config; | ||
private final String testSuite; | ||
|
||
public OverallStatsTable(Configuration config, String testSuite) { | ||
this.config = config; | ||
this.testSuite = testSuite; | ||
} | ||
|
||
/** | ||
* Generate the overall stats table across all the tools for the bottom of the home page. | ||
* | ||
* @param tools - The set of all tools being scored. Each Tool includes it's scored results. | ||
* @return The HTML of the overall stats table. | ||
*/ | ||
public String generateFor(Set<Tool> tools) { | ||
HtmlStringBuilder htmlBuilder = new HtmlStringBuilder(); | ||
|
||
htmlBuilder.beginTable("table"); | ||
|
||
addHeaderTo(htmlBuilder); | ||
|
||
tools.stream() | ||
.filter(tool -> !(config.showAveOnlyMode && tool.isCommercial())) | ||
.forEach(tool -> appendRowTo(htmlBuilder, tool)); | ||
|
||
htmlBuilder.endTable(); | ||
|
||
htmlBuilder.p( | ||
"*-Please refer to each tool's scorecard for the data used to calculate these values."); | ||
|
||
return htmlBuilder.toString(); | ||
} | ||
|
||
private void addHeaderTo(HtmlStringBuilder htmlBuilder) { | ||
htmlBuilder.beginTr(); | ||
htmlBuilder.th("Tool"); | ||
|
||
if (config.mixedMode) { | ||
htmlBuilder.th(testSuite + " Version"); | ||
} | ||
|
||
htmlBuilder.th("Type"); | ||
|
||
if (config.includePrecision) { | ||
htmlBuilder.th("Precision*"); | ||
htmlBuilder.th("F-score*"); | ||
} | ||
|
||
htmlBuilder.th("${tprlabel}*"); | ||
htmlBuilder.th("FPR*"); | ||
htmlBuilder.th("Score*"); | ||
|
||
htmlBuilder.endTr(); | ||
} | ||
|
||
private void appendRowTo(HtmlStringBuilder htmlBuilder, Tool tool) { | ||
ToolResults results = tool.getOverallResults(); | ||
|
||
htmlBuilder.beginTr(cssClassFor(results)); | ||
htmlBuilder.td(tool.getToolNameAndVersion()); | ||
|
||
if (config.mixedMode) { | ||
htmlBuilder.td(tool.getTestSuiteVersion()); | ||
} | ||
|
||
htmlBuilder.td(tool.getToolType().name()); | ||
|
||
if (config.includePrecision) { | ||
htmlBuilder | ||
.td(twoDecimalPlacesPercentage.format(results.getPrecision())) | ||
.td(fourDecimalPlacesNumber.format(results.getFScore())); | ||
} | ||
|
||
htmlBuilder | ||
.td(twoDecimalPlacesPercentage.format(results.getTruePositiveRate())) | ||
.td(twoDecimalPlacesPercentage.format(results.getFalsePositiveRate())) | ||
.td(twoDecimalPlacesPercentage.format(results.getOverallScore())) | ||
.endTr(); | ||
} | ||
|
||
private String cssClassFor(ToolResults results) { | ||
String cssClass = null; | ||
|
||
if (isDanger(results)) { | ||
cssClass = "danger"; | ||
} else if (isSuccess(results)) { | ||
cssClass = "success"; | ||
} | ||
|
||
return cssClass; | ||
} | ||
|
||
private boolean isSuccess(ToolResults results) { | ||
return results.getTruePositiveRate() > .7 && results.getFalsePositiveRate() < .3; | ||
} | ||
|
||
private boolean isDanger(ToolResults results) { | ||
return Math.abs(results.getTruePositiveRate() - results.getFalsePositiveRate()) < .1; | ||
} | ||
} |
Oops, something went wrong.