2019-05-19 12:33:42 +00:00
|
|
|
#include <gtest/gtest.h>
|
2020-06-16 21:39:45 +00:00
|
|
|
#include "webfuse_provider/impl/jsonrpc/proxy.h"
|
|
|
|
#include "webfuse_provider/status.h"
|
|
|
|
#include "webfuse_provider/impl/timer/manager.h"
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-21 19:02:34 +00:00
|
|
|
#include "webfuse_provider/jsonrpc/mock_timer.hpp"
|
2020-03-22 19:09:40 +00:00
|
|
|
|
2020-07-10 21:27:33 +00:00
|
|
|
#include <libwebsockets.h>
|
|
|
|
|
2020-03-01 16:23:59 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
using namespace std::chrono_literals;
|
2020-06-16 21:57:41 +00:00
|
|
|
using wfp_jsonrpc_test::MockTimer;
|
2020-03-22 19:09:40 +00:00
|
|
|
using testing::Return;
|
|
|
|
using testing::_;
|
|
|
|
using testing::DoAll;
|
|
|
|
using testing::SaveArg;
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
#define WFP_DEFAULT_TIMEOUT (10 * 1000)
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2020-03-01 16:44:12 +00:00
|
|
|
int jsonrpc_get_status(json_t * error)
|
|
|
|
{
|
|
|
|
json_t * code = json_object_get(error, "code");
|
2020-06-16 21:57:41 +00:00
|
|
|
return (json_is_integer(code)) ? json_integer_value(code) : WFP_BAD_FORMAT;
|
2020-03-01 16:44:12 +00:00
|
|
|
}
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
struct SendContext
|
|
|
|
{
|
2020-07-10 21:12:35 +00:00
|
|
|
std::string response;
|
2019-05-19 12:33:42 +00:00
|
|
|
bool is_called;
|
|
|
|
|
2020-07-10 19:41:10 +00:00
|
|
|
explicit SendContext()
|
2020-07-10 21:12:35 +00:00
|
|
|
: is_called(false)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~SendContext()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-10 19:41:10 +00:00
|
|
|
void jsonrpc_send(
|
2020-07-10 21:12:35 +00:00
|
|
|
char * request,
|
|
|
|
size_t length,
|
2019-05-19 12:33:42 +00:00
|
|
|
void * user_data)
|
|
|
|
{
|
|
|
|
SendContext * context = reinterpret_cast<SendContext*>(user_data);
|
|
|
|
context->is_called = true;
|
|
|
|
context->response = request;
|
2020-07-10 21:27:33 +00:00
|
|
|
|
|
|
|
char * raw_data = request - LWS_PRE;
|
|
|
|
free(raw_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct FinishedContext
|
|
|
|
{
|
|
|
|
bool is_called;
|
|
|
|
json_t * result;
|
2020-02-28 22:17:41 +00:00
|
|
|
json_t * error;
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
FinishedContext()
|
|
|
|
: is_called(false)
|
|
|
|
, result(nullptr)
|
2020-02-28 22:17:41 +00:00
|
|
|
, error(nullptr)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
~FinishedContext()
|
|
|
|
{
|
|
|
|
if (nullptr != result)
|
|
|
|
{
|
|
|
|
json_decref(result);
|
|
|
|
}
|
2020-02-28 22:17:41 +00:00
|
|
|
|
|
|
|
if (nullptr != error)
|
|
|
|
{
|
|
|
|
json_decref(error);
|
|
|
|
}
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void jsonrpc_finished(
|
|
|
|
void * user_data,
|
2020-02-28 22:17:41 +00:00
|
|
|
json_t const * result,
|
|
|
|
json_t const * error)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
|
|
|
FinishedContext * context = reinterpret_cast<FinishedContext*>(user_data);
|
|
|
|
context->is_called = true;
|
|
|
|
context->result = json_deep_copy(result);
|
2020-02-28 22:17:41 +00:00
|
|
|
context->error = json_deep_copy(error);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_jsonrpc_proxy, init)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext context;
|
|
|
|
void * user_data = reinterpret_cast<void*>(&context);
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, WFP_DEFAULT_TIMEOUT, &jsonrpc_send, user_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wfp_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_FALSE(context.is_called);
|
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_jsonrpc_proxy, invoke)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, WFP_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
FinishedContext finished_context;
|
|
|
|
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(send_context.is_called);
|
2020-07-10 21:12:35 +00:00
|
|
|
ASSERT_STREQ("{\"method\":\"foo\",\"params\":[\"bar\",42],\"id\":42}", send_context.response.c_str());
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_FALSE(finished_context.is_called);
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wfp_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(finished_context.is_called);
|
2020-02-28 22:17:41 +00:00
|
|
|
ASSERT_FALSE(nullptr == finished_context.error);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_jsonrpc_proxy, invoke_fails_if_another_request_is_pending)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, WFP_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
FinishedContext finished_context;
|
|
|
|
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
FinishedContext finished_context2;
|
|
|
|
void * finished_data2 = reinterpret_cast<void*>(&finished_context2);
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data2, "foo", "");
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(send_context.is_called);
|
|
|
|
|
|
|
|
ASSERT_FALSE(finished_context.is_called);
|
|
|
|
|
|
|
|
ASSERT_TRUE(finished_context2.is_called);
|
2020-06-16 21:57:41 +00:00
|
|
|
ASSERT_EQ(WFP_BAD_BUSY, jsonrpc_get_status(finished_context2.error));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wfp_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 19:41:10 +00:00
|
|
|
TEST(wfp_jsonrpc_proxy, invoke_invalid_request)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, WFP_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
FinishedContext finished_context;
|
|
|
|
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "?", "error");
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-07-10 19:41:10 +00:00
|
|
|
ASSERT_TRUE(send_context.is_called);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-07-10 19:41:10 +00:00
|
|
|
ASSERT_FALSE(finished_context.is_called);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wfp_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_jsonrpc_proxy, on_result)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, WFP_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
FinishedContext finished_context;
|
|
|
|
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(send_context.is_called);
|
|
|
|
|
|
|
|
json_t * response = json_object();
|
|
|
|
json_object_set_new(response, "result", json_string("okay"));
|
2020-07-10 21:12:35 +00:00
|
|
|
json_object_set_new(response, "id", json_integer(42));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_onresult(proxy, response);
|
2019-05-19 12:33:42 +00:00
|
|
|
json_decref(response);
|
|
|
|
|
|
|
|
ASSERT_TRUE(finished_context.is_called);
|
2020-02-28 22:17:41 +00:00
|
|
|
ASSERT_EQ(nullptr, finished_context.error);
|
2019-05-19 12:33:42 +00:00
|
|
|
ASSERT_TRUE(json_is_string(finished_context.result));
|
|
|
|
ASSERT_STREQ("okay", json_string_value(finished_context.result));
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wfp_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_jsonrpc_proxy, on_result_reject_response_with_unknown_id)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, WFP_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
FinishedContext finished_context;
|
|
|
|
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(send_context.is_called);
|
|
|
|
|
|
|
|
json_t * response = json_object();
|
|
|
|
json_object_set_new(response, "result", json_string("okay"));
|
2020-07-10 21:12:35 +00:00
|
|
|
json_object_set_new(response, "id", json_integer(1234));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_onresult(proxy, response);
|
2019-05-19 12:33:42 +00:00
|
|
|
json_decref(response);
|
|
|
|
|
|
|
|
ASSERT_FALSE(finished_context.is_called);
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wfp_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_jsonrpc_proxy, timeout)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, 0, &jsonrpc_send, send_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
FinishedContext finished_context;
|
|
|
|
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(send_context.is_called);
|
|
|
|
|
2020-03-01 16:23:59 +00:00
|
|
|
std::this_thread::sleep_for(10ms);
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_manager_check(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(finished_context.is_called);
|
2020-06-16 21:57:41 +00:00
|
|
|
ASSERT_EQ(WFP_BAD_TIMEOUT, jsonrpc_get_status(finished_context.error));
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wfp_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_jsonrpc_proxy, cleanup_pending_request)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, 10, &jsonrpc_send, send_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
FinishedContext finished_context;
|
|
|
|
void * finished_data = reinterpret_cast<void*>(&finished_context);
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_invoke(proxy, &jsonrpc_finished, finished_data, "foo", "si", "bar", 42);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(send_context.is_called);
|
|
|
|
|
|
|
|
ASSERT_FALSE(finished_context.is_called);
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_dispose(proxy);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(finished_context.is_called);
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_jsonrpc_proxy, notify)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, WFP_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_notify(proxy, "foo", "si", "bar", 42);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(send_context.is_called);
|
2020-07-10 21:12:35 +00:00
|
|
|
ASSERT_STREQ("{\"method\":\"foo\",\"params\":[\"bar\",42]}", send_context.response.c_str());
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wfp_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 19:41:10 +00:00
|
|
|
TEST(wfp_jsonrpc_proxy, notify_send_invalid_request)
|
2019-05-19 12:33:42 +00:00
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
2019-05-19 12:33:42 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, WFP_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_notify(proxy, "foo", "?");
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-07-10 19:41:10 +00:00
|
|
|
ASSERT_TRUE(send_context.is_called);
|
2019-05-19 12:33:42 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wfp_timer_manager_dispose(timer_manager);
|
2019-05-19 12:33:42 +00:00
|
|
|
}
|
2020-03-22 19:09:40 +00:00
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_jsonrpc_proxy, swallow_timeout_if_no_request_pending)
|
2020-03-22 19:09:40 +00:00
|
|
|
{
|
|
|
|
MockTimer timer_api;
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_timer_on_timer_fn * on_timer = nullptr;
|
2020-03-22 19:09:40 +00:00
|
|
|
void * timer_context = nullptr;
|
2020-06-16 21:57:41 +00:00
|
|
|
EXPECT_CALL(timer_api, wfp_timer_create(_, _, _))
|
2020-03-22 19:09:40 +00:00
|
|
|
.Times(1)
|
|
|
|
.WillOnce(DoAll(SaveArg<1>(&on_timer), SaveArg<2>(&timer_context), Return(nullptr)));
|
2020-06-16 21:57:41 +00:00
|
|
|
EXPECT_CALL(timer_api, wfp_timer_dispose(_)).Times(1);
|
2020-03-22 19:09:40 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(nullptr, 1, &jsonrpc_send, send_data);
|
2020-03-22 19:09:40 +00:00
|
|
|
|
|
|
|
on_timer(nullptr, timer_context);
|
|
|
|
ASSERT_FALSE(send_context.is_called);
|
|
|
|
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_dispose(proxy);
|
2020-03-22 19:09:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
TEST(wfp_jsonrpc_proxy, on_result_swallow_if_no_request_pending)
|
2020-03-22 19:09:40 +00:00
|
|
|
{
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_timer_manager * timer_manager = wfp_timer_manager_create();
|
2020-03-22 19:09:40 +00:00
|
|
|
|
|
|
|
SendContext send_context;
|
|
|
|
void * send_data = reinterpret_cast<void*>(&send_context);
|
2020-06-16 21:57:41 +00:00
|
|
|
struct wfp_jsonrpc_proxy * proxy = wfp_jsonrpc_proxy_create(timer_manager, WFP_DEFAULT_TIMEOUT, &jsonrpc_send, send_data);
|
2020-03-22 19:09:40 +00:00
|
|
|
|
|
|
|
json_t * response = json_object();
|
|
|
|
json_object_set_new(response, "result", json_string("okay"));
|
|
|
|
json_object_set_new(response, "id", json_integer(42));
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_onresult(proxy, response);
|
2020-03-22 19:09:40 +00:00
|
|
|
json_decref(response);
|
|
|
|
|
2020-06-16 21:57:41 +00:00
|
|
|
wfp_jsonrpc_proxy_dispose(proxy);
|
|
|
|
wfp_timer_manager_dispose(timer_manager);
|
2020-03-22 19:09:40 +00:00
|
|
|
}
|
|
|
|
|