1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
57
58
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
79
80
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
98
99
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
116
117
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
145
146
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
168
169
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
190
191
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
210
211
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
239
240
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 }