Path: blob/master/test/jdk/javax/security/auth/login/modules/JaasModularDefaultHandlerTest.java
51695 views
/*1* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.nio.file.Files;24import java.nio.file.Path;25import java.nio.file.Paths;26import java.nio.file.StandardCopyOption;27import java.util.Collections;28import java.util.LinkedList;29import java.util.List;30import java.io.File;31import java.io.OutputStream;32import java.lang.module.ModuleDescriptor;33import java.lang.module.ModuleDescriptor.Builder;34import jdk.internal.module.ModuleInfoWriter;35import java.util.stream.Stream;36import jdk.test.lib.process.ProcessTools;37import jdk.test.lib.process.OutputAnalyzer;38import jdk.test.lib.util.JarUtils;3940/*41* @test42* @bug 8151654 818331043* @summary Test default callback handler with all possible modular option.44* @library /test/lib45* @modules java.base/jdk.internal.module46* @build jdk.test.lib.util.JarUtils47* @build TestCallbackHandler TestLoginModule JaasClientWithDefaultHandler48* @run main JaasModularDefaultHandlerTest49*/50public class JaasModularDefaultHandlerTest {5152private static final Path SRC = Paths.get(System.getProperty("test.src"));53private static final Path TEST_CLASSES54= Paths.get(System.getProperty("test.classes"));55private static final Path ARTIFACT_DIR = Paths.get("jars");56private static final String PS = File.pathSeparator;57private static final String H_TYPE = "handler.TestCallbackHandler";58private static final String C_TYPE = "login.JaasClientWithDefaultHandler";5960/**61* Here is the naming convention followed for each jar.62* h.jar - Unnamed handler jar.63* mh.jar - Modular handler jar.64* c.jar - Unnamed client jar.65* mc.jar - Modular client jar.66* amc.jar - Modular client used for automatic handler jar.67*/68private static final Path H_JAR = artifact("h.jar");69private static final Path MH_JAR = artifact("mh.jar");70private static final Path C_JAR = artifact("c.jar");71private static final Path MC_JAR = artifact("mc.jar");72private static final Path AMC_JAR = artifact("amc.jar");7374private final String unnH;75private final String modH;76private final String unnC;77private final String modC;78private final String autoMC;79// Common set of VM arguments used in all test cases80private final List<String> commonArgs;8182public JaasModularDefaultHandlerTest() {8384List<String> argList = new LinkedList<>();85argList.add("-Djava.security.auth.login.config="86+ toAbsPath(SRC.resolve("jaas.conf")));87commonArgs = Collections.unmodifiableList(argList);8889// Based on Testcase, select unnamed/modular jar files to use.90unnH = toAbsPath(H_JAR);91modH = toAbsPath(MH_JAR);92unnC = toAbsPath(C_JAR);93modC = toAbsPath(MC_JAR);94autoMC = toAbsPath(AMC_JAR);95}9697/*98* Test cases are based on the following logic,99* for (clientType : {"NAMED", "AUTOMATIC", "UNNAMED"}) {100* for (handlerType : {"NAMED", "AUTOMATIC", "UNNAMED"}) {101* Create and run java command for each possible case102* }103* }104*/105public static void main(String[] args) throws Exception {106107// Generates unnamed and modular jars.108setUp();109JaasModularDefaultHandlerTest jt = new JaasModularDefaultHandlerTest();110jt.process();111}112113private void process() throws Exception {114115// Case: NAMED-NAMED, NAMED-AUTOMATIC, NAMED-UNNAMED116System.out.println("Case: Modular Client and Modular Handler");117execute(String.format("--module-path %s%s%s -m mc/%s %s",118modC, PS, modH, C_TYPE, H_TYPE));119System.out.println("Case: Modular Client and automatic Handler");120execute(String.format("--module-path %s%s%s --add-modules=h -m mc/%s %s",121autoMC, PS, unnH, C_TYPE, H_TYPE));122System.out.println("Case: Modular Client and unnamed Handler");123execute(String.format("--module-path %s -cp %s -m mc/%s %s", autoMC,124unnH, C_TYPE, H_TYPE));125126// Case: AUTOMATIC-NAMED, AUTOMATIC-AUTOMATIC, AUTOMATIC-UNNAMED127System.out.println("Case: Automatic Client and modular Handler");128execute(String.format("--module-path %s%s%s --add-modules=mh -m c/%s %s",129unnC, PS, modH, C_TYPE, H_TYPE));130System.out.println("Case: Automatic Client and automatic Handler");131execute(String.format("--module-path %s%s%s --add-modules=h -m c/%s %s",132unnC, PS, unnH, C_TYPE, H_TYPE));133System.out.println("Case: Automatic Client and unnamed Handler");134execute(String.format("--module-path %s -cp %s -m c/%s %s", unnC,135unnH, C_TYPE, H_TYPE));136137// Case: UNNAMED-NAMED, UNNAMED-AUTOMATIC, UNNAMED-UNNAMED138System.out.println("Case: Unnamed Client and modular Handler");139execute(String.format("-cp %s --module-path %s --add-modules=mh %s %s",140unnC, modH, C_TYPE, H_TYPE));141System.out.println("Case: Unnamed Client and automatic Handler");142execute(String.format("-cp %s --module-path %s --add-modules=h %s %s",143unnC, unnH, C_TYPE, H_TYPE));144System.out.println("Case: Unnamed Client and unnamed Handler");145execute(String.format("-cp %s%s%s %s %s", unnC, PS, unnH, C_TYPE,146H_TYPE));147148// Case: unnamed jars in --module-path and modular jars in -cp.149System.out.println("Case: Unnamed Client and Handler in modulepath");150execute(String.format("--module-path %s%s%s --add-modules=h -m c/%s %s",151unnC, PS, unnH, C_TYPE, H_TYPE));152System.out.println("Case: Modular Client and Provider in classpath");153execute(String.format("-cp %s%s%s %s %s",154modC, PS, modH, C_TYPE, H_TYPE));155}156157/**158* Execute with command arguments and process the result.159*/160private void execute(String args) throws Exception {161162String[] safeArgs = Stream.concat(commonArgs.stream(),163Stream.of(args.split("\\s+"))).filter(s -> {164if (s.contains(" ")) {165throw new RuntimeException("No spaces in args");166}167return !s.isEmpty();168}).toArray(String[]::new);169OutputAnalyzer out = ProcessTools.executeTestJvm(safeArgs);170// Handle response.171if (out.getExitValue() != 0) {172System.out.printf("OUTPUT: %s", out.getOutput());173throw new RuntimeException("FAIL: Unknown failure occured.");174} else {175System.out.println("Passed.");176}177}178179/**180* Creates Unnamed/modular jar files for TestClient and TestClassLoader.181*/182private static void setUp() throws Exception {183184if (ARTIFACT_DIR.toFile().exists()) {185System.out.println("Skipping setup: Artifacts already exists.");186return;187}188// Generate unnamed handler jar file.189JarUtils.createJarFile(H_JAR, TEST_CLASSES,190"handler/TestCallbackHandler.class");191// Generate unnamed client jar file.192JarUtils.createJarFile(C_JAR, TEST_CLASSES,193"login/TestLoginModule.class",194"login/JaasClientWithDefaultHandler.class");195196Builder mBuilder = ModuleDescriptor.newModule("mh");197// Modular jar exports package to let the handler type accessible.198generateJar(H_JAR, MH_JAR, mBuilder.exports("handler").build());199200mBuilder = ModuleDescriptor.newModule("mc").exports("login")201.requires("jdk.security.auth");202// Generate modular client jar file to use automatic handler jar.203generateJar(C_JAR, AMC_JAR, mBuilder.build());204// Generate modular client jar file to use modular handler jar.205generateJar(C_JAR, MC_JAR, mBuilder.requires("mh").build());206}207208/**209* Update Unnamed jars and include module descriptor files.210*/211private static void generateJar(Path sjar, Path djar,212ModuleDescriptor mDesc) throws Exception {213214Files.copy(sjar, djar, StandardCopyOption.REPLACE_EXISTING);215Path dir = Files.createTempDirectory("tmp");216if (mDesc != null) {217Path mi = dir.resolve("module-info.class");218try (OutputStream out = Files.newOutputStream(mi)) {219ModuleInfoWriter.write(mDesc, out);220}221System.out.format("Added 'module-info.class' in '%s'%n", djar);222}223JarUtils.updateJarFile(djar, dir);224}225226/**227* Look for file path in generated jars.228*/229private static Path artifact(String file) {230return ARTIFACT_DIR.resolve(file);231}232233/**234* Convert to absolute file path.235*/236private static String toAbsPath(Path path) {237return path.toFile().getAbsolutePath();238}239}240241242