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