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