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