github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/fuse/linux/unlink_test.cc (about)

     1  // Copyright 2020 The gVisor Authors.
     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.
    14  
    15  #include <errno.h>
    16  #include <fcntl.h>
    17  #include <linux/fuse.h>
    18  #include <sys/mount.h>
    19  #include <sys/stat.h>
    20  #include <sys/statfs.h>
    21  #include <sys/types.h>
    22  #include <unistd.h>
    23  
    24  #include <string>
    25  #include <vector>
    26  
    27  #include "gtest/gtest.h"
    28  #include "test/fuse/linux/fuse_base.h"
    29  #include "test/util/fuse_util.h"
    30  #include "test/util/test_util.h"
    31  
    32  namespace gvisor {
    33  namespace testing {
    34  
    35  namespace {
    36  
    37  class UnlinkTest : public FuseTest {
    38   protected:
    39    const std::string test_file_ = "test_file";
    40    const std::string test_subdir_ = "test_subdir";
    41  };
    42  
    43  TEST_F(UnlinkTest, RegularFile) {
    44    const std::string test_file_path =
    45        JoinPath(mount_point_.path().c_str(), test_file_);
    46    SetServerInodeLookup(test_file_, S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO);
    47  
    48    struct fuse_out_header out_header = {
    49        .len = sizeof(struct fuse_out_header),
    50    };
    51    auto iov_out = FuseGenerateIovecs(out_header);
    52    SetServerResponse(FUSE_UNLINK, iov_out);
    53  
    54    ASSERT_THAT(unlink(test_file_path.c_str()), SyscallSucceeds());
    55    struct fuse_in_header in_header;
    56    std::vector<char> unlinked_file(test_file_.length() + 1);
    57    auto iov_in = FuseGenerateIovecs(in_header, unlinked_file);
    58    GetServerActualRequest(iov_in);
    59  
    60    EXPECT_EQ(in_header.len, sizeof(in_header) + test_file_.length() + 1);
    61    EXPECT_EQ(in_header.opcode, FUSE_UNLINK);
    62    EXPECT_EQ(std::string(unlinked_file.data()), test_file_);
    63  }
    64  
    65  TEST_F(UnlinkTest, RegularFileSubDir) {
    66    SetServerInodeLookup(test_subdir_, S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO);
    67    const std::string test_file_path =
    68        JoinPath(mount_point_.path().c_str(), test_subdir_, test_file_);
    69    SetServerInodeLookup(test_file_, S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO);
    70  
    71    struct fuse_out_header out_header = {
    72        .len = sizeof(struct fuse_out_header),
    73    };
    74    auto iov_out = FuseGenerateIovecs(out_header);
    75    SetServerResponse(FUSE_UNLINK, iov_out);
    76  
    77    ASSERT_THAT(unlink(test_file_path.c_str()), SyscallSucceeds());
    78    struct fuse_in_header in_header;
    79    std::vector<char> unlinked_file(test_file_.length() + 1);
    80    auto iov_in = FuseGenerateIovecs(in_header, unlinked_file);
    81    GetServerActualRequest(iov_in);
    82  
    83    EXPECT_EQ(in_header.len, sizeof(in_header) + test_file_.length() + 1);
    84    EXPECT_EQ(in_header.opcode, FUSE_UNLINK);
    85    EXPECT_EQ(std::string(unlinked_file.data()), test_file_);
    86  }
    87  
    88  TEST_F(UnlinkTest, NoFile) {
    89    const std::string test_file_path =
    90        JoinPath(mount_point_.path().c_str(), test_file_);
    91    SetServerInodeLookup(test_file_, S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO);
    92  
    93    struct fuse_out_header out_header = {
    94        .len = sizeof(struct fuse_out_header),
    95        .error = -ENOENT,
    96    };
    97    auto iov_out = FuseGenerateIovecs(out_header);
    98    SetServerResponse(FUSE_UNLINK, iov_out);
    99  
   100    ASSERT_THAT(unlink(test_file_path.c_str()), SyscallFailsWithErrno(ENOENT));
   101    SkipServerActualRequest();
   102  }
   103  
   104  }  // namespace
   105  
   106  }  // namespace testing
   107  }  // namespace gvisor