1 /*
   2  * Copyright (c) 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 /*
  25  * @test
  26  * @bug 7184246
  27  * @summary Simplify Config.get() of krb5
  28  * @modules java.security.jgss/sun.security.krb5:+open
  29  */
  30 import java.lang.reflect.Field;
  31 import java.lang.reflect.Method;
  32 import java.util.Hashtable;
  33 import java.util.Vector;
  34 import sun.security.krb5.Config;
  35 import sun.security.krb5.SCDynamicStoreConfig;
  36 
  37 public class SCDynamicConfigTest {
  38 
  39     static Vector<Hashtable<String,String>>hosts() {
  40         Vector <Hashtable<String,String>> result = new Vector<>();
  41         Hashtable<String,String> pair = new Hashtable<>();
  42         pair.put("host", "127.0.0.1");
  43         result.add(pair);
  44         pair = new Hashtable<>();
  45         pair.put("host", "127.0.0.2");
  46         result.add(pair);
  47         return result;
  48     }
  49 
  50     public static void main(String[] args) throws Exception {
  51         // Reconstruct a typical SCDynamicConfig.getKerberosConfig() output
  52         Hashtable<String, Object> conf = new Hashtable<>();
  53 
  54         Hashtable<String, Object> libdefaults = new Hashtable<>();
  55         libdefaults.put("default_realm", "REALM.COM");
  56         conf.put("libdefaults", libdefaults);
  57 
  58         Hashtable<String, Object> realms = new Hashtable<>();
  59         Hashtable<String, Object> thisRealm = new Hashtable<>();
  60         realms.put("REALM.COM", thisRealm);
  61         thisRealm.put("kpasswd", hosts());
  62         thisRealm.put("kadmin", hosts());
  63         thisRealm.put("kdc", hosts());
  64         conf.put("realms", realms);
  65 
  66         Hashtable<String, Object> domain_realm = new Hashtable<>();
  67         domain_realm.put(".realm.com", "REALM.COM");
  68         domain_realm.put("realm.com", "REALM.COM");
  69         conf.put("domain_realm", domain_realm);
  70 
  71         System.out.println("SCDynamicConfig:\n");
  72         System.out.println(conf);
  73 
  74         // Simulate SCDynamicConfig.getConfig() output
  75         Method m = SCDynamicStoreConfig.class.getDeclaredMethod(
  76                 "convertNativeConfig", Hashtable.class);
  77         m.setAccessible(true);
  78         conf = (Hashtable)m.invoke(null, conf);
  79 
  80         System.out.println("\nkrb5.conf:\n");
  81         System.out.println(conf);
  82 
  83         // Feed it into a Config object
  84         System.setProperty("java.security.krb5.conf", "not-a-file");
  85         Config cf = Config.getInstance();
  86         Field f = Config.class.getDeclaredField("stanzaTable");
  87         f.setAccessible(true);
  88         f.set(cf, conf);
  89 
  90         System.out.println("\nConfig:\n");
  91         System.out.println(cf);
  92 
  93         if (!cf.getDefaultRealm().equals("REALM.COM")) {
  94             throw new Exception();
  95         }
  96         if (!cf.getKDCList("REALM.COM").equals("127.0.0.1 127.0.0.2")) {
  97             throw new Exception();
  98         }
  99         if (!cf.get("domain_realm", ".realm.com").equals("REALM.COM")) {
 100             throw new Exception();
 101         }
 102     }
 103 }