OpenSSL: Updates to use FileFilter instead of file extension.

Former-commit-id: 974bc28171b5b3ac3e772c93447489e1afb797f1
This commit is contained in:
Dale Visser
2015-07-09 16:35:04 -04:00
parent e5fdda60fc
commit 433c2e5916
2 changed files with 18 additions and 42 deletions

View File

@@ -18,18 +18,16 @@
package org.owasp.dependencycheck.analyzer; package org.owasp.dependencycheck.analyzer;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.NameFileFilter;
import org.owasp.dependencycheck.Engine; import org.owasp.dependencycheck.Engine;
import org.owasp.dependencycheck.analyzer.exception.AnalysisException; import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
import org.owasp.dependencycheck.dependency.Confidence; import org.owasp.dependencycheck.dependency.Confidence;
import org.owasp.dependencycheck.dependency.Dependency; import org.owasp.dependencycheck.dependency.Dependency;
import org.owasp.dependencycheck.utils.FileFilterBuilder;
import org.owasp.dependencycheck.utils.Settings; import org.owasp.dependencycheck.utils.Settings;
import java.io.File; import java.io.File;
import java.io.FileFilter; import java.io.FileFilter;
import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import java.util.Set;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@@ -46,15 +44,10 @@ public class OpenSSLAnalyzer extends AbstractFileTypeAnalyzer {
*/ */
private static final String OPENSSLV_H = "opensslv.h"; private static final String OPENSSLV_H = "opensslv.h";
/**
* Filename extensions for files to be analyzed.
*/
private static final Set<String> EXTENSIONS = Collections
.unmodifiableSet(Collections.singleton("h"));
/** /**
* Filter that detects files named "__init__.py". * Filter that detects files named "__init__.py".
*/ */
private static final FileFilter OPENSSLV_FILTER = new NameFileFilter(OPENSSLV_H); private static final FileFilter OPENSSLV_FILTER = FileFilterBuilder.newInstance().addFilenames(OPENSSLV_H).build();
private static final Pattern VERSION_PATTERN = Pattern.compile( private static final Pattern VERSION_PATTERN = Pattern.compile(
"define\\s+OPENSSL_VERSION_NUMBER\\s+0x([0-9a-zA-Z]{8})L", Pattern.DOTALL "define\\s+OPENSSL_VERSION_NUMBER\\s+0x([0-9a-zA-Z]{8})L", Pattern.DOTALL
| Pattern.CASE_INSENSITIVE); | Pattern.CASE_INSENSITIVE);
@@ -107,8 +100,8 @@ public class OpenSSLAnalyzer extends AbstractFileTypeAnalyzer {
* @return the set of supported file extensions * @return the set of supported file extensions
*/ */
@Override @Override
protected Set<String> getSupportedExtensions() { protected FileFilter getFileFilter() {
return EXTENSIONS; return OPENSSLV_FILTER;
} }
/** /**
@@ -132,20 +125,17 @@ public class OpenSSLAnalyzer extends AbstractFileTypeAnalyzer {
protected void analyzeFileType(Dependency dependency, Engine engine) protected void analyzeFileType(Dependency dependency, Engine engine)
throws AnalysisException { throws AnalysisException {
final File file = dependency.getActualFile(); final File file = dependency.getActualFile();
final File parent = file.getParentFile(); final String parentName = file.getParentFile().getName();
final String parentName = parent.getName();
boolean found = false; boolean found = false;
if (OPENSSLV_FILTER.accept(file)) {
final String contents = getFileContents(file); final String contents = getFileContents(file);
if (!contents.isEmpty()) { if (!contents.isEmpty()) {
final Matcher matcher = VERSION_PATTERN.matcher(contents); final Matcher matcher = VERSION_PATTERN.matcher(contents);
while (matcher.find()) { if (matcher.find()) {
dependency.getVersionEvidence().addEvidence(OPENSSLV_H, "Version Constant", dependency.getVersionEvidence().addEvidence(OPENSSLV_H, "Version Constant",
getOpenSSLVersion(Long.parseLong(matcher.group(1), HEXADECIMAL)), Confidence.HIGH); getOpenSSLVersion(Long.parseLong(matcher.group(1), HEXADECIMAL)), Confidence.HIGH);
found = true; found = true;
} }
} }
}
if (found) { if (found) {
dependency.setDisplayFileName(parentName + File.separatorChar + OPENSSLV_H); dependency.setDisplayFileName(parentName + File.separatorChar + OPENSSLV_H);
dependency.getVendorEvidence().addEvidence(OPENSSLV_H, "Vendor", "OpenSSL", Confidence.HIGHEST); dependency.getVendorEvidence().addEvidence(OPENSSLV_H, "Vendor", "OpenSSL", Confidence.HIGHEST);
@@ -164,7 +154,7 @@ public class OpenSSLAnalyzer extends AbstractFileTypeAnalyzer {
*/ */
private String getFileContents(final File actualFile) private String getFileContents(final File actualFile)
throws AnalysisException { throws AnalysisException {
String contents = ""; String contents;
try { try {
contents = FileUtils.readFileToString(actualFile).trim(); contents = FileUtils.readFileToString(actualFile).trim();
} catch (IOException e) { } catch (IOException e) {

View File

@@ -17,7 +17,6 @@
*/ */
package org.owasp.dependencycheck.analyzer; package org.owasp.dependencycheck.analyzer;
import org.apache.commons.lang.StringUtils;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -25,11 +24,10 @@ import org.owasp.dependencycheck.BaseTest;
import org.owasp.dependencycheck.analyzer.exception.AnalysisException; import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
import org.owasp.dependencycheck.dependency.Dependency; import org.owasp.dependencycheck.dependency.Dependency;
import java.util.Arrays; import java.io.File;
import java.util.HashSet;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
/** /**
* Unit tests for OpenSSLAnalyzerAnalyzer. * Unit tests for OpenSSLAnalyzerAnalyzer.
@@ -75,25 +73,13 @@ public class OpenSSLAnalyzerTest extends BaseTest {
analyzer.getName()); analyzer.getName());
} }
/**
* Test of getSupportedExtensions method, of class OpenSSLAnalyzer.
*/
@Test
public void testGetSupportedExtensions() {
final String[] expected = {"h"};
assertEquals("Supported extensions should just have the following: "
+ StringUtils.join(expected, ", "),
new HashSet<String>(Arrays.asList(expected)),
analyzer.getSupportedExtensions());
}
/** /**
* Test of supportsExtension method, of class PythonPackageAnalyzer. * Test of supportsExtension method, of class PythonPackageAnalyzer.
*/ */
@Test @Test
public void testSupportsExtension() { public void testAccept() {
assertTrue("Should support \"h\" extension.", assertTrue("Should support files named \"opensslv.h\".",
analyzer.supportsExtension("h")); analyzer.accept(new File("opensslv.h")));
} }
@Test @Test