jdk/test/sun/security/tools/keytool/StartDateTest.java
author weijun
Sat, 08 Mar 2008 22:51:14 +0800
changeset 91 2f9e71bc1bac
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6643094: Test on keytool -startdate forgets about December Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6468285
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary keytool ability to backdate self-signed certificates to compensate for clock skew
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.KeyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Calendar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.GregorianCalendar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.tools.KeyTool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class StartDateTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        // Part 1: Test function
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        Calendar cal = new GregorianCalendar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        int year = cal.get(Calendar.YEAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        int month = cal.get(Calendar.MONTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        new File("jks").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        run("-keystore jks -storetype jks -storepass changeit -keypass changeit -alias me " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                "-genkeypair -dname CN=Haha -startdate +1y");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        cal.setTime(getIssueDate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        System.out.println(cal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (cal.get(Calendar.YEAR) != year + 1) {
91
2f9e71bc1bac 6643094: Test on keytool -startdate forgets about December
weijun
parents: 2
diff changeset
    55
            throw new Exception("Function check #1 fails");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        run("-keystore jks -storetype jks -storepass changeit -keypass changeit -alias me " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                "-selfcert -startdate +1m");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        cal.setTime(getIssueDate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        System.out.println(cal);
91
2f9e71bc1bac 6643094: Test on keytool -startdate forgets about December
weijun
parents: 2
diff changeset
    62
        if (cal.get(Calendar.MONTH) != (month + 1) % 12) {
2f9e71bc1bac 6643094: Test on keytool -startdate forgets about December
weijun
parents: 2
diff changeset
    63
            throw new Exception("Function check #2 fails");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        new File("jks").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // Part 2: Test format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        Method m = KeyTool.class.getDeclaredMethod("getStartDate", String.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        m.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        for (String s: new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                null,       //NOW!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                "+1m+1d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                "+1y-1m+1d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                "+3H",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                "+1M",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                "-5M",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                "+011d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                "+22S",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                "+500S",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                "2001/01/01",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                "15:15:15",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                "2001/01/01 11:11:11",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                }) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                System.out.println(s + " " + m.invoke(null, s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                throw new Exception("Failed at " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        for (String s: new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                "",         // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                "+3",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                "+3m+",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                "+3m+3",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                "1m",       // no sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                "+0x011d",  // hex number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                "+1m1d",    // no sign for the 2nd sub value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                "m",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                "+1h",      // h is not H
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                "-1m1d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                "-m",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                "x",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                "+1m +1d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                "2007/07",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                "01:01",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                "+01:01:01",                // what's this?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                "1:01:01",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                "12pm",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                "2007/07/07  12:12:12",     // extra blank between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                "2001/01/01-11:11:11",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                "2007-07-07",               // non-standard date delim
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                "2007/7/7",                 // no padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                "07/07/07",                 // year's length not 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                "1:1:1",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                }) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                System.out.println(m.invoke(null, s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                System.out.println(s + " " + e.getCause());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (!failed) throw new Exception("Failed at " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    static void run(String s) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        KeyTool.main((s+" -debug").split(" "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    static Date getIssueDate() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        KeyStore ks = KeyStore.getInstance("jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        ks.load(new FileInputStream("jks"), "changeit".toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        X509Certificate cert = (X509Certificate)ks.getCertificate("me");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return cert.getNotBefore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
}