blob: 2c772632bb672a72e36e2d00160f90d9febdc728 [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
lberkiaea56b32017-05-30 12:35:33 +020016import 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
ccalvarin94c171c2018-08-16 15:42:19 -070019import com.google.devtools.build.lib.vfs.DigestHashFunction;
Ulf Adams8afbd3c2017-02-28 10:42:48 +000020import com.google.devtools.build.lib.vfs.FileSystem;
21import com.google.devtools.build.lib.vfs.FileSystemUtils;
22import com.google.devtools.build.lib.vfs.Path;
23import com.google.devtools.build.lib.vfs.SymlinkAwareFileSystemTest;
24import com.google.devtools.build.lib.vfs.Symlinks;
25import java.io.IOException;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010026import org.junit.Test;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010027
Ulf Adams8afbd3c2017-02-28 10:42:48 +000028/** Tests for the {@link com.google.devtools.build.lib.unix.UnixFileSystem} class. */
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010029public class UnixFileSystemTest extends SymlinkAwareFileSystemTest {
30
31 @Override
ccalvarin94c171c2018-08-16 15:42:19 -070032 protected FileSystem getFreshFileSystem(DigestHashFunction digestHashFunction) {
Ed Schouten05650ff2020-09-17 10:01:25 -070033 return new UnixFileSystem(digestHashFunction, /*hashAttributeName=*/ "");
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010034 }
35
36 @Override
37 public void destroyFileSystem(FileSystem fileSystem) {
38 // Nothing.
39 }
40
41 @Override
42 protected void expectNotFound(Path path) throws IOException {
lberkiaea56b32017-05-30 12:35:33 +020043 assertThat(path.statIfFound()).isNull();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010044 }
45
46 // Most tests are just inherited from FileSystemTest.
47
48 @Test
49 public void testCircularSymlinkFound() throws Exception {
50 Path linkA = absolutize("link-a");
51 Path linkB = absolutize("link-b");
52 linkA.createSymbolicLink(linkB);
53 linkB.createSymbolicLink(linkA);
lberkiaea56b32017-05-30 12:35:33 +020054 assertThat(linkA.exists(Symlinks.FOLLOW)).isFalse();
jcater6cfebc92019-05-01 12:40:08 -070055 assertThrows(IOException.class, () -> linkA.statIfFound(Symlinks.FOLLOW));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010056 }
Nathan Harmatad8b6ff22015-10-20 21:54:34 +000057
58 @Test
59 public void testIsSpecialFile() throws Exception {
60 Path regular = absolutize("regular");
61 Path fifo = absolutize("fifo");
62 FileSystemUtils.createEmptyFile(regular);
Lukacs Berki7ecb2ce2016-01-26 15:40:42 +000063 NativePosixFiles.mkfifo(fifo.toString(), 0777);
lberkiaea56b32017-05-30 12:35:33 +020064 assertThat(regular.isFile()).isTrue();
65 assertThat(regular.isSpecialFile()).isFalse();
66 assertThat(regular.stat().isFile()).isTrue();
67 assertThat(regular.stat().isSpecialFile()).isFalse();
68 assertThat(fifo.isFile()).isTrue();
69 assertThat(fifo.isSpecialFile()).isTrue();
70 assertThat(fifo.stat().isFile()).isTrue();
71 assertThat(fifo.stat().isSpecialFile()).isTrue();
Nathan Harmatad8b6ff22015-10-20 21:54:34 +000072 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010073}