View Javadoc
1   /*
2    * This file is part of dependency-check-core.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
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   * @author Jeremy Long
37   */
38  public class DependencyTest extends BaseTest {
39  
40      /**
41       * Test of getFileName method, of class Dependency.
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       * Test of setFileName method, of class Dependency.
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       * Test of setActualFilePath method, of class Dependency.
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       * Test of getActualFilePath method, of class Dependency.
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       * Test of setFilePath method, of class Dependency.
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      * Test of getFilePath method, of class Dependency.
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      * Test of getMd5sum method, of class Dependency.
113      */
114     @Test
115     public void testGetMd5sum() {
116         //File file = new File(this.getClass().getClassLoader().getResource("struts2-core-2.1.2.jar").getPath());
117         File file = BaseTest.getResourceAsFile(this, "struts2-core-2.1.2.jar");
118 
119         Dependency instance = new Dependency(file);
120         //assertEquals("89CE9E36AA9A9E03F1450936D2F4F8DD0F961F8B", result.getSha1sum());
121         //String expResult = "C30B57142E1CCBC1EFD5CD15F307358F";
122         String expResult = "c30b57142e1ccbc1efd5cd15f307358f";
123         String result = instance.getMd5sum();
124         assertEquals(expResult, result);
125     }
126 
127     /**
128      * Test of setMd5sum method, of class Dependency.
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      * Test of getSha1sum method, of class Dependency.
140      */
141     @Test
142     public void testGetSha1sum() {
143         //File file = new File(this.getClass().getClassLoader().getResource("struts2-core-2.1.2.jar").getPath());
144         File file = BaseTest.getResourceAsFile(this, "struts2-core-2.1.2.jar");
145         Dependency instance = new Dependency(file);
146         //String expResult = "89CE9E36AA9A9E03F1450936D2F4F8DD0F961F8B";
147         String expResult = "89ce9e36aa9a9e03f1450936d2f4f8dd0f961f8b";
148         String result = instance.getSha1sum();
149         assertEquals(expResult, result);
150     }
151 
152     /**
153      * Test of setSha1sum method, of class Dependency.
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      * Test of getIdentifiers method, of class Dependency.
165      */
166     @Test
167     public void testGetIdentifiers() {
168         Dependency instance = new Dependency();
169         Set<Identifier> result = instance.getIdentifiers();
170 
171         assertTrue(true); //this is just a getter setter pair.
172     }
173 
174     /**
175      * Test of setIdentifiers method, of class Dependency.
176      */
177     @Test
178     public void testSetIdentifiers() {
179         Set<Identifier> identifiers = null;
180         Dependency instance = new Dependency();
181         instance.setIdentifiers(identifiers);
182         assertTrue(true); //this is just a getter setter pair.
183     }
184 
185     /**
186      * Test of addIdentifier method, of class Dependency.
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      * Test of getEvidence method, of class Dependency.
203      */
204     @Test
205     public void testGetEvidence() {
206         Dependency instance = new Dependency();
207         EvidenceCollection expResult = null;
208         EvidenceCollection result = instance.getEvidence();
209         assertTrue(true); //this is just a getter setter pair.
210     }
211 
212     /**
213      * Test of getEvidenceUsed method, of class Dependency.
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      * Test of getVendorEvidence method, of class Dependency.
234      */
235     @Test
236     public void testGetVendorEvidence() {
237         Dependency instance = new Dependency();
238         EvidenceCollection expResult = null;
239         EvidenceCollection result = instance.getVendorEvidence();
240         assertTrue(true); //this is just a getter setter pair.
241     }
242 
243     /**
244      * Test of getProductEvidence method, of class Dependency.
245      */
246     @Test
247     public void testGetProductEvidence() {
248         Dependency instance = new Dependency();
249         EvidenceCollection expResult = null;
250         EvidenceCollection result = instance.getProductEvidence();
251         assertTrue(true); //this is just a getter setter pair.
252     }
253 
254     /**
255      * Test of getVersionEvidence method, of class Dependency.
256      */
257     @Test
258     public void testGetVersionEvidence() {
259         Dependency instance = new Dependency();
260         EvidenceCollection expResult = null;
261         EvidenceCollection result = instance.getVersionEvidence();
262         assertTrue(true); //this is just a getter setter pair.
263     }
264 
265     /**
266      * Test of addAsEvidence method, of class Dependency.
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      * Test of addAsEvidence method, of class Dependency.
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 }