Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/krb5/canonicalize/Test.java
38853 views
/*1* Copyright (c) 2009, 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*/22/*23* @test24* @bug 6682516 814952125* @summary SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms26* @run main/othervm -Dsun.net.spi.nameservice.provider.1=ns,mock -Djava.security.krb5.conf=krb5.conf Test27*/2829import java.net.InetAddress;30import java.net.UnknownHostException;31import sun.net.spi.nameservice.NameService;32import sun.net.spi.nameservice.NameServiceDescriptor;33import sun.security.krb5.PrincipalName;3435public class Test implements NameServiceDescriptor {36public static void main(String[] args) throws Exception {37// This config file is generated using Kerberos.app on a Mac38System.setProperty("java.security.krb5.realm", "THIS.REALM");39System.setProperty("java.security.krb5.kdc", "localhost");4041// add using canonicalized name42check("c1", "c1.this.domain");43check("c1.this", "c1.this.domain");44check("c1.this.domain", "c1.this.domain");45check("c1.this.domain.", "c1.this.domain");4647// canonicalized name goes IP, reject48check("c2", "c2");49check("c2.", "c2");5051// canonicalized name goes strange, reject52check("c3", "c3");5354// unsupported55check("c4", "c4");56}5758static void check(String input, String output) throws Exception {59System.out.println(input + " -> " + output);60PrincipalName pn = new PrincipalName("host/"+input,61PrincipalName.KRB_NT_SRV_HST);62if (!pn.getNameStrings()[1].equals(output)) {63throw new Exception("Output is " + pn);64}65}6667@Override68public NameService createNameService() throws Exception {69NameService ns = new NameService() {70@Override71public InetAddress[] lookupAllHostAddr(String host)72throws UnknownHostException {73// All c<n>.* goes to 127.0.0.n74int i = Integer.valueOf(host.split("\\.")[0].substring(1));75return new InetAddress[]{76InetAddress.getByAddress(host, new byte[]{127,0,0,(byte)i})77};78}79@Override80public String getHostByAddr(byte[] addr)81throws UnknownHostException {82int i = addr[3];83switch (i) {84case 1: return "c1.this.domain"; // Good85case 2: return "127.0.0.2"; // Only IP86case 3: return "d3.this.domain"; // name change87default:88throw new UnknownHostException();89}90}91};92return ns;93}9495@Override96public String getProviderName() {97return "mock";98}99100@Override101public String getType() {102return "ns";103}104}105106107