mirror of
https://github.com/falk-werner/webfuse-provider
synced 2024-10-27 20:44:10 +00:00
added test to lookup file
This commit is contained in:
parent
edcad5dc4c
commit
28f3f4ca47
@ -146,6 +146,7 @@ static int wf_impl_client_protocol_lws_callback(
|
||||
break;
|
||||
case LWS_CALLBACK_CLIENT_RECEIVE:
|
||||
wf_impl_client_protocol_process(protocol, in, len);
|
||||
break;
|
||||
case LWS_CALLBACK_SERVER_WRITEABLE:
|
||||
// fall-through
|
||||
case LWS_CALLBACK_CLIENT_WRITEABLE:
|
||||
@ -169,6 +170,11 @@ static int wf_impl_client_protocol_lws_callback(
|
||||
}
|
||||
}
|
||||
break;
|
||||
case LWS_CALLBACK_RAW_RX_FILE:
|
||||
if ((NULL != protocol->filesystem) && (wsi == protocol->filesystem->wsi))
|
||||
{
|
||||
wf_impl_filesystem_process_request(protocol->filesystem);
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@ -201,15 +207,17 @@ void
|
||||
wf_impl_client_protocol_cleanup(
|
||||
struct wf_client_protocol * protocol)
|
||||
{
|
||||
if (NULL != protocol->filesystem)
|
||||
{
|
||||
wf_impl_filesystem_dispose(protocol->filesystem);
|
||||
}
|
||||
|
||||
protocol->callback(protocol->user_data, WF_CLIENT_CLEANUP, NULL);
|
||||
|
||||
wf_jsonrpc_proxy_dispose(protocol->proxy);
|
||||
wf_timer_manager_dispose(protocol->timer_manager);
|
||||
wf_message_queue_cleanup(&protocol->messages);
|
||||
|
||||
if (NULL != protocol->filesystem)
|
||||
{
|
||||
wf_impl_filesystem_dispose(protocol->filesystem);
|
||||
protocol->filesystem = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
|
50
test/webfuse/mocks/lookup_matcher.hpp
Normal file
50
test/webfuse/mocks/lookup_matcher.hpp
Normal file
@ -0,0 +1,50 @@
|
||||
#ifndef WF_LOOKUP_MATCHER_HPP
|
||||
#define WF_LOOKUP_MATCHER_HPP
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include <jansson.h>
|
||||
#include <cstring>
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
MATCHER_P2(Lookup, parent, name, "")
|
||||
{
|
||||
if (!json_is_array(arg))
|
||||
{
|
||||
*result_listener << "json array expected";
|
||||
return false;
|
||||
}
|
||||
|
||||
json_t * parent_ = json_array_get(arg, 1);
|
||||
if (!json_is_integer(parent_))
|
||||
{
|
||||
*result_listener << "parent is expected to be an integer";
|
||||
return false;
|
||||
}
|
||||
if (parent != json_integer_value(parent_))
|
||||
{
|
||||
*result_listener << "parent mismatch: expected " << parent
|
||||
<< " but was " << json_integer_value(parent_);
|
||||
return false;
|
||||
}
|
||||
|
||||
json_t * name_ = json_array_get(arg, 2);
|
||||
if (!json_is_string(name_))
|
||||
{
|
||||
*result_listener << "name is expected to be a string";
|
||||
return false;
|
||||
}
|
||||
if (0 != strcmp(name, json_string_value(name_)))
|
||||
{
|
||||
*result_listener << "name mismatch: expected \"" << name
|
||||
<< "\" but was \"" << json_string_value(name_) << "\"";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -9,12 +9,16 @@
|
||||
#include "webfuse/mocks/mock_adapter_client_callback.hpp"
|
||||
#include "webfuse/mocks/mock_invokation_handler.hpp"
|
||||
#include "webfuse/utils/timeout_watcher.hpp"
|
||||
#include "webfuse/tests/integration/file.hpp"
|
||||
#include "webfuse/mocks/lookup_matcher.hpp"
|
||||
|
||||
using webfuse_test::AdapterClient;
|
||||
using webfuse_test::WsServer2;
|
||||
using webfuse_test::MockInvokationHander;
|
||||
using webfuse_test::MockAdapterClientCallback;
|
||||
using webfuse_test::TimeoutWatcher;
|
||||
using webfuse_test::File;
|
||||
using webfuse_test::Lookup;
|
||||
using testing::_;
|
||||
using testing::Invoke;
|
||||
using testing::AnyNumber;
|
||||
@ -182,3 +186,137 @@ TEST(AdapterClient, AuthenticationFailed)
|
||||
client.Disconnect();
|
||||
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return disconnected; }));
|
||||
}
|
||||
|
||||
TEST(AdapterClient, AddFileSystem)
|
||||
{
|
||||
TimeoutWatcher watcher(TIMEOUT);
|
||||
|
||||
MockInvokationHander handler;
|
||||
WsServer2 server(handler, WF_PROTOCOL_NAME_PROVIDER_SERVER);
|
||||
EXPECT_CALL(handler, Invoke(StrEq("add_filesystem"),_)).Times(1)
|
||||
.WillOnce(Return("{\"id\": \"test\"}"));
|
||||
EXPECT_CALL(handler, Invoke(StrEq("lookup"), _)).Times(AnyNumber())
|
||||
.WillRepeatedly(Throw(std::runtime_error("unknown")));
|
||||
|
||||
MockAdapterClientCallback callback;
|
||||
EXPECT_CALL(callback, Invoke(_, _, _)).Times(AnyNumber());
|
||||
|
||||
bool connected = false;
|
||||
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CONNECTED, nullptr)).Times(1)
|
||||
.WillOnce(Invoke([&] (wf_client *, int, void *) mutable { connected = true; }));
|
||||
|
||||
bool disconnected = false;
|
||||
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_DISCONNECTED, nullptr)).Times(1)
|
||||
.WillOnce(Invoke([&] (wf_client *, int, void *) mutable { disconnected = true; }));
|
||||
|
||||
bool called = false;
|
||||
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_FILESYSTEM_ADDED, nullptr)).Times(1)
|
||||
.WillOnce(Invoke([&called] (wf_client *, int, void *) mutable {
|
||||
called = true;
|
||||
}));
|
||||
|
||||
AdapterClient client(callback.GetCallbackFn(), callback.GetUserData(), server.GetUrl());
|
||||
|
||||
client.Connect();
|
||||
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return connected; }));
|
||||
|
||||
client.AddFileSystem();
|
||||
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return called; }));
|
||||
|
||||
client.Disconnect();
|
||||
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return disconnected; }));
|
||||
}
|
||||
|
||||
TEST(AdapterClient, AddFileSystemFailed)
|
||||
{
|
||||
TimeoutWatcher watcher(TIMEOUT);
|
||||
|
||||
MockInvokationHander handler;
|
||||
WsServer2 server(handler, WF_PROTOCOL_NAME_PROVIDER_SERVER);
|
||||
EXPECT_CALL(handler, Invoke(StrEq("add_filesystem"),_)).Times(1)
|
||||
.WillOnce(Throw(std::runtime_error("failed")));
|
||||
|
||||
MockAdapterClientCallback callback;
|
||||
EXPECT_CALL(callback, Invoke(_, _, _)).Times(AnyNumber());
|
||||
|
||||
bool connected = false;
|
||||
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CONNECTED, nullptr)).Times(1)
|
||||
.WillOnce(Invoke([&] (wf_client *, int, void *) mutable { connected = true; }));
|
||||
|
||||
bool disconnected = false;
|
||||
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_DISCONNECTED, nullptr)).Times(1)
|
||||
.WillOnce(Invoke([&] (wf_client *, int, void *) mutable { disconnected = true; }));
|
||||
|
||||
bool called = false;
|
||||
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_FILESYSTEM_ADD_FAILED, nullptr)).Times(1)
|
||||
.WillOnce(Invoke([&called] (wf_client *, int, void *) mutable {
|
||||
called = true;
|
||||
}));
|
||||
|
||||
AdapterClient client(callback.GetCallbackFn(), callback.GetUserData(), server.GetUrl());
|
||||
|
||||
client.Connect();
|
||||
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return connected; }));
|
||||
|
||||
client.AddFileSystem();
|
||||
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return called; }));
|
||||
|
||||
client.Disconnect();
|
||||
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return disconnected; }));
|
||||
}
|
||||
|
||||
TEST(AdapterClient, LookupFile)
|
||||
{
|
||||
TimeoutWatcher watcher(TIMEOUT);
|
||||
|
||||
MockInvokationHander handler;
|
||||
WsServer2 server(handler, WF_PROTOCOL_NAME_PROVIDER_SERVER);
|
||||
EXPECT_CALL(handler, Invoke(StrEq("add_filesystem"),_)).Times(1)
|
||||
.WillOnce(Return("{\"id\": \"test\"}"));
|
||||
EXPECT_CALL(handler, Invoke(StrEq("lookup"), _)).Times(AnyNumber())
|
||||
.WillRepeatedly(Throw(std::runtime_error("unknown")));
|
||||
EXPECT_CALL(handler, Invoke(StrEq("lookup"), Lookup(1, "Hello.txt"))).Times(AnyNumber())
|
||||
.WillRepeatedly(Return(
|
||||
"{"
|
||||
"\"inode\": 2,"
|
||||
"\"mode\": 420," //0644
|
||||
"\"type\": \"file\","
|
||||
"\"size\": 42,"
|
||||
"\"atime\": 0,"
|
||||
"\"mtime\": 0,"
|
||||
"\"ctime\": 0"
|
||||
"}"
|
||||
));
|
||||
|
||||
MockAdapterClientCallback callback;
|
||||
EXPECT_CALL(callback, Invoke(_, _, _)).Times(AnyNumber());
|
||||
|
||||
bool connected = false;
|
||||
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CONNECTED, nullptr)).Times(1)
|
||||
.WillOnce(Invoke([&] (wf_client *, int, void *) mutable { connected = true; }));
|
||||
|
||||
bool disconnected = false;
|
||||
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_DISCONNECTED, nullptr)).Times(1)
|
||||
.WillOnce(Invoke([&] (wf_client *, int, void *) mutable { disconnected = true; }));
|
||||
|
||||
bool called = false;
|
||||
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_FILESYSTEM_ADDED, nullptr)).Times(1)
|
||||
.WillOnce(Invoke([&called] (wf_client *, int, void *) mutable {
|
||||
called = true;
|
||||
}));
|
||||
|
||||
AdapterClient client(callback.GetCallbackFn(), callback.GetUserData(), server.GetUrl());
|
||||
|
||||
client.Connect();
|
||||
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return connected; }));
|
||||
|
||||
client.AddFileSystem();
|
||||
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return called; }));
|
||||
|
||||
std::string file_name = client.GetDir() + "/Hello.txt";
|
||||
File file(file_name);
|
||||
ASSERT_TRUE(file.isFile());
|
||||
|
||||
client.Disconnect();
|
||||
ASSERT_TRUE(watcher.waitUntil([&]() mutable { return disconnected; }));
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include "webfuse/utils/adapter_client.hpp"
|
||||
#include "webfuse/utils/tempdir.hpp"
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
|
||||
namespace
|
||||
{
|
||||
@ -31,6 +31,7 @@ public:
|
||||
: client(wf_client_create(callback, user_data))
|
||||
, url_(url)
|
||||
, command(Command::run)
|
||||
, tempdir("webfuse_adpter_client")
|
||||
{
|
||||
thread = std::thread(&Run, this);
|
||||
}
|
||||
@ -52,6 +53,11 @@ public:
|
||||
wf_client_interrupt(client);
|
||||
}
|
||||
|
||||
std::string GetDir()
|
||||
{
|
||||
return tempdir.path();
|
||||
}
|
||||
|
||||
private:
|
||||
static void Run(Private * self)
|
||||
{
|
||||
@ -80,7 +86,7 @@ private:
|
||||
wf_client_authenticate(self->client);
|
||||
break;
|
||||
case Command::add_filesystem:
|
||||
wf_client_add_filesystem(self->client, "/tmp", "test");
|
||||
wf_client_add_filesystem(self->client, self->tempdir.path(), "test");
|
||||
break;
|
||||
case Command::shutdown:
|
||||
// fall-through
|
||||
@ -95,6 +101,7 @@ private:
|
||||
wf_client * client;
|
||||
std::string url_;
|
||||
Command command;
|
||||
TempDir tempdir;
|
||||
std::thread thread;
|
||||
std::mutex mutex;
|
||||
};
|
||||
@ -133,4 +140,10 @@ void AdapterClient::AddFileSystem()
|
||||
d->ApplyCommand(Command::add_filesystem);
|
||||
}
|
||||
|
||||
std::string AdapterClient::GetDir() const
|
||||
{
|
||||
return d->GetDir();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -21,6 +21,7 @@ public:
|
||||
void Disconnect();
|
||||
void Authenticate();
|
||||
void AddFileSystem();
|
||||
std::string GetDir() const;
|
||||
private:
|
||||
class Private;
|
||||
Private * d;
|
||||
|
Loading…
Reference in New Issue
Block a user