Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapper for test suite name #79

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.owasp.benchmarkutils.helpers.Categories;
import org.owasp.benchmarkutils.helpers.Category;
import org.owasp.benchmarkutils.helpers.Utils;
import org.owasp.benchmarkutils.score.domain.TestSuiteName;
import org.owasp.benchmarkutils.score.parsers.Reader;
import org.owasp.benchmarkutils.score.report.ScatterHome;
import org.owasp.benchmarkutils.score.report.ScatterInterpretation;
Expand All @@ -75,7 +76,7 @@ public class BenchmarkScore extends AbstractMojo {
// Prefixes for generated test suites and file names. Used by lots of other classes for
// scorecard generation.
public static String TESTSUITEVERSION; // Pulled from expected results file
public static String TESTSUITE; // Pulled from expected results file
public static TestSuiteName TESTSUITENAME; // Pulled from expected results file
public static final String TEST = "Test";
public static String TESTCASENAME; // Set w/TESTSUITE. i.e., TESTSUITE + TEST;

Expand Down Expand Up @@ -480,12 +481,7 @@ public static void main(String[] args) {
// Step 7: Generate the tool scorecards now that the overall Vulnerability scorecards and
// stats have been calculated
ToolScorecard toolScorecard =
new ToolScorecard(
overallAveToolResults,
scoreCardDir,
config,
TESTSUITE,
fullTestSuiteName(TESTSUITE));
new ToolScorecard(overallAveToolResults, scoreCardDir, config, TESTSUITENAME);

tools.forEach(toolScorecard::generate);

Expand All @@ -498,7 +494,7 @@ public static void main(String[] args) {

// Step 10: Generate the results table across all the tools in this test
try {
OverallStatsTable overallStatsTable = new OverallStatsTable(config, TESTSUITE);
OverallStatsTable overallStatsTable = new OverallStatsTable(config, TESTSUITENAME);

String html =
new String(Files.readAllBytes(homeFilePath))
Expand Down Expand Up @@ -527,7 +523,7 @@ public static void main(String[] args) {
e.printStackTrace();
}

System.out.println(BenchmarkScore.TESTSUITE + " scorecards complete.");
System.out.println(BenchmarkScore.TESTSUITENAME.simpleName() + " scorecards complete.");

System.exit(0);
}
Expand Down Expand Up @@ -881,7 +877,7 @@ private static TestSuiteResults readExpectedResults(File file) {
try {
TestSuiteResults tr = ExpectedResultsProvider.parse(new ResultFile(file));

BenchmarkScore.TESTSUITE = tr.getTestSuiteName();
BenchmarkScore.TESTSUITENAME = new TestSuiteName(tr.getTestSuiteName());
BenchmarkScore.TESTCASENAME = tr.getTestSuiteName() + BenchmarkScore.TEST;

return tr;
Expand Down Expand Up @@ -910,7 +906,7 @@ private static String produceResultsFile(TestSuiteResults actual, File scoreCard
String resultsFileName =
scoreCardDir.getAbsolutePath()
+ File.separator
+ TESTSUITE
+ TESTSUITENAME.simpleName()
+ "_v"
+ testSuiteVersion
+ "_Scorecard_for_"
Expand All @@ -930,7 +926,7 @@ private static String produceResultsFile(TestSuiteResults actual, File scoreCard
if (fulldetails) ps.print("source, data flow, sink, ");
ps.print(
"real vulnerability, identified by tool, pass/fail, "
+ TESTSUITE
+ TESTSUITENAME.simpleName()
+ " version: "
+ testSuiteVersion);

Expand Down Expand Up @@ -990,7 +986,7 @@ private static void generateVulnerabilityScorecards(
final ClassLoader CL = BenchmarkScore.class.getClassLoader();

VulnerabilityStatsTable vulnerabilityStatsTable =
new VulnerabilityStatsTable(config, TESTSUITE, tools);
new VulnerabilityStatsTable(config, TESTSUITENAME, tools);

for (String cat : catSet) {
try {
Expand All @@ -1011,7 +1007,7 @@ private static void generateVulnerabilityScorecards(
BenchmarkScore.overallAveToolResults.put(cat, scatter.getOverallCategoryResults());

String filename =
TESTSUITE
TESTSUITENAME.simpleName()
+ "_v"
+ TESTSUITEVERSION
+ "_Scorecard_for_"
Expand All @@ -1028,14 +1024,9 @@ private static void generateVulnerabilityScorecards(
}

String html = IOUtils.toString(vulnTemplateStream, StandardCharsets.UTF_8);
html =
html.replace(
"${testsuite}",
BenchmarkScore.fullTestSuiteName(BenchmarkScore.TESTSUITE));
html = html.replace("${testsuite}", BenchmarkScore.TESTSUITENAME.fullName());
String fullTitle =
BenchmarkScore.fullTestSuiteName(BenchmarkScore.TESTSUITE)
+ " Scorecard for "
+ cat;
BenchmarkScore.TESTSUITENAME.fullName() + " Scorecard for " + cat;

html = html.replace("${image}", filename + ".png");
html = html.replace("${title}", fullTitle);
Expand Down Expand Up @@ -1069,8 +1060,10 @@ private static void generateVulnerabilityScorecards(
if (commercialAveragesTable.hasEntries()) {
try {
commercialAveScorecardFilename =
TESTSUITE + "_v" + TESTSUITEVERSION + "_Scorecard_for_Commercial_Tools";

TESTSUITENAME.simpleName()
+ "_v"
+ TESTSUITEVERSION
+ "_Scorecard_for_Commercial_Tools";
Path htmlfile =
Paths.get(
scoreCardDir.getAbsolutePath()
Expand All @@ -1081,10 +1074,7 @@ private static void generateVulnerabilityScorecards(
InputStream vulnTemplateStream =
CL.getResourceAsStream(scoreCardDir + "/commercialAveTemplate.html");
String html = IOUtils.toString(vulnTemplateStream, StandardCharsets.UTF_8);
html =
html.replace(
"${testsuite}",
BenchmarkScore.fullTestSuiteName(BenchmarkScore.TESTSUITE));
html = html.replace("${testsuite}", BenchmarkScore.TESTSUITENAME.fullName());
html = html.replace("${version}", TESTSUITEVERSION);
html = html.replace("${projectlink}", BenchmarkScore.PROJECTLINKENTRY);

Expand Down Expand Up @@ -1147,7 +1137,11 @@ private static void updateMenus(
sb = new StringBuffer();
for (String cat : catSet) {
String filename =
TESTSUITE + "_v" + TESTSUITEVERSION + "_Scorecard_for_" + cat.replace(' ', '_');
TESTSUITENAME.simpleName()
+ "_v"
+ TESTSUITEVERSION
+ "_Scorecard_for_"
+ cat.replace(' ', '_');
sb.append(" <li><a href=\"");
sb.append(filename);
sb.append(".html\">");
Expand All @@ -1172,10 +1166,7 @@ private static void updateMenuTemplates(String toolmenu, String vulnmenu, File s
String html = new String(Files.readAllBytes(f.toPath()));
html = html.replace("${toolmenu}", toolmenu);
html = html.replace("${vulnmenu}", vulnmenu);
html =
html.replace(
"${testsuite}",
BenchmarkScore.fullTestSuiteName(BenchmarkScore.TESTSUITE));
html = html.replace("${testsuite}", TESTSUITENAME.fullName());
html = html.replace("${version}", TESTSUITEVERSION);
html = html.replace("${projectlink}", BenchmarkScore.PROJECTLINKENTRY);
html = html.replace("${cwecategoryname}", config.cweCategoryName);
Expand All @@ -1193,10 +1184,4 @@ private static void updateMenuTemplates(String toolmenu, String vulnmenu, File s
}
}
}

// A utility method for providing a more descriptive test suite name than the base, single word,
// test suite name.
public static String fullTestSuiteName(String suite) {
return ("Benchmark".equals(suite) ? "OWASP Benchmark" : suite);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* 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.domain;

public class TestSuiteName {

private final String name;

public TestSuiteName(String name) {
this.name = name;
}

public String simpleName() {
return name;
}

/**
* If required, provide a more descriptive test suite name than the base, single word test suite
* name.
*/
public String fullName() {
if ("Benchmark".equals(name)) {
return "OWASP Benchmark";
}

return simpleName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ private void makeLegend(
public static void generateComparisonChart(Set<Tool> tools, String focus, File scoreCardDir) {
try {
String scatterTitle =
BenchmarkScore.fullTestSuiteName(BenchmarkScore.TESTSUITE)
BenchmarkScore.TESTSUITENAME.fullName()
+ (BenchmarkScore.config.mixedMode
? ""
: " v" + BenchmarkScore.TESTSUITEVERSION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ScatterInterpretation extends ScatterPlot {
public ScatterInterpretation(int height) {
display(
" "
+ BenchmarkScore.fullTestSuiteName(BenchmarkScore.TESTSUITE)
+ BenchmarkScore.TESTSUITENAME.fullName()
+ " Results Interpretation Guide",
height);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ public static ScatterVulns generateComparisonChart(
String category, Set<Tool> toolResults, String focus, File scoreCardDir) {
try {
String scatterTitle =
BenchmarkScore.fullTestSuiteName(BenchmarkScore.TESTSUITE)
BenchmarkScore.TESTSUITENAME.fullName()
+ (BenchmarkScore.config.mixedMode
? " -"
: " v" + BenchmarkScore.TESTSUITEVERSION)
Expand All @@ -547,7 +547,7 @@ public static ScatterVulns generateComparisonChart(
scatter.writeChartToFile(
new File(
scoreCardDir,
BenchmarkScore.TESTSUITE
BenchmarkScore.TESTSUITENAME.simpleName()
+ "_v"
+ BenchmarkScore.TESTSUITEVERSION
+ "_Scorecard_for_"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private static void createBarChart(
*/
public static String generateBarChartFileName(Tool tool, BarChartType type) {
String filename =
BenchmarkScore.TESTSUITE
BenchmarkScore.TESTSUITENAME.simpleName()
+ " v"
+ tool.getTestSuiteVersion()
+ " "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ public String generateHtml(Tool currentTool, String title, File scorecardImageFi
.getResourceAsStream(BenchmarkScore.SCORECARDDIRNAME + "/template.html");
String html = IOUtils.toString(templateFileStream, StandardCharsets.UTF_8);

html =
html.replace(
"${testsuite}", BenchmarkScore.fullTestSuiteName(BenchmarkScore.TESTSUITE));
html = html.replace("${testsuite}", BenchmarkScore.TESTSUITENAME.fullName());
html = html.replace("${title}", title);
html = html.replace("${tests}", Integer.toString(overallToolResults.getTotalTestCases()));
html = html.replace("${time}", overallToolResults.getScanTime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
import org.owasp.benchmarkutils.score.Configuration;
import org.owasp.benchmarkutils.score.Tool;
import org.owasp.benchmarkutils.score.ToolResults;
import org.owasp.benchmarkutils.score.domain.TestSuiteName;

public class OverallStatsTable {

private final Configuration config;
private final String testSuite;
private final TestSuiteName testSuiteName;

public OverallStatsTable(Configuration config, String testSuite) {
public OverallStatsTable(Configuration config, TestSuiteName testSuiteName) {
this.config = config;
this.testSuite = testSuite;
this.testSuiteName = testSuiteName;
}

/**
Expand Down Expand Up @@ -65,7 +66,7 @@ private void addHeaderTo(HtmlStringBuilder htmlBuilder) {
htmlBuilder.th("Tool");

if (config.mixedMode) {
htmlBuilder.th(testSuite + " Version");
htmlBuilder.th(testSuiteName.simpleName() + " Version");
}

htmlBuilder.th("Type");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.owasp.benchmarkutils.score.CategoryResults;
import org.owasp.benchmarkutils.score.Configuration;
import org.owasp.benchmarkutils.score.Tool;
import org.owasp.benchmarkutils.score.domain.TestSuiteName;
import org.owasp.benchmarkutils.score.report.ScatterTools;
import org.owasp.benchmarkutils.score.report.ToolBarChart;
import org.owasp.benchmarkutils.score.report.ToolReport;
Expand All @@ -34,8 +35,7 @@ public class ToolScorecard {

private final File scoreCardDir;
private final Configuration config;
private final String testSuite;
private final String fullTestSuiteName;
private final TestSuiteName testSuiteName;

private ToolBarChartProvider toolBarChart;
private ToolReportProvider toolReport;
Expand All @@ -44,12 +44,10 @@ public ToolScorecard(
Map<String, CategoryResults> overallAveToolResults,
File scoreCardDir,
Configuration config,
String testSuite,
String fullTestSuiteName) {
TestSuiteName testSuiteName) {
this.scoreCardDir = scoreCardDir;
this.config = config;
this.testSuite = testSuite;
this.fullTestSuiteName = fullTestSuiteName;
this.testSuiteName = testSuiteName;

this.toolBarChart = new ToolBarChart(overallAveToolResults, scoreCardDir);
this.toolReport = new ToolReport(overallAveToolResults);
Expand Down Expand Up @@ -88,21 +86,24 @@ private String reportPathFor(Tool tool) {
}

private String titleFor(Tool tool) {
String fullTitle = fullTestSuiteName + " Scorecard for " + tool.getToolNameAndVersion();
String fullTitle =
testSuiteName.fullName() + " Scorecard for " + tool.getToolNameAndVersion();

// If not in anonymous mode OR the tool is not commercial, add the type at the end of
// the name. It's not added to anonymous commercial tools, because it would be
// redundant.
if (!config.anonymousMode || !tool.isCommercial()) {
fullTitle += " (" + tool.getToolType() + ")";
}

return fullTitle;
}

private File storedGraphFor(Tool tool) {
String shortTitle =
format(
"{0} v{1} Scorecard for {2}",
testSuite, tool.getTestSuiteVersion(), tool.getToolName());
testSuiteName.simpleName(), tool.getTestSuiteVersion(), tool.getToolName());

File img = new File(scoreCardDir, filenameFor(tool) + ".png");

Expand All @@ -126,7 +127,9 @@ private ScatterTools graph(Tool tool, String shortTitle) {
public String filenameFor(Tool tool) {
return (format(
"{0} v{1} Scorecard for {2}",
testSuite, tool.getTestSuiteVersion(), tool.getToolNameAndVersion()))
testSuiteName.simpleName(),
tool.getTestSuiteVersion(),
tool.getToolNameAndVersion()))
.replace(' ', '_');
}
}
Loading