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         Set<Identifier> result = instance.getIdentifiers();
189 
190         assertTrue(true); //this is just a getter setter pair.
191     }
192 
193     /**
194      * Test of setIdentifiers method, of class Dependency.
195      */
196     @Test
197     public void testSetIdentifiers() {
198         Set<Identifier> identifiers = null;
199         Dependency instance = new Dependency();
200         instance.setIdentifiers(identifiers);
201         assertTrue(true); //this is just a getter setter pair.
202     }
203 
204     /**
205      * Test of addIdentifier method, of class Dependency.
206      */
207     @Test
208     public void testAddIdentifier() {
209         String type = "cpe";
210         String value = "cpe:/a:apache:struts:2.1.2";
211         String url = "http://somewhere";
212         Identifier expResult = new Identifier(type, value, url);
213 
214         Dependency instance = new Dependency();
215         instance.addIdentifier(type, value, url);
216         assertEquals(1, instance.getIdentifiers().size());
217         assertTrue("Identifier doesn't contain expected result.", instance.getIdentifiers().contains(expResult));
218     }
219 
220     /**
221      * Test of getEvidence method, of class Dependency.
222      */
223     @Test
224     public void testGetEvidence() {
225         Dependency instance = new Dependency();
226         EvidenceCollection expResult = null;
227         EvidenceCollection result = instance.getEvidence();
228         assertTrue(true); //this is just a getter setter pair.
229     }
230 
231     /**
232      * Test of getEvidenceUsed method, of class Dependency.
233      */
234     @Test
235     public void testGetEvidenceUsed() {
236         Dependency instance = new Dependency();
237         String expResult = "used";
238 
239         instance.getProductEvidence().addEvidence("used", "used", "used", Confidence.HIGH);
240         instance.getProductEvidence().addEvidence("not", "not", "not", Confidence.MEDIUM);
241         for (Evidence e : instance.getProductEvidence().iterator(Confidence.HIGH)) {
242             String use = e.getValue();
243         }
244 
245         EvidenceCollection result = instance.getEvidenceUsed();
246 
247         assertEquals(1, result.size());
248         assertTrue(result.containsUsedString(expResult));
249     }
250 
251     /**
252      * Test of getVendorEvidence method, of class Dependency.
253      */
254     @Test
255     public void testGetVendorEvidence() {
256         Dependency instance = new Dependency();
257         EvidenceCollection expResult = null;
258         EvidenceCollection result = instance.getVendorEvidence();
259         assertTrue(true); //this is just a getter setter pair.
260     }
261 
262     /**
263      * Test of getProductEvidence method, of class Dependency.
264      */
265     @Test
266     public void testGetProductEvidence() {
267         Dependency instance = new Dependency();
268         EvidenceCollection expResult = null;
269         EvidenceCollection result = instance.getProductEvidence();
270         assertTrue(true); //this is just a getter setter pair.
271     }
272 
273     /**
274      * Test of getVersionEvidence method, of class Dependency.
275      */
276     @Test
277     public void testGetVersionEvidence() {
278         Dependency instance = new Dependency();
279         EvidenceCollection expResult = null;
280         EvidenceCollection result = instance.getVersionEvidence();
281         assertTrue(true); //this is just a getter setter pair.
282     }
283 
284     /**
285      * Test of addAsEvidence method, of class Dependency.
286      */
287     @Test
288     public void testAddAsEvidence() {
289         Dependency instance = new Dependency();
290         MavenArtifact mavenArtifact = new MavenArtifact("group", "artifact", "version", "url");
291         instance.addAsEvidence("pom", mavenArtifact, Confidence.HIGH);
292         assertTrue(instance.getEvidence().contains(Confidence.HIGH));
293         assertFalse(instance.getEvidence().getEvidence("pom", "groupid").isEmpty());
294         assertFalse(instance.getEvidence().getEvidence("pom", "artifactid").isEmpty());
295         assertFalse(instance.getEvidence().getEvidence("pom", "version").isEmpty());
296         assertFalse(instance.getIdentifiers().isEmpty());
297     }
298 
299     /**
300      * Test of addAsEvidence method, of class Dependency.
301      */
302     @Test
303     public void testAddAsEvidenceWithEmptyArtefact() {
304         Dependency instance = new Dependency();
305         MavenArtifact mavenArtifact = new MavenArtifact(null, null, null, null);
306         instance.addAsEvidence("pom", mavenArtifact, Confidence.HIGH);
307         assertFalse(instance.getEvidence().contains(Confidence.HIGH));
308         assertTrue(instance.getEvidence().getEvidence("pom", "groupid").isEmpty());
309         assertTrue(instance.getEvidence().getEvidence("pom", "artifactid").isEmpty());
310         assertTrue(instance.getEvidence().getEvidence("pom", "version").isEmpty());
311         assertTrue(instance.getIdentifiers().isEmpty());
312     }
313 }