2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 The FreeBSD Foundation
6 * This software was developed by BFF Storage Systems, LLC under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 using namespace testing;
42 class Symlink: public FuseTest {
45 void expect_symlink(uint64_t ino, const char *target, const char *relpath)
47 EXPECT_CALL(*m_mock, process(
48 ResultOf([=](auto in) {
49 const char *name = (const char*)in.body.bytes;
50 const char *linkname = name + strlen(name) + 1;
51 return (in.header.opcode == FUSE_SYMLINK &&
52 (0 == strcmp(linkname, target)) &&
53 (0 == strcmp(name, relpath)));
56 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
57 SET_OUT_HEADER_LEN(out, entry);
58 out.body.entry.attr.mode = S_IFLNK | 0777;
59 out.body.entry.nodeid = ino;
60 out.body.entry.entry_valid = UINT64_MAX;
61 out.body.entry.attr_valid = UINT64_MAX;
67 class Symlink_7_8: public FuseTest {
69 virtual void SetUp() {
70 m_kernel_minor_version = 8;
74 void expect_symlink(uint64_t ino, const char *target, const char *relpath)
76 EXPECT_CALL(*m_mock, process(
77 ResultOf([=](auto in) {
78 const char *name = (const char*)in.body.bytes;
79 const char *linkname = name + strlen(name) + 1;
80 return (in.header.opcode == FUSE_SYMLINK &&
81 (0 == strcmp(linkname, target)) &&
82 (0 == strcmp(name, relpath)));
85 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
86 SET_OUT_HEADER_LEN(out, entry_7_8);
87 out.body.entry.attr.mode = S_IFLNK | 0777;
88 out.body.entry.nodeid = ino;
89 out.body.entry.entry_valid = UINT64_MAX;
90 out.body.entry.attr_valid = UINT64_MAX;
97 * A successful symlink should clear the parent directory's attribute cache,
98 * because the fuse daemon should update its mtime and ctime
100 TEST_F(Symlink, clear_attr_cache)
102 const char FULLPATH[] = "mountpoint/src";
103 const char RELPATH[] = "src";
104 const char dst[] = "dst";
105 const uint64_t ino = 42;
108 EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
109 .WillOnce(Invoke(ReturnErrno(ENOENT)));
110 EXPECT_CALL(*m_mock, process(
111 ResultOf([=](auto in) {
112 return (in.header.opcode == FUSE_GETATTR &&
113 in.header.nodeid == FUSE_ROOT_ID);
117 .WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
118 SET_OUT_HEADER_LEN(out, attr);
119 out.body.attr.attr.ino = FUSE_ROOT_ID;
120 out.body.attr.attr.mode = S_IFDIR | 0755;
121 out.body.attr.attr_valid = UINT64_MAX;
123 expect_symlink(ino, dst, RELPATH);
125 EXPECT_EQ(0, stat("mountpoint", &sb)) << strerror(errno);
126 EXPECT_EQ(0, symlink(dst, FULLPATH)) << strerror(errno);
127 EXPECT_EQ(0, stat("mountpoint", &sb)) << strerror(errno);
130 TEST_F(Symlink, enospc)
132 const char FULLPATH[] = "mountpoint/lnk";
133 const char RELPATH[] = "lnk";
134 const char dst[] = "dst";
136 EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
137 .WillOnce(Invoke(ReturnErrno(ENOENT)));
139 EXPECT_CALL(*m_mock, process(
140 ResultOf([=](auto in) {
141 const char *name = (const char*)in.body.bytes;
142 const char *linkname = name + strlen(name) + 1;
143 return (in.header.opcode == FUSE_SYMLINK &&
144 (0 == strcmp(linkname, dst)) &&
145 (0 == strcmp(name, RELPATH)));
148 ).WillOnce(Invoke(ReturnErrno(ENOSPC)));
150 EXPECT_EQ(-1, symlink(dst, FULLPATH));
151 EXPECT_EQ(ENOSPC, errno);
156 const char FULLPATH[] = "mountpoint/src";
157 const char RELPATH[] = "src";
158 const char dst[] = "dst";
159 const uint64_t ino = 42;
161 EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
162 .WillOnce(Invoke(ReturnErrno(ENOENT)));
163 expect_symlink(ino, dst, RELPATH);
165 EXPECT_EQ(0, symlink(dst, FULLPATH)) << strerror(errno);
168 TEST_F(Symlink_7_8, ok)
170 const char FULLPATH[] = "mountpoint/src";
171 const char RELPATH[] = "src";
172 const char dst[] = "dst";
173 const uint64_t ino = 42;
175 EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
176 .WillOnce(Invoke(ReturnErrno(ENOENT)));
177 expect_symlink(ino, dst, RELPATH);
179 EXPECT_EQ(0, symlink(dst, FULLPATH)) << strerror(errno);