1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.io; 18 19 import static com.google.common.base.StandardSystemProperty.JAVA_IO_TMPDIR; 20 import static com.google.common.base.StandardSystemProperty.JAVA_SPECIFICATION_VERSION; 21 import static com.google.common.base.StandardSystemProperty.OS_NAME; 22 import static com.google.common.truth.Truth.assertThat; 23 import static java.nio.file.attribute.PosixFilePermission.OWNER_EXECUTE; 24 import static java.nio.file.attribute.PosixFilePermission.OWNER_READ; 25 import static java.nio.file.attribute.PosixFilePermission.OWNER_WRITE; 26 import static org.junit.Assert.assertThrows; 27 28 import java.io.File; 29 import java.io.IOException; 30 import java.nio.file.attribute.PosixFileAttributeView; 31 import java.nio.file.attribute.PosixFileAttributes; 32 import junit.framework.TestCase; 33 34 /** 35 * Unit test for {@link Files#createTempDir}. 36 * 37 * @author Chris Nokleberg 38 */ 39 40 @SuppressWarnings("deprecation") // tests of a deprecated method 41 public class FilesCreateTempDirTest extends TestCase { testCreateTempDir()42 public void testCreateTempDir() throws IOException { 43 if (JAVA_IO_TMPDIR.value().equals("/sdcard")) { 44 assertThrows(IllegalStateException.class, Files::createTempDir); 45 return; 46 } 47 File temp = Files.createTempDir(); 48 try { 49 assertThat(temp.exists()).isTrue(); 50 assertThat(temp.isDirectory()).isTrue(); 51 assertThat(temp.listFiles()).isEmpty(); 52 File child = new File(temp, "child"); 53 assertThat(child.createNewFile()).isTrue(); 54 assertThat(child.delete()).isTrue(); 55 56 if (!isAndroid() && !isWindows()) { 57 PosixFileAttributes attributes = 58 java.nio.file.Files.getFileAttributeView(temp.toPath(), PosixFileAttributeView.class) 59 .readAttributes(); 60 assertThat(attributes.permissions()) 61 .containsExactly(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE); 62 } 63 } finally { 64 assertThat(temp.delete()).isTrue(); 65 } 66 } 67 testBogusSystemPropertiesUsername()68 public void testBogusSystemPropertiesUsername() { 69 if (isAndroid()) { 70 /* 71 * The test calls directly into the "ACL-based filesystem" code, which isn't available under 72 * old versions of Android. Since Android doesn't use that code path, anyway, there's no need 73 * to test it. 74 */ 75 return; 76 } 77 78 /* 79 * Only under Windows (or hypothetically when running with some other non-POSIX, ACL-based 80 * filesystem) does our prod code look up the username. Thus, this test doesn't necessarily test 81 * anything interesting under most environments. Still, we can run it (except for Android, at 82 * least old versions), so we mostly do. This is useful because we don't actually run our CI on 83 * Windows under Java 8, at least as of this writing. 84 * 85 * Under Windows in particular, we want to test that: 86 * 87 * - Under Java 9+, createTempDir() succeeds because it can look up the *real* username, rather 88 * than relying on the one from the system property. 89 * 90 * - Under Java 8, createTempDir() fails because it falls back to the bogus username from the 91 * system property. 92 */ 93 94 String save = System.getProperty("user.name"); 95 System.setProperty("user.name", "-this-is-definitely-not-the-username-we-are-running-as//?"); 96 try { 97 TempFileCreator.testMakingUserPermissionsFromScratch(); 98 assertThat(isJava8()).isFalse(); 99 } catch (IOException expectedIfJava8) { 100 assertThat(isJava8()).isTrue(); 101 } finally { 102 System.setProperty("user.name", save); 103 } 104 } 105 isAndroid()106 private static boolean isAndroid() { 107 return System.getProperty("java.runtime.name", "").contains("Android"); 108 } 109 isWindows()110 private static boolean isWindows() { 111 return OS_NAME.value().startsWith("Windows"); 112 } 113 isJava8()114 private static boolean isJava8() { 115 return JAVA_SPECIFICATION_VERSION.value().equals("1.8"); 116 } 117 } 118