changeset 2406:ff9cc9789bb3

6882687: KerberosTime too imprecise Reviewed-by: valeriep
author weijun
date Mon, 24 May 2010 09:37:02 +0800
parents c36617d0dae7
children 625c75559b0d
files src/share/classes/sun/security/krb5/internal/KerberosTime.java test/sun/security/krb5/MicroTime.java
diffstat 2 files changed, 86 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/sun/security/krb5/internal/KerberosTime.java	Mon May 24 09:28:25 2010 +0800
+++ b/src/share/classes/sun/security/krb5/internal/KerberosTime.java	Mon May 24 09:37:02 2010 +0800
@@ -57,11 +57,20 @@
  * specification available at
  * <a href="http://www.ietf.org/rfc/rfc4120.txt">
  * http://www.ietf.org/rfc/rfc4120.txt</a>.
+ *
+ * The implementation also includes the microseconds info so that the
+ * same class can be used as a precise timestamp in Authenticator etc.
  */
 
 public class KerberosTime implements Cloneable {
 
     private long kerberosTime; // milliseconds since epoch, a Date.getTime() value
+    private int  microSeconds; // the last three digits of the microsecond value
+
+    // The time when this class is loaded. Used in setNow()
+    private static final long initMilli = System.currentTimeMillis();
+    private static final long initMicro = System.nanoTime() / 1000;
+
     private static long syncTime;
     private static boolean DEBUG = Krb5.DEBUG;
 
@@ -77,9 +86,13 @@
         kerberosTime = time;
     }
 
+    private KerberosTime(long time, int micro) {
+        kerberosTime = time;
+        microSeconds = micro;
+    }
 
     public Object clone() {
-        return new KerberosTime(kerberosTime);
+        return new KerberosTime(kerberosTime, microSeconds);
     }
 
     // This constructor is used in the native code
@@ -109,8 +122,8 @@
         //  |   | | | | | |
         //  0   4 6 8 | | |
         //           10 | |
-        //                         12 |
-        //                           14
+        //             12 |
+        //               14
 
         if (time.length() != 15)
             throw new Asn1Exception(Krb5.ASN1_BAD_TIMEFORMAT);
@@ -148,11 +161,8 @@
 
     public KerberosTime(boolean initToNow) {
         if (initToNow) {
-            Date temp = new Date();
-            setTime(temp);
+            setNow();
         }
-        else
-            kerberosTime = 0;
     }
 
     /**
@@ -192,10 +202,12 @@
 
     public void setTime(Date time) {
         kerberosTime = time.getTime(); // (time.getTimezoneOffset() * 60000L);
+        microSeconds = 0;
     }
 
     public void setTime(long time) {
         kerberosTime = time;
+        microSeconds = 0;
     }
 
     public Date toDate() {
@@ -205,16 +217,18 @@
     }
 
     public void setNow() {
-        Date temp = new Date();
-        setTime(temp);
+        long microElapsed = System.nanoTime() / 1000 - initMicro;
+        setTime(initMilli + microElapsed/1000);
+        microSeconds = (int)(microElapsed % 1000);
     }
 
     public int getMicroSeconds() {
         Long temp_long = new Long((kerberosTime % 1000L) * 1000L);
-        return temp_long.intValue();
+        return temp_long.intValue() + microSeconds;
     }
 
     public void setMicroSeconds(int usec) {
+        microSeconds = usec % 1000;
         Integer temp_int = new Integer(usec);
         long temp_long = temp_int.longValue() / 1000L;
         kerberosTime = kerberosTime - (kerberosTime % 1000L) + temp_long;
@@ -222,6 +236,7 @@
 
     public void setMicroSeconds(Integer usec) {
         if (usec != null) {
+            microSeconds = usec.intValue() % 1000;
             long temp_long = usec.longValue() / 1000L;
             kerberosTime = kerberosTime - (kerberosTime % 1000L) + temp_long;
         }
@@ -262,7 +277,9 @@
     }
 
     public boolean greaterThan(KerberosTime time) {
-        return kerberosTime > time.kerberosTime;
+        return kerberosTime > time.kerberosTime ||
+            kerberosTime == time.kerberosTime &&
+                    microSeconds > time.microSeconds;
     }
 
     public boolean equals(Object obj) {
@@ -274,15 +291,17 @@
             return false;
         }
 
-        return kerberosTime == ((KerberosTime)obj).kerberosTime;
+        return kerberosTime == ((KerberosTime)obj).kerberosTime &&
+                microSeconds == ((KerberosTime)obj).microSeconds;
     }
 
     public int hashCode() {
-        return 37 * 17 + (int)(kerberosTime ^ (kerberosTime >>> 32));
+        int result = 37 * 17 + (int)(kerberosTime ^ (kerberosTime >>> 32));
+        return result * 17 + microSeconds;
     }
 
     public boolean isZero() {
-        return kerberosTime == 0;
+        return kerberosTime == 0 && microSeconds == 0;
     }
 
     public int getSeconds() {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/sun/security/krb5/MicroTime.java	Mon May 24 09:37:02 2010 +0800
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2010 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+/*
+ * @test
+ * @bug 6882687
+ * @summary KerberosTime too imprecise
+ */
+
+import sun.security.krb5.internal.KerberosTime;
+
+public class MicroTime {
+    public static void main(String[] args) throws Exception {
+        // We count how many different KerberosTime values
+        // can be acquired within one second.
+        KerberosTime t1 = new KerberosTime(true);
+        KerberosTime last = t1;
+        int count = 0;
+        while (true) {
+            KerberosTime t2 = new KerberosTime(true);
+            if (t2.getTime() - t1.getTime() > 1000) break;
+            if (!last.equals(t2)) {
+                last = t2;
+                count++;
+            }
+        }
+        // We believe a nice KerberosTime can at least tell the
+        // difference of 100 musec.
+        if (count < 10000) {
+            throw new Exception("What? only " + (1000000/count) +
+                    " musec precision?");
+        }
+    }
+}