1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2019 The FreeBSD Foundation
5 *
6 * This software was developed by BFF Storage Systems, LLC under sponsorship
7 * from the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
17 *
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
28 * SUCH DAMAGE.
29 *
30 * $FreeBSD$
31 */
32
33 extern "C" {
34 #include <fcntl.h>
35 #include <unistd.h>
36 }
37
38 #include "mockfs.hh"
39 #include "utils.hh"
40
41 using namespace testing;
42
43 class Access: public FuseTest {
44 public:
expect_lookup(const char * relpath,uint64_t ino)45 void expect_lookup(const char *relpath, uint64_t ino)
46 {
47 FuseTest::expect_lookup(relpath, ino, S_IFREG | 0644, 0, 1);
48 }
49 };
50
51 class RofsAccess: public Access {
52 public:
SetUp()53 virtual void SetUp() {
54 m_ro = true;
55 Access::SetUp();
56 }
57 };
58
59 /* The error case of FUSE_ACCESS. */
TEST_F(Access,eaccess)60 TEST_F(Access, eaccess)
61 {
62 const char FULLPATH[] = "mountpoint/some_file.txt";
63 const char RELPATH[] = "some_file.txt";
64 uint64_t ino = 42;
65 mode_t access_mode = X_OK;
66
67 expect_access(FUSE_ROOT_ID, X_OK, 0);
68 expect_lookup(RELPATH, ino);
69 expect_access(ino, access_mode, EACCES);
70
71 ASSERT_NE(0, access(FULLPATH, access_mode));
72 ASSERT_EQ(EACCES, errno);
73 }
74
75 /*
76 * If the filesystem returns ENOSYS, then it is treated as a permanent success,
77 * and subsequent VOP_ACCESS calls will succeed automatically without querying
78 * the daemon.
79 */
TEST_F(Access,enosys)80 TEST_F(Access, enosys)
81 {
82 const char FULLPATH[] = "mountpoint/some_file.txt";
83 const char RELPATH[] = "some_file.txt";
84 uint64_t ino = 42;
85 mode_t access_mode = R_OK;
86
87 expect_access(FUSE_ROOT_ID, X_OK, ENOSYS);
88 FuseTest::expect_lookup(RELPATH, ino, S_IFREG | 0644, 0, 2);
89
90 ASSERT_EQ(0, access(FULLPATH, access_mode)) << strerror(errno);
91 ASSERT_EQ(0, access(FULLPATH, access_mode)) << strerror(errno);
92 }
93
TEST_F(RofsAccess,erofs)94 TEST_F(RofsAccess, erofs)
95 {
96 const char FULLPATH[] = "mountpoint/some_file.txt";
97 const char RELPATH[] = "some_file.txt";
98 uint64_t ino = 42;
99 mode_t access_mode = W_OK;
100
101 expect_access(FUSE_ROOT_ID, X_OK, 0);
102 expect_lookup(RELPATH, ino);
103
104 ASSERT_NE(0, access(FULLPATH, access_mode));
105 ASSERT_EQ(EROFS, errno);
106 }
107
108 /* The successful case of FUSE_ACCESS. */
TEST_F(Access,ok)109 TEST_F(Access, ok)
110 {
111 const char FULLPATH[] = "mountpoint/some_file.txt";
112 const char RELPATH[] = "some_file.txt";
113 uint64_t ino = 42;
114 mode_t access_mode = R_OK;
115
116 expect_access(FUSE_ROOT_ID, X_OK, 0);
117 expect_lookup(RELPATH, ino);
118 expect_access(ino, access_mode, 0);
119
120 ASSERT_EQ(0, access(FULLPATH, access_mode)) << strerror(errno);
121 }
122