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

     1  // Copyright 2021 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 <sys/mount.h>
    16  
    17  #include <iomanip>
    18  #include <sstream>
    19  
    20  #include "gmock/gmock.h"
    21  #include "gtest/gtest.h"
    22  #include "test/util/capability_util.h"
    23  #include "test/util/temp_path.h"
    24  #include "test/util/test_util.h"
    25  #include "test/util/verity_util.h"
    26  
    27  namespace gvisor {
    28  namespace testing {
    29  
    30  namespace {
    31  
    32  // Mount verity file system on an existing tmpfs mount.
    33  TEST(MountTest, MountExisting) {
    34    // Verity is implemented in VFS2.
    35    SKIP_IF(IsRunningWithVFS1());
    36  
    37    SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_SYS_ADMIN)));
    38  
    39    // Mount a new tmpfs file system.
    40    auto const tmpfs_dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
    41    ASSERT_THAT(mount("", tmpfs_dir.path().c_str(), "tmpfs", 0, ""),
    42                SyscallSucceeds());
    43  
    44    // Mount a verity file system on the existing gofer mount.
    45    auto const verity_dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
    46    std::string opts = "lower_path=" + tmpfs_dir.path();
    47    ASSERT_THAT(mount("", verity_dir.path().c_str(), "verity", 0, opts.c_str()),
    48                SyscallSucceeds());
    49    auto const fd =
    50        ASSERT_NO_ERRNO_AND_VALUE(Open(verity_dir.path(), O_RDONLY, 0777));
    51    EXPECT_THAT(ioctl(fd.get(), FS_IOC_ENABLE_VERITY), SyscallSucceeds());
    52  }
    53  
    54  }  // namespace
    55  
    56  }  // namespace testing
    57  }  // namespace gvisor