|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @library /test/lib .. ./cases/modules |
| 27 | + * @build JNativeScanTestBase |
| 28 | + * org.singlejar/* org.lib/* org.myapp/* org.service/* |
| 29 | + * cases.classpath.singlejar.main.Main |
| 30 | + * cases.classpath.lib.Lib |
| 31 | + * cases.classpath.app.App |
| 32 | + * cases.classpath.unnamed_package.UnnamedPackage |
| 33 | + * @run junit TestJNativeScan |
| 34 | + */ |
| 35 | + |
| 36 | +import jdk.test.lib.process.OutputAnalyzer; |
| 37 | +import jdk.test.lib.util.JarUtils; |
| 38 | +import org.junit.jupiter.api.BeforeAll; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | + |
| 41 | +import java.io.File; |
| 42 | +import java.io.IOException; |
| 43 | +import java.nio.file.Path; |
| 44 | +import java.util.HashSet; |
| 45 | +import java.util.Set; |
| 46 | +import java.util.jar.Attributes; |
| 47 | +import java.util.jar.Manifest; |
| 48 | + |
| 49 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 50 | + |
| 51 | +class TestJNativeScan extends JNativeScanTestBase { |
| 52 | + |
| 53 | + static Path TEST_CLASSES; |
| 54 | + |
| 55 | + static Path CLASS_PATH_APP; |
| 56 | + static Path SINGLE_JAR_CLASS_PATH; |
| 57 | + static Path SINGLE_JAR_MODULAR; |
| 58 | + static Path ORG_MYAPP; |
| 59 | + static Path ORG_LIB; |
| 60 | + static Path UNNAMED_PACKAGE_JAR; |
| 61 | + static Path LIB_JAR; |
| 62 | + |
| 63 | + @BeforeAll |
| 64 | + public static void before() throws IOException { |
| 65 | + SINGLE_JAR_CLASS_PATH = Path.of("singleJar.jar"); |
| 66 | + TEST_CLASSES = Path.of(System.getProperty("test.classes", "")); |
| 67 | + JarUtils.createJarFile(SINGLE_JAR_CLASS_PATH, TEST_CLASSES, Path.of("main", "Main.class")); |
| 68 | + |
| 69 | + LIB_JAR = Path.of("lib.jar"); |
| 70 | + JarUtils.createJarFile(LIB_JAR, TEST_CLASSES, Path.of("lib", "Lib.class")); |
| 71 | + Manifest manifest = new Manifest(); |
| 72 | + Attributes mainAttrs = manifest.getMainAttributes(); |
| 73 | + mainAttrs.put(Attributes.Name.MANIFEST_VERSION, "1.0"); // need version or other attributes will be ignored |
| 74 | + mainAttrs.putValue("Class-Path", "lib.jar non-existent.jar"); |
| 75 | + CLASS_PATH_APP = Path.of("app.jar"); |
| 76 | + JarUtils.createJarFile(CLASS_PATH_APP, manifest, TEST_CLASSES, Path.of("app", "App.class")); |
| 77 | + |
| 78 | + SINGLE_JAR_MODULAR = makeModularJar("org.singlejar"); |
| 79 | + ORG_MYAPP = makeModularJar("org.myapp"); |
| 80 | + ORG_LIB = makeModularJar("org.lib"); |
| 81 | + makeModularJar("org.service"); |
| 82 | + |
| 83 | + UNNAMED_PACKAGE_JAR = Path.of("unnamed_package.jar"); |
| 84 | + JarUtils.createJarFile(UNNAMED_PACKAGE_JAR, TEST_CLASSES, Path.of("UnnamedPackage.class")); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testSingleJarClassPath() { |
| 89 | + assertSuccess(jnativescan("--class-path", SINGLE_JAR_CLASS_PATH.toString())) |
| 90 | + .stderrShouldBeEmpty() |
| 91 | + .stdoutShouldContain("ALL-UNNAMED") |
| 92 | + .stdoutShouldContain("main.Main") |
| 93 | + .stdoutShouldContain("main.Main::m()void is a native method declaration") |
| 94 | + .stdoutShouldContain("main.Main::main(String[])void references restricted methods") |
| 95 | + .stdoutShouldContain("java.lang.foreign.MemorySegment::reinterpret(long)MemorySegment"); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testSingleJarModulePath() { |
| 100 | + assertSuccess(jnativescan("--module-path", MODULE_PATH, "--add-modules", "org.singlejar")) |
| 101 | + .stderrShouldBeEmpty() |
| 102 | + .stdoutShouldContain("org.singlejar") |
| 103 | + .stdoutShouldContain("org.singlejar.main.Main") |
| 104 | + .stdoutShouldContain("org.singlejar.main.Main::m()void is a native method declaration") |
| 105 | + .stdoutShouldContain("org.singlejar.main.Main::main(String[])void references restricted methods") |
| 106 | + .stdoutShouldContain("java.lang.foreign.MemorySegment::reinterpret(long)MemorySegment"); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testWithDepModule() { |
| 111 | + assertSuccess(jnativescan("--module-path", MODULE_PATH, "--add-modules", "org.myapp")) |
| 112 | + .stderrShouldBeEmpty() |
| 113 | + .stdoutShouldContain("org.lib") |
| 114 | + .stdoutShouldContain("org.lib.Lib") |
| 115 | + .stdoutShouldContain("org.lib.Lib::m()void is a native method declaration") |
| 116 | + .stdoutShouldContain("org.lib.Lib::doIt()void references restricted methods") |
| 117 | + .stdoutShouldContain("java.lang.foreign.MemorySegment::reinterpret(long)MemorySegment") |
| 118 | + .stdoutShouldContain("org.service") |
| 119 | + .stdoutShouldContain("org.service.ServiceImpl") |
| 120 | + .stdoutShouldContain("org.service.ServiceImpl::m()void is a native method declaration") |
| 121 | + .stdoutShouldContain("org.service.ServiceImpl::doIt()void references restricted methods") |
| 122 | + .stdoutShouldContain("java.lang.foreign.MemorySegment::reinterpret(long)MemorySegment"); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testAllModulePath() { |
| 127 | + assertSuccess(jnativescan("--module-path", MODULE_PATH, "--add-modules", "ALL-MODULE-PATH")) |
| 128 | + .stderrShouldBeEmpty() |
| 129 | + .stdoutShouldContain("org.singlejar") |
| 130 | + .stdoutShouldContain("org.lib") |
| 131 | + .stdoutShouldContain("org.service"); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void testClassPathAttribute() { |
| 136 | + assertSuccess(jnativescan("--class-path", CLASS_PATH_APP.toString())) |
| 137 | + .stderrShouldBeEmpty() |
| 138 | + .stdoutShouldContain("ALL-UNNAMED") |
| 139 | + .stdoutShouldContain("lib.Lib") |
| 140 | + .stdoutShouldContain("lib.Lib::m()void is a native method declaration") |
| 141 | + .stdoutShouldContain("lib.Lib::doIt()void references restricted methods") |
| 142 | + .stdoutShouldContain("java.lang.foreign.MemorySegment::reinterpret(long)MemorySegment"); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void testInvalidRelease() { |
| 147 | + assertFailure(jnativescan("--module-path", MODULE_PATH, "--add-modules", "ALL-MODULE-PATH", "--release", "asdf")) |
| 148 | + .stderrShouldContain("Invalid release"); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testReleaseNotSupported() { |
| 153 | + assertFailure(jnativescan("--module-path", MODULE_PATH, "--add-modules", "ALL-MODULE-PATH", "--release", "9999999")) |
| 154 | + .stderrShouldContain("Release: 9999999 not supported"); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void testFileDoesNotExist() { |
| 159 | + assertFailure(jnativescan("--class-path", "non-existent.jar")) |
| 160 | + .stderrShouldContain("Path does not appear to be a jar file, or directory containing classes"); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void testModuleNotAJarFile() { |
| 165 | + String modulePath = moduleRoot("org.myapp").toString() + File.pathSeparator + ORG_LIB.toString(); |
| 166 | + assertSuccess(jnativescan("--module-path", modulePath, |
| 167 | + "--add-modules", "ALL-MODULE-PATH")) |
| 168 | + .stdoutShouldContain("lib.Lib") |
| 169 | + .stdoutShouldContain("lib.Lib::m()void is a native method declaration") |
| 170 | + .stdoutShouldContain("lib.Lib::doIt()void references restricted methods") |
| 171 | + .stdoutShouldContain("java.lang.foreign.MemorySegment::reinterpret(long)MemorySegment"); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void testPrintNativeAccess() { |
| 176 | + assertSuccess(jnativescan("--module-path", MODULE_PATH, |
| 177 | + "-add-modules", "org.singlejar,org.myapp", |
| 178 | + "--print-native-access")) |
| 179 | + .stdoutShouldMatch("org.lib,org.service,org.singlejar"); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + public void testNoDuplicateNames() { |
| 184 | + String classPath = SINGLE_JAR_CLASS_PATH + File.pathSeparator + CLASS_PATH_APP; |
| 185 | + OutputAnalyzer output = assertSuccess(jnativescan("--class-path", classPath, "--print-native-access")); |
| 186 | + String[] moduleNames = output.getStdout().split(","); |
| 187 | + Set<String> names = new HashSet<>(); |
| 188 | + for (String name : moduleNames) { |
| 189 | + assertTrue(names.add(name.strip())); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + public void testUnnamedPackage() { |
| 195 | + assertSuccess(jnativescan("--class-path", UNNAMED_PACKAGE_JAR.toString())) |
| 196 | + .stderrShouldBeEmpty() |
| 197 | + .stdoutShouldContain("ALL-UNNAMED") |
| 198 | + .stdoutShouldNotContain(".UnnamedPackage") |
| 199 | + .stdoutShouldContain("UnnamedPackage") |
| 200 | + .stdoutShouldContain("UnnamedPackage::m()void is a native method declaration") |
| 201 | + .stdoutShouldContain("UnnamedPackage::main(String[])void references restricted methods") |
| 202 | + .stdoutShouldContain("java.lang.foreign.MemorySegment::reinterpret(long)MemorySegment"); |
| 203 | + } |
| 204 | + |
| 205 | + @Test |
| 206 | + public void testPositionalArguments() { |
| 207 | + assertFailure(jnativescan("foo")) |
| 208 | + .stdoutShouldBeEmpty() |
| 209 | + .stderrShouldContain("jnativescan does not accept positional arguments"); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + public void testMissingRootModules() { |
| 214 | + assertFailure(jnativescan("--module-path", MODULE_PATH)) |
| 215 | + .stdoutShouldBeEmpty() |
| 216 | + .stderrShouldContain("Missing required option(s) [add-modules]"); |
| 217 | + } |
| 218 | + |
| 219 | + @Test |
| 220 | + public void testClassPathDirectory() { |
| 221 | + assertSuccess(jnativescan("--class-path", TEST_CLASSES.toString())) |
| 222 | + .stderrShouldBeEmpty() |
| 223 | + .stdoutShouldContain("ALL-UNNAMED") |
| 224 | + .stdoutShouldContain("UnnamedPackage") |
| 225 | + .stdoutShouldContain("UnnamedPackage::m()void is a native method declaration") |
| 226 | + .stdoutShouldContain("UnnamedPackage::main(String[])void references restricted methods") |
| 227 | + .stdoutShouldContain("main.Main") |
| 228 | + .stdoutShouldContain("main.Main::m()void is a native method declaration") |
| 229 | + .stdoutShouldContain("main.Main::main(String[])void references restricted methods") |
| 230 | + .stdoutShouldContain("lib.Lib") |
| 231 | + .stdoutShouldContain("lib.Lib::m()void is a native method declaration") |
| 232 | + .stdoutShouldContain("lib.Lib::doIt()void references restricted methods") |
| 233 | + .stdoutShouldContain("java.lang.foreign.MemorySegment::reinterpret(long)MemorySegment"); |
| 234 | + } |
| 235 | + |
| 236 | + @Test |
| 237 | + public void testMultipleClassPathJars() { |
| 238 | + // make sure all of these are reported, even when they are all in the ALL-UNNAMED module |
| 239 | + String classPath = UNNAMED_PACKAGE_JAR |
| 240 | + + File.pathSeparator + SINGLE_JAR_CLASS_PATH |
| 241 | + + File.pathSeparator + LIB_JAR; |
| 242 | + assertSuccess(jnativescan("--class-path", classPath)) |
| 243 | + .stderrShouldBeEmpty() |
| 244 | + .stdoutShouldContain("ALL-UNNAMED") |
| 245 | + .stdoutShouldContain("UnnamedPackage") |
| 246 | + .stdoutShouldContain(UNNAMED_PACKAGE_JAR.toString()) |
| 247 | + .stdoutShouldContain("lib.Lib") |
| 248 | + .stdoutShouldContain(LIB_JAR.toString()) |
| 249 | + .stdoutShouldContain("main.Main") |
| 250 | + .stdoutShouldContain(SINGLE_JAR_CLASS_PATH.toString()); |
| 251 | + } |
| 252 | +} |
0 commit comments