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  * @library /lib/testlibrary
28  * @run main/othervm DisabledMechanisms
29  *      DIGEST-MD5 DIGEST-MD5
30  * @run main/othervm -DdisabledMechanisms= DisabledMechanisms
31  *      DIGEST-MD5 DIGEST-MD5
32  * @run main/othervm -DdisabledMechanisms=DIGEST-MD5,NTLM DisabledMechanisms
33  *      null null
34  * @run main/othervm -DdisabledMechanisms=DIGEST-MD5 DisabledMechanisms
35  *      NTLM null
36  * @run main/othervm -DdisabledMechanisms=NTLM DisabledMechanisms
37  *      DIGEST-MD5 DIGEST-MD5
38  */
39 
40 import java.security.Security;
41 import java.util.Collections;
42 import java.util.Map;
43 import javax.security.auth.callback.PasswordCallback;
44 import javax.security.sasl.Sasl;
45 import javax.security.sasl.SaslClient;
46 import javax.security.sasl.SaslServer;
47 import javax.security.auth.callback.Callback;
48 import javax.security.auth.callback.CallbackHandler;
49 
50 import jdk.testlibrary.Asserts;
51 
52 public class DisabledMechanisms {
53 
main(String[] args)54     public static void main(String[] args) throws Exception {
55 
56         String authorizationId = "username";
57         String protocol = "ldap";
58         String serverName = "server1";
59         Map props = Collections.emptyMap();
60 
61         String disabled = System.getProperty("disabledMechanisms");
62         if (disabled != null) {
63             Security.setProperty("jdk.sasl.disabledMechanisms", disabled);
64         }
65 
66         CallbackHandler callbackHandler = callbacks -> {
67             for (Callback cb : callbacks) {
68                 if (cb instanceof PasswordCallback) {
69                     ((PasswordCallback) cb).setPassword("password".toCharArray());
70                 }
71             }
72         };
73 
74         SaslClient client = Sasl.createSaslClient(
75                 new String[]{"DIGEST-MD5", "NTLM"}, authorizationId,
76                 protocol, serverName, props, callbackHandler);
77         Asserts.assertEQ(client == null ? null : client.getMechanismName(),
78                 args[0].equals("null") ? null : args[0]);
79 
80         SaslServer server = Sasl.createSaslServer(
81                 "DIGEST-MD5", protocol, serverName, props, callbackHandler);
82         Asserts.assertEQ(server == null ? null : server.getMechanismName(),
83                 args[1].equals("null") ? null : args[1]);
84     }
85 }
86