1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2026-03-02 04:09:18 +00:00

feat(webfuse): static file provider (#30)

* added API stub of static_filesystem

* added callback functions

* added basic directory listing

* resize filesystem if necessary

* added path stub

* adds imlementation and tests of path

* adds mock of wpf_request

* adds test implementation and some matchers

* added matcher of readdir results

* fixes default directory test

* adds implementation of static_filesystem_add and add_text

* added implementation of read

* adds implementation of filesystem_read

* corrects naming of some functions

* removes Flawfinder comments, since Flawfinder is disabled
This commit is contained in:
Falk Werner
2019-04-26 20:49:09 +02:00
committed by GitHub
parent a717248e80
commit fa78e23533
19 changed files with 1301 additions and 9 deletions

34
test/json_matcher.hpp Normal file
View File

@@ -0,0 +1,34 @@
#ifndef WF_JSON_MATCHER_HPP
#define FW_JSON_MATCHER_HPP
#include <gtest/gtest.h>
#include <jansson.h>
namespace webfuse_test
{
MATCHER_P(JsonMatcher, expected_str, "")
{
std::cout << "--- JsonMatcher ---" << std::endl;
bool matches = false;
json_t * expected = json_loads(expected_str, 0, nullptr);
if (nullptr != expected)
{
matches = (1 == json_equal(expected, arg));
if (!matches)
{
char * actual = json_dumps(arg, 0);
std::cout << actual << std::endl;
*result_listener << "where arg is " << actual;
free(actual);
}
json_decref(expected);
}
return true; //matches;
}
}
#endif

55
test/mock_request.cc Normal file
View File

@@ -0,0 +1,55 @@
#include "mock_request.hpp"
#include <cstdlib>
namespace
{
extern "C" void
respond(
json_t * response,
void * user_data)
{
webfuse_test::Request * request = reinterpret_cast<webfuse_test::Request*>(user_data);
json_t * result = json_object_get(response, "result");
json_t * error = json_object_get(response, "error");
json_t * id_holder = json_object_get(response, "id");
int id = -1;
if (json_is_integer(id_holder))
{
id = json_integer_value(id_holder);
}
if (nullptr != result)
{
request->respond(result, id);
}
else
{
request->respond_error(error, id);
}
}
}
namespace webfuse_test
{
struct wfp_request *
request_create(
Request * req,
int id)
{
struct wfp_request * request = reinterpret_cast<struct wfp_request *>(malloc(sizeof(struct wfp_request)));
request->id = id;
request->user_data = reinterpret_cast<void*>(req);
request->respond = &respond;
return request;
}
}

122
test/mock_request.hpp Normal file
View File

@@ -0,0 +1,122 @@
#ifndef WF_MOCK_REQUEST_HPP
#define WF_MOCK_REQUEST_HPP
#include <gmock/gmock.h>
#include <jansson.h>
#include <cstring>
#include "webfuse/provider/impl/request.h"
namespace webfuse_test
{
class Request
{
public:
virtual ~Request() { }
virtual void respond(json_t * result, int id) = 0;
virtual void respond_error(json_t * error, int id) = 0;
};
class MockRequest: public Request
{
public:
MOCK_METHOD2(respond, void(json_t * result, int id));
MOCK_METHOD2(respond_error, void(json_t * error, int id));
};
extern struct wfp_request *
request_create(
Request * req,
int id);
MATCHER_P3(GetAttrMatcher, inode, mode, file_type, "")
{
json_t * inode_holder = json_object_get(arg, "inode");
if ((!json_is_integer(inode_holder)) || (inode != json_integer_value(inode_holder)))
{
*result_listener << "missing inode";
return false;
}
json_t * mode_holder = json_object_get(arg, "mode");
if ((!json_is_integer(mode_holder)) || (mode != json_integer_value(mode_holder)))
{
*result_listener << "missing mode";
return false;
}
json_t * type_holder = json_object_get(arg, "type");
if ((!json_is_string(type_holder)) || (0 != strcmp(file_type, json_string_value(type_holder))))
{
*result_listener << "missing type";
return false;
}
return true;
}
MATCHER_P(ReaddirMatcher, contained_elements , "")
{
if (!json_is_array(arg))
{
*result_listener << "result is not array";
return false;
}
{
size_t i;
json_t * value;
json_array_foreach(arg, i, value)
{
json_t * inode = json_object_get(value, "inode");
json_t * name = json_object_get(value, "name");
if(!json_is_integer(inode))
{
*result_listener << "invalid result: missing inode";
return false;
}
if (!json_is_string(name))
{
*result_listener << "invalid result: missing name";
return false;
}
}
}
for(size_t i = 0; NULL != contained_elements[i]; i++)
{
char const * element = contained_elements[i];
bool found = false;
size_t j;
json_t * value;
json_array_foreach(arg, j, value)
{
json_t * name = json_object_get(value, "name");
found = (0 == strcmp(element, json_string_value(name)));
if (found)
{
break;
}
}
if (!found)
{
*result_listener << "missing required directory element: " << element;
return false;
}
}
return true;
}
}
#endif

58
test/test_path.cc Normal file
View File

@@ -0,0 +1,58 @@
#include <gtest/gtest.h>
#include "webfuse/core/path.h"
TEST(wf_path, empty)
{
struct wf_path * path = wf_path_create("");
ASSERT_EQ(0, wf_path_element_count(path));
ASSERT_EQ(nullptr, wf_path_get_element(path, 0));
wf_path_dispose(path);
}
TEST(wf_path, relative_file)
{
struct wf_path * path = wf_path_create("some.file");
ASSERT_EQ(1, wf_path_element_count(path));
ASSERT_STREQ("some.file", wf_path_get_element(path, 0));
wf_path_dispose(path);
}
TEST(wf_path, absolute_file)
{
struct wf_path * path = wf_path_create("/absolute.file");
ASSERT_EQ(1, wf_path_element_count(path));
ASSERT_STREQ("absolute.file", wf_path_get_element(path, 0));
wf_path_dispose(path);
}
TEST(wf_path, nested_path)
{
struct wf_path * path = wf_path_create("/a/nested/path");
ASSERT_EQ(3, wf_path_element_count(path));
ASSERT_STREQ("a", wf_path_get_element(path, 0));
ASSERT_STREQ("nested", wf_path_get_element(path, 1));
ASSERT_STREQ("path", wf_path_get_element(path, 2));
wf_path_dispose(path);
}
TEST(wf_path, deep_nested_path)
{
struct wf_path * path = wf_path_create("/this/is/a/very/deep/nested/path/to/some/file");
ASSERT_EQ(10, wf_path_element_count(path));
ASSERT_STREQ("this", wf_path_get_element(path, 0));
ASSERT_STREQ("is", wf_path_get_element(path, 1));
ASSERT_STREQ("a", wf_path_get_element(path, 2));
ASSERT_STREQ("very", wf_path_get_element(path, 3));
ASSERT_STREQ("deep", wf_path_get_element(path, 4));
ASSERT_STREQ("nested", wf_path_get_element(path, 5));
ASSERT_STREQ("path", wf_path_get_element(path, 6));
ASSERT_STREQ("to", wf_path_get_element(path, 7));
ASSERT_STREQ("some", wf_path_get_element(path, 8));
ASSERT_STREQ("file", wf_path_get_element(path, 9));
wf_path_dispose(path);
}

View 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 "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);
}