1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
59
60
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
81
82
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
100
101
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
118
119
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
147
148
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
170
171
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
192
193
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
212
213
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
241
242
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 }