github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/fuse/linux/rmdir_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 <sys/uio.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/fs_util.h"
    30  #include "test/util/fuse_util.h"
    31  #include "test/util/test_util.h"
    32  
    33  namespace gvisor {
    34  namespace testing {
    35  
    36  namespace {
    37  
    38  class RmDirTest : public FuseTest {
    39   protected:
    40    const std::string test_dir_name_ = "test_dir";
    41    const std::string test_subdir_ = "test_subdir";
    42    const mode_t test_dir_mode_ = S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO;
    43  };
    44  
    45  TEST_F(RmDirTest, NormalRmDir) {
    46    const std::string test_dir_path_ =
    47        JoinPath(mount_point_.path().c_str(), test_dir_name_);
    48  
    49    SetServerInodeLookup(test_dir_name_, test_dir_mode_);
    50  
    51    // RmDir code.
    52    struct fuse_out_header rmdir_header = {
    53        .len = sizeof(struct fuse_out_header),
    54    };
    55  
    56    auto iov_out = FuseGenerateIovecs(rmdir_header);
    57    SetServerResponse(FUSE_RMDIR, iov_out);
    58  
    59    ASSERT_THAT(rmdir(test_dir_path_.c_str()), SyscallSucceeds());
    60  
    61    struct fuse_in_header in_header;
    62    std::vector<char> actual_dirname(test_dir_name_.length() + 1);
    63    auto iov_in = FuseGenerateIovecs(in_header, actual_dirname);
    64    GetServerActualRequest(iov_in);
    65  
    66    EXPECT_EQ(in_header.len, sizeof(in_header) + test_dir_name_.length() + 1);
    67    EXPECT_EQ(in_header.opcode, FUSE_RMDIR);
    68    EXPECT_EQ(std::string(actual_dirname.data()), test_dir_name_);
    69  }
    70  
    71  TEST_F(RmDirTest, NormalRmDirSubdir) {
    72    SetServerInodeLookup(test_subdir_, S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO);
    73    const std::string test_dir_path_ =
    74        JoinPath(mount_point_.path().c_str(), test_subdir_, test_dir_name_);
    75    SetServerInodeLookup(test_dir_name_, test_dir_mode_);
    76  
    77    // RmDir code.
    78    struct fuse_out_header rmdir_header = {
    79        .len = sizeof(struct fuse_out_header),
    80    };
    81  
    82    auto iov_out = FuseGenerateIovecs(rmdir_header);
    83    SetServerResponse(FUSE_RMDIR, iov_out);
    84  
    85    ASSERT_THAT(rmdir(test_dir_path_.c_str()), SyscallSucceeds());
    86  
    87    struct fuse_in_header in_header;
    88    std::vector<char> actual_dirname(test_dir_name_.length() + 1);
    89    auto iov_in = FuseGenerateIovecs(in_header, actual_dirname);
    90    GetServerActualRequest(iov_in);
    91  
    92    EXPECT_EQ(in_header.len, sizeof(in_header) + test_dir_name_.length() + 1);
    93    EXPECT_EQ(in_header.opcode, FUSE_RMDIR);
    94    EXPECT_EQ(std::string(actual_dirname.data()), test_dir_name_);
    95  }
    96  
    97  }  // namespace
    98  
    99  }  // namespace testing
   100  }  // namespace gvisor