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) 2015 Institute for Defense Analyses. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.analyzer;
19  
20  import org.junit.After;
21  import org.junit.Before;
22  import org.junit.Test;
23  import org.owasp.dependencycheck.BaseTest;
24  import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
25  import org.owasp.dependencycheck.dependency.Dependency;
26  import org.owasp.dependencycheck.dependency.Evidence;
27  
28  import java.io.File;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertTrue;
32  
33  /**
34   * Unit tests for PythonPackageAnalyzer.
35   *
36   * @author Dale Visser
37   */
38  public class PythonPackageAnalyzerTest extends BaseTest {
39  
40      /**
41       * The package analyzer to test.
42       */
43      private PythonPackageAnalyzer analyzer;
44  
45      /**
46       * Setup the {@link PythonPackageAnalyzer}.
47       *
48       * @throws Exception if there is a problem
49       */
50      @Before
51      public void setUp() throws Exception {
52          analyzer = new PythonPackageAnalyzer();
53          analyzer.setFilesMatched(true);
54          analyzer.initialize();
55      }
56  
57      /**
58       * Cleanup any resources used.
59       *
60       * @throws Exception if there is a problem
61       */
62      @After
63      public void tearDown() throws Exception {
64          analyzer.close();
65          analyzer = null;
66      }
67  
68      /**
69       * Test of getName method, of class PythonPackageAnalyzer.
70       */
71      @Test
72      public void testGetName() {
73          assertEquals("Analyzer name wrong.", "Python Package Analyzer",
74                  analyzer.getName());
75      }
76  
77      /**
78       * Test of supportsExtension method, of class PythonPackageAnalyzer.
79       */
80      @Test
81      public void testSupportsFileExtension() {
82          assertTrue("Should support \"py\" extension.",
83                  analyzer.accept(new File("test.py")));
84      }
85  
86      @Test
87      public void testAnalyzeSourceMetadata() throws AnalysisException {
88          boolean found = false;
89          final Dependency result = new Dependency(BaseTest.getResourceAsFile(
90                  this, "python/eggtest/__init__.py"));
91          analyzer.analyze(result, null);
92          assertTrue("Expected vendor evidence to contain \"example\".", result
93                  .getVendorEvidence().toString().contains("example"));
94          for (final Evidence e : result.getVersionEvidence()) {
95              if ("0.0.1".equals(e.getValue())) {
96                  found = true;
97                  break;
98              }
99          }
100         assertTrue("Version 0.0.1 not found in EggTest dependency.", found);
101     }
102 
103 }