Lots of updates, incorporated nvd cve data.

Former-commit-id: d54b2964cf11776521ee7114f536c8c3d9e14028
This commit is contained in:
jeremylong
2012-10-23 14:57:50 -04:00
parent 65700a5a08
commit 2f9b1f6314
156 changed files with 12925 additions and 194795 deletions

View File

@@ -4,7 +4,6 @@
*/
package org.codesecure.dependencycheck;
import org.codesecure.dependencycheck.data.lucene.BaseIndexTestCase;
import org.codesecure.dependencycheck.reporting.ReportGenerator;
import org.junit.After;
import org.junit.AfterClass;
@@ -17,10 +16,11 @@ import static org.junit.Assert.*;
*
* @author Jeremy Long (jeremy.long@gmail.com)
*/
public class EngineTest extends BaseIndexTestCase {
public class EngineTest {
public EngineTest(String testName) {
super(testName);
public EngineTest() throws Exception {
org.codesecure.dependencycheck.data.nvdcve.BaseIndexTestCase.ensureIndexExists();
org.codesecure.dependencycheck.data.cpe.BaseIndexTestCase.ensureIndexExists();
}
@BeforeClass

View File

@@ -2,7 +2,7 @@
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.codesecure.dependencycheck.data.lucene;
package org.codesecure.dependencycheck.data.cpe;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
@@ -30,15 +30,15 @@ public abstract class BaseIndexTestCase extends TestCase {
ensureIndexExists();
}
protected void ensureIndexExists() throws Exception {
String indexPath = Settings.getString("cpe");
public static void ensureIndexExists() throws Exception {
String indexPath = Settings.getString(Settings.KEYS.CPE_INDEX);
java.io.File f = new File(indexPath);
if (!f.exists()) {
f.mkdirs();
FileInputStream fis = null;
ZipInputStream zin = null;
try {
File path = new File(this.getClass().getClassLoader().getResource("index.cpe.zip").getPath());
File path = new File(BaseIndexTestCase.class.getClassLoader().getResource("index.cpe.zip").getPath());
fis = new FileInputStream(path);
zin = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;

View File

@@ -11,7 +11,6 @@ import java.util.List;
import java.util.Set;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;
import org.codesecure.dependencycheck.data.lucene.BaseIndexTestCase;
import org.codesecure.dependencycheck.dependency.Dependency;
import org.codesecure.dependencycheck.analyzer.JarAnalyzer;
import org.codesecure.dependencycheck.dependency.Evidence;

View File

@@ -4,7 +4,6 @@
*/
package org.codesecure.dependencycheck.data.cpe;
import org.codesecure.dependencycheck.data.cpe.Entry;
import junit.framework.TestCase;
/**

View File

@@ -5,12 +5,7 @@
package org.codesecure.dependencycheck.data.cpe;
import java.io.File;
import org.codesecure.dependencycheck.data.lucene.BaseIndexTestCase;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.store.Directory;
import org.junit.After;
import org.junit.AfterClass;
@@ -23,9 +18,9 @@ import static org.junit.Assert.*;
*
* @author Jeremy Long (jeremy.long@gmail.com)
*/
public class IndexTest extends BaseIndexTestCase {
public class IndexIntegrationTest extends BaseIndexTestCase {
public IndexTest(String testCase) {
public IndexIntegrationTest(String testCase) {
super(testCase);
}
@@ -78,8 +73,8 @@ public class IndexTest extends BaseIndexTestCase {
* Test of update method, of class Index.
*/
@Test
public void testUpdateIndexFromWeb() throws Exception {
System.out.println("updateIndexFromWeb");
public void testUpdate() throws Exception {
System.out.println("update");
Index instance = new Index();
instance.update();
}
@@ -91,8 +86,7 @@ public class IndexTest extends BaseIndexTestCase {
public void testUpdateNeeded() throws Exception {
System.out.println("updateNeeded");
Index instance = new Index();
long expResult = 0L;
long result = instance.updateNeeded();
instance.updateNeeded();
//if an exception is thrown this test fails. However, because it depends on the
// order of the tests what this will return I am just testing for the exception.
//assertTrue(expResult < result);

View File

@@ -1,45 +0,0 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.codesecure.dependencycheck.data.cpe.xml;
import java.io.File;
import junit.framework.TestCase;
/**
*
* @author jeremy
*/
public class CPEHandlerTest extends TestCase {
public CPEHandlerTest(String testName) {
super(testName);
}
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
/**
* Test of all methods within class CPEHandler.
*/
public void testHandler() throws Exception {
System.out.println("CPEHandler");
File path = new File(this.getClass().getClassLoader().getResource("official-cpe-dictionary_v2.2.xml").getPath());
Importer.importXML(path.getCanonicalPath());
}
}

View File

@@ -32,7 +32,7 @@ public class ImporterTest extends TestCase {
/**
* Test of all methods within class CPEHandler.
* @throws Exception is thrown when an excpetion occurs.
* @throws Exception is thrown when an excretion occurs.
*/
public void testHandler() throws Exception {
System.out.println("importXML");

View File

@@ -0,0 +1,98 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.codesecure.dependencycheck.data.nvdcve;
import org.codesecure.dependencycheck.data.cpe.*;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import junit.framework.TestCase;
import org.codesecure.dependencycheck.utils.Settings;
/**
*
* @author Jeremy Long (jeremy.long@gmail.com)
*/
public abstract class BaseIndexTestCase extends TestCase {
public BaseIndexTestCase(String testName) {
super(testName);
}
@Override
protected void setUp() throws Exception {
super.setUp();
ensureIndexExists();
}
public static void ensureIndexExists() throws Exception {
String indexPath = Settings.getString(Settings.KEYS.CVE_INDEX);
java.io.File f = new File(indexPath);
if (!f.exists()) {
f.mkdirs();
FileInputStream fis = null;
ZipInputStream zin = null;
try {
File path = new File(BaseIndexTestCase.class.getClassLoader().getResource("index.nvdcve.zip").getPath());
fis = new FileInputStream(path);
zin = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null) {
if (entry.isDirectory()) {
continue;
}
int BUFFER = 2048;
String outputName = indexPath + File.separatorChar + entry.getName();
FileOutputStream fos = null;
BufferedOutputStream dest = null;
try {
File o = new File(outputName);
// File oPath = new File(o.getParent());
// if (!oPath.exists()) {
// oPath.mkdir();
// }
o.createNewFile();
fos = new FileOutputStream(o,false);
dest = new BufferedOutputStream(fos, BUFFER);
byte data[] = new byte[BUFFER];
int count;
while ((count = zin.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
} catch (Exception ex) {
String ignore = ex.getMessage();
} finally {
try {
dest.flush();
dest.close();
dest = null;
} catch (Throwable ex) { String ignore = ex.getMessage(); }
try {
fos.close();
fos = null;
} catch (Throwable ex) { String ignore = ex.getMessage(); }
}
}
} finally {
try {
if (zin!=null) {
zin.close();
}
zin = null;
} catch (Throwable ex) { String ignore = ex.getMessage(); }
try {
if (fis!=null) {
fis.close();
}
fis = null;
} catch (Throwable ex) { String ignore = ex.getMessage(); }
}
}
}
}

View File

@@ -0,0 +1,85 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.codesecure.dependencycheck.data.nvdcve;
import java.io.File;
import java.util.Map;
import org.apache.lucene.store.Directory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.*;
/**
*
* @author Jeremy
*/
public class IndexIntegrationTest extends BaseIndexTestCase {
public IndexIntegrationTest(String testName) {
super(testName);
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of retrieveCurrentTimestampFromWeb method, of class Index.
*/
@Test
public void testRetrieveCurrentTimestampFromWeb() throws Exception {
System.out.println("retrieveCurrentTimestampFromWeb");
Index instance = new Index();
Map<String, Index.NvdCveUrl> result = instance.retrieveCurrentTimestampsFromWeb();
assertEquals(12, result.size());
}
/**
* Test of getDirectory method, of class Index.
*/
@Test
public void testGetDirectory() throws Exception {
System.out.println("getDirectory");
Index instance = new Index();
String exp = File.separatorChar + "target" + File.separatorChar + "data" + File.separatorChar + "cve";
Directory result = instance.getDirectory();
assertTrue(result.toString().contains(exp));
}
/**
* Test of update method, of class Index.
*/
@Test
public void testUpdate() throws Exception {
System.out.println("update");
Index instance = new Index();
instance.update();
}
/**
* Test of updateNeeded method, of class Index.
*/
@Test
public void testUpdateNeeded() throws Exception {
System.out.println("updateNeeded");
Index instance = new Index();
instance.updateNeeded();
//if an exception is thrown this test fails. However, because it depends on the
// order of the tests what this will return I am just testing for the exception.
}
}

View File

@@ -0,0 +1,77 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.codesecure.dependencycheck.data.nvdcve.xml;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.MalformedURLException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.codesecure.dependencycheck.data.nvdcve.generated.VulnerabilityType;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
/**
*
* @author Jeremy
*/
public class NvdCveXmlFilterTest {
public NvdCveXmlFilterTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of process method, of class NvdCveXmlFilter.
*/
@Test
public void testFilter() throws JAXBException, SAXException, ParserConfigurationException, MalformedURLException, IOException {
System.out.println("filter");
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
XMLReader reader = factory.newSAXParser().getXMLReader();
JAXBContext context = JAXBContext.newInstance("org.codesecure.dependencycheck.data.nvdcve.generated");
NvdCveXmlFilter filter = new NvdCveXmlFilter(context);
reader.setContentHandler(filter);
File file = new File(this.getClass().getClassLoader().getResource("nvdcve-2.0-2012.xml").getPath());
Reader fileReader = new FileReader(file);
InputSource is = new InputSource(fileReader);
reader.parse(is);
}
}

View File

@@ -10,7 +10,7 @@ import java.util.ArrayList;
import java.io.File;
import org.codesecure.dependencycheck.dependency.Dependency;
import java.util.HashMap;
import org.codesecure.dependencycheck.data.lucene.BaseIndexTestCase;
import org.codesecure.dependencycheck.data.cpe.BaseIndexTestCase;
import java.util.Map;
import org.codesecure.dependencycheck.dependency.Evidence.Confidence;
import org.junit.After;

View File

@@ -32,7 +32,7 @@ public class ChecksumTest extends TestCase {
/**
* Test of getChecksum method, of class Checksum.
* @throws Exception thrown when an excpetion occurs.
* @throws Exception thrown when an exception occurs.
*/
@Test
public void testGetChecksum() throws Exception {

View File

@@ -101,7 +101,7 @@ public class SettingsTest extends TestCase {
* Test of getInt method, of class Settings.
*/
@Test
public void testGetInt() {
public void testGetInt() throws InvalidSettingException {
System.out.println("getInt");
String key = "SomeNumber";
int expResult = 85;
@@ -114,7 +114,7 @@ public class SettingsTest extends TestCase {
* Test of getLong method, of class Settings.
*/
@Test
public void testGetLong() {
public void testGetLong() throws InvalidSettingException {
System.out.println("getLong");
String key = "SomeNumber";
long expResult = 300L;
@@ -127,7 +127,7 @@ public class SettingsTest extends TestCase {
* Test of getBoolean method, of class Settings.
*/
@Test
public void testGetBoolean() {
public void testGetBoolean() throws InvalidSettingException {
System.out.println("getBoolean");
String key = "SomeBoolean";
Settings.setString(key, "false");