Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 14 | package com.google.devtools.build.lib.unix; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
michajlo | 660d17f | 2020-03-27 09:01:57 -0700 | [diff] [blame] | 17 | import static org.junit.Assert.assertThrows; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 19 | import com.google.common.testing.EqualsTester; |
ccalvarin | 8794308 | 2018-08-20 06:56:54 -0700 | [diff] [blame] | 20 | import com.google.devtools.build.lib.vfs.DigestHashFunction; |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.vfs.FileSystem; |
| 22 | import com.google.devtools.build.lib.vfs.Path; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.junit.runner.RunWith; |
| 26 | import org.junit.runners.JUnit4; |
| 27 | |
| 28 | /** |
| 29 | * This tests how canonical paths and non-canonical paths are equal with each |
| 30 | * other, and also how paths from different filesystems behave with each other. |
| 31 | */ |
| 32 | @RunWith(JUnit4.class) |
| 33 | public class UnixPathEqualityTest { |
| 34 | |
| 35 | private FileSystem otherUnixFs; |
| 36 | private FileSystem unixFs; |
| 37 | |
| 38 | @Before |
Ed Schouten | 05650ff | 2020-09-17 10:01:25 -0700 | [diff] [blame] | 39 | public final void initializeFileSystem() { |
| 40 | unixFs = new UnixFileSystem(DigestHashFunction.SHA256, /*hashAttributeName=*/ ""); |
| 41 | otherUnixFs = new UnixFileSystem(DigestHashFunction.SHA256, /*hashAttributeName=*/ ""); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 42 | assertThat(unixFs != otherUnixFs).isTrue(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | private void assertTwoWayEquals(Object obj1, Object obj2) { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 46 | assertThat(obj1).isEqualTo(obj2); |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 47 | new EqualsTester().addEqualityGroup(obj1, obj2).testEquals(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | private void assertTwoWayNotEquals(Object obj1, Object obj2) { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 51 | assertThat(obj1.equals(obj2)).isFalse(); |
| 52 | assertThat(obj2.equals(obj1)).isFalse(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void testPathsAreEqualEvenIfNotCanonical() { |
| 57 | // This path is already canonical, so there's no difference between |
| 58 | // the canonical / nonCanonical path, as far as equals is concerned |
| 59 | Path nonCanonical = unixFs.getPath("/a/canonical/unix/path"); |
| 60 | Path canonical = unixFs.getPath("/a/canonical/unix/path"); |
| 61 | assertTwoWayEquals(nonCanonical, canonical); |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | public void testPathsAreNeverEqualWithStrings() { |
| 66 | // Make sure that paths aren't equal to plain old strings |
| 67 | Path nonCanonical = unixFs.getPath("/a/non/../canonical/unix/path"); |
| 68 | Path canonical = unixFs.getPath("/a/non/../canonical/unix/path"); |
| 69 | assertTwoWayNotEquals(nonCanonical, "/a/non/../canonical/unix/path"); |
| 70 | assertTwoWayNotEquals(canonical, "/a/non/../canonical/unix/path"); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void testCanonicalPathsFromDifferentFileSystemsAreNeverEqual() { |
| 75 | Path canonical = unixFs.getPath("/canonical/path"); |
| 76 | Path otherCanonical = otherUnixFs.getPath("/canonical/path"); |
| 77 | assertTwoWayNotEquals(canonical, otherCanonical); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void testNonCanonicalPathsFromDifferentFileSystemsAreNeverEqual() { |
| 82 | Path nonCanonical = unixFs.getPath("/non/canonical/path"); |
| 83 | Path otherNonCanonical = otherUnixFs.getPath("/non/canonical/path"); |
| 84 | assertTwoWayNotEquals(nonCanonical, otherNonCanonical); |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void testCrossFilesystemStartsWithReturnsFalse() { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 89 | assertThat(unixFs.getPath("/a").startsWith(otherUnixFs.getPath("/b"))).isFalse(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void testCrossFilesystemOperationsForbidden() throws Exception { |
| 94 | Path a = unixFs.getPath("/a"); |
| 95 | Path b = otherUnixFs.getPath("/b"); |
| 96 | |
jcater | b0c7ee5 | 2019-05-02 12:33:13 -0700 | [diff] [blame] | 97 | assertThrows(IllegalArgumentException.class, () -> a.renameTo(b)); |
| 98 | assertThrows(IllegalArgumentException.class, () -> a.relativeTo(b)); |
| 99 | assertThrows(IllegalArgumentException.class, () -> a.createSymbolicLink(b)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 100 | } |
| 101 | } |