2020-02-19 21:44:56 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <gmock/gmock.h>
|
|
|
|
|
2020-06-16 21:39:45 +00:00
|
|
|
#include <webfuse_provider/client_protocol.h>
|
|
|
|
#include <webfuse_provider/client_config.h>
|
2020-06-21 19:18:43 +00:00
|
|
|
#include "webfuse_provider/test_util/ws_server.h"
|
2020-06-16 21:39:45 +00:00
|
|
|
#include "webfuse_provider/mocks/mock_provider_client.hpp"
|
|
|
|
#include "webfuse_provider/protocol_names.h"
|
2020-06-21 19:18:43 +00:00
|
|
|
#include "webfuse_provider/test_util/timeout_watcher.hpp"
|
2020-07-12 13:13:34 +00:00
|
|
|
#include "webfuse_provider/test_util/json_doc.hpp"
|
2020-07-12 09:09:50 +00:00
|
|
|
#include "webfuse_provider/impl/json/node.h"
|
2020-02-19 21:44:56 +00:00
|
|
|
|
2020-06-13 09:35:39 +00:00
|
|
|
#include <libwebsockets.h>
|
|
|
|
|
2020-02-19 21:44:56 +00:00
|
|
|
#include <cstring>
|
2020-07-12 09:09:50 +00:00
|
|
|
#include <sstream>
|
2020-02-19 21:44:56 +00:00
|
|
|
#include <thread>
|
|
|
|
|
2020-06-12 14:53:36 +00:00
|
|
|
using webfuse_test::WsServer;
|
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-06-12 14:48:15 +00:00
|
|
|
using webfuse_test::TimeoutWatcher;
|
2020-07-12 13:13:34 +00:00
|
|
|
using webfuse_test::JsonDoc;
|
2020-02-19 21:44:56 +00:00
|
|
|
using testing::_;
|
2020-02-24 17:16:30 +00:00
|
|
|
using testing::AtMost;
|
2020-02-28 22:17:41 +00:00
|
|
|
using testing::Invoke;
|
2020-02-19 21:44:56 +00:00
|
|
|
|
2020-06-12 14:48:15 +00:00
|
|
|
#define DEFAULT_TIMEOUT (std::chrono::milliseconds(5 * 1000))
|
|
|
|
|
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-28 22:17:41 +00:00
|
|
|
explicit ClientProtocolFixture(IProviderClient& client, bool enableAuthentication = false)
|
2020-02-19 21:44:56 +00:00
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
server = new WsServer(WFP_PROTOCOL_NAME_ADAPTER_SERVER);
|
2020-06-12 14:48:15 +00:00
|
|
|
|
2020-02-20 19:13:39 +00:00
|
|
|
config = wfp_client_config_create();
|
2020-02-28 22:17:41 +00:00
|
|
|
client.AttachTo(config, enableAuthentication);
|
2020-02-20 19:13:39 +00:00
|
|
|
protocol = wfp_client_protocol_create(config);
|
|
|
|
|
2020-06-12 14:48:15 +00:00
|
|
|
memset(protocols, 0, sizeof(struct lws_protocols) * 2);
|
|
|
|
wfp_client_protocol_init_lws(protocol, protocols);
|
|
|
|
|
|
|
|
memset(&info, 0, sizeof(struct lws_context_creation_info));
|
|
|
|
info.port = CONTEXT_PORT_NO_LISTEN;
|
|
|
|
info.protocols = protocols;
|
|
|
|
info.uid = -1;
|
|
|
|
info.gid = -1;
|
2020-02-20 19:13:39 +00:00
|
|
|
|
2020-06-12 14:48:15 +00:00
|
|
|
context = lws_create_context(&info);
|
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-06-12 14:48:15 +00:00
|
|
|
lws_context_destroy(context);
|
2020-02-20 19:13:39 +00:00
|
|
|
wfp_client_protocol_dispose(protocol);
|
|
|
|
wfp_client_config_dispose(config);
|
2020-06-12 14:48:15 +00:00
|
|
|
delete server;
|
2020-02-24 17:16:30 +00:00
|
|
|
}
|
2020-02-19 21:44:56 +00:00
|
|
|
|
2020-02-24 17:16:30 +00:00
|
|
|
void Connect()
|
|
|
|
{
|
2020-06-12 14:48:15 +00:00
|
|
|
TimeoutWatcher watcher(DEFAULT_TIMEOUT);
|
|
|
|
|
|
|
|
wfp_client_protocol_connect(protocol, context, server->GetUrl().c_str());
|
|
|
|
while (!server->IsConnected())
|
|
|
|
{
|
|
|
|
watcher.check();
|
|
|
|
lws_service(context, 0);
|
|
|
|
}
|
2020-02-23 20:02:01 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:37:50 +00:00
|
|
|
void Disconnect()
|
|
|
|
{
|
|
|
|
wfp_client_protocol_disconnect(protocol);
|
|
|
|
}
|
|
|
|
|
2020-07-12 09:09:50 +00:00
|
|
|
void SendToClient(std::string const & request)
|
2020-02-23 22:20:26 +00:00
|
|
|
{
|
2020-06-12 14:48:15 +00:00
|
|
|
server->SendMessage(request);
|
2020-02-23 22:20:26 +00:00
|
|
|
}
|
|
|
|
|
2020-07-12 09:09:50 +00:00
|
|
|
std::string ReceiveMessageFromClient()
|
2020-02-23 20:02:01 +00:00
|
|
|
{
|
2020-06-12 14:48:15 +00:00
|
|
|
TimeoutWatcher watcher(DEFAULT_TIMEOUT);
|
2020-07-12 09:09:50 +00:00
|
|
|
std::string result = server->ReceiveMessage();
|
|
|
|
while (result.empty())
|
2020-06-12 14:48:15 +00:00
|
|
|
{
|
|
|
|
watcher.check();
|
|
|
|
lws_service(context, 0);
|
|
|
|
result = server->ReceiveMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
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)
|
|
|
|
{
|
2020-07-12 13:13:34 +00:00
|
|
|
JsonDoc doc(ReceiveMessageFromClient());
|
|
|
|
wfp_json const * request = doc.root();
|
2020-07-12 09:09:50 +00:00
|
|
|
ASSERT_TRUE(wfp_impl_json_is_object(request));
|
2020-02-25 14:36:28 +00:00
|
|
|
|
2020-07-12 09:09:50 +00:00
|
|
|
wfp_json const * method = wfp_impl_json_object_get(request, "method");
|
|
|
|
ASSERT_TRUE(wfp_impl_json_is_string(method));
|
2020-07-12 13:34:50 +00:00
|
|
|
ASSERT_STREQ("authenticate", wfp_impl_json_string_get(method));
|
2020-02-25 14:36:28 +00:00
|
|
|
|
2020-07-12 09:09:50 +00:00
|
|
|
wfp_json const * id = wfp_impl_json_object_get(request, "id");
|
|
|
|
ASSERT_TRUE(wfp_impl_json_is_int(id));
|
2020-02-25 14:36:28 +00:00
|
|
|
|
2020-07-12 09:09:50 +00:00
|
|
|
wfp_json const * params = wfp_impl_json_object_get(request, "params");
|
|
|
|
ASSERT_TRUE(wfp_impl_json_is_array(params));
|
|
|
|
ASSERT_EQ(2, wfp_impl_json_array_size(params));
|
2020-02-25 14:36:28 +00:00
|
|
|
|
2020-07-12 09:09:50 +00:00
|
|
|
wfp_json const * type = wfp_impl_json_array_get(params, 0);
|
|
|
|
ASSERT_TRUE(wfp_impl_json_is_string(type));
|
2020-07-12 13:34:50 +00:00
|
|
|
ASSERT_STREQ("username", wfp_impl_json_string_get(type));
|
2020-02-25 14:36:28 +00:00
|
|
|
|
2020-07-12 09:09:50 +00:00
|
|
|
wfp_json const * credentials = wfp_impl_json_array_get(params, 1);
|
|
|
|
ASSERT_TRUE(wfp_impl_json_is_object(credentials));
|
2020-02-25 14:36:28 +00:00
|
|
|
|
2020-07-12 09:09:50 +00:00
|
|
|
wfp_json const * username = wfp_impl_json_object_get(credentials, "username");
|
|
|
|
ASSERT_TRUE(wfp_impl_json_is_string(username));
|
2020-07-12 13:34:50 +00:00
|
|
|
ASSERT_STREQ(expected_username.c_str(), wfp_impl_json_string_get(username));
|
2020-02-25 14:36:28 +00:00
|
|
|
|
2020-07-12 09:09:50 +00:00
|
|
|
wfp_json const * password = wfp_impl_json_object_get(credentials, "password");
|
|
|
|
ASSERT_TRUE(wfp_impl_json_is_string(password));
|
2020-07-12 13:34:50 +00:00
|
|
|
ASSERT_STREQ(expected_password.c_str(), wfp_impl_json_string_get(password));
|
2020-02-25 14:36:28 +00:00
|
|
|
|
2020-07-12 09:09:50 +00:00
|
|
|
std::ostringstream response;
|
2020-07-12 13:34:50 +00:00
|
|
|
response << "{\"result\": {}, \"id\": " << wfp_impl_json_int_get(id) << "}";
|
2020-07-12 09:09:50 +00:00
|
|
|
SendToClient(response.str());
|
2020-02-25 14:36:28 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 22:20:26 +00:00
|
|
|
void AwaitAddFilesystem(std::string& filesystemName)
|
2020-02-22 15:46:21 +00:00
|
|
|
{
|
2020-07-12 13:13:34 +00:00
|
|
|
JsonDoc doc(ReceiveMessageFromClient());
|
|
|
|
wfp_json const * request = doc.root();
|
2020-07-12 09:09:50 +00:00
|
|
|
ASSERT_TRUE(wfp_impl_json_is_object(request));
|
2020-02-22 15:46:21 +00:00
|
|
|
|
2020-07-12 09:09:50 +00:00
|
|
|
wfp_json const * method = wfp_impl_json_object_get(request, "method");
|
|
|
|
ASSERT_TRUE(wfp_impl_json_is_string(method));
|
2020-07-12 13:34:50 +00:00
|
|
|
ASSERT_STREQ("add_filesystem", wfp_impl_json_string_get(method));
|
2020-02-22 15:46:21 +00:00
|
|
|
|
2020-07-12 09:09:50 +00:00
|
|
|
wfp_json const * params = wfp_impl_json_object_get(request, "params");
|
|
|
|
ASSERT_TRUE(wfp_impl_json_is_array(params));
|
|
|
|
ASSERT_EQ(1, wfp_impl_json_array_size(params));
|
2020-02-22 15:46:21 +00:00
|
|
|
|
2020-07-12 09:09:50 +00:00
|
|
|
wfp_json const * filesystem = wfp_impl_json_array_get(params, 0);
|
|
|
|
ASSERT_TRUE(wfp_impl_json_is_string(filesystem));
|
2020-02-22 15:46:21 +00:00
|
|
|
|
2020-07-12 09:09:50 +00:00
|
|
|
wfp_json const * id = wfp_impl_json_object_get(request, "id");
|
|
|
|
ASSERT_TRUE(wfp_impl_json_is_int(id));
|
2020-02-22 15:46:21 +00:00
|
|
|
|
2020-07-12 09:09:50 +00:00
|
|
|
std::ostringstream response;
|
2020-07-12 13:34:50 +00:00
|
|
|
response << "{\"result\": {\"id\": \"" << wfp_impl_json_string_get(filesystem) << "\"}, \"id\": " << wfp_impl_json_int_get(id) << "}";
|
2020-02-22 15:46:21 +00:00
|
|
|
|
2020-07-12 09:09:50 +00:00
|
|
|
SendToClient(response.str());
|
2020-02-22 15:46:21 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 19:13:39 +00:00
|
|
|
private:
|
2020-06-12 14:53:36 +00:00
|
|
|
WsServer * server;
|
2020-02-20 19:13:39 +00:00
|
|
|
wfp_client_config * config;
|
|
|
|
wfp_client_protocol * protocol;
|
2020-06-12 14:48:15 +00:00
|
|
|
struct lws_context_creation_info info;
|
|
|
|
struct lws_protocols protocols[2];
|
|
|
|
struct lws_context * context;
|
|
|
|
|
2020-02-20 19:13:39 +00:00
|
|
|
};
|
2020-02-19 21:44:56 +00:00
|
|
|
|
2020-02-28 22:17:41 +00:00
|
|
|
void GetCredentials(wfp_credentials * credentials)
|
|
|
|
{
|
|
|
|
wfp_credentials_set_type(credentials, "username");
|
|
|
|
wfp_credentials_add(credentials, "username", "bob");
|
|
|
|
wfp_credentials_add(credentials, "password", "secret");
|
|
|
|
}
|
|
|
|
|
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-04-07 18:37:50 +00:00
|
|
|
TEST(client_protocol, disconnect_without_connect)
|
|
|
|
{
|
|
|
|
MockProviderClient provider;
|
|
|
|
ClientProtocolFixture fixture(provider);
|
|
|
|
|
|
|
|
EXPECT_CALL(provider, OnDisconnected()).Times(1);
|
|
|
|
|
|
|
|
fixture.Disconnect();
|
|
|
|
}
|
|
|
|
|
2020-02-25 14:36:28 +00:00
|
|
|
TEST(client_protocol, connect_with_username_authentication)
|
|
|
|
{
|
|
|
|
MockProviderClient provider;
|
2020-02-28 22:17:41 +00:00
|
|
|
ClientProtocolFixture fixture(provider, true);
|
2020-02-25 14:36:28 +00:00
|
|
|
|
|
|
|
EXPECT_CALL(provider, OnConnected()).Times(AtMost(1));
|
|
|
|
EXPECT_CALL(provider, OnDisconnected()).Times(1);
|
2020-03-01 12:42:46 +00:00
|
|
|
EXPECT_CALL(provider, GetCredentials(_)).Times(1).WillOnce(Invoke(GetCredentials));
|
2020-02-25 14:36:28 +00:00
|
|
|
|
|
|
|
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; }
|
|
|
|
|
2020-07-12 09:09:50 +00:00
|
|
|
std::ostringstream request;
|
|
|
|
request << "{\"method\": \"getattr\", \"params\": [\"" << filesystem << "\", 1], \"id\": 42}";
|
2020-02-22 15:46:21 +00:00
|
|
|
|
2020-07-12 09:09:50 +00:00
|
|
|
fixture.SendToClient(request.str());
|
|
|
|
std::string response = fixture.ReceiveMessageFromClient();
|
|
|
|
ASSERT_FALSE(response.empty());
|
2020-04-07 18:37:50 +00:00
|
|
|
|
|
|
|
fixture.Disconnect();
|
2020-02-19 21:44:56 +00:00
|
|
|
}
|