/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2023 Axcient4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627extern "C" {28#include <fcntl.h>29#include <unistd.h>30}3132#include "mockfs.hh"33#include "utils.hh"3435using namespace testing;3637class BadServer: public FuseTest {};3839/*40* If the server sends a response for an unknown request, the kernel should41* gracefully return EINVAL.42*/43TEST_F(BadServer, UnknownUnique)44{45mockfs_buf_out out;4647out.header.len = sizeof(out.header);48out.header.error = 0;49out.header.unique = 99999; // Invalid!50out.expected_errno = EINVAL;51m_mock->write_response(out);52}5354/*55* If the server sends less than a header's worth of data, the kernel should56* gracefully return EINVAL.57*/58TEST_F(BadServer, ShortWrite)59{60mockfs_buf_out out;6162out.header.len = sizeof(out.header) - 1;63out.header.error = 0;64out.header.unique = 0; // Asynchronous notification65out.expected_errno = EINVAL;66m_mock->write_response(out);67}6869/*70* It is illegal to report an error, and also send back a payload.71*/72TEST_F(BadServer, ErrorWithPayload)73{74const char FULLPATH[] = "mountpoint/some_file.txt";75const char RELPATH[] = "some_file.txt";7677EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)78.WillOnce(Invoke([&](auto in, auto &out) {79// First send an invalid response80std::unique_ptr<mockfs_buf_out> out0(new mockfs_buf_out);81out0->header.unique = in.header.unique;82out0->header.error = -ENOENT;83SET_OUT_HEADER_LEN(*out0, entry); // Invalid!84out0->expected_errno = EINVAL;85out.push_back(std::move(out0));8687// Then, respond to the lookup so we can complete the test88std::unique_ptr<mockfs_buf_out> out1(new mockfs_buf_out);89out1->header.unique = in.header.unique;90out1->header.error = -ENOENT;91out1->header.len = sizeof(out1->header);92out.push_back(std::move(out1));9394// The kernel may disconnect us for bad behavior, so don't try95// to read any more.96m_mock->m_quit = true;97}));9899EXPECT_NE(0, access(FULLPATH, F_OK));100101EXPECT_EQ(ENOENT, errno);102}103104105