From 97a0e184896825bfce97ab81d8f4317f7704c3f7 Mon Sep 17 00:00:00 2001 From: cpovirk Date: Tue, 26 Sep 2023 11:41:37 -0700 Subject: [PATCH] clone of https://github.com/google/guava/pull/6730 but without the prod change -- should fail Windows testing Fix `Files.createTempDir` and `FileBackedOutputStream` under Windows _services_, a rare use case. Fixes https://github.com/google/guava/issues/6634 Relevant to https://github.com/google/guava/issues/2686 in that it shows that we would ideally run our Windows testing under both Java 8 *and* a newer version.... RELNOTES=`io`: Fixed `Files.createTempDir` and `FileBackedOutputStream` under [Windows _services_, a rare use case](https://github.com/google/guava/issues/6634). PiperOrigin-RevId: 568604081 --- .github/workflows/ci.yml | 4 +-- .../common/io/FilesCreateTempDirTest.java | 36 +++++++++++++++++++ .../common/io/FilesCreateTempDirTest.java | 36 +++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 902dac31c755..766b0e5ca663 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,8 +20,8 @@ jobs: strategy: matrix: os: [ ubuntu-latest ] - java: [ 8, 11, 17 ] - root-pom: [ 'pom.xml', 'android/pom.xml' ] + java: [ 8 ] + root-pom: [ 'pom.xml' ] include: - os: windows-latest java: 17 diff --git a/android/guava-tests/test/com/google/common/io/FilesCreateTempDirTest.java b/android/guava-tests/test/com/google/common/io/FilesCreateTempDirTest.java index a31c43770e3b..c0c74d6640ec 100644 --- a/android/guava-tests/test/com/google/common/io/FilesCreateTempDirTest.java +++ b/android/guava-tests/test/com/google/common/io/FilesCreateTempDirTest.java @@ -17,6 +17,7 @@ package com.google.common.io; import static com.google.common.base.StandardSystemProperty.JAVA_IO_TMPDIR; +import static com.google.common.base.StandardSystemProperty.JAVA_SPECIFICATION_VERSION; import static com.google.common.base.StandardSystemProperty.OS_NAME; import static com.google.common.truth.Truth.assertThat; import static java.nio.file.attribute.PosixFilePermission.OWNER_EXECUTE; @@ -64,6 +65,41 @@ public void testCreateTempDir() throws IOException { } } + public void testBogusSystemPropertiesUsername() { + /* + * 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. + * + * Under Windows, we 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(); + + String save = System.getProperty("user.name"); + System.setProperty("user.name", "-this-is-definitely-not-the-username-we-are-running-as//?"); + File temp = null; + try { + temp = Files.createTempDir(); + assertThat(isJava8OnWindows).isFalse(); + } catch (IllegalStateException expectedIfJavaWindows8) { + assertThat(isJava8OnWindows).isTrue(); + } finally { + System.setProperty("user.name", save); + if (temp != null) { + assertThat(temp.delete()).isTrue(); + } + } + } + private static boolean isAndroid() { return System.getProperty("java.runtime.name", "").contains("Android"); } diff --git a/guava-tests/test/com/google/common/io/FilesCreateTempDirTest.java b/guava-tests/test/com/google/common/io/FilesCreateTempDirTest.java index a31c43770e3b..c0c74d6640ec 100644 --- a/guava-tests/test/com/google/common/io/FilesCreateTempDirTest.java +++ b/guava-tests/test/com/google/common/io/FilesCreateTempDirTest.java @@ -17,6 +17,7 @@ package com.google.common.io; import static com.google.common.base.StandardSystemProperty.JAVA_IO_TMPDIR; +import static com.google.common.base.StandardSystemProperty.JAVA_SPECIFICATION_VERSION; import static com.google.common.base.StandardSystemProperty.OS_NAME; import static com.google.common.truth.Truth.assertThat; import static java.nio.file.attribute.PosixFilePermission.OWNER_EXECUTE; @@ -64,6 +65,41 @@ public void testCreateTempDir() throws IOException { } } + public void testBogusSystemPropertiesUsername() { + /* + * 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. + * + * Under Windows, we 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(); + + String save = System.getProperty("user.name"); + System.setProperty("user.name", "-this-is-definitely-not-the-username-we-are-running-as//?"); + File temp = null; + try { + temp = Files.createTempDir(); + assertThat(isJava8OnWindows).isFalse(); + } catch (IllegalStateException expectedIfJavaWindows8) { + assertThat(isJava8OnWindows).isTrue(); + } finally { + System.setProperty("user.name", save); + if (temp != null) { + assertThat(temp.delete()).isTrue(); + } + } + } + private static boolean isAndroid() { return System.getProperty("java.runtime.name", "").contains("Android"); }