Random fixes to issues found by IntelliJ IDEA code inspection.

This commit is contained in:
Dale Visser
2015-08-17 18:55:51 -04:00
parent f66ffbdd63
commit 2db1f8d2b6
6 changed files with 44 additions and 64 deletions

View File

@@ -173,10 +173,10 @@ public class AutoconfAnalyzer extends AbstractFileTypeAnalyzer {
} }
} 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
final List<Dependency> deps = new ArrayList<Dependency>( final List<Dependency> dependencies = new ArrayList<Dependency>(
engine.getDependencies()); engine.getDependencies());
deps.remove(dependency); dependencies.remove(dependency);
engine.setDependencies(deps); engine.setDependencies(dependencies);
} }
} }
@@ -225,7 +225,7 @@ public class AutoconfAnalyzer extends AbstractFileTypeAnalyzer {
contents = FileUtils.readFileToString(actualFile).trim(); contents = FileUtils.readFileToString(actualFile).trim();
} catch (IOException e) { } catch (IOException e) {
throw new AnalysisException( throw new AnalysisException(
"Problem occured while reading dependency file.", e); "Problem occurred while reading dependency file.", e);
} }
return contents; return contents;
} }

View File

@@ -53,7 +53,7 @@ import org.owasp.dependencycheck.utils.UrlStringUtils;
public class PythonDistributionAnalyzer extends AbstractFileTypeAnalyzer { public class PythonDistributionAnalyzer extends AbstractFileTypeAnalyzer {
/** /**
* Name of egg metatdata files to analyze. * Name of egg metadata files to analyze.
*/ */
private static final String PKG_INFO = "PKG-INFO"; private static final String PKG_INFO = "PKG-INFO";
@@ -269,10 +269,8 @@ public class PythonDistributionAnalyzer extends AbstractFileTypeAnalyzer {
* *
* @param dependency the dependency being analyzed * @param dependency the dependency being analyzed
* @param file a reference to the manifest/properties file * @param file a reference to the manifest/properties file
* @throws AnalysisException thrown when there is an error
*/ */
private static void collectWheelMetadata(Dependency dependency, File file) private static void collectWheelMetadata(Dependency dependency, File file) {
throws AnalysisException {
final InternetHeaders headers = getManifestProperties(file); final InternetHeaders headers = getManifestProperties(file);
addPropertyToEvidence(headers, dependency.getVersionEvidence(), addPropertyToEvidence(headers, dependency.getVersionEvidence(),
"Version", Confidence.HIGHEST); "Version", Confidence.HIGHEST);
@@ -352,7 +350,7 @@ public class PythonDistributionAnalyzer extends AbstractFileTypeAnalyzer {
} }
/** /**
* Retrieves the next temporary destingation directory for extracting an archive. * Retrieves the next temporary destination directory for extracting an archive.
* *
* @return a directory * @return a directory
* @throws AnalysisException thrown if unable to create temporary directory * @throws AnalysisException thrown if unable to create temporary directory

View File

@@ -28,13 +28,10 @@ import org.owasp.dependencycheck.dependency.EvidenceCollection;
import org.owasp.dependencycheck.utils.FileFilterBuilder; import org.owasp.dependencycheck.utils.FileFilterBuilder;
import org.owasp.dependencycheck.utils.Settings; import org.owasp.dependencycheck.utils.Settings;
import org.owasp.dependencycheck.utils.UrlStringUtils; import org.owasp.dependencycheck.utils.UrlStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.net.MalformedURLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@@ -53,12 +50,6 @@ public class PythonPackageAnalyzer extends AbstractFileTypeAnalyzer {
private static final int REGEX_OPTIONS = Pattern.DOTALL private static final int REGEX_OPTIONS = Pattern.DOTALL
| Pattern.CASE_INSENSITIVE; | Pattern.CASE_INSENSITIVE;
/**
* The logger.
*/
private static final Logger LOGGER = LoggerFactory
.getLogger(PythonPackageAnalyzer.class);
/** /**
* Filename extensions for files to be analyzed. * Filename extensions for files to be analyzed.
*/ */
@@ -173,7 +164,7 @@ public class PythonPackageAnalyzer extends AbstractFileTypeAnalyzer {
* Analyzes python packages and adds evidence to the dependency. * Analyzes python packages and adds evidence to the dependency.
* *
* @param dependency the dependency being analyzed * @param dependency the dependency being analyzed
* @param engine the engine being used to perform the scan * @param engine the engine being used to perform the scan
* @throws AnalysisException thrown if there is an unrecoverable error analyzing the dependency * @throws AnalysisException thrown if there is an unrecoverable error analyzing the dependency
*/ */
@Override @Override
@@ -184,8 +175,8 @@ public class PythonPackageAnalyzer extends AbstractFileTypeAnalyzer {
final String parentName = parent.getName(); final String parentName = parent.getName();
boolean found = false; boolean found = false;
if (INIT_PY_FILTER.accept(file)) { if (INIT_PY_FILTER.accept(file)) {
for (final File sourcefile : parent.listFiles(PY_FILTER)) { for (final File sourceFile : parent.listFiles(PY_FILTER)) {
found |= analyzeFileContents(dependency, sourcefile); found |= analyzeFileContents(dependency, sourceFile);
} }
} }
if (found) { if (found) {
@@ -194,10 +185,10 @@ public class PythonPackageAnalyzer extends AbstractFileTypeAnalyzer {
"PackageName", parentName, Confidence.MEDIUM); "PackageName", parentName, Confidence.MEDIUM);
} 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
final List<Dependency> deps = new ArrayList<Dependency>( final List<Dependency> dependencies = new ArrayList<Dependency>(
engine.getDependencies()); engine.getDependencies());
deps.remove(dependency); dependencies.remove(dependency);
engine.setDependencies(deps); engine.setDependencies(dependencies);
} }
} }
@@ -206,7 +197,7 @@ public class PythonPackageAnalyzer extends AbstractFileTypeAnalyzer {
* __summary__, __uri__, __url__, __home*page__, __author__, and their all caps equivalents. * __summary__, __uri__, __url__, __home*page__, __author__, and their all caps equivalents.
* *
* @param dependency the dependency being analyzed * @param dependency the dependency being analyzed
* @param file the file name to analyze * @param file the file name to analyze
* @return whether evidence was found * @return whether evidence was found
* @throws AnalysisException thrown if there is an unrecoverable error * @throws AnalysisException thrown if there is an unrecoverable error
*/ */
@@ -238,14 +229,10 @@ public class PythonPackageAnalyzer extends AbstractFileTypeAnalyzer {
.getVendorEvidence(); .getVendorEvidence();
found |= gatherEvidence(AUTHOR_PATTERN, contents, source, found |= gatherEvidence(AUTHOR_PATTERN, contents, source,
vendorEvidence, "SourceAuthor", Confidence.MEDIUM); vendorEvidence, "SourceAuthor", Confidence.MEDIUM);
try { found |= gatherHomePageEvidence(URI_PATTERN, vendorEvidence,
found |= gatherHomePageEvidence(URI_PATTERN, vendorEvidence, source, "URL", contents);
source, "URL", contents); found |= gatherHomePageEvidence(HOMEPAGE_PATTERN,
found |= gatherHomePageEvidence(HOMEPAGE_PATTERN, vendorEvidence, source, "HomePage", contents);
vendorEvidence, source, "HomePage", contents);
} catch (MalformedURLException e) {
LOGGER.warn(e.getMessage());
}
} }
return found; return found;
} }
@@ -254,15 +241,15 @@ public class PythonPackageAnalyzer extends AbstractFileTypeAnalyzer {
* Adds summary information to the dependency * Adds summary information to the dependency
* *
* @param dependency the dependency being analyzed * @param dependency the dependency being analyzed
* @param pattern the pattern used to perform analysis * @param pattern the pattern used to perform analysis
* @param group the group from the pattern that indicates the data to use * @param group the group from the pattern that indicates the data to use
* @param contents the data being analyzed * @param contents the data being analyzed
* @param source the source name to use when recording the evidence * @param source the source name to use when recording the evidence
* @param key the key name to use when recording the evidence * @param key the key name to use when recording the evidence
* @return true if evidence was collected; otherwise false * @return true if evidence was collected; otherwise false
*/ */
private boolean addSummaryInfo(Dependency dependency, Pattern pattern, private boolean addSummaryInfo(Dependency dependency, Pattern pattern,
int group, String contents, String source, String key) { int group, String contents, String source, String key) {
final Matcher matcher = pattern.matcher(contents); final Matcher matcher = pattern.matcher(contents);
final boolean found = matcher.find(); final boolean found = matcher.find();
if (found) { if (found) {
@@ -275,17 +262,16 @@ public class PythonPackageAnalyzer extends AbstractFileTypeAnalyzer {
/** /**
* Collects evidence from the home page URL. * Collects evidence from the home page URL.
* *
* @param pattern the pattern to match * @param pattern the pattern to match
* @param evidence the evidence collection to add the evidence to * @param evidence the evidence collection to add the evidence to
* @param source the source of the evidence * @param source the source of the evidence
* @param name the name of the evidence * @param name the name of the evidence
* @param contents the home page URL * @param contents the home page URL
* @return true if evidence was collected; otherwise false * @return true if evidence was collected; otherwise false
* @throws MalformedURLException thrown if the URL is malformed
*/ */
private boolean gatherHomePageEvidence(Pattern pattern, private boolean gatherHomePageEvidence(Pattern pattern,
EvidenceCollection evidence, String source, String name, EvidenceCollection evidence, String source, String name,
String contents) throws MalformedURLException { String contents) {
final Matcher matcher = pattern.matcher(contents); final Matcher matcher = pattern.matcher(contents);
boolean found = false; boolean found = false;
if (matcher.find()) { if (matcher.find()) {
@@ -299,19 +285,19 @@ public class PythonPackageAnalyzer extends AbstractFileTypeAnalyzer {
} }
/** /**
* Gather evidence from a Python source file usin the given string assignment regex pattern. * Gather evidence from a Python source file using the given string assignment regex pattern.
* *
* @param pattern to scan contents with * @param pattern to scan contents with
* @param contents of Python source file * @param contents of Python source file
* @param source for storing evidence * @param source for storing evidence
* @param evidence to store evidence in * @param evidence to store evidence in
* @param name of evidence * @param name of evidence
* @param confidence in evidence * @param confidence in evidence
* @return whether evidence was found * @return whether evidence was found
*/ */
private boolean gatherEvidence(Pattern pattern, String contents, private boolean gatherEvidence(Pattern pattern, String contents,
String source, EvidenceCollection evidence, String name, String source, EvidenceCollection evidence, String name,
Confidence confidence) { Confidence confidence) {
final Matcher matcher = pattern.matcher(contents); final Matcher matcher = pattern.matcher(contents);
final boolean found = matcher.find(); final boolean found = matcher.find();
if (found) { if (found) {

View File

@@ -39,10 +39,10 @@ public class OpenSSLAnalyzerTest extends BaseTest {
/** /**
* The package analyzer to test. * The package analyzer to test.
*/ */
OpenSSLAnalyzer analyzer; private OpenSSLAnalyzer analyzer;
/** /**
* Setup the PtyhonPackageAnalyzer. * Setup the {@link OpenSSLAnalyzer}.
* *
* @throws Exception if there is a problem * @throws Exception if there is a problem
*/ */

View File

@@ -40,7 +40,7 @@ public class PythonDistributionAnalyzerTest extends BaseTest {
/** /**
* The analyzer to test. * The analyzer to test.
*/ */
PythonDistributionAnalyzer analyzer; private PythonDistributionAnalyzer analyzer;
/** /**
* Correctly setup the analyzer for testing. * Correctly setup the analyzer for testing.

View File

@@ -40,10 +40,10 @@ public class PythonPackageAnalyzerTest extends BaseTest {
/** /**
* The package analyzer to test. * The package analyzer to test.
*/ */
PythonPackageAnalyzer analyzer; private PythonPackageAnalyzer analyzer;
/** /**
* Setup the PtyhonPackageAnalyzer. * Setup the {@link PythonPackageAnalyzer}.
* *
* @throws Exception if there is a problem * @throws Exception if there is a problem
*/ */
@@ -85,14 +85,9 @@ public class PythonPackageAnalyzerTest extends BaseTest {
@Test @Test
public void testAnalyzeSourceMetadata() throws AnalysisException { public void testAnalyzeSourceMetadata() throws AnalysisException {
eggtestAssertions(this,
"python/eggtest/__init__.py");
}
public void eggtestAssertions(Object context, final String resource) throws AnalysisException {
boolean found = false; boolean found = false;
final Dependency result = new Dependency(BaseTest.getResourceAsFile( final Dependency result = new Dependency(BaseTest.getResourceAsFile(
context, resource)); this, "python/eggtest/__init__.py"));
analyzer.analyze(result, null); analyzer.analyze(result, null);
assertTrue("Expected vendor evidence to contain \"example\".", result assertTrue("Expected vendor evidence to contain \"example\".", result
.getVendorEvidence().toString().contains("example")); .getVendorEvidence().toString().contains("example"));
@@ -104,4 +99,5 @@ public class PythonPackageAnalyzerTest extends BaseTest {
} }
assertTrue("Version 0.0.1 not found in EggTest dependency.", found); assertTrue("Version 0.0.1 not found in EggTest dependency.", found);
} }
} }