Path: blob/master/test/jdk/sun/security/krb5/auto/NoAddresses.java
41152 views
/*1* Copyright (c) 2011, 2019, 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 703235426* @library /test/lib27* @run main/othervm NoAddresses setup28* @run main/othervm -Djdk.net.hosts.file=TestHosts NoAddresses 129* @run main/othervm -Djdk.net.hosts.file=TestHosts NoAddresses 230* @run main/othervm/fail -Djdk.net.hosts.file=TestHosts NoAddresses 331* @summary no-addresses should not be used on acceptor side32*/3334import java.net.InetAddress;35import org.ietf.jgss.ChannelBinding;36import sun.security.jgss.GSSUtil;37import sun.security.krb5.Config;38import java.io.PrintWriter;39import java.io.FileWriter;40import java.io.BufferedWriter;41import java.nio.file.*;4243public class NoAddresses {4445public static void main(String[] args)46throws Exception {4748if (args[0].equalsIgnoreCase("setup")) {49// add a mapping of test host name to 127.0.0.1 to test's hosts file50InetAddress localHost = InetAddress.getLocalHost();51String localHostName = localHost.getHostName();52String hostsFileName = System.getProperty("test.src", ".") + "/TestHosts";53String hostsFileNameLocal = "TestHosts";54String loopBackAddress = "127.0.0.1";55Files.copy(Paths.get(hostsFileName), Paths.get(hostsFileNameLocal));56addMappingToHostsFile(localHostName, loopBackAddress, hostsFileNameLocal, true);57} else {58OneKDC kdc = new OneKDC(null);59kdc.writeJAASConf();60KDC.saveConfig(OneKDC.KRB5_CONF, kdc,61"noaddresses = false",62"default_keytab_name = " + OneKDC.KTAB);63Config.refresh();6465Context c = Context.fromJAAS("client");66Context s = Context.fromJAAS("server");6768c.startAsClient(OneKDC.SERVER, GSSUtil.GSS_KRB5_MECH_OID);69s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID);7071InetAddress initiator = InetAddress.getLocalHost();72InetAddress acceptor = InetAddress.getLocalHost();73switch (args[0]) {74case "1":75// no initiator host address available, should be OK76break;77case "2":78// correct initiator host address, still fine79c.x().setChannelBinding(80new ChannelBinding(initiator, acceptor, null));81s.x().setChannelBinding(82new ChannelBinding(initiator, acceptor, null));83break;84case "3":85// incorrect initiator host address, fail86initiator = InetAddress.getByAddress(new byte[]{1,1,1,1});87c.x().setChannelBinding(88new ChannelBinding(initiator, acceptor, null));89s.x().setChannelBinding(90new ChannelBinding(initiator, acceptor, null));91break;92}9394Context.handshake(c, s);95}96}9798private static void addMappingToHostsFile (String host,99String addr,100String hostsFileName,101boolean append)102throws Exception {103String mapping = addr + " " + host;104try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(105new FileWriter(hostsFileName, append)))) {106hfPWriter.println(mapping);107}108}109}110111112