Path: blob/master/test/jdk/com/sun/security/sasl/digest/AuthRealms.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 that supplying a list of realms in the27* "com.sun.security.sasl.digest.realm" property to the SASL server works.28*/2930/*31* Server sends list of specified realms to client. Client selects one.32* Can set logging to FINEST to view exchange.33*/34import javax.security.sasl.*;35import javax.security.auth.callback.*;36import java.security.Security;37import java.util.*;3839public class AuthRealms {40private static final String MECH = "DIGEST-MD5";41private static final String SERVER_FQDN = "machineX.imc.org";42private static final String PROTOCOL = "jmx";4344private static final byte[] EMPTY = new byte[0];4546private static String pwfile, namesfile, proxyfile;47private static boolean auto;48private static boolean verbose = false;4950private static void init(String[] args) throws Exception {51if (args.length == 0) {52pwfile = "pw.properties";53namesfile = "names.properties";54auto = true;55} else {56int i = 0;57if (args[i].equals("-m")) {58i++;59auto = false;60}61if (args.length > i) {62pwfile = args[i++];6364if (args.length > i) {65namesfile = args[i++];6667if (args.length > i) {68proxyfile = args[i];69}70}71} else {72pwfile = "pw.properties";73namesfile = "names.properties";74}75}76}7778public static void main(String[] args) throws Exception {7980init(args);8182CallbackHandler clntCbh = new ClientCallbackHandler(auto);8384CallbackHandler srvCbh =85new PropertiesFileCallbackHandler(pwfile, namesfile, proxyfile);8687Map props = new HashMap();88props.put("com.sun.security.sasl.digest.realm",89"IMC.ORG foo.bar machineX");9091SaslClient clnt = Sasl.createSaslClient(92new String[]{MECH}, null, PROTOCOL, SERVER_FQDN, null, clntCbh);9394SaslServer srv = Sasl.createSaslServer(MECH, PROTOCOL, SERVER_FQDN, props,95srvCbh);9697if (clnt == null) {98throw new IllegalStateException(99"Unable to find client impl for " + MECH);100}101if (srv == null) {102throw new IllegalStateException(103"Unable to find server impl for " + MECH);104}105106byte[] response = (clnt.hasInitialResponse()?107clnt.evaluateChallenge(EMPTY) : EMPTY);108byte[] challenge;109110while (!clnt.isComplete() || !srv.isComplete()) {111challenge = srv.evaluateResponse(response);112113if (challenge != null) {114response = clnt.evaluateChallenge(challenge);115}116}117118if (clnt.isComplete() && srv.isComplete()) {119if (verbose) {120System.out.println("SUCCESS");121System.out.println("authzid is " + srv.getAuthorizationID());122}123} else {124throw new IllegalStateException("FAILURE: mismatched state:" +125" client complete? " + clnt.isComplete() +126" server complete? " + srv.isComplete());127}128129clnt.dispose();130srv.dispose();131}132}133134135