Path: blob/master/test/jdk/sun/security/tools/keytool/StartDateTest.java
41152 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 646828526* @summary keytool ability to backdate self-signed certificates to compensate for clock skew27* @modules java.base/sun.security.tools.keytool:+open28* @run main StartDateTest29*/3031import java.io.File;32import java.io.FileInputStream;33import java.lang.reflect.Method;34import java.security.KeyStore;35import java.security.cert.X509Certificate;36import java.util.Calendar;37import java.util.Date;38import java.util.GregorianCalendar;3940public class StartDateTest {41public static void main(String[] args) throws Exception {4243// Part 1: Test function44Calendar cal = new GregorianCalendar();45int year = cal.get(Calendar.YEAR);46int month = cal.get(Calendar.MONTH);4748new File("jks").delete();4950run("one", "+1y");51cal.setTime(getIssueDate("one"));52System.out.println(cal);53if (cal.get(Calendar.YEAR) != year + 1) {54throw new Exception("Function check #1 fails");55}5657run("two", "+1m");58cal.setTime(getIssueDate("two"));59System.out.println(cal);60if (cal.get(Calendar.MONTH) != (month + 1) % 12) {61throw new Exception("Function check #2 fails");62}6364run("three", "2009/10/11 12:34:56");65cal.setTime(getIssueDate("three"));66System.out.println(cal);67if (cal.get(Calendar.YEAR) != 2009 ||68cal.get(Calendar.MONTH) != Calendar.OCTOBER ||69cal.get(Calendar.DAY_OF_MONTH) != 11 ||70cal.get(Calendar.HOUR_OF_DAY) != 12 ||71cal.get(Calendar.MINUTE) != 34 ||72cal.get(Calendar.SECOND) != 56) {73throw new Exception("Function check #3 fails");74}7576// Part 2: Test format77Method m = sun.security.tools.keytool.Main.class.getDeclaredMethod(78"getStartDate", String.class);79m.setAccessible(true);80for (String s: new String[] {81null, //NOW!82"+1m+1d",83"+1y-1m+1d",84"+3H",85"+1M",86"-5M",87"+011d",88"+22S",89"+500S",90"2001/01/01",91"15:15:15",92"2001/01/01 11:11:11",93}) {94try {95System.out.println(s + " " + m.invoke(null, s));96} catch (Exception e) {97e.printStackTrace();98throw new Exception("Failed at " + s);99}100}101for (String s: new String[] {102"", // empty103"+3",104"+3m+",105"+3m+3",106"1m", // no sign107"+0x011d", // hex number108"+1m1d", // no sign for the 2nd sub value109"m",110"+1h", // h is not H111"-1m1d",112"-m",113"x",114"+1m +1d",115"2007/07",116"01:01",117"+01:01:01", // what's this?118"1:01:01",119"12pm",120"2007/07/07 12:12:12", // extra blank between121"2001/01/01-11:11:11",122"2007-07-07", // non-standard date delim123"2007/7/7", // no padding124"07/07/07", // year's length not 4125"1:1:1",126}) {127boolean failed = false;128try {129System.out.println(m.invoke(null, s));130} catch (Exception e) {131System.out.println(s + " " + e.getCause());132failed = true;133}134if (!failed) throw new Exception("Failed at " + s);135}136}137138// The keytool command line template, alias and startdate TBD139static String[] cmd = ("-alias tbd -startdate tbd -keystore jks " +140"-storetype jks -storepass changeit -keypass changeit " +141"-keyalg rsa -genkeypair -dname CN=Haha -debug").split(" ");142143static void run(String alias, String startDate) throws Exception {144cmd[1] = alias;145cmd[3] = startDate;146sun.security.tools.keytool.Main.main(cmd);147}148149static Date getIssueDate(String alias) throws Exception {150KeyStore ks = KeyStore.getInstance("jks");151try (FileInputStream fis = new FileInputStream("jks")) {152ks.load(fis, "changeit".toCharArray());153}154X509Certificate cert = (X509Certificate)ks.getCertificate(alias);155return cert.getNotBefore();156}157}158159160