refactored initialization of the analyzer

Former-commit-id: c9e32fbd039c87eafc25bf0bf62dad23c6a7279a
This commit is contained in:
Jeremy Long
2015-05-02 07:31:05 -04:00
parent 4f18e9ee7f
commit b6c0426c1c

View File

@@ -24,6 +24,8 @@ import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.owasp.dependencycheck.BaseTest; import org.owasp.dependencycheck.BaseTest;
import org.owasp.dependencycheck.analyzer.exception.AnalysisException; import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
@@ -37,129 +39,135 @@ import org.owasp.dependencycheck.dependency.Evidence;
*/ */
public class PythonDistributionAnalyzerTest extends BaseTest { public class PythonDistributionAnalyzerTest extends BaseTest {
/** PythonDistributionAnalyzer analyzer;
* Test of getName method, of class PythonDistributionAnalyzer.
*/
@Test
public void testGetName() {
assertEquals("Analyzer name wrong.", "Python Distribution Analyzer",
new PythonDistributionAnalyzer().getName());
}
/** @Before
* Test of getSupportedExtensions method, of class public void setUp() throws Exception {
* PythonDistributionAnalyzer. analyzer = new PythonDistributionAnalyzer();
*/ analyzer.setFilesMatched(true);
@Test analyzer.initialize();
public void testGetSupportedExtensions() { }
final String[] expected = { "whl", "egg", "zip", "METADATA", "PKG-INFO" };
assertEquals("Supported extensions should just have the following: "
+ StringUtils.join(expected, ", "),
new HashSet<String>(Arrays.asList(expected)),
new PythonDistributionAnalyzer().getSupportedExtensions());
}
/** @After
* Test of supportsExtension method, of class PythonDistributionAnalyzer. public void tearDown() throws Exception {
*/ analyzer.close();
@Test analyzer = null;
public void testSupportsExtension() { }
final PythonDistributionAnalyzer analyzer = new PythonDistributionAnalyzer();
assertTrue("Should support \"whl\" extension.",
analyzer.supportsExtension("whl"));
assertTrue("Should support \"egg\" extension.",
analyzer.supportsExtension("egg"));
assertTrue("Should support \"zip\" extension.",
analyzer.supportsExtension("zip"));
assertTrue("Should support \"METADATA\" extension.",
analyzer.supportsExtension("METADATA"));
assertTrue("Should support \"PKG-INFO\" extension.",
analyzer.supportsExtension("PKG-INFO"));
}
/** /**
* Test of inspect method, of class PythonDistributionAnalyzer. * Test of getName method, of class PythonDistributionAnalyzer.
* */
* @throws Exception @Test
* is thrown when an exception occurs. public void testGetName() {
*/ assertEquals("Analyzer name wrong.", "Python Distribution Analyzer",
@Test analyzer.getName());
public void testAnalyzeWheel() throws AnalysisException { }
djangoAssertions(new Dependency(BaseTest.getResourceAsFile(this,
"python/Django-1.7.2-py2.py3-none-any.whl")));
}
/** /**
* Test of inspect method, of class PythonDistributionAnalyzer. * Test of getSupportedExtensions method, of class PythonDistributionAnalyzer.
* */
* @throws Exception @Test
* is thrown when an exception occurs. public void testGetSupportedExtensions() {
*/ final String[] expected = {"whl", "egg", "zip", "METADATA", "PKG-INFO"};
@Test assertEquals("Supported extensions should just have the following: "
public void testAnalyzeSitePackage() throws AnalysisException { + StringUtils.join(expected, ", "),
final Dependency result = new Dependency(BaseTest.getResourceAsFile( new HashSet<String>(Arrays.asList(expected)),
this, "python/site-packages/Django-1.7.2.dist-info/METADATA")); analyzer.getSupportedExtensions());
djangoAssertions(result); }
assertEquals("Django-1.7.2.dist-info/METADATA",
result.getDisplayFileName());
}
private void djangoAssertions(final Dependency result) /**
throws AnalysisException { * Test of supportsExtension method, of class PythonDistributionAnalyzer.
new PythonDistributionAnalyzer().analyze(result, null); */
assertTrue("Expected vendor evidence to contain \"djangoproject\".", @Test
result.getVendorEvidence().toString().contains("djangoproject")); public void testSupportsExtension() {
boolean found = false; assertTrue("Should support \"whl\" extension.",
for (final Evidence e : result.getVersionEvidence()) { analyzer.supportsExtension("whl"));
if ("Version".equals(e.getName()) && "1.7.2".equals(e.getValue())) { assertTrue("Should support \"egg\" extension.",
found = true; analyzer.supportsExtension("egg"));
break; assertTrue("Should support \"zip\" extension.",
} analyzer.supportsExtension("zip"));
} assertTrue("Should support \"METADATA\" extension.",
assertTrue("Version 1.7.2 not found in Django dependency.", found); analyzer.supportsExtension("METADATA"));
} assertTrue("Should support \"PKG-INFO\" extension.",
analyzer.supportsExtension("PKG-INFO"));
}
@Test /**
public void testAnalyzeEggInfoFolder() throws AnalysisException { * Test of inspect method, of class PythonDistributionAnalyzer.
eggtestAssertions(this, *
"python/site-packages/EggTest.egg-info/PKG-INFO", * @throws Exception is thrown when an exception occurs.
new PythonDistributionAnalyzer()); */
} @Test
public void testAnalyzeWheel() throws AnalysisException {
djangoAssertions(new Dependency(BaseTest.getResourceAsFile(this,
"python/Django-1.7.2-py2.py3-none-any.whl")));
}
@Test /**
public void testAnalyzeEggArchive() throws AnalysisException { * Test of inspect method, of class PythonDistributionAnalyzer.
eggtestAssertions(this, "python/dist/EggTest-0.0.1-py2.7.egg", *
new PythonDistributionAnalyzer()); * @throws Exception is thrown when an exception occurs.
} */
@Test
public void testAnalyzeSitePackage() throws AnalysisException {
final Dependency result = new Dependency(BaseTest.getResourceAsFile(
this, "python/site-packages/Django-1.7.2.dist-info/METADATA"));
djangoAssertions(result);
assertEquals("Django-1.7.2.dist-info/METADATA",
result.getDisplayFileName());
}
@Test private void djangoAssertions(final Dependency result)
public void testAnalyzeEggArchiveNamedZip() throws AnalysisException { throws AnalysisException {
eggtestAssertions(this, "python/dist/EggTest-0.0.1-py2.7.zip", boolean found = false;
new PythonDistributionAnalyzer()); analyzer.analyze(result, null);
} assertTrue("Expected vendor evidence to contain \"djangoproject\".",
result.getVendorEvidence().toString().contains("djangoproject"));
for (final Evidence e : result.getVersionEvidence()) {
if ("Version".equals(e.getName()) && "1.7.2".equals(e.getValue())) {
found = true;
break;
}
}
assertTrue("Version 1.7.2 not found in Django dependency.", found);
}
@Test @Test
public void testAnalyzeEggFolder() throws AnalysisException { public void testAnalyzeEggInfoFolder() throws AnalysisException {
eggtestAssertions( eggtestAssertions(this,
this, "python/site-packages/EggTest.egg-info/PKG-INFO");
"python/site-packages/EggTest-0.0.1-py2.7.egg/EGG-INFO/PKG-INFO", }
new PythonDistributionAnalyzer());
}
public static void eggtestAssertions(Object context, final String resource, @Test
Analyzer analyzer) throws AnalysisException { public void testAnalyzeEggArchive() throws AnalysisException {
final Dependency result = new Dependency(BaseTest.getResourceAsFile( eggtestAssertions(this, "python/dist/EggTest-0.0.1-py2.7.egg");
context, resource)); }
analyzer.analyze(result, null);
assertTrue("Expected vendor evidence to contain \"example\".", result @Test
.getVendorEvidence().toString().contains("example")); public void testAnalyzeEggArchiveNamedZip() throws AnalysisException {
boolean found = false; eggtestAssertions(this, "python/dist/EggTest-0.0.1-py2.7.zip");
for (final Evidence e : result.getVersionEvidence()) { }
if ("0.0.1".equals(e.getValue())) {
found = true; @Test
break; public void testAnalyzeEggFolder() throws AnalysisException {
} eggtestAssertions(
} this,
assertTrue("Version 0.0.1 not found in EggTest dependency.", found); "python/site-packages/EggTest-0.0.1-py2.7.egg/EGG-INFO/PKG-INFO");
} }
public void eggtestAssertions(Object context, final String resource) throws AnalysisException {
boolean found = false;
final Dependency result = new Dependency(BaseTest.getResourceAsFile(
context, resource));
analyzer.analyze(result, null);
assertTrue("Expected vendor evidence to contain \"example\".", result
.getVendorEvidence().toString().contains("example"));
for (final Evidence e : result.getVersionEvidence()) {
if ("0.0.1".equals(e.getValue())) {
found = true;
break;
}
}
assertTrue("Version 0.0.1 not found in EggTest dependency.", found);
}
} }