github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/fuse/linux/readlink_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/test_util.h" 30 31 namespace gvisor { 32 namespace testing { 33 34 namespace { 35 36 class ReadlinkTest : public FuseTest { 37 protected: 38 const std::string test_file_ = "test_file_"; 39 const mode_t perms_ = S_IRWXU | S_IRWXG | S_IRWXO; 40 }; 41 42 TEST_F(ReadlinkTest, ReadSymLink) { 43 const std::string symlink_path = 44 JoinPath(mount_point_.path().c_str(), test_file_); 45 SetServerInodeLookup(test_file_, S_IFLNK | perms_); 46 47 struct fuse_out_header out_header = { 48 .len = static_cast<uint32_t>(sizeof(struct fuse_out_header)) + 49 static_cast<uint32_t>(test_file_.length()) + 1, 50 }; 51 std::string link = test_file_; 52 auto iov_out = FuseGenerateIovecs(out_header, link); 53 SetServerResponse(FUSE_READLINK, iov_out); 54 const std::string actual_link = 55 ASSERT_NO_ERRNO_AND_VALUE(ReadLink(symlink_path)); 56 57 struct fuse_in_header in_header; 58 auto iov_in = FuseGenerateIovecs(in_header); 59 GetServerActualRequest(iov_in); 60 61 EXPECT_EQ(in_header.len, sizeof(in_header)); 62 EXPECT_EQ(in_header.opcode, FUSE_READLINK); 63 EXPECT_EQ(0, memcmp(actual_link.c_str(), link.data(), link.size())); 64 65 // next readlink should have link cached, so shouldn't have new request to 66 // server. 67 uint32_t recieved_before = GetServerTotalReceivedBytes(); 68 ASSERT_NO_ERRNO(ReadLink(symlink_path)); 69 EXPECT_EQ(GetServerTotalReceivedBytes(), recieved_before); 70 } 71 72 TEST_F(ReadlinkTest, NotSymlink) { 73 const std::string test_file_path = 74 JoinPath(mount_point_.path().c_str(), test_file_); 75 SetServerInodeLookup(test_file_, S_IFREG | perms_); 76 77 std::vector<char> buf(PATH_MAX + 1); 78 ASSERT_THAT(readlink(test_file_path.c_str(), buf.data(), PATH_MAX), 79 SyscallFailsWithErrno(EINVAL)); 80 } 81 82 } // namespace 83 84 } // namespace testing 85 } // namespace gvisor