2016-10-27 07:57:55 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specic language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libappfuse/FuseBridgeLoop.h"
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <thread>
|
|
|
|
|
2016-11-08 02:17:51 +01:00
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/unique_fd.h>
|
2016-10-27 07:57:55 +02:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
namespace android {
|
2016-11-08 02:17:51 +01:00
|
|
|
namespace fuse {
|
|
|
|
namespace {
|
2016-10-27 07:57:55 +02:00
|
|
|
|
2016-11-08 02:17:51 +01:00
|
|
|
class Callback : public FuseBridgeLoopCallback {
|
2016-10-27 07:57:55 +02:00
|
|
|
public:
|
|
|
|
bool mounted;
|
2017-03-06 07:37:20 +01:00
|
|
|
bool closed;
|
|
|
|
Callback() : mounted(false), closed(false) {}
|
|
|
|
|
|
|
|
void OnMount(int /*mount_id*/) override { mounted = true; }
|
|
|
|
|
|
|
|
void OnClosed(int /* mount_id */) override { closed = true; }
|
2016-10-27 07:57:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class FuseBridgeLoopTest : public ::testing::Test {
|
|
|
|
protected:
|
2016-11-08 02:17:51 +01:00
|
|
|
base::unique_fd dev_sockets_[2];
|
|
|
|
base::unique_fd proxy_sockets_[2];
|
2016-10-27 07:57:55 +02:00
|
|
|
Callback callback_;
|
|
|
|
std::thread thread_;
|
|
|
|
|
|
|
|
FuseRequest request_;
|
|
|
|
FuseResponse response_;
|
|
|
|
|
2016-11-08 02:17:51 +01:00
|
|
|
void SetUp() override {
|
|
|
|
base::SetMinimumLogSeverity(base::VERBOSE);
|
2017-03-06 06:02:42 +01:00
|
|
|
ASSERT_TRUE(SetupMessageSockets(&dev_sockets_));
|
|
|
|
ASSERT_TRUE(SetupMessageSockets(&proxy_sockets_));
|
2016-10-27 07:57:55 +02:00
|
|
|
thread_ = std::thread([this] {
|
2017-03-06 07:37:20 +01:00
|
|
|
FuseBridgeLoop loop;
|
|
|
|
loop.AddBridge(1, std::move(dev_sockets_[1]), std::move(proxy_sockets_[0]));
|
|
|
|
loop.Start(&callback_);
|
2016-10-27 07:57:55 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckNotImpl(uint32_t opcode) {
|
|
|
|
SCOPED_TRACE((std::ostringstream() << "opcode: " << opcode).str());
|
|
|
|
|
|
|
|
memset(&request_, 0, sizeof(FuseRequest));
|
|
|
|
request_.header.opcode = opcode;
|
|
|
|
request_.header.len = sizeof(fuse_in_header);
|
2017-06-15 08:48:07 +02:00
|
|
|
request_.header.unique = 1;
|
2016-10-27 07:57:55 +02:00
|
|
|
ASSERT_TRUE(request_.Write(dev_sockets_[0]));
|
|
|
|
|
|
|
|
memset(&response_, 0, sizeof(FuseResponse));
|
|
|
|
ASSERT_TRUE(response_.Read(dev_sockets_[0]));
|
|
|
|
EXPECT_EQ(-ENOSYS, response_.header.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckProxy(uint32_t opcode) {
|
|
|
|
SCOPED_TRACE((std::ostringstream() << "opcode: " << opcode).str());
|
|
|
|
|
|
|
|
memset(&request_, 0, sizeof(FuseRequest));
|
|
|
|
request_.header.opcode = opcode;
|
|
|
|
request_.header.unique = opcode; // Use opcode as unique.
|
|
|
|
request_.header.len = sizeof(fuse_in_header);
|
|
|
|
ASSERT_TRUE(request_.Write(dev_sockets_[0]));
|
|
|
|
|
|
|
|
memset(&request_, 0, sizeof(FuseRequest));
|
|
|
|
ASSERT_TRUE(request_.Read(proxy_sockets_[1]));
|
|
|
|
EXPECT_EQ(opcode, request_.header.opcode);
|
|
|
|
EXPECT_EQ(opcode, request_.header.unique);
|
|
|
|
|
|
|
|
memset(&response_, 0, sizeof(FuseResponse));
|
|
|
|
response_.header.len = sizeof(fuse_out_header);
|
|
|
|
response_.header.unique = opcode; // Use opcode as unique.
|
|
|
|
response_.header.error = kFuseSuccess;
|
|
|
|
ASSERT_TRUE(response_.Write(proxy_sockets_[1]));
|
|
|
|
|
|
|
|
memset(&response_, 0, sizeof(FuseResponse));
|
|
|
|
ASSERT_TRUE(response_.Read(dev_sockets_[0]));
|
|
|
|
EXPECT_EQ(opcode, response_.header.unique);
|
|
|
|
EXPECT_EQ(kFuseSuccess, response_.header.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SendInitRequest(uint64_t unique) {
|
|
|
|
memset(&request_, 0, sizeof(FuseRequest));
|
|
|
|
request_.header.opcode = FUSE_INIT;
|
|
|
|
request_.header.unique = unique;
|
|
|
|
request_.header.len = sizeof(fuse_in_header) + sizeof(fuse_init_in);
|
|
|
|
request_.init_in.major = FUSE_KERNEL_VERSION;
|
|
|
|
request_.init_in.minor = FUSE_KERNEL_MINOR_VERSION;
|
|
|
|
ASSERT_TRUE(request_.Write(dev_sockets_[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Close() {
|
2016-11-08 02:17:51 +01:00
|
|
|
dev_sockets_[0].reset();
|
|
|
|
dev_sockets_[1].reset();
|
|
|
|
proxy_sockets_[0].reset();
|
|
|
|
proxy_sockets_[1].reset();
|
2016-10-27 07:57:55 +02:00
|
|
|
if (thread_.joinable()) {
|
|
|
|
thread_.join();
|
|
|
|
}
|
2017-03-06 07:37:20 +01:00
|
|
|
ASSERT_TRUE(callback_.closed);
|
2016-10-27 07:57:55 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 02:17:51 +01:00
|
|
|
void TearDown() override {
|
2016-10-27 07:57:55 +02:00
|
|
|
Close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-08 02:17:51 +01:00
|
|
|
} // namespace
|
|
|
|
|
2016-10-27 07:57:55 +02:00
|
|
|
TEST_F(FuseBridgeLoopTest, FuseInit) {
|
|
|
|
SendInitRequest(1u);
|
|
|
|
|
|
|
|
memset(&response_, 0, sizeof(FuseResponse));
|
|
|
|
ASSERT_TRUE(response_.Read(dev_sockets_[0]));
|
|
|
|
EXPECT_EQ(kFuseSuccess, response_.header.error);
|
|
|
|
EXPECT_EQ(1u, response_.header.unique);
|
|
|
|
|
|
|
|
// Unmount.
|
|
|
|
Close();
|
|
|
|
EXPECT_TRUE(callback_.mounted);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(FuseBridgeLoopTest, FuseForget) {
|
|
|
|
memset(&request_, 0, sizeof(FuseRequest));
|
|
|
|
request_.header.opcode = FUSE_FORGET;
|
|
|
|
request_.header.unique = 1u;
|
|
|
|
request_.header.len = sizeof(fuse_in_header) + sizeof(fuse_forget_in);
|
|
|
|
ASSERT_TRUE(request_.Write(dev_sockets_[0]));
|
|
|
|
|
|
|
|
SendInitRequest(2u);
|
|
|
|
|
|
|
|
memset(&response_, 0, sizeof(FuseResponse));
|
|
|
|
ASSERT_TRUE(response_.Read(dev_sockets_[0]));
|
|
|
|
EXPECT_EQ(2u, response_.header.unique) <<
|
|
|
|
"The loop must not respond to FUSE_FORGET";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(FuseBridgeLoopTest, FuseNotImpl) {
|
|
|
|
CheckNotImpl(FUSE_SETATTR);
|
|
|
|
CheckNotImpl(FUSE_READLINK);
|
|
|
|
CheckNotImpl(FUSE_SYMLINK);
|
|
|
|
CheckNotImpl(FUSE_MKNOD);
|
|
|
|
CheckNotImpl(FUSE_MKDIR);
|
|
|
|
CheckNotImpl(FUSE_UNLINK);
|
|
|
|
CheckNotImpl(FUSE_RMDIR);
|
|
|
|
CheckNotImpl(FUSE_RENAME);
|
|
|
|
CheckNotImpl(FUSE_LINK);
|
|
|
|
CheckNotImpl(FUSE_STATFS);
|
|
|
|
CheckNotImpl(FUSE_SETXATTR);
|
|
|
|
CheckNotImpl(FUSE_GETXATTR);
|
|
|
|
CheckNotImpl(FUSE_LISTXATTR);
|
|
|
|
CheckNotImpl(FUSE_REMOVEXATTR);
|
2016-11-08 02:17:51 +01:00
|
|
|
CheckNotImpl(FUSE_FLUSH);
|
2016-10-27 07:57:55 +02:00
|
|
|
CheckNotImpl(FUSE_OPENDIR);
|
|
|
|
CheckNotImpl(FUSE_READDIR);
|
|
|
|
CheckNotImpl(FUSE_RELEASEDIR);
|
|
|
|
CheckNotImpl(FUSE_FSYNCDIR);
|
|
|
|
CheckNotImpl(FUSE_GETLK);
|
|
|
|
CheckNotImpl(FUSE_SETLK);
|
|
|
|
CheckNotImpl(FUSE_SETLKW);
|
|
|
|
CheckNotImpl(FUSE_ACCESS);
|
|
|
|
CheckNotImpl(FUSE_CREATE);
|
|
|
|
CheckNotImpl(FUSE_INTERRUPT);
|
|
|
|
CheckNotImpl(FUSE_BMAP);
|
|
|
|
CheckNotImpl(FUSE_DESTROY);
|
|
|
|
CheckNotImpl(FUSE_IOCTL);
|
|
|
|
CheckNotImpl(FUSE_POLL);
|
|
|
|
CheckNotImpl(FUSE_NOTIFY_REPLY);
|
|
|
|
CheckNotImpl(FUSE_BATCH_FORGET);
|
|
|
|
CheckNotImpl(FUSE_FALLOCATE);
|
|
|
|
CheckNotImpl(FUSE_READDIRPLUS);
|
|
|
|
CheckNotImpl(FUSE_RENAME2);
|
|
|
|
CheckNotImpl(FUSE_LSEEK);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(FuseBridgeLoopTest, Proxy) {
|
|
|
|
CheckProxy(FUSE_LOOKUP);
|
|
|
|
CheckProxy(FUSE_GETATTR);
|
|
|
|
CheckProxy(FUSE_READ);
|
|
|
|
CheckProxy(FUSE_WRITE);
|
2016-11-08 02:17:51 +01:00
|
|
|
CheckProxy(FUSE_FSYNC);
|
2016-11-15 01:31:21 +01:00
|
|
|
|
|
|
|
// Invoke FUSE_OPEN and FUSE_RELEASE at last as the loop will exit when all files are closed.
|
|
|
|
CheckProxy(FUSE_OPEN);
|
|
|
|
CheckProxy(FUSE_RELEASE);
|
|
|
|
|
|
|
|
// Ensure the loop exits.
|
|
|
|
Close();
|
2016-10-27 07:57:55 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 02:17:51 +01:00
|
|
|
} // namespace fuse
|
|
|
|
} // namespace android
|