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
38 using namespace testing;
40 class Symlink: public FuseTest {
43 void expect_symlink(uint64_t ino, const char *target, const char *relpath)
45 EXPECT_CALL(*m_mock, process(
46 ResultOf([=](auto in) {
47 const char *name = (const char*)in.body.bytes;
48 const char *linkname = name + strlen(name) + 1;
49 return (in.header.opcode == FUSE_SYMLINK &&
50 (0 == strcmp(linkname, target)) &&
51 (0 == strcmp(name, relpath)));
54 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
55 SET_OUT_HEADER_LEN(out, entry);
56 out.body.entry.attr.mode = S_IFLNK | 0777;
57 out.body.entry.nodeid = ino;
58 out.body.entry.entry_valid = UINT64_MAX;
59 out.body.entry.attr_valid = UINT64_MAX;
65 class Symlink_7_8: public FuseTest {
67 virtual void SetUp() {
68 m_kernel_minor_version = 8;
72 void expect_symlink(uint64_t ino, const char *target, const char *relpath)
74 EXPECT_CALL(*m_mock, process(
75 ResultOf([=](auto in) {
76 const char *name = (const char*)in.body.bytes;
77 const char *linkname = name + strlen(name) + 1;
78 return (in.header.opcode == FUSE_SYMLINK &&
79 (0 == strcmp(linkname, target)) &&
80 (0 == strcmp(name, relpath)));
83 ).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
84 SET_OUT_HEADER_LEN(out, entry_7_8);
85 out.body.entry.attr.mode = S_IFLNK | 0777;
86 out.body.entry.nodeid = ino;
87 out.body.entry.entry_valid = UINT64_MAX;
88 out.body.entry.attr_valid = UINT64_MAX;
95 * A successful symlink should clear the parent directory's attribute cache,
96 * because the fuse daemon should update its mtime and ctime
98 TEST_F(Symlink, clear_attr_cache)
100 const char FULLPATH[] = "mountpoint/src";
101 const char RELPATH[] = "src";
102 const char dst[] = "dst";
103 const uint64_t ino = 42;
106 EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
107 EXPECT_CALL(*m_mock, process(
108 ResultOf([=](auto in) {
109 return (in.header.opcode == FUSE_GETATTR &&
110 in.header.nodeid == 1);
114 .WillRepeatedly(Invoke(ReturnImmediate([=](auto i __unused, auto& out) {
115 SET_OUT_HEADER_LEN(out, attr);
116 out.body.attr.attr.ino = 1;
117 out.body.attr.attr.mode = S_IFDIR | 0755;
118 out.body.attr.attr_valid = UINT64_MAX;
120 expect_symlink(ino, dst, RELPATH);
122 EXPECT_EQ(0, stat("mountpoint", &sb)) << strerror(errno);
123 EXPECT_EQ(0, symlink(dst, FULLPATH)) << strerror(errno);
124 EXPECT_EQ(0, stat("mountpoint", &sb)) << strerror(errno);
127 TEST_F(Symlink, enospc)
129 const char FULLPATH[] = "mountpoint/lnk";
130 const char RELPATH[] = "lnk";
131 const char dst[] = "dst";
133 EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
135 EXPECT_CALL(*m_mock, process(
136 ResultOf([=](auto in) {
137 const char *name = (const char*)in.body.bytes;
138 const char *linkname = name + strlen(name) + 1;
139 return (in.header.opcode == FUSE_SYMLINK &&
140 (0 == strcmp(linkname, dst)) &&
141 (0 == strcmp(name, RELPATH)));
144 ).WillOnce(Invoke(ReturnErrno(ENOSPC)));
146 EXPECT_EQ(-1, symlink(dst, FULLPATH));
147 EXPECT_EQ(ENOSPC, errno);
152 const char FULLPATH[] = "mountpoint/src";
153 const char RELPATH[] = "src";
154 const char dst[] = "dst";
155 const uint64_t ino = 42;
157 EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
158 expect_symlink(ino, dst, RELPATH);
160 EXPECT_EQ(0, symlink(dst, FULLPATH)) << strerror(errno);
163 TEST_F(Symlink_7_8, ok)
165 const char FULLPATH[] = "mountpoint/src";
166 const char RELPATH[] = "src";
167 const char dst[] = "dst";
168 const uint64_t ino = 42;
170 EXPECT_LOOKUP(1, RELPATH).WillOnce(Invoke(ReturnErrno(ENOENT)));
171 expect_symlink(ino, dst, RELPATH);
173 EXPECT_EQ(0, symlink(dst, FULLPATH)) << strerror(errno);