blob: 1b8b0aaa000076f943c6f1dc8bcb31817a94f003 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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 Adams8afbd3c2017-02-28 10:42:48 +000014package com.google.devtools.build.lib.unix;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010015
16import static com.google.common.truth.Truth.assertThat;
michajlo660d17f2020-03-27 09:01:57 -070017import static org.junit.Assert.assertThrows;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018
Ulf Adams795895a2015-03-06 15:58:35 +000019import com.google.common.testing.EqualsTester;
ccalvarin87943082018-08-20 06:56:54 -070020import com.google.devtools.build.lib.vfs.DigestHashFunction;
Ulf Adams8afbd3c2017-02-28 10:42:48 +000021import com.google.devtools.build.lib.vfs.FileSystem;
22import com.google.devtools.build.lib.vfs.Path;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010023import org.junit.Before;
24import org.junit.Test;
25import org.junit.runner.RunWith;
26import 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)
33public class UnixPathEqualityTest {
34
35 private FileSystem otherUnixFs;
36 private FileSystem unixFs;
37
38 @Before
Ed Schouten05650ff2020-09-17 10:01:25 -070039 public final void initializeFileSystem() {
40 unixFs = new UnixFileSystem(DigestHashFunction.SHA256, /*hashAttributeName=*/ "");
41 otherUnixFs = new UnixFileSystem(DigestHashFunction.SHA256, /*hashAttributeName=*/ "");
lberkiaea56b32017-05-30 12:35:33 +020042 assertThat(unixFs != otherUnixFs).isTrue();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010043 }
44
45 private void assertTwoWayEquals(Object obj1, Object obj2) {
lberkiaea56b32017-05-30 12:35:33 +020046 assertThat(obj1).isEqualTo(obj2);
Ulf Adams795895a2015-03-06 15:58:35 +000047 new EqualsTester().addEqualityGroup(obj1, obj2).testEquals();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010048 }
49
50 private void assertTwoWayNotEquals(Object obj1, Object obj2) {
lberkiaea56b32017-05-30 12:35:33 +020051 assertThat(obj1.equals(obj2)).isFalse();
52 assertThat(obj2.equals(obj1)).isFalse();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010053 }
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() {
lberkiaea56b32017-05-30 12:35:33 +020089 assertThat(unixFs.getPath("/a").startsWith(otherUnixFs.getPath("/b"))).isFalse();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010090 }
91
92 @Test
93 public void testCrossFilesystemOperationsForbidden() throws Exception {
94 Path a = unixFs.getPath("/a");
95 Path b = otherUnixFs.getPath("/b");
96
jcaterb0c7ee52019-05-02 12:33:13 -070097 assertThrows(IllegalArgumentException.class, () -> a.renameTo(b));
98 assertThrows(IllegalArgumentException.class, () -> a.relativeTo(b));
99 assertThrows(IllegalArgumentException.class, () -> a.createSymbolicLink(b));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100100 }
101}