1 /* 2 * Copyright (c) 2006, 2010, 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 * @summary Check extracted files have date as per those in the .jar file 28 */ 29 30 import java.io.File; 31 import java.io.PrintWriter; 32 import java.util.Date; 33 import sun.tools.jar.Main; 34 35 public class JarEntryTime { 36 static boolean cleanup(File dir) throws Throwable { 37 boolean rc = true; 38 File[] x = dir.listFiles(); 39 if (x != null) { 40 for (int i = 0; i < x.length; i++) { 41 rc &= x[i].delete(); 42 } 43 } 44 return rc & dir.delete(); 45 } 46 47 static void extractJar(File jarFile, boolean useExtractionTime) throws Throwable { 48 String javahome = System.getProperty("java.home"); 49 if (javahome.endsWith("jre")) { 50 javahome = javahome.substring(0, javahome.length() - 4); 51 } 52 String jarcmd = javahome + File.separator + "bin" + File.separator + "jar"; 53 String[] args; 54 if (useExtractionTime) { 55 args = new String[] { 56 jarcmd, 57 "-J-Dsun.tools.jar.useExtractionTime=true", 58 "xf", 59 jarFile.getName() }; 60 } else { 61 args = new String[] { 62 jarcmd, 63 "xf", 64 jarFile.getName() }; 65 } 66 Process p = Runtime.getRuntime().exec(args); 67 check(p != null && (p.waitFor() == 0)); 68 } 69 70 public static void realMain(String[] args) throws Throwable { 71 72 File dirOuter = new File("outer"); 73 File dirInner = new File(dirOuter, "inner"); 74 File jarFile = new File("JarEntryTime.jar"); 75 76 // Remove any leftovers from prior run 77 cleanup(dirInner); 78 cleanup(dirOuter); 79 jarFile.delete(); 80 81 /* Create a directory structure 82 * outer/ 83 * inner/ 84 * foo.txt 85 * Set the lastModified dates so that outer is created now, inner 86 * yesterday, and foo.txt created "earlier". 87 */ 88 check(dirOuter.mkdir()); 89 check(dirInner.mkdir()); 90 File fileInner = new File(dirInner, "foo.txt"); 91 PrintWriter pw = new PrintWriter(fileInner); 92 pw.println("hello, world"); 93 pw.close(); 94 95 // Get the "now" from the "last-modified-time" of the last file we 96 // just created, instead of the "System.currentTimeMillis()", to 97 // workaround the possible "time difference" due to nfs. 98 final long now = fileInner.lastModified(); 99 final long earlier = now - (60L * 60L * 6L * 1000L); 100 final long yesterday = now - (60L * 60L * 24L * 1000L); 101 // ZipEntry's mod date has 2 seconds precision: give extra time to 102 // allow for e.g. rounding/truncation and networked/samba drives. 103 final long PRECISION = 10000L; 104 105 dirOuter.setLastModified(now); 106 dirInner.setLastModified(yesterday); 107 fileInner.setLastModified(earlier); 108 109 // Make a jar file from that directory structure 110 Main jartool = new Main(System.out, System.err, "jar"); 111 check(jartool.run(new String[] { 112 "cf", 113 jarFile.getName(), dirOuter.getName() } )); 114 check(jarFile.exists()); 115 116 check(cleanup(dirInner)); 117 check(cleanup(dirOuter)); 118 119 // Extract and check that the last modified values are those specified 120 // in the archive 121 extractJar(jarFile, false); 122 check(dirOuter.exists()); 123 check(dirInner.exists()); 124 check(fileInner.exists()); 125 check(Math.abs(dirOuter.lastModified() - now) <= PRECISION); 126 check(Math.abs(dirInner.lastModified() - yesterday) <= PRECISION); 127 check(Math.abs(fileInner.lastModified() - earlier) <= PRECISION); 128 129 check(cleanup(dirInner)); 130 check(cleanup(dirOuter)); 131 132 // Extract and check the last modified values are the current times. 133 // See sun.tools.jar.Main 134 extractJar(jarFile, true); 135 check(dirOuter.exists()); 136 check(dirInner.exists()); 137 check(fileInner.exists()); 138 check(Math.abs(dirOuter.lastModified() - now) <= PRECISION); 139 check(Math.abs(dirInner.lastModified() - now) <= PRECISION); 140 check(Math.abs(fileInner.lastModified() - now) <= PRECISION); 141 142 check(cleanup(dirInner)); 143 check(cleanup(dirOuter)); 144 145 check(jarFile.delete()); 146 } 147 148 //--------------------- Infrastructure --------------------------- 149 static volatile int passed = 0, failed = 0; 150 static void pass() {passed++;} 151 static void fail() {failed++; Thread.dumpStack();} 152 static void fail(String msg) {System.out.println(msg); fail();} 153 static void unexpected(Throwable t) {failed++; t.printStackTrace();} 154 static void check(boolean cond) {if (cond) pass(); else fail();} 155 static void equal(Object x, Object y) { 156 if (x == null ? y == null : x.equals(y)) pass(); 157 else fail(x + " not equal to " + y);} 158 public static void main(String[] args) throws Throwable { 159 try {realMain(args);} catch (Throwable t) {unexpected(t);} 160 System.out.println("\nPassed = " + passed + " failed = " + failed); 161 if (failed > 0) throw new AssertionError("Some tests failed");} 162 }