github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/fuse/linux/open_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 25 #include "gtest/gtest.h" 26 #include "test/fuse/linux/fuse_base.h" 27 #include "test/util/fuse_util.h" 28 #include "test/util/test_util.h" 29 30 namespace gvisor { 31 namespace testing { 32 33 namespace { 34 35 class OpenTest : public FuseTest { 36 // OpenTest doesn't care the release request when close a fd, 37 // so doesn't check leftover requests when tearing down. 38 void TearDown() { UnmountFuse(); } 39 40 protected: 41 const std::string test_file_ = "test_file"; 42 const mode_t regular_file_ = S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO; 43 44 struct fuse_open_out out_payload_ = { 45 .fh = 1, 46 .open_flags = O_RDWR, 47 }; 48 }; 49 50 TEST_F(OpenTest, RegularFile) { 51 const std::string test_file_path = 52 JoinPath(mount_point_.path().c_str(), test_file_); 53 SetServerInodeLookup(test_file_, regular_file_); 54 55 struct fuse_out_header out_header = { 56 .len = sizeof(struct fuse_out_header) + sizeof(struct fuse_open_out), 57 }; 58 auto iov_out = FuseGenerateIovecs(out_header, out_payload_); 59 SetServerResponse(FUSE_OPEN, iov_out); 60 FileDescriptor fd = 61 ASSERT_NO_ERRNO_AND_VALUE(Open(test_file_path.c_str(), O_RDWR)); 62 63 struct fuse_in_header in_header; 64 struct fuse_open_in in_payload; 65 auto iov_in = FuseGenerateIovecs(in_header, in_payload); 66 GetServerActualRequest(iov_in); 67 68 EXPECT_EQ(in_header.len, sizeof(in_header) + sizeof(in_payload)); 69 EXPECT_EQ(in_header.opcode, FUSE_OPEN); 70 EXPECT_EQ(in_payload.flags, O_RDWR); 71 EXPECT_THAT(fcntl(fd.get(), F_GETFL), SyscallSucceedsWithValue(O_RDWR)); 72 } 73 74 TEST_F(OpenTest, SetNoOpen) { 75 const std::string test_file_path = 76 JoinPath(mount_point_.path().c_str(), test_file_); 77 SetServerInodeLookup(test_file_, regular_file_); 78 79 // ENOSYS indicates open is not implemented. 80 struct fuse_out_header out_header = { 81 .len = sizeof(struct fuse_out_header) + sizeof(struct fuse_open_out), 82 .error = -ENOSYS, 83 }; 84 auto iov_out = FuseGenerateIovecs(out_header, out_payload_); 85 SetServerResponse(FUSE_OPEN, iov_out); 86 ASSERT_NO_ERRNO_AND_VALUE(Open(test_file_path.c_str(), O_RDWR)); 87 SkipServerActualRequest(); 88 89 // check open doesn't send new request. 90 uint32_t recieved_before = GetServerTotalReceivedBytes(); 91 ASSERT_NO_ERRNO_AND_VALUE(Open(test_file_path.c_str(), O_RDWR)); 92 EXPECT_EQ(GetServerTotalReceivedBytes(), recieved_before); 93 } 94 95 TEST_F(OpenTest, OpenFail) { 96 struct fuse_out_header out_header = { 97 .len = sizeof(struct fuse_out_header) + sizeof(struct fuse_open_out), 98 .error = -ENOENT, 99 }; 100 101 auto iov_out = FuseGenerateIovecs(out_header, out_payload_); 102 SetServerResponse(FUSE_OPENDIR, iov_out); 103 ASSERT_THAT(open(mount_point_.path().c_str(), O_RDWR), 104 SyscallFailsWithErrno(ENOENT)); 105 106 struct fuse_in_header in_header; 107 struct fuse_open_in in_payload; 108 auto iov_in = FuseGenerateIovecs(in_header, in_payload); 109 GetServerActualRequest(iov_in); 110 111 EXPECT_EQ(in_header.len, sizeof(in_header) + sizeof(in_payload)); 112 EXPECT_EQ(in_header.opcode, FUSE_OPENDIR); 113 EXPECT_EQ(in_payload.flags, O_RDWR); 114 } 115 116 TEST_F(OpenTest, DirectoryFlagOnRegularFile) { 117 const std::string test_file_path = 118 JoinPath(mount_point_.path().c_str(), test_file_); 119 120 SetServerInodeLookup(test_file_, regular_file_); 121 ASSERT_THAT(open(test_file_path.c_str(), O_RDWR | O_DIRECTORY), 122 SyscallFailsWithErrno(ENOTDIR)); 123 } 124 125 } // namespace 126 127 } // namespace testing 128 } // namespace gvisor