1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.dependency;
19
20 import java.io.File;
21 import java.util.List;
22 import java.util.Set;
23 import org.junit.After;
24 import org.junit.AfterClass;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.owasp.dependencycheck.BaseTest;
32 import org.owasp.dependencycheck.data.nexus.MavenArtifact;
33
34
35
36
37
38 public class DependencyTest extends BaseTest {
39
40
41
42
43 @Test
44 public void testGetFileName() {
45 Dependency instance = new Dependency();
46 String expResult = "filename";
47 instance.setFileName(expResult);
48 String result = instance.getFileName();
49 assertEquals(expResult, result);
50 }
51
52
53
54
55 @Test
56 public void testSetFileName() {
57 String fileName = "file.tar";
58 Dependency instance = new Dependency();
59 instance.setFileName(fileName);
60 assertEquals(fileName, instance.getFileName());
61 }
62
63
64
65
66 @Test
67 public void testSetActualFilePath() {
68 String actualFilePath = "file.tar";
69 Dependency instance = new Dependency();
70 instance.setSha1sum("non-null value");
71 instance.setActualFilePath(actualFilePath);
72 assertEquals(actualFilePath, instance.getActualFilePath());
73 }
74
75
76
77
78 @Test
79 public void testGetActualFilePath() {
80 Dependency instance = new Dependency();
81 String expResult = "file.tar";
82 instance.setSha1sum("non-null value");
83 instance.setActualFilePath(expResult);
84 String result = instance.getActualFilePath();
85 assertEquals(expResult, result);
86 }
87
88
89
90
91 @Test
92 public void testSetFilePath() {
93 String filePath = "file.tar";
94 Dependency instance = new Dependency();
95 instance.setFilePath(filePath);
96 assertEquals(filePath, instance.getFilePath());
97 }
98
99
100
101
102 @Test
103 public void testGetFilePath() {
104 Dependency instance = new Dependency();
105 String expResult = "file.tar";
106 instance.setFilePath(expResult);
107 String result = instance.getFilePath();
108 assertEquals(expResult, result);
109 }
110
111
112
113
114 @Test
115 public void testGetMd5sum() {
116
117 File file = BaseTest.getResourceAsFile(this, "struts2-core-2.1.2.jar");
118
119 Dependency instance = new Dependency(file);
120
121
122 String expResult = "c30b57142e1ccbc1efd5cd15f307358f";
123 String result = instance.getMd5sum();
124 assertEquals(expResult, result);
125 }
126
127
128
129
130 @Test
131 public void testSetMd5sum() {
132 String md5sum = "test";
133 Dependency instance = new Dependency();
134 instance.setMd5sum(md5sum);
135 assertEquals(md5sum, instance.getMd5sum());
136 }
137
138
139
140
141 @Test
142 public void testGetSha1sum() {
143
144 File file = BaseTest.getResourceAsFile(this, "struts2-core-2.1.2.jar");
145 Dependency instance = new Dependency(file);
146
147 String expResult = "89ce9e36aa9a9e03f1450936d2f4f8dd0f961f8b";
148 String result = instance.getSha1sum();
149 assertEquals(expResult, result);
150 }
151
152
153
154
155 @Test
156 public void testSetSha1sum() {
157 String sha1sum = "test";
158 Dependency instance = new Dependency();
159 instance.setSha1sum(sha1sum);
160 assertEquals(sha1sum, instance.getSha1sum());
161 }
162
163
164
165
166 @Test
167 public void testGetIdentifiers() {
168 Dependency instance = new Dependency();
169 Set<Identifier> result = instance.getIdentifiers();
170
171 assertTrue(true);
172 }
173
174
175
176
177 @Test
178 public void testSetIdentifiers() {
179 Set<Identifier> identifiers = null;
180 Dependency instance = new Dependency();
181 instance.setIdentifiers(identifiers);
182 assertTrue(true);
183 }
184
185
186
187
188 @Test
189 public void testAddIdentifier() {
190 String type = "cpe";
191 String value = "cpe:/a:apache:struts:2.1.2";
192 String url = "http://somewhere";
193 Identifier expResult = new Identifier(type, value, url);
194
195 Dependency instance = new Dependency();
196 instance.addIdentifier(type, value, url);
197 assertEquals(1, instance.getIdentifiers().size());
198 assertTrue("Identifier doesn't contain expected result.", instance.getIdentifiers().contains(expResult));
199 }
200
201
202
203
204 @Test
205 public void testGetEvidence() {
206 Dependency instance = new Dependency();
207 EvidenceCollection expResult = null;
208 EvidenceCollection result = instance.getEvidence();
209 assertTrue(true);
210 }
211
212
213
214
215 @Test
216 public void testGetEvidenceUsed() {
217 Dependency instance = new Dependency();
218 String expResult = "used";
219
220 instance.getProductEvidence().addEvidence("used", "used", "used", Confidence.HIGH);
221 instance.getProductEvidence().addEvidence("not", "not", "not", Confidence.MEDIUM);
222 for (Evidence e : instance.getProductEvidence().iterator(Confidence.HIGH)) {
223 String use = e.getValue();
224 }
225
226 EvidenceCollection result = instance.getEvidenceUsed();
227
228 assertEquals(1, result.size());
229 assertTrue(result.containsUsedString(expResult));
230 }
231
232
233
234
235 @Test
236 public void testGetVendorEvidence() {
237 Dependency instance = new Dependency();
238 EvidenceCollection expResult = null;
239 EvidenceCollection result = instance.getVendorEvidence();
240 assertTrue(true);
241 }
242
243
244
245
246 @Test
247 public void testGetProductEvidence() {
248 Dependency instance = new Dependency();
249 EvidenceCollection expResult = null;
250 EvidenceCollection result = instance.getProductEvidence();
251 assertTrue(true);
252 }
253
254
255
256
257 @Test
258 public void testGetVersionEvidence() {
259 Dependency instance = new Dependency();
260 EvidenceCollection expResult = null;
261 EvidenceCollection result = instance.getVersionEvidence();
262 assertTrue(true);
263 }
264
265
266
267
268 @Test
269 public void testAddAsEvidence() {
270 Dependency instance = new Dependency();
271 MavenArtifact mavenArtifact = new MavenArtifact("group", "artifact", "version", "url");
272 instance.addAsEvidence("pom", mavenArtifact, Confidence.HIGH);
273 assertTrue(instance.getEvidence().contains(Confidence.HIGH));
274 assertFalse(instance.getEvidence().getEvidence("pom", "groupid").isEmpty());
275 assertFalse(instance.getEvidence().getEvidence("pom", "artifactid").isEmpty());
276 assertFalse(instance.getEvidence().getEvidence("pom", "version").isEmpty());
277 assertFalse(instance.getIdentifiers().isEmpty());
278 }
279
280
281
282
283 @Test
284 public void testAddAsEvidenceWithEmptyArtefact() {
285 Dependency instance = new Dependency();
286 MavenArtifact mavenArtifact = new MavenArtifact(null, null, null, null);
287 instance.addAsEvidence("pom", mavenArtifact, Confidence.HIGH);
288 assertFalse(instance.getEvidence().contains(Confidence.HIGH));
289 assertTrue(instance.getEvidence().getEvidence("pom", "groupid").isEmpty());
290 assertTrue(instance.getEvidence().getEvidence("pom", "artifactid").isEmpty());
291 assertTrue(instance.getEvidence().getEvidence("pom", "version").isEmpty());
292 assertTrue(instance.getIdentifiers().isEmpty());
293 }
294 }