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:
42
test/webfuse/mocks/mock_authenticator.cc
Normal file
42
test/webfuse/mocks/mock_authenticator.cc
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "webfuse/mocks/mock_authenticator.hpp"
|
||||
|
||||
#define WF_AUTHENTICATOR_COUNT 3
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
webfuse_test::Authenticator * g_authenticators[WF_AUTHENTICATOR_COUNT];
|
||||
|
||||
}
|
||||
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
void set_authenticator(Authenticator * authenticator)
|
||||
{
|
||||
set_authenticator(0, authenticator);
|
||||
}
|
||||
|
||||
void set_authenticator(size_t i, Authenticator * authenticator)
|
||||
{
|
||||
g_authenticators[i] = authenticator;
|
||||
}
|
||||
|
||||
bool authenticate(struct wf_credentials * creds, void * user_data)
|
||||
{
|
||||
return g_authenticators[0]->authenticate(creds, user_data);
|
||||
}
|
||||
|
||||
bool authenticate_1(struct wf_credentials * creds, void * user_data)
|
||||
{
|
||||
return g_authenticators[1]->authenticate(creds, user_data);
|
||||
}
|
||||
|
||||
bool authenticate_2(struct wf_credentials * creds, void * user_data)
|
||||
{
|
||||
return g_authenticators[2]->authenticate(creds, user_data);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
34
test/webfuse/mocks/mock_authenticator.hpp
Normal file
34
test/webfuse/mocks/mock_authenticator.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef MOCK_AUTHENTICATOR_H
|
||||
#define MOCK_AUTHENTICATOR_H
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
#include "webfuse/adapter/impl/authenticator.h"
|
||||
|
||||
namespace webfuse_test
|
||||
{
|
||||
|
||||
class Authenticator
|
||||
{
|
||||
public:
|
||||
virtual ~Authenticator() { }
|
||||
virtual bool authenticate(
|
||||
struct wf_credentials * credentials,
|
||||
void * user_data) = 0;
|
||||
};
|
||||
|
||||
class MockAuthenticator: public Authenticator
|
||||
{
|
||||
public:
|
||||
MOCK_METHOD2(authenticate, bool (struct wf_credentials * credentials, void * user_data));
|
||||
};
|
||||
|
||||
void set_authenticator(Authenticator * authenticator);
|
||||
void set_authenticator(size_t index, Authenticator * authenticator);
|
||||
|
||||
bool authenticate(struct wf_credentials * creds, void * user_data);
|
||||
bool authenticate_1(struct wf_credentials * creds, void * user_data);
|
||||
bool authenticate_2(struct wf_credentials * creds, void * user_data);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
55
test/webfuse/mocks/mock_request.cc
Normal file
55
test/webfuse/mocks/mock_request.cc
Normal file
@@ -0,0 +1,55 @@
|
||||
#include "webfuse/mocks/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/webfuse/mocks/mock_request.hpp
Normal file
122
test/webfuse/mocks/mock_request.hpp
Normal 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
|
||||
Reference in New Issue
Block a user