1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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 }