gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/util/fuse_util.h (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  #ifndef GVISOR_TEST_UTIL_FUSE_UTIL_H_
    16  #define GVISOR_TEST_UTIL_FUSE_UTIL_H_
    17  
    18  #include <linux/fuse.h>
    19  #include <sys/uio.h>
    20  
    21  #include <string>
    22  #include <vector>
    23  
    24  namespace gvisor {
    25  namespace testing {
    26  
    27  // The fundamental generation function with a single argument. If passed by
    28  // std::string or std::vector<char>, it will call specialized versions as
    29  // implemented below.
    30  template <typename T>
    31  std::vector<struct iovec> FuseGenerateIovecs(T &first) {
    32    return {(struct iovec){.iov_base = &first, .iov_len = sizeof(first)}};
    33  }
    34  
    35  // If an argument is of type std::string, it must be used in read-only scenario.
    36  // Because we are setting up iovec, which contains the original address of a
    37  // data structure, we have to drop const qualification. Usually used with
    38  // variable-length payload data.
    39  template <typename T = std::string>
    40  std::vector<struct iovec> FuseGenerateIovecs(std::string &first) {
    41    // Pad one byte for null-terminate c-string.
    42    return {(struct iovec){.iov_base = const_cast<char *>(first.c_str()),
    43                           .iov_len = first.size() + 1}};
    44  }
    45  
    46  // If an argument is of type std::vector<char>, it must be used in write-only
    47  // scenario and the size of the variable must be greater than or equal to the
    48  // size of the expected data. Usually used with variable-length payload data.
    49  template <typename T = std::vector<char>>
    50  std::vector<struct iovec> FuseGenerateIovecs(std::vector<char> &first) {
    51    return {(struct iovec){.iov_base = first.data(), .iov_len = first.size()}};
    52  }
    53  
    54  // A helper function to set up an array of iovec struct for testing purpose.
    55  // Use variadic class template to generalize different numbers and different
    56  // types of FUSE structs.
    57  template <typename T, typename... Types>
    58  std::vector<struct iovec> FuseGenerateIovecs(T &first, Types &...args) {
    59    auto first_iovec = FuseGenerateIovecs(first);
    60    auto iovecs = FuseGenerateIovecs(args...);
    61    first_iovec.insert(std::end(first_iovec), std::begin(iovecs),
    62                       std::end(iovecs));
    63    return first_iovec;
    64  }
    65  
    66  // Create a fuse_attr filled with the specified mode and inode.
    67  fuse_attr DefaultFuseAttr(mode_t mode, uint64_t inode, uint64_t size = 512);
    68  
    69  // Return a fuse_entry_out FUSE server response body.
    70  fuse_entry_out DefaultEntryOut(mode_t mode, uint64_t node_id,
    71                                 uint64_t size = 512);
    72  
    73  }  // namespace testing
    74  }  // namespace gvisor
    75  #endif  // GVISOR_TEST_UTIL_FUSE_UTIL_H_