github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/fuse/linux/mkdir_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/temp_umask.h"
    30  #include "test/util/test_util.h"
    31  
    32  namespace gvisor {
    33  namespace testing {
    34  
    35  namespace {
    36  
    37  class MkdirTest : public FuseTest {
    38   protected:
    39    const std::string test_dir_ = "test_dir";
    40    const mode_t perms_ = S_IRWXU | S_IRWXG | S_IRWXO;
    41  };
    42  
    43  TEST_F(MkdirTest, CreateDir) {
    44    const std::string test_dir_path_ =
    45        JoinPath(mount_point_.path().c_str(), test_dir_);
    46    const mode_t new_umask = 0077;
    47  
    48    struct fuse_out_header out_header = {
    49        .len = sizeof(struct fuse_out_header) + sizeof(struct fuse_entry_out),
    50    };
    51    struct fuse_entry_out out_payload = DefaultEntryOut(S_IFDIR | perms_, 5);
    52    auto iov_out = FuseGenerateIovecs(out_header, out_payload);
    53    SetServerResponse(FUSE_MKDIR, iov_out);
    54    TempUmask mask(new_umask);
    55    ASSERT_THAT(mkdir(test_dir_path_.c_str(), 0777), SyscallSucceeds());
    56  
    57    struct fuse_in_header in_header;
    58    struct fuse_mkdir_in in_payload;
    59    std::vector<char> actual_dir(test_dir_.length() + 1);
    60    auto iov_in = FuseGenerateIovecs(in_header, in_payload, actual_dir);
    61    GetServerActualRequest(iov_in);
    62  
    63    EXPECT_EQ(in_header.len,
    64              sizeof(in_header) + sizeof(in_payload) + test_dir_.length() + 1);
    65    EXPECT_EQ(in_header.opcode, FUSE_MKDIR);
    66    EXPECT_EQ(in_payload.mode & 0777, perms_ & ~new_umask);
    67    EXPECT_EQ(in_payload.umask, new_umask);
    68    EXPECT_EQ(std::string(actual_dir.data()), test_dir_);
    69  }
    70  
    71  TEST_F(MkdirTest, FileTypeError) {
    72    const std::string test_dir_path_ =
    73        JoinPath(mount_point_.path().c_str(), test_dir_);
    74  
    75    struct fuse_out_header out_header = {
    76        .len = sizeof(struct fuse_out_header) + sizeof(struct fuse_entry_out),
    77    };
    78    struct fuse_entry_out out_payload = DefaultEntryOut(S_IFREG | perms_, 5);
    79    auto iov_out = FuseGenerateIovecs(out_header, out_payload);
    80    SetServerResponse(FUSE_MKDIR, iov_out);
    81    ASSERT_THAT(mkdir(test_dir_path_.c_str(), 0777), SyscallFailsWithErrno(EIO));
    82    SkipServerActualRequest();
    83  }
    84  
    85  }  // namespace
    86  
    87  }  // namespace testing
    88  }  // namespace gvisor