1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00
falk-werner_webfuse-provider/test/webfuse_provider/provider/test_client.cc

255 lines
7.3 KiB
C++
Raw Normal View History

2020-06-24 20:03:38 +00:00
#include "webfuse_provider/webfuse_provider.h"
#include "webfuse_provider/test_util/webfuse_server.hpp"
#include "webfuse_provider/mocks/mock_provider_client.hpp"
#include "webfuse_provider/test_util/client.hpp"
#include <gtest/gtest.h>
2020-06-25 17:21:00 +00:00
#include <future>
#include <chrono>
2020-06-24 20:03:38 +00:00
using webfuse_test::WebfuseServer;
using webfuse_test::MockProviderClient;
using webfuse_test::Client;
2020-06-25 17:21:00 +00:00
using testing::Invoke;
2020-06-25 19:19:45 +00:00
using testing::_;
using testing::StrEq;
2020-06-25 17:21:00 +00:00
#define TIMEOUT (std::chrono::seconds(10))
2020-06-24 20:03:38 +00:00
TEST(Client, Connect)
{
MockProviderClient provider;
2020-06-25 17:21:00 +00:00
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(); }));
2020-06-24 20:03:38 +00:00
wfp_client_config * config = wfp_client_config_create();
provider.AttachTo(config);
2020-06-25 17:21:00 +00:00
{
WebfuseServer server;
Client client(config, server.GetUrl());
ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT));
2020-06-24 20:03:38 +00:00
2020-06-25 17:21:00 +00:00
client.Disconnect();
ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT));
}
2020-06-24 20:03:38 +00:00
2020-06-25 16:36:51 +00:00
wfp_client_config_dispose(config);
}
2020-06-25 20:08:48 +00:00
TEST(Client, ConnectWithTls)
{
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(); }));
wfp_client_config * config = wfp_client_config_create();
wfp_client_config_set_certpath(config, "client-cert.pem");
wfp_client_config_set_keypath(config, "client-key.pem");
wfp_client_config_set_ca_filepath(config, "server-cert.pem");
provider.AttachTo(config);
{
WebfuseServer server(true);
Client client(config, server.GetUrl());
ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT));
client.Disconnect();
ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT));
}
wfp_client_config_dispose(config);
}
2020-06-25 19:52:20 +00:00
TEST(Client, ConnectFailWithInvalidUrl)
{
MockProviderClient provider;
EXPECT_CALL(provider, OnConnected()).Times(0);
std::promise<void> disconnected;
EXPECT_CALL(provider, OnDisconnected()).Times(1)
.WillOnce(Invoke([&]() { disconnected.set_value(); }));
wfp_client_config * config = wfp_client_config_create();
provider.AttachTo(config);
{
Client client(config, "invalid_url");
ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT));
}
wfp_client_config_dispose(config);
}
TEST(Client, ConnectFailNotReachable)
{
MockProviderClient provider;
EXPECT_CALL(provider, OnConnected()).Times(0);
std::promise<void> disconnected;
EXPECT_CALL(provider, OnDisconnected()).Times(1)
.WillOnce(Invoke([&]() { disconnected.set_value(); }));
wfp_client_config * config = wfp_client_config_create();
provider.AttachTo(config);
{
Client client(config, "ws://localhost:4/");
ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT));
}
wfp_client_config_dispose(config);
}
2020-06-25 19:19:45 +00:00
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);
}
2020-06-25 20:08:48 +00:00
TEST(Client, LookupFail)
{
MockProviderClient provider;
2020-06-25 19:19:45 +00:00
2020-06-25 20:08:48 +00:00
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) {
throw std::runtime_error("something went wrong");
}));
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 * error = json_object_get(response, "error");
json_t * code = json_object_get(error, "code");
ASSERT_NE(0, json_integer_value(code));
json_decref(response);
client.Disconnect();
ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT));
}
wfp_client_config_dispose(config);
}
TEST(Client, Open)
2020-06-25 16:36:51 +00:00
{
MockProviderClient provider;
2020-06-25 17:21:00 +00:00
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(); }));
2020-06-25 16:36:51 +00:00
2020-06-25 20:22:26 +00:00
EXPECT_CALL(provider, Open(1, 0, _)).Times(1)
.WillOnce(Invoke([](int, int, uint32_t * handle) {
*handle = 4711;
2020-06-25 20:08:48 +00:00
}));
2020-06-25 16:36:51 +00:00
wfp_client_config * config = wfp_client_config_create();
provider.AttachTo(config);
2020-06-25 17:21:00 +00:00
{
2020-06-25 20:08:48 +00:00
WebfuseServer server;
2020-06-25 17:21:00 +00:00
Client client(config, server.GetUrl());
ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT));
2020-06-25 16:36:51 +00:00
2020-06-25 20:22:26 +00:00
json_t * response = server.Open(1, 0);
2020-06-25 20:08:48 +00:00
ASSERT_TRUE(json_is_object(response));
json_t * result = json_object_get(response, "result");
2020-06-25 20:22:26 +00:00
json_t * handle = json_object_get(result, "handle");
ASSERT_EQ(4711, json_integer_value(handle));
2020-06-25 20:08:48 +00:00
json_decref(response);
2020-06-25 17:21:00 +00:00
client.Disconnect();
ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT));
}
2020-06-25 16:36:51 +00:00
2020-06-24 20:03:38 +00:00
wfp_client_config_dispose(config);
2020-06-25 20:08:48 +00:00
}