View Javadoc

    1   /*
    2    * This file is part of dependency-check-core.
    3    *
    4    * Dependency-check-core is free software: you can redistribute it and/or modify it
    5    * under the terms of the GNU General Public License as published by the Free
    6    * Software Foundation, either version 3 of the License, or (at your option) any
    7    * later version.
    8    *
    9    * Dependency-check-core is distributed in the hope that it will be useful, but
    10   * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    12   * details.
    13   *
    14   * You should have received a copy of the GNU General Public License along with
    15   * dependency-check-core. If not, see http://www.gnu.org/licenses/.
    16   *
    17   * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
    18   */
    19  package org.owasp.dependencycheck.reporting;
    20
    21  import org.owasp.dependencycheck.Engine;
    22  import org.owasp.dependencycheck.data.cpe.BaseIndexTestCase;
    23  import org.junit.After;
    24  import org.junit.AfterClass;
    25  import org.junit.Before;
    26  import org.junit.BeforeClass;
    27  import org.junit.Test;
    28
    29  import javax.xml.XMLConstants;
    30  import javax.xml.transform.stream.StreamSource;
    31  import javax.xml.validation.Schema;
    32  import javax.xml.validation.SchemaFactory;
    33  import javax.xml.validation.Validator;
    34  import java.io.File;
    35  import java.io.InputStream;
    36
    37  /**
    38   *
    39   * @author Jeremy Long (jeremy.long@owasp.org)
    40   */
    41  public class ReportGeneratorTest {
    42
    43      @BeforeClass
    44      public static void setUpClass() throws Exception {
    45      }
    46
    47      @AfterClass
    48      public static void tearDownClass() throws Exception {
    49      }
    50
    51      @Before
    52      public void setUp() {
    53      }
    54
    55      @After
    56      public void tearDown() {
    57      }
    58
    59      /**
    60       * Test of generateReport method, of class ReportGenerator.
    61       *
    62       * @throws Exception is thrown when an exception occurs.
    63       */
    64      @Test
    65      public void testGenerateReport() throws Exception {
    66          String templateName = "HtmlReport";
    67  //        File f = new File("target/test-reports");
    68  //        if (!f.exists()) {
    69  //            f.mkdir();
    70  //        }
    71  //        String writeTo = "target/test-reports/Report.html";
    72  //        Map<String, Object> properties = new HashMap<String, Object>();
    73  //        Dependency d = new Dependency();
    74  //        d.setFileName("FileName.jar");
    75  //        d.setActualFilePath("lib/FileName.jar");
    76  //        d.addCPEentry("cpe://a:/some:cpe:1.0");
    77  //
    78  //        List<Dependency> dependencies = new ArrayList<Dependency>();
    79  //        d.getProductEvidence().addEvidence("jar","filename","<test>test", Confidence.HIGH);
    80  //        d.getProductEvidence().addEvidence("manifest","vendor","<test>test", Confidence.HIGH);
    81  //
    82  //        for (Evidence e : d.getProductEvidence().iterator(Confidence.HIGH)) {
    83  //            String t = e.getValue();
    84  //        }
    85  //        dependencies.add(d);
    86  //
    87  //        Dependency d2 = new Dependency();
    88  //        d2.setFileName("Another.jar");
    89  //        d2.setActualFilePath("lib/Another.jar");
    90  //        d2.addCPEentry("cpe://a:/another:cpe:1.0");
    91  //        d2.addCPEentry("cpe://a:/another:cpe:1.1");
    92  //        d2.addCPEentry("cpe://a:/another:cpe:1.2");
    93  //        d2.getProductEvidence().addEvidence("jar","filename","another.jar", Confidence.HIGH);
    94  //        d2.getProductEvidence().addEvidence("manifest","vendor","Company A", Confidence.MEDIUM);
    95  //
    96  //        for (Evidence e : d2.getProductEvidence().iterator(Confidence.HIGH)) {
    97  //            String t = e.getValue();
    98  //        }
    99  //
    100 //        dependencies.add(d2);
    101 //
    102 //        Dependency d3 = new Dependency();
    103 //        d3.setFileName("Third.jar");
    104 //        d3.setActualFilePath("lib/Third.jar");
    105 //        d3.getProductEvidence().addEvidence("jar","filename","third.jar", Confidence.HIGH);
    106 //
    107 //        for (Evidence e : d3.getProductEvidence().iterator(Confidence.HIGH)) {
    108 //            String t = e.getValue();
    109 //        }
    110 //
    111 //        dependencies.add(d3);
    112 //
    113 //        properties.put("dependencies",dependencies);
    114 //
    115 //        ReportGenerator instance = new ReportGenerator();
    116 //        instance.generateReport(templateName, writeTo, properties);
    117         //assertTrue("need to add a real check here", false);
    118     }
    119
    120     /**
    121      * Generates an XML report containing known vulnerabilities and realistic
    122      * data and validates the generated XML document against the XSD.
    123      * @throws Exception
    124      */
    125     @Test
    126     public void testGenerateXMLReport() throws Exception {
    127         String templateName = "XmlReport";
    128
    129         File f = new File("target/test-reports");
    130         if (!f.exists()) {
    131             f.mkdir();
    132         }
    133         String writeTo = "target/test-reports/Report.xml";
    134
    135         File struts = new File(this.getClass().getClassLoader().getResource("struts2-core-2.1.2.jar").getPath());
    136         File axis = new File(this.getClass().getClassLoader().getResource("axis2-adb-1.4.1.jar").getPath());
    137         File jetty = new File(this.getClass().getClassLoader().getResource("org.mortbay.jetty.jar").getPath());
    138
    139         Engine engine = new Engine();
    140         engine.scan(struts);
    141         engine.scan(axis);
    142         engine.scan(jetty);
    143         engine.analyzeDependencies();
    144
    145         ReportGenerator generator = new ReportGenerator("Test Report", engine.getDependencies(), engine.getAnalyzers());
    146         generator.generateReport(templateName, writeTo);
    147
    148         InputStream xsdStream = ReportGenerator.class.getClassLoader().getResourceAsStream("schema/DependencyCheck.xsd");
    149         StreamSource xsdSource = new StreamSource(xsdStream);
    150         StreamSource xmlSource =  new StreamSource(new File(writeTo));
    151         SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    152         Schema schema = sf.newSchema(xsdSource);
    153         Validator validator = schema.newValidator();
    154         validator.validate(xmlSource);
    155     }
    156 }