1 /*
   2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.util.Date;
  25 import java.util.Locale;
  26 import jdk.testlibrary.OutputAnalyzer;
  27 import jdk.test.lib.util.JarUtils;
  28 
  29 /**
  30  * @test
  31  * @bug 8024302 8026037 8196213
  32  * @summary Checks warnings if -tsa and -tsacert options are not specified
  33  * @library /lib/testlibrary /test/lib ../
  34  * @build jdk.test.lib.util.JarUtils
  35  * @run main NoTimestampTest
  36  */
  37 public class NoTimestampTest extends Test {
  38 
  39     /**
  40      * The test signs and verifies a jar file without -tsa and -tsacert options,
  41      * and checks that proper warnings are shown.
  42      */
  43     public static void main(String[] args) throws Throwable {
  44         Locale reservedLocale = Locale.getDefault();
  45         Locale.setDefault(Locale.US);
  46 
  47         try {
  48             NoTimestampTest test = new NoTimestampTest();
  49             test.start();
  50         } finally {
  51             // Restore the reserved locale
  52             Locale.setDefault(reservedLocale);
  53         }
  54     }
  55 
  56     private void start() throws Throwable {
  57         String timezone = System.getProperty("user.timezone");
  58         System.out.println(String.format("Timezone = %s", timezone));
  59 
  60         // create a jar file that contains one class file
  61         Utils.createFiles(FIRST_FILE);
  62         JarUtils.createJar(UNSIGNED_JARFILE, FIRST_FILE);
  63 
  64         // calculate certificate expiration date
  65         Date expirationDate = new Date(System.currentTimeMillis() + VALIDITY
  66                 * 24 * 60 * 60 * 1000L);
  67 
  68         // create key pair
  69         createAlias(CA_KEY_ALIAS);
  70         createAlias(KEY_ALIAS);
  71         issueCert(KEY_ALIAS,
  72                 "-validity", Integer.toString(VALIDITY));
  73 
  74         // sign jar file
  75         OutputAnalyzer analyzer = jarsigner(
  76                 "-J-Duser.timezone=" + timezone,
  77                 "-keystore", KEYSTORE,
  78                 "-storepass", PASSWORD,
  79                 "-keypass", PASSWORD,
  80                 "-signedjar", SIGNED_JARFILE,
  81                 UNSIGNED_JARFILE,
  82                 KEY_ALIAS);
  83 
  84         String warning = String.format(NO_TIMESTAMP_SIGNING_WARN_TEMPLATE,
  85                 expirationDate);
  86         checkSigning(analyzer, warning);
  87 
  88         // verify signed jar
  89         analyzer = jarsigner(
  90                 "-J-Duser.timezone=" + timezone,
  91                 "-verify",
  92                 "-keystore", KEYSTORE,
  93                 "-storepass", PASSWORD,
  94                 "-keypass", PASSWORD,
  95                 SIGNED_JARFILE,
  96                 KEY_ALIAS);
  97 
  98         warning = String.format(NO_TIMESTAMP_VERIFYING_WARN_TEMPLATE, expirationDate);
  99         checkVerifying(analyzer, 0, warning);
 100 
 101         // verify signed jar in strict mode
 102         analyzer = jarsigner(
 103                 "-J-Duser.timezone=" + timezone,
 104                 "-verify",
 105                 "-strict",
 106                 "-keystore", KEYSTORE,
 107                 "-storepass", PASSWORD,
 108                 "-keypass", PASSWORD,
 109                 SIGNED_JARFILE,
 110                 KEY_ALIAS);
 111 
 112         checkVerifying(analyzer, 0, warning);
 113 
 114         System.out.println("Test passed");
 115     }
 116 
 117 }