From 65e7759e78980ffc1ffe9844ecc56121984fe0e1 Mon Sep 17 00:00:00 2001 From: i556354 Date: Mon, 17 Jul 2023 07:54:07 +0000 Subject: [PATCH] 8220410: sun/security/tools/jarsigner/warnings/NoTimestampTest.java failed with missing expected output Using the same timezone for jar verifying and date formatting Reviewed-by: goetz Backport-of: 3c34b7a2616922dddaf67d5f9e5907f260d302c8 --- .../jarsigner/warnings/NoTimestampTest.java | 16 +++++++++++++--- .../security/tools/jarsigner/warnings/Test.java | 13 ++++++++----- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/test/jdk/sun/security/tools/jarsigner/warnings/NoTimestampTest.java b/test/jdk/sun/security/tools/jarsigner/warnings/NoTimestampTest.java index a7c895fa2b6..ca5f17e9079 100644 --- a/test/jdk/sun/security/tools/jarsigner/warnings/NoTimestampTest.java +++ b/test/jdk/sun/security/tools/jarsigner/warnings/NoTimestampTest.java @@ -27,6 +27,7 @@ import java.security.cert.X509Certificate; import java.util.Date; import java.util.Locale; +import java.util.TimeZone; import jdk.test.lib.process.OutputAnalyzer; import jdk.test.lib.util.JarUtils; @@ -46,6 +47,7 @@ public class NoTimestampTest extends Test { * and checks that proper warnings are shown. */ public static void main(String[] args) throws Throwable { + Locale reservedLocale = Locale.getDefault(); Locale.setDefault(Locale.US); @@ -61,6 +63,9 @@ public static void main(String[] args) throws Throwable { private void start() throws Throwable { String timezone = System.getProperty("user.timezone"); System.out.println(String.format("Timezone = %s", timezone)); + if (timezone != null) { + TimeZone.setDefault(TimeZone.getTimeZone(timezone)); + } // create a jar file that contains one class file Utils.createFiles(FIRST_FILE); @@ -73,10 +78,11 @@ private void start() throws Throwable { "-validity", Integer.toString(VALIDITY)); Date expirationDate = getCertExpirationDate(); + System.out.println("Cert expiration: " + expirationDate); // sign jar file OutputAnalyzer analyzer = jarsigner( - "-J-Duser.timezone=" + timezone, + userTimezoneOpt(timezone), "-keystore", KEYSTORE, "-storepass", PASSWORD, "-keypass", PASSWORD, @@ -90,7 +96,7 @@ private void start() throws Throwable { // verify signed jar analyzer = jarsigner( - "-J-Duser.timezone=" + timezone, + userTimezoneOpt(timezone), "-verify", "-keystore", KEYSTORE, "-storepass", PASSWORD, @@ -103,7 +109,7 @@ private void start() throws Throwable { // verify signed jar in strict mode analyzer = jarsigner( - "-J-Duser.timezone=" + timezone, + userTimezoneOpt(timezone), "-verify", "-strict", "-keystore", KEYSTORE, @@ -117,6 +123,10 @@ private void start() throws Throwable { System.out.println("Test passed"); } + private static String userTimezoneOpt(String timezone) { + return timezone == null ? null : "-J-Duser.timezone=" + timezone; + } + private static Date getCertExpirationDate() throws Exception { KeyStore ks = KeyStore.getInstance("JKS"); try (InputStream in = new FileInputStream(KEYSTORE)) { diff --git a/test/jdk/sun/security/tools/jarsigner/warnings/Test.java b/test/jdk/sun/security/tools/jarsigner/warnings/Test.java index 98ec45cc8b9..414caf22570 100644 --- a/test/jdk/sun/security/tools/jarsigner/warnings/Test.java +++ b/test/jdk/sun/security/tools/jarsigner/warnings/Test.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -21,12 +21,13 @@ * questions. */ -import jdk.test.lib.process.OutputAnalyzer; -import jdk.test.lib.process.ProcessTools; - import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; + +import jdk.test.lib.process.OutputAnalyzer; +import jdk.test.lib.process.ProcessTools; /** * Base class. @@ -263,7 +264,9 @@ private OutputAnalyzer tool(String tool, String... args) throws Throwable { cmd.add(tool); cmd.add("-J-Duser.language=en"); cmd.add("-J-Duser.country=US"); - cmd.addAll(Arrays.asList(args)); + cmd.addAll(Arrays.asList(args).stream().filter(arg -> { + return arg != null && !arg.isEmpty(); + }).collect(Collectors.toList())); return ProcessTools.executeCommand(cmd.toArray(new String[cmd.size()])); } }