mirror of
https://github.com/falk-werner/webfuse-provider
synced 2026-03-02 04:09:18 +00:00
organized unit tests
This commit is contained in:
72
test/webfuse/tests/provider/test_client_protocol.cc
Normal file
72
test/webfuse/tests/provider/test_client_protocol.cc
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
#include <webfuse/provider/client_protocol.h>
|
||||
#include <webfuse/provider/client_config.h>
|
||||
#include "webfuse/fakes/fake_adapter_server.hpp"
|
||||
|
||||
#include <cstring>
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
|
||||
using webfuse_test::FakeAdapterServer;
|
||||
using testing::_;
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct Context
|
||||
{
|
||||
lws_context * context;
|
||||
std::atomic<bool> isShutdownRequested;
|
||||
};
|
||||
|
||||
void run(Context * context)
|
||||
{
|
||||
while (!context->isShutdownRequested)
|
||||
{
|
||||
lws_service(context->context, 100);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
TEST(client_protocol, connect)
|
||||
{
|
||||
FakeAdapterServer server(54321);
|
||||
|
||||
wfp_client_config * config = wfp_client_config_create();
|
||||
wfp_client_protocol * protocol = wfp_client_protocol_create(config);
|
||||
|
||||
struct lws_protocols protocols[2];
|
||||
memset(protocols, 0, sizeof(struct lws_protocols) * 2);
|
||||
protocols[0].name = "fs";
|
||||
wfp_client_protocol_init_lws(protocol, &protocols[0]);
|
||||
|
||||
struct lws_context_creation_info info;
|
||||
memset(&info, 0, sizeof(struct lws_context_creation_info));
|
||||
info.port = CONTEXT_PORT_NO_LISTEN;
|
||||
info.protocols = protocols;
|
||||
info.uid = -1;
|
||||
info.gid = -1;
|
||||
|
||||
struct lws_context * context = lws_create_context(&info);
|
||||
wfp_client_protocol_connect(protocol, context, "ws://localhost:54321/");
|
||||
|
||||
Context ctx;
|
||||
ctx.context = context;
|
||||
ctx.isShutdownRequested = false;
|
||||
std::thread client_thread(run, &ctx);
|
||||
|
||||
server.waitForConnection();
|
||||
|
||||
ctx.isShutdownRequested = true;
|
||||
client_thread.join();
|
||||
|
||||
lws_context_destroy(context);
|
||||
|
||||
wfp_client_protocol_dispose(protocol);
|
||||
wfp_client_config_dispose(config);
|
||||
}
|
||||
61
test/webfuse/tests/provider/test_static_filesystem.cc
Normal file
61
test/webfuse/tests/provider/test_static_filesystem.cc
Normal file
@@ -0,0 +1,61 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "webfuse/provider/impl/static_filesystem.h"
|
||||
#include "webfuse/provider/client_config.h"
|
||||
#include "webfuse/provider/impl/client_config.h"
|
||||
|
||||
#include "webfuse/mocks/mock_request.hpp"
|
||||
|
||||
using webfuse_test::request_create;
|
||||
using webfuse_test::MockRequest;
|
||||
using webfuse_test::GetAttrMatcher;
|
||||
using webfuse_test::ReaddirMatcher;
|
||||
using testing::_;
|
||||
|
||||
TEST(wfp_static_filesystem, has_root_dir)
|
||||
{
|
||||
struct wfp_client_config * config = wfp_client_config_create();
|
||||
struct wfp_static_filesystem * filesystem = wfp_impl_static_filesystem_create(config);
|
||||
|
||||
MockRequest mock;
|
||||
struct wfp_request * request = request_create(&mock, 42);
|
||||
EXPECT_CALL(mock, respond(GetAttrMatcher(1, 0555, "dir"), 42)).Times(1);
|
||||
|
||||
config->provider.getattr(request, 1, config->user_data);
|
||||
|
||||
wfp_impl_static_filesystem_dispose(filesystem);
|
||||
wfp_client_config_dispose(config);
|
||||
}
|
||||
|
||||
TEST(wfp_static_filesystem, contains_default_dirs)
|
||||
{
|
||||
struct wfp_client_config * config = wfp_client_config_create();
|
||||
struct wfp_static_filesystem * filesystem = wfp_impl_static_filesystem_create(config);
|
||||
|
||||
MockRequest mock;
|
||||
struct wfp_request * request = request_create(&mock, 23);
|
||||
char const * default_dirs[] = {".", "..", nullptr};
|
||||
EXPECT_CALL(mock, respond(ReaddirMatcher(default_dirs), 23)).Times(1);
|
||||
|
||||
config->provider.readdir(request, 1, config->user_data);
|
||||
|
||||
wfp_impl_static_filesystem_dispose(filesystem);
|
||||
wfp_client_config_dispose(config);
|
||||
}
|
||||
|
||||
TEST(wfp_static_filesystem, add_text)
|
||||
{
|
||||
struct wfp_client_config * config = wfp_client_config_create();
|
||||
struct wfp_static_filesystem * filesystem = wfp_impl_static_filesystem_create(config);
|
||||
wfp_impl_static_filesystem_add_text(filesystem, "text.file", 666, "some text");
|
||||
|
||||
MockRequest mock;
|
||||
struct wfp_request * request = request_create(&mock, 23);
|
||||
char const * contained_elements[] = {"text.file", nullptr};
|
||||
EXPECT_CALL(mock, respond(ReaddirMatcher(contained_elements), 23)).Times(1);
|
||||
|
||||
config->provider.readdir(request, 1, config->user_data);
|
||||
|
||||
wfp_impl_static_filesystem_dispose(filesystem);
|
||||
wfp_client_config_dispose(config);
|
||||
}
|
||||
92
test/webfuse/tests/provider/test_url.cc
Normal file
92
test/webfuse/tests/provider/test_url.cc
Normal file
@@ -0,0 +1,92 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "webfuse/provider/impl/url.h"
|
||||
|
||||
TEST(url, ParseWs)
|
||||
{
|
||||
struct wfp_impl_url url;
|
||||
bool result = wfp_impl_url_init(&url, "ws://localhost/");
|
||||
ASSERT_TRUE(result);
|
||||
ASSERT_EQ(80, url.port);
|
||||
ASSERT_FALSE(url.use_tls);
|
||||
ASSERT_STREQ("localhost", url.host);
|
||||
ASSERT_STREQ("/", url.path);
|
||||
|
||||
wfp_impl_url_cleanup(&url);
|
||||
}
|
||||
|
||||
TEST(url, ParswWss)
|
||||
{
|
||||
struct wfp_impl_url url;
|
||||
bool result = wfp_impl_url_init(&url, "wss://localhost/");
|
||||
ASSERT_TRUE(result);
|
||||
ASSERT_EQ(443, url.port);
|
||||
ASSERT_TRUE(url.use_tls);
|
||||
ASSERT_STREQ("localhost", url.host);
|
||||
ASSERT_STREQ("/", url.path);
|
||||
|
||||
wfp_impl_url_cleanup(&url);
|
||||
}
|
||||
|
||||
TEST(url, ParseIPAdress)
|
||||
{
|
||||
struct wfp_impl_url url;
|
||||
bool result = wfp_impl_url_init(&url, "ws://127.0.0.1/");
|
||||
ASSERT_TRUE(result);
|
||||
ASSERT_EQ(80, url.port);
|
||||
ASSERT_STREQ("127.0.0.1", url.host);
|
||||
ASSERT_STREQ("/", url.path);
|
||||
|
||||
wfp_impl_url_cleanup(&url);
|
||||
}
|
||||
|
||||
TEST(url, ParsePort)
|
||||
{
|
||||
struct wfp_impl_url url;
|
||||
bool result = wfp_impl_url_init(&url, "ws://localhost:54321/");
|
||||
ASSERT_TRUE(result);
|
||||
ASSERT_EQ(54321, url.port);
|
||||
|
||||
wfp_impl_url_cleanup(&url);
|
||||
}
|
||||
|
||||
TEST(url, ParseNonEmptyPath)
|
||||
{
|
||||
struct wfp_impl_url url;
|
||||
bool result = wfp_impl_url_init(&url, "ws://localhost/some_path?query");
|
||||
ASSERT_TRUE(result);
|
||||
ASSERT_STREQ("/some_path?query", url.path);
|
||||
|
||||
wfp_impl_url_cleanup(&url);
|
||||
}
|
||||
|
||||
TEST(url, FailToParseUnknownProtocol)
|
||||
{
|
||||
struct wfp_impl_url url;
|
||||
bool result = wfp_impl_url_init(&url, "unknown://localhost/");
|
||||
ASSERT_FALSE(result);
|
||||
ASSERT_EQ(0, url.port);
|
||||
ASSERT_EQ(nullptr, url.path);
|
||||
ASSERT_EQ(nullptr, url.host);
|
||||
}
|
||||
|
||||
TEST(url, FailToParseMissingProtocol)
|
||||
{
|
||||
struct wfp_impl_url url;
|
||||
bool result = wfp_impl_url_init(&url, "unknown");
|
||||
ASSERT_FALSE(result);
|
||||
ASSERT_EQ(0, url.port);
|
||||
ASSERT_EQ(nullptr, url.path);
|
||||
ASSERT_EQ(nullptr, url.host);
|
||||
}
|
||||
|
||||
TEST(url, FailToParseMissingPath)
|
||||
{
|
||||
struct wfp_impl_url url;
|
||||
bool result = wfp_impl_url_init(&url, "ws://localhost");
|
||||
ASSERT_FALSE(result);
|
||||
ASSERT_EQ(0, url.port);
|
||||
ASSERT_EQ(nullptr, url.path);
|
||||
ASSERT_EQ(nullptr, url.host);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user