diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/Engine.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/Engine.java index b81da05e8..9e48bce14 100644 --- a/dependency-check-core/src/main/java/org/owasp/dependencycheck/Engine.java +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/Engine.java @@ -57,7 +57,14 @@ import static org.owasp.dependencycheck.analyzer.AnalysisPhase.*; */ public class Engine implements FileFilter { + /** + * {@link Engine} execution modes. + */ public enum Mode { + /** + * In evidence collection mode the {@link Engine} only collect evidence from the scan targets, + * and doesn't require a database. + */ EVIDENCE_COLLECTION( false, INITIAL, @@ -65,6 +72,11 @@ public class Engine implements FileFilter { INFORMATION_COLLECTION, POST_INFORMATION_COLLECTION ), + /** + * In evidence processing mode the {@link Engine} processes the evidence collected using the + * {@link #EVIDENCE_COLLECTION} mode. Dependencies should be injected into the {@link Engine} + * using {@link Engine#setDependencies(List)}. + */ EVIDENCE_PROCESSING( true, PRE_IDENTIFIER_ANALYSIS, @@ -75,6 +87,9 @@ public class Engine implements FileFilter { POST_FINDING_ANALYSIS, FINAL ), + /** + * In standalone mode the {@link Engine} will collect and process evidence in a single execution. + */ STANDALONE(true, AnalysisPhase.values()); public final boolean requiresDatabase; @@ -117,18 +132,21 @@ public class Engine implements FileFilter { private static final Logger LOGGER = LoggerFactory.getLogger(Engine.class); /** - * Creates a new Engine. + * Creates a new {@link Mode#STANDALONE} Engine. */ public Engine() { this(Mode.STANDALONE); } + /** + * Creates a new Engine. + */ public Engine(Mode mode) { this(Thread.currentThread().getContextClassLoader(), mode); } /** - * Creates a new Engine. + * Creates a new {@link Mode#STANDALONE} Engine. * * @param serviceClassLoader a reference the class loader being used */ @@ -140,6 +158,7 @@ public class Engine implements FileFilter { * Creates a new Engine. * * @param serviceClassLoader a reference the class loader being used + * @param mode the mode of the engine */ public Engine(ClassLoader serviceClassLoader, Mode mode) { this.serviceClassLoader = serviceClassLoader; diff --git a/dependency-check-core/src/test/java/org/owasp/dependencycheck/EngineModeTest.java b/dependency-check-core/src/test/java/org/owasp/dependencycheck/EngineModeTest.java new file mode 100644 index 000000000..680c89e09 --- /dev/null +++ b/dependency-check-core/src/test/java/org/owasp/dependencycheck/EngineModeTest.java @@ -0,0 +1,81 @@ +package org.owasp.dependencycheck; + +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.rules.TestName; +import org.owasp.dependencycheck.analyzer.AnalysisPhase; +import org.owasp.dependencycheck.utils.Settings; + +import java.nio.file.Files; +import java.nio.file.Path; + +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +/** + * @author Mark Rekveld + */ +public class EngineModeTest extends BaseTest { + + @Rule + public TemporaryFolder tempDir = new TemporaryFolder(); + @Rule + public TestName testName = new TestName(); + private Engine engine; + + @Before + public void setUp() throws Exception { + Settings.setString(Settings.KEYS.DATA_DIRECTORY, tempDir.newFolder().getAbsolutePath()); + } + + @After + public void tearDown() throws Exception { + engine.cleanup(); + } + + @Test + public void testEvidenceCollectionMode() throws Exception { + engine = new Engine(Engine.Mode.EVIDENCE_COLLECTION); + assertDatabase(false); + for (AnalysisPhase phase : Engine.Mode.EVIDENCE_COLLECTION.phases) { + assertThat(engine.getAnalyzers(phase), is(notNullValue())); + } + for (AnalysisPhase phase : Engine.Mode.EVIDENCE_PROCESSING.phases) { + assertThat(engine.getAnalyzers(phase), is(nullValue())); + } + } + + @Test + public void testEvidenceProcessingMode() throws Exception { + engine = new Engine(Engine.Mode.EVIDENCE_PROCESSING); + assertDatabase(true); + for (AnalysisPhase phase : Engine.Mode.EVIDENCE_PROCESSING.phases) { + assertThat(engine.getAnalyzers(phase), is(notNullValue())); + } + for (AnalysisPhase phase : Engine.Mode.EVIDENCE_COLLECTION.phases) { + assertThat(engine.getAnalyzers(phase), is(nullValue())); + } + } + + @Test + public void testStandaloneMode() throws Exception { + engine = new Engine(Engine.Mode.STANDALONE); + assertDatabase(true); + for (AnalysisPhase phase : Engine.Mode.STANDALONE.phases) { + assertThat(engine.getAnalyzers(phase), is(notNullValue())); + } + } + + private void assertDatabase(boolean exists) throws Exception { + Path directory = Settings.getDataDirectory().toPath(); + assertThat(Files.exists(directory), is(true)); + assertThat(Files.isDirectory(directory), is(true)); + Path database = directory.resolve(Settings.getString(Settings.KEYS.DB_FILE_NAME)); + assertThat(Files.exists(database), is(exists)); + } +} diff --git a/dependency-check-core/src/test/java/org/owasp/dependencycheck/analyzer/AnalyzerServiceTest.java b/dependency-check-core/src/test/java/org/owasp/dependencycheck/analyzer/AnalyzerServiceTest.java index b88bc4f3d..34f83be44 100644 --- a/dependency-check-core/src/test/java/org/owasp/dependencycheck/analyzer/AnalyzerServiceTest.java +++ b/dependency-check-core/src/test/java/org/owasp/dependencycheck/analyzer/AnalyzerServiceTest.java @@ -17,13 +17,18 @@ */ package org.owasp.dependencycheck.analyzer; -import java.util.List; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import org.junit.Test; import org.owasp.dependencycheck.BaseDBTestCase; import org.owasp.dependencycheck.utils.Settings; +import java.util.List; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.owasp.dependencycheck.analyzer.AnalysisPhase.FINAL; +import static org.owasp.dependencycheck.analyzer.AnalysisPhase.INITIAL; + /** * * @author Jeremy Long @@ -46,7 +51,22 @@ public class AnalyzerServiceTest extends BaseDBTestCase { } assertTrue("JarAnalyzer loaded", found); } - + + /** + * Test of getAnalyzers method, of class AnalyzerService. + */ + @Test + public void testGetAnalyzers_SpecificPhases() throws Exception { + AnalyzerService instance = new AnalyzerService(Thread.currentThread().getContextClassLoader()); + List result = instance.getAnalyzers(INITIAL, FINAL); + + for (Analyzer a : result) { + if (a.getAnalysisPhase() != INITIAL && a.getAnalysisPhase() != FINAL) { + fail("Only expecting analyzers for phases " + INITIAL + " and " + FINAL); + } + } + } + /** * Test of getAnalyzers method, of class AnalyzerService. */