jdk/test/com/sun/security/auth/module/LdapLoginModule/CheckOptions.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7970 af1579474d16
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7970
diff changeset
     2
 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @author Vincent Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @bug 4814522
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Check that a LdapLoginModule can be initialized using various
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          options.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *          (LdapLoginModule replaces the JndiLoginModule for LDAP access)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.security.auth.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.security.auth.login.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.security.auth.callback.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import com.sun.security.auth.module.LdapLoginModule;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class CheckOptions {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final String USER_PROVIDER_OPTION = "UsErPrOvIdeR";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        testInvalidOptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        testNullCallbackHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        testWithCallbackHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static void init() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static void testInvalidOptions() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        // empty set of options
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        LdapLoginModule ldap = new LdapLoginModule();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        Subject subject = new Subject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        ldap.initialize(subject, null, null, Collections.EMPTY_MAP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            ldap.login();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            throw new SecurityException("expected a LoginException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        } catch (LoginException le) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            // expected behaviour
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            System.out.println("Caught a LoginException, as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        // bad value for userProvider option
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
    76
        Map<String, String> options = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        options.put(USER_PROVIDER_OPTION, "ldap://localhost:23456");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        ldap.initialize(subject, null, null, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            ldap.login();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throw new SecurityException("expected a LoginException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } catch (LoginException le) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            // expected behaviour
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            System.out.println("Caught a LoginException, as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static void testNullCallbackHandler() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // empty set of options
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        LdapLoginModule ldap = new LdapLoginModule();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        Subject subject = new Subject();
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
    96
        Map<String, String> options = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        ldap.initialize(subject, null, null, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            ldap.login();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new SecurityException("expected LoginException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        } catch (LoginException le) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            // expected behaviour
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            System.out.println("Caught a LoginException, as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static void testWithCallbackHandler() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        LdapLoginModule ldap = new LdapLoginModule();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        Subject subject = new Subject();
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   113
        Map<String, String> options = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        CallbackHandler goodHandler = new MyCallbackHandler(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        ldap.initialize(subject, goodHandler, null, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            ldap.login();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new SecurityException("expected LoginException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        } catch (LoginException le) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            // expected behaviour
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            System.out.println("Caught a LoginException, as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        CallbackHandler badHandler = new MyCallbackHandler(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        ldap.initialize(subject, badHandler, null, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            ldap.login();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new SecurityException("expected LoginException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } catch (LoginException le) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            // expected behaviour
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            System.out.println("Caught a LoginException, as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private static class MyCallbackHandler implements CallbackHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        private final boolean good;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        public MyCallbackHandler(boolean good) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            this.good = good;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        public void handle(Callback[] callbacks)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                throws IOException, UnsupportedCallbackException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            for (int i = 0; i < callbacks.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                if (callbacks[i] instanceof NameCallback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    NameCallback nc = (NameCallback) callbacks[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    if (good) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        nc.setName("foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        // do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                } else if (callbacks[i] instanceof PasswordCallback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    PasswordCallback pc = (PasswordCallback) callbacks[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    if (good) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        pc.setPassword("foo".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        // do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
}