From 0b0c16bf10708f41ba7dda4725bca581bac22aaf Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Mon, 30 Mar 2020 21:42:13 +0200 Subject: [PATCH] added tests for wfp_impl_open --- cmake/unit_tests.cmake | 1 + test/webfuse/mocks/mock_request.hpp | 12 ++ .../tests/provider/operation/test_open.cc | 110 ++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 test/webfuse/tests/provider/operation/test_open.cc diff --git a/cmake/unit_tests.cmake b/cmake/unit_tests.cmake index 3ded8ae..2c271b5 100644 --- a/cmake/unit_tests.cmake +++ b/cmake/unit_tests.cmake @@ -50,6 +50,7 @@ add_executable(alltests test/webfuse/tests/provider/operation/test_close.cc test/webfuse/tests/provider/operation/test_getattr.cc test/webfuse/tests/provider/operation/test_lookup.cc + test/webfuse/tests/provider/operation/test_open.cc test/webfuse/tests/integration/test_integration.cc test/webfuse/tests/integration/server.cc test/webfuse/tests/integration/provider.cc diff --git a/test/webfuse/mocks/mock_request.hpp b/test/webfuse/mocks/mock_request.hpp index e3476ba..f286409 100644 --- a/test/webfuse/mocks/mock_request.hpp +++ b/test/webfuse/mocks/mock_request.hpp @@ -44,6 +44,18 @@ MATCHER_P3(StatMatcher, inode, mode, file_type, "") return true; } +MATCHER_P(OpenMatcher, handle, "") +{ + json_t * handle_holder = json_object_get(arg, "handle"); + if ((!json_is_integer(handle_holder)) || (handle != json_integer_value(handle_holder))) + { + *result_listener << "missing handle"; + return false; + } + + return true; +} + MATCHER_P(ReaddirMatcher, contained_elements , "") { if (!json_is_array(arg)) diff --git a/test/webfuse/tests/provider/operation/test_open.cc b/test/webfuse/tests/provider/operation/test_open.cc new file mode 100644 index 0000000..68a53f0 --- /dev/null +++ b/test/webfuse/tests/provider/operation/test_open.cc @@ -0,0 +1,110 @@ +#include "webfuse/provider/impl/operation/open.h" +#include "webfuse/mocks/mock_request.hpp" +#include "webfuse/mocks/mock_provider.hpp" +#include "webfuse/mocks/fake_invokation_context.hpp" + +#include +#include + +using ::webfuse_test::MockProvider; +using ::webfuse_test::MockRequest; +using ::webfuse_test::OpenMatcher; +using ::webfuse_test::create_context; +using ::testing::_; +using ::testing::Invoke; +using ::testing::StrEq; + +namespace +{ + +void free_request(wfp_request * request, ino_t, int) +{ + free(request); +} + +} + +TEST(wfp_impl_open, invoke_provider) +{ + ino_t inode = 42; + int flags = 0; + MockProvider provider; + EXPECT_CALL(provider,open(_, inode, flags)).Times(1) + .WillOnce(Invoke(free_request)); + + wfp_request request = {nullptr, nullptr, 0}; + wfp_impl_invokation_context context = create_context(provider, &request); + + json_t * params = json_array(); + json_array_append_new(params, json_string("test.filesystem")); + json_array_append_new(params, json_integer(inode)); + json_array_append_new(params, json_integer(flags)); + + wfp_impl_open(&context, params, 42); +} + +TEST(wfp_impl_open, fail_invalid_param_count) +{ + MockProvider provider; + EXPECT_CALL(provider,open(_, _, _)).Times(0); + + wfp_request request = {nullptr, nullptr, 0}; + wfp_impl_invokation_context context = create_context(provider, &request); + + json_t * params = json_array(); + json_array_append_new(params, json_string("test.filesystem")); + json_array_append_new(params, json_integer(23)); + + wfp_impl_open(&context, params, 42); +} + +TEST(wfp_impl_open, fail_invalid_inode_type) +{ + MockProvider provider; + EXPECT_CALL(provider,open(_, _, _)).Times(0); + + wfp_request request = {nullptr, nullptr, 0}; + wfp_impl_invokation_context context = create_context(provider, &request); + + json_t * params = json_array(); + json_array_append_new(params, json_string("test.filesystem")); + json_array_append_new(params, json_string("")); + json_array_append_new(params, json_integer(0)); + + wfp_impl_open(&context, params, 42); +} + +TEST(wfp_impl_open, fail_invalid_flags_type) +{ + MockProvider provider; + EXPECT_CALL(provider,open(_, _, _)).Times(0); + + wfp_request request = {nullptr, nullptr, 0}; + wfp_impl_invokation_context context = create_context(provider, &request); + + json_t * params = json_array(); + json_array_append_new(params, json_string("test.filesystem")); + json_array_append_new(params, json_integer(23)); + json_array_append_new(params, json_string("")); + + wfp_impl_open(&context, params, 42); +} + +TEST(wfp_impl_open, default_responds_error) +{ + MockRequest request; + auto * req = request.create_request(42); + EXPECT_CALL(request, respond_error(_,42)).Times(1); + + wfp_impl_open_default(req, 1, 0, nullptr); +} + +TEST(wfp_impl_open, respond) +{ + MockRequest request; + auto * req = request.create_request(42); + EXPECT_CALL(request, respond(OpenMatcher(23), 42)).Times(1); + + wfp_impl_respond_open(req, 23); +} +