github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/fuse/linux/mknod_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 MknodTest : public FuseTest { 38 protected: 39 const std::string test_file_ = "test_file"; 40 const mode_t perms_ = S_IRWXU | S_IRWXG | S_IRWXO; 41 }; 42 43 TEST_F(MknodTest, RegularFile) { 44 const std::string test_file_path = 45 JoinPath(mount_point_.path().c_str(), test_file_); 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_IFREG | perms_, 5); 52 auto iov_out = FuseGenerateIovecs(out_header, out_payload); 53 SetServerResponse(FUSE_MKNOD, iov_out); 54 TempUmask mask(new_umask); 55 ASSERT_THAT(mknod(test_file_path.c_str(), perms_, 0), SyscallSucceeds()); 56 57 struct fuse_in_header in_header; 58 struct fuse_mknod_in in_payload; 59 std::vector<char> actual_file(test_file_.length() + 1); 60 auto iov_in = FuseGenerateIovecs(in_header, in_payload, actual_file); 61 GetServerActualRequest(iov_in); 62 63 EXPECT_EQ(in_header.len, 64 sizeof(in_header) + sizeof(in_payload) + test_file_.length() + 1); 65 EXPECT_EQ(in_header.opcode, FUSE_MKNOD); 66 EXPECT_EQ(in_payload.mode & 0777, perms_ & ~new_umask); 67 EXPECT_EQ(in_payload.umask, new_umask); 68 EXPECT_EQ(in_payload.rdev, 0); 69 EXPECT_EQ(std::string(actual_file.data()), test_file_); 70 } 71 72 TEST_F(MknodTest, FileTypeError) { 73 const std::string test_file_path = 74 JoinPath(mount_point_.path().c_str(), test_file_); 75 76 struct fuse_out_header out_header = { 77 .len = sizeof(struct fuse_out_header) + sizeof(struct fuse_entry_out), 78 }; 79 // server return directory instead of regular file should cause an error. 80 struct fuse_entry_out out_payload = DefaultEntryOut(S_IFDIR | perms_, 5); 81 auto iov_out = FuseGenerateIovecs(out_header, out_payload); 82 SetServerResponse(FUSE_MKNOD, iov_out); 83 ASSERT_THAT(mknod(test_file_path.c_str(), perms_, 0), 84 SyscallFailsWithErrno(EIO)); 85 SkipServerActualRequest(); 86 } 87 88 TEST_F(MknodTest, NodeIDError) { 89 const std::string test_file_path = 90 JoinPath(mount_point_.path().c_str(), test_file_); 91 92 struct fuse_out_header out_header = { 93 .len = sizeof(struct fuse_out_header) + sizeof(struct fuse_entry_out), 94 }; 95 struct fuse_entry_out out_payload = 96 DefaultEntryOut(S_IFREG | perms_, FUSE_ROOT_ID); 97 auto iov_out = FuseGenerateIovecs(out_header, out_payload); 98 SetServerResponse(FUSE_MKNOD, iov_out); 99 ASSERT_THAT(mknod(test_file_path.c_str(), perms_, 0), 100 SyscallFailsWithErrno(EIO)); 101 SkipServerActualRequest(); 102 } 103 104 } // namespace 105 106 } // namespace testing 107 } // namespace gvisor