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