jdk/test/sun/security/krb5/auto/SaslBasic.java
changeset 14340 e150cbaf584e
child 25403 e982fe3e83a4
equal deleted inserted replaced
14339:3b561cef789b 14340:e150cbaf584e
       
     1 /*
       
     2  * Copyright (c) 2012, 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 7110803
       
    27  * @summary SASL service for multiple hostnames
       
    28  * @compile -XDignore.symbol.file SaslBasic.java
       
    29  * @run main/othervm SaslBasic bound
       
    30  * @run main/othervm SaslBasic unbound
       
    31  */
       
    32 import java.io.IOException;
       
    33 import java.util.Arrays;
       
    34 import java.util.HashMap;
       
    35 import java.util.Locale;
       
    36 import javax.security.auth.callback.Callback;
       
    37 import javax.security.auth.callback.CallbackHandler;
       
    38 import javax.security.auth.callback.UnsupportedCallbackException;
       
    39 import javax.security.sasl.*;
       
    40 
       
    41 // The basic krb5 test skeleton you can copy from
       
    42 public class SaslBasic {
       
    43 
       
    44     public static void main(String[] args) throws Exception {
       
    45 
       
    46         boolean bound = args[0].equals("bound");
       
    47         String name = "host." + OneKDC.REALM.toLowerCase(Locale.US);
       
    48 
       
    49         new OneKDC(null).writeJAASConf();
       
    50         System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
       
    51 
       
    52         HashMap clntprops = new HashMap();
       
    53         clntprops.put(Sasl.QOP, "auth-conf");
       
    54         SaslClient sc = Sasl.createSaslClient(
       
    55                 new String[]{"GSSAPI"}, null, "server",
       
    56                 name, clntprops, null);
       
    57 
       
    58         final HashMap srvprops = new HashMap();
       
    59         srvprops.put(Sasl.QOP, "auth,auth-int,auth-conf");
       
    60         SaslServer ss = Sasl.createSaslServer("GSSAPI", "server",
       
    61                 bound? name: null, srvprops,
       
    62                 new CallbackHandler() {
       
    63                     public void handle(Callback[] callbacks)
       
    64                             throws IOException, UnsupportedCallbackException {
       
    65                         for (Callback cb : callbacks) {
       
    66                             if (cb instanceof RealmCallback) {
       
    67                                 ((RealmCallback) cb).setText(OneKDC.REALM);
       
    68                             } else if (cb instanceof AuthorizeCallback) {
       
    69                                 ((AuthorizeCallback) cb).setAuthorized(true);
       
    70                             }
       
    71                         }
       
    72                     }
       
    73                 });
       
    74 
       
    75         byte[] token = new byte[0];
       
    76         while (!sc.isComplete() || !ss.isComplete()) {
       
    77             if (!sc.isComplete()) {
       
    78                 token = sc.evaluateChallenge(token);
       
    79             }
       
    80             if (!ss.isComplete()) {
       
    81                 token = ss.evaluateResponse(token);
       
    82             }
       
    83         }
       
    84         if (!bound) {
       
    85             String boundName = (String)ss.getNegotiatedProperty(Sasl.BOUND_SERVER_NAME);
       
    86             if (!boundName.equals(name)) {
       
    87                 throw new Exception("Wrong bound server name");
       
    88             }
       
    89         }
       
    90         byte[] hello = "hello".getBytes();
       
    91         token = sc.wrap(hello, 0, hello.length);
       
    92         token = ss.unwrap(token, 0, token.length);
       
    93         if (!Arrays.equals(hello, token)) {
       
    94             throw new Exception("Message altered");
       
    95         }
       
    96     }
       
    97 }