Path: blob/master/test/jdk/com/sun/security/sasl/digest/AuthOnly.java
41154 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 463489226* @summary Ensure digest authentication works.27*/28import javax.security.sasl.*;29import javax.security.auth.callback.*;30import java.security.Security;31import java.util.*;3233public class AuthOnly {34private static final String MECH = "DIGEST-MD5";35private static final String SERVER_FQDN = "machineX.imc.org";36private static final String PROTOCOL = "jmx";3738private static final byte[] EMPTY = new byte[0];3940private static String pwfile, namesfile, proxyfile;41private static boolean auto;42private static boolean verbose = false;4344private static void init(String[] args) throws Exception {45if (args.length == 0) {46pwfile = "pw.properties";47namesfile = "names.properties";48auto = true;49} else {50int i = 0;51if (args[i].equals("-m")) {52i++;53auto = false;54}55if (args.length > i) {56pwfile = args[i++];5758if (args.length > i) {59namesfile = args[i++];6061if (args.length > i) {62proxyfile = args[i];63}64}65} else {66pwfile = "pw.properties";67namesfile = "names.properties";68}69}70}7172public static void main(String[] args) throws Exception {7374init(args);7576CallbackHandler clntCbh = new ClientCallbackHandler(auto);7778CallbackHandler srvCbh =79new PropertiesFileCallbackHandler(pwfile, namesfile, proxyfile);8081Map props = System.getProperties();8283SaslClient clnt = Sasl.createSaslClient(84new String[]{MECH}, null, PROTOCOL, SERVER_FQDN, null, clntCbh);8586SaslServer srv = Sasl.createSaslServer(MECH, PROTOCOL, SERVER_FQDN, props,87srvCbh);8889if (clnt == null) {90throw new IllegalStateException(91"Unable to find client impl for " + MECH);92}93if (srv == null) {94throw new IllegalStateException(95"Unable to find server impl for " + MECH);96}9798byte[] response = (clnt.hasInitialResponse()?99clnt.evaluateChallenge(EMPTY) : EMPTY);100byte[] challenge;101102while (!clnt.isComplete() || !srv.isComplete()) {103challenge = srv.evaluateResponse(response);104105if (challenge != null) {106response = clnt.evaluateChallenge(challenge);107}108}109110if (clnt.isComplete() && srv.isComplete()) {111if (verbose) {112System.out.println("SUCCESS");113System.out.println("authzid is " + srv.getAuthorizationID());114}115} else {116throw new IllegalStateException(117"FAILURE: mismatched state:" +118" client complete? " + clnt.isComplete() +119" server complete? " + srv.isComplete());120}121122clnt.dispose();123srv.dispose();124}125}126127128