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) 2013 Jeremy Long. All Rights Reserved.
    18   */
    19  package org.owasp.dependencycheck.analyzer;
    20
    21  import java.io.File;
    22  import java.util.HashSet;
    23  import java.util.Set;
    24  import org.junit.After;
    25  import org.junit.AfterClass;
    26  import org.junit.Before;
    27  import org.junit.BeforeClass;
    28  import org.junit.Test;
    29  import static org.junit.Assert.*;
    30  import org.owasp.dependencycheck.Engine;
    31  import org.owasp.dependencycheck.data.cpe.BaseIndexTestCase;
    32  import org.owasp.dependencycheck.dependency.Dependency;
    33  import org.owasp.dependencycheck.utils.Settings;
    34
    35  /**
    36   *
    37   * @author Jeremy Long (jeremy.long@owasp.org)
    38   */
    39  public class ArchiveAnalyzerTest extends BaseIndexTestCase {
    40
    41      public ArchiveAnalyzerTest() {
    42      }
    43
    44      @BeforeClass
    45      public static void setUpClass() {
    46      }
    47
    48      @AfterClass
    49      public static void tearDownClass() {
    50      }
    51
    52      @Before
    53      public void setUp() throws Exception {
    54          super.setUp();
    55      }
    56
    57      @After
    58      public void tearDown() throws Exception {
    59          super.tearDown();
    60      }
    61
    62      /**
    63       * Test of getSupportedExtensions method, of class ArchiveAnalyzer.
    64       */
    65      @Test
    66      public void testGetSupportedExtensions() {
    67          ArchiveAnalyzer instance = new ArchiveAnalyzer();
    68          Set expResult = new HashSet<String>();
    69          expResult.add("zip");
    70          expResult.add("war");
    71          expResult.add("ear");
    72          expResult.add("tar");
    73          expResult.add("gz");
    74          expResult.add("tgz");
    75          Set result = instance.getSupportedExtensions();
    76          assertEquals(expResult, result);
    77      }
    78
    79      /**
    80       * Test of getName method, of class ArchiveAnalyzer.
    81       */
    82      @Test
    83      public void testGetName() {
    84          ArchiveAnalyzer instance = new ArchiveAnalyzer();
    85          String expResult = "Archive Analyzer";
    86          String result = instance.getName();
    87          assertEquals(expResult, result);
    88      }
    89
    90      /**
    91       * Test of supportsExtension method, of class ArchiveAnalyzer.
    92       */
    93      @Test
    94      public void testSupportsExtension() {
    95          String extension = "7z"; //not supported
    96          ArchiveAnalyzer instance = new ArchiveAnalyzer();
    97          boolean expResult = false;
    98          boolean result = instance.supportsExtension(extension);
    99          assertEquals(expResult, result);
    100
    101         extension = "war"; //supported
    102         expResult = true;
    103         result = instance.supportsExtension(extension);
    104         assertEquals(expResult, result);
    105
    106         extension = "ear"; //supported
    107         result = instance.supportsExtension(extension);
    108         assertEquals(expResult, result);
    109
    110         extension = "zip"; //supported
    111         result = instance.supportsExtension(extension);
    112         assertEquals(expResult, result);
    113     }
    114
    115     /**
    116      * Test of getAnalysisPhase method, of class ArchiveAnalyzer.
    117      */
    118     @Test
    119     public void testGetAnalysisPhase() {
    120         ArchiveAnalyzer instance = new ArchiveAnalyzer();
    121         AnalysisPhase expResult = AnalysisPhase.INITIAL;
    122         AnalysisPhase result = instance.getAnalysisPhase();
    123         assertEquals(expResult, result);
    124     }
    125
    126     /**
    127      * Test of initialize and close methods, of class ArchiveAnalyzer.
    128      */
    129     @Test
    130     public void testInitialize() throws Exception {
    131         ArchiveAnalyzer instance = new ArchiveAnalyzer();
    132         instance.initialize();
    133
    134         instance.close();
    135
    136         //no exception means things worked.
    137     }
    138
    139     /**
    140      * Test of analyze method, of class ArchiveAnalyzer.
    141      */
    142     @Test
    143     public void testAnalyze() throws Exception {
    144         ArchiveAnalyzer instance = new ArchiveAnalyzer();
    145         try {
    146             instance.initialize();
    147
    148             File file = new File(this.getClass().getClassLoader().getResource("daytrader-ear-2.1.7.ear").getPath());
    149             Dependency dependency = new Dependency(file);
    150             Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, false);
    151             Engine engine = new Engine();
    152
    153             int initial_size = engine.getDependencies().size();
    154             instance.analyze(dependency, engine);
    155             int ending_size = engine.getDependencies().size();
    156
    157             assertTrue(initial_size < ending_size);
    158
    159         } finally {
    160             instance.close();
    161         }
    162     }
    163
    164     /**
    165      * Test of analyze method, of class ArchiveAnalyzer.
    166      */
    167     @Test
    168     public void testAnalyzeTar() throws Exception {
    169         ArchiveAnalyzer instance = new ArchiveAnalyzer();
    170         try {
    171             instance.initialize();
    172
    173             File file = new File(this.getClass().getClassLoader().getResource("file.tar").getPath());
    174             Dependency dependency = new Dependency(file);
    175             Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, false);
    176             Engine engine = new Engine();
    177
    178             int initial_size = engine.getDependencies().size();
    179             instance.analyze(dependency, engine);
    180             int ending_size = engine.getDependencies().size();
    181
    182             assertTrue(initial_size < ending_size);
    183
    184         } finally {
    185             instance.close();
    186         }
    187     }
    188
    189     /**
    190      * Test of analyze method, of class ArchiveAnalyzer.
    191      */
    192     @Test
    193     public void testAnalyzeTarGz() throws Exception {
    194         ArchiveAnalyzer instance = new ArchiveAnalyzer();
    195         try {
    196             instance.initialize();
    197
    198             File file = new File(this.getClass().getClassLoader().getResource("file.tar.gz").getPath());
    199             //Dependency dependency = new Dependency(file);
    200             Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, false);
    201             Engine engine = new Engine();
    202
    203             int initial_size = engine.getDependencies().size();
    204             //instance.analyze(dependency, engine);
    205             engine.scan(file);
    206             engine.analyzeDependencies();
    207             int ending_size = engine.getDependencies().size();
    208
    209             assertTrue(initial_size < ending_size);
    210
    211         } finally {
    212             instance.close();
    213         }
    214     }
    215
    216     /**
    217      * Test of analyze method, of class ArchiveAnalyzer.
    218      */
    219     @Test
    220     public void testAnalyzeTgz() throws Exception {
    221         ArchiveAnalyzer instance = new ArchiveAnalyzer();
    222         try {
    223             instance.initialize();
    224
    225             File file = new File(this.getClass().getClassLoader().getResource("file.tgz").getPath());
    226             Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, false);
    227             Engine engine = new Engine();
    228
    229             int initial_size = engine.getDependencies().size();
    230             engine.scan(file);
    231             engine.analyzeDependencies();
    232             int ending_size = engine.getDependencies().size();
    233
    234             assertTrue(initial_size < ending_size);
    235
    236         } finally {
    237             instance.close();
    238         }
    239     }
    240
    241     /**
    242      * Test of analyze method, of class ArchiveAnalyzer.
    243      */
    244     @Test
    245     public void testAnalyze_badZip() throws Exception {
    246         ArchiveAnalyzer instance = new ArchiveAnalyzer();
    247         try {
    248             instance.initialize();
    249
    250             File file = new File(this.getClass().getClassLoader().getResource("test.zip").getPath());
    251             Dependency dependency = new Dependency(file);
    252             Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, false);
    253             Engine engine = new Engine();
    254             int initial_size = engine.getDependencies().size();
    255 //            boolean failed = false;
    256 //            try {
    257             instance.analyze(dependency, engine);
    258 //            } catch (java.lang.UnsupportedClassVersionError ex) {
    259 //                failed = true;
    260 //            }
    261 //            assertTrue(failed);
    262             int ending_size = engine.getDependencies().size();
    263             assertEquals(initial_size, ending_size);
    264         } finally {
    265             instance.close();
    266         }
    267     }
    268 }