1 /*
   2  * Copyright (c) 2009, 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  * @test
  25  * @bug 6682516
  26  * @summary SPNEGO_HTTP_AUTH/WWW_KRB and SPNEGO_HTTP_AUTH/WWW_SPNEGO failed on all non-windows platforms
  27  * @run main/othervm -Dsun.net.spi.nameservice.provider.1=ns,mock -Djava.security.krb5.conf=krb5.conf Test
  28  */
  29 
  30 import java.net.InetAddress;
  31 import java.net.UnknownHostException;
  32 import sun.net.spi.nameservice.NameService;
  33 import sun.net.spi.nameservice.NameServiceDescriptor;
  34 import sun.security.krb5.PrincipalName;
  35 
  36 public class Test implements NameServiceDescriptor {
  37     public static void main(String[] args) throws Exception {
  38         // This config file is generated using Kerberos.app on a Mac
  39         System.setProperty("java.security.krb5.realm", "THIS.REALM");
  40         System.setProperty("java.security.krb5.kdc", "localhost");
  41 
  42         // add using canonicalized name
  43         check("c1", "c1.this.domain");
  44         check("c1.this", "c1.this.domain");
  45         check("c1.this.domain", "c1.this.domain");
  46 
  47         // canonicalized name goes IP, reject
  48         check("c2", "c2");
  49 
  50         // canonicalized name goes strange, reject
  51         check("c3", "c3");
  52 
  53         // unsupported
  54         check("c4", "c4");
  55     }
  56 
  57     static void check(String input, String output) throws Exception {
  58         System.out.println(input + " -> " + output);
  59         PrincipalName pn = new PrincipalName("host/"+input,
  60                 PrincipalName.KRB_NT_SRV_HST);
  61         if (!pn.getNameStrings()[1].equals(output)) {
  62             throw new Exception("Output is " + pn);
  63         }
  64     }
  65 
  66     @Override
  67     public NameService createNameService() throws Exception {
  68         NameService ns = new NameService() {
  69             @Override
  70             public InetAddress[] lookupAllHostAddr(String host)
  71                     throws UnknownHostException {
  72                 // All c<n>.* goes to 127.0.0.n
  73                 int i = Integer.valueOf(host.split("\\.")[0].substring(1));
  74                 return new InetAddress[]{
  75                     InetAddress.getByAddress(host, new byte[]{127,0,0,(byte)i})
  76                 };
  77             }
  78             @Override
  79             public String getHostByAddr(byte[] addr)
  80                     throws UnknownHostException {
  81                 int i = addr[3];
  82                 switch (i) {
  83                     case 1: return "c1.this.domain";        // Good
  84                     case 2: return "127.0.0.2";             // Only IP
  85                     case 3: return "d3.this.domain";        // name change
  86                     default:
  87                         throw new UnknownHostException();
  88                 }
  89             }
  90         };
  91         return ns;
  92     }
  93 
  94     @Override
  95     public String getProviderName() {
  96         return "mock";
  97     }
  98 
  99     @Override
 100     public String getType() {
 101         return "ns";
 102     }
 103 }