7176574: sun/security/krb5/auto/TcpTimeout.java failed with solaris-i586
authorweijun
Wed, 13 Jun 2012 19:23:23 +0800
changeset 13016 8e43ddef9617
parent 13015 b87f392315f2
child 13017 2e79ad4fea09
7176574: sun/security/krb5/auto/TcpTimeout.java failed with solaris-i586 Reviewed-by: chegar
jdk/test/sun/security/krb5/auto/TcpTimeout.java
--- a/jdk/test/sun/security/krb5/auto/TcpTimeout.java	Tue Jun 12 15:16:32 2012 -0700
+++ b/jdk/test/sun/security/krb5/auto/TcpTimeout.java	Wed Jun 13 19:23:23 2012 +0800
@@ -48,49 +48,60 @@
         k.addPrincipalRandKey("krbtgt/" + OneKDC.REALM);
 
         // Start two listener that does not communicate, simulate timeout
-        int p1 = new ServerSocket(0).getLocalPort();
-        int p2 = new ServerSocket(0).getLocalPort();
+        ServerSocket ss1 = null;
+        ServerSocket ss2 = null;
 
-        FileWriter fw = new FileWriter("alternative-krb5.conf");
+        try {
+            ss1 = new ServerSocket(0);
+            ss2 = new ServerSocket(0);
+            int p1 = ss1.getLocalPort();
+            int p2 = ss2.getLocalPort();
+
+            FileWriter fw = new FileWriter("alternative-krb5.conf");
 
-        fw.write("[libdefaults]\n" +
-                "udp_preference_limit = 1\n" +
-                "max_retries = 2\n" +
-                "default_realm = " + OneKDC.REALM + "\n" +
-                "kdc_timeout = 5000\n");
-        fw.write("[realms]\n" + OneKDC.REALM + " = {\n" +
-                "kdc = " + OneKDC.KDCHOST + ":" + p1 + "\n" +
-                "kdc = " + OneKDC.KDCHOST + ":" + p2 + "\n" +
-                "kdc = " + OneKDC.KDCHOST + ":" + p3 + "\n" +
-                "}\n");
+            fw.write("[libdefaults]\n" +
+                    "udp_preference_limit = 1\n" +
+                    "max_retries = 2\n" +
+                    "default_realm = " + OneKDC.REALM + "\n" +
+                    "kdc_timeout = 5000\n");
+            fw.write("[realms]\n" + OneKDC.REALM + " = {\n" +
+                    "kdc = " + OneKDC.KDCHOST + ":" + p1 + "\n" +
+                    "kdc = " + OneKDC.KDCHOST + ":" + p2 + "\n" +
+                    "kdc = " + OneKDC.KDCHOST + ":" + p3 + "\n" +
+                    "}\n");
 
-        fw.close();
-        System.setProperty("java.security.krb5.conf", "alternative-krb5.conf");
-        Config.refresh();
-
-        System.out.println("Ports opened on " + p1 + ", " + p2 + ", " + p3);
+            fw.close();
+            System.setProperty("java.security.krb5.conf",
+                    "alternative-krb5.conf");
+            Config.refresh();
 
-        // The correct behavior should be:
-        // 5 sec on p1, 5 sec on p1, fail
-        // 5 sec on p2, 5 sec on p2, fail
-        // p3 ok, p3 ok again for preauth.
-        int count = 6;
+            System.out.println("Ports opened on " + p1 + ", " + p2 + ", " + p3);
+
+            // The correct behavior should be:
+            // 5 sec on p1, 5 sec on p1, fail
+            // 5 sec on p2, 5 sec on p2, fail
+            // p3 ok, p3 ok again for preauth.
+            int count = 6;
+
+            ByteArrayOutputStream bo = new ByteArrayOutputStream();
+            PrintStream oldout = System.out;
+            System.setOut(new PrintStream(bo));
+            Context c = Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false);
+            System.setOut(oldout);
 
-        ByteArrayOutputStream bo = new ByteArrayOutputStream();
-        PrintStream oldout = System.out;
-        System.setOut(new PrintStream(bo));
-        Context c = Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false);
-        System.setOut(oldout);
-
-        String[] lines = new String(bo.toByteArray()).split("\n");
-        for (String line: lines) {
-            if (line.startsWith(">>> KDCCommunication")) {
-                System.out.println(line);
-                count--;
+            String[] lines = new String(bo.toByteArray()).split("\n");
+            for (String line: lines) {
+                if (line.startsWith(">>> KDCCommunication")) {
+                    System.out.println(line);
+                    count--;
+                }
             }
-        }
-        if (count != 0) {
-            throw new Exception("Retry count is " + count + " less");
+            if (count != 0) {
+                throw new Exception("Retry count is " + count + " less");
+            }
+        } finally {
+            if (ss1 != null) ss1.close();
+            if (ss2 != null) ss2.close();
         }
     }
 }