2020-02-19 21:44:56 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <gmock/gmock.h>
|
|
|
|
|
|
|
|
#include <webfuse/provider/client_protocol.h>
|
|
|
|
#include <webfuse/provider/client_config.h>
|
2020-02-22 15:46:21 +00:00
|
|
|
#include "webfuse/utils/ws_server.hpp"
|
2020-02-23 22:20:26 +00:00
|
|
|
#include "webfuse/mocks/mock_provider_client.hpp"
|
2020-02-19 21:44:56 +00:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <thread>
|
|
|
|
#include <atomic>
|
|
|
|
|
2020-02-22 15:46:21 +00:00
|
|
|
using webfuse_test::WebsocketServer;
|
2020-02-23 22:20:26 +00:00
|
|
|
using webfuse_test::MockProviderClient;
|
2020-02-24 17:16:30 +00:00
|
|
|
using webfuse_test::IProviderClient;
|
2020-02-19 21:44:56 +00:00
|
|
|
using testing::_;
|
2020-02-24 17:16:30 +00:00
|
|
|
using testing::AtMost;
|
2020-02-19 21:44:56 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2020-02-24 17:16:30 +00:00
|
|
|
class ClientProtocolFixture
|
2020-02-19 21:44:56 +00:00
|
|
|
{
|
2020-02-24 17:25:25 +00:00
|
|
|
ClientProtocolFixture(ClientProtocolFixture const &) = delete;
|
|
|
|
ClientProtocolFixture& operator=(ClientProtocolFixture const &) = delete;
|
2020-02-22 15:49:56 +00:00
|
|
|
public:
|
2020-02-24 17:25:25 +00:00
|
|
|
explicit ClientProtocolFixture(IProviderClient& client)
|
2020-02-19 21:44:56 +00:00
|
|
|
{
|
2020-02-20 19:13:39 +00:00
|
|
|
config = wfp_client_config_create();
|
2020-02-24 17:16:30 +00:00
|
|
|
client.AttachTo(config);
|
2020-02-23 22:20:26 +00:00
|
|
|
|
2020-02-20 19:13:39 +00:00
|
|
|
protocol = wfp_client_protocol_create(config);
|
|
|
|
|
2020-02-23 20:02:01 +00:00
|
|
|
struct lws_protocols client_protocol;
|
|
|
|
memset(&client_protocol, 0, sizeof(struct lws_protocols));
|
|
|
|
wfp_client_protocol_init_lws(protocol, &client_protocol);
|
2020-02-20 19:13:39 +00:00
|
|
|
|
2020-02-23 20:02:01 +00:00
|
|
|
server = new WebsocketServer(54321, &client_protocol, 1);
|
2020-02-19 21:44:56 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 17:16:30 +00:00
|
|
|
~ClientProtocolFixture()
|
2020-02-20 19:13:39 +00:00
|
|
|
{
|
2020-02-23 20:02:01 +00:00
|
|
|
delete server;
|
2020-02-20 19:13:39 +00:00
|
|
|
wfp_client_protocol_dispose(protocol);
|
|
|
|
wfp_client_config_dispose(config);
|
2020-02-24 17:16:30 +00:00
|
|
|
}
|
2020-02-19 21:44:56 +00:00
|
|
|
|
2020-02-25 14:36:28 +00:00
|
|
|
void SetUsernameCredentials(
|
|
|
|
std::string const & username,
|
|
|
|
std::string const & password)
|
|
|
|
{
|
|
|
|
wfp_client_config_set_username_credentials(config, username.c_str(), password.c_str());
|
|
|
|
}
|
|
|
|
|
2020-02-24 17:16:30 +00:00
|
|
|
void Connect()
|
|
|
|
{
|
|
|
|
wfp_client_protocol_connect(protocol, server->getContext(), "ws://localhost:54321/");
|
|
|
|
server->waitForConnection();
|
2020-02-23 20:02:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 17:16:30 +00:00
|
|
|
void SendToClient(json_t * request)
|
2020-02-23 22:20:26 +00:00
|
|
|
{
|
2020-02-24 17:16:30 +00:00
|
|
|
server->sendMessage(request);
|
2020-02-23 22:20:26 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 17:16:30 +00:00
|
|
|
json_t * ReceiveMessageFromClient()
|
2020-02-23 20:02:01 +00:00
|
|
|
{
|
2020-02-24 17:16:30 +00:00
|
|
|
return server->receiveMessage();
|
2020-02-20 19:13:39 +00:00
|
|
|
}
|
2020-02-19 21:44:56 +00:00
|
|
|
|
2020-02-25 14:36:28 +00:00
|
|
|
void AwaitAuthentication(
|
|
|
|
std::string const & expected_username,
|
|
|
|
std::string const & expected_password)
|
|
|
|
{
|
|
|
|
json_t * request = server->receiveMessage();
|
|
|
|
ASSERT_TRUE(json_is_object(request));
|
|
|
|
|
|
|
|
json_t * method = json_object_get(request, "method");
|
|
|
|
ASSERT_TRUE(json_is_string(method));
|
|
|
|
ASSERT_STREQ("authenticate", json_string_value(method));
|
|
|
|
|
|
|
|
json_t * id = json_object_get(request, "id");
|
|
|
|
ASSERT_TRUE(json_is_integer(id));
|
|
|
|
|
|
|
|
json_t * params = json_object_get(request, "params");
|
|
|
|
ASSERT_TRUE(json_is_array(params));
|
|
|
|
ASSERT_EQ(2, json_array_size(params));
|
|
|
|
|
|
|
|
json_t * type = json_array_get(params, 0);
|
|
|
|
ASSERT_TRUE(json_is_string(type));
|
|
|
|
ASSERT_STREQ("username", json_string_value(type));
|
|
|
|
|
|
|
|
json_t * credentials = json_array_get(params, 1);
|
|
|
|
ASSERT_TRUE(json_is_object(credentials));
|
|
|
|
|
|
|
|
json_t * username = json_object_get(credentials, "username");
|
|
|
|
ASSERT_TRUE(json_is_string(username));
|
|
|
|
ASSERT_STREQ(expected_username.c_str(), json_string_value(username));
|
|
|
|
|
|
|
|
json_t * password = json_object_get(credentials, "password");
|
|
|
|
ASSERT_TRUE(json_is_string(password));
|
|
|
|
ASSERT_STREQ(expected_password.c_str(), json_string_value(password));
|
|
|
|
|
|
|
|
json_t * response = json_object();
|
|
|
|
json_object_set_new(response, "result", json_object());
|
|
|
|
json_object_set(response, "id", id);
|
|
|
|
server->sendMessage(response);
|
|
|
|
|
|
|
|
json_decref(request);
|
|
|
|
}
|
|
|
|
|
2020-02-23 22:20:26 +00:00
|
|
|
void AwaitAddFilesystem(std::string& filesystemName)
|
2020-02-22 15:46:21 +00:00
|
|
|
{
|
|
|
|
json_t * addFilesystemRequest = server->receiveMessage();
|
|
|
|
ASSERT_NE(nullptr, addFilesystemRequest);
|
|
|
|
ASSERT_TRUE(json_is_object(addFilesystemRequest));
|
|
|
|
|
|
|
|
json_t * method = json_object_get(addFilesystemRequest, "method");
|
|
|
|
ASSERT_TRUE(json_is_string(method));
|
|
|
|
ASSERT_STREQ("add_filesystem", json_string_value(method));
|
|
|
|
|
|
|
|
json_t * params = json_object_get(addFilesystemRequest, "params");
|
|
|
|
ASSERT_TRUE(json_is_array(params));
|
|
|
|
ASSERT_EQ(1, json_array_size(params));
|
|
|
|
|
|
|
|
json_t * filesystem = json_array_get(params, 0);
|
|
|
|
ASSERT_TRUE(json_is_string(filesystem));
|
|
|
|
filesystemName = json_string_value(filesystem);
|
|
|
|
|
|
|
|
json_t * id = json_object_get(addFilesystemRequest, "id");
|
|
|
|
ASSERT_TRUE(json_is_integer(id));
|
|
|
|
|
|
|
|
json_t * response = json_object();
|
|
|
|
json_t * result = json_object();
|
|
|
|
json_object_set(result, "id", filesystem);
|
|
|
|
json_object_set_new(response, "result", result);
|
|
|
|
json_object_set(response, "id", id);
|
|
|
|
|
|
|
|
server->sendMessage(response);
|
|
|
|
|
|
|
|
json_decref(addFilesystemRequest);
|
|
|
|
}
|
|
|
|
|
2020-02-20 19:13:39 +00:00
|
|
|
private:
|
2020-02-24 17:16:30 +00:00
|
|
|
WebsocketServer * server;
|
2020-02-20 19:13:39 +00:00
|
|
|
wfp_client_config * config;
|
|
|
|
wfp_client_protocol * protocol;
|
|
|
|
};
|
2020-02-19 21:44:56 +00:00
|
|
|
|
2020-02-20 19:13:39 +00:00
|
|
|
}
|
2020-02-19 21:44:56 +00:00
|
|
|
|
2020-02-24 17:16:30 +00:00
|
|
|
TEST(client_protocol, connect)
|
2020-02-20 19:13:39 +00:00
|
|
|
{
|
2020-02-24 17:16:30 +00:00
|
|
|
MockProviderClient provider;
|
|
|
|
ClientProtocolFixture fixture(provider);
|
|
|
|
|
|
|
|
EXPECT_CALL(provider, OnConnected()).Times(AtMost(1));
|
|
|
|
EXPECT_CALL(provider, OnDisconnected()).Times(1);
|
|
|
|
|
|
|
|
fixture.Connect();
|
2020-02-23 20:02:01 +00:00
|
|
|
if (HasFatalFailure()) { return; }
|
2020-02-23 22:20:26 +00:00
|
|
|
|
2020-02-24 17:16:30 +00:00
|
|
|
std::string filesystem;
|
|
|
|
fixture.AwaitAddFilesystem(filesystem);
|
|
|
|
if (HasFatalFailure()) { return; }
|
2020-02-22 15:46:21 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 14:36:28 +00:00
|
|
|
TEST(client_protocol, connect_with_username_authentication)
|
|
|
|
{
|
|
|
|
MockProviderClient provider;
|
|
|
|
ClientProtocolFixture fixture(provider);
|
|
|
|
fixture.SetUsernameCredentials("bob", "secret");
|
|
|
|
|
|
|
|
EXPECT_CALL(provider, OnConnected()).Times(AtMost(1));
|
|
|
|
EXPECT_CALL(provider, OnDisconnected()).Times(1);
|
|
|
|
|
|
|
|
fixture.Connect();
|
|
|
|
if (HasFatalFailure()) { return; }
|
|
|
|
|
|
|
|
fixture.AwaitAuthentication("bob", "secret");
|
|
|
|
if (HasFatalFailure()) { return; }
|
|
|
|
|
|
|
|
std::string filesystem;
|
|
|
|
fixture.AwaitAddFilesystem(filesystem);
|
|
|
|
if (HasFatalFailure()) { return; }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-24 17:16:30 +00:00
|
|
|
TEST(client_protocol, getattr)
|
2020-02-22 15:46:21 +00:00
|
|
|
{
|
2020-02-24 17:16:30 +00:00
|
|
|
MockProviderClient provider;
|
|
|
|
ClientProtocolFixture fixture(provider);
|
2020-02-23 22:20:26 +00:00
|
|
|
|
2020-02-24 17:16:30 +00:00
|
|
|
EXPECT_CALL(provider, OnConnected()).Times(1);
|
|
|
|
EXPECT_CALL(provider, OnDisconnected()).Times(1);
|
|
|
|
EXPECT_CALL(provider, GetAttr(1, _)).Times(1);
|
2020-02-23 22:20:26 +00:00
|
|
|
|
2020-02-24 17:16:30 +00:00
|
|
|
fixture.Connect();
|
2020-02-23 20:02:01 +00:00
|
|
|
if (HasFatalFailure()) { return; }
|
2020-02-22 15:46:21 +00:00
|
|
|
|
|
|
|
std::string filesystem;
|
2020-02-24 17:16:30 +00:00
|
|
|
fixture.AwaitAddFilesystem(filesystem);
|
2020-02-22 15:46:21 +00:00
|
|
|
if (HasFatalFailure()) { return; }
|
|
|
|
|
|
|
|
json_t * params = json_array();
|
|
|
|
json_array_append_new(params, json_string(filesystem.c_str()));
|
|
|
|
json_array_append_new(params, json_integer(1));
|
|
|
|
json_t * request = json_object();
|
|
|
|
json_object_set_new(request, "method", json_string("getattr"));
|
|
|
|
json_object_set_new(request, "params", params);
|
|
|
|
json_object_set_new(request, "id", json_integer(42));
|
|
|
|
|
2020-02-24 17:16:30 +00:00
|
|
|
fixture.SendToClient(request);
|
|
|
|
json_t * response = fixture.ReceiveMessageFromClient();
|
2020-02-22 15:46:21 +00:00
|
|
|
ASSERT_TRUE(json_is_object(response));
|
|
|
|
|
|
|
|
json_decref(response);
|
2020-02-19 21:44:56 +00:00
|
|
|
}
|