Path: blob/master/test/jdk/com/sun/security/auth/module/LdapLoginModule/CheckOptions.java
51748 views
/*1* Copyright (c) 2005, 2011, 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* @author Vincent Ryan26* @bug 481452227* @summary Check that a LdapLoginModule can be initialized using various28* options.29* (LdapLoginModule replaces the JndiLoginModule for LDAP access)30*/3132import java.io.IOException;33import java.util.Collections;34import java.util.Map;35import java.util.HashMap;3637import javax.security.auth.*;38import javax.security.auth.login.*;39import javax.security.auth.callback.*;40import com.sun.security.auth.module.LdapLoginModule;4142public class CheckOptions {4344private static final String USER_PROVIDER_OPTION = "UsErPrOvIdeR";4546public static void main(String[] args) throws Exception {47init();48testInvalidOptions();49testNullCallbackHandler();50testWithCallbackHandler();51}5253private static void init() throws Exception {54}5556private static void testInvalidOptions() throws Exception {5758// empty set of options5960LdapLoginModule ldap = new LdapLoginModule();61Subject subject = new Subject();62ldap.initialize(subject, null, null, Collections.EMPTY_MAP);6364try {65ldap.login();66throw new SecurityException("expected a LoginException");6768} catch (LoginException le) {69// expected behaviour70System.out.println("Caught a LoginException, as expected");71}7273// bad value for userProvider option7475Map<String, String> options = new HashMap<>();76options.put(USER_PROVIDER_OPTION, "ldap://localhost:23456");77ldap.initialize(subject, null, null, options);7879try {80ldap.login();81throw new SecurityException("expected a LoginException");8283} catch (LoginException le) {84// expected behaviour85System.out.println("Caught a LoginException, as expected");86}87}8889private static void testNullCallbackHandler() throws Exception {9091// empty set of options9293LdapLoginModule ldap = new LdapLoginModule();94Subject subject = new Subject();95Map<String, String> options = new HashMap<>();96ldap.initialize(subject, null, null, options);9798try {99ldap.login();100throw new SecurityException("expected LoginException");101102} catch (LoginException le) {103// expected behaviour104System.out.println("Caught a LoginException, as expected");105}106}107108private static void testWithCallbackHandler() throws Exception {109110LdapLoginModule ldap = new LdapLoginModule();111Subject subject = new Subject();112Map<String, String> options = new HashMap<>();113114CallbackHandler goodHandler = new MyCallbackHandler(true);115ldap.initialize(subject, goodHandler, null, options);116117try {118ldap.login();119throw new SecurityException("expected LoginException");120121} catch (LoginException le) {122// expected behaviour123System.out.println("Caught a LoginException, as expected");124}125126CallbackHandler badHandler = new MyCallbackHandler(false);127ldap.initialize(subject, badHandler, null, options);128129try {130ldap.login();131throw new SecurityException("expected LoginException");132133} catch (LoginException le) {134// expected behaviour135System.out.println("Caught a LoginException, as expected");136}137}138139private static class MyCallbackHandler implements CallbackHandler {140141private final boolean good;142143public MyCallbackHandler(boolean good) {144this.good = good;145}146147public void handle(Callback[] callbacks)148throws IOException, UnsupportedCallbackException {149150for (int i = 0; i < callbacks.length; i++) {151152if (callbacks[i] instanceof NameCallback) {153NameCallback nc = (NameCallback) callbacks[i];154155if (good) {156nc.setName("foo");157} else {158// do nothing159}160161} else if (callbacks[i] instanceof PasswordCallback) {162PasswordCallback pc = (PasswordCallback) callbacks[i];163164if (good) {165pc.setPassword("foo".toCharArray());166} else {167// do nothing168}169}170}171}172}173}174175176