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_unknown() throws Exception {
125
126 String[] args = {"-unknown"};
127
128 PrintStream out = System.out;
129 PrintStream err = System.err;
130 ByteArrayOutputStream baos_out = new ByteArrayOutputStream();
131 ByteArrayOutputStream baos_err = new ByteArrayOutputStream();
132 System.setOut(new PrintStream(baos_out));
133 System.setErr(new PrintStream(baos_err));
134
135 CliParser instance = new CliParser();
136
137 try {
138 instance.parse(args);
139 } catch (ParseException ex) {
140 Assert.assertTrue(ex.getMessage().contains("Unrecognized option"));
141 }
142 Assert.assertFalse(instance.isGetVersion());
143 Assert.assertFalse(instance.isGetHelp());
144 Assert.assertFalse(instance.isRunScan());
145 }
146
147
148
149
150
151
152 @Test
153 public void testParse_scan() throws Exception {
154
155 String[] args = {"-scan"};
156
157 CliParser instance = new CliParser();
158
159 try {
160 instance.parse(args);
161 } catch (ParseException ex) {
162 Assert.assertTrue(ex.getMessage().contains("Missing argument"));
163 }
164
165 Assert.assertFalse(instance.isGetVersion());
166 Assert.assertFalse(instance.isGetHelp());
167 Assert.assertFalse(instance.isRunScan());
168 }
169
170
171
172
173
174
175 @Test
176 public void testParse_scan_unknownFile() throws Exception {
177
178 String[] args = {"-scan", "jar.that.does.not.exist", "-app", "test"};
179
180 CliParser instance = new CliParser();
181 try {
182 instance.parse(args);
183 } catch (FileNotFoundException ex) {
184 Assert.assertTrue(ex.getMessage().contains("Invalid 'scan' argument"));
185 }
186
187 Assert.assertFalse(instance.isGetVersion());
188 Assert.assertFalse(instance.isGetHelp());
189 Assert.assertFalse(instance.isRunScan());
190 }
191
192
193
194
195
196
197 @Test
198 public void testParse_scan_withFileExists() throws Exception {
199 File path = new File(this.getClass().getClassLoader().getResource("checkSumTest.file").getPath());
200 String[] args = {"-scan", path.getCanonicalPath(), "-out", "./", "-app", "test"};
201
202 CliParser instance = new CliParser();
203 instance.parse(args);
204
205 Assert.assertEquals(path.getCanonicalPath(), instance.getScanFiles()[0]);
206
207 Assert.assertFalse(instance.isGetVersion());
208 Assert.assertFalse(instance.isGetHelp());
209 Assert.assertTrue(instance.isRunScan());
210 }
211
212
213
214
215
216
217 @Test
218 public void testParse_printVersionInfo() throws Exception {
219
220 PrintStream out = System.out;
221 ByteArrayOutputStream baos = new ByteArrayOutputStream();
222 System.setOut(new PrintStream(baos));
223
224 CliParser instance = new CliParser();
225 instance.printVersionInfo();
226 try {
227 baos.flush();
228 String text = (new String(baos.toByteArray())).toLowerCase();
229 String[] lines = text.split(System.getProperty("line.separator"));
230 Assert.assertEquals(1, lines.length);
231 Assert.assertTrue(text.contains("version"));
232 Assert.assertTrue(!text.contains("unknown"));
233 } catch (IOException ex) {
234 System.setOut(out);
235 Assert.fail("CliParser.printVersionInfo did not write anything to system.out.");
236 } finally {
237 System.setOut(out);
238 }
239 }
240
241
242
243
244
245
246 @Test
247 public void testParse_printHelp() throws Exception {
248
249 PrintStream out = System.out;
250 ByteArrayOutputStream baos = new ByteArrayOutputStream();
251 System.setOut(new PrintStream(baos));
252
253 CliParser instance = new CliParser();
254 String[] args = {"-h"};
255 instance.parse(args);
256 instance.printHelp();
257 args[0] = "-ah";
258 instance.parse(args);
259 instance.printHelp();
260 try {
261 baos.flush();
262 String text = (new String(baos.toByteArray()));
263 String[] lines = text.split(System.getProperty("line.separator"));
264 Assert.assertTrue(lines[0].startsWith("usage: "));
265 Assert.assertTrue((lines.length > 2));
266 } catch (IOException ex) {
267 System.setOut(out);
268 Assert.fail("CliParser.printVersionInfo did not write anything to system.out.");
269 } finally {
270 System.setOut(out);
271 }
272 }
273 }