2020-06-09 20:41:38 +00:00
|
|
|
#include <gtest/gtest.h>
|
2020-06-12 17:00:28 +00:00
|
|
|
#include <gmock/gmock.h>
|
2020-06-09 20:41:38 +00:00
|
|
|
|
|
|
|
#include "webfuse/adapter/client.h"
|
2020-06-10 20:42:26 +00:00
|
|
|
#include "webfuse/adapter/credentials.h"
|
2020-06-12 14:48:15 +00:00
|
|
|
#include "webfuse/core/protocol_names.h"
|
2020-06-12 14:53:36 +00:00
|
|
|
#include "webfuse/utils/ws_server.h"
|
2020-06-12 17:00:28 +00:00
|
|
|
#include "webfuse/mocks/mock_adapter_client_callback.hpp"
|
2020-06-11 20:57:56 +00:00
|
|
|
|
2020-06-12 14:53:36 +00:00
|
|
|
using webfuse_test::WsServer;
|
2020-06-12 17:00:28 +00:00
|
|
|
using webfuse_test::MockAdapterClientCallback;
|
|
|
|
using testing::_;
|
2020-06-09 20:41:38 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
enum class connection_state
|
|
|
|
{
|
|
|
|
disconnected,
|
|
|
|
connected,
|
|
|
|
connecting
|
|
|
|
};
|
|
|
|
|
|
|
|
struct context
|
|
|
|
{
|
|
|
|
connection_state state;
|
|
|
|
};
|
|
|
|
|
|
|
|
void callback(
|
|
|
|
wf_client * client,
|
|
|
|
int reason,
|
|
|
|
void * args)
|
|
|
|
{
|
|
|
|
auto * ctx = reinterpret_cast<context*>(wf_client_get_userdata(client));
|
|
|
|
|
|
|
|
switch (reason)
|
|
|
|
{
|
|
|
|
case WF_CLIENT_CREATED:
|
|
|
|
ctx->state = connection_state::connecting;
|
|
|
|
wf_client_connect(client, "ws://dummy-server/");
|
|
|
|
break;
|
|
|
|
case WF_CLIENT_CONNECTED:
|
|
|
|
ctx->state = connection_state::connected;
|
|
|
|
wf_client_authenticate(client);
|
|
|
|
break;
|
|
|
|
case WF_CLIENT_AUTHENTICATED:
|
|
|
|
wf_client_add_filesystem(client, ".", "test");
|
|
|
|
break;
|
|
|
|
case WF_CLIENT_AUTHENTICATION_FAILED:
|
|
|
|
wf_client_disconnect(client);
|
|
|
|
break;
|
|
|
|
case WF_CLIENT_AUTHENTICATE_GET_CREDENTIALS:
|
|
|
|
{
|
2020-06-10 20:42:26 +00:00
|
|
|
auto * credentials = reinterpret_cast<wf_credentials*>(args);
|
|
|
|
wf_credentials_set_type(credentials, "username");
|
|
|
|
wf_credentials_add(credentials, "user", "bob");
|
|
|
|
wf_credentials_add(credentials, "password", "secret");
|
2020-06-09 20:41:38 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WF_CLIENT_FILESYSTEM_ADDED:
|
|
|
|
// operational
|
|
|
|
break;
|
|
|
|
case WF_CLIENT_FILESYSTEM_ADD_FAILED:
|
|
|
|
wf_client_disconnect(client);
|
|
|
|
break;
|
|
|
|
case WF_CLIENT_DISCONNECTED:
|
|
|
|
ctx->state = connection_state::disconnected;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-12 17:00:28 +00:00
|
|
|
TEST(AdapterClient, GeneralUsage)
|
2020-06-09 20:41:38 +00:00
|
|
|
{
|
|
|
|
context ctx;
|
|
|
|
ctx.state = connection_state::connecting;
|
|
|
|
|
|
|
|
wf_client * client = wf_client_create(
|
|
|
|
&callback, reinterpret_cast<void*>(&ctx));
|
|
|
|
|
2020-06-11 20:57:56 +00:00
|
|
|
while (ctx.state != connection_state::disconnected)
|
|
|
|
{
|
|
|
|
wf_client_service(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
wf_client_dispose(client);
|
|
|
|
}
|
|
|
|
|
2020-06-12 17:00:28 +00:00
|
|
|
TEST(AdapterClient, CreateAndDispose)
|
|
|
|
{
|
|
|
|
MockAdapterClientCallback callback;
|
|
|
|
|
|
|
|
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_INIT, nullptr)).Times(1);
|
|
|
|
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CREATED, nullptr)).Times(1);
|
|
|
|
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_GET_TLS_CONFIG, _)).Times(1);
|
|
|
|
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CLEANUP, nullptr)).Times(1);
|
|
|
|
|
|
|
|
wf_client * client = wf_client_create(
|
|
|
|
callback.GetCallbackFn(), callback.GetUserData());
|
|
|
|
|
|
|
|
wf_client_dispose(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AdapterClient, Connect)
|
2020-06-11 20:57:56 +00:00
|
|
|
{
|
2020-06-12 14:53:36 +00:00
|
|
|
WsServer server(WF_PROTOCOL_NAME_PROVIDER_SERVER);
|
2020-06-12 17:00:28 +00:00
|
|
|
MockAdapterClientCallback callback;
|
|
|
|
|
|
|
|
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_INIT, nullptr)).Times(1);
|
|
|
|
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CREATED, nullptr)).Times(1);
|
|
|
|
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_GET_TLS_CONFIG, _)).Times(1);
|
|
|
|
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CLEANUP, nullptr)).Times(1);
|
|
|
|
|
|
|
|
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_CONNECTED, nullptr)).Times(1);
|
|
|
|
EXPECT_CALL(callback, Invoke(_, WF_CLIENT_DISCONNECTED, nullptr)).Times(1);
|
2020-06-11 20:57:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
wf_client * client = wf_client_create(
|
2020-06-12 17:00:28 +00:00
|
|
|
callback.GetCallbackFn(), callback.GetUserData());
|
2020-06-11 20:57:56 +00:00
|
|
|
|
2020-06-12 08:17:17 +00:00
|
|
|
wf_client_connect(client, server.GetUrl().c_str());
|
2020-06-12 17:00:28 +00:00
|
|
|
while (!server.IsConnected())
|
2020-06-09 20:41:38 +00:00
|
|
|
{
|
2020-06-11 20:57:56 +00:00
|
|
|
wf_client_service(client);
|
|
|
|
}
|
2020-06-09 20:41:38 +00:00
|
|
|
|
2020-06-11 20:57:56 +00:00
|
|
|
wf_client_disconnect(client);
|
2020-06-12 17:00:28 +00:00
|
|
|
while (server.IsConnected())
|
2020-06-11 20:57:56 +00:00
|
|
|
{
|
|
|
|
wf_client_service(client);
|
2020-06-09 20:41:38 +00:00
|
|
|
}
|
2020-06-11 20:57:56 +00:00
|
|
|
|
|
|
|
wf_client_dispose(client);
|
2020-06-09 20:41:38 +00:00
|
|
|
}
|