Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/tools/keytool/StartDateTest.java
38853 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* @compile -XDignore.symbol.file StartDateTest.java28* @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("-keystore jks -storetype jks -storepass changeit -keypass changeit -alias me " +51"-keyalg rsa -genkeypair -dname CN=Haha -startdate +1y");52cal.setTime(getIssueDate());53System.out.println(cal);54if (cal.get(Calendar.YEAR) != year + 1) {55throw new Exception("Function check #1 fails");56}5758run("-keystore jks -storetype jks -storepass changeit -keypass changeit -alias me " +59"-selfcert -startdate +1m");60cal.setTime(getIssueDate());61System.out.println(cal);62if (cal.get(Calendar.MONTH) != (month + 1) % 12) {63throw new Exception("Function check #2 fails");64}6566new File("jks").delete();6768// Part 2: Test format69Method m = sun.security.tools.keytool.Main.class.getDeclaredMethod(70"getStartDate", String.class);71m.setAccessible(true);72for (String s: new String[] {73null, //NOW!74"+1m+1d",75"+1y-1m+1d",76"+3H",77"+1M",78"-5M",79"+011d",80"+22S",81"+500S",82"2001/01/01",83"15:15:15",84"2001/01/01 11:11:11",85}) {86try {87System.out.println(s + " " + m.invoke(null, s));88} catch (Exception e) {89e.printStackTrace();90throw new Exception("Failed at " + s);91}92}93for (String s: new String[] {94"", // empty95"+3",96"+3m+",97"+3m+3",98"1m", // no sign99"+0x011d", // hex number100"+1m1d", // no sign for the 2nd sub value101"m",102"+1h", // h is not H103"-1m1d",104"-m",105"x",106"+1m +1d",107"2007/07",108"01:01",109"+01:01:01", // what's this?110"1:01:01",111"12pm",112"2007/07/07 12:12:12", // extra blank between113"2001/01/01-11:11:11",114"2007-07-07", // non-standard date delim115"2007/7/7", // no padding116"07/07/07", // year's length not 4117"1:1:1",118}) {119boolean failed = false;120try {121System.out.println(m.invoke(null, s));122} catch (Exception e) {123System.out.println(s + " " + e.getCause());124failed = true;125}126if (!failed) throw new Exception("Failed at " + s);127}128}129130static void run(String s) throws Exception {131sun.security.tools.keytool.Main.main((s+" -debug").split(" "));132}133134static Date getIssueDate() throws Exception {135KeyStore ks = KeyStore.getInstance("jks");136try (FileInputStream fis = new FileInputStream("jks")) {137ks.load(fis, "changeit".toCharArray());138}139X509Certificate cert = (X509Certificate)ks.getCertificate("me");140return cert.getNotBefore();141}142}143144145