diff --git a/cmake/unit_tests.cmake b/cmake/unit_tests.cmake index 619afbe..06d510d 100644 --- a/cmake/unit_tests.cmake +++ b/cmake/unit_tests.cmake @@ -30,6 +30,7 @@ add_executable(alltests test/webfuse/mocks/mock_request.cc test/webfuse/mocks/mock_provider_client.cc test/webfuse/mocks/mock_provider.cc + test/webfuse/mocks/mock_fuse.cc test/webfuse//tests/core/test_util.cc test/webfuse/tests/core/test_container_of.cc test/webfuse/tests/core/test_string.cc @@ -45,6 +46,7 @@ add_executable(alltests test/webfuse/tests/adapter/test_authenticators.cc test/webfuse/tests/adapter/test_mountpoint.cc test/webfuse/tests/adapter/test_fuse_req.cc + test/webfuse/tests/adapter/operation/test_open.cc test/webfuse/tests/provider/test_url.cc test/webfuse/tests/provider/test_client_protocol.cc test/webfuse/tests/provider/operation/test_close.cc @@ -67,6 +69,9 @@ target_link_libraries(alltests PUBLIC -Wl,--wrap=wf_timer_dispose -Wl,--wrap=wf_timer_start -Wl,--wrap=wf_timer_cancel + -Wl,--wrap=fuse_req_userdata + -Wl,--wrap=fuse_reply_open + -Wl,--wrap=fuse_reply_err webfuse-adapter-static webfuse-provider-static diff --git a/lib/webfuse/adapter/impl/operation/open.c b/lib/webfuse/adapter/impl/operation/open.c index 8e21100..07d8ea9 100644 --- a/lib/webfuse/adapter/impl/operation/open.c +++ b/lib/webfuse/adapter/impl/operation/open.c @@ -22,7 +22,7 @@ static void wf_impl_operation_open_finished( if (NULL != result) { json_t * handle_holder = json_object_get(result, "handle"); - if ((NULL != handle_holder) && (json_is_integer(handle_holder))) + if (json_is_integer(handle_holder)) { file_info.fh = json_integer_value(handle_holder); } diff --git a/test/webfuse/mocks/mock_fuse.cc b/test/webfuse/mocks/mock_fuse.cc new file mode 100644 index 0000000..fa2f981 --- /dev/null +++ b/test/webfuse/mocks/mock_fuse.cc @@ -0,0 +1,28 @@ +#include "webfuse/mocks/mock_fuse.hpp" +#include "webfuse/utils/wrap.hpp" + +extern "C" +{ +static webfuse_test::FuseMock * webfuse_test_FuseMock = nullptr; + +WF_WRAP_FUNC1(webfuse_test_FuseMock, void*, fuse_req_userdata, fuse_req_t); +WF_WRAP_FUNC2(webfuse_test_FuseMock, int, fuse_reply_open, fuse_req_t, const struct fuse_file_info *); +WF_WRAP_FUNC2(webfuse_test_FuseMock, int, fuse_reply_err, fuse_req_t, int); + +} + +namespace webfuse_test +{ + +FuseMock::FuseMock() +{ + webfuse_test_FuseMock = this; +} + +FuseMock::~FuseMock() +{ + webfuse_test_FuseMock = nullptr; +} + + +} \ No newline at end of file diff --git a/test/webfuse/mocks/mock_fuse.hpp b/test/webfuse/mocks/mock_fuse.hpp new file mode 100644 index 0000000..ed6e6ff --- /dev/null +++ b/test/webfuse/mocks/mock_fuse.hpp @@ -0,0 +1,19 @@ +#include "webfuse/adapter/impl/fuse_wrapper.h" +#include + +namespace webfuse_test +{ + +class FuseMock +{ +public: + FuseMock(); + virtual ~FuseMock(); + + MOCK_METHOD1(fuse_req_userdata, void *(fuse_req_t req)); + MOCK_METHOD2(fuse_reply_open, int (fuse_req_t req, const struct fuse_file_info *fi)); + MOCK_METHOD2(fuse_reply_err, int (fuse_req_t req, int err)); +}; + + +} \ No newline at end of file diff --git a/test/webfuse/tests/adapter/operation/test_open.cc b/test/webfuse/tests/adapter/operation/test_open.cc new file mode 100644 index 0000000..d1b5960 --- /dev/null +++ b/test/webfuse/tests/adapter/operation/test_open.cc @@ -0,0 +1,28 @@ +#include "webfuse/adapter/impl/operations.h" +#include "webfuse/mocks/mock_fuse.hpp" +#include "webfuse/adapter/impl/session.h" + +#include + +using webfuse_test::FuseMock; +using testing::_; +using testing::Return; + +TEST(wf_impl_operation_open, fail_rpc_null) +{ + wf_impl_session session; + memset(&session, 0, sizeof(session)); + wf_impl_operations_context context = + {&session, 1.0f, nullptr }; + + FuseMock fuse; + EXPECT_CALL(fuse, fuse_req_userdata(_)).Times(1) + .WillOnce(Return(reinterpret_cast(&context))); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1) + .WillOnce(Return(0)); + + fuse_req_t request = nullptr; + fuse_ino_t inode = 1; + fuse_file_info * file_info = nullptr; + wf_impl_operation_open(request, inode, file_info); +} \ No newline at end of file