View Javadoc

    1   /*
    2    * This file is part of Dependency-Check.
    3    *
    4    * Dependency-Check 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 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. If not, see http://www.gnu.org/licenses/.
    16   *
    17   * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
    18   */
    19  package org.owasp.dependencycheck.cli;
    20
    21  import org.owasp.dependencycheck.cli.CliParser;
    22  import java.io.ByteArrayOutputStream;
    23  import java.io.File;
    24  import java.io.FileNotFoundException;
    25  import java.io.IOException;
    26  import java.io.PrintStream;
    27  import org.apache.commons.cli.ParseException;
    28  import org.junit.After;
    29  import org.junit.AfterClass;
    30  import org.junit.Assert;
    31  import org.junit.Before;
    32  import org.junit.BeforeClass;
    33  import org.junit.Test;
    34
    35  /**
    36   *
    37   * @author Jeremy Long (jeremy.long@owasp.org)
    38   */
    39  public class CliParserTest {
    40
    41      @BeforeClass
    42      public static void setUpClass() throws Exception {
    43      }
    44
    45      @AfterClass
    46      public static void tearDownClass() throws Exception {
    47      }
    48
    49      @Before
    50      public void setUp() throws Exception {
    51      }
    52
    53      @After
    54      public void tearDown() throws Exception {
    55      }
    56
    57      /**
    58       * Test of parse method, of class CliParser.
    59       *
    60       * @throws Exception thrown when an exception occurs.
    61       */
    62      @Test
    63      public void testParse() throws Exception {
    64
    65          String[] args = {};
    66          PrintStream out = System.out;
    67
    68          ByteArrayOutputStream baos = new ByteArrayOutputStream();
    69          System.setOut(new PrintStream(baos));
    70
    71          CliParser instance = new CliParser();
    72          instance.parse(args);
    73
    74          Assert.assertFalse(instance.isGetVersion());
    75          Assert.assertFalse(instance.isGetHelp());
    76          Assert.assertFalse(instance.isRunScan());
    77      }
    78
    79      /**
    80       * Test of parse method with help arg, of class CliParser.
    81       *
    82       * @throws Exception thrown when an exception occurs.
    83       */
    84      @Test
    85      public void testParse_help() throws Exception {
    86
    87          String[] args = {"-help"};
    88          PrintStream out = System.out;
    89
    90          CliParser instance = new CliParser();
    91          instance.parse(args);
    92
    93          Assert.assertFalse(instance.isGetVersion());
    94          Assert.assertTrue(instance.isGetHelp());
    95          Assert.assertFalse(instance.isRunScan());
    96      }
    97
    98      /**
    99       * Test of parse method with version arg, of class CliParser.
    100      *
    101      * @throws Exception thrown when an exception occurs.
    102      */
    103     @Test
    104     public void testParse_version() throws Exception {
    105
    106         String[] args = {"-version"};
    107
    108         CliParser instance = new CliParser();
    109         instance.parse(args);
    110         Assert.assertTrue(instance.isGetVersion());
    111         Assert.assertFalse(instance.isGetHelp());
    112         Assert.assertFalse(instance.isRunScan());
    113
    114     }
    115
    116     /**
    117      * Test of parse method with jar and cpe args, of class CliParser.
    118      *
    119      * @throws Exception thrown when an exception occurs.
    120      */
    121     @Test
    122     public void testParse_unknown() throws Exception {
    123
    124         String[] args = {"-unknown"};
    125
    126         PrintStream out = System.out;
    127         PrintStream err = System.err;
    128         ByteArrayOutputStream baos_out = new ByteArrayOutputStream();
    129         ByteArrayOutputStream baos_err = new ByteArrayOutputStream();
    130         System.setOut(new PrintStream(baos_out));
    131         System.setErr(new PrintStream(baos_err));
    132
    133         CliParser instance = new CliParser();
    134
    135         try {
    136             instance.parse(args);
    137         } catch (ParseException ex) {
    138             Assert.assertTrue(ex.getMessage().contains("Unrecognized option"));
    139         }
    140         Assert.assertFalse(instance.isGetVersion());
    141         Assert.assertFalse(instance.isGetHelp());
    142         Assert.assertFalse(instance.isRunScan());
    143     }
    144
    145     /**
    146      * Test of parse method with scan arg, of class CliParser.
    147      *
    148      * @throws Exception thrown when an exception occurs.
    149      */
    150     @Test
    151     public void testParse_scan() throws Exception {
    152
    153         String[] args = {"-scan"};
    154
    155         CliParser instance = new CliParser();
    156
    157         try {
    158             instance.parse(args);
    159         } catch (ParseException ex) {
    160             Assert.assertTrue(ex.getMessage().contains("Missing argument"));
    161         }
    162
    163         Assert.assertFalse(instance.isGetVersion());
    164         Assert.assertFalse(instance.isGetHelp());
    165         Assert.assertFalse(instance.isRunScan());
    166     }
    167
    168     /**
    169      * Test of parse method with jar arg, of class CliParser.
    170      *
    171      * @throws Exception thrown when an exception occurs.
    172      */
    173     @Test
    174     public void testParse_scan_unknownFile() throws Exception {
    175
    176         String[] args = {"-scan", "jar.that.does.not.exist", "-app", "test"};
    177
    178         CliParser instance = new CliParser();
    179         try {
    180             instance.parse(args);
    181         } catch (FileNotFoundException ex) {
    182             Assert.assertTrue(ex.getMessage().contains("Invalid 'scan' argument"));
    183         }
    184
    185         Assert.assertFalse(instance.isGetVersion());
    186         Assert.assertFalse(instance.isGetHelp());
    187         Assert.assertFalse(instance.isRunScan());
    188     }
    189
    190     /**
    191      * Test of parse method with jar arg, of class CliParser.
    192      *
    193      * @throws Exception thrown when an exception occurs.
    194      */
    195     @Test
    196     public void testParse_scan_withFileExists() throws Exception {
    197         File path = new File(this.getClass().getClassLoader().getResource("checkSumTest.file").getPath());
    198         String[] args = {"-scan", path.getCanonicalPath(), "-out", "./", "-app", "test"};
    199
    200         CliParser instance = new CliParser();
    201         instance.parse(args);
    202
    203         Assert.assertEquals(path.getCanonicalPath(), instance.getScanFiles()[0]);
    204
    205         Assert.assertFalse(instance.isGetVersion());
    206         Assert.assertFalse(instance.isGetHelp());
    207         Assert.assertTrue(instance.isRunScan());
    208     }
    209
    210     /**
    211      * Test of printVersionInfo, of class CliParser.
    212      *
    213      * @throws Exception thrown when an exception occurs.
    214      */
    215     @Test
    216     public void testParse_printVersionInfo() throws Exception {
    217
    218         PrintStream out = System.out;
    219         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    220         System.setOut(new PrintStream(baos));
    221
    222         CliParser instance = new CliParser();
    223         instance.printVersionInfo();
    224         try {
    225             baos.flush();
    226             String text = (new String(baos.toByteArray())).toLowerCase();
    227             String[] lines = text.split(System.getProperty("line.separator"));
    228             Assert.assertEquals(1, lines.length);
    229             Assert.assertTrue(text.contains("version"));
    230             Assert.assertTrue(!text.contains("unknown"));
    231         } catch (IOException ex) {
    232             System.setOut(out);
    233             Assert.fail("CliParser.printVersionInfo did not write anything to system.out.");
    234         } finally {
    235             System.setOut(out);
    236         }
    237     }
    238
    239     /**
    240      * Test of printHelp, of class CliParser.
    241      *
    242      * @throws Exception thrown when an exception occurs.
    243      */
    244     @Test
    245     public void testParse_printHelp() throws Exception {
    246
    247         PrintStream out = System.out;
    248         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    249         System.setOut(new PrintStream(baos));
    250
    251         CliParser instance = new CliParser();
    252         String[] args = {"-h"};
    253         instance.parse(args);
    254         instance.printHelp();
    255         args[0] = "-ah";
    256         instance.parse(args);
    257         instance.printHelp();
    258         try {
    259             baos.flush();
    260             String text = (new String(baos.toByteArray()));
    261             String[] lines = text.split(System.getProperty("line.separator"));
    262             Assert.assertTrue(lines[0].startsWith("usage: "));
    263             Assert.assertTrue((lines.length > 2));
    264         } catch (IOException ex) {
    265             System.setOut(out);
    266             Assert.fail("CliParser.printVersionInfo did not write anything to system.out.");
    267         } finally {
    268             System.setOut(out);
    269         }
    270     }
    271 }