Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/security/auth/login/LoginContext/LCTest.java
38862 views
/*1* Copyright (c) 2015, 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 com.sun.security.auth.UnixPrincipal;2425import javax.security.auth.Subject;26import javax.security.auth.callback.*;27import javax.security.auth.login.FailedLoginException;28import javax.security.auth.login.LoginContext;29import javax.security.auth.login.LoginException;30import javax.security.auth.spi.LoginModule;31import java.io.IOException;32import java.security.Principal;33import java.util.ArrayList;34import java.util.List;35import java.util.Map;3637/*38* @test39* @bug 805046040* @summary Test checks that proper methods associated with login/logout process41* of LoginContext are called for different configurations and circumstances.42* @modules jdk.security.auth43*44* @run main/othervm LCTest EmptyModuleConfig false45* @run main/othervm LCTest IncorrectName false46* @run main/othervm LCTest AbortRequisite false abort47* @run main/othervm LCTest AbortSufficient false abort48* @run main/othervm LCTest AbortRequired false abort49* @run main/othervm LCTest LogoutRequisite false logout50* @run main/othervm LCTest LogoutSufficient true logout51* @run main/othervm LCTest LogoutRequired false logout52* @run main/othervm LCTest LoginRequisite false login53* @run main/othervm LCTest LoginSufficient true login54* @run main/othervm LCTest LoginRequired false login55*/5657public class LCTest {5859private static final String USER_NAME = "testUser";60private static final String PASSWORD = "testPassword";61private static final List<String> loggedActions = new ArrayList<>();6263static {64System.setProperty("java.security.auth.login.config",65System.getProperty("test.src")66+ System.getProperty("file.separator")67+ "LCTest.jaas.config");68}6970public static void main(String[] args) {71if (args.length < 2) {72throw new RuntimeException("Incorrect test params");73}74String nameOfContext = args[0];75boolean isPositive = Boolean.parseBoolean(args[1]);76String actionName = null;77if (args.length == 3) {78actionName = args[2];79}80try {81LoginContext lc = new LoginContext(nameOfContext,82new MyCallbackHandler());83lc.login();84checkPrincipal(lc, true);85lc.logout();86checkPrincipal(lc, false);87if (!isPositive) {88throw new RuntimeException("Test failed. Exception expected.");89}90} catch (LoginException le) {91if (isPositive) {92throw new RuntimeException("Test failed. Unexpected " +93"exception", le);94}95System.out.println("Expected exception: "96+ le.getMessage());97}98checkActions(actionName);99System.out.println("Test passed.");100}101102/*103* Log action from login modules104*/105private static void logAction(String actionName) {106loggedActions.add(actionName);107}108109/*110* Check if logged actions are as expected. We always expected 3 actions111* if any.112*/113private static void checkActions(String actionName) {114if (actionName == null) {115if (loggedActions.size() != 0) {116throw new RuntimeException("No logged actions expected");117}118} else {119int loggedActionsFound = 0;120System.out.println("Logged actions : " + loggedActions);121for (String s : loggedActions) {122if (s.equals(actionName)) {123loggedActionsFound++;124}125}126if (loggedActionsFound != 3) {127throw new RuntimeException("Incorrect number of actions " +128actionName + " : " + loggedActionsFound);129}130}131}132133/*134* Check context for principal of the test user.135*/136private static void checkPrincipal(LoginContext loginContext, boolean137principalShouldExist) {138if (!principalShouldExist) {139if (loginContext.getSubject().getPrincipals().size() != 0) {140throw new RuntimeException("Test failed. Principal was not " +141"cleared.");142}143} else {144for (Principal p : loginContext.getSubject().getPrincipals()) {145if (p instanceof UnixPrincipal &&146USER_NAME.equals(p.getName())) {147//Proper principal was found, return.148return;149}150}151throw new RuntimeException("Test failed. UnixPrincipal "152+ USER_NAME + " expected.");153}154}155156private static class MyCallbackHandler implements CallbackHandler {157158@Override159public void handle(Callback[] callbacks) throws IOException,160UnsupportedCallbackException {161for (Callback callback : callbacks) {162if (callback instanceof NameCallback) {163((NameCallback) callback).setName(USER_NAME);164} else if (callback instanceof PasswordCallback) {165((PasswordCallback) callback).setPassword(166PASSWORD.toCharArray());167} else {168throw new UnsupportedCallbackException(callback);169}170}171}172}173174/* -------------------------------------------------------------------------175* Test login modules176* -------------------------------------------------------------------------177*/178179/*180* Login module that should pass through all phases.181*/182public static class LoginModuleAllPass extends LoginModuleBase {183184}185186/*187* Login module that throws Exception in abort method.188*/189public static class LoginModuleWithAbortException extends LoginModuleBase {190191@Override192public boolean abort() throws LoginException {193super.abort();194throw new LoginException("Abort failed!");195}196}197198/*199* Login module that throws Exception in login method.200*/201public static class LoginModuleWithLoginException extends LoginModuleBase {202203@Override204public boolean login() throws LoginException {205super.login();206throw new FailedLoginException("Login failed!");207}208}209210/*211* Login module that throws Exception in logout method.212*/213public static class LoginModuleWithLogoutException extends LoginModuleBase {214215@Override216public boolean logout() throws LoginException {217super.logout();218throw new FailedLoginException("Logout failed!");219}220}221222/*223* Base class for login modules224*/225public static abstract class LoginModuleBase implements LoginModule {226// initial state227private Subject subject;228private CallbackHandler callbackHandler;229private Map sharedState;230private Map options;231private UnixPrincipal userPrincipal;232233// username and password234private String username;235private String password;236237// the authentication status238private boolean succeeded = false;239private boolean commitSucceeded = false;240241@Override242public void initialize(Subject subject, CallbackHandler callbackHandler,243Map<String, ?> sharedState, Map<String, ?> options) {244245this.subject = subject;246this.callbackHandler = callbackHandler;247this.sharedState = sharedState;248this.options = options;249System.out.println("Login module initialized.");250}251252/*253* Authenticate the user by prompting for a username and password.254*/255@Override256public boolean login() throws LoginException {257LCTest.logAction("login");258if (callbackHandler == null) {259throw new LoginException("No CallbackHandler available");260}261262Callback[] callbacks = new Callback[2];263callbacks[0] = new NameCallback("Username: ");264callbacks[1] = new PasswordCallback("Password: ", false);265266try {267callbackHandler.handle(callbacks);268username = ((NameCallback) callbacks[0]).getName();269password = new String(((PasswordCallback) callbacks[1])270.getPassword());271if (username.equals(LCTest.USER_NAME) &&272password.equals(LCTest.PASSWORD)) {273succeeded = true;274return true;275}276throw new FailedLoginException("Incorrect username/password!");277} catch (IOException | UnsupportedCallbackException e) {278throw new LoginException("Login failed: " + e.getMessage());279}280}281282@Override283public boolean commit() throws LoginException {284LCTest.logAction("commit");285if (succeeded == false) {286return false;287}288userPrincipal = new UnixPrincipal(username);289final Subject s = subject;290final UnixPrincipal up = userPrincipal;291java.security.AccessController.doPrivileged292((java.security.PrivilegedAction) () -> {293if (!s.getPrincipals().contains(up)) {294s.getPrincipals().add(up);295}296return null;297});298password = null;299commitSucceeded = true;300return true;301}302303@Override304public boolean abort() throws LoginException {305LCTest.logAction("abort");306if (succeeded == false) {307return false;308}309clearState();310return true;311}312313@Override314public boolean logout() throws LoginException {315LCTest.logAction("logout");316clearState();317return true;318}319320private void clearState() {321if (commitSucceeded) {322final Subject s = subject;323final UnixPrincipal up = userPrincipal;324java.security.AccessController.doPrivileged325((java.security.PrivilegedAction) () -> {326s.getPrincipals().remove(up);327return null;328});329}330username = null;331password = null;332userPrincipal = null;333}334}335336}337338339