View Javadoc
1   /*
2    * Copyright 2015 OWASP.
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  package org.owasp.dependencycheck.analyzer;
17  
18  import java.io.File;
19  import java.lang.reflect.InvocationTargetException;
20  import java.lang.reflect.Method;
21  import java.util.logging.Level;
22  import java.util.logging.Logger;
23  
24  import org.junit.Before;
25  import org.junit.Test;
26  import static org.junit.Assert.*;
27  import static org.junit.Assume.assumeFalse;
28  
29  import org.owasp.dependencycheck.BaseTest;
30  import org.owasp.dependencycheck.utils.Settings;
31  
32  /**
33   *
34   * @author jeremy
35   */
36  public class ArchiveAnalyzerTest extends BaseTest {
37  
38      @Before
39      public void setUp() {
40          Settings.setString(Settings.KEYS.ADDITIONAL_ZIP_EXTENSIONS, "z2, z3");
41      }
42  
43      /**
44       * Test of analyzeDependency method, of class ArchiveAnalyzer.
45       */
46      @Test
47      public void testZippableExtensions() throws Exception {
48          assumeFalse(isPreviouslyLoaded("org.owasp.dependencycheck.analyzer.ArchiveAnalyzer"));
49          ArchiveAnalyzer instance = new ArchiveAnalyzer();
50          assertTrue(instance.getFileFilter().accept(new File("c:/test.zip")));
51          assertTrue(instance.getFileFilter().accept(new File("c:/test.z2")));
52          assertTrue(instance.getFileFilter().accept(new File("c:/test.z3")));
53          assertFalse(instance.getFileFilter().accept(new File("c:/test.z4")));
54      }
55  
56      private boolean isPreviouslyLoaded(String className) {
57          try {
58              Method m = ClassLoader.class.getDeclaredMethod("findLoadedClass", new Class[]{String.class});
59              m.setAccessible(true);
60              Object t = m.invoke(Thread.currentThread().getContextClassLoader(), className);
61              return t != null;
62          } catch (NoSuchMethodException ex) {
63              Logger.getLogger(ArchiveAnalyzerTest.class.getName()).log(Level.SEVERE, null, ex);
64          } catch (SecurityException ex) {
65              Logger.getLogger(ArchiveAnalyzerTest.class.getName()).log(Level.SEVERE, null, ex);
66          } catch (IllegalAccessException ex) {
67              Logger.getLogger(ArchiveAnalyzerTest.class.getName()).log(Level.SEVERE, null, ex);
68          } catch (IllegalArgumentException ex) {
69              Logger.getLogger(ArchiveAnalyzerTest.class.getName()).log(Level.SEVERE, null, ex);
70          } catch (InvocationTargetException ex) {
71              Logger.getLogger(ArchiveAnalyzerTest.class.getName()).log(Level.SEVERE, null, ex);
72          }
73          return false;
74      }
75  }