J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @author Vincent Ryan |
| 27 | * @bug 4814522 |
| 28 | * @summary Check that a LdapLoginModule can be initialized using various |
| 29 | * options. |
| 30 | * (LdapLoginModule replaces the JndiLoginModule for LDAP access) |
| 31 | */ |
| 32 | |
| 33 | import java.io.IOException; |
| 34 | import java.util.Collections; |
| 35 | import java.util.Map; |
| 36 | import java.util.HashMap; |
| 37 | |
| 38 | import javax.security.auth.*; |
| 39 | import javax.security.auth.login.*; |
| 40 | import javax.security.auth.callback.*; |
| 41 | import com.sun.security.auth.module.LdapLoginModule; |
| 42 | |
| 43 | public class CheckOptions { |
| 44 | |
| 45 | private static final String USER_PROVIDER_OPTION = "UsErPrOvIdeR"; |
| 46 | |
| 47 | public static void main(String[] args) throws Exception { |
| 48 | init(); |
| 49 | testInvalidOptions(); |
| 50 | testNullCallbackHandler(); |
| 51 | testWithCallbackHandler(); |
| 52 | } |
| 53 | |
| 54 | private static void init() throws Exception { |
| 55 | } |
| 56 | |
| 57 | private static void testInvalidOptions() throws Exception { |
| 58 | |
| 59 | // empty set of options |
| 60 | |
| 61 | LdapLoginModule ldap = new LdapLoginModule(); |
| 62 | Subject subject = new Subject(); |
| 63 | ldap.initialize(subject, null, null, Collections.EMPTY_MAP); |
| 64 | |
| 65 | try { |
| 66 | ldap.login(); |
| 67 | throw new SecurityException("expected a LoginException"); |
| 68 | |
| 69 | } catch (LoginException le) { |
| 70 | // expected behaviour |
| 71 | System.out.println("Caught a LoginException, as expected"); |
| 72 | } |
| 73 | |
| 74 | // bad value for userProvider option |
| 75 | |
| 76 | Map<String, String> options = new HashMap<String, String>(); |
| 77 | options.put(USER_PROVIDER_OPTION, "ldap://localhost:23456"); |
| 78 | ldap.initialize(subject, null, null, options); |
| 79 | |
| 80 | try { |
| 81 | ldap.login(); |
| 82 | throw new SecurityException("expected a LoginException"); |
| 83 | |
| 84 | } catch (LoginException le) { |
| 85 | // expected behaviour |
| 86 | System.out.println("Caught a LoginException, as expected"); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private static void testNullCallbackHandler() throws Exception { |
| 91 | |
| 92 | // empty set of options |
| 93 | |
| 94 | LdapLoginModule ldap = new LdapLoginModule(); |
| 95 | Subject subject = new Subject(); |
| 96 | Map<String, String> options = new HashMap<String, String>(); |
| 97 | ldap.initialize(subject, null, null, options); |
| 98 | |
| 99 | try { |
| 100 | ldap.login(); |
| 101 | throw new SecurityException("expected LoginException"); |
| 102 | |
| 103 | } catch (LoginException le) { |
| 104 | // expected behaviour |
| 105 | System.out.println("Caught a LoginException, as expected"); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | private static void testWithCallbackHandler() throws Exception { |
| 110 | |
| 111 | LdapLoginModule ldap = new LdapLoginModule(); |
| 112 | Subject subject = new Subject(); |
| 113 | Map<String, String> options = new HashMap<String, String>(); |
| 114 | |
| 115 | CallbackHandler goodHandler = new MyCallbackHandler(true); |
| 116 | ldap.initialize(subject, goodHandler, null, options); |
| 117 | |
| 118 | try { |
| 119 | ldap.login(); |
| 120 | throw new SecurityException("expected LoginException"); |
| 121 | |
| 122 | } catch (LoginException le) { |
| 123 | // expected behaviour |
| 124 | System.out.println("Caught a LoginException, as expected"); |
| 125 | } |
| 126 | |
| 127 | CallbackHandler badHandler = new MyCallbackHandler(false); |
| 128 | ldap.initialize(subject, badHandler, null, options); |
| 129 | |
| 130 | try { |
| 131 | ldap.login(); |
| 132 | throw new SecurityException("expected LoginException"); |
| 133 | |
| 134 | } catch (LoginException le) { |
| 135 | // expected behaviour |
| 136 | System.out.println("Caught a LoginException, as expected"); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | private static class MyCallbackHandler implements CallbackHandler { |
| 141 | |
| 142 | private final boolean good; |
| 143 | |
| 144 | public MyCallbackHandler(boolean good) { |
| 145 | this.good = good; |
| 146 | } |
| 147 | |
| 148 | public void handle(Callback[] callbacks) |
| 149 | throws IOException, UnsupportedCallbackException { |
| 150 | |
| 151 | for (int i = 0; i < callbacks.length; i++) { |
| 152 | |
| 153 | if (callbacks[i] instanceof NameCallback) { |
| 154 | NameCallback nc = (NameCallback) callbacks[i]; |
| 155 | |
| 156 | if (good) { |
| 157 | nc.setName("foo"); |
| 158 | } else { |
| 159 | // do nothing |
| 160 | } |
| 161 | |
| 162 | } else if (callbacks[i] instanceof PasswordCallback) { |
| 163 | PasswordCallback pc = (PasswordCallback) callbacks[i]; |
| 164 | |
| 165 | if (good) { |
| 166 | pc.setPassword("foo".toCharArray()); |
| 167 | } else { |
| 168 | // do nothing |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |