1 /*
   2  * Copyright (c) 2009, 2012, 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 6857795
  26  * @bug 6858589
  27  * @bug 6972005
  28  * @compile -XDignore.symbol.file ConfPlusProp.java
  29  * @run main/othervm ConfPlusProp
  30  * @summary krb5.conf ignored if system properties on realm and kdc are provided
  31  */
  32 
  33 import sun.security.krb5.Config;
  34 
  35 public class ConfPlusProp {
  36     Config config;
  37     public static void main(String[] args) throws Exception {
  38         new ConfPlusProp().run();
  39     }
  40 
  41     void refresh() throws Exception {
  42         Config.refresh();
  43         config = Config.getInstance();
  44     }
  45 
  46     void checkDefaultRealm(String r) throws Exception {
  47         try {
  48             if (!config.getDefaultRealm().equals(r)) {
  49                 throw new AssertionError("Default realm error");
  50             }
  51         } catch (Exception e) {
  52             if (r != null) throw e;
  53         }
  54     }
  55 
  56     void check(String r, String k) throws Exception {
  57         try {
  58             if (!config.getKDCList(r).equals(k)) {
  59                 throw new AssertionError(r + " kdc not " + k);
  60             }
  61         } catch (Exception e) {
  62             if (k != null) throw e;
  63         }
  64     }
  65 
  66     void run() throws Exception {
  67 
  68         // No prop, only conf
  69 
  70         // Point to a file with existing default_realm
  71         System.setProperty("java.security.krb5.conf",
  72                 System.getProperty("test.src", ".") +"/confplusprop.conf");
  73         refresh();
  74 
  75         checkDefaultRealm("R1");
  76         check("R1", "k1");
  77         check("R2", "old");
  78         check("R3", null);
  79         if (!config.get("libdefaults", "forwardable").equals("well")) {
  80             throw new Exception("Extra config error");
  81         }
  82 
  83         // Point to a file with no libdefaults
  84         System.setProperty("java.security.krb5.conf",
  85                 System.getProperty("test.src", ".") +"/confplusprop2.conf");
  86         refresh();
  87 
  88         checkDefaultRealm(null);
  89         check("R1", "k12");
  90         check("R2", "old");
  91         check("R3", null);
  92 
  93         int version = System.getProperty("java.version").charAt(2) - '0';
  94         System.out.println("JDK version is " + version);
  95 
  96         // Zero-config is supported since 1.7
  97         if (version >= 7) {
  98             // Point to a non-existing file
  99             System.setProperty("java.security.krb5.conf", "i-am-not-a file");
 100             refresh();
 101 
 102             // Default realm might come from DNS
 103             //checkDefaultRealm(null);
 104             check("R1", null);
 105             check("R2", null);
 106             check("R3", null);
 107             if (config.get("libdefaults", "forwardable") != null) {
 108                 throw new Exception("Extra config error");
 109             }
 110         }
 111 
 112         // Add prop
 113         System.setProperty("java.security.krb5.realm", "R2");
 114         System.setProperty("java.security.krb5.kdc", "k2");
 115 
 116         // Point to a file with existing default_realm
 117         System.setProperty("java.security.krb5.conf",
 118                 System.getProperty("test.src", ".") +"/confplusprop.conf");
 119         refresh();
 120 
 121         checkDefaultRealm("R2");
 122         check("R1", "k1");
 123         check("R2", "k2");
 124         check("R3", "k2");
 125         if (!config.get("libdefaults", "forwardable").equals("well")) {
 126             throw new Exception("Extra config error");
 127         }
 128 
 129         // Point to a file with no libdefaults
 130         System.setProperty("java.security.krb5.conf",
 131                 System.getProperty("test.src", ".") +"/confplusprop2.conf");
 132         refresh();
 133 
 134         checkDefaultRealm("R2");
 135         check("R1", "k12");
 136         check("R2", "k2");
 137         check("R3", "k2");
 138 
 139         // Point to a non-existing file
 140         System.setProperty("java.security.krb5.conf", "i-am-not-a file");
 141         refresh();
 142 
 143         checkDefaultRealm("R2");
 144         check("R1", "k2");
 145         check("R2", "k2");
 146         check("R3", "k2");
 147         if (config.get("libdefaults", "forwardable") != null) {
 148             throw new Exception("Extra config error");
 149         }
 150     }
 151 }