1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck;
19
20 import org.owasp.dependencycheck.CliParser;
21 import java.io.ByteArrayOutputStream;
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.PrintStream;
26 import org.apache.commons.cli.ParseException;
27 import org.junit.After;
28 import org.junit.AfterClass;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.owasp.dependencycheck.utils.Settings;
34
35
36
37
38
39 public class CliParserTest {
40
41 @BeforeClass
42 public static void setUpClass() throws Exception {
43 Settings.initialize();
44 }
45
46 @AfterClass
47 public static void tearDownClass() throws Exception {
48 Settings.cleanup(true);
49 }
50
51 @Before
52 public void setUp() throws Exception {
53 }
54
55 @After
56 public void tearDown() throws Exception {
57 }
58
59
60
61
62
63
64 @Test
65 public void testParse() throws Exception {
66
67 String[] args = {};
68 PrintStream out = System.out;
69
70 ByteArrayOutputStream baos = new ByteArrayOutputStream();
71 System.setOut(new PrintStream(baos));
72
73 CliParser instance = new CliParser();
74 instance.parse(args);
75
76 Assert.assertFalse(instance.isGetVersion());
77 Assert.assertFalse(instance.isGetHelp());
78 Assert.assertFalse(instance.isRunScan());
79 }
80
81
82
83
84
85
86 @Test
87 public void testParse_help() throws Exception {
88
89 String[] args = {"-help"};
90 PrintStream out = System.out;
91
92 CliParser instance = new CliParser();
93 instance.parse(args);
94
95 Assert.assertFalse(instance.isGetVersion());
96 Assert.assertTrue(instance.isGetHelp());
97 Assert.assertFalse(instance.isRunScan());
98 }
99
100
101
102
103
104
105 @Test
106 public void testParse_version() throws Exception {
107
108 String[] args = {"-version"};
109
110 CliParser instance = new CliParser();
111 instance.parse(args);
112 Assert.assertTrue(instance.isGetVersion());
113 Assert.assertFalse(instance.isGetHelp());
114 Assert.assertFalse(instance.isRunScan());
115
116 }
117
118
119
120
121
122
123 @Test
124 public void testParse_failOnCVSSNoArg() throws Exception {
125
126 String[] args = {"--failOnCVSS"};
127
128 CliParser instance = new CliParser();
129 try {
130 instance.parse(args);
131 } catch (ParseException ex) {
132 Assert.assertTrue(ex.getMessage().contains("Missing argument"));
133 }
134 Assert.assertFalse(instance.isGetVersion());
135 Assert.assertFalse(instance.isGetHelp());
136 Assert.assertFalse(instance.isRunScan());
137 }
138
139
140
141
142
143
144 @Test
145 public void testParse_failOnCVSSInvalidArgument() throws Exception {
146
147 String[] args = {"--failOnCVSS","bad"};
148
149 CliParser instance = new CliParser();
150 instance.parse(args);
151 Assert.assertEquals("Default should be 11", 11, instance.getFailOnCVSS());
152 Assert.assertFalse(instance.isGetVersion());
153 Assert.assertFalse(instance.isGetHelp());
154 Assert.assertFalse(instance.isRunScan());
155 }
156
157
158
159
160
161
162 @Test
163 public void testParse_failOnCVSSValidArgument() throws Exception {
164
165 String[] args = {"--failOnCVSS","6"};
166
167 CliParser instance = new CliParser();
168 instance.parse(args);
169 Assert.assertEquals(6, instance.getFailOnCVSS());
170 Assert.assertFalse(instance.isGetVersion());
171 Assert.assertFalse(instance.isGetHelp());
172 Assert.assertFalse(instance.isRunScan());
173 }
174
175
176
177
178
179
180 @Test
181 public void testParse_unknown() throws Exception {
182
183 String[] args = {"-unknown"};
184
185 PrintStream out = System.out;
186 PrintStream err = System.err;
187 ByteArrayOutputStream baos_out = new ByteArrayOutputStream();
188 ByteArrayOutputStream baos_err = new ByteArrayOutputStream();
189 System.setOut(new PrintStream(baos_out));
190 System.setErr(new PrintStream(baos_err));
191
192 CliParser instance = new CliParser();
193
194 try {
195 instance.parse(args);
196 } catch (ParseException ex) {
197 Assert.assertTrue(ex.getMessage().contains("Unrecognized option"));
198 }
199 Assert.assertFalse(instance.isGetVersion());
200 Assert.assertFalse(instance.isGetHelp());
201 Assert.assertFalse(instance.isRunScan());
202 }
203
204
205
206
207
208
209 @Test
210 public void testParse_scan() throws Exception {
211
212 String[] args = {"-scan"};
213
214 CliParser instance = new CliParser();
215
216 try {
217 instance.parse(args);
218 } catch (ParseException ex) {
219 Assert.assertTrue(ex.getMessage().contains("Missing argument"));
220 }
221
222 Assert.assertFalse(instance.isGetVersion());
223 Assert.assertFalse(instance.isGetHelp());
224 Assert.assertFalse(instance.isRunScan());
225 }
226
227
228
229
230
231
232 @Test
233 public void testParse_scan_unknownFile() throws Exception {
234
235 String[] args = {"-scan", "jar.that.does.not.exist", "-app", "test"};
236
237 CliParser instance = new CliParser();
238 try {
239 instance.parse(args);
240 } catch (FileNotFoundException ex) {
241 Assert.assertTrue(ex.getMessage().contains("Invalid 'scan' argument"));
242 }
243
244 Assert.assertFalse(instance.isGetVersion());
245 Assert.assertFalse(instance.isGetHelp());
246 Assert.assertFalse(instance.isRunScan());
247 }
248
249
250
251
252
253
254 @Test
255 public void testParse_scan_withFileExists() throws Exception {
256 File path = new File(this.getClass().getClassLoader().getResource("checkSumTest.file").toURI().getPath());
257 String[] args = {"-scan", path.getCanonicalPath(), "-out", "./", "-app", "test"};
258
259 CliParser instance = new CliParser();
260 instance.parse(args);
261
262 Assert.assertEquals(path.getCanonicalPath(), instance.getScanFiles()[0]);
263
264 Assert.assertFalse(instance.isGetVersion());
265 Assert.assertFalse(instance.isGetHelp());
266 Assert.assertTrue(instance.isRunScan());
267 }
268
269
270
271
272
273
274 @Test
275 public void testParse_printVersionInfo() throws Exception {
276
277 PrintStream out = System.out;
278 ByteArrayOutputStream baos = new ByteArrayOutputStream();
279 System.setOut(new PrintStream(baos));
280
281 CliParser instance = new CliParser();
282 instance.printVersionInfo();
283 try {
284 baos.flush();
285 String text = (new String(baos.toByteArray())).toLowerCase();
286 String[] lines = text.split(System.getProperty("line.separator"));
287 Assert.assertEquals(1, lines.length);
288 Assert.assertTrue(text.contains("version"));
289 Assert.assertTrue(!text.contains("unknown"));
290 } catch (IOException ex) {
291 System.setOut(out);
292 Assert.fail("CliParser.printVersionInfo did not write anything to system.out.");
293 } finally {
294 System.setOut(out);
295 }
296 }
297
298
299
300
301
302
303 @Test
304 public void testParse_printHelp() throws Exception {
305
306 PrintStream out = System.out;
307 ByteArrayOutputStream baos = new ByteArrayOutputStream();
308 System.setOut(new PrintStream(baos));
309
310 CliParser instance = new CliParser();
311 String[] args = {"-h"};
312 instance.parse(args);
313 instance.printHelp();
314 args[0] = "-ah";
315 instance.parse(args);
316 instance.printHelp();
317 try {
318 baos.flush();
319 String text = (new String(baos.toByteArray()));
320 String[] lines = text.split(System.getProperty("line.separator"));
321 Assert.assertTrue(lines[0].startsWith("usage: "));
322 Assert.assertTrue((lines.length > 2));
323 } catch (IOException ex) {
324 System.setOut(out);
325 Assert.fail("CliParser.printVersionInfo did not write anything to system.out.");
326 } finally {
327 System.setOut(out);
328 }
329 }
330 }