From 1f7c64e279e1d19f7816e0934406bfa6ecf05088 Mon Sep 17 00:00:00 2001 From: Jeremy Long Date: Fri, 16 Aug 2013 14:13:01 -0400 Subject: [PATCH] corrected minor issue with path to zipped test data Former-commit-id: 9d4d5d6aa5d56028a8fcb871498f20a7cf2febc9 --- dependency-check-ant/pom.xml | 32 ++++++++++++++++++- .../taskdefs/DependencyCheckTaskTest.java | 5 ++- dependency-check-core/pom.xml | 12 +++++++ .../owasp/dependencycheck/utils/Settings.java | 30 ++++++++++++++--- .../data/cpe/BaseIndexTestCase.java | 14 ++++---- .../data/nvdcve/BaseDBTestCase.java | 15 +++++---- 6 files changed, 89 insertions(+), 19 deletions(-) diff --git a/dependency-check-ant/pom.xml b/dependency-check-ant/pom.xml index 30de909b2..337f14900 100644 --- a/dependency-check-ant/pom.xml +++ b/dependency-check-ant/pom.xml @@ -146,13 +146,32 @@ Copyright (c) 2013 - Jeremy Long. All Rights Reserved. ${basedir}/../src/test/resources false - jetty-6.1.0.jar org.mortbay.jetty.jar + + copy-data + validate + + copy-resources + + + ${project.build.directory}/test-classes + + + ${basedir}/../src/test/resources + false + + db.cve.zip + index.cpe.zip + + + + + @@ -238,6 +257,10 @@ Copyright (c) 2013 - Jeremy Long. All Rights Reserved. ${project.build.directory}/cobertura/cobertura.ser target + + data.directory + ${project.build.directory}/dependency-check-data + @@ -389,6 +412,13 @@ Copyright (c) 2013 - Jeremy Long. All Rights Reserved. dependency-check-core ${project.parent.version} + + org.owasp + dependency-check-core + ${project.parent.version} + test-jar + test + org.apache.ant ant diff --git a/dependency-check-ant/src/test/java/org/owasp/dependencycheck/taskdefs/DependencyCheckTaskTest.java b/dependency-check-ant/src/test/java/org/owasp/dependencycheck/taskdefs/DependencyCheckTaskTest.java index 34c0c5591..dee7f7bda 100644 --- a/dependency-check-ant/src/test/java/org/owasp/dependencycheck/taskdefs/DependencyCheckTaskTest.java +++ b/dependency-check-ant/src/test/java/org/owasp/dependencycheck/taskdefs/DependencyCheckTaskTest.java @@ -46,7 +46,10 @@ public class DependencyCheckTaskTest extends BuildFileTest { @Before @Override - public void setUp() { + public void setUp() throws Exception { + org.owasp.dependencycheck.data.nvdcve.BaseDBTestCase.ensureDBExists(DependencyCheckTaskTest.class); + org.owasp.dependencycheck.data.cpe.BaseIndexTestCase.ensureIndexExists(this.getClass()); + final String buildFile = this.getClass().getClassLoader().getResource("build.xml").getPath(); configureProject(buildFile); } diff --git a/dependency-check-core/pom.xml b/dependency-check-core/pom.xml index 521bd9333..c501e78cc 100644 --- a/dependency-check-core/pom.xml +++ b/dependency-check-core/pom.xml @@ -112,6 +112,18 @@ along with Dependency-Check. If not, see . + + org.apache.maven.plugins + maven-jar-plugin + 2.4 + + + + test-jar + + + + org.codehaus.mojo cobertura-maven-plugin diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/Settings.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/Settings.java index 893d7466e..a807476a6 100644 --- a/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/Settings.java +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/utils/Settings.java @@ -258,15 +258,17 @@ public final class Settings { * to the folder containing the JAR file containing this class. * * @param key the key to lookup within the properties file + * @param clazz the class to obtain the base directory from in case "[JAR]\" + * exists * @return the property from the properties file converted to a File object * @throws IOException thrown if the file path to the JAR cannot be found */ - public static File getFile(String key) throws IOException { + public static File getFile(String key, Class clazz) throws IOException { final String file = getString(key); final String baseDir = getString(Settings.KEYS.DATA_DIRECTORY); if (baseDir != null) { if (baseDir.startsWith("[JAR]/")) { - final File jarPath = getJarPath(); + final File jarPath = getJarPath(clazz); final File newBase = new File(jarPath.getCanonicalPath(), baseDir.substring(6)); return new File(newBase, file); } @@ -275,14 +277,31 @@ public final class Settings { return new File(file); } + /** + * Returns a value from the properties file as a File object. If the value + * was specified as a system property or passed in via the -Dprop=value + * argument - this method will return the value from the system properties + * before the values in the contained configuration file. + * + * This method will also replace a leading "[JAR]\" sequence with the path + * to the folder containing the JAR file containing this class. + * + * @param key the key to lookup within the properties file + * @return the property from the properties file converted to a File object + * @throws IOException thrown if the file path to the JAR cannot be found + */ + public static File getFile(String key) throws IOException { + return getFile(key, Settings.class); + } + /** * Attempts to retrieve the folder containing the Jar file containing the * Settings class. * * @return a File object */ - private static File getJarPath() { - final String jarPath = Settings.class.getProtectionDomain().getCodeSource().getLocation().getPath(); + private static File getJarPath(Class clazz) { + final String jarPath = clazz.getProtectionDomain().getCodeSource().getLocation().getPath(); String decodedPath = "."; try { decodedPath = URLDecoder.decode(jarPath, "UTF-8"); @@ -291,7 +310,8 @@ public final class Settings { } final File path = new File(decodedPath); - if (path.getName().toLowerCase().endsWith(".jar")) { + //TODO - need to remove the "test-classes" check which is only here to make test cases work. + if (path.getName().toLowerCase().endsWith(".jar") || path.getName().equals("test-classes")) { return path.getParentFile(); } else { return new File("."); diff --git a/dependency-check-core/src/test/java/org/owasp/dependencycheck/data/cpe/BaseIndexTestCase.java b/dependency-check-core/src/test/java/org/owasp/dependencycheck/data/cpe/BaseIndexTestCase.java index 84efe73fb..91418cd43 100644 --- a/dependency-check-core/src/test/java/org/owasp/dependencycheck/data/cpe/BaseIndexTestCase.java +++ b/dependency-check-core/src/test/java/org/owasp/dependencycheck/data/cpe/BaseIndexTestCase.java @@ -61,16 +61,18 @@ public abstract class BaseIndexTestCase { public void tearDown() throws Exception { } - protected static File getDataDirectory() throws IOException { - final String fileName = Settings.getString(Settings.KEYS.CPE_DATA_DIRECTORY); - final String dataDirectory = Settings.getString(Settings.KEYS.DATA_DIRECTORY); - return new File(dataDirectory, fileName); - //return FileUtils.getDataDirectory(fileName, Index.class); + protected static File getDataDirectory(Class clazz) throws IOException { + final File dataDirectory = Settings.getFile(Settings.KEYS.CPE_DATA_DIRECTORY, clazz); + return dataDirectory; } public static void ensureIndexExists() throws Exception { + ensureIndexExists(BaseIndexTestCase.class); + } + + public static void ensureIndexExists(Class clazz) throws Exception { //String indexPath = Settings.getString(Settings.KEYS.CPE_DATA_DIRECTORY); - String indexPath = getDataDirectory().getCanonicalPath(); + String indexPath = getDataDirectory(clazz).getAbsolutePath(); java.io.File f = new File(indexPath); if (!f.exists() || (f.isDirectory() && f.listFiles().length == 0)) { diff --git a/dependency-check-core/src/test/java/org/owasp/dependencycheck/data/nvdcve/BaseDBTestCase.java b/dependency-check-core/src/test/java/org/owasp/dependencycheck/data/nvdcve/BaseDBTestCase.java index f36dcb69e..1ccd47484 100644 --- a/dependency-check-core/src/test/java/org/owasp/dependencycheck/data/nvdcve/BaseDBTestCase.java +++ b/dependency-check-core/src/test/java/org/owasp/dependencycheck/data/nvdcve/BaseDBTestCase.java @@ -46,21 +46,24 @@ public abstract class BaseDBTestCase extends TestCase { ensureDBExists(); } - protected static File getDataDirectory() throws IOException { - final String fileName = Settings.getString(Settings.KEYS.CVE_DATA_DIRECTORY); - final String dataDirectory = Settings.getString(Settings.KEYS.DATA_DIRECTORY); - return new File(dataDirectory, fileName); + protected static File getDataDirectory(Class clazz) throws IOException { + final File dataDirectory = Settings.getFile(Settings.KEYS.CVE_DATA_DIRECTORY, clazz); + return dataDirectory; } public static void ensureDBExists() throws Exception { - String indexPath = getDataDirectory().getCanonicalPath(); + ensureDBExists(BaseDBTestCase.class); + } + + public static void ensureDBExists(Class clazz) throws Exception { + String indexPath = getDataDirectory(clazz).getAbsolutePath(); java.io.File f = new File(indexPath); if (!f.exists() || (f.isDirectory() && f.listFiles().length == 0)) { f.mkdirs(); FileInputStream fis = null; ZipInputStream zin = null; try { - File path = new File(BaseDBTestCase.class.getClassLoader().getResource("db.cve.zip").getPath()); + File path = new File(clazz.getClassLoader().getResource("db.cve.zip").getPath()); fis = new FileInputStream(path); zin = new ZipInputStream(new BufferedInputStream(fis)); ZipEntry entry;