mirror of
https://github.com/falk-werner/webfuse-provider
synced 2024-10-27 20:44:10 +00:00
added test for lookup
This commit is contained in:
parent
aa06846c15
commit
b45b7873ab
@ -32,6 +32,7 @@ static void webfuse_test_iproviderclient_onlookup(
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
struct stat buffer;
|
struct stat buffer;
|
||||||
|
memset(&buffer, 0, sizeof(buffer));
|
||||||
self->Lookup(parent, name, &buffer);
|
self->Lookup(parent, name, &buffer);
|
||||||
wfp_respond_lookup(request, &buffer);
|
wfp_respond_lookup(request, &buffer);
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,8 @@ using webfuse_test::WebfuseServer;
|
|||||||
using webfuse_test::MockProviderClient;
|
using webfuse_test::MockProviderClient;
|
||||||
using webfuse_test::Client;
|
using webfuse_test::Client;
|
||||||
using testing::Invoke;
|
using testing::Invoke;
|
||||||
|
using testing::_;
|
||||||
|
using testing::StrEq;
|
||||||
|
|
||||||
#define TIMEOUT (std::chrono::seconds(10))
|
#define TIMEOUT (std::chrono::seconds(10))
|
||||||
|
|
||||||
@ -42,6 +44,56 @@ TEST(Client, Connect)
|
|||||||
wfp_client_config_dispose(config);
|
wfp_client_config_dispose(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(Client, Lookup)
|
||||||
|
{
|
||||||
|
MockProviderClient provider;
|
||||||
|
|
||||||
|
std::promise<void> connected;
|
||||||
|
EXPECT_CALL(provider, OnConnected()).Times(1)
|
||||||
|
.WillOnce(Invoke([&]() { connected.set_value(); }));
|
||||||
|
|
||||||
|
std::promise<void> disconnected;
|
||||||
|
EXPECT_CALL(provider, OnDisconnected()).Times(1)
|
||||||
|
.WillOnce(Invoke([&]() { disconnected.set_value(); }));
|
||||||
|
|
||||||
|
EXPECT_CALL(provider, Lookup(1,StrEq("foo"),_)).Times(1)
|
||||||
|
.WillOnce(Invoke([](ino_t, char const *, struct stat * result) {
|
||||||
|
result->st_ino = 42;
|
||||||
|
result->st_mode = S_IFREG | 0644;
|
||||||
|
}));
|
||||||
|
|
||||||
|
wfp_client_config * config = wfp_client_config_create();
|
||||||
|
provider.AttachTo(config);
|
||||||
|
|
||||||
|
{
|
||||||
|
WebfuseServer server;
|
||||||
|
Client client(config, server.GetUrl());
|
||||||
|
|
||||||
|
ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT));
|
||||||
|
|
||||||
|
json_t * response = server.Lookup(1, "foo");
|
||||||
|
ASSERT_TRUE(json_is_object(response));
|
||||||
|
json_t * result = json_object_get(response, "result");
|
||||||
|
|
||||||
|
json_t * inode = json_object_get(result, "inode");
|
||||||
|
ASSERT_EQ(42, json_integer_value(inode));
|
||||||
|
|
||||||
|
json_t * mode = json_object_get(result, "mode");
|
||||||
|
ASSERT_EQ(0644, json_integer_value(mode));
|
||||||
|
|
||||||
|
json_t * type = json_object_get(result, "type");
|
||||||
|
ASSERT_STREQ("file", json_string_value(type));
|
||||||
|
|
||||||
|
json_decref(response);
|
||||||
|
|
||||||
|
client.Disconnect();
|
||||||
|
ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT));
|
||||||
|
}
|
||||||
|
|
||||||
|
wfp_client_config_dispose(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
TEST(Client, ConnectWithTls)
|
TEST(Client, ConnectWithTls)
|
||||||
{
|
{
|
||||||
MockProviderClient provider;
|
MockProviderClient provider;
|
||||||
|
@ -5,9 +5,13 @@
|
|||||||
#include <libwebsockets.h>
|
#include <libwebsockets.h>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <future>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
#define TIMEOUT (std::chrono::seconds(10))
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
@ -74,7 +78,9 @@ class WebfuseServer::Private: public IServer
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Private(bool use_tls)
|
Private(bool use_tls)
|
||||||
: is_shutdown_requested(false)
|
: id(0)
|
||||||
|
, is_shutdown_requested(false)
|
||||||
|
, message(nullptr)
|
||||||
, client(nullptr)
|
, client(nullptr)
|
||||||
{
|
{
|
||||||
wfp_impl_lwslog_disable();
|
wfp_impl_lwslog_disable();
|
||||||
@ -132,9 +138,44 @@ public:
|
|||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string const & GetFilesystem() const
|
||||||
|
{
|
||||||
|
return filesystem;
|
||||||
|
}
|
||||||
|
|
||||||
json_t * Invoke(std::string const & method, json_t * params)
|
json_t * Invoke(std::string const & method, json_t * params)
|
||||||
{
|
{
|
||||||
throw std::runtime_error("not implemented");
|
std::promise<std::string> response;
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(mutex);
|
||||||
|
message = &response;
|
||||||
|
id++;
|
||||||
|
|
||||||
|
json_t * request = json_object();
|
||||||
|
json_object_set_new(request, "method", json_string(method.c_str()));
|
||||||
|
json_object_set_new(request, "params", params);
|
||||||
|
json_object_set_new(request, "id", json_integer(id));
|
||||||
|
|
||||||
|
char * request_text = json_dumps(request, 0);
|
||||||
|
write_queue.push(request_text);
|
||||||
|
free(request_text);
|
||||||
|
json_decref(request);
|
||||||
|
}
|
||||||
|
lws_callback_on_writable(client);
|
||||||
|
|
||||||
|
json_t * result = nullptr;
|
||||||
|
auto future = response.get_future();
|
||||||
|
auto state = future.wait_for(TIMEOUT);
|
||||||
|
if (std::future_status::ready == state)
|
||||||
|
{
|
||||||
|
std::string response_text = future.get();
|
||||||
|
result = json_loadb(response_text.c_str(), response_text.size(), 0, nullptr);
|
||||||
|
|
||||||
|
std::unique_lock<std::mutex> lock(mutex);
|
||||||
|
message = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnConnected(lws * wsi) override
|
void OnConnected(lws * wsi) override
|
||||||
@ -151,6 +192,14 @@ public:
|
|||||||
|
|
||||||
void OnMessageReceived(lws * wsi, char const * data, size_t length) override
|
void OnMessageReceived(lws * wsi, char const * data, size_t length) override
|
||||||
{
|
{
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(mutex);
|
||||||
|
if (nullptr != message)
|
||||||
|
{
|
||||||
|
message->set_value(std::string(data, length));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
json_t * message = json_loadb(data, length, 0, nullptr);
|
json_t * message = json_loadb(data, length, 0, nullptr);
|
||||||
if (message)
|
if (message)
|
||||||
{
|
{
|
||||||
@ -163,7 +212,7 @@ public:
|
|||||||
|
|
||||||
json_t * response = json_object();
|
json_t * response = json_object();
|
||||||
json_t * result = json_object();
|
json_t * result = json_object();
|
||||||
json_object_set_new(result, "id", json_string("fs"));
|
json_object_set_new(result, "id", json_string(GetFilesystem().c_str()));
|
||||||
json_object_set_new(response, "result", result);
|
json_object_set_new(response, "result", result);
|
||||||
json_object_set(response, "id", id);
|
json_object_set(response, "id", id);
|
||||||
|
|
||||||
@ -178,6 +227,7 @@ public:
|
|||||||
lws_callback_on_writable(wsi);
|
lws_callback_on_writable(wsi);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
json_decref(message);
|
json_decref(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -225,7 +275,9 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int id;
|
||||||
bool is_shutdown_requested;
|
bool is_shutdown_requested;
|
||||||
|
std::promise<std::string> * message;
|
||||||
lws * client;
|
lws * client;
|
||||||
std::string url;
|
std::string url;
|
||||||
lws_context * context;
|
lws_context * context;
|
||||||
@ -234,6 +286,8 @@ private:
|
|||||||
std::thread thread;
|
std::thread thread;
|
||||||
std::mutex mutex;
|
std::mutex mutex;
|
||||||
std::queue<std::string> write_queue;
|
std::queue<std::string> write_queue;
|
||||||
|
|
||||||
|
std::string filesystem = "fs";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -253,10 +307,26 @@ std::string const & WebfuseServer::GetUrl()
|
|||||||
return d->GetUrl();
|
return d->GetUrl();
|
||||||
}
|
}
|
||||||
|
|
||||||
json_t * WebfuseServer::Invoke(std::string method, json_t * params)
|
json_t * WebfuseServer::Invoke(std::string const & method, json_t * params)
|
||||||
{
|
{
|
||||||
return d->Invoke(method, params);
|
return d->Invoke(method, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
json_t * WebfuseServer::Invoke(std::string const & method, std::string const & params)
|
||||||
|
{
|
||||||
|
json_t * params_json = json_loads(params.c_str(), 0, nullptr);
|
||||||
|
return d->Invoke(method, params_json);
|
||||||
|
}
|
||||||
|
|
||||||
|
json_t * WebfuseServer::Lookup(int parent, std::string const & name)
|
||||||
|
{
|
||||||
|
json_t * params = json_array();
|
||||||
|
json_array_append_new(params, json_string(d->GetFilesystem().c_str()));
|
||||||
|
json_array_append_new(params, json_integer(parent));
|
||||||
|
json_array_append_new(params, json_string(name.c_str()));
|
||||||
|
|
||||||
|
return d->Invoke("lookup", params);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -15,7 +15,9 @@ public:
|
|||||||
WebfuseServer(bool use_tls = false);
|
WebfuseServer(bool use_tls = false);
|
||||||
~WebfuseServer();
|
~WebfuseServer();
|
||||||
std::string const & GetUrl();
|
std::string const & GetUrl();
|
||||||
json_t * Invoke(std::string method, json_t * params);
|
json_t * Invoke(std::string const & method, json_t * params);
|
||||||
|
json_t * Invoke(std::string const & method, std::string const & params);
|
||||||
|
json_t * Lookup(int parent, std::string const & name);
|
||||||
private:
|
private:
|
||||||
class Private;
|
class Private;
|
||||||
Private * d;
|
Private * d;
|
||||||
|
Loading…
Reference in New Issue
Block a user