]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/fs/fusefs/default_permissions.cc
MFHead @345353
[FreeBSD/FreeBSD.git] / tests / sys / fs / fusefs / default_permissions.cc
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
31 /*
32  * Tests for the "default_permissions" mount option.  They must be in their own
33  * file so they can be run as an unprivileged user
34  */
35
36 extern "C" {
37 #include <fcntl.h>
38 #include <unistd.h>
39 }
40
41 #include "mockfs.hh"
42 #include "utils.hh"
43
44 using namespace testing;
45
46 class DefaultPermissions: public FuseTest {
47
48 virtual void SetUp() {
49         FuseTest::SetUp();
50
51         if (geteuid() == 0) {
52                 GTEST_SKIP() << "This test requires an unprivileged user";
53         }
54         m_default_permissions = true;
55 }
56
57 public:
58 void expect_lookup(const char *relpath, uint64_t ino, mode_t mode)
59 {
60         FuseTest::expect_lookup(relpath, ino, S_IFREG | mode, 0, 1);
61 }
62
63 };
64
65 class Access: public DefaultPermissions {};
66 class Open: public DefaultPermissions {};
67
68 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=216391 */
69 TEST_F(Access, DISABLED_eaccess)
70 {
71         const char FULLPATH[] = "mountpoint/some_file.txt";
72         const char RELPATH[] = "some_file.txt";
73         uint64_t ino = 42;
74         mode_t  access_mode = X_OK;
75
76         expect_lookup(RELPATH, ino, S_IFREG | 0644);
77         /* 
78          * Once default_permissions is properly implemented, there might be
79          * another FUSE_GETATTR or something in here.  But there should not be
80          * a FUSE_ACCESS
81          */
82
83         ASSERT_NE(0, access(FULLPATH, access_mode));
84         ASSERT_EQ(EACCES, errno);
85 }
86
87 TEST_F(Access, ok)
88 {
89         const char FULLPATH[] = "mountpoint/some_file.txt";
90         const char RELPATH[] = "some_file.txt";
91         uint64_t ino = 42;
92         mode_t  access_mode = R_OK;
93
94         expect_lookup(RELPATH, ino, S_IFREG | 0644);
95         /* 
96          * Once default_permissions is properly implemented, there might be
97          * another FUSE_GETATTR or something in here.  But there should not be
98          * a FUSE_ACCESS
99          */
100
101         ASSERT_EQ(0, access(FULLPATH, access_mode)) << strerror(errno);
102 }
103
104 TEST_F(Open, ok)
105 {
106         const char FULLPATH[] = "mountpoint/some_file.txt";
107         const char RELPATH[] = "some_file.txt";
108         uint64_t ino = 42;
109         int fd;
110
111         expect_lookup(RELPATH, ino, S_IFREG | 0644);
112         expect_open(ino, 0, 1);
113         /* Until the attr cache is working, we may send an additional GETATTR */
114         expect_getattr(ino, 0);
115
116         fd = open(FULLPATH, O_RDONLY);
117         EXPECT_LE(0, fd) << strerror(errno);
118         /* Deliberately leak fd.  close(2) will be tested in release.cc */
119 }
120
121 /* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=216391 */
122 TEST_F(Open, DISABLED_eperm)
123 {
124         const char FULLPATH[] = "mountpoint/some_file.txt";
125         const char RELPATH[] = "some_file.txt";
126         uint64_t ino = 42;
127
128         expect_lookup(RELPATH, ino, S_IFREG | 0644);
129         /* 
130          * Once default_permissions is properly implemented, there might be
131          * another FUSE_GETATTR or something in here.  But there should not be
132          * a FUSE_ACCESS
133          */
134
135         EXPECT_NE(0, open(FULLPATH, O_RDWR));
136         EXPECT_EQ(EPERM, errno);
137 }