1 /*
   2  * Copyright (c) 2006, 2016, 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 /**
  25  * @test
  26  * @bug 4225317 6969651
  27  * @modules jdk.jartool/sun.tools.jar
  28  * @summary Check extracted files have date as per those in the .jar file
  29  */
  30 
  31 import java.io.File;
  32 import java.io.PrintWriter;
  33 import java.nio.file.attribute.FileTime;
  34 import java.util.Date;
  35 import java.util.TimeZone;
  36 import sun.tools.jar.Main;
  37 
  38 public class JarEntryTime {
  39 
  40     // ZipEntry's mod date has 2 seconds precision: give extra time to
  41     // allow for e.g. rounding/truncation and networked/samba drives.
  42     static final long PRECISION = 10000L;
  43 
  44     static final TimeZone TZ = TimeZone.getDefault();
  45     static final boolean DST = TZ.inDaylightTime(new Date());
  46 
  47     static boolean cleanup(File dir) throws Throwable {
  48         boolean rc = true;
  49         File[] x = dir.listFiles();
  50         if (x != null) {
  51             for (int i = 0; i < x.length; i++) {
  52                 rc &= x[i].delete();
  53             }
  54         }
  55         return rc & dir.delete();
  56     }
  57 
  58     static void extractJar(File jarFile, boolean useExtractionTime) throws Throwable {
  59         String javahome = System.getProperty("java.home");
  60         String jarcmd = javahome + File.separator + "bin" + File.separator + "jar";
  61         String[] args;
  62         if (useExtractionTime) {
  63             args = new String[] {
  64                 jarcmd,
  65                 "-J-Dsun.tools.jar.useExtractionTime=true",
  66                 "xf",
  67                 jarFile.getName() };
  68         } else {
  69             args = new String[] {
  70                 jarcmd,
  71                 "xf",
  72                 jarFile.getName() };
  73         }
  74         Process p = Runtime.getRuntime().exec(args);
  75         check(p != null && (p.waitFor() == 0));
  76     }
  77 
  78     public static void realMain(String[] args) throws Throwable {
  79 
  80         File dirOuter = new File("outer");
  81         File dirInner = new File(dirOuter, "inner");
  82         File jarFile = new File("JarEntryTime.jar");
  83         File testFile = new File("JarEntryTimeTest.txt");
  84 
  85         // Remove any leftovers from prior run
  86         cleanup(dirInner);
  87         cleanup(dirOuter);
  88         jarFile.delete();
  89         testFile.delete();
  90 
  91         /* Create a directory structure
  92          * outer/
  93          *     inner/
  94          *         foo.txt
  95          * Set the lastModified dates so that outer is created now, inner
  96          * yesterday, and foo.txt created "earlier".
  97          */
  98         check(dirOuter.mkdir());
  99         check(dirInner.mkdir());
 100         File fileInner = new File(dirInner, "foo.txt");
 101         try (PrintWriter pw = new PrintWriter(fileInner)) {
 102             pw.println("hello, world");
 103         }
 104 
 105         // Get the "now" from the "last-modified-time" of the last file we
 106         // just created, instead of the "System.currentTimeMillis()", to
 107         // workaround the possible "time difference" due to nfs.
 108         final long now = fileInner.lastModified();
 109         final long earlier = now - (60L * 60L * 6L * 1000L);
 110         final long yesterday = now - (60L * 60L * 24L * 1000L);
 111 
 112         check(dirOuter.setLastModified(now));
 113         check(dirInner.setLastModified(yesterday));
 114         check(fileInner.setLastModified(earlier));
 115 
 116         // Make a jar file from that directory structure
 117         Main jartool = new Main(System.out, System.err, "jar");
 118         check(jartool.run(new String[] {
 119                 "cf",
 120                 jarFile.getName(), dirOuter.getName() } ));
 121         check(jarFile.exists());
 122 
 123         check(cleanup(dirInner));
 124         check(cleanup(dirOuter));
 125 
 126         // Extract and check that the last modified values are those specified
 127         // in the archive
 128         extractJar(jarFile, false);
 129         check(dirOuter.exists());
 130         check(dirInner.exists());
 131         check(fileInner.exists());
 132         checkFileTime(dirOuter.lastModified(), now);
 133         checkFileTime(dirInner.lastModified(), yesterday);
 134         checkFileTime(fileInner.lastModified(), earlier);
 135 
 136         check(cleanup(dirInner));
 137         check(cleanup(dirOuter));
 138 
 139         try (PrintWriter pw = new PrintWriter(testFile)) {
 140             pw.println("hello, world");
 141         }
 142         final long start = testFile.lastModified();
 143 
 144         // Extract and check the last modified values are the current times.
 145         // See sun.tools.jar.Main
 146         extractJar(jarFile, true);
 147 
 148         try (PrintWriter pw = new PrintWriter(testFile)) {
 149             pw.println("hello, world");
 150         }
 151         final long end = testFile.lastModified();
 152 
 153         check(dirOuter.exists());
 154         check(dirInner.exists());
 155         check(fileInner.exists());
 156         checkFileTime(start, dirOuter.lastModified(), end);
 157         checkFileTime(start, dirInner.lastModified(), end);
 158         checkFileTime(start, fileInner.lastModified(), end);
 159 
 160         check(cleanup(dirInner));
 161         check(cleanup(dirOuter));
 162 
 163         check(jarFile.delete());
 164         check(testFile.delete());
 165     }
 166 
 167     static void checkFileTime(long now, long original) {
 168         if (isTimeSettingChanged()) {
 169             return;
 170         }
 171 
 172         if (Math.abs(now - original) > PRECISION) {
 173             System.out.format("Extracted to %s, expected to be close to %s%n",
 174                 FileTime.fromMillis(now), FileTime.fromMillis(original));
 175             fail();
 176         }
 177     }
 178 
 179     static void checkFileTime(long start, long now, long end) {
 180         if (isTimeSettingChanged()) {
 181             return;
 182         }
 183 
 184         if (now < start || now > end) {
 185             System.out.format("Extracted to %s, "
 186                               + "expected to be after %s and before %s%n",
 187                               FileTime.fromMillis(now),
 188                               FileTime.fromMillis(start),
 189                               FileTime.fromMillis(end));
 190             fail();
 191         }
 192     }
 193 
 194     private static boolean isTimeSettingChanged() {
 195         TimeZone currentTZ = TimeZone.getDefault();
 196         boolean currentDST = currentTZ.inDaylightTime(new Date());
 197         return (!currentTZ.equals(TZ) || currentDST != DST);
 198     }
 199 
 200     //--------------------- Infrastructure ---------------------------
 201     static volatile int passed = 0, failed = 0;
 202     static void pass() {passed++;}
 203     static void fail() {failed++; Thread.dumpStack();}
 204     static void fail(String msg) {System.out.println(msg); fail();}
 205     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 206     static void check(boolean cond) {if (cond) pass(); else fail();}
 207     static void equal(Object x, Object y) {
 208         if (x == null ? y == null : x.equals(y)) pass();
 209         else fail(x + " not equal to " + y);}
 210     public static void main(String[] args) throws Throwable {
 211         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 212         System.out.println("\nPassed = " + passed + " failed = " + failed);
 213         if (failed > 0) throw new AssertionError("Some tests failed");}
 214 }