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