aboutsummaryrefslogtreecommitdiff
path: root/tests/sys/fs/fusefs/statfs.cc
blob: c6268c36cc471b792ba366f15af3a77a9b70cba3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2019 The FreeBSD Foundation
 *
 * This software was developed by BFF Storage Systems, LLC under sponsorship
 * from the FreeBSD Foundation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

extern "C" {
#include <sys/param.h>
#include <sys/mount.h>
#include <semaphore.h>
}

#include "mockfs.hh"
#include "utils.hh"

using namespace testing;

class Statfs: public FuseTest {};

TEST_F(Statfs, eio)
{
	struct statfs statbuf;

	EXPECT_CALL(*m_mock, process(
		ResultOf([](auto in) {
			return (in.header.opcode == FUSE_STATFS);
		}, Eq(true)),
		_)
	).WillOnce(Invoke(ReturnErrno(EIO)));

	ASSERT_NE(0, statfs("mountpoint", &statbuf));
	ASSERT_EQ(EIO, errno);
}

/*
 * When the daemon is dead but the filesystem is still mounted, fuse(4) fakes
 * the statfs(2) response, which is necessary for unmounting.
 */
TEST_F(Statfs, enotconn)
{
	struct statfs statbuf;
	char mp[PATH_MAX];

	m_mock->kill_daemon();

	ASSERT_NE(nullptr, getcwd(mp, PATH_MAX)) << strerror(errno);
	strlcat(mp, "/mountpoint", PATH_MAX);
	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);

	EXPECT_EQ(getuid(), statbuf.f_owner);
	EXPECT_EQ(0, strcmp("fusefs", statbuf.f_fstypename));
	EXPECT_EQ(0, strcmp("/dev/fuse", statbuf.f_mntfromname));
	EXPECT_EQ(0, strcmp(mp, statbuf.f_mntonname));
}

static void* statfs_th(void* arg) {
	ssize_t r;
	struct statfs *sb = (struct statfs*)arg;

	r = statfs("mountpoint", sb);
	if (r >= 0)
		return 0;
	else
		return (void*)(intptr_t)errno;
}

/*
 * Like the enotconn test, but in this case the daemon dies after we send the
 * FUSE_STATFS operation but before we get a response.
 */
TEST_F(Statfs, enotconn_while_blocked)
{
	struct statfs statbuf;
	void *thr0_value;
	pthread_t th0;
	char mp[PATH_MAX];
	sem_t sem;

	ASSERT_EQ(0, sem_init(&sem, 0, 0)) << strerror(errno);

	EXPECT_CALL(*m_mock, process(
		ResultOf([](auto in) {
			return (in.header.opcode == FUSE_STATFS);
		}, Eq(true)),
		_)
	).WillOnce(Invoke([&](auto in __unused, auto &out __unused) {
		sem_post(&sem);
		/* Just block until the daemon dies */
	}));

	ASSERT_NE(nullptr, getcwd(mp, PATH_MAX)) << strerror(errno);
	strlcat(mp, "/mountpoint", PATH_MAX);
	ASSERT_EQ(0, pthread_create(&th0, NULL, statfs_th, (void*)&statbuf))
		<< strerror(errno);

	ASSERT_EQ(0, sem_wait(&sem)) << strerror(errno);
	m_mock->kill_daemon();

	pthread_join(th0, &thr0_value);
	ASSERT_EQ(0, (intptr_t)thr0_value);

	EXPECT_EQ(getuid(), statbuf.f_owner);
	EXPECT_EQ(0, strcmp("fusefs", statbuf.f_fstypename));
	EXPECT_EQ(0, strcmp("/dev/fuse", statbuf.f_mntfromname));
	EXPECT_EQ(0, strcmp(mp, statbuf.f_mntonname));
}

TEST_F(Statfs, ok)
{
	struct statfs statbuf;
	char mp[PATH_MAX];

	EXPECT_CALL(*m_mock, process(
		ResultOf([](auto in) {
			return (in.header.opcode == FUSE_STATFS);
		}, Eq(true)),
		_)
	).WillOnce(Invoke(ReturnImmediate([=](auto in __unused, auto& out) {
		SET_OUT_HEADER_LEN(out, statfs);
		out.body.statfs.st.blocks = 1000;
		out.body.statfs.st.bfree = 100;
		out.body.statfs.st.bavail = 200;
		out.body.statfs.st.files = 5;
		out.body.statfs.st.ffree = 6;
		out.body.statfs.st.namelen = 128;
		out.body.statfs.st.frsize = 1024;
	})));

	ASSERT_NE(nullptr, getcwd(mp, PATH_MAX)) << strerror(errno);
	strlcat(mp, "/mountpoint", PATH_MAX);
	ASSERT_EQ(0, statfs("mountpoint", &statbuf)) << strerror(errno);
	EXPECT_EQ(1024ul, statbuf.f_bsize);
	/* 
	 * fuse(4) ignores the filesystem's reported optimal transfer size, and
	 * chooses a size that works well with the rest of the system instead
	 */
	EXPECT_EQ(1000ul, statbuf.f_blocks);
	EXPECT_EQ(100ul, statbuf.f_bfree);
	EXPECT_EQ(200l, statbuf.f_bavail);
	EXPECT_EQ(5ul, statbuf.f_files);
	EXPECT_EQ(6l, statbuf.f_ffree);
	EXPECT_EQ(128u, statbuf.f_namemax);
	EXPECT_EQ(getuid(), statbuf.f_owner);
	EXPECT_EQ(0, strcmp("fusefs", statbuf.f_fstypename));
	EXPECT_EQ(0, strcmp("/dev/fuse", statbuf.f_mntfromname));
	EXPECT_EQ(0, strcmp(mp, statbuf.f_mntonname));
}