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 The OWASP Foundatio. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.analyzer;
19  
20  import mockit.Mock;
21  import mockit.MockUp;
22  import org.junit.After;
23  import org.junit.Before;
24  import org.junit.Test;
25  import org.owasp.dependencycheck.BaseDBTestCase;
26  import org.owasp.dependencycheck.BaseTest;
27  import org.owasp.dependencycheck.Engine;
28  import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
29  import org.owasp.dependencycheck.dependency.Dependency;
30  import org.owasp.dependencycheck.exception.InitializationException;
31  
32  import java.io.File;
33  import java.security.MessageDigest;
34  import java.security.NoSuchAlgorithmException;
35  
36  import static org.junit.Assert.assertEquals;
37  import static org.junit.Assert.assertFalse;
38  import static org.junit.Assert.assertTrue;
39  
40  /**
41   * Unit tests for NodePackageAnalyzer.
42   *
43   * @author Dale Visser
44   */
45  public class ComposerLockAnalyzerTest extends BaseDBTestCase {
46  
47      /**
48       * The analyzer to test.
49       */
50      ComposerLockAnalyzer analyzer;
51  
52      /**
53       * Correctly setup the analyzer for testing.
54       *
55       * @throws Exception thrown if there is a problem
56       */
57      @Override
58      @Before
59      public void setUp() throws Exception {
60          super.setUp();
61          analyzer = new ComposerLockAnalyzer();
62          analyzer.setFilesMatched(true);
63          analyzer.initialize();
64      }
65  
66      /**
67       * Cleanup the analyzer's temp files, etc.
68       *
69       * @throws Exception thrown if there is a problem
70       */
71      @After
72      public void tearDown() throws Exception {
73          analyzer.close();
74          analyzer = null;
75      }
76  
77      /**
78       * Test of getName method, of class ComposerLockAnalyzer.
79       */
80      @Test
81      public void testGetName() {
82          assertEquals("Composer.lock analyzer", analyzer.getName());
83      }
84  
85      /**
86       * Test of supportsExtension method, of class ComposerLockAnalyzer.
87       */
88      @Test
89      public void testSupportsFiles() {
90          assertTrue(analyzer.accept(new File("composer.lock")));
91      }
92  
93      /**
94       * Test of inspect method, of class PythonDistributionAnalyzer.
95       *
96       * @throws AnalysisException is thrown when an exception occurs.
97       */
98      @Test
99      public void testAnalyzePackageJson() throws Exception {
100         final Engine engine = new Engine();
101         final Dependency result = new Dependency(BaseTest.getResourceAsFile(this,
102                 "composer.lock"));
103         analyzer.analyze(result, engine);
104     }
105 
106 
107     @Test(expected = InitializationException.class)
108     public void analyzerIsDisabledInCaseOfMissingMessageDigest() throws InitializationException {
109         new MockUp<MessageDigest>() {
110             @Mock
111             MessageDigest getInstance(String ignore) throws NoSuchAlgorithmException {
112                 throw new NoSuchAlgorithmException();
113             }
114         };
115 
116         analyzer = new ComposerLockAnalyzer();
117         analyzer.setFilesMatched(true);
118         assertTrue(analyzer.isEnabled());
119         analyzer.initialize();
120 
121         assertFalse(analyzer.isEnabled());
122     }
123 }