Skip to content

Commit

Permalink
"merge"
Browse files Browse the repository at this point in the history
  • Loading branch information
cpovirk committed Sep 26, 2023
1 parent 0ed8fae commit bb94109
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,39 @@ public void testCreateTempDir() throws IOException {
}

public void testBogusSystemPropertiesUsername() {
if (isAndroid()) {
/*
* The test calls directly into the "ACL-based filesystem" code, which isn't available under
* old versions of Android. Since Android doesn't use that code path, anyway, there's no need
* to test it.
*/
return;
}

/*
* Only under Windows (or hypothetically when running with some other non-POSIX, ACL-based
* filesystem) do we look up the username. Thus, this test doesn't test anything interesting
* under most environments.
* filesystem) does our prod code look up the username. Thus, this test doesn't necessarily test
* anything interesting under most environments. Still, we can run it (except for Android, at
* least old versions), so we mostly do. This is useful because we don't actually run our CI on
* Windows under Java 8, at least as of this writing.
*
* Under Windows, we test that:
* Under Windows in particular, we want to test that:
*
* - Under Java 9+, createTempDir() succeeds because it can look up the *real* username, rather
* than relying on the one from the system property.
*
* - Under Java 8, createTempDir() fails because it falls back to the bogus username from the
* system property.
*
* However: Note that we don't actually run our CI on Windows under Java 8, at least as of this
* writing.
*/
boolean isJava8OnWindows = JAVA_SPECIFICATION_VERSION.value().equals("1.8") && isWindows();
boolean isJava8 = JAVA_SPECIFICATION_VERSION.value().equals("1.8");

String save = System.getProperty("user.name");
System.setProperty("user.name", "-this-is-definitely-not-the-username-we-are-running-as//?");
try {
TempFileCreator.testMakingUserPermissionsFromScratch();
assertThat(isJava8OnWindows).isFalse();
} catch (IOException expectedIfJavaWindows8) {
assertThat(isJava8OnWindows).isTrue();
assertThat(isJava8).isFalse();
} catch (IOException expectedIfJava8) {
assertThat(isJava8).isTrue();
} finally {
System.setProperty("user.name", save);
}
Expand Down
5 changes: 4 additions & 1 deletion android/guava/src/com/google/common/io/TempFileCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ private static TempFileCreator pickSecureCreator() {

/**
* Creates the permissions normally used for Windows filesystems, looking up the user afresh, even
* if previous calls have initialized the PermissionSupplier fields.
* if previous calls have initialized the {@code PermissionSupplier} fields.
*
* <p>This lets us test the effects of different values of the {@code user.name} system property
* without needing a separate VM or classloader.
*/
@IgnoreJRERequirement // used only when Path is available (and only from tests)
@VisibleForTesting
Expand Down
28 changes: 18 additions & 10 deletions guava-tests/test/com/google/common/io/FilesCreateTempDirTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,39 @@ public void testCreateTempDir() throws IOException {
}

public void testBogusSystemPropertiesUsername() {
if (isAndroid()) {
/*
* The test calls directly into the "ACL-based filesystem" code, which isn't available under
* old versions of Android. Since Android doesn't use that code path, anyway, there's no need
* to test it.
*/
return;
}

/*
* Only under Windows (or hypothetically when running with some other non-POSIX, ACL-based
* filesystem) do we look up the username. Thus, this test doesn't test anything interesting
* under most environments.
* filesystem) does our prod code look up the username. Thus, this test doesn't necessarily test
* anything interesting under most environments. Still, we can run it (except for Android, at
* least old versions), so we mostly do. This is useful because we don't actually run our CI on
* Windows under Java 8, at least as of this writing.
*
* Under Windows, we test that:
* Under Windows in particular, we want to test that:
*
* - Under Java 9+, createTempDir() succeeds because it can look up the *real* username, rather
* than relying on the one from the system property.
*
* - Under Java 8, createTempDir() fails because it falls back to the bogus username from the
* system property.
*
* However: Note that we don't actually run our CI on Windows under Java 8, at least as of this
* writing.
*/
boolean isJava8OnWindows = JAVA_SPECIFICATION_VERSION.value().equals("1.8") && isWindows();
boolean isJava8 = JAVA_SPECIFICATION_VERSION.value().equals("1.8");

String save = System.getProperty("user.name");
System.setProperty("user.name", "-this-is-definitely-not-the-username-we-are-running-as//?");
try {
TempFileCreator.testMakingUserPermissionsFromScratch();
assertThat(isJava8OnWindows).isFalse();
} catch (IOException expectedIfJavaWindows8) {
assertThat(isJava8OnWindows).isTrue();
assertThat(isJava8).isFalse();
} catch (IOException expectedIfJava8) {
assertThat(isJava8).isTrue();
} finally {
System.setProperty("user.name", save);
}
Expand Down

0 comments on commit bb94109

Please sign in to comment.