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.analyzer;
    20
    21  import java.util.Properties;
    22  import org.owasp.dependencycheck.analyzer.JarAnalyzer;
    23  import org.owasp.dependencycheck.dependency.Dependency;
    24  import org.owasp.dependencycheck.dependency.Evidence;
    25  import java.util.HashSet;
    26  import java.io.File;
    27  import java.util.Set;
    28  import org.junit.After;
    29  import org.junit.AfterClass;
    30  import org.junit.Before;
    31  import org.junit.BeforeClass;
    32  import org.junit.Test;
    33  import static org.junit.Assert.*;
    34
    35  /**
    36   *
    37   * @author Jeremy Long (jeremy.long@owasp.org)
    38   */
    39  public class JarAnalyzerTest {
    40
    41      public JarAnalyzerTest() {
    42      }
    43
    44      @BeforeClass
    45      public static void setUpClass() throws Exception {
    46      }
    47
    48      @AfterClass
    49      public static void tearDownClass() throws Exception {
    50      }
    51
    52      @Before
    53      public void setUp() {
    54      }
    55
    56      @After
    57      public void tearDown() {
    58      }
    59
    60      /**
    61       * Test of inspect method, of class JarAnalyzer.
    62       *
    63       * @throws Exception is thrown when an exception occurs.
    64       */
    65      @Test
    66      public void testAnalyze() throws Exception {
    67          File file = new File(this.getClass().getClassLoader().getResource("struts2-core-2.1.2.jar").getPath());
    68          Dependency result = new Dependency(file);
    69          JarAnalyzer instance = new JarAnalyzer();
    70          instance.analyze(result, null);
    71          assertTrue(result.getVendorEvidence().toString().toLowerCase().contains("apache"));
    72          assertTrue(result.getVendorEvidence().getWeighting().contains("apache"));
    73
    74          file = new File(this.getClass().getClassLoader().getResource("org.mortbay.jetty.jar").getPath());
    75          result = new Dependency(file);
    76          instance.analyze(result, null);
    77          boolean found = false;
    78          for (Evidence e : result.getProductEvidence()) {
    79              if (e.getName().equalsIgnoreCase("package-title")
    80                      && e.getValue().equalsIgnoreCase("org.mortbay.http")) {
    81                  found = true;
    82                  break;
    83              }
    84          }
    85          assertTrue("package-title of org.mortbay.http not found in org.mortbay.jetty.jar", found);
    86
    87          found = false;
    88          for (Evidence e : result.getVendorEvidence()) {
    89              if (e.getName().equalsIgnoreCase("implementation-url")
    90                      && e.getValue().equalsIgnoreCase("http://jetty.mortbay.org")) {
    91                  found = true;
    92                  break;
    93              }
    94          }
    95          assertTrue("implementation-url of http://jetty.mortbay.org not found in org.mortbay.jetty.jar", found);
    96
    97          found = false;
    98          for (Evidence e : result.getVersionEvidence()) {
    99              if (e.getName().equalsIgnoreCase("Implementation-Version")
    100                     && e.getValue().equalsIgnoreCase("4.2.27")) {
    101                 found = true;
    102                 break;
    103             }
    104         }
    105         assertTrue("implementation-version of 4.2.27 not found in org.mortbay.jetty.jar", found);
    106
    107         file = new File(this.getClass().getClassLoader().getResource("org.mortbay.jmx.jar").getPath());
    108         result = new Dependency(file);
    109         instance.analyze(result, null);
    110         assertEquals("org.mortbar,jmx.jar has version evidence?", result.getVersionEvidence().size(), 0);
    111     }
    112
    113     /**
    114      * Test of getSupportedExtensions method, of class JarAnalyzer.
    115      */
    116     @Test
    117     public void testGetSupportedExtensions() {
    118         JarAnalyzer instance = new JarAnalyzer();
    119         Set expResult = new HashSet();
    120         expResult.add("jar");
    121         expResult.add("war");
    122         Set result = instance.getSupportedExtensions();
    123         assertEquals(expResult, result);
    124     }
    125
    126     /**
    127      * Test of getName method, of class JarAnalyzer.
    128      */
    129     @Test
    130     public void testGetName() {
    131         JarAnalyzer instance = new JarAnalyzer();
    132         String expResult = "Jar Analyzer";
    133         String result = instance.getName();
    134         assertEquals(expResult, result);
    135     }
    136
    137     /**
    138      * Test of supportsExtension method, of class JarAnalyzer.
    139      */
    140     @Test
    141     public void testSupportsExtension() {
    142         String extension = "jar";
    143         JarAnalyzer instance = new JarAnalyzer();
    144         boolean expResult = true;
    145         boolean result = instance.supportsExtension(extension);
    146         assertEquals(expResult, result);
    147     }
    148
    149     @Test
    150     public void testInterpolateString() {
    151         Properties prop = new Properties();
    152         prop.setProperty("key", "value");
    153         prop.setProperty("nested", "nested ${key}");
    154         String text = "This is a test of '${key}' '${nested}'";
    155         String expResults = "This is a test of 'value' 'nested value'";
    156         JarAnalyzer instance = new JarAnalyzer();
    157         String results = instance.interpolateString(text, prop);
    158         assertEquals(expResults, results);
    159     }
    160 }