|
17 | 17 | package org.apache.tomcat.util.scan; |
18 | 18 |
|
19 | 19 | import java.io.File; |
| 20 | +import java.io.FileOutputStream; |
20 | 21 | import java.io.IOException; |
21 | 22 | import java.net.MalformedURLException; |
22 | 23 | import java.net.URL; |
23 | 24 | import java.net.URLClassLoader; |
24 | 25 | import java.util.ArrayList; |
| 26 | +import java.util.Deque; |
25 | 27 | import java.util.List; |
| 28 | +import java.util.jar.Attributes; |
| 29 | +import java.util.jar.JarOutputStream; |
| 30 | +import java.util.jar.Manifest; |
26 | 31 |
|
27 | 32 | import org.junit.Assert; |
28 | 33 | import org.junit.Assume; |
@@ -103,6 +108,57 @@ public ClassLoader getClassLoader() { |
103 | 108 | scanner.scan(JarScanType.PLUGGABILITY, context, callback); |
104 | 109 | } |
105 | 110 |
|
| 111 | + @Test |
| 112 | + public void testScanManifestDefault() throws Exception { |
| 113 | + Assert.assertTrue("Referenced JAR from manifest Class-Path should be scanned", |
| 114 | + doTestScanManifest(true)); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testScanManifestDisabled() throws Exception { |
| 119 | + Assert.assertFalse("Referenced JAR from manifest Class-Path should not be scanned", |
| 120 | + doTestScanManifest(false)); |
| 121 | + } |
| 122 | + |
| 123 | + private boolean doTestScanManifest(boolean scanManifest) throws Exception { |
| 124 | + File referencedJar = new File(System.getProperty("java.io.tmpdir"), "referenced.jar"); |
| 125 | + referencedJar.deleteOnExit(); |
| 126 | + JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(referencedJar), new Manifest()); |
| 127 | + jarOutputStream.close(); |
| 128 | + |
| 129 | + File testJar = new File(System.getProperty("java.io.tmpdir"), "manifest-test.jar"); |
| 130 | + testJar.deleteOnExit(); |
| 131 | + |
| 132 | + Manifest manifest = new Manifest(); |
| 133 | + manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); |
| 134 | + manifest.getMainAttributes().put(Attributes.Name.CLASS_PATH, "referenced.jar"); |
| 135 | + |
| 136 | + jarOutputStream = new JarOutputStream(new FileOutputStream(testJar), manifest); |
| 137 | + jarOutputStream.close(); |
| 138 | + |
| 139 | + StandardJarScanner scanner = new StandardJarScanner() { |
| 140 | + @Override |
| 141 | + protected void addClassPath(Deque<URL> classPathUrlsToProcess) { |
| 142 | + super.addClassPath(classPathUrlsToProcess); |
| 143 | + try { |
| 144 | + classPathUrlsToProcess.add(testJar.toURI().toURL()); |
| 145 | + } catch (MalformedURLException e) { |
| 146 | + throw new RuntimeException(e); |
| 147 | + } |
| 148 | + } |
| 149 | + }; |
| 150 | + scanner.setScanManifest(scanManifest); |
| 151 | + |
| 152 | + LoggingCallback callback = new LoggingCallback(); |
| 153 | + scanner.scan(JarScanType.PLUGGABILITY, new TesterServletContext(), callback); |
| 154 | + |
| 155 | + for (String cb : callback.callbacks) { |
| 156 | + if (cb.contains("referenced.jar")) { |
| 157 | + return true; |
| 158 | + } |
| 159 | + } |
| 160 | + return false; |
| 161 | + } |
106 | 162 |
|
107 | 163 | private static class LoggingCallback implements JarScannerCallback { |
108 | 164 |
|
|
0 commit comments