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 * $FreeBSD$ 28 */ 29 30 extern "C" { 31 #include <fcntl.h> 32 #include <unistd.h> 33 } 34 35 #include "mockfs.hh" 36 #include "utils.hh" 37 38 using namespace testing; 39 40 class BadServer: public FuseTest {}; 41 42 /* 43 * If the server sends a response for an unknown request, the kernel should 44 * gracefully return EINVAL. 45 */ 46 TEST_F(BadServer, UnknownUnique) 47 { 48 mockfs_buf_out out; 49 50 out.header.len = sizeof(out.header); 51 out.header.error = 0; 52 out.header.unique = 99999; // Invalid! 53 out.expected_errno = EINVAL; 54 m_mock->write_response(out); 55 } 56 57 /* 58 * If the server sends less than a header's worth of data, the kernel should 59 * gracefully return EINVAL. 60 */ 61 TEST_F(BadServer, ShortWrite) 62 { 63 mockfs_buf_out out; 64 65 out.header.len = sizeof(out.header) - 1; 66 out.header.error = 0; 67 out.header.unique = 0; // Asynchronous notification 68 out.expected_errno = EINVAL; 69 m_mock->write_response(out); 70 } 71 72 /* 73 * It is illegal to report an error, and also send back a payload. 74 */ 75 TEST_F(BadServer, ErrorWithPayload) 76 { 77 const char FULLPATH[] = "mountpoint/some_file.txt"; 78 const char RELPATH[] = "some_file.txt"; 79 80 EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH) 81 .WillOnce(Invoke([&](auto in, auto &out) { 82 // First send an invalid response 83 std::unique_ptr<mockfs_buf_out> out0(new mockfs_buf_out); 84 out0->header.unique = in.header.unique; 85 out0->header.error = -ENOENT; 86 SET_OUT_HEADER_LEN(*out0, entry); // Invalid! 87 out0->expected_errno = EINVAL; 88 out.push_back(std::move(out0)); 89 90 // Then, respond to the lookup so we can complete the test 91 std::unique_ptr<mockfs_buf_out> out1(new mockfs_buf_out); 92 out1->header.unique = in.header.unique; 93 out1->header.error = -ENOENT; 94 out1->header.len = sizeof(out1->header); 95 out.push_back(std::move(out1)); 96 97 // The kernel may disconnect us for bad behavior, so don't try 98 // to read any more. 99 m_mock->m_quit = true; 100 })); 101 102 EXPECT_NE(0, access(FULLPATH, F_OK)); 103 104 EXPECT_EQ(ENOENT, errno); 105 } 106