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