Path: blob/master/test/jdk/tools/jar/JarEntryTime.java
66643 views
/*1* Copyright (c) 2006, 2021, 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 4225317 6969651 827742226* @modules jdk.jartool27* @summary Check extracted files have date as per those in the .jar file28*/2930import java.io.File;31import java.io.PrintWriter;32import java.nio.file.attribute.FileTime;33import java.time.ZoneId;34import java.util.Date;35import java.util.TimeZone;36import java.util.spi.ToolProvider;3738public class JarEntryTime {39static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")40.orElseThrow(() ->41new RuntimeException("jar tool not found")42);434445// ZipEntry's mod date has 2 seconds precision: give extra time to46// allow for e.g. rounding/truncation and networked/samba drives.47static final long PRECISION = 10000L;4849static final TimeZone TZ = TimeZone.getDefault();50static final boolean DST = TZ.inDaylightTime(new Date());5152static boolean cleanup(File dir) throws Throwable {53boolean rc = true;54File[] x = dir.listFiles();55if (x != null) {56for (int i = 0; i < x.length; i++) {57rc &= x[i].delete();58}59}60return rc & dir.delete();61}6263static void extractJar(File jarFile, boolean useExtractionTime) throws Throwable {64String javahome = System.getProperty("java.home");65String jarcmd = javahome + File.separator + "bin" + File.separator + "jar";66String[] args;67if (useExtractionTime) {68args = new String[] {69jarcmd,70"-J-Dsun.tools.jar.useExtractionTime=true",71"xf",72jarFile.getName() };73} else {74args = new String[] {75jarcmd,76"xf",77jarFile.getName() };78}79Process p = Runtime.getRuntime().exec(args);80check(p != null && (p.waitFor() == 0));81}8283public static void realMain(String[] args) throws Throwable {8485File dirOuter = new File("outer");86File dirInner = new File(dirOuter, "inner");87File jarFile = new File("JarEntryTime.jar");88File testFile = new File("JarEntryTimeTest.txt");8990// Remove any leftovers from prior run91cleanup(dirInner);92cleanup(dirOuter);93jarFile.delete();94testFile.delete();9596var date = new Date();97var defZone = ZoneId.systemDefault();98if (defZone.getRules().getTransition(99date.toInstant().atZone(defZone).toLocalDateTime()) != null) {100System.out.println("At the offset transition. JarEntryTime test skipped.");101return;102}103104/* Create a directory structure105* outer/106* inner/107* foo.txt108* Set the lastModified dates so that outer is created now, inner109* yesterday, and foo.txt created "earlier".110*/111check(dirOuter.mkdir());112check(dirInner.mkdir());113File fileInner = new File(dirInner, "foo.txt");114try (PrintWriter pw = new PrintWriter(fileInner)) {115pw.println("hello, world");116}117118// Get the "now" from the "last-modified-time" of the last file we119// just created, instead of the "System.currentTimeMillis()", to120// workaround the possible "time difference" due to nfs.121final long now = fileInner.lastModified();122final long earlier = now - (60L * 60L * 6L * 1000L);123final long yesterday = now - (60L * 60L * 24L * 1000L);124125check(dirOuter.setLastModified(now));126check(dirInner.setLastModified(yesterday));127check(fileInner.setLastModified(earlier));128129// Make a jar file from that directory structure130check(JAR_TOOL.run(System.out, System.err,131"cf", jarFile.getName(), dirOuter.getName()) == 0);132check(jarFile.exists());133134check(cleanup(dirInner));135check(cleanup(dirOuter));136137// Extract and check that the last modified values are those specified138// in the archive139extractJar(jarFile, false);140check(dirOuter.exists());141check(dirInner.exists());142check(fileInner.exists());143checkFileTime(dirOuter.lastModified(), now);144checkFileTime(dirInner.lastModified(), yesterday);145checkFileTime(fileInner.lastModified(), earlier);146147check(cleanup(dirInner));148check(cleanup(dirOuter));149150try (PrintWriter pw = new PrintWriter(testFile)) {151pw.println("hello, world");152}153final long start = testFile.lastModified();154155// Extract and check the last modified values are the current times.156extractJar(jarFile, true);157158try (PrintWriter pw = new PrintWriter(testFile)) {159pw.println("hello, world");160}161final long end = testFile.lastModified();162163check(dirOuter.exists());164check(dirInner.exists());165check(fileInner.exists());166checkFileTime(start, dirOuter.lastModified(), end);167checkFileTime(start, dirInner.lastModified(), end);168checkFileTime(start, fileInner.lastModified(), end);169170check(cleanup(dirInner));171check(cleanup(dirOuter));172173check(jarFile.delete());174check(testFile.delete());175}176177static void checkFileTime(long now, long original) {178if (isTimeSettingChanged()) {179return;180}181182if (Math.abs(now - original) > PRECISION) {183System.out.format("Extracted to %s, expected to be close to %s%n",184FileTime.fromMillis(now), FileTime.fromMillis(original));185fail();186}187}188189static void checkFileTime(long start, long now, long end) {190if (isTimeSettingChanged()) {191return;192}193194if (now < start || now > end) {195System.out.format("Extracted to %s, "196+ "expected to be after %s and before %s%n",197FileTime.fromMillis(now),198FileTime.fromMillis(start),199FileTime.fromMillis(end));200fail();201}202}203204private static boolean isTimeSettingChanged() {205TimeZone currentTZ = TimeZone.getDefault();206boolean currentDST = currentTZ.inDaylightTime(new Date());207return (!currentTZ.equals(TZ) || currentDST != DST);208}209210//--------------------- Infrastructure ---------------------------211static volatile int passed = 0, failed = 0;212static void pass() {passed++;}213static void fail() {failed++; Thread.dumpStack();}214static void fail(String msg) {System.out.println(msg); fail();}215static void unexpected(Throwable t) {failed++; t.printStackTrace();}216static void check(boolean cond) {if (cond) pass(); else fail();}217static void equal(Object x, Object y) {218if (x == null ? y == null : x.equals(y)) pass();219else fail(x + " not equal to " + y);}220public static void main(String[] args) throws Throwable {221try {realMain(args);} catch (Throwable t) {unexpected(t);}222System.out.println("\nPassed = " + passed + " failed = " + failed);223if (failed > 0) throw new AssertionError("Some tests failed");}224}225226227