|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 | +/* @test |
| 25 | + * @bug 8295753 |
| 26 | + * @summary Verify correct operation of Path.toRealPath |
| 27 | + * @library .. /test/lib |
| 28 | + * @build ToRealPath jdk.test.lib.Platform |
| 29 | + * @run junit ToRealPath |
| 30 | + */ |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.io.UncheckedIOException; |
| 34 | +import java.nio.file.Files; |
| 35 | +import java.nio.file.Path; |
| 36 | + |
| 37 | +import jdk.test.lib.Platform; |
| 38 | + |
| 39 | +import org.junit.jupiter.api.AfterAll; |
| 40 | +import org.junit.jupiter.api.Test; |
| 41 | +import org.junit.jupiter.api.condition.EnabledIf; |
| 42 | +import org.junit.jupiter.api.condition.EnabledOnOs; |
| 43 | +import org.junit.jupiter.api.condition.OS; |
| 44 | + |
| 45 | +import static java.nio.file.LinkOption.*; |
| 46 | +import static org.junit.jupiter.api.Assertions.*; |
| 47 | + |
| 48 | +public class ToRealPath { |
| 49 | + static final boolean SUPPORTS_LINKS; |
| 50 | + static final Path DIR; |
| 51 | + static final Path SUBDIR; |
| 52 | + static final Path FILE; |
| 53 | + static final Path LINK; |
| 54 | + |
| 55 | + static { |
| 56 | + try { |
| 57 | + DIR = TestUtil.createTemporaryDirectory(); |
| 58 | + SUBDIR = Files.createDirectory(DIR.resolve("subdir")); |
| 59 | + FILE = Files.createFile(DIR.resolve("foo")); |
| 60 | + LINK = DIR.resolve("link"); |
| 61 | + SUPPORTS_LINKS = TestUtil.supportsLinks(DIR); |
| 62 | + } catch (IOException e) { |
| 63 | + throw new UncheckedIOException(e); |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + public boolean supportsLinks() { |
| 68 | + return SUPPORTS_LINKS; |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void locateSameFile() throws IOException { |
| 73 | + assertTrue(Files.isSameFile(FILE.toRealPath(), |
| 74 | + FILE.toRealPath(NOFOLLOW_LINKS))); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void failNotExist() { |
| 79 | + Path doesNotExist = DIR.resolve("DoesNotExist"); |
| 80 | + assertThrows(IOException.class, () -> doesNotExist.toRealPath()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void failNotExistNoFollow() { |
| 85 | + Path doesNotExist = DIR.resolve("DoesNotExist"); |
| 86 | + assertThrows(IOException.class, |
| 87 | + () -> doesNotExist.toRealPath(NOFOLLOW_LINKS)); |
| 88 | + } |
| 89 | + |
| 90 | + @EnabledIf("supportsLinks") |
| 91 | + @Test |
| 92 | + public void shouldResolveLinks() throws IOException { |
| 93 | + Path resolvedFile = FILE; |
| 94 | + if (Platform.isWindows()) { |
| 95 | + // Path::toRealPath does not work with environments using the |
| 96 | + // legacy subst mechanism. This is a workaround to keep the |
| 97 | + // test working if 'dir' points to a location on a subst drive. |
| 98 | + // See JDK-8213216. |
| 99 | + // |
| 100 | + Path tempLink = DIR.resolve("tempLink"); |
| 101 | + Files.createSymbolicLink(tempLink, DIR.toAbsolutePath()); |
| 102 | + Path resolvedDir = tempLink.toRealPath(); |
| 103 | + Files.delete(tempLink); |
| 104 | + resolvedFile = resolvedDir.resolve(FILE.getFileName()); |
| 105 | + } |
| 106 | + |
| 107 | + Files.createSymbolicLink(LINK, resolvedFile.toAbsolutePath()); |
| 108 | + assertTrue(LINK.toRealPath().equals(resolvedFile.toRealPath())); |
| 109 | + Files.delete(LINK); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + @EnabledIf("supportsLinks") |
| 114 | + public void shouldNotResolveLinks() throws IOException { |
| 115 | + Files.createSymbolicLink(LINK, FILE.toAbsolutePath()); |
| 116 | + assertEquals(LINK.toRealPath(NOFOLLOW_LINKS).getFileName(), |
| 117 | + LINK.getFileName()); |
| 118 | + Files.delete(LINK); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void eliminateDot() throws IOException { |
| 123 | + assertEquals(DIR.resolve(".").toRealPath(), |
| 124 | + DIR.toRealPath()); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void eliminateDotNoFollow() throws IOException { |
| 129 | + assertEquals(DIR.resolve(".").toRealPath(NOFOLLOW_LINKS), |
| 130 | + DIR.toRealPath(NOFOLLOW_LINKS)); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void eliminateDots() throws IOException { |
| 135 | + assertEquals(SUBDIR.resolve("..").toRealPath(), |
| 136 | + DIR.toRealPath()); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void eliminateDotsNoFollow() throws IOException { |
| 141 | + assertEquals(SUBDIR.resolve("..").toRealPath(NOFOLLOW_LINKS), |
| 142 | + DIR.toRealPath(NOFOLLOW_LINKS)); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + @EnabledOnOs(OS.MAC) |
| 147 | + public final void macOSTests() throws IOException { |
| 148 | + // theTarget = dir/subdir/theTarget |
| 149 | + Path theTarget = Path.of(SUBDIR.toString(), "theTarget"); |
| 150 | + Files.createFile(theTarget); |
| 151 | + |
| 152 | + // dir/theLink -> dir/subdir |
| 153 | + Path theLink = Path.of(DIR.toString(), "theLink"); |
| 154 | + Files.createSymbolicLink(theLink, SUBDIR); |
| 155 | + |
| 156 | + // thePath = dir/thelink/thetarget (all lower case) |
| 157 | + Path thePath = Path.of(DIR.toString(), "thelink", "thetarget"); |
| 158 | + Path noFollow = thePath.toRealPath(NOFOLLOW_LINKS); |
| 159 | + int nc = noFollow.getNameCount(); |
| 160 | + |
| 161 | + // Real path should retain case as dir/theLink/theTarget |
| 162 | + assertEquals(noFollow.getName(nc - 2), Path.of("theLink")); |
| 163 | + assertEquals(noFollow.getName(nc - 1), Path.of("theTarget")); |
| 164 | + assertEquals(noFollow.toString(), |
| 165 | + Path.of(DIR.toString(), "theLink", "theTarget").toString()); |
| 166 | + |
| 167 | + // Test where a link is preceded by ".." in the path |
| 168 | + Path superBeforeLink = |
| 169 | + Path.of(SUBDIR.toString(), "..", "thelink", "thetarget"); |
| 170 | + noFollow = superBeforeLink.toRealPath(NOFOLLOW_LINKS); |
| 171 | + nc = noFollow.getNameCount(); |
| 172 | + assertEquals(noFollow.getName(nc - 2), Path.of("theLink")); |
| 173 | + assertEquals(noFollow.getName(nc - 1), Path.of("theTarget")); |
| 174 | + |
| 175 | + // Test where a link is followed by ".." in the path |
| 176 | + Path linkBeforeSuper = |
| 177 | + Path.of(DIR.toString(), "thelink", "..", "subdir", "thetarget"); |
| 178 | + noFollow = linkBeforeSuper.toRealPath(NOFOLLOW_LINKS); |
| 179 | + nc = noFollow.getNameCount(); |
| 180 | + assertEquals(noFollow.getName(nc - 4), Path.of("theLink")); |
| 181 | + assertEquals(noFollow.getName(nc - 1), Path.of("theTarget")); |
| 182 | + |
| 183 | + Files.delete(theTarget); |
| 184 | + } |
| 185 | + |
| 186 | + @AfterAll |
| 187 | + public static void cleanup() throws IOException { |
| 188 | + Files.delete(SUBDIR); |
| 189 | + Files.delete(FILE); |
| 190 | + } |
| 191 | +} |
0 commit comments