jdk/test/sun/security/krb5/ConfPlusProp.java
changeset 3315 2c61231c7973
parent 3221 98ac5a3e79e9
child 3422 72bf5182bb8b
--- a/jdk/test/sun/security/krb5/ConfPlusProp.java	Mon Jul 20 17:16:34 2009 -0400
+++ b/jdk/test/sun/security/krb5/ConfPlusProp.java	Wed Jul 22 16:39:34 2009 +0800
@@ -23,31 +23,56 @@
 /*
  * @test
  * @bug 6857795
+ * @buf 6858589
  * @summary krb5.conf ignored if system properties on realm and kdc are provided
  */
 
 import sun.security.krb5.Config;
-import sun.security.krb5.KrbException;
 
 public class ConfPlusProp {
+    Config config;
     public static void main(String[] args) throws Exception {
-        System.setProperty("java.security.krb5.realm", "R2");
-        System.setProperty("java.security.krb5.kdc", "k2");
+        new ConfPlusProp().run();
+    }
+
+    void refresh() throws Exception {
+        Config.refresh();
+        config = Config.getInstance();
+    }
+
+    void checkDefaultRealm(String r) throws Exception {
+        try {
+            if (!config.getDefaultRealm().equals(r)) {
+                throw new AssertionError("Default realm error");
+            }
+        } catch (Exception e) {
+            if (r != null) throw e;
+        }
+    }
+
+    void check(String r, String k) throws Exception {
+        try {
+            if (!config.getKDCList(r).equals(k)) {
+                throw new AssertionError(r + " kdc not " + k);
+            }
+        } catch (Exception e) {
+            if (k != null) throw e;
+        }
+    }
+
+    void run() throws Exception {
+
+        // No prop, only conf
 
         // Point to a file with existing default_realm
         System.setProperty("java.security.krb5.conf",
                 System.getProperty("test.src", ".") +"/confplusprop.conf");
-        Config config = Config.getInstance();
+        refresh();
 
-        if (!config.getDefaultRealm().equals("R2")) {
-            throw new Exception("Default realm error");
-        }
-        if (!config.getKDCList("R1").equals("k1")) {
-            throw new Exception("R1 kdc error");
-        }
-        if (!config.getKDCList("R2").equals("k2")) {
-            throw new Exception("R2 kdc error");
-        }
+        checkDefaultRealm("R1");
+        check("R1", "k1");
+        check("R2", "old");
+        check("R3", null);
         if (!config.getDefault("forwardable", "libdefaults").equals("well")) {
             throw new Exception("Extra config error");
         }
@@ -55,38 +80,66 @@
         // Point to a file with no libdefaults
         System.setProperty("java.security.krb5.conf",
                 System.getProperty("test.src", ".") +"/confplusprop2.conf");
-        Config.refresh();
+        refresh();
+
+        checkDefaultRealm(null);
+        check("R1", "k12");
+        check("R2", "old");
+        check("R3", null);
+
+        int version = System.getProperty("java.version").charAt(2) - '0';
+        System.out.println("JDK version is " + version);
 
-        config = Config.getInstance();
+        // Zero-config is supported since 1.7
+        if (version >= 7) {
+            // Point to a non-existing file
+            System.setProperty("java.security.krb5.conf", "i-am-not-a file");
+            refresh();
 
-        if (!config.getDefaultRealm().equals("R2")) {
-            throw new Exception("Default realm error again");
+            checkDefaultRealm(null);
+            check("R1", null);
+            check("R2", null);
+            check("R3", null);
+            if (config.getDefault("forwardable", "libdefaults") != null) {
+                throw new Exception("Extra config error");
+            }
         }
-        if (!config.getKDCList("R1").equals("k12")) {
-            throw new Exception("R1 kdc error");
+
+        // Add prop
+        System.setProperty("java.security.krb5.realm", "R2");
+        System.setProperty("java.security.krb5.kdc", "k2");
+
+        // Point to a file with existing default_realm
+        System.setProperty("java.security.krb5.conf",
+                System.getProperty("test.src", ".") +"/confplusprop.conf");
+        refresh();
+
+        checkDefaultRealm("R2");
+        check("R1", "k1");
+        check("R2", "k2");
+        check("R3", "k2");
+        if (!config.getDefault("forwardable", "libdefaults").equals("well")) {
+            throw new Exception("Extra config error");
         }
-        if (!config.getKDCList("R2").equals("k2")) {
-            throw new Exception("R2 kdc error");
-        }
+
+        // Point to a file with no libdefaults
+        System.setProperty("java.security.krb5.conf",
+                System.getProperty("test.src", ".") +"/confplusprop2.conf");
+        refresh();
+
+        checkDefaultRealm("R2");
+        check("R1", "k12");
+        check("R2", "k2");
+        check("R3", "k2");
 
         // Point to a non-existing file
         System.setProperty("java.security.krb5.conf", "i-am-not-a file");
-        Config.refresh();
-
-        config = Config.getInstance();
+        refresh();
 
-        if (!config.getDefaultRealm().equals("R2")) {
-            throw new Exception("Default realm error");
-        }
-        try {
-            config.getKDCList("R1");
-            throw new Exception("R1 is nowhere");
-        } catch (KrbException ke) {
-            // OK
-        }
-        if (!config.getKDCList("R2").equals("k2")) {
-            throw new Exception("R2 kdc error");
-        }
+        checkDefaultRealm("R2");
+        check("R1", "k2");
+        check("R2", "k2");
+        check("R3", "k2");
         if (config.getDefault("forwardable", "libdefaults") != null) {
             throw new Exception("Extra config error");
         }