1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
|
/*
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.TimeZone;
/*
* @test
* @summary Tests that the Properties.store() APIs generate output that is reproducible
* @bug 8231640 8282023 8316540
* @comment The test launches several processes and in the presence of -Xcomp it's too slow
* and thus causes timeouts
* @requires vm.compMode != "Xcomp"
* @library /test/lib
* @run driver StoreReproducibilityTest
*/
public class StoreReproducibilityTest {
private static final String DATE_FORMAT_PATTERN = "EEE MMM dd HH:mm:ss zzz uuuu";
private static final String SYS_PROP_JAVA_PROPERTIES_DATE = "java.properties.date";
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(DATE_FORMAT_PATTERN, Locale.ROOT)
.withZone(ZoneOffset.UTC);
public static void main(final String[] args) throws Exception {
// no security manager enabled
testWithoutSecurityManager();
// security manager enabled and security policy explicitly allows
// read permissions on java.properties.date system property
testWithSecMgrExplicitPermission();
// security manager enabled and no explicit permission on java.properties.date system property
testWithSecMgrNoSpecificPermission();
// free form non-date value for java.properties.date system property
testNonDateSysPropValue();
// blank value for java.properties.date system property
testBlankSysPropValue();
// empty value for java.properties.date system property
testEmptySysPropValue();
// value for java.properties.date system property contains line terminator characters
testMultiLineSysPropValue();
// value for java.properties.date system property contains backslash character
testBackSlashInSysPropValue();
}
/**
* Launches a Java program which is responsible for using Properties.store() to write out the
* properties to a file. The launched Java program is passed a value for the
* {@code java.properties.date} system property and the date comment written out
* to the file is expected to use this value.
* The program is launched multiple times with the same value for {@code java.properties.date}
* and the output written by each run of this program is verified to be exactly the same.
* Additionally, the date comment that's written out is verified to be the expected date that
* corresponds to the passed {@code java.properties.date}.
* The launched Java program is run without any security manager
*/
private static void testWithoutSecurityManager() throws Exception {
final List<Path> storedFiles = new ArrayList<>();
final String sysPropVal = FORMATTER.format(Instant.ofEpochSecond(243535322));
for (int i = 0; i < 5; i++) {
final Path tmpFile = Files.createTempFile("8231640", ".props");
storedFiles.add(tmpFile);
final ProcessBuilder processBuilder = ProcessTools.createTestJavaProcessBuilder(
"-D" + SYS_PROP_JAVA_PROPERTIES_DATE + "=" + sysPropVal,
StoreTest.class.getName(),
tmpFile.toString(),
i % 2 == 0 ? "--use-outputstream" : "--use-writer");
executeJavaProcess(processBuilder);
assertExpectedComment(tmpFile, sysPropVal);
if (!StoreTest.propsToStore.equals(loadProperties(tmpFile))) {
throw new RuntimeException("Unexpected properties stored in " + tmpFile);
}
}
assertAllFileContentsAreSame(storedFiles, sysPropVal);
}
/**
* Launches a Java program which is responsible for using Properties.store() to write out the
* properties to a file. The launched Java program is passed a value for the
* {@code java.properties.date} system property and the date comment written out to the file
* is expected to use this value.
* The launched Java program is run with the default security manager and is granted
* a {@code read} permission on {@code java.properties.date}.
* The program is launched multiple times with the same value for {@code java.properties.date}
* and the output written by each run of this program is verified to be exactly the same.
* Additionally, the date comment that's written out is verified to be the expected date that
* corresponds to the passed {@code java.properties.date}.
*/
private static void testWithSecMgrExplicitPermission() throws Exception {
final Path policyFile = Files.createTempFile("8231640", ".policy");
Files.write(policyFile, Collections.singleton("""
grant {
// test writes/stores to a file, so FilePermission
permission java.io.FilePermission "<<ALL FILES>>", "read,write";
// explicitly grant read permission on java.properties.date system property
// to verify store() APIs work fine
permission java.util.PropertyPermission "java.properties.date", "read";
};
"""));
final List<Path> storedFiles = new ArrayList<>();
final String sysPropVal = FORMATTER.format(Instant.ofEpochSecond(1234342423));
for (int i = 0; i < 5; i++) {
final Path tmpFile = Files.createTempFile("8231640", ".props");
storedFiles.add(tmpFile);
final ProcessBuilder processBuilder = ProcessTools.createTestJavaProcessBuilder(
"-D" + SYS_PROP_JAVA_PROPERTIES_DATE + "=" + sysPropVal,
"-Djava.security.manager",
"-Djava.security.policy=" + policyFile,
StoreTest.class.getName(),
tmpFile.toString(),
i % 2 == 0 ? "--use-outputstream" : "--use-writer");
executeJavaProcess(processBuilder);
assertExpectedComment(tmpFile, sysPropVal);
if (!StoreTest.propsToStore.equals(loadProperties(tmpFile))) {
throw new RuntimeException("Unexpected properties stored in " + tmpFile);
}
}
assertAllFileContentsAreSame(storedFiles, sysPropVal);
}
/**
* Launches a Java program which is responsible for using Properties.store() to write out the
* properties to a file. The launched Java program is passed a value for the
* {@code java.properties.date} system property and the date comment written out to the file
* is expected to use this value.
* The launched Java program is run with the default security manager and is NOT granted
* any explicit permission for {@code java.properties.date} system property.
* The program is launched multiple times with the same value for {@code java.properties.date}
* and the output written by each run of this program is verified to be exactly the same.
* Additionally, the date comment that's written out is verified to be the expected date that
* corresponds to the passed {@code java.properties.date}.
*/
private static void testWithSecMgrNoSpecificPermission() throws Exception {
final Path policyFile = Files.createTempFile("8231640", ".policy");
Files.write(policyFile, Collections.singleton("""
grant {
// test writes/stores to a file, so FilePermission
permission java.io.FilePermission "<<ALL FILES>>", "read,write";
// no other grants, not even "read" java.properties.date system property.
// test should still work fine and the date comment should correspond to the value of
// java.properties.date system property.
};
"""));
final List<Path> storedFiles = new ArrayList<>();
final String sysPropVal = FORMATTER.format(Instant.ofEpochSecond(1234342423));
for (int i = 0; i < 5; i++) {
final Path tmpFile = Files.createTempFile("8231640", ".props");
storedFiles.add(tmpFile);
final ProcessBuilder processBuilder = ProcessTools.createTestJavaProcessBuilder(
"-D" + SYS_PROP_JAVA_PROPERTIES_DATE + "=" + sysPropVal,
"-Djava.security.manager",
"-Djava.security.policy=" + policyFile,
StoreTest.class.getName(),
tmpFile.toString(),
i % 2 == 0 ? "--use-outputstream" : "--use-writer");
executeJavaProcess(processBuilder);
assertExpectedComment(tmpFile, sysPropVal);
if (!StoreTest.propsToStore.equals(loadProperties(tmpFile))) {
throw new RuntimeException("Unexpected properties stored in " + tmpFile);
}
}
assertAllFileContentsAreSame(storedFiles, sysPropVal);
}
/**
* Launches a Java program which is responsible for using Properties.store() to write out the
* properties to a file. The launched Java program is passed a {@link String#isBlank() blank} value
* for the {@code java.properties.date} system property.
* It is expected and verified in this test that such a value for the system property
* will cause a comment line to be written out with only whitespaces.
* The launched program is expected to complete without any errors.
*/
private static void testBlankSysPropValue() throws Exception {
final List<Path> storedFiles = new ArrayList<>();
final String sysPropVal = " \t";
for (int i = 0; i < 2; i++) {
final Path tmpFile = Files.createTempFile("8231640", ".props");
storedFiles.add(tmpFile);
final ProcessBuilder processBuilder = ProcessTools.createTestJavaProcessBuilder(
"-D" + SYS_PROP_JAVA_PROPERTIES_DATE + "=" + sysPropVal,
StoreTest.class.getName(),
tmpFile.toString(),
i % 2 == 0 ? "--use-outputstream" : "--use-writer");
executeJavaProcess(processBuilder);
if (!StoreTest.propsToStore.equals(loadProperties(tmpFile))) {
throw new RuntimeException("Unexpected properties stored in " + tmpFile);
}
String blankCommentLine = findNthComment(tmpFile, 2);
if (blankCommentLine == null) {
throw new RuntimeException("Comment line representing the value of "
+ SYS_PROP_JAVA_PROPERTIES_DATE + " system property is missing in file " + tmpFile);
}
if (!blankCommentLine.isBlank()) {
throw new RuntimeException("Expected comment line to be blank but was " + blankCommentLine);
}
}
assertAllFileContentsAreSame(storedFiles, sysPropVal);
}
/**
* Launches a Java program which is responsible for using Properties.store() to write out the
* properties to a file. The launched Java program is passed a {@link String#isEmpty() empty} value
* for the {@code java.properties.date} system property.
* It is expected and verified in this test that such a value for the system property
* will cause the current date and time to be written out as a comment.
* The launched program is expected to complete without any errors.
*/
private static void testEmptySysPropValue() throws Exception {
for (int i = 0; i < 2; i++) {
final Path tmpFile = Files.createTempFile("8231640", ".props");
final ProcessBuilder processBuilder = ProcessTools.createTestJavaProcessBuilder(
"-D" + SYS_PROP_JAVA_PROPERTIES_DATE + "=",
"-Duser.timezone=UTC",
StoreTest.class.getName(),
tmpFile.toString(),
i % 2 == 0 ? "--use-outputstream" : "--use-writer");
Date launchedAt = new Date();
// wait for a second before launching so that we can then expect
// the date written out by the store() APIs to be "after" this launch date
Thread.sleep(1000);
executeJavaProcess(processBuilder);
if (!StoreTest.propsToStore.equals(loadProperties(tmpFile))) {
throw new RuntimeException("Unexpected properties stored in " + tmpFile);
}
assertCurrentDate(tmpFile, launchedAt);
}
}
/**
* Launches a Java program which is responsible for using Properties.store() to write out the
* properties to a file. The launched Java program is passed the {@code java.properties.date}
* system property with a value that doesn't represent a formatted date.
* It is expected and verified in this test that such a value for the system property
* will cause the comment to use that value verbatim. The launched program is expected to complete
* without any errors.
*/
private static void testNonDateSysPropValue() throws Exception {
final String sysPropVal = "foo-bar";
final List<Path> storedFiles = new ArrayList<>();
for (int i = 0; i < 2; i++) {
final Path tmpFile = Files.createTempFile("8231640", ".props");
storedFiles.add(tmpFile);
final ProcessBuilder processBuilder = ProcessTools.createTestJavaProcessBuilder(
"-D" + SYS_PROP_JAVA_PROPERTIES_DATE + "=" + sysPropVal,
StoreTest.class.getName(),
tmpFile.toString(),
i % 2 == 0 ? "--use-outputstream" : "--use-writer");
executeJavaProcess(processBuilder);
if (!StoreTest.propsToStore.equals(loadProperties(tmpFile))) {
throw new RuntimeException("Unexpected properties stored in " + tmpFile);
}
assertExpectedComment(tmpFile, sysPropVal);
}
assertAllFileContentsAreSame(storedFiles, sysPropVal);
}
/**
* Launches a Java program which is responsible for using Properties.store() to write out the
* properties to a file. The launched Java program is passed the {@code java.properties.date}
* system property with a value that has line terminator characters.
* It is expected and verified in this test that such a value for the system property
* will cause the comment written out to be multiple separate comments. The launched program is expected
* to complete without any errors.
*/
private static void testMultiLineSysPropValue() throws Exception {
final String[] sysPropVals = {"hello-world\nc=d", "hello-world\rc=d", "hello-world\r\nc=d"};
for (final String sysPropVal : sysPropVals) {
final List<Path> storedFiles = new ArrayList<>();
for (int i = 0; i < 2; i++) {
final Path tmpFile = Files.createTempFile("8231640", ".props");
storedFiles.add(tmpFile);
final ProcessBuilder processBuilder = ProcessTools.createTestJavaProcessBuilder(
"-D" + SYS_PROP_JAVA_PROPERTIES_DATE + "=" + sysPropVal,
StoreTest.class.getName(),
tmpFile.toString(),
i % 2 == 0 ? "--use-outputstream" : "--use-writer");
executeJavaProcess(processBuilder);
if (!StoreTest.propsToStore.equals(loadProperties(tmpFile))) {
throw new RuntimeException("Unexpected properties stored in " + tmpFile);
}
// verify this results in 2 separate comment lines in the stored file
String commentLine1 = findNthComment(tmpFile, 2);
String commentLine2 = findNthComment(tmpFile, 3);
if (commentLine1 == null || commentLine2 == null) {
throw new RuntimeException("Did not find the expected multi-line comments in " + tmpFile);
}
if (!commentLine1.equals("hello-world")) {
throw new RuntimeException("Unexpected comment line " + commentLine1 + " in " + tmpFile);
}
if (!commentLine2.equals("c=d")) {
throw new RuntimeException("Unexpected comment line " + commentLine2 + " in " + tmpFile);
}
}
assertAllFileContentsAreSame(storedFiles, sysPropVal);
}
}
/**
* Launches a Java program which is responsible for using Properties.store() to write out the
* properties to a file. The launched Java program is passed the {@code java.properties.date}
* system property with a value that has backslash character.
* It is expected and verified in this test that such a value for the system property
* will not cause any malformed comments or introduce any new properties in the stored content.
* The launched program is expected to complete without any errors.
*/
private static void testBackSlashInSysPropValue() throws Exception {
final String[] sysPropVals = {"\\hello-world", "hello-world\\", "hello-world\\c=d",
"newline-plus-backslash\\\nc=d"};
for (final String sysPropVal : sysPropVals) {
final List<Path> storedFiles = new ArrayList<>();
for (int i = 0; i < 2; i++) {
final Path tmpFile = Files.createTempFile("8231640", ".props");
storedFiles.add(tmpFile);
final ProcessBuilder processBuilder = ProcessTools.createTestJavaProcessBuilder(
"-D" + SYS_PROP_JAVA_PROPERTIES_DATE + "=" + sysPropVal,
StoreTest.class.getName(),
tmpFile.toString(),
i % 2 == 0 ? "--use-outputstream" : "--use-writer");
executeJavaProcess(processBuilder);
if (!StoreTest.propsToStore.equals(loadProperties(tmpFile))) {
throw new RuntimeException("Unexpected properties stored in " + tmpFile);
}
String commentLine1 = findNthComment(tmpFile, 2);
if (commentLine1 == null) {
throw new RuntimeException("Did not find the expected comment line in " + tmpFile);
}
if (sysPropVal.contains("newline-plus-backslash")) {
if (!commentLine1.equals("newline-plus-backslash\\")) {
throw new RuntimeException("Unexpected comment line " + commentLine1 + " in " + tmpFile);
}
// we expect this specific system property value to be written out into 2 separate comment lines
String commentLine2 = findNthComment(tmpFile, 3);
if (commentLine2 == null) {
throw new RuntimeException(sysPropVal + " was expected to be split into 2 comment line, " +
"but wasn't, in " + tmpFile);
}
if (!commentLine2.equals("c=d")) {
throw new RuntimeException("Unexpected comment line " + commentLine2 + " in " + tmpFile);
}
} else {
if (!commentLine1.equals(sysPropVal)) {
throw new RuntimeException("Unexpected comment line " + commentLine1 + " in " + tmpFile);
}
}
}
assertAllFileContentsAreSame(storedFiles, sysPropVal);
}
}
// launches the java process and waits for it to exit. throws an exception if exit value is non-zero
private static void executeJavaProcess(ProcessBuilder pb) throws Exception {
final OutputAnalyzer outputAnalyzer = ProcessTools.executeProcess(pb);
try {
outputAnalyzer.shouldHaveExitValue(0);
} finally {
// print out any stdout/err that was generated in the launched program
outputAnalyzer.reportDiagnosticSummary();
}
}
// Properties.load() from the passed file and return the loaded Properties instance
private static Properties loadProperties(final Path file) throws IOException {
final Properties props = new Properties();
props.load(Files.newBufferedReader(file));
return props;
}
/**
* Verifies that the comment in the {@code destFile} is same as {@code expectedComment},
* instead of the default date comment.
*/
private static void assertExpectedComment(final Path destFile,
final String expectedComment) throws Exception {
final String actualComment = findNthComment(destFile, 2);
if (actualComment == null) {
throw new RuntimeException("Comment \"" + expectedComment + "\" not found in stored properties " + destFile);
}
if (!expectedComment.equals(actualComment)) {
throw new RuntimeException("Expected comment \"" + expectedComment + "\" but found \"" + actualComment + "\" " +
"in stored properties " + destFile);
}
}
/**
* Verifies that the date comment in the {@code destFile} can be parsed using the
* "EEE MMM dd HH:mm:ss zzz uuuu" format and the time represented by it is {@link Date#after(Date) after}
* the passed {@code date}
* The JVM runtime to invoke this method should set the time zone to UTC, i.e, specify
* "-Duser.timezone=UTC" at the command line. Otherwise, it will fail with some time
* zones that have ambiguous short names, such as "IST"
*/
private static void assertCurrentDate(final Path destFile, final Date date) throws Exception {
final String dateComment = findNthComment(destFile, 2);
if (dateComment == null) {
throw new RuntimeException("Date comment not found in stored properties " + destFile);
}
System.out.println("Found date comment " + dateComment + " in file " + destFile);
final Date parsedDate;
try {
Instant instant = Instant.from(FORMATTER.parse(dateComment));
parsedDate = new Date(instant.toEpochMilli());
} catch (DateTimeParseException pe) {
throw new RuntimeException("Unexpected date " + dateComment + " in stored properties " + destFile, pe);
}
if (!parsedDate.after(date)) {
throw new RuntimeException("Expected date comment " + dateComment + " to be after " + date
+ " but was " + parsedDate);
}
}
// returns the "Nth" comment from the file. Comment index starts from 1.
private static String findNthComment(Path file, int commentIndex) throws IOException {
List<String> comments = new ArrayList<>();
try (final BufferedReader reader = Files.newBufferedReader(file)) {
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("#")) {
comments.add(line.substring(1));
if (comments.size() == commentIndex) {
return comments.get(commentIndex - 1);
}
}
}
}
return null;
}
// verifies the byte equality of the contents in each of the files
private static void assertAllFileContentsAreSame(final List<Path> files,
final String sysPropVal) throws Exception {
final byte[] file1Contents = Files.readAllBytes(files.get(0));
for (int i = 1; i < files.size(); i++) {
final byte[] otherFileContents = Files.readAllBytes(files.get(i));
if (!Arrays.equals(file1Contents, otherFileContents)) {
throw new RuntimeException("Properties.store() did not generate reproducible content when " +
SYS_PROP_JAVA_PROPERTIES_DATE + " was set to " + sysPropVal);
}
}
}
static class StoreTest {
private static final Properties propsToStore = new Properties();
static {
propsToStore.setProperty("a", "b");
}
/**
* Uses Properties.store() APIs to store the properties into file
*/
public static void main(final String[] args) throws Exception {
final Path destFile = Path.of(args[0]);
final String comment = "some user specified comment";
System.out.println("Current default timezone is " + TimeZone.getDefault());
if (args[1].equals("--use-outputstream")) {
try (var os = Files.newOutputStream(destFile)) {
propsToStore.store(os, comment);
}
} else {
try (var br = Files.newBufferedWriter(destFile)) {
propsToStore.store(br, comment);
}
}
}
}
}
|