github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/fuse/linux/symlink_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/stat.h>
    19  #include <sys/statfs.h>
    20  #include <sys/types.h>
    21  #include <unistd.h>
    22  
    23  #include <string>
    24  #include <vector>
    25  
    26  #include "gtest/gtest.h"
    27  #include "test/fuse/linux/fuse_base.h"
    28  #include "test/util/fuse_util.h"
    29  #include "test/util/test_util.h"
    30  
    31  namespace gvisor {
    32  namespace testing {
    33  
    34  namespace {
    35  
    36  class SymlinkTest : public FuseTest {
    37   protected:
    38    const std::string target_file_ = "target_file_";
    39    const std::string symlink_ = "symlink_";
    40    const mode_t perms_ = S_IRWXU | S_IRWXG | S_IRWXO;
    41  };
    42  
    43  TEST_F(SymlinkTest, CreateSymLink) {
    44    const std::string symlink_path =
    45        JoinPath(mount_point_.path().c_str(), symlink_);
    46  
    47    struct fuse_out_header out_header = {
    48        .len = sizeof(struct fuse_out_header) + sizeof(struct fuse_entry_out),
    49    };
    50    struct fuse_entry_out out_payload = DefaultEntryOut(S_IFLNK | perms_, 5);
    51    auto iov_out = FuseGenerateIovecs(out_header, out_payload);
    52    SetServerResponse(FUSE_SYMLINK, iov_out);
    53    ASSERT_THAT(symlink(target_file_.c_str(), symlink_path.c_str()),
    54                SyscallSucceeds());
    55  
    56    struct fuse_in_header in_header;
    57    std::vector<char> actual_target_file(target_file_.length() + 1);
    58    std::vector<char> actual_symlink(symlink_.length() + 1);
    59    auto iov_in =
    60        FuseGenerateIovecs(in_header, actual_symlink, actual_target_file);
    61    GetServerActualRequest(iov_in);
    62  
    63    EXPECT_EQ(in_header.len,
    64              sizeof(in_header) + symlink_.length() + target_file_.length() + 2);
    65    EXPECT_EQ(in_header.opcode, FUSE_SYMLINK);
    66    EXPECT_EQ(std::string(actual_target_file.data()), target_file_);
    67    EXPECT_EQ(std::string(actual_symlink.data()), symlink_);
    68  }
    69  
    70  TEST_F(SymlinkTest, FileTypeError) {
    71    const std::string symlink_path =
    72        JoinPath(mount_point_.path().c_str(), symlink_);
    73  
    74    struct fuse_out_header out_header = {
    75        .len = sizeof(struct fuse_out_header) + sizeof(struct fuse_entry_out),
    76    };
    77    struct fuse_entry_out out_payload = DefaultEntryOut(S_IFREG | perms_, 5);
    78    auto iov_out = FuseGenerateIovecs(out_header, out_payload);
    79    SetServerResponse(FUSE_SYMLINK, iov_out);
    80    ASSERT_THAT(symlink(target_file_.c_str(), symlink_path.c_str()),
    81                SyscallFailsWithErrno(EIO));
    82    SkipServerActualRequest();
    83  }
    84  
    85  }  // namespace
    86  
    87  }  // namespace testing
    88  }  // namespace gvisor