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