test/jdk/sun/security/tools/keytool/StartDateTest.java
author vtewari
Wed, 25 Apr 2018 12:29:48 +0530
changeset 49882 a02abc7e5536
parent 47216 71c04702a3d5
permissions -rw-r--r--
8144806: sun/security/tools/keytool/standard.sh fails intermittently at deleting x.jks Reviewed-by: weijun Contributed-by: bhanu.prakash.gopularam@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21342
diff changeset
     2
 * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * questions.
2
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
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40393
diff changeset
    28
 * @modules java.base/sun.security.tools.keytool:+open
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10328
diff changeset
    29
 * @run main StartDateTest
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.KeyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Calendar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.GregorianCalendar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class StartDateTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        // Part 1: Test function
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        Calendar cal = new GregorianCalendar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        int year = cal.get(Calendar.YEAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        int month = cal.get(Calendar.MONTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        new File("jks").delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
40393
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
    51
        run("one", "+1y");
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
    52
        cal.setTime(getIssueDate("one"));
2
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
40393
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
    58
        run("two", "+1m");
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
    59
        cal.setTime(getIssueDate("two"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        System.out.println(cal);
91
2f9e71bc1bac 6643094: Test on keytool -startdate forgets about December
weijun
parents: 2
diff changeset
    61
        if (cal.get(Calendar.MONTH) != (month + 1) % 12) {
2f9e71bc1bac 6643094: Test on keytool -startdate forgets about December
weijun
parents: 2
diff changeset
    62
            throw new Exception("Function check #2 fails");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
40393
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
    65
        run("three", "2009/10/11 12:34:56");
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
    66
        cal.setTime(getIssueDate("three"));
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
    67
        System.out.println(cal);
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
    68
        if (cal.get(Calendar.YEAR) != 2009 ||
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
    69
                cal.get(Calendar.MONTH) != Calendar.OCTOBER ||
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
    70
                cal.get(Calendar.DAY_OF_MONTH) != 11 ||
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
    71
                cal.get(Calendar.HOUR_OF_DAY) != 12 ||
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
    72
                cal.get(Calendar.MINUTE) != 34 ||
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
    73
                cal.get(Calendar.SECOND) != 56) {
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
    74
            throw new Exception("Function check #3 fails");
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
    75
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        // Part 2: Test format
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10328
diff changeset
    78
        Method m = sun.security.tools.keytool.Main.class.getDeclaredMethod(
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 10328
diff changeset
    79
                   "getStartDate", String.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        m.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        for (String s: new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                null,       //NOW!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                "+1m+1d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                "+1y-1m+1d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                "+3H",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                "+1M",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                "-5M",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                "+011d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                "+22S",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                "+500S",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                "2001/01/01",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                "15:15:15",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                "2001/01/01 11:11:11",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                }) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                System.out.println(s + " " + m.invoke(null, s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                throw new Exception("Failed at " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        for (String s: new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                "",         // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                "+3",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                "+3m+",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                "+3m+3",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                "1m",       // no sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                "+0x011d",  // hex number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                "+1m1d",    // no sign for the 2nd sub value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                "m",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                "+1h",      // h is not H
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                "-1m1d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                "-m",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                "x",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                "+1m +1d",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                "2007/07",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                "01:01",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                "+01:01:01",                // what's this?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                "1:01:01",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                "12pm",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                "2007/07/07  12:12:12",     // extra blank between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                "2001/01/01-11:11:11",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                "2007-07-07",               // non-standard date delim
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                "2007/7/7",                 // no padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                "07/07/07",                 // year's length not 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                "1:1:1",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                }) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                System.out.println(m.invoke(null, s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                System.out.println(s + " " + e.getCause());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (!failed) throw new Exception("Failed at " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
40393
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
   139
    // The keytool command line template, alias and startdate TBD
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
   140
    static String[] cmd = ("-alias tbd -startdate tbd -keystore jks " +
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
   141
            "-storetype jks -storepass changeit -keypass changeit " +
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
   142
            "-keyalg rsa -genkeypair -dname CN=Haha -debug").split(" ");
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
   143
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
   144
    static void run(String alias, String startDate) throws Exception {
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
   145
        cmd[1] = alias;
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
   146
        cmd[3] = startDate;
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
   147
        sun.security.tools.keytool.Main.main(cmd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
40393
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
   150
    static Date getIssueDate(String alias) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        KeyStore ks = KeyStore.getInstance("jks");
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   152
        try (FileInputStream fis = new FileInputStream("jks")) {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   153
            ks.load(fis, "changeit".toCharArray());
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   154
        }
40393
cb883ad42653 8141411: keytool can wrongly parse the start date value given by the -startdate option
weijun
parents: 30820
diff changeset
   155
        X509Certificate cert = (X509Certificate)ks.getCertificate(alias);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return cert.getNotBefore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
}