Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/security/auth/login/LoginContext/DefaultHandler.java
38861 views
/*1* Copyright (c) 2000, 2001, 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*/2223/*24* @test25* @bug 437718126* @summary Provide default configurable CallbackHandlers27*28* @build DefaultHandler DefaultHandlerImpl DefaultHandlerModule29* @run main/othervm -Djava.security.auth.login.config=file:${test.src}/DefaultHandler.config DefaultHandler30*/3132import javax.security.auth.*;33import javax.security.auth.login.*;3435public class DefaultHandler {3637public static void main(String[] args) {3839// first test if a default is not provided.40// we should get an exception4142LoginContext lc = null;43try {44lc = new LoginContext("SampleLogin");45} catch (LoginException le) {46System.out.println47("DefaultHandler test failed - login construction failed");48throw new SecurityException(le.getMessage());49}5051try {52lc.login();53throw new SecurityException54("DefaultHandler test failed: got a handler!");55} catch (LoginException le) {56// good!57System.out.println58("Good: CallbackHandler implementation not found");59le.printStackTrace();60}6162// set the security property for the default handler63java.security.Security.setProperty("auth.login.defaultCallbackHandler",64"DefaultHandlerImpl");6566// now test to see if the default handler is picked up.67// this should succeed.6869LoginContext lc2 = null;70try {71lc2 = new LoginContext("SampleLogin");72} catch (LoginException le) {73System.out.println74("DefaultHandler test failed - constructing LoginContext");75throw new SecurityException(le.getMessage());76}7778try {79lc2.login();80} catch (LoginException le) {81System.out.println82("DefaultHandler test failed - login method");83throw new SecurityException(le.getMessage());84}8586System.out.println("DefaultHandler test succeeded");87}88}899091