AuthRealmChoices.java revision 7088:81be41c7323f
118334Speter/*
2132718Skan * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
390075Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
418334Speter *
5132718Skan * This code is free software; you can redistribute it and/or modify it
618334Speter * under the terms of the GNU General Public License version 2 only, as
7132718Skan * published by the Free Software Foundation.
818334Speter *
918334Speter * This code is distributed in the hope that it will be useful, but WITHOUT
1018334Speter * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1118334Speter * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12132718Skan * version 2 for more details (a copy is included in the LICENSE file that
1318334Speter * accompanied this code).
1418334Speter *
1518334Speter * You should have received a copy of the GNU General Public License version
1618334Speter * 2 along with this work; if not, write to the Free Software Foundation,
1718334Speter * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18132718Skan *
19169689Skan * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20169689Skan * or visit www.oracle.com if you need additional information or have any
2118334Speter * questions.
2218334Speter */
2318334Speter
2418334Speter/*
2518334Speter * @test
2618334Speter * @bug 8013855
2718334Speter * @run main AuthRealmChoices 1
2818334Speter * @run main AuthRealmChoices 2
2918334Speter * @summary DigestMD5Client has not checked RealmChoiceCallback value
3018334Speter */
3118334Speter
3218334Speterimport java.io.IOException;
3318334Speterimport java.util.HashMap;
3418334Speterimport java.util.Map;
35169689Skanimport javax.security.auth.callback.Callback;
36117395Skanimport javax.security.auth.callback.CallbackHandler;
37169689Skanimport javax.security.auth.callback.UnsupportedCallbackException;
38import javax.security.sasl.*;
39
40public class AuthRealmChoices {
41    private static final String MECH = "DIGEST-MD5";
42    private static final String SERVER_FQDN = "machineX.imc.org";
43    private static final String PROTOCOL = "jmx";
44
45    private static final byte[] EMPTY = new byte[0];
46
47    public static void main(String[] args) throws Exception {
48
49        Map props = new HashMap();
50        props.put("com.sun.security.sasl.digest.realm",
51            "IMC.ORG foo.bar machineX");
52
53        SaslClient clnt = Sasl.createSaslClient(
54            new String[]{MECH}, null, PROTOCOL, SERVER_FQDN, null,
55                new CallbackHandler() {
56                    @Override
57                    public void handle(Callback[] callbacks)
58                            throws IOException, UnsupportedCallbackException {
59                        for (Callback cb: callbacks) {
60                            if (cb instanceof RealmChoiceCallback) {
61                                // Two tests:
62                                // 1. Set an index out of bound
63                                // 2. No index set at all
64                                if (args[0].equals("1")) {
65                                    ((RealmChoiceCallback)cb).setSelectedIndex(10);
66                                }
67                            }
68                        }
69                    }
70                });
71
72        SaslServer srv = Sasl.createSaslServer(MECH, PROTOCOL, SERVER_FQDN, props,
73            new CallbackHandler() {
74                @Override
75                public void handle(Callback[] callbacks)
76                        throws IOException, UnsupportedCallbackException {
77                    for (Callback cb: callbacks) {
78                        System.out.println(cb);
79                    }
80                }
81            });
82
83        byte[] challenge = srv.evaluateResponse(EMPTY);
84
85        try {
86            clnt.evaluateChallenge(challenge);
87            throw new Exception();
88        } catch (SaslException se) {
89            System.out.println(se);
90        }
91    }
92}
93