xref: /freebsd/tests/sys/fs/fusefs/bad_server.cc (revision 1ddff51060ad759e35dcc4716b0bdcdb40255862)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2023 Axcient
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 extern "C" {
29 #include <fcntl.h>
30 #include <unistd.h>
31 }
32 
33 #include "mockfs.hh"
34 #include "utils.hh"
35 
36 using namespace testing;
37 
38 class BadServer: public FuseTest {};
39 
40 /*
41  * If the server sends a response for an unknown request, the kernel should
42  * gracefully return EINVAL.
43  */
44 TEST_F(BadServer, UnknownUnique)
45 {
46 	mockfs_buf_out out;
47 
48 	out.header.len = sizeof(out.header);
49 	out.header.error = 0;
50 	out.header.unique = 99999;		// Invalid!
51 	out.expected_errno = EINVAL;
52 	m_mock->write_response(out);
53 }
54 
55 /*
56  * If the server sends less than a header's worth of data, the kernel should
57  * gracefully return EINVAL.
58  */
59 TEST_F(BadServer, ShortWrite)
60 {
61 	mockfs_buf_out out;
62 
63 	out.header.len = sizeof(out.header) - 1;
64 	out.header.error = 0;
65 	out.header.unique = 0;			// Asynchronous notification
66 	out.expected_errno = EINVAL;
67 	m_mock->write_response(out);
68 	/*
69 	 * Tell the event loop to quit.  The kernel has already disconnected us
70 	 * because of the short write.
71 	 */
72 	m_mock->m_quit = true;
73 }
74 
75 /*
76  * It is illegal to report an error, and also send back a payload.
77  */
78 TEST_F(BadServer, ErrorWithPayload)
79 {
80 	const char FULLPATH[] = "mountpoint/some_file.txt";
81 	const char RELPATH[] = "some_file.txt";
82 
83 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
84 	.WillOnce(Invoke([&](auto in, auto &out) {
85 		// First send an invalid response
86 		std::unique_ptr<mockfs_buf_out> out0(new mockfs_buf_out);
87 		out0->header.unique = in.header.unique;
88 		out0->header.error = -ENOENT;
89 		SET_OUT_HEADER_LEN(*out0, entry);	// Invalid!
90 		out0->expected_errno = EINVAL;
91 		out.push_back(std::move(out0));
92 
93 		// Then, respond to the lookup so we can complete the test
94 		std::unique_ptr<mockfs_buf_out> out1(new mockfs_buf_out);
95 		out1->header.unique = in.header.unique;
96 		out1->header.error = -ENOENT;
97 		out1->header.len = sizeof(out1->header);
98 		out.push_back(std::move(out1));
99 
100 		// The kernel may disconnect us for bad behavior, so don't try
101 		// to read any more.
102 		m_mock->m_quit = true;
103 	}));
104 
105 	EXPECT_NE(0, access(FULLPATH, F_OK));
106 
107 	EXPECT_EQ(ENOENT, errno);
108 }
109