AutoconfAnalyzer: Some code refactoring to eliminate duplicate code

and redundant condition checking, plus fixes/additions to Javadoc.

Former-commit-id: 1c18377b6d871f354915ca210df6ee22534553ba
This commit is contained in:
Dale Visser
2015-06-02 16:14:13 -04:00
parent cc3a72f4fd
commit 7d3ac21e42
2 changed files with 46 additions and 33 deletions

View File

@@ -35,14 +35,20 @@ import org.owasp.dependencycheck.utils.Settings;
import org.owasp.dependencycheck.utils.UrlStringUtils; import org.owasp.dependencycheck.utils.UrlStringUtils;
/** /**
* Used to analyze a Wheel or egg distribution files, or their contents in * Used to analyze Autoconf input files named configure.ac or configure.in.
* unzipped form, and collect information that can be used to determine the * Files simply named "configure" are also analyzed, assuming they are generated
* associated CPE. * by Autoconf, and contain certain special package descriptor variables.
* *
* @author Dale Visser <dvisser@ida.org> * @author Dale Visser <dvisser@ida.org>
* @see <a href="https://www.gnu.org/software/autoconf/">Autoconf - GNU Project - Free Software Foundation (FSF)</a>
*/ */
public class AutoconfAnalyzer extends AbstractFileTypeAnalyzer { public class AutoconfAnalyzer extends AbstractFileTypeAnalyzer {
/**
* Autoconf output filename.
*/
private static final String CONFIGURE = "configure";
/** /**
* Autoconf input filename. * Autoconf input filename.
*/ */
@@ -67,7 +73,7 @@ public class AutoconfAnalyzer extends AbstractFileTypeAnalyzer {
* The set of file extensions supported by this analyzer. * The set of file extensions supported by this analyzer.
*/ */
private static final Set<String> EXTENSIONS = newHashSet("ac", "in", private static final Set<String> EXTENSIONS = newHashSet("ac", "in",
"configure"); CONFIGURE);
/** /**
* Matches AC_INIT variables in the output configure script. * Matches AC_INIT variables in the output configure script.
@@ -142,21 +148,22 @@ public class AutoconfAnalyzer extends AbstractFileTypeAnalyzer {
throws AnalysisException { throws AnalysisException {
final File actualFile = dependency.getActualFile(); final File actualFile = dependency.getActualFile();
final String name = actualFile.getName(); final String name = actualFile.getName();
if (CONFIGURE_AC.equals(name) || CONFIGURE_IN.equals(name)) { if (name.startsWith(CONFIGURE)) {
final File parent = actualFile.getParentFile(); final File parent = actualFile.getParentFile();
final String parentName = parent.getName(); final String parentName = parent.getName();
dependency.setDisplayFileName(parentName + "/" + name); dependency.setDisplayFileName(parentName + "/" + name);
final String contents = getFileContents(actualFile); final boolean isOutputScript = CONFIGURE.equals(name);
if (!contents.isEmpty()) { if (isOutputScript || CONFIGURE_AC.equals(name)
gatherEvidence(dependency, name, contents); || CONFIGURE_IN.equals(name)) {
} final String contents = getFileContents(actualFile);
} else if ("configure".equals(name)) { if (!contents.isEmpty()) {
final File parent = actualFile.getParentFile(); if (isOutputScript) {
final String parentName = parent.getName(); extractConfigureScriptEvidence(dependency, name,
dependency.setDisplayFileName(parentName + "/" + name); contents);
final String contents = getFileContents(actualFile); } else {
if (!contents.isEmpty()) { gatherEvidence(dependency, name, contents);
extractConfigureScriptEvidence(dependency, name, contents); }
}
} }
} else { } else {
// copy, alter and set in case some other thread is iterating over // copy, alter and set in case some other thread is iterating over

View File

@@ -32,9 +32,15 @@ import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
import org.owasp.dependencycheck.dependency.Dependency; import org.owasp.dependencycheck.dependency.Dependency;
/** /**
* Unit tests for PythonDistributionAnalyzer. * Unit tests for AutoconfAnalyzer. The test resources under autoconf/ were
* obtained from outside open source software projects. Links to those projects
* are given below.
* *
* @author Dale Visser <dvisser@ida.org> * @author Dale Visser <dvisser@ida.org>
* @see <a href="http://readable.sourceforge.net/">Readable Lisp S-expressions
* Project</a>
* @see <a href="https://gnu.org/software/binutils/">GNU Binutils</a>
* @see <a href="https://gnu.org/software/ghostscript/">GNU Ghostscript</a>
*/ */
public class AutoconfAnalyzerTest extends BaseTest { public class AutoconfAnalyzerTest extends BaseTest {
@@ -43,19 +49,19 @@ public class AutoconfAnalyzerTest extends BaseTest {
*/ */
AutoconfAnalyzer analyzer; AutoconfAnalyzer analyzer;
private void assertCommonEvidence(Dependency result, String product, String version, private void assertCommonEvidence(Dependency result, String product,
String vendor) { String version, String vendor) {
assertProductAndVersion(result, product, version); assertProductAndVersion(result, product, version);
assertTrue("Expected vendor evidence to contain \"" + vendor + "\".", result assertTrue("Expected vendor evidence to contain \"" + vendor + "\".",
.getVendorEvidence().toString().contains(vendor)); result.getVendorEvidence().toString().contains(vendor));
} }
private void assertProductAndVersion(Dependency result, String product, private void assertProductAndVersion(Dependency result, String product,
String version) { String version) {
assertTrue("Expected product evidence to contain \"" + product + "\".", assertTrue("Expected product evidence to contain \"" + product + "\".",
result.getProductEvidence().toString().contains(product)); result.getProductEvidence().toString().contains(product));
assertTrue("Expected version evidence to contain \"" + version + "\".", result assertTrue("Expected version evidence to contain \"" + version + "\".",
.getVersionEvidence().toString().contains(version)); result.getVersionEvidence().toString().contains(version));
} }
/** /**
@@ -84,7 +90,8 @@ public class AutoconfAnalyzerTest extends BaseTest {
} }
/** /**
* Test of inspect method, of class PythonDistributionAnalyzer. * Test whether expected evidence is gathered from Ghostscript's
* configure.ac.
* *
* @throws AnalysisException * @throws AnalysisException
* is thrown when an exception occurs. * is thrown when an exception occurs.
@@ -98,7 +105,7 @@ public class AutoconfAnalyzerTest extends BaseTest {
} }
/** /**
* Test of inspect method, of class PythonDistributionAnalyzer. * Test whether expected evidence is gathered from Readable's configure.ac.
* *
* @throws AnalysisException * @throws AnalysisException
* is thrown when an exception occurs. * is thrown when an exception occurs.
@@ -119,8 +126,8 @@ public class AutoconfAnalyzerTest extends BaseTest {
} }
/** /**
* Test of inspect method, of class PythonDistributionAnalyzer. * Test whether expected evidence is gathered from GNU Binutil's configure.
* *
* @throws AnalysisException * @throws AnalysisException
* is thrown when an exception occurs. * is thrown when an exception occurs.
*/ */
@@ -133,7 +140,8 @@ public class AutoconfAnalyzerTest extends BaseTest {
} }
/** /**
* Test of inspect method, of class PythonDistributionAnalyzer. * Test whether expected evidence is gathered from GNU Ghostscript's
* configure.
* *
* @throws AnalysisException * @throws AnalysisException
* is thrown when an exception occurs. * is thrown when an exception occurs.
@@ -147,7 +155,7 @@ public class AutoconfAnalyzerTest extends BaseTest {
} }
/** /**
* Test of getName method, of class PythonDistributionAnalyzer. * Test of getName method, of {@link AutoconfAnalyzer}.
*/ */
@Test @Test
public void testGetName() { public void testGetName() {
@@ -156,8 +164,7 @@ public class AutoconfAnalyzerTest extends BaseTest {
} }
/** /**
* Test of getSupportedExtensions method, of class * Test of {@link AutoconfAnalyzer#getSupportedExtensions}.
* PythonDistributionAnalyzer.
*/ */
@Test @Test
public void testGetSupportedExtensions() { public void testGetSupportedExtensions() {
@@ -169,7 +176,7 @@ public class AutoconfAnalyzerTest extends BaseTest {
} }
/** /**
* Test of supportsExtension method, of class PythonDistributionAnalyzer. * Test of {@link AutoconfAnalyzer#supportsExtension}.
*/ */
@Test @Test
public void testSupportsExtension() { public void testSupportsExtension() {
@@ -180,5 +187,4 @@ public class AutoconfAnalyzerTest extends BaseTest {
assertTrue("Should support \"configure\" extension.", assertTrue("Should support \"configure\" extension.",
analyzer.supportsExtension("configure")); analyzer.supportsExtension("configure"));
} }
} }