test/jdk/javax/security/sasl/Sasl/DisabledMechanisms.java
changeset 54802 b0a1572ec64a
equal deleted inserted replaced
54801:ac8493c4a747 54802:b0a1572ec64a
       
     1 /*
       
     2  * Copyright (c) 2019, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8200400
       
    27  * @modules java.security.sasl
       
    28  * @library /test/lib
       
    29  * @run main/othervm DisabledMechanisms
       
    30  *      DIGEST-MD5 DIGEST-MD5
       
    31  * @run main/othervm -DdisabledMechanisms= DisabledMechanisms
       
    32  *      DIGEST-MD5 DIGEST-MD5
       
    33  * @run main/othervm -DdisabledMechanisms=DIGEST-MD5,NTLM DisabledMechanisms
       
    34  *      null null
       
    35  * @run main/othervm -DdisabledMechanisms=DIGEST-MD5 DisabledMechanisms
       
    36  *      NTLM null
       
    37  * @run main/othervm -DdisabledMechanisms=NTLM DisabledMechanisms
       
    38  *      DIGEST-MD5 DIGEST-MD5
       
    39  */
       
    40 
       
    41 import java.security.Security;
       
    42 import java.util.Collections;
       
    43 import java.util.Map;
       
    44 import javax.security.auth.callback.PasswordCallback;
       
    45 import javax.security.sasl.Sasl;
       
    46 import javax.security.sasl.SaslClient;
       
    47 import javax.security.sasl.SaslServer;
       
    48 import javax.security.auth.callback.Callback;
       
    49 import javax.security.auth.callback.CallbackHandler;
       
    50 
       
    51 import jdk.test.lib.Asserts;
       
    52 
       
    53 public class DisabledMechanisms {
       
    54 
       
    55     public static void main(String[] args) throws Exception {
       
    56 
       
    57         String authorizationId = "username";
       
    58         String protocol = "ldap";
       
    59         String serverName = "server1";
       
    60         Map props = Collections.emptyMap();
       
    61 
       
    62         String disabled = System.getProperty("disabledMechanisms");
       
    63         if (disabled != null) {
       
    64             Security.setProperty("jdk.sasl.disabledMechanisms", disabled);
       
    65         }
       
    66 
       
    67         CallbackHandler callbackHandler = callbacks -> {
       
    68             for (Callback cb : callbacks) {
       
    69                 if (cb instanceof PasswordCallback) {
       
    70                     ((PasswordCallback) cb).setPassword("password".toCharArray());
       
    71                 }
       
    72             }
       
    73         };
       
    74 
       
    75         SaslClient client = Sasl.createSaslClient(
       
    76                 new String[]{"DIGEST-MD5", "NTLM"}, authorizationId,
       
    77                 protocol, serverName, props, callbackHandler);
       
    78         Asserts.assertEQ(client == null ? null : client.getMechanismName(),
       
    79                 args[0].equals("null") ? null : args[0]);
       
    80 
       
    81         SaslServer server = Sasl.createSaslServer(
       
    82                 "DIGEST-MD5", protocol, serverName, props, callbackHandler);
       
    83         Asserts.assertEQ(server == null ? null : server.getMechanismName(),
       
    84                 args[1].equals("null") ? null : args[1]);
       
    85     }
       
    86 }