diff --git a/.travis.yml b/.travis.yml index ceddb6c..ae18a7a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,18 +12,18 @@ addons: - qemu-user-static env: global: - - DISTRO=ubuntu - PARALLELMFLAGS="-j4" - PORTABLE_WORSPACE=1 matrix: - - BUILDTYPE=Debug MARCH=amd64 CHECK_TARGET=memcheck - - BUILDTYPE=Coverage MARCH=amd64 CHECK_TARGET=check - - BUILDTYPE=Release MARCH=amd64 CHECK_TARGET=memcheck - - BUILDTYPE=Debug MARCH=arm32v7 CHECK_TARGET=check - - BUILDTYPE=MinSizeRel MARCH=arm32v7 CHECK_TARGET=check + - DISTRO=ubuntu BUILDTYPE=Debug MARCH=amd64 CHECK_TARGET=memcheck + - DISTRO=ubuntu BUILDTYPE=Coverage MARCH=amd64 CHECK_TARGET=check + - DISTRO=ubuntu BUILDTYPE=Release MARCH=amd64 CHECK_TARGET=memcheck + - DISTRO=ubuntu BUILDTYPE=Debug MARCH=arm32v7 CHECK_TARGET=check + - DISTRO=ubuntu BUILDTYPE=MinSizeRel MARCH=arm32v7 CHECK_TARGET=check + - DISTRO=alpine BUILDTYPE=Debug MARCH=amd64 CHECK_TARGET=check before_script: - make BUILDTYPE=$BUILDTYPE MARCH=$MARCH script: -- make BUILDTYPE=$BUILDTYPE MARCH=$MARCH $CHECK_TARGET +- make DISTRO=$DISTRO BUILDTYPE=$BUILDTYPE MARCH=$MARCH $CHECK_TARGET after_success: - bash <(curl -s https://codecov.io/bash) diff --git a/CMakeLists.txt b/CMakeLists.txt index cfe430b..5e3f18c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,8 +37,6 @@ add_compile_options( "-pthread" ) -include(wf_timer) -include(wf_jsonrpc) include(webfuse_core) include(webfuse_adapter) include(webfuse_provider) diff --git a/build/amd64-ubuntu-builder.dockerfile b/build/amd64-ubuntu-builder.dockerfile index 7f5cc46..cbdc241 100644 --- a/build/amd64-ubuntu-builder.dockerfile +++ b/build/amd64-ubuntu-builder.dockerfile @@ -58,6 +58,7 @@ RUN set -x \ && mkdir -p "$builddir" \ && cd "$builddir" \ && meson "/usr/local/src/libfuse-fuse-$FUSE_VERSION" \ + && meson configure -Dexamples=false \ && ninja \ && ninja install \ && pip3 uninstall -y meson \ diff --git a/build/arm32v7-ubuntu-builder.dockerfile b/build/arm32v7-ubuntu-builder.dockerfile index a84a8e3..a9da298 100644 --- a/build/arm32v7-ubuntu-builder.dockerfile +++ b/build/arm32v7-ubuntu-builder.dockerfile @@ -61,6 +61,7 @@ RUN set -x \ && mkdir -p "$builddir" \ && cd "$builddir" \ && meson "/usr/local/src/libfuse-fuse-$FUSE_VERSION" \ + && meson configure -Dexamples=false \ && ninja \ && ninja install \ && pip3 uninstall -y meson \ diff --git a/cmake/unit_tests.cmake b/cmake/unit_tests.cmake index aaf5381..60023ee 100644 --- a/cmake/unit_tests.cmake +++ b/cmake/unit_tests.cmake @@ -1,31 +1,42 @@ if(NOT WITHOUT_TESTS AND NOT WITHOUT_ADAPTER AND NOT WITHOUT_PROVIDER) +set(MEMORYCHECK_COMMAND_OPTIONS "--leak-check=full --error-exitcode=1") include (CTest) +add_executable(fs_check + test/webfuse/tests/integration/fs_check.c +) + pkg_check_modules(GTEST gtest_main) include(GoogleTest) pkg_check_modules(GMOCK gmock) add_executable(alltests - lib/wf/jsonrpc/test/wf/jsonrpc/test_is_request.cc - lib/wf/jsonrpc/test/wf/jsonrpc/test_request.cc - lib/wf/jsonrpc/test/wf/jsonrpc/test_is_response.cc - lib/wf/jsonrpc/test/wf/jsonrpc/test_response.cc - lib/wf/jsonrpc/test/wf/jsonrpc/test_server.cc - lib/wf/jsonrpc/test/wf/jsonrpc/test_proxy.cc - lib/wf/jsonrpc/test/wf/jsonrpc/test_response_parser.cc - lib/wf/timer/test/wf/timer/test_timepoint.cc - lib/wf/timer/test/wf/timer/test_timer.cc + test/webfuse/tests/core/jsonrpc/mock_timer_callback.cc + test/webfuse/tests/core/jsonrpc/mock_timer.cc + test/webfuse/tests/core/jsonrpc/test_is_request.cc + test/webfuse/tests/core/jsonrpc/test_request.cc + test/webfuse/tests/core/jsonrpc/test_is_response.cc + test/webfuse/tests/core/jsonrpc/test_response.cc + test/webfuse/tests/core/jsonrpc/test_server.cc + test/webfuse/tests/core/jsonrpc/test_proxy.cc + test/webfuse/tests/core/jsonrpc/test_response_parser.cc + test/webfuse/tests/core/timer/test_timepoint.cc + test/webfuse/tests/core/timer/test_timer.cc test/webfuse/utils/tempdir.cc test/webfuse/utils/file_utils.cc - test/webfuse/utils/die_if.cc test/webfuse/utils/timeout_watcher.cc test/webfuse/utils/path.c test/webfuse/utils/static_filesystem.c test/webfuse/utils/ws_server.cc + test/webfuse/mocks/fake_invokation_context.cc test/webfuse/mocks/mock_authenticator.cc 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/mocks/mock_operation_context.cc + test/webfuse/mocks/mock_jsonrpc_proxy.cc test/webfuse//tests/core/test_util.cc test/webfuse/tests/core/test_container_of.cc test/webfuse/tests/core/test_string.cc @@ -41,31 +52,50 @@ 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_context.cc + test/webfuse/tests/adapter/operation/test_open.cc + test/webfuse/tests/adapter/operation/test_close.cc + test/webfuse/tests/adapter/operation/test_read.cc + test/webfuse/tests/adapter/operation/test_readdir.cc + test/webfuse/tests/adapter/operation/test_getattr.cc + test/webfuse/tests/adapter/operation/test_lookup.cc test/webfuse/tests/provider/test_url.cc test/webfuse/tests/provider/test_client_protocol.cc + 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/provider/operation/test_read.cc + test/webfuse/tests/provider/operation/test_readdir.cc + test/webfuse/tests/integration/test_lowlevel.cc test/webfuse/tests/integration/test_integration.cc + test/webfuse/tests/integration/file.cc test/webfuse/tests/integration/server.cc test/webfuse/tests/integration/provider.cc ) -target_include_directories(alltests PRIVATE - lib/wf/jsonrpc/include - lib/wf/jsonrpc/src - lib/wf/timer/include - lib/wf/timer/src - ${FUSE3_INCLUDE_DIRS} -) - -target_compile_options(alltests PUBLIC - ${FUSE3_CFLAGS_OTHER} -) +target_link_libraries(alltests PUBLIC + -Wl,--wrap=wf_timer_manager_create + -Wl,--wrap=wf_timer_manager_dispose + -Wl,--wrap=wf_timer_manager_check + -Wl,--wrap=wf_timer_create + -Wl,--wrap=wf_timer_dispose + -Wl,--wrap=wf_timer_start + -Wl,--wrap=wf_timer_cancel + -Wl,--wrap=wf_impl_operation_context_get_proxy + -Wl,--wrap=wf_jsonrpc_proxy_vinvoke + -Wl,--wrap=wf_jsonrpc_proxy_vnotify + -Wl,--wrap=fuse_req_userdata + -Wl,--wrap=fuse_reply_open + -Wl,--wrap=fuse_reply_err + -Wl,--wrap=fuse_reply_buf + -Wl,--wrap=fuse_reply_attr + -Wl,--wrap=fuse_reply_entry + -Wl,--wrap=fuse_req_ctx -target_link_libraries(alltests PUBLIC webfuse-adapter-static webfuse-provider-static webfuse-core - wf_jsonrpc - wf_timer ${FUSE3_LIBRARIES} ${LWS_LIBRARIES} ${JANSSON_LIBRARIES} @@ -74,8 +104,16 @@ target_link_libraries(alltests PUBLIC ${GTEST_LIBRARIES} ) -target_include_directories(alltests PUBLIC test lib ${GMOCK_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS}) -target_compile_options(alltests PUBLIC ${GMOCK_CFLAGS} ${GTEST_CFLAGS}) +target_include_directories(alltests PUBLIC test lib ${FUSE3_INCLUDE_DIRS} ${GMOCK_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS}) +target_compile_options(alltests PUBLIC ${FUSE3_CFLAGS_OTHER} ${GMOCK_CFLAGS} ${GTEST_CFLAGS}) + +add_custom_command(OUTPUT server-key.pem + COMMAND openssl req -x509 -newkey rsa:4096 -keyout server-key.pem -out server-cert.pem -days 365 -nodes -batch -subj '/CN=localhost' + COMMAND openssl req -x509 -newkey rsa:4096 -keyout client-key.pem -out client-cert.pem -days 365 -nodes -batch -subj '/CN=localhost' +) + +add_custom_target(gen-tls DEPENDS server-key.pem) +add_dependencies(alltests gen-tls) enable_testing() gtest_discover_tests(alltests TEST_PREFIX alltests:) @@ -91,7 +129,7 @@ add_custom_target(coverage add_dependencies(coverage alltests) add_custom_target(coverage-report - COMMAND genhtml --branch-coverage --highlight --legend --prefix "${CMAKE_SOURCE_DIR}" coverage/lcov.info --output-directory coverage/report + COMMAND genhtml --branch-coverage --highlight --legend coverage/lcov.info --output-directory coverage/report ) add_dependencies(coverage-report coverage) diff --git a/cmake/webfuse_adapter.cmake b/cmake/webfuse_adapter.cmake index f6e777c..b15b655 100644 --- a/cmake/webfuse_adapter.cmake +++ b/cmake/webfuse_adapter.cmake @@ -13,9 +13,9 @@ add_library(webfuse-adapter-static STATIC lib/webfuse/adapter/impl/authenticator.c lib/webfuse/adapter/impl/authenticators.c lib/webfuse/adapter/impl/credentials.c - lib/webfuse/adapter/impl/operations.c lib/webfuse/adapter/impl/mountpoint.c lib/webfuse/adapter/impl/mountpoint_factory.c + lib/webfuse/adapter/impl/operation/context.c lib/webfuse/adapter/impl/operation/lookup.c lib/webfuse/adapter/impl/operation/getattr.c lib/webfuse/adapter/impl/operation/readdir.c @@ -26,8 +26,6 @@ add_library(webfuse-adapter-static STATIC target_include_directories(webfuse-adapter-static PRIVATE lib - lib/wf/timer/include - lib/wf/jsonrpc/include ${FUSE3_INCLUDE_DIRS} ) @@ -56,7 +54,7 @@ set_target_properties(webfuse-adapter PROPERTIES SOVERSION 0) set_target_properties(webfuse-adapter PROPERTIES C_VISIBILITY_PRESET hidden) set_target_properties(webfuse-adapter PROPERTIES COMPILE_DEFINITIONS "WF_API=WF_EXPORT") -target_link_libraries(webfuse-adapter PRIVATE webfuse-adapter-static webfuse-core wf_jsonrpc wf_timer) +target_link_libraries(webfuse-adapter PRIVATE webfuse-adapter-static webfuse-core) file(WRITE "${PROJECT_BINARY_DIR}/libwebfuse-adapter.pc" "prefix=\"${CMAKE_INSTALL_PREFIX}\" diff --git a/cmake/webfuse_core.cmake b/cmake/webfuse_core.cmake index dab6223..c0b1b88 100644 --- a/cmake/webfuse_core.cmake +++ b/cmake/webfuse_core.cmake @@ -9,6 +9,17 @@ add_library(webfuse-core STATIC lib/webfuse/core/base64.c lib/webfuse/core/lws_log.c lib/webfuse/core/json_util.c + lib/webfuse/core/timer/manager.c + lib/webfuse/core/timer/timepoint.c + lib/webfuse/core/timer/timer.c + lib/webfuse/core/jsonrpc/proxy.c + lib/webfuse/core/jsonrpc/proxy_variadic.c + lib/webfuse/core/jsonrpc/server.c + lib/webfuse/core/jsonrpc/method.c + lib/webfuse/core/jsonrpc/request.c + lib/webfuse/core/jsonrpc/response.c + lib/webfuse/core/jsonrpc/error.c + ) set_target_properties(webfuse-core PROPERTIES OUTPUT_NAME webfuse-core) diff --git a/cmake/webfuse_provider.cmake b/cmake/webfuse_provider.cmake index 4cf285d..fbfa104 100644 --- a/cmake/webfuse_provider.cmake +++ b/cmake/webfuse_provider.cmake @@ -22,8 +22,6 @@ set_target_properties(webfuse-provider-static PROPERTIES OUTPUT_NAME webfuse-pro set_target_properties(webfuse-provider-static PROPERTIES C_VISIBILITY_PRESET hidden) target_include_directories(webfuse-provider-static PRIVATE lib - lib/wf/timer/include - lib/wf/jsonrpc/include ) add_library(webfuse-provider SHARED @@ -36,7 +34,7 @@ set_target_properties(webfuse-provider PROPERTIES C_VISIBILITY_PRESET hidden) set_target_properties(webfuse-provider PROPERTIES COMPILE_DEFINITIONS "WFP_API=WFP_EXPORT") target_include_directories(webfuse-provider PUBLIC lib) -target_link_libraries(webfuse-provider PRIVATE webfuse-provider-static webfuse-core wf_jsonrpc wf_timer) +target_link_libraries(webfuse-provider PRIVATE webfuse-provider-static webfuse-core) file(WRITE "${PROJECT_BINARY_DIR}/libwebfuse-provider.pc" "prefix=\"${CMAKE_INSTALL_PREFIX}\" diff --git a/cmake/wf_jsonrpc.cmake b/cmake/wf_jsonrpc.cmake deleted file mode 100644 index fae8d74..0000000 --- a/cmake/wf_jsonrpc.cmake +++ /dev/null @@ -1,24 +0,0 @@ -# wf_jsonrpc - -add_library(wf_jsonrpc STATIC - lib/wf/jsonrpc/src/wf/jsonrpc/api.c - lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.c - lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.c - lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.c - lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.c - lib/wf/jsonrpc/src/wf/jsonrpc/impl/response.c - lib/wf/jsonrpc/src/wf/jsonrpc/impl/error.c -) - -target_link_libraries(wf_jsonrpc PUBLIC wf_timer) - -target_include_directories(wf_jsonrpc PRIVATE - lib/wf/timer/include - lib/wf/jsonrpc/src -) - -target_include_directories(wf_jsonrpc PUBLIC - lib/wf/jsonrpc/include -) - -set_target_properties(wf_jsonrpc PROPERTIES C_VISIBILITY_PRESET hidden) diff --git a/cmake/wf_timer.cmake b/cmake/wf_timer.cmake deleted file mode 100644 index 8bdecf2..0000000 --- a/cmake/wf_timer.cmake +++ /dev/null @@ -1,18 +0,0 @@ -# timer - -add_library(wf_timer STATIC - lib/wf/timer/src/wf/timer/api.c - lib/wf/timer/src/wf/timer/impl/manager.c - lib/wf/timer/src/wf/timer/impl/timepoint.c - lib/wf/timer/src/wf/timer/impl/timer.c -) - -target_include_directories(wf_timer PRIVATE - lib/wf/timer/src -) - -target_include_directories(wf_timer PUBLIC - lib/wf/timer/include -) - -set_target_properties(wf_timer PROPERTIES C_VISIBILITY_PRESET hidden) diff --git a/codecov.yml b/codecov.yml index 2efff1f..e0d4bce 100644 --- a/codecov.yml +++ b/codecov.yml @@ -1,2 +1,12 @@ ignore: - test + - lib/wf/timer/test + - lib/wf/jsonrpc/test + +parsers: + gcov: + branch_detection: + conditional: yes + loop: yes + method: yes + macro: yes diff --git a/include/webfuse/provider/client_config.h b/include/webfuse/provider/client_config.h index 0c79dd7..fe138f6 100644 --- a/include/webfuse/provider/client_config.h +++ b/include/webfuse/provider/client_config.h @@ -108,6 +108,19 @@ extern WFP_API void wfp_client_config_set_certpath( struct wfp_client_config * config, char const * cert_path); +//------------------------------------------------------------------------------ +/// \brief Sets the path of ca file to verify servers. +/// +/// \note To enable TLS both, private key and certificate, must be specified. +/// Otherwise, TLS is not used. +/// +/// \param config pointer to client configuration +/// \param ca_filepath path of the ca file (pem file) +//------------------------------------------------------------------------------ +extern WFP_API void wfp_client_config_set_ca_filepath( + struct wfp_client_config * config, + char const * ca_filepath); + //------------------------------------------------------------------------------ /// \brief Sets the onconnected handler. /// diff --git a/lib/webfuse/adapter/impl/authenticator.c b/lib/webfuse/adapter/impl/authenticator.c index 0be4b62..562a1dd 100644 --- a/lib/webfuse/adapter/impl/authenticator.c +++ b/lib/webfuse/adapter/impl/authenticator.c @@ -11,13 +11,10 @@ struct wf_impl_authenticator * wf_impl_authenticator_create( void * user_data) { struct wf_impl_authenticator * authenticator = malloc(sizeof(struct wf_impl_authenticator)); - if (NULL != authenticator) - { - authenticator->type = strdup(type); - authenticator->authenticate = authenticate; - authenticator->user_data = user_data; - authenticator->next = NULL; - } + authenticator->type = strdup(type); + authenticator->authenticate = authenticate; + authenticator->user_data = user_data; + authenticator->next = NULL; return authenticator; } diff --git a/lib/webfuse/adapter/impl/filesystem.c b/lib/webfuse/adapter/impl/filesystem.c index 03c2217..0c321e9 100644 --- a/lib/webfuse/adapter/impl/filesystem.c +++ b/lib/webfuse/adapter/impl/filesystem.c @@ -1,5 +1,11 @@ #include "webfuse/adapter/impl/filesystem.h" -#include "webfuse/adapter/impl/operations.h" +#include "webfuse/adapter/impl/operation/context.h" +#include "webfuse/adapter/impl/operation/open.h" +#include "webfuse/adapter/impl/operation/close.h" +#include "webfuse/adapter/impl/operation/read.h" +#include "webfuse/adapter/impl/operation/readdir.h" +#include "webfuse/adapter/impl/operation/getattr.h" +#include "webfuse/adapter/impl/operation/lookup.h" #include "webfuse/adapter/impl/session.h" #include "webfuse/adapter/impl/mountpoint.h" @@ -99,14 +105,11 @@ struct wf_impl_filesystem * wf_impl_filesystem_create( struct wf_mountpoint * mountpoint) { struct wf_impl_filesystem * filesystem = malloc(sizeof(struct wf_impl_filesystem)); - if (NULL != filesystem) + bool success = wf_impl_filesystem_init(filesystem, session, name, mountpoint); + if (!success) { - bool success = wf_impl_filesystem_init(filesystem, session, name, mountpoint); - if (!success) - { - free(filesystem); - filesystem = NULL; - } + free(filesystem); + filesystem = NULL; } return filesystem; diff --git a/lib/webfuse/adapter/impl/filesystem.h b/lib/webfuse/adapter/impl/filesystem.h index 83660d8..791170a 100644 --- a/lib/webfuse/adapter/impl/filesystem.h +++ b/lib/webfuse/adapter/impl/filesystem.h @@ -6,7 +6,7 @@ #endif #include "webfuse/adapter/impl/fuse_wrapper.h" -#include "webfuse/adapter/impl/operations.h" +#include "webfuse/adapter/impl/operation/context.h" #include "webfuse/core/slist.h" #ifdef __cplusplus @@ -24,7 +24,7 @@ struct wf_impl_filesystem struct fuse_args args; struct fuse_session * session; struct fuse_buf buffer; - struct wf_impl_operations_context user_data; + struct wf_impl_operation_context user_data; struct lws * wsi; struct wf_mountpoint * mountpoint; }; diff --git a/lib/webfuse/adapter/impl/operation/close.c b/lib/webfuse/adapter/impl/operation/close.c index dcb65fb..62d1ec3 100644 --- a/lib/webfuse/adapter/impl/operation/close.c +++ b/lib/webfuse/adapter/impl/operation/close.c @@ -1,10 +1,11 @@ -#include "webfuse/adapter/impl/operations.h" +#include "webfuse/adapter/impl/operation/close.h" +#include "webfuse/adapter/impl/operation/context.h" #include #include #include -#include "wf/jsonrpc/proxy.h" +#include "webfuse/core/jsonrpc/proxy.h" #include "webfuse/core/util.h" void wf_impl_operation_close( @@ -12,8 +13,8 @@ void wf_impl_operation_close( fuse_ino_t inode, struct fuse_file_info * file_info) { - struct wf_impl_operations_context * user_data = fuse_req_userdata(request); - struct wf_jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); + struct wf_impl_operation_context * user_data = fuse_req_userdata(request); + struct wf_jsonrpc_proxy * rpc = wf_impl_operation_context_get_proxy(user_data); if (NULL != rpc) { diff --git a/lib/webfuse/adapter/impl/operation/close.h b/lib/webfuse/adapter/impl/operation/close.h new file mode 100644 index 0000000..d254f67 --- /dev/null +++ b/lib/webfuse/adapter/impl/operation/close.h @@ -0,0 +1,20 @@ +#ifndef WF_ADAPTER_IMPL_OPERATION_CLOSE_H +#define WF_ADAPTER_IMPL_OPERATION_CLOSE_H + +#include "webfuse/adapter/impl/fuse_wrapper.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +extern void wf_impl_operation_close( + fuse_req_t request, + fuse_ino_t inode, + struct fuse_file_info * file_info); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/webfuse/adapter/impl/operations.c b/lib/webfuse/adapter/impl/operation/context.c similarity index 65% rename from lib/webfuse/adapter/impl/operations.c rename to lib/webfuse/adapter/impl/operation/context.c index 9ddad3e..a39978f 100644 --- a/lib/webfuse/adapter/impl/operations.c +++ b/lib/webfuse/adapter/impl/operation/context.c @@ -1,10 +1,10 @@ -#include "webfuse/adapter/impl/operations.h" +#include "webfuse/adapter/impl/operation/context.h" #include "webfuse/adapter/impl/session_manager.h" #include "webfuse/adapter/impl/session.h" #include -struct wf_jsonrpc_proxy * wf_impl_operations_context_get_proxy( - struct wf_impl_operations_context * context) +struct wf_jsonrpc_proxy * wf_impl_operation_context_get_proxy( + struct wf_impl_operation_context * context) { struct wf_jsonrpc_proxy * proxy = NULL; diff --git a/lib/webfuse/adapter/impl/operation/context.h b/lib/webfuse/adapter/impl/operation/context.h new file mode 100644 index 0000000..51d886f --- /dev/null +++ b/lib/webfuse/adapter/impl/operation/context.h @@ -0,0 +1,28 @@ +#ifndef WF_ADAPTER_IMPL_OPERATION_CONTEXT_H +#define WF_ADAPTER_IMPL_OPERATION_CONTEXT_H + +#include "webfuse/adapter/impl/fuse_wrapper.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct wf_impl_session; +struct wf_jsonrpc_proxy; + +struct wf_impl_operation_context +{ + struct wf_impl_session * session; + double timeout; + char * name; +}; + +extern struct wf_jsonrpc_proxy * wf_impl_operation_context_get_proxy( + struct wf_impl_operation_context * context); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/lib/webfuse/adapter/impl/operation/getattr.c b/lib/webfuse/adapter/impl/operation/getattr.c index 767b06c..8318dc2 100644 --- a/lib/webfuse/adapter/impl/operation/getattr.c +++ b/lib/webfuse/adapter/impl/operation/getattr.c @@ -1,4 +1,5 @@ -#include "webfuse/adapter/impl/operations.h" +#include "webfuse/adapter/impl/operation/getattr.h" +#include "webfuse/adapter/impl/operation/context.h" #include #include @@ -7,20 +8,11 @@ #include #include -#include "wf/jsonrpc/proxy.h" +#include "webfuse/core/jsonrpc/proxy.h" #include "webfuse/core/json_util.h" #include "webfuse/core/util.h" -struct wf_impl_operation_getattr_context -{ - fuse_req_t request; - fuse_ino_t inode; - double timeout; - uid_t uid; - gid_t gid; -}; - -static void wf_impl_operation_getattr_finished( +void wf_impl_operation_getattr_finished( void * user_data, json_t const * result, json_t const * error) @@ -33,8 +25,7 @@ static void wf_impl_operation_getattr_finished( { json_t * mode_holder = json_object_get(result, "mode"); json_t * type_holder = json_object_get(result, "type"); - if ((NULL != mode_holder) && (json_is_integer(mode_holder)) && - (NULL != type_holder) && (json_is_string(type_holder))) + if ((json_is_integer(mode_holder)) && (json_is_string(type_holder))) { memset(&buffer, 0, sizeof(struct stat)); @@ -82,8 +73,8 @@ void wf_impl_operation_getattr ( struct fuse_file_info * WF_UNUSED_PARAM(file_info)) { struct fuse_ctx const * context = fuse_req_ctx(request); - struct wf_impl_operations_context * user_data = fuse_req_userdata(request); - struct wf_jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); + struct wf_impl_operation_context * user_data = fuse_req_userdata(request); + struct wf_jsonrpc_proxy * rpc = wf_impl_operation_context_get_proxy(user_data); if (NULL != rpc) { diff --git a/lib/webfuse/adapter/impl/operation/getattr.h b/lib/webfuse/adapter/impl/operation/getattr.h new file mode 100644 index 0000000..3908922 --- /dev/null +++ b/lib/webfuse/adapter/impl/operation/getattr.h @@ -0,0 +1,37 @@ +#ifndef WF_ADAPTER_IMPL_OPERATION_GETATTR_H +#define WF_ADAPTER_IMPL_OPERATION_GETATTR_H + +#include "webfuse/adapter/impl/fuse_wrapper.h" + +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct wf_impl_operation_getattr_context +{ + fuse_req_t request; + fuse_ino_t inode; + double timeout; + uid_t uid; + gid_t gid; +}; + +extern void wf_impl_operation_getattr_finished( + void * user_data, + json_t const * result, + json_t const * error); + +extern void wf_impl_operation_getattr ( + fuse_req_t request, + fuse_ino_t inode, + struct fuse_file_info *file_info); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/webfuse/adapter/impl/operation/lookup.c b/lib/webfuse/adapter/impl/operation/lookup.c index 596f7e4..4ebbe3c 100644 --- a/lib/webfuse/adapter/impl/operation/lookup.c +++ b/lib/webfuse/adapter/impl/operation/lookup.c @@ -1,4 +1,5 @@ -#include "webfuse/adapter/impl/operations.h" +#include "webfuse/adapter/impl/operation/lookup.h" +#include "webfuse/adapter/impl/operation/context.h" #include #include @@ -10,19 +11,11 @@ #include -#include "wf/jsonrpc/proxy.h" +#include "webfuse/core/jsonrpc/proxy.h" #include "webfuse/core/json_util.h" #include "webfuse/core/util.h" -struct wf_impl_operation_lookup_context -{ - fuse_req_t request; - double timeout; - uid_t uid; - gid_t gid; -}; - -static void wf_impl_operation_lookup_finished( +void wf_impl_operation_lookup_finished( void * user_data, json_t const * result, json_t const * error @@ -37,9 +30,9 @@ static void wf_impl_operation_lookup_finished( json_t * inode_holder = json_object_get(result, "inode"); json_t * mode_holder = json_object_get(result, "mode"); json_t * type_holder = json_object_get(result, "type"); - if ((NULL != inode_holder) && (json_is_integer(inode_holder)) && - (NULL != mode_holder) && (json_is_integer(mode_holder)) && - (NULL != type_holder) && (json_is_string(type_holder))) + if ((json_is_integer(inode_holder)) && + (json_is_integer(mode_holder)) && + (json_is_string(type_holder))) { memset(&buffer, 0, sizeof(struct stat)); @@ -91,8 +84,8 @@ void wf_impl_operation_lookup ( char const * name) { struct fuse_ctx const * context = fuse_req_ctx(request); - struct wf_impl_operations_context * user_data = fuse_req_userdata(request); - struct wf_jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); + struct wf_impl_operation_context * user_data = fuse_req_userdata(request); + struct wf_jsonrpc_proxy * rpc = wf_impl_operation_context_get_proxy(user_data); if (NULL != rpc) { diff --git a/lib/webfuse/adapter/impl/operation/lookup.h b/lib/webfuse/adapter/impl/operation/lookup.h new file mode 100644 index 0000000..c43e864 --- /dev/null +++ b/lib/webfuse/adapter/impl/operation/lookup.h @@ -0,0 +1,36 @@ +#ifndef WF_ADAPTER_IMPL_OPERATION_LOOKUP_H +#define WF_ADAPTER_IMPL_OPERATION_LOOKUP_H + +#include "webfuse/adapter/impl/fuse_wrapper.h" + +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct wf_impl_operation_lookup_context +{ + fuse_req_t request; + double timeout; + uid_t uid; + gid_t gid; +}; + +extern void wf_impl_operation_lookup_finished( + void * user_data, + json_t const * result, + json_t const * error); + +extern void wf_impl_operation_lookup ( + fuse_req_t req, + fuse_ino_t parent, + char const * name); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/webfuse/adapter/impl/operation/open.c b/lib/webfuse/adapter/impl/operation/open.c index 0c10c45..1b25d99 100644 --- a/lib/webfuse/adapter/impl/operation/open.c +++ b/lib/webfuse/adapter/impl/operation/open.c @@ -1,15 +1,15 @@ -#include "webfuse/adapter/impl/operations.h" +#include "webfuse/adapter/impl/operation/open.h" +#include "webfuse/adapter/impl/operation/context.h" -#include -#include -#include - -#include "wf/jsonrpc/proxy.h" +#include "webfuse/core/jsonrpc/proxy.h" #include "webfuse/core/util.h" #include "webfuse/core/status.h" #include "webfuse/core/json_util.h" -static void wf_impl_operation_open_finished( +#include +#include + +void wf_impl_operation_open_finished( void * user_data, json_t const * result, json_t const * error) @@ -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); } @@ -48,8 +48,8 @@ void wf_impl_operation_open( fuse_ino_t inode, struct fuse_file_info * file_info) { - struct wf_impl_operations_context * user_data = fuse_req_userdata(request); - struct wf_jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); + struct wf_impl_operation_context * user_data = fuse_req_userdata(request); + struct wf_jsonrpc_proxy * rpc = wf_impl_operation_context_get_proxy(user_data); if (NULL != rpc) { diff --git a/lib/webfuse/adapter/impl/operation/open.h b/lib/webfuse/adapter/impl/operation/open.h new file mode 100644 index 0000000..eb40490 --- /dev/null +++ b/lib/webfuse/adapter/impl/operation/open.h @@ -0,0 +1,26 @@ +#ifndef WF_ADAPTER_IMPL_OPERATION_OPEN_H +#define WF_ADAPTER_IMPL_OPERATION_OPEN_H + +#include "webfuse/adapter/impl/fuse_wrapper.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +extern void wf_impl_operation_open( + fuse_req_t request, + fuse_ino_t inode, + struct fuse_file_info * file_info); + +extern void wf_impl_operation_open_finished( + void * user_data, + json_t const * result, + json_t const * error); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/webfuse/adapter/impl/operation/read.c b/lib/webfuse/adapter/impl/operation/read.c index 87330fc..7e2e0f4 100644 --- a/lib/webfuse/adapter/impl/operation/read.c +++ b/lib/webfuse/adapter/impl/operation/read.c @@ -1,18 +1,20 @@ -#include "webfuse/adapter/impl/operations.h" +#include "webfuse/adapter/impl/operation/read.h" +#include "webfuse/adapter/impl/operation/context.h" #include #include #include #include -#include "wf/jsonrpc/proxy.h" +#include "webfuse/core/jsonrpc/proxy.h" #include "webfuse/core/base64.h" #include "webfuse/core/json_util.h" #define WF_MAX_READ_LENGTH 4096 -static char * wf_impl_fill_buffer( +char * wf_impl_fill_buffer( char const * data, + size_t data_size, char const * format, size_t count, wf_status * status) @@ -20,15 +22,26 @@ static char * wf_impl_fill_buffer( *status = WF_GOOD; char * buffer = malloc(count); - if ((NULL != buffer) && (0 < count)) + if (0 < count) { if (0 == strcmp("identity", format)) { - memcpy(buffer, data, count); + if (count == data_size) + { + memcpy(buffer, data, count); + } + else + { + *status = WF_BAD; + } } else if (0 == strcmp("base64", format)) { - wf_base64_decode(data, strlen(data), (uint8_t *) buffer, count); + size_t result = wf_base64_decode(data, data_size, (uint8_t *) buffer, count); + if (result != count) + { + *status = WF_BAD; + } } else { @@ -36,10 +49,16 @@ static char * wf_impl_fill_buffer( } } + if (WF_GOOD != *status) + { + free(buffer); + buffer = NULL; + } + return buffer; } -static void wf_impl_operation_read_finished( +void wf_impl_operation_read_finished( void * user_data, json_t const * result, json_t const * error) @@ -60,10 +79,11 @@ static void wf_impl_operation_read_finished( json_is_integer(count_holder)) { char const * const data = json_string_value(data_holder); + size_t const data_size = json_string_length(data_holder); char const * const format = json_string_value(format_holder); length = (size_t) json_integer_value(count_holder); - buffer = wf_impl_fill_buffer(data, format, length, &status); + buffer = wf_impl_fill_buffer(data, data_size, format, length, &status); } else { @@ -91,8 +111,8 @@ void wf_impl_operation_read( off_t offset, struct fuse_file_info * file_info) { - struct wf_impl_operations_context * user_data = fuse_req_userdata(request); - struct wf_jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); + struct wf_impl_operation_context * user_data = fuse_req_userdata(request); + struct wf_jsonrpc_proxy * rpc = wf_impl_operation_context_get_proxy(user_data); if (NULL != rpc) { diff --git a/lib/webfuse/adapter/impl/operation/read.h b/lib/webfuse/adapter/impl/operation/read.h new file mode 100644 index 0000000..89228b0 --- /dev/null +++ b/lib/webfuse/adapter/impl/operation/read.h @@ -0,0 +1,36 @@ +#ifndef WF_ADAPTER_IMPL_OPERATION_READ_H +#define WF_ADAPTER_IMPL_OPERATION_READ_H + +#include "webfuse/adapter/impl/fuse_wrapper.h" +#include "webfuse/core/status.h" + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +extern void wf_impl_operation_read( + fuse_req_t request, + fuse_ino_t ino, size_t size, off_t off, + struct fuse_file_info *fi); + +extern char * wf_impl_fill_buffer( + char const * data, + size_t data_size, + char const * format, + size_t count, + wf_status * status); + +extern void wf_impl_operation_read_finished( + void * user_data, + json_t const * result, + json_t const * error); + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/webfuse/adapter/impl/operation/readdir.c b/lib/webfuse/adapter/impl/operation/readdir.c index f188084..6d54636 100644 --- a/lib/webfuse/adapter/impl/operation/readdir.c +++ b/lib/webfuse/adapter/impl/operation/readdir.c @@ -1,4 +1,5 @@ -#include "webfuse/adapter/impl/operations.h" +#include "webfuse/adapter/impl/operation/readdir.h" +#include "webfuse/adapter/impl/operation/context.h" #include #include @@ -8,20 +9,13 @@ #include #include -#include "wf/jsonrpc/proxy.h" +#include "webfuse/core/jsonrpc/proxy.h" #include "webfuse/core/util.h" #include "webfuse/core/json_util.h" #define WF_DIRBUFFER_INITIAL_SIZE 1024 -struct wf_impl_operation_readdir_context -{ - fuse_req_t request; - size_t size; - off_t offset; -}; - struct wf_impl_dirbuffer { char * data; @@ -72,7 +66,7 @@ static size_t wf_impl_min(size_t a, size_t b) return (a < b) ? a : b; } -static void wf_impl_operation_readdir_finished( +void wf_impl_operation_readdir_finished( void * user_data, json_t const * result, json_t const * error) @@ -83,31 +77,40 @@ static void wf_impl_operation_readdir_finished( struct wf_impl_dirbuffer buffer; wf_impl_dirbuffer_init(&buffer); - if (NULL != result) + if (json_is_array(result)) { - if (json_is_array(result)) + size_t const count = json_array_size(result); + for(size_t i = 0; i < count; i++) { - bool buffer_full = false; - size_t const count = json_array_size(result); - for(size_t i = 0; (!buffer_full) && (i < count); i++) + json_t * entry =json_array_get(result, i); + if (json_is_object(entry)) { - json_t * entry =json_array_get(result, i); - if (json_is_object(entry)) + json_t * name_holder = json_object_get(entry, "name"); + json_t * inode_holder = json_object_get(entry, "inode"); + + if ((json_is_string(name_holder)) && (json_is_integer(inode_holder))) + { + char const * name = json_string_value(name_holder); + fuse_ino_t entry_inode = (fuse_ino_t) json_integer_value(inode_holder); + wf_impl_dirbuffer_add(context->request, &buffer, name, entry_inode); + } + else { - json_t * name_holder = json_object_get(entry, "name"); - json_t * inode_holder = json_object_get(entry, "inode"); - - if ((NULL != name_holder) && (json_is_string(name_holder)) && - (NULL != inode_holder) && (json_is_integer(inode_holder))) - { - char const * name = json_string_value(name_holder); - fuse_ino_t entry_inode = (fuse_ino_t) json_integer_value(inode_holder); - wf_impl_dirbuffer_add(context->request, &buffer, name, entry_inode); - } + status = WF_BAD_FORMAT; + break; } } + else + { + status = WF_BAD_FORMAT; + break; + } } } + else if (WF_GOOD == status) + { + status = WF_BAD_FORMAT; + } if (WF_GOOD == status) { @@ -138,8 +141,8 @@ void wf_impl_operation_readdir ( off_t offset, struct fuse_file_info * WF_UNUSED_PARAM(file_info)) { - struct wf_impl_operations_context * user_data = fuse_req_userdata(request); - struct wf_jsonrpc_proxy * rpc = wf_impl_operations_context_get_proxy(user_data); + struct wf_impl_operation_context * user_data = fuse_req_userdata(request); + struct wf_jsonrpc_proxy * rpc = wf_impl_operation_context_get_proxy(user_data); if (NULL != rpc) { diff --git a/lib/webfuse/adapter/impl/operation/readdir.h b/lib/webfuse/adapter/impl/operation/readdir.h new file mode 100644 index 0000000..40eb09e --- /dev/null +++ b/lib/webfuse/adapter/impl/operation/readdir.h @@ -0,0 +1,35 @@ +#ifndef WF_ADAPTER_IMPL_OPERATION_READDIR_H +#define WF_ADAPTER_IMPL_OPERATION_READDIR_H + +#include "webfuse/adapter/impl/fuse_wrapper.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct wf_impl_operation_readdir_context +{ + fuse_req_t request; + size_t size; + off_t offset; +}; + +extern void wf_impl_operation_readdir ( + fuse_req_t request, + fuse_ino_t inode, + size_t size, + off_t offset, + struct fuse_file_info *file_info); + +extern void wf_impl_operation_readdir_finished( + void * user_data, + json_t const * result, + json_t const * error); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/webfuse/adapter/impl/operations.h b/lib/webfuse/adapter/impl/operations.h deleted file mode 100644 index 4d23db2..0000000 --- a/lib/webfuse/adapter/impl/operations.h +++ /dev/null @@ -1,60 +0,0 @@ -#ifndef WF_ADAPTER_IMPL_OPERATIONS_H -#define WF_ADAPTER_IMPL_OPERATIONS_H - -#include "webfuse/adapter/impl/fuse_wrapper.h" - -#ifdef __cplusplus -extern "C" { -#endif - -struct wf_impl_session; -struct wf_jsonrpc_proxy; - -struct wf_impl_operations_context -{ - struct wf_impl_session * session; - double timeout; - char * name; -}; - -extern void wf_impl_operation_lookup ( - fuse_req_t req, - fuse_ino_t parent, - char const * name); - -extern void wf_impl_operation_getattr ( - fuse_req_t request, - fuse_ino_t inode, - struct fuse_file_info *file_info); - -extern void wf_impl_operation_readdir ( - fuse_req_t request, - fuse_ino_t inode, - size_t size, - off_t offset, - struct fuse_file_info *file_info); - -extern void wf_impl_operation_open( - fuse_req_t request, - fuse_ino_t inode, - struct fuse_file_info * file_info); - -extern void wf_impl_operation_close( - fuse_req_t request, - fuse_ino_t inode, - struct fuse_file_info * file_info); - -extern void wf_impl_operation_read( - fuse_req_t request, - fuse_ino_t ino, size_t size, off_t off, - struct fuse_file_info *fi); - -extern struct wf_jsonrpc_proxy * wf_impl_operations_context_get_proxy( - struct wf_impl_operations_context * context); - -#ifdef __cplusplus -} -#endif - -#endif - diff --git a/lib/webfuse/adapter/impl/server.c b/lib/webfuse/adapter/impl/server.c index f6e1f63..8f9a01d 100644 --- a/lib/webfuse/adapter/impl/server.c +++ b/lib/webfuse/adapter/impl/server.c @@ -83,13 +83,10 @@ struct wf_server * wf_impl_server_create( if (wf_impl_mountpoint_factory_isvalid(&config->mountpoint_factory)) { server = malloc(sizeof(struct wf_server)); - if (NULL != server) - { - wf_impl_server_protocol_init(&server->protocol, &config->mountpoint_factory); - wf_impl_server_config_clone(config, &server->config); - wf_impl_authenticators_move(&server->config.authenticators, &server->protocol.authenticators); - server->context = wf_impl_server_context_create(server); - } + wf_impl_server_protocol_init(&server->protocol, &config->mountpoint_factory); + wf_impl_server_config_clone(config, &server->config); + wf_impl_authenticators_move(&server->config.authenticators, &server->protocol.authenticators); + server->context = wf_impl_server_context_create(server); } return server; diff --git a/lib/webfuse/adapter/impl/server_config.c b/lib/webfuse/adapter/impl/server_config.c index bcf022e..ab214f3 100644 --- a/lib/webfuse/adapter/impl/server_config.c +++ b/lib/webfuse/adapter/impl/server_config.c @@ -56,10 +56,7 @@ void wf_impl_server_config_clone( struct wf_server_config * wf_impl_server_config_create(void) { struct wf_server_config * config = malloc(sizeof(struct wf_server_config)); - if (NULL != config) - { - wf_impl_server_config_init(config); - } + wf_impl_server_config_init(config); return config; } diff --git a/lib/webfuse/adapter/impl/server_protocol.c b/lib/webfuse/adapter/impl/server_protocol.c index 927d1cb..d97c9a8 100644 --- a/lib/webfuse/adapter/impl/server_protocol.c +++ b/lib/webfuse/adapter/impl/server_protocol.c @@ -11,9 +11,9 @@ #include "webfuse/adapter/impl/credentials.h" #include "webfuse/core/status_intern.h" -#include "wf/jsonrpc/request.h" -#include "wf/timer/manager.h" -#include "wf/timer/timer.h" +#include "webfuse/core/jsonrpc/request.h" +#include "webfuse/core/timer/manager.h" +#include "webfuse/core/timer/timer.h" static int wf_impl_server_protocol_callback( struct lws * wsi, @@ -23,13 +23,10 @@ static int wf_impl_server_protocol_callback( size_t len) { struct lws_protocols const * ws_protocol = lws_get_protocol(wsi); - if (NULL == ws_protocol) - { - return 0; - } + if (NULL == ws_protocol) { return 0; } + if (ws_protocol->callback != &wf_impl_server_protocol_callback) { return 0; } struct wf_server_protocol * protocol = ws_protocol->user; - wf_timer_manager_check(protocol->timer_manager); struct wf_impl_session * session = wf_impl_session_manager_get(&protocol->session_manager, wsi); @@ -85,14 +82,11 @@ struct wf_server_protocol * wf_impl_server_protocol_create( void * create_mountpoint_context) { struct wf_server_protocol * protocol = malloc(sizeof(struct wf_server_protocol)); - if (NULL != protocol) - { - struct wf_impl_mountpoint_factory mountpoint_factory; - wf_impl_mountpoint_factory_init(&mountpoint_factory, - create_mountpoint, create_mountpoint_context); + struct wf_impl_mountpoint_factory mountpoint_factory; + wf_impl_mountpoint_factory_init(&mountpoint_factory, + create_mountpoint, create_mountpoint_context); - wf_impl_server_protocol_init(protocol, &mountpoint_factory); - } + wf_impl_server_protocol_init(protocol, &mountpoint_factory); return protocol; diff --git a/lib/webfuse/adapter/impl/server_protocol.h b/lib/webfuse/adapter/impl/server_protocol.h index df98c8f..269e3be 100644 --- a/lib/webfuse/adapter/impl/server_protocol.h +++ b/lib/webfuse/adapter/impl/server_protocol.h @@ -4,8 +4,8 @@ #include "webfuse/adapter/impl/authenticators.h" #include "webfuse/adapter/impl/mountpoint_factory.h" #include "webfuse/adapter/impl/session_manager.h" -#include "wf/jsonrpc/proxy.h" -#include "wf/jsonrpc/server.h" +#include "webfuse/core/jsonrpc/proxy.h" +#include "webfuse/core/jsonrpc/server.h" #ifndef __cplusplus #include diff --git a/lib/webfuse/adapter/impl/session.c b/lib/webfuse/adapter/impl/session.c index e1fdd87..6bacd10 100644 --- a/lib/webfuse/adapter/impl/session.c +++ b/lib/webfuse/adapter/impl/session.c @@ -8,9 +8,9 @@ #include "webfuse/core/container_of.h" #include "webfuse/core/util.h" -#include "wf/jsonrpc/proxy.h" -#include "wf/jsonrpc/request.h" -#include "wf/jsonrpc/response.h" +#include "webfuse/core/jsonrpc/proxy.h" +#include "webfuse/core/jsonrpc/request.h" +#include "webfuse/core/jsonrpc/response.h" #include #include @@ -51,18 +51,15 @@ struct wf_impl_session * wf_impl_session_create( { struct wf_impl_session * session = malloc(sizeof(struct wf_impl_session)); - if (NULL != session) - { - wf_slist_init(&session->filesystems); - - session->wsi = wsi; - session->is_authenticated = false; - session->authenticators = authenticators; - session->server = server; - session->mountpoint_factory = mountpoint_factory; - session->rpc = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &wf_impl_session_send, session); - wf_slist_init(&session->messages); - } + wf_slist_init(&session->filesystems); + + session->wsi = wsi; + session->is_authenticated = false; + session->authenticators = authenticators; + session->server = server; + session->mountpoint_factory = mountpoint_factory; + session->rpc = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &wf_impl_session_send, session); + wf_slist_init(&session->messages); return session; } diff --git a/lib/webfuse/adapter/impl/session.h b/lib/webfuse/adapter/impl/session.h index 82b042f..1046a2e 100644 --- a/lib/webfuse/adapter/impl/session.h +++ b/lib/webfuse/adapter/impl/session.h @@ -13,8 +13,8 @@ using std::size_t; #include "webfuse/adapter/impl/filesystem.h" #include "webfuse/core/slist.h" -#include "wf/jsonrpc/proxy.h" -#include "wf/jsonrpc/server.h" +#include "webfuse/core/jsonrpc/proxy.h" +#include "webfuse/core/jsonrpc/server.h" #ifdef __cplusplus extern "C" diff --git a/lib/webfuse/adapter/impl/session_manager.c b/lib/webfuse/adapter/impl/session_manager.c index cc41fa0..a872d54 100644 --- a/lib/webfuse/adapter/impl/session_manager.c +++ b/lib/webfuse/adapter/impl/session_manager.c @@ -33,10 +33,7 @@ struct wf_impl_session * wf_impl_session_manager_add( { struct wf_impl_session * session = wf_impl_session_create( wsi, authenticators, timer_manager, server, mountpoint_factory); - if (NULL != session) - { - wf_slist_append(&manager->sessions, &session->item); - } + wf_slist_append(&manager->sessions, &session->item); return session; } diff --git a/lib/webfuse/core/json_util.c b/lib/webfuse/core/json_util.c index 48fe956..535e07e 100644 --- a/lib/webfuse/core/json_util.c +++ b/lib/webfuse/core/json_util.c @@ -1,5 +1,4 @@ #include "webfuse/core/json_util.h" -#include "wf/jsonrpc/status.h" int wf_impl_json_get_int(json_t const * object, char const * key, int default_value) { @@ -14,25 +13,6 @@ int wf_impl_json_get_int(json_t const * object, char const * key, int default_va return result; } -static wf_status wf_impl_jsonrc_code_to_status(int code) -{ - switch (code) - { - case WF_JSONRPC_GOOD: - return WF_GOOD; - case WF_JSONRPC_BAD: - return WF_BAD; - case WF_JSONRPC_BAD_BUSY: - return WF_BAD_BUSY; - case WF_JSONRPC_BAD_TIMEOUT: - return WF_BAD_TIMEOUT; - case WF_JSONRPC_BAD_FORMAT: - return WF_BAD_FORMAT; - default: - return (wf_status) code; - } -} - wf_status wf_impl_jsonrpc_get_status( json_t const * error) @@ -40,8 +20,7 @@ wf_impl_jsonrpc_get_status( wf_status status = WF_GOOD; if (NULL != error) { - int code = wf_impl_json_get_int(error, "code", WF_BAD_FORMAT); - status = wf_impl_jsonrc_code_to_status(code); + status = wf_impl_json_get_int(error, "code", WF_BAD_FORMAT); } return status; diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/error.c b/lib/webfuse/core/jsonrpc/error.c similarity index 73% rename from lib/wf/jsonrpc/src/wf/jsonrpc/impl/error.c rename to lib/webfuse/core/jsonrpc/error.c index eba2e86..ad74a58 100644 --- a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/error.c +++ b/lib/webfuse/core/jsonrpc/error.c @@ -1,7 +1,7 @@ -#include "wf/jsonrpc/impl/error.h" +#include "webfuse/core/jsonrpc/error.h" json_t * -wf_jsonrpc_impl_error( +wf_jsonrpc_error( int code, char const * message) { @@ -13,13 +13,13 @@ wf_jsonrpc_impl_error( } void -wf_jsonrpc_impl_propate_error( +wf_jsonrpc_propate_error( wf_jsonrpc_proxy_finished_fn * finised, void * user_data, int code, char const * message) { - json_t * error = wf_jsonrpc_impl_error(code, message); + json_t * error = wf_jsonrpc_error(code, message); finised(user_data, NULL, error); json_decref(error); diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/error.h b/lib/webfuse/core/jsonrpc/error.h similarity index 63% rename from lib/wf/jsonrpc/src/wf/jsonrpc/impl/error.h rename to lib/webfuse/core/jsonrpc/error.h index fda0f89..2edfd92 100644 --- a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/error.h +++ b/lib/webfuse/core/jsonrpc/error.h @@ -1,8 +1,8 @@ -#ifndef WF_JSONRPC_IMPL_ERROR_H -#define WF_JSONRPC_IMPL_ERROR_H +#ifndef WF_JSONRPC_ERROR_H +#define WF_JSONRPC_ERROR_H #include -#include "wf/jsonrpc/proxy_finished_fn.h" +#include "webfuse/core/jsonrpc/proxy_finished_fn.h" #ifdef __cplusplus extern "C" @@ -10,12 +10,12 @@ extern "C" #endif extern json_t * -wf_jsonrpc_impl_error( +wf_jsonrpc_error( int code, char const * message); extern void -wf_jsonrpc_impl_propate_error( +wf_jsonrpc_propate_error( wf_jsonrpc_proxy_finished_fn * finised, void * user_data, int code, diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.c b/lib/webfuse/core/jsonrpc/method.c similarity index 51% rename from lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.c rename to lib/webfuse/core/jsonrpc/method.c index 87b3ff1..a184dca 100644 --- a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.c +++ b/lib/webfuse/core/jsonrpc/method.c @@ -1,25 +1,22 @@ -#include "wf/jsonrpc/impl/method.h" +#include "webfuse/core/jsonrpc/method.h" #include #include -struct wf_jsonrpc_method * wf_jsonrpc_impl_method_create( +struct wf_jsonrpc_method * wf_jsonrpc_method_create( char const * method_name, wf_jsonrpc_method_invoke_fn * invoke, void * user_data) { struct wf_jsonrpc_method * method = malloc(sizeof(struct wf_jsonrpc_method)); - if (NULL != method) - { - method->next = NULL; - method->name = strdup(method_name); - method->invoke = invoke; - method->user_data = user_data; - } + method->next = NULL; + method->name = strdup(method_name); + method->invoke = invoke; + method->user_data = user_data; return method; } -void wf_jsonrpc_impl_method_dispose( +void wf_jsonrpc_method_dispose( struct wf_jsonrpc_method * method) { free(method->name); diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.h b/lib/webfuse/core/jsonrpc/method.h similarity index 70% rename from lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.h rename to lib/webfuse/core/jsonrpc/method.h index d0e42f0..e196bff 100644 --- a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.h +++ b/lib/webfuse/core/jsonrpc/method.h @@ -1,7 +1,7 @@ -#ifndef WF_JSONRPC_IMPL_METHOD_H -#define WF_JSONRPC_IMPL_METHOD_H +#ifndef WF_JSONRPC_METHOD_H +#define WF_JSONRPC_METHOD_H -#include "wf/jsonrpc/method_invoke_fn.h" +#include "webfuse/core/jsonrpc/method_invoke_fn.h" #ifdef __cplusplus extern "C" @@ -17,13 +17,13 @@ struct wf_jsonrpc_method }; extern struct wf_jsonrpc_method * -wf_jsonrpc_impl_method_create( +wf_jsonrpc_method_create( char const * method_name, wf_jsonrpc_method_invoke_fn * invoke, void * user_data); extern void -wf_jsonrpc_impl_method_dispose( +wf_jsonrpc_method_dispose( struct wf_jsonrpc_method * method); #ifdef __cplusplus diff --git a/lib/wf/jsonrpc/include/wf/jsonrpc/method_invoke_fn.h b/lib/webfuse/core/jsonrpc/method_invoke_fn.h similarity index 92% rename from lib/wf/jsonrpc/include/wf/jsonrpc/method_invoke_fn.h rename to lib/webfuse/core/jsonrpc/method_invoke_fn.h index d7a9cc8..046dfb6 100644 --- a/lib/wf/jsonrpc/include/wf/jsonrpc/method_invoke_fn.h +++ b/lib/webfuse/core/jsonrpc/method_invoke_fn.h @@ -1,7 +1,6 @@ #ifndef WF_JSONRPC_METHOD_INVOKE_FN_H #define WF_JSONRPC_METHOD_INVOKE_FN_H -#include #include #ifdef __cplusplus @@ -17,7 +16,6 @@ typedef void wf_jsonrpc_method_invoke_fn( json_t * params, void * user_data); - #ifdef __cplusplus } #endif diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.c b/lib/webfuse/core/jsonrpc/proxy.c similarity index 74% rename from lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.c rename to lib/webfuse/core/jsonrpc/proxy.c index a2b9816..ee5fc0d 100644 --- a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.c +++ b/lib/webfuse/core/jsonrpc/proxy.c @@ -1,37 +1,34 @@ -#include "wf/jsonrpc/impl/proxy.h" -#include "wf/jsonrpc/impl/response.h" -#include "wf/jsonrpc/impl/error.h" -#include "wf/jsonrpc/status.h" +#include "webfuse/core/jsonrpc/proxy_intern.h" +#include "webfuse/core/jsonrpc/response_intern.h" +#include "webfuse/core/jsonrpc/error.h" +#include "webfuse/core/status.h" -#include +#include "webfuse/core/timer/timer.h" #include #include struct wf_jsonrpc_proxy * -wf_jsonrpc_impl_proxy_create( +wf_jsonrpc_proxy_create( struct wf_timer_manager * manager, int timeout, wf_jsonrpc_send_fn * send, void * user_data) { struct wf_jsonrpc_proxy * proxy = malloc(sizeof(struct wf_jsonrpc_proxy)); - if (NULL != proxy) - { - wf_jsonrpc_impl_proxy_init(proxy, manager, timeout, send, user_data); - } + wf_jsonrpc_proxy_init(proxy, manager, timeout, send, user_data); return proxy; } -void wf_jsonrpc_impl_proxy_dispose( +void wf_jsonrpc_proxy_dispose( struct wf_jsonrpc_proxy * proxy) { - wf_jsonrpc_impl_proxy_cleanup(proxy); + wf_jsonrpc_proxy_cleanup(proxy); free(proxy); } -static void wf_jsonrpc_impl_proxy_on_timeout( +static void wf_jsonrpc_proxy_on_timeout( struct wf_timer * timer, void * proxy_ptr) { struct wf_jsonrpc_proxy * proxy = proxy_ptr; @@ -47,11 +44,11 @@ static void wf_jsonrpc_impl_proxy_on_timeout( proxy->request.finished = NULL; wf_timer_cancel(timer); - wf_jsonrpc_impl_propate_error(finished, user_data, WF_JSONRPC_BAD_TIMEOUT, "Timeout"); + wf_jsonrpc_propate_error(finished, user_data, WF_BAD_TIMEOUT, "Timeout"); } } -static json_t * wf_jsonrpc_impl_request_create( +static json_t * wf_jsonrpc_request_create( char const * method, int id, char const * param_info, @@ -101,7 +98,7 @@ static json_t * wf_jsonrpc_impl_request_create( return request; } -void wf_jsonrpc_impl_proxy_init( +void wf_jsonrpc_proxy_init( struct wf_jsonrpc_proxy * proxy, struct wf_timer_manager * timeout_manager, int timeout, @@ -113,10 +110,10 @@ void wf_jsonrpc_impl_proxy_init( proxy->user_data = user_data; proxy->request.is_pending = false; proxy->request.timer = wf_timer_create(timeout_manager, - &wf_jsonrpc_impl_proxy_on_timeout, proxy); + &wf_jsonrpc_proxy_on_timeout, proxy); } -void wf_jsonrpc_impl_proxy_cleanup( +void wf_jsonrpc_proxy_cleanup( struct wf_jsonrpc_proxy * proxy) { if (proxy->request.is_pending) @@ -130,20 +127,19 @@ void wf_jsonrpc_impl_proxy_cleanup( proxy->request.id = 0; wf_timer_cancel(proxy->request.timer); - wf_jsonrpc_impl_propate_error(finished, user_data, WF_JSONRPC_BAD, "Bad: cancelled pending request during shutdown"); + wf_jsonrpc_propate_error(finished, user_data, WF_BAD, "Bad: cancelled pending request during shutdown"); } wf_timer_dispose(proxy->request.timer); } -void wf_jsonrpc_impl_proxy_invoke( +void wf_jsonrpc_proxy_vinvoke( struct wf_jsonrpc_proxy * proxy, wf_jsonrpc_proxy_finished_fn * finished, void * user_data, char const * method_name, char const * param_info, - va_list args -) + va_list args) { if (!proxy->request.is_pending) { @@ -153,7 +149,7 @@ void wf_jsonrpc_impl_proxy_invoke( proxy->request.id = 42; wf_timer_start(proxy->request.timer, proxy->timeout); - json_t * request = wf_jsonrpc_impl_request_create(method_name, proxy->request.id, param_info, args); + json_t * request = wf_jsonrpc_request_create(method_name, proxy->request.id, param_info, args); bool const is_send = ((NULL != request) && (proxy->send(request, proxy->user_data))); if (!is_send) @@ -164,7 +160,7 @@ void wf_jsonrpc_impl_proxy_invoke( proxy->request.id = 0; wf_timer_cancel(proxy->request.timer); - wf_jsonrpc_impl_propate_error(finished, user_data, WF_JSONRPC_BAD, "Bad: requenst is not sent"); + wf_jsonrpc_propate_error(finished, user_data, WF_BAD, "Bad: requenst is not sent"); } if (NULL != request) @@ -174,18 +170,18 @@ void wf_jsonrpc_impl_proxy_invoke( } else { - wf_jsonrpc_impl_propate_error(finished, user_data, WF_JSONRPC_BAD_BUSY, "Busy"); + wf_jsonrpc_propate_error(finished, user_data, WF_BAD_BUSY, "Busy"); } } -extern void wf_jsonrpc_impl_proxy_notify( +extern void wf_jsonrpc_proxy_vnotify( struct wf_jsonrpc_proxy * proxy, char const * method_name, char const * param_info, - va_list args -) -{ - json_t * request = wf_jsonrpc_impl_request_create(method_name, 0, param_info, args); + va_list args) +{ + json_t * request = wf_jsonrpc_request_create(method_name, 0, param_info, args); + if (NULL != request) { proxy->send(request, proxy->user_data); @@ -194,12 +190,12 @@ extern void wf_jsonrpc_impl_proxy_notify( } -void wf_jsonrpc_impl_proxy_onresult( +void wf_jsonrpc_proxy_onresult( struct wf_jsonrpc_proxy * proxy, json_t * message) { struct wf_jsonrpc_response response; - wf_jsonrpc_impl_response_init(&response, message); + wf_jsonrpc_response_init(&response, message); if ((proxy->request.is_pending) && (response.id == proxy->request.id)) { @@ -215,6 +211,6 @@ void wf_jsonrpc_impl_proxy_onresult( finished(user_data, response.result, response.error); } - wf_jsonrpc_impl_response_cleanup(&response); + wf_jsonrpc_response_cleanup(&response); } diff --git a/lib/wf/jsonrpc/include/wf/jsonrpc/proxy.h b/lib/webfuse/core/jsonrpc/proxy.h similarity index 80% rename from lib/wf/jsonrpc/include/wf/jsonrpc/proxy.h rename to lib/webfuse/core/jsonrpc/proxy.h index 66fd1a5..8a69936 100644 --- a/lib/wf/jsonrpc/include/wf/jsonrpc/proxy.h +++ b/lib/webfuse/core/jsonrpc/proxy.h @@ -12,9 +12,8 @@ using std::size_t; #endif #include -#include -#include -#include +#include "webfuse/core/jsonrpc/send_fn.h" +#include "webfuse/core/jsonrpc/proxy_finished_fn.h" #ifdef __cplusplus extern "C" { @@ -23,14 +22,14 @@ extern "C" { struct wf_jsonrpc_proxy; struct wf_timer_manager; -extern WF_JSONRPC_API struct wf_jsonrpc_proxy * +extern struct wf_jsonrpc_proxy * wf_jsonrpc_proxy_create( struct wf_timer_manager * manager, int timeout, wf_jsonrpc_send_fn * send, void * user_data); -extern WF_JSONRPC_API void wf_jsonrpc_proxy_dispose( +extern void wf_jsonrpc_proxy_dispose( struct wf_jsonrpc_proxy * proxy); //------------------------------------------------------------------------------ @@ -47,7 +46,7 @@ extern WF_JSONRPC_API void wf_jsonrpc_proxy_dispose( /// \param param_info types of the param (s = string, i = integer, j = json) /// \param ... params //------------------------------------------------------------------------------ -extern WF_JSONRPC_API void wf_jsonrpc_proxy_invoke( +extern void wf_jsonrpc_proxy_invoke( struct wf_jsonrpc_proxy * proxy, wf_jsonrpc_proxy_finished_fn * finished, void * user_data, @@ -56,14 +55,14 @@ extern WF_JSONRPC_API void wf_jsonrpc_proxy_invoke( ... ); -extern WF_JSONRPC_API void wf_jsonrpc_proxy_notify( +extern void wf_jsonrpc_proxy_notify( struct wf_jsonrpc_proxy * proxy, char const * method_name, char const * param_info, ... ); -extern WF_JSONRPC_API void wf_jsonrpc_proxy_onresult( +extern void wf_jsonrpc_proxy_onresult( struct wf_jsonrpc_proxy * proxy, json_t * message); diff --git a/lib/wf/jsonrpc/include/wf/jsonrpc/proxy_finished_fn.h b/lib/webfuse/core/jsonrpc/proxy_finished_fn.h similarity index 100% rename from lib/wf/jsonrpc/include/wf/jsonrpc/proxy_finished_fn.h rename to lib/webfuse/core/jsonrpc/proxy_finished_fn.h diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.h b/lib/webfuse/core/jsonrpc/proxy_intern.h similarity index 54% rename from lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.h rename to lib/webfuse/core/jsonrpc/proxy_intern.h index 60d2f7a..fd284a0 100644 --- a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.h +++ b/lib/webfuse/core/jsonrpc/proxy_intern.h @@ -1,15 +1,15 @@ -#ifndef WF_JSONRPC_IMPL_PROXY_H -#define WF_JSONRPC_IMPL_PROXY_H +#ifndef WF_JSONRPC_PROXY_INTERN_H +#define WF_JSONRPC_PROXY_INTERN_H -#include "wf/jsonrpc/proxy_finished_fn.h" -#include "wf/jsonrpc/send_fn.h" +#include "webfuse/core/jsonrpc/proxy.h" +#include "webfuse/core/jsonrpc/proxy_finished_fn.h" +#include "webfuse/core/jsonrpc/send_fn.h" #ifdef __cplusplus extern "C" { #endif -struct wf_timer_manager; struct wf_timer; struct wf_jsonrpc_request @@ -30,7 +30,7 @@ struct wf_jsonrpc_proxy }; extern void -wf_jsonrpc_impl_proxy_init( +wf_jsonrpc_proxy_init( struct wf_jsonrpc_proxy * proxy, struct wf_timer_manager * manager, int timeout, @@ -38,44 +38,22 @@ wf_jsonrpc_impl_proxy_init( void * user_data); extern void -wf_jsonrpc_impl_proxy_cleanup( +wf_jsonrpc_proxy_cleanup( struct wf_jsonrpc_proxy * proxy); -extern struct wf_jsonrpc_proxy * -wf_jsonrpc_impl_proxy_create( - struct wf_timer_manager * manager, - int timeout, - wf_jsonrpc_send_fn * send, - void * user_data); - -extern void -wf_jsonrpc_impl_proxy_dispose( - struct wf_jsonrpc_proxy * proxy); - - -extern void -wf_jsonrpc_impl_proxy_invoke( +extern void wf_jsonrpc_proxy_vinvoke( struct wf_jsonrpc_proxy * proxy, wf_jsonrpc_proxy_finished_fn * finished, void * user_data, char const * method_name, char const * param_info, - va_list args -); + va_list args); -extern void -wf_jsonrpc_impl_proxy_notify( +extern void wf_jsonrpc_proxy_vnotify( struct wf_jsonrpc_proxy * proxy, char const * method_name, char const * param_info, - va_list args -); - -extern void -wf_jsonrpc_impl_proxy_onresult( - struct wf_jsonrpc_proxy * proxy, - json_t * message); - + va_list args); #ifdef __cplusplus } diff --git a/lib/webfuse/core/jsonrpc/proxy_variadic.c b/lib/webfuse/core/jsonrpc/proxy_variadic.c new file mode 100644 index 0000000..2447511 --- /dev/null +++ b/lib/webfuse/core/jsonrpc/proxy_variadic.c @@ -0,0 +1,28 @@ +#include "webfuse/core/jsonrpc/proxy_intern.h" + +void wf_jsonrpc_proxy_invoke( + struct wf_jsonrpc_proxy * proxy, + wf_jsonrpc_proxy_finished_fn * finished, + void * user_data, + char const * method_name, + char const * param_info, + ...) +{ + va_list args; + va_start(args, param_info); + wf_jsonrpc_proxy_vinvoke(proxy, finished, user_data, method_name, param_info, args); + va_end(args); +} + +extern void wf_jsonrpc_proxy_notify( + struct wf_jsonrpc_proxy * proxy, + char const * method_name, + char const * param_info, + ... +) +{ + va_list args; + va_start(args, param_info); + wf_jsonrpc_proxy_vnotify(proxy, method_name, param_info, args); + va_end(args); +} diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.c b/lib/webfuse/core/jsonrpc/request.c similarity index 70% rename from lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.c rename to lib/webfuse/core/jsonrpc/request.c index e46f19e..30212bd 100644 --- a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.c +++ b/lib/webfuse/core/jsonrpc/request.c @@ -1,5 +1,5 @@ -#include "wf/jsonrpc/impl/request.h" -#include "wf/jsonrpc/impl/error.h" +#include "webfuse/core/jsonrpc/request.h" +#include "webfuse/core/jsonrpc/error.h" #include struct wf_jsonrpc_request @@ -10,7 +10,7 @@ struct wf_jsonrpc_request }; bool -wf_jsonrpc_impl_is_request( +wf_jsonrpc_is_request( json_t * message) { json_t * id = json_object_get(message, "id"); @@ -23,31 +23,28 @@ wf_jsonrpc_impl_is_request( struct wf_jsonrpc_request * -wf_jsonrpc_impl_request_create( +wf_jsonrpc_request_create( int id, wf_jsonrpc_send_fn * send, void * user_data) { struct wf_jsonrpc_request * request = malloc(sizeof(struct wf_jsonrpc_request)); - if (NULL != request) - { - request->id = id; - request->send = send; - request->user_data = user_data; - } + request->id = id; + request->send = send; + request->user_data = user_data; return request; } void -wf_jsonrpc_impl_request_dispose( +wf_jsonrpc_request_dispose( struct wf_jsonrpc_request * request) { free(request); } void * -wf_jsonrpc_impl_request_get_userdata( +wf_jsonrpc_request_get_userdata( struct wf_jsonrpc_request * request) { return request->user_data; @@ -55,7 +52,7 @@ wf_jsonrpc_impl_request_get_userdata( void -wf_jsonrpc_impl_respond( +wf_jsonrpc_respond( struct wf_jsonrpc_request * request, json_t * result) { @@ -65,20 +62,20 @@ wf_jsonrpc_impl_respond( request->send(response, request->user_data); json_decref(response); - wf_jsonrpc_impl_request_dispose(request); + wf_jsonrpc_request_dispose(request); } -void wf_jsonrpc_impl_respond_error( +void wf_jsonrpc_respond_error( struct wf_jsonrpc_request * request, int code, char const * message) { json_t * response = json_object(); - json_object_set_new(response, "error", wf_jsonrpc_impl_error(code, message)); + json_object_set_new(response, "error", wf_jsonrpc_error(code, message)); json_object_set_new(response, "id", json_integer(request->id)); request->send(response, request->user_data); json_decref(response); - wf_jsonrpc_impl_request_dispose(request); + wf_jsonrpc_request_dispose(request); } diff --git a/lib/wf/jsonrpc/include/wf/jsonrpc/request.h b/lib/webfuse/core/jsonrpc/request.h similarity index 64% rename from lib/wf/jsonrpc/include/wf/jsonrpc/request.h rename to lib/webfuse/core/jsonrpc/request.h index 73295e6..234ec62 100644 --- a/lib/wf/jsonrpc/include/wf/jsonrpc/request.h +++ b/lib/webfuse/core/jsonrpc/request.h @@ -12,8 +12,7 @@ using std::size_t; #endif #include -#include -#include "wf/jsonrpc/send_fn.h" +#include "webfuse/core/jsonrpc/send_fn.h" #ifdef __cplusplus extern "C" @@ -22,26 +21,26 @@ extern "C" struct wf_jsonrpc_request; -extern WF_JSONRPC_API bool wf_jsonrpc_is_request( +extern bool wf_jsonrpc_is_request( json_t * message); -extern WF_JSONRPC_API struct wf_jsonrpc_request * +extern struct wf_jsonrpc_request * wf_jsonrpc_request_create( int id, wf_jsonrpc_send_fn * send, void * user_data); -extern WF_JSONRPC_API void wf_jsonrpc_request_dispose( +extern void wf_jsonrpc_request_dispose( struct wf_jsonrpc_request * request); -extern WF_JSONRPC_API void * wf_jsonrpc_request_get_userdata( +extern void * wf_jsonrpc_request_get_userdata( struct wf_jsonrpc_request * request); -extern WF_JSONRPC_API void wf_jsonrpc_respond( +extern void wf_jsonrpc_respond( struct wf_jsonrpc_request * request, json_t * result); -extern WF_JSONRPC_API void wf_jsonrpc_respond_error( +extern void wf_jsonrpc_respond_error( struct wf_jsonrpc_request * request, int code, char const * message); diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/response.c b/lib/webfuse/core/jsonrpc/response.c similarity index 70% rename from lib/wf/jsonrpc/src/wf/jsonrpc/impl/response.c rename to lib/webfuse/core/jsonrpc/response.c index 07171fb..69f7033 100644 --- a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/response.c +++ b/lib/webfuse/core/jsonrpc/response.c @@ -1,9 +1,9 @@ -#include "wf/jsonrpc/impl/response.h" -#include "wf/jsonrpc/impl/error.h" -#include "wf/jsonrpc/status.h" +#include "webfuse/core/jsonrpc/response_intern.h" +#include "webfuse/core/jsonrpc/error.h" +#include "webfuse/core/status.h" bool -wf_jsonrpc_impl_is_response( +wf_jsonrpc_is_response( json_t * message) { json_t * id = json_object_get(message, "id"); @@ -16,7 +16,7 @@ wf_jsonrpc_impl_is_response( void -wf_jsonrpc_impl_response_init( +wf_jsonrpc_response_init( struct wf_jsonrpc_response * result, json_t * response) { @@ -25,9 +25,9 @@ wf_jsonrpc_impl_response_init( result->error = NULL; json_t * id_holder = json_object_get(response, "id"); - if ((NULL == id_holder) || (!json_is_integer(id_holder))) + if (!json_is_integer(id_holder)) { - result->error = wf_jsonrpc_impl_error(WF_JSONRPC_BAD_FORMAT, "invalid format: missing id"); + result->error = wf_jsonrpc_error(WF_BAD_FORMAT, "invalid format: missing id"); return; } @@ -47,13 +47,13 @@ wf_jsonrpc_impl_response_init( } else { - result->error = wf_jsonrpc_impl_error(WF_JSONRPC_BAD_FORMAT, "invalid format: invalid error object"); + result->error = wf_jsonrpc_error(WF_BAD_FORMAT, "invalid format: invalid error object"); } } } void -wf_jsonrpc_impl_response_cleanup( +wf_jsonrpc_response_cleanup( struct wf_jsonrpc_response * response) { if (NULL != response->result) diff --git a/lib/wf/jsonrpc/include/wf/jsonrpc/response.h b/lib/webfuse/core/jsonrpc/response.h similarity index 74% rename from lib/wf/jsonrpc/include/wf/jsonrpc/response.h rename to lib/webfuse/core/jsonrpc/response.h index abe2011..0bf0561 100644 --- a/lib/wf/jsonrpc/include/wf/jsonrpc/response.h +++ b/lib/webfuse/core/jsonrpc/response.h @@ -6,14 +6,13 @@ #endif #include -#include #ifdef __cplusplus extern "C" { #endif -extern WF_JSONRPC_API bool wf_jsonrpc_is_response( +extern bool wf_jsonrpc_is_response( json_t * message); #ifdef __cplusplus diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/response.h b/lib/webfuse/core/jsonrpc/response_intern.h similarity index 57% rename from lib/wf/jsonrpc/src/wf/jsonrpc/impl/response.h rename to lib/webfuse/core/jsonrpc/response_intern.h index 3e1e02e..137a615 100644 --- a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/response.h +++ b/lib/webfuse/core/jsonrpc/response_intern.h @@ -1,16 +1,15 @@ -#ifndef WF_JSONRPC_IMPL_RESPONSE_H -#define WF_JSONRPC_IMPL_RESPONSE_H +#ifndef WF_JSONRPC_RESPONSE_INTERN_H +#define WF_JSONRPC_RESPONSE_INTERN_H + +#include "webfuse/core/jsonrpc/response.h" #ifndef __cplusplus -#include #include #else #include using std::size_t; #endif -#include - #ifdef __cplusplus extern "C" { #endif @@ -22,14 +21,11 @@ struct wf_jsonrpc_response int id; }; -extern bool wf_jsonrpc_impl_is_response( - json_t * message); - -extern void wf_jsonrpc_impl_response_init( +extern void wf_jsonrpc_response_init( struct wf_jsonrpc_response * response, json_t * message); -extern void wf_jsonrpc_impl_response_cleanup( +extern void wf_jsonrpc_response_cleanup( struct wf_jsonrpc_response * response); #ifdef __cplusplus diff --git a/lib/wf/jsonrpc/include/wf/jsonrpc/send_fn.h b/lib/webfuse/core/jsonrpc/send_fn.h similarity index 90% rename from lib/wf/jsonrpc/include/wf/jsonrpc/send_fn.h rename to lib/webfuse/core/jsonrpc/send_fn.h index 9ea9ef4..2892687 100644 --- a/lib/wf/jsonrpc/include/wf/jsonrpc/send_fn.h +++ b/lib/webfuse/core/jsonrpc/send_fn.h @@ -6,7 +6,6 @@ #endif #include -#include #ifdef __cplusplus extern "C" diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.c b/lib/webfuse/core/jsonrpc/server.c similarity index 58% rename from lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.c rename to lib/webfuse/core/jsonrpc/server.c index e510daa..03e30ae 100644 --- a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.c +++ b/lib/webfuse/core/jsonrpc/server.c @@ -1,8 +1,8 @@ -#include "wf/jsonrpc/impl/server.h" -#include "wf/jsonrpc/impl/method.h" -#include "wf/jsonrpc/impl/request.h" -#include "wf/jsonrpc/impl/unused_param.h" -#include "wf/jsonrpc/status.h" +#include "webfuse/core/jsonrpc/server.h" +#include "webfuse/core/jsonrpc/method.h" +#include "webfuse/core/jsonrpc/request.h" +#include "webfuse/core/status.h" +#include "webfuse/core/util.h" #include #include @@ -13,83 +13,79 @@ struct wf_jsonrpc_server }; static void -wf_jsonrpc_impl_server_init( +wf_jsonrpc_server_init( struct wf_jsonrpc_server * server); static void -wf_jsonrpc_impl_server_cleanup( +wf_jsonrpc_server_cleanup( struct wf_jsonrpc_server * server); struct wf_jsonrpc_server * -wf_jsonrpc_impl_server_create(void) +wf_jsonrpc_server_create(void) { struct wf_jsonrpc_server * server = malloc(sizeof(struct wf_jsonrpc_server)); - if (NULL != server) - { - wf_jsonrpc_impl_server_init(server); - } + wf_jsonrpc_server_init(server); return server; } void -wf_jsonrpc_impl_server_dispose( +wf_jsonrpc_server_dispose( struct wf_jsonrpc_server * server) { - wf_jsonrpc_impl_server_cleanup(server); + wf_jsonrpc_server_cleanup(server); free(server); } - -static void wf_jsonrpc_impl_server_init( +static void wf_jsonrpc_server_init( struct wf_jsonrpc_server * server) { server->methods = NULL; } -static void wf_jsonrpc_impl_server_cleanup( +static void wf_jsonrpc_server_cleanup( struct wf_jsonrpc_server * server) { struct wf_jsonrpc_method * current = server->methods; while (NULL != current) { struct wf_jsonrpc_method * next = current->next; - wf_jsonrpc_impl_method_dispose(current); + wf_jsonrpc_method_dispose(current); current = next; } server->methods = NULL; } -void wf_jsonrpc_impl_server_add( +void wf_jsonrpc_server_add( struct wf_jsonrpc_server * server, char const * method_name, wf_jsonrpc_method_invoke_fn * invoke, void * user_data) { - struct wf_jsonrpc_method * method = wf_jsonrpc_impl_method_create(method_name, invoke, user_data); + struct wf_jsonrpc_method * method = wf_jsonrpc_method_create(method_name, invoke, user_data); method->next = server->methods; server->methods = method; } -static void wf_jsonrpc_impl_server_invalid_method_invoke( +static void wf_jsonrpc_server_invalid_method_invoke( struct wf_jsonrpc_request * request, - char const * WF_JSONRPC_UNUSED_PARAM(method_name), - json_t * WF_JSONRPC_UNUSED_PARAM(params), - void * WF_JSONRPC_UNUSED_PARAM(user_data)) + char const * WF_UNUSED_PARAM(method_name), + json_t * WF_UNUSED_PARAM(params), + void * WF_UNUSED_PARAM(user_data)) { - wf_jsonrpc_impl_respond_error(request, WF_JSONRPC_BAD_NOTIMPLEMENTED, "not implemented"); + wf_jsonrpc_respond_error(request, WF_BAD_NOTIMPLEMENTED, "not implemented"); } -static struct wf_jsonrpc_method const wf_jsonrpc_impl_server_invalid_method = +static struct wf_jsonrpc_method const wf_jsonrpc_server_invalid_method = { .next = NULL, .name = "", - .invoke = &wf_jsonrpc_impl_server_invalid_method_invoke, + .invoke = &wf_jsonrpc_server_invalid_method_invoke, .user_data = NULL }; static struct wf_jsonrpc_method const * -wf_jsonrpc_impl_server_get_method( +wf_jsonrpc_server_get_method( struct wf_jsonrpc_server * server, char const * method_name) { @@ -104,10 +100,10 @@ wf_jsonrpc_impl_server_get_method( current = current->next; } - return &wf_jsonrpc_impl_server_invalid_method; + return &wf_jsonrpc_server_invalid_method; } -void wf_jsonrpc_impl_server_process( +void wf_jsonrpc_server_process( struct wf_jsonrpc_server * server, json_t * request_data, wf_jsonrpc_send_fn * send, @@ -123,8 +119,8 @@ void wf_jsonrpc_impl_server_process( { char const * method_name = json_string_value(method_holder); int id = json_integer_value(id_holder); - struct wf_jsonrpc_request * request = wf_jsonrpc_impl_request_create(id, send, user_data); - struct wf_jsonrpc_method const * method = wf_jsonrpc_impl_server_get_method(server, method_name); + struct wf_jsonrpc_request * request = wf_jsonrpc_request_create(id, send, user_data); + struct wf_jsonrpc_method const * method = wf_jsonrpc_server_get_method(server, method_name); method->invoke(request, method_name, params, method->user_data); } diff --git a/lib/wf/jsonrpc/include/wf/jsonrpc/server.h b/lib/webfuse/core/jsonrpc/server.h similarity index 69% rename from lib/wf/jsonrpc/include/wf/jsonrpc/server.h rename to lib/webfuse/core/jsonrpc/server.h index c4850a5..143f121 100644 --- a/lib/wf/jsonrpc/include/wf/jsonrpc/server.h +++ b/lib/webfuse/core/jsonrpc/server.h @@ -9,9 +9,8 @@ #endif #include -#include -#include -#include +#include "webfuse/core/jsonrpc/method_invoke_fn.h" +#include "webfuse/core/jsonrpc/send_fn.h" #ifdef __cplusplus extern "C" @@ -20,20 +19,20 @@ extern "C" struct wf_jsonrpc_server; -extern WF_JSONRPC_API struct wf_jsonrpc_server * +extern struct wf_jsonrpc_server * wf_jsonrpc_server_create(void); -extern WF_JSONRPC_API void +extern void wf_jsonrpc_server_dispose( struct wf_jsonrpc_server * server); -extern WF_JSONRPC_API void wf_jsonrpc_server_add( +extern void wf_jsonrpc_server_add( struct wf_jsonrpc_server * server, char const * method_name, wf_jsonrpc_method_invoke_fn * invoke, void * user_data); -extern WF_JSONRPC_API void wf_jsonrpc_server_process( +extern void wf_jsonrpc_server_process( struct wf_jsonrpc_server * server, json_t * request, wf_jsonrpc_send_fn * send, diff --git a/lib/webfuse/core/message.c b/lib/webfuse/core/message.c index e94d1c6..bfb0016 100644 --- a/lib/webfuse/core/message.c +++ b/lib/webfuse/core/message.c @@ -12,13 +12,10 @@ extern struct wf_message * wf_message_create(json_t const * value) { char * data = malloc(sizeof(struct wf_message) + LWS_PRE + length); message = (struct wf_message *) data; - if (NULL != message) - { - message->data = &data[sizeof(struct wf_message) + LWS_PRE]; - message->length = length; + message->data = &data[sizeof(struct wf_message) + LWS_PRE]; + message->length = length; - json_dumpb(value, message->data, length, JSON_COMPACT); - } + json_dumpb(value, message->data, length, JSON_COMPACT); } return message; diff --git a/lib/webfuse/core/string.c b/lib/webfuse/core/string.c index c5117eb..84608cb 100644 --- a/lib/webfuse/core/string.c +++ b/lib/webfuse/core/string.c @@ -16,18 +16,15 @@ char * wf_create_string(char const * format, ...) if (0 <= needed) { result = malloc(needed + 1); - if (NULL != result) - { - va_list args; - va_start(args, format); - int count = vsnprintf(result, needed + 1, format, args); - va_end(args); + va_list args; + va_start(args, format); + int count = vsnprintf(result, needed + 1, format, args); + va_end(args); - if ((count < 0) || (needed < count)) - { - free(result); - result = NULL; - } + if ((count < 0) || (needed < count)) + { + free(result); + result = NULL; } } diff --git a/lib/wf/timer/src/wf/timer/impl/manager.c b/lib/webfuse/core/timer/manager.c similarity index 70% rename from lib/wf/timer/src/wf/timer/impl/manager.c rename to lib/webfuse/core/timer/manager.c index 2b00f80..99540e9 100644 --- a/lib/wf/timer/src/wf/timer/impl/manager.c +++ b/lib/webfuse/core/timer/manager.c @@ -1,6 +1,6 @@ -#include "wf/timer/impl/manager.h" -#include "wf/timer/impl/timer.h" -#include "wf/timer/impl/timepoint.h" +#include "webfuse/core/timer/manager_intern.h" +#include "webfuse/core/timer/timer_intern.h" +#include "webfuse/core/timer/timepoint.h" #include #include @@ -11,19 +11,16 @@ struct wf_timer_manager }; struct wf_timer_manager * -wf_timer_impl_manager_create(void) +wf_timer_manager_create(void) { struct wf_timer_manager * manager = malloc(sizeof(struct wf_timer_manager)); - if (NULL != manager) - { - manager->timers = NULL; - } + manager->timers = NULL; return manager; } void -wf_timer_impl_manager_dispose( +wf_timer_manager_dispose( struct wf_timer_manager * manager) { struct wf_timer * timer = manager->timers; @@ -31,7 +28,7 @@ wf_timer_impl_manager_dispose( { struct wf_timer * next = timer->next; - wf_timer_impl_trigger(timer); + wf_timer_trigger(timer); timer = next; } @@ -39,7 +36,7 @@ wf_timer_impl_manager_dispose( } -void wf_timer_impl_manager_check( +void wf_timer_manager_check( struct wf_timer_manager * manager) { struct wf_timer * timer = manager->timers; @@ -47,17 +44,17 @@ void wf_timer_impl_manager_check( { struct wf_timer * next = timer->next; - if (wf_timer_impl_is_timeout(timer)) + if (wf_timer_is_timeout(timer)) { - wf_timer_impl_manager_removetimer(manager, timer); - wf_timer_impl_trigger(timer); + wf_timer_manager_removetimer(manager, timer); + wf_timer_trigger(timer); } timer = next; } } -void wf_timer_impl_manager_addtimer( +void wf_timer_manager_addtimer( struct wf_timer_manager * manager, struct wf_timer * timer) { @@ -71,7 +68,7 @@ void wf_timer_impl_manager_addtimer( manager->timers = timer; } -void wf_timer_impl_manager_removetimer( +void wf_timer_manager_removetimer( struct wf_timer_manager * manager, struct wf_timer * timer) { diff --git a/lib/wf/timer/include/wf/timer/manager.h b/lib/webfuse/core/timer/manager.h similarity index 72% rename from lib/wf/timer/include/wf/timer/manager.h rename to lib/webfuse/core/timer/manager.h index e0febc0..870a2d0 100644 --- a/lib/wf/timer/include/wf/timer/manager.h +++ b/lib/webfuse/core/timer/manager.h @@ -1,8 +1,6 @@ #ifndef WF_TIMER_MANAGER_H #define WF_TIMER_MANAGER_H -#include - #ifdef __cplusplus extern "C" { @@ -10,14 +8,14 @@ extern "C" struct wf_timer_manager; -extern WF_TIMER_API struct wf_timer_manager * +extern struct wf_timer_manager * wf_timer_manager_create(void); -extern WF_TIMER_API void +extern void wf_timer_manager_dispose( struct wf_timer_manager * manager); -extern WF_TIMER_API void +extern void wf_timer_manager_check( struct wf_timer_manager * manager); diff --git a/lib/webfuse/core/timer/manager_intern.h b/lib/webfuse/core/timer/manager_intern.h new file mode 100644 index 0000000..473df2b --- /dev/null +++ b/lib/webfuse/core/timer/manager_intern.h @@ -0,0 +1,26 @@ +#ifndef WF_TIMER_MANAGER_INTERN_H +#define WF_TIMER_MANAGER_INTERN_H + +#include "webfuse/core/timer/manager.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct wf_timer; + +extern void wf_timer_manager_addtimer( + struct wf_timer_manager * manager, + struct wf_timer * timer); + +extern void wf_timer_manager_removetimer( + struct wf_timer_manager * manager, + struct wf_timer * timer); + +#ifdef __cplusplus +} +#endif + + +#endif diff --git a/lib/wf/timer/include/wf/timer/on_timer_fn.h b/lib/webfuse/core/timer/on_timer_fn.h similarity index 100% rename from lib/wf/timer/include/wf/timer/on_timer_fn.h rename to lib/webfuse/core/timer/on_timer_fn.h diff --git a/lib/wf/timer/src/wf/timer/impl/timepoint.c b/lib/webfuse/core/timer/timepoint.c similarity index 59% rename from lib/wf/timer/src/wf/timer/impl/timepoint.c rename to lib/webfuse/core/timer/timepoint.c index 877b9c9..09fc025 100644 --- a/lib/wf/timer/src/wf/timer/impl/timepoint.c +++ b/lib/webfuse/core/timer/timepoint.c @@ -1,11 +1,11 @@ -#include "wf/timer/impl/timepoint.h" +#include "webfuse/core/timer/timepoint.h" #include #define WF_TIMER_MSEC_PER_SEC ((wf_timer_timepoint) 1000) #define WF_TIMER_NSEC_PER_MSEC ((wf_timer_timepoint) 1000 * 1000) -wf_timer_timepoint wf_timer_impl_timepoint_now(void) +wf_timer_timepoint wf_timer_timepoint_now(void) { struct timespec tp; clock_gettime(CLOCK_MONOTONIC, &tp); @@ -14,17 +14,17 @@ wf_timer_timepoint wf_timer_impl_timepoint_now(void) return now; } -wf_timer_timepoint wf_timer_impl_timepoint_in_msec(wf_timer_timediff value) +wf_timer_timepoint wf_timer_timepoint_in_msec(wf_timer_timediff value) { - wf_timer_timepoint const now = wf_timer_impl_timepoint_now(); + wf_timer_timepoint const now = wf_timer_timepoint_now(); wf_timer_timepoint result = now + ((wf_timer_timepoint) value); return result; } -bool wf_timer_impl_timepoint_is_elapsed(wf_timer_timepoint tp) +bool wf_timer_timepoint_is_elapsed(wf_timer_timepoint tp) { - wf_timer_timepoint const now = wf_timer_impl_timepoint_now(); + wf_timer_timepoint const now = wf_timer_timepoint_now(); wf_timer_timediff const diff = (wf_timer_timediff) (tp - now); return (0 > diff); diff --git a/lib/wf/timer/src/wf/timer/impl/timepoint.h b/lib/webfuse/core/timer/timepoint.h similarity index 57% rename from lib/wf/timer/src/wf/timer/impl/timepoint.h rename to lib/webfuse/core/timer/timepoint.h index 584aa92..cb78197 100644 --- a/lib/wf/timer/src/wf/timer/impl/timepoint.h +++ b/lib/webfuse/core/timer/timepoint.h @@ -1,5 +1,5 @@ -#ifndef WF_TIMER_IMPL_TIMEPOINT_H -#define WF_TIMER_IMPL_TIMEPOINT_H +#ifndef WF_TIMER_TIMEPOINT_H +#define WF_TIMER_TIMEPOINT_H #ifndef __cplusplus #include @@ -16,12 +16,12 @@ extern "C" typedef uint64_t wf_timer_timepoint; typedef int64_t wf_timer_timediff; -extern wf_timer_timepoint wf_timer_impl_timepoint_now(void); +extern wf_timer_timepoint wf_timer_timepoint_now(void); -extern wf_timer_timepoint wf_timer_impl_timepoint_in_msec( +extern wf_timer_timepoint wf_timer_timepoint_in_msec( wf_timer_timediff value); -extern bool wf_timer_impl_timepoint_is_elapsed( +extern bool wf_timer_timepoint_is_elapsed( wf_timer_timepoint timepoint); #ifdef __cplusplus diff --git a/lib/wf/timer/src/wf/timer/impl/timer.c b/lib/webfuse/core/timer/timer.c similarity index 61% rename from lib/wf/timer/src/wf/timer/impl/timer.c rename to lib/webfuse/core/timer/timer.c index 5559451..1f4d650 100644 --- a/lib/wf/timer/src/wf/timer/impl/timer.c +++ b/lib/webfuse/core/timer/timer.c @@ -1,13 +1,13 @@ -#include "wf/timer/impl/timer.h" -#include "wf/timer/impl/manager.h" -#include "wf/timer/impl/timepoint.h" +#include "webfuse/core/timer/timer_intern.h" +#include "webfuse/core/timer/manager_intern.h" +#include "webfuse/core/timer/timepoint.h" #include #include #include struct wf_timer * -wf_timer_impl_create( +wf_timer_create( struct wf_timer_manager * manager, wf_timer_on_timer_fn * on_timer, void * user_data) @@ -24,37 +24,37 @@ wf_timer_impl_create( } void -wf_timer_impl_dispose( +wf_timer_dispose( struct wf_timer * timer) { free(timer); } -void wf_timer_impl_start( +void wf_timer_start( struct wf_timer * timer, int timeout_ms) { - timer->timeout = wf_timer_impl_timepoint_in_msec(timeout_ms); + timer->timeout = wf_timer_timepoint_in_msec(timeout_ms); - wf_timer_impl_manager_addtimer(timer->manager, timer); + wf_timer_manager_addtimer(timer->manager, timer); } -void wf_timer_impl_cancel( +void wf_timer_cancel( struct wf_timer * timer) { - wf_timer_impl_manager_removetimer(timer->manager, timer); + wf_timer_manager_removetimer(timer->manager, timer); timer->timeout = 0; } -bool wf_timer_impl_is_timeout( +bool wf_timer_is_timeout( struct wf_timer * timer) { - return wf_timer_impl_timepoint_is_elapsed(timer->timeout); + return wf_timer_timepoint_is_elapsed(timer->timeout); } -void wf_timer_impl_trigger( +void wf_timer_trigger( struct wf_timer * timer) { if (0 != timer->on_timer) diff --git a/lib/wf/timer/include/wf/timer/timer.h b/lib/webfuse/core/timer/timer.h similarity index 72% rename from lib/wf/timer/include/wf/timer/timer.h rename to lib/webfuse/core/timer/timer.h index 686eb89..48149bd 100644 --- a/lib/wf/timer/include/wf/timer/timer.h +++ b/lib/webfuse/core/timer/timer.h @@ -1,8 +1,7 @@ #ifndef WF_TIMER_TIMER_H #define WF_TIMER_TIMER_H -#include -#include +#include "webfuse/core/timer/on_timer_fn.h" #ifdef __cplusplus extern "C" @@ -12,22 +11,22 @@ extern "C" struct wf_timer; struct wf_timer_manager; -extern WF_TIMER_API struct wf_timer * +extern struct wf_timer * wf_timer_create( struct wf_timer_manager * manager, wf_timer_on_timer_fn * on_timer, void * user_data); -extern WF_TIMER_API void +extern void wf_timer_dispose( struct wf_timer * timer); -extern WF_TIMER_API void +extern void wf_timer_start( struct wf_timer * timer, int timeout_ms); -extern WF_TIMER_API void +extern void wf_timer_cancel( struct wf_timer * timer); diff --git a/lib/webfuse/core/timer/timer_intern.h b/lib/webfuse/core/timer/timer_intern.h new file mode 100644 index 0000000..c910a3b --- /dev/null +++ b/lib/webfuse/core/timer/timer_intern.h @@ -0,0 +1,40 @@ +#ifndef WF_TIMER_TIMER_H +#define WF_TIMER_TIMER_H + +#include "webfuse/core/timer/timer.h" +#include "webfuse/core/timer/on_timer_fn.h" +#include "webfuse/core/timer/timepoint.h" + +#ifndef __cplusplus +#include +#endif + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct wf_timer +{ + struct wf_timer_manager * manager; + wf_timer_timepoint timeout; + wf_timer_on_timer_fn * on_timer; + void * user_data; + struct wf_timer * next; + struct wf_timer * prev; +}; + +extern bool wf_timer_is_timeout( + struct wf_timer * timer); + +extern void wf_timer_trigger( + struct wf_timer * timer); + + +#ifdef __cplusplus +} +#endif + + + +#endif diff --git a/lib/webfuse/provider/api.c b/lib/webfuse/provider/api.c index 0704095..dc42fba 100644 --- a/lib/webfuse/provider/api.c +++ b/lib/webfuse/provider/api.c @@ -95,6 +95,13 @@ void wfp_client_config_set_certpath( wfp_impl_client_config_set_certpath(config, cert_path); } +void wfp_client_config_set_ca_filepath( + struct wfp_client_config * config, + char const * ca_filepath) +{ + wfp_impl_client_config_set_ca_filepath(config, ca_filepath); +} + void wfp_client_config_set_onconnected( struct wfp_client_config * config, wfp_connected_fn * handler) diff --git a/lib/webfuse/provider/impl/client.c b/lib/webfuse/provider/impl/client.c index 21af148..9f45a5a 100644 --- a/lib/webfuse/provider/impl/client.c +++ b/lib/webfuse/provider/impl/client.c @@ -30,25 +30,32 @@ struct wfp_client * wfp_impl_client_create( wf_lwslog_disable(); struct wfp_client * client = malloc(sizeof(struct wfp_client)); - if (NULL != client) - { - wfp_impl_client_protocol_init(&client->protocol, &config->provider, config->user_data); + wfp_impl_client_protocol_init(&client->protocol, &config->provider, config->user_data); - memset(client->protocols, 0, sizeof(struct lws_protocols) * WFP_CLIENT_PROTOCOL_COUNT); - wfp_impl_client_protocol_init_lws(&client->protocol, &client->protocols[0]); + memset(client->protocols, 0, sizeof(struct lws_protocols) * WFP_CLIENT_PROTOCOL_COUNT); + wfp_impl_client_protocol_init_lws(&client->protocol, &client->protocols[0]); - memset(&client->info, 0, sizeof(struct lws_context_creation_info)); - client->info.port = CONTEXT_PORT_NO_LISTEN; - client->info.protocols = client->protocols; - client->info.uid = -1; - client->info.gid = -1; + memset(&client->info, 0, sizeof(struct lws_context_creation_info)); + client->info.port = CONTEXT_PORT_NO_LISTEN; + client->info.protocols = client->protocols; + client->info.uid = -1; + client->info.gid = -1; - if ((NULL != config->cert_path) && (NULL != config->key_path)) - { - - } + if ((NULL != config->cert_path) && (NULL != config->key_path)) + { + client->info.options |= LWS_SERVER_OPTION_EXPLICIT_VHOSTS; + } - client->context = lws_create_context(&client->info); + client->context = lws_create_context(&client->info); + + if ((NULL != config->cert_path) && (NULL != config->key_path)) + { + struct lws_vhost * vhost = lws_create_vhost(client->context, &client->info); + client->info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT; + client->info.client_ssl_cert_filepath = config->cert_path; + client->info.client_ssl_private_key_filepath = config->key_path; + client->info.client_ssl_ca_filepath = config->ca_filepath; + lws_init_vhost_client_ssl(&client->info, vhost); } return client; diff --git a/lib/webfuse/provider/impl/client_config.c b/lib/webfuse/provider/impl/client_config.c index 0e8afc6..9e6489b 100644 --- a/lib/webfuse/provider/impl/client_config.c +++ b/lib/webfuse/provider/impl/client_config.c @@ -6,13 +6,11 @@ struct wfp_client_config * wfp_impl_client_config_create(void) { struct wfp_client_config * config = malloc(sizeof(struct wfp_client_config)); - if (NULL != config) - { - wfp_impl_provider_init(&config->provider); - config->user_data = NULL; - config->key_path = NULL; - config->cert_path = NULL; - } + wfp_impl_provider_init(&config->provider); + config->user_data = NULL; + config->key_path = NULL; + config->cert_path = NULL; + config->ca_filepath = NULL; return config; } @@ -22,6 +20,7 @@ void wfp_impl_client_config_dispose( { free(config->key_path); free(config->cert_path); + free(config->ca_filepath); free(config); } @@ -48,6 +47,14 @@ void wfp_impl_client_config_set_certpath( config->cert_path = strdup(cert_path); } +void wfp_impl_client_config_set_ca_filepath( + struct wfp_client_config * config, + char const * ca_filepath) +{ + free(config->ca_filepath); + config->ca_filepath = strdup(ca_filepath); +} + void wfp_impl_client_config_set_onconnected( struct wfp_client_config * config, wfp_connected_fn * handler) diff --git a/lib/webfuse/provider/impl/client_config.h b/lib/webfuse/provider/impl/client_config.h index f8d5b9b..a15ff87 100644 --- a/lib/webfuse/provider/impl/client_config.h +++ b/lib/webfuse/provider/impl/client_config.h @@ -15,6 +15,7 @@ struct wfp_client_config void * user_data; char * key_path; char * cert_path; + char * ca_filepath; }; extern struct wfp_client_config * wfp_impl_client_config_create(void); @@ -34,6 +35,10 @@ extern void wfp_impl_client_config_set_certpath( struct wfp_client_config * config, char const * cert_path); +extern void wfp_impl_client_config_set_ca_filepath( + struct wfp_client_config * config, + char const * ca_filepath); + extern void wfp_impl_client_config_set_onconnected( struct wfp_client_config * config, wfp_connected_fn * handler); diff --git a/lib/webfuse/provider/impl/client_protocol.c b/lib/webfuse/provider/impl/client_protocol.c index 4f61b8a..a85514a 100644 --- a/lib/webfuse/provider/impl/client_protocol.c +++ b/lib/webfuse/provider/impl/client_protocol.c @@ -16,11 +16,11 @@ #include "webfuse/provider/impl/url.h" #include "webfuse/core/protocol_names.h" -#include "wf/timer/manager.h" +#include "webfuse/core/timer/manager.h" -#include "wf/jsonrpc/response.h" -#include "wf/jsonrpc/request.h" -#include "wf/jsonrpc/proxy.h" +#include "webfuse/core/jsonrpc/response.h" +#include "webfuse/core/jsonrpc/request.h" +#include "webfuse/core/jsonrpc/proxy.h" #define WF_DEFAULT_TIMEOUT (10 * 1000) @@ -270,10 +270,7 @@ struct wfp_client_protocol * wfp_impl_client_protocol_create( struct wfp_client_config const * config) { struct wfp_client_protocol * protocol = malloc(sizeof(struct wfp_client_protocol)); - if (NULL != protocol) - { - wfp_impl_client_protocol_init(protocol, &config->provider, config->user_data); - } + wfp_impl_client_protocol_init(protocol, &config->provider, config->user_data); return protocol; } diff --git a/lib/webfuse/provider/impl/dirbuffer.c b/lib/webfuse/provider/impl/dirbuffer.c index db401ef..9bc5f22 100644 --- a/lib/webfuse/provider/impl/dirbuffer.c +++ b/lib/webfuse/provider/impl/dirbuffer.c @@ -4,10 +4,7 @@ struct wfp_dirbuffer * wfp_impl_dirbuffer_create(void) { struct wfp_dirbuffer * buffer = malloc(sizeof(struct wfp_dirbuffer)); - if (NULL != buffer) - { - buffer->entries = json_array(); - } + buffer->entries = json_array(); return buffer; } diff --git a/lib/webfuse/provider/impl/operation/read.c b/lib/webfuse/provider/impl/operation/read.c index 3101ea0..5c2b639 100644 --- a/lib/webfuse/provider/impl/operation/read.c +++ b/lib/webfuse/provider/impl/operation/read.c @@ -56,28 +56,21 @@ void wfp_impl_respond_read( { size_t const size = wf_base64_encoded_size(length) + 1; char * buffer = malloc(size); - if (NULL != buffer) - { - wf_base64_encode((uint8_t const *) data, length, buffer, size); + wf_base64_encode((uint8_t const *) data, length, buffer, size); - json_t * result = json_object(); - json_object_set_new(result, "data", json_string(buffer)); - json_object_set_new(result, "format", json_string("base64")); - json_object_set_new(result, "count", json_integer((int) length)); + json_t * result = json_object(); + json_object_set_new(result, "data", json_string(buffer)); + json_object_set_new(result, "format", json_string("base64")); + json_object_set_new(result, "count", json_integer((int) length)); - wfp_impl_respond(request, result); - free(buffer); - } - else - { - wfp_impl_respond_error(request, WF_BAD); - } + wfp_impl_respond(request, result); + free(buffer); } else { json_t * result = json_object(); json_object_set_new(result, "data", json_string("")); - json_object_set_new(result, "format", json_string("identitiy")); + json_object_set_new(result, "format", json_string("identity")); json_object_set_new(result, "count", json_integer(0)); wfp_impl_respond(request, result); diff --git a/lib/webfuse/provider/impl/operation/readdir.c b/lib/webfuse/provider/impl/operation/readdir.c index 4fc3db7..63d59c1 100644 --- a/lib/webfuse/provider/impl/operation/readdir.c +++ b/lib/webfuse/provider/impl/operation/readdir.c @@ -14,7 +14,7 @@ void wfp_impl_readdir( { json_t * inode_holder = json_array_get(params, 1); - if ((NULL != inode_holder) && (json_is_integer(inode_holder))) + if (json_is_integer(inode_holder)) { ino_t inode = (ino_t) json_integer_value(inode_holder); struct wfp_request * request = wfp_impl_request_create(context->request, id); diff --git a/lib/webfuse/provider/impl/provider.c b/lib/webfuse/provider/impl/provider.c index 14282d2..c9c159f 100644 --- a/lib/webfuse/provider/impl/provider.c +++ b/lib/webfuse/provider/impl/provider.c @@ -94,8 +94,7 @@ void wfp_impl_provider_invoke( json_t * params = json_object_get(request, "params"); json_t * id_holder = json_object_get(request, "id"); - if ((NULL != method_holder) && (json_is_string(method_holder)) && - (NULL != params) && (json_is_array(params))) + if ((json_is_string(method_holder)) && (json_is_array(params))) { char const * method = json_string_value(method_holder); int id = json_is_integer(id_holder) ? json_integer_value(id_holder) : 0; diff --git a/lib/webfuse/provider/impl/provider.h b/lib/webfuse/provider/impl/provider.h index a9b814d..1e8ec1f 100644 --- a/lib/webfuse/provider/impl/provider.h +++ b/lib/webfuse/provider/impl/provider.h @@ -30,7 +30,7 @@ struct wfp_provider struct wfp_impl_invokation_context { - struct wfp_provider * provider; + struct wfp_provider const * provider; void * user_data; struct wfp_request * request; }; diff --git a/lib/webfuse/provider/impl/request.c b/lib/webfuse/provider/impl/request.c index 42ea835..7f8b05f 100644 --- a/lib/webfuse/provider/impl/request.c +++ b/lib/webfuse/provider/impl/request.c @@ -8,12 +8,9 @@ struct wfp_request * wfp_impl_request_create( int id) { struct wfp_request * request = malloc(sizeof(struct wfp_request)); - if (NULL != request) - { - request->respond = prototype->respond; - request->user_data = prototype->user_data; - request->id = id; - } + request->respond = prototype->respond; + request->user_data = prototype->user_data; + request->id = id; return request; } diff --git a/lib/wf/jsonrpc/include/wf/jsonrpc.h b/lib/wf/jsonrpc/include/wf/jsonrpc.h deleted file mode 100644 index ab7f1dc..0000000 --- a/lib/wf/jsonrpc/include/wf/jsonrpc.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef WF_JSONRPC_H -#define WF_JSONRPC_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#endif diff --git a/lib/wf/jsonrpc/include/wf/jsonrpc/api.h b/lib/wf/jsonrpc/include/wf/jsonrpc/api.h deleted file mode 100644 index 648bf0f..0000000 --- a/lib/wf/jsonrpc/include/wf/jsonrpc/api.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef WF_JSONRPC_API_H -#define WF_JSONRPC_API_H - -#ifndef WF_JSONRPC_API -#define WF_JSONRPC_API -#endif - -#endif diff --git a/lib/wf/jsonrpc/include/wf/jsonrpc/status.h b/lib/wf/jsonrpc/include/wf/jsonrpc/status.h deleted file mode 100644 index 85da73d..0000000 --- a/lib/wf/jsonrpc/include/wf/jsonrpc/status.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef WF_JSONRPC_STATUS_H -#define WF_JSONRPC_STATUS_H - -#define WF_JSONRPC_GOOD 0 -#define WF_JSONRPC_BAD -1 -#define WF_JSONRPC_BAD_NOTIMPLEMENTED -2 -#define WF_JSONRPC_BAD_TIMEOUT -3 -#define WF_JSONRPC_BAD_BUSY -4 -#define WF_JSONRPC_BAD_FORMAT -5 - -#endif diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/api.c b/lib/wf/jsonrpc/src/wf/jsonrpc/api.c deleted file mode 100644 index 891bcce..0000000 --- a/lib/wf/jsonrpc/src/wf/jsonrpc/api.c +++ /dev/null @@ -1,144 +0,0 @@ -#include "wf/jsonrpc.h" - -#include "wf/jsonrpc/impl/proxy.h" -#include "wf/jsonrpc/impl/request.h" -#include "wf/jsonrpc/impl/response.h" -#include "wf/jsonrpc/impl/server.h" - -// proxy - -struct wf_jsonrpc_proxy * -wf_jsonrpc_proxy_create( - struct wf_timer_manager * manager, - int timeout, - wf_jsonrpc_send_fn * send, - void * user_data) -{ - return wf_jsonrpc_impl_proxy_create(manager, timeout, send, user_data); -} - -void wf_jsonrpc_proxy_dispose( - struct wf_jsonrpc_proxy * proxy) -{ - wf_jsonrpc_impl_proxy_dispose(proxy); -} - -void wf_jsonrpc_proxy_invoke( - struct wf_jsonrpc_proxy * proxy, - wf_jsonrpc_proxy_finished_fn * finished, - void * user_data, - char const * method_name, - char const * param_info, - ... -) -{ - va_list args; - va_start(args, param_info); - wf_jsonrpc_impl_proxy_invoke(proxy, finished, user_data, method_name, param_info, args); - va_end(args); -} - -void wf_jsonrpc_proxy_notify( - struct wf_jsonrpc_proxy * proxy, - char const * method_name, - char const * param_info, - ... -) -{ - va_list args; - va_start(args, param_info); - wf_jsonrpc_impl_proxy_notify(proxy, method_name, param_info, args); - va_end(args); -} - -void wf_jsonrpc_proxy_onresult( - struct wf_jsonrpc_proxy * proxy, - json_t * message) -{ - wf_jsonrpc_impl_proxy_onresult(proxy, message); -} - - -// request - -bool wf_jsonrpc_is_request( - json_t * message) -{ - return wf_jsonrpc_impl_is_request(message); -} - -struct wf_jsonrpc_request * wf_jsonrpc_request_create( - int id, - wf_jsonrpc_send_fn * send, - void * user_data) -{ - return wf_jsonrpc_impl_request_create(id, send, user_data); -} - -void wf_jsonrpc_request_dispose( - struct wf_jsonrpc_request * request) -{ - wf_jsonrpc_impl_request_dispose(request); -} - -void * wf_jsonrpc_request_get_userdata( - struct wf_jsonrpc_request * request) -{ - return wf_jsonrpc_impl_request_get_userdata(request); -} - -void wf_jsonrpc_respond( - struct wf_jsonrpc_request * request, - json_t * result) -{ - wf_jsonrpc_impl_respond(request, result); -} - -void wf_jsonrpc_respond_error( - struct wf_jsonrpc_request * request, - int code, - char const * message) -{ - wf_jsonrpc_impl_respond_error(request, code, message); -} - -// response - -bool wf_jsonrpc_is_response( - json_t * message) -{ - return wf_jsonrpc_impl_is_response(message); -} - -// server - -struct wf_jsonrpc_server * -wf_jsonrpc_server_create(void) -{ - return wf_jsonrpc_impl_server_create(); -} - -void -wf_jsonrpc_server_dispose( - struct wf_jsonrpc_server * server) -{ - wf_jsonrpc_impl_server_dispose(server); -} - -void wf_jsonrpc_server_add( - struct wf_jsonrpc_server * server, - char const * method_name, - wf_jsonrpc_method_invoke_fn * invoke, - void * user_data) -{ - wf_jsonrpc_impl_server_add(server, method_name, invoke, user_data); -} - -void wf_jsonrpc_server_process( - struct wf_jsonrpc_server * server, - json_t * request, - wf_jsonrpc_send_fn * send, - void * user_data) -{ - wf_jsonrpc_impl_server_process(server, request, send, user_data); -} diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.h b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.h deleted file mode 100644 index 0d510e9..0000000 --- a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.h +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef WF_JSONRPC_IMPL_REQUEST_H -#define WF_JSONRPC_IMPL_REQUEST_H - -#ifndef __cplusplus -#include -#include -#include -#else -#include -#include -using std::size_t; -#endif - -#include -#include -#include "wf/jsonrpc/send_fn.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -struct wf_jsonrpc_request; - -extern bool wf_jsonrpc_impl_is_request( - json_t * message); - -extern struct wf_jsonrpc_request * wf_jsonrpc_impl_request_create( - int id, - wf_jsonrpc_send_fn * send, - void * user_data); - -extern void wf_jsonrpc_impl_request_dispose( - struct wf_jsonrpc_request * request); - -extern void * wf_jsonrpc_impl_request_get_userdata( - struct wf_jsonrpc_request * request); - -extern void wf_jsonrpc_impl_respond( - struct wf_jsonrpc_request * request, - json_t * result); - -extern void wf_jsonrpc_impl_respond_error( - struct wf_jsonrpc_request * request, - int code, - char const * message); - - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.h b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.h deleted file mode 100644 index 82c7421..0000000 --- a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef WF_JSONRPC_IMPL_SERVER_H -#define WF_JSONRPC_IMPL_SERVER_H - -#include -#include "wf/jsonrpc/method_invoke_fn.h" -#include "wf/jsonrpc/send_fn.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -struct wf_jsonrpc_server; - -extern struct wf_jsonrpc_server * -wf_jsonrpc_impl_server_create(void); - -extern void -wf_jsonrpc_impl_server_dispose( - struct wf_jsonrpc_server * server); - -extern void -wf_jsonrpc_impl_server_add( - struct wf_jsonrpc_server * server, - char const * method_name, - wf_jsonrpc_method_invoke_fn * invoke, - void * user_data); - -extern void -wf_jsonrpc_impl_server_process( - struct wf_jsonrpc_server * server, - json_t * request, - wf_jsonrpc_send_fn * send, - void * user_data); - -#ifdef __cplusplus -} -#endif - - -#endif diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/unused_param.h b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/unused_param.h deleted file mode 100644 index 41323c8..0000000 --- a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/unused_param.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef WF_JSONRPC_UTIL_H -#define WF_JSONRPC_UTIL_H - -#ifdef __GNUC__ -#define WF_JSONRPC_UNUSED_PARAM(param) param __attribute__((unused)) -#else -#define WF_JSONRPC_UNUSED_PARAM(param) -#endif - -#endif diff --git a/lib/wf/jsonrpc/test/wf/jsonrpc/test_response.cc b/lib/wf/jsonrpc/test/wf/jsonrpc/test_response.cc deleted file mode 100644 index e8515aa..0000000 --- a/lib/wf/jsonrpc/test/wf/jsonrpc/test_response.cc +++ /dev/null @@ -1,58 +0,0 @@ -#include -#include "wf/jsonrpc/impl/response.h" -#include "wf/jsonrpc/status.h" - -TEST(wf_json_response, init_result) -{ - json_t * message = json_object(); - json_object_set_new(message, "result", json_integer(47)); - json_object_set_new(message, "id", json_integer(11)); - - struct wf_jsonrpc_response response; - wf_jsonrpc_impl_response_init(&response, message); - - ASSERT_EQ(nullptr, response.error); - ASSERT_TRUE(json_is_integer(response.result)); - ASSERT_EQ(47, json_integer_value(response.result)); - ASSERT_EQ(11, response.id); - - wf_jsonrpc_impl_response_cleanup(&response); - json_decref(message); -} - -TEST(wf_json_response, init_error) -{ - json_t * message = json_object(); - json_t * err = json_object(); - json_object_set_new(err, "code", json_integer(42)); - json_object_set_new(err, "message", json_string("Don't Panic!")); - json_object_set_new(message, "error", err); - json_object_set_new(message, "id", json_integer(23)); - - struct wf_jsonrpc_response response; - wf_jsonrpc_impl_response_init(&response, message); - - ASSERT_EQ(42, json_integer_value(json_object_get(response.error, "code"))); - ASSERT_STREQ("Don't Panic!", json_string_value(json_object_get(response.error, "message"))); - ASSERT_EQ(nullptr, response.result); - ASSERT_EQ(23, response.id); - - wf_jsonrpc_impl_response_cleanup(&response); - json_decref(message); -} - -TEST(wf_json_response, init_format_error) -{ - json_t * message = json_object(); - json_object_set_new(message, "id", json_integer(12)); - - struct wf_jsonrpc_response response; - wf_jsonrpc_impl_response_init(&response, message); - - ASSERT_EQ(WF_JSONRPC_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code"))); - ASSERT_EQ(nullptr, response.result); - ASSERT_EQ(12, response.id); - - wf_jsonrpc_impl_response_cleanup(&response); - json_decref(message); -} diff --git a/lib/wf/jsonrpc/test/wf/jsonrpc/test_server.cc b/lib/wf/jsonrpc/test/wf/jsonrpc/test_server.cc deleted file mode 100644 index 14f7ac5..0000000 --- a/lib/wf/jsonrpc/test/wf/jsonrpc/test_server.cc +++ /dev/null @@ -1,123 +0,0 @@ -#include -#include "wf/jsonrpc/server.h" -#include "wf/jsonrpc/request.h" -#include "wf/jsonrpc/status.h" - -namespace -{ - struct Context - { - json_t * response; - bool is_called; - }; - - bool jsonrpc_send( - json_t * request, - void * user_data) - { - Context * context = reinterpret_cast(user_data); - context->is_called = true; - context->response = request; - json_incref(request); - - return true; - } - - void sayHello( - struct wf_jsonrpc_request * request, - char const * method_name, - json_t * params, - void * user_data) - { - (void) method_name; - (void) params; - (void) user_data; - - json_t * result = json_string("Hello"); - wf_jsonrpc_respond(request, result); - } - -} - -TEST(wf_jsonrpc_server, process_request) -{ - struct wf_jsonrpc_server * server = wf_jsonrpc_server_create(); - wf_jsonrpc_server_add(server, "sayHello", &sayHello, nullptr); - - Context context{nullptr, false}; - void * user_data = reinterpret_cast(&context); - json_t * request = json_object(); - json_object_set_new(request, "method", json_string("sayHello")); - json_object_set_new(request, "params", json_array()); - json_object_set_new(request, "id", json_integer(23)); - wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data); - - ASSERT_TRUE(context.is_called); - ASSERT_NE(nullptr, context.response); - ASSERT_TRUE(json_is_object(context.response)); - - json_t * id = json_object_get(context.response, "id"); - ASSERT_TRUE(json_is_integer(id)); - ASSERT_EQ(23, json_integer_value(id)); - - json_t * result = json_object_get(context.response, "result"); - ASSERT_TRUE(json_is_string(result)); - ASSERT_STREQ("Hello", json_string_value(result)); - - json_decref(context.response); - json_decref(request); - wf_jsonrpc_server_dispose(server); -} - -TEST(wf_jsonrpc_server, invoke_unknown_method) -{ - struct wf_jsonrpc_server * server = wf_jsonrpc_server_create(); - wf_jsonrpc_server_add(server, "sayHello", &sayHello, nullptr); - - Context context{nullptr, false}; - void * user_data = reinterpret_cast(&context); - json_t * request = json_object(); - json_object_set_new(request, "method", json_string("greet")); - json_object_set_new(request, "params", json_array()); - json_object_set_new(request, "id", json_integer(42)); - wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data); - - ASSERT_TRUE(context.is_called); - ASSERT_NE(nullptr, context.response); - ASSERT_TRUE(json_is_object(context.response)); - - json_t * id = json_object_get(context.response, "id"); - ASSERT_TRUE(json_is_integer(id)); - ASSERT_EQ(42, json_integer_value(id)); - - json_t * err = json_object_get(context.response, "error"); - ASSERT_TRUE(json_is_object(err)); - - json_t * err_code = json_object_get(err, "code"); - ASSERT_TRUE(json_is_integer(err_code)); - ASSERT_EQ(WF_JSONRPC_BAD_NOTIMPLEMENTED, json_integer_value(err_code)); - - json_t * err_message = json_object_get(err, "message"); - ASSERT_TRUE(json_is_string(err_message)); - - json_decref(context.response); - json_decref(request); - wf_jsonrpc_server_dispose(server); -} - -TEST(wf_jsonrpc_server, skip_invalid_request) -{ - struct wf_jsonrpc_server * server = wf_jsonrpc_server_create(); - - Context context{nullptr, false}; - void * user_data = reinterpret_cast(&context); - json_t * request = json_object(); - json_object_set_new(request, "method", json_string("sayHello")); - json_object_set_new(request, "params", json_array()); - wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data); - - ASSERT_FALSE(context.is_called); - - json_decref(request); - wf_jsonrpc_server_dispose(server); -} diff --git a/lib/wf/timer/include/wf/timer.h b/lib/wf/timer/include/wf/timer.h deleted file mode 100644 index 341792b..0000000 --- a/lib/wf/timer/include/wf/timer.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef WF_TIMER_H -#define WF_TIMER_H - -#include -#include -#include -#include - -#endif diff --git a/lib/wf/timer/include/wf/timer/api.h b/lib/wf/timer/include/wf/timer/api.h deleted file mode 100644 index c327ba7..0000000 --- a/lib/wf/timer/include/wf/timer/api.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef WF_TIMER_API_H -#define WF_TIMER_API_H - -#ifndef WF_TIMER_API -#define WF_TIMER_API -#endif - -#endif diff --git a/lib/wf/timer/src/wf/timer/api.c b/lib/wf/timer/src/wf/timer/api.c deleted file mode 100644 index d0399a9..0000000 --- a/lib/wf/timer/src/wf/timer/api.c +++ /dev/null @@ -1,59 +0,0 @@ -#include "wf/timer.h" - -#include "wf/timer/impl/manager.h" -#include "wf/timer/impl/timer.h" - -// manager - -struct wf_timer_manager * -wf_timer_manager_create(void) -{ - return wf_timer_impl_manager_create(); -} - -void -wf_timer_manager_dispose( - struct wf_timer_manager * manager) -{ - wf_timer_impl_manager_dispose(manager); -} - -void -wf_timer_manager_check( - struct wf_timer_manager * manager) -{ - wf_timer_impl_manager_check(manager); -} - -// timer - -struct wf_timer * -wf_timer_create( - struct wf_timer_manager * manager, - wf_timer_on_timer_fn * on_timer, - void * user_data) -{ - return wf_timer_impl_create(manager, on_timer, user_data); -} - -void -wf_timer_dispose( - struct wf_timer * timer) -{ - wf_timer_impl_dispose(timer); -} - -void -wf_timer_start( - struct wf_timer * timer, - int timeout_ms) -{ - wf_timer_impl_start(timer, timeout_ms); -} - -void -wf_timer_cancel( - struct wf_timer * timer) -{ - wf_timer_impl_cancel(timer); -} diff --git a/lib/wf/timer/src/wf/timer/impl/manager.h b/lib/wf/timer/src/wf/timer/impl/manager.h deleted file mode 100644 index c4fc64a..0000000 --- a/lib/wf/timer/src/wf/timer/impl/manager.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef WF_TIMER_IMPL_MANAGER_H -#define WF_TIMER_IMPL_MANAGER_H - -#ifdef __cplusplus -extern "C" -{ -#endif - -struct wf_timer; -struct wf_timer_manager; - -extern struct wf_timer_manager * -wf_timer_impl_manager_create(void); - -extern void -wf_timer_impl_manager_dispose( - struct wf_timer_manager * manager); - -extern void -wf_timer_impl_manager_check( - struct wf_timer_manager * manager); - -extern void wf_timer_impl_manager_addtimer( - struct wf_timer_manager * manager, - struct wf_timer * timer); - -extern void wf_timer_impl_manager_removetimer( - struct wf_timer_manager * manager, - struct wf_timer * timer); - -#ifdef __cplusplus -} -#endif - - -#endif diff --git a/lib/wf/timer/src/wf/timer/impl/timer.h b/lib/wf/timer/src/wf/timer/impl/timer.h deleted file mode 100644 index 2e18a8b..0000000 --- a/lib/wf/timer/src/wf/timer/impl/timer.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef WF_ADAPTER_IMPL_TIME_TIMER_H -#define WF_ADAPTER_IMPL_TIME_TIMER_H - -#include "wf/timer/on_timer_fn.h" -#include "wf/timer/impl/timepoint.h" - -#ifndef __cplusplus -#include -#endif - -#ifdef __cplusplus -extern "C" -{ -#endif - -struct wf_timer_manager; - -struct wf_timer -{ - struct wf_timer_manager * manager; - wf_timer_timepoint timeout; - wf_timer_on_timer_fn * on_timer; - void * user_data; - struct wf_timer * next; - struct wf_timer * prev; -}; - - -struct wf_timer * -wf_timer_impl_create( - struct wf_timer_manager * manager, - wf_timer_on_timer_fn * on_timer, - void * user_data); - -void -wf_timer_impl_dispose( - struct wf_timer * timer); - -extern void wf_timer_impl_start( - struct wf_timer * timer, - int timeout_ms); - -extern void wf_timer_impl_cancel( - struct wf_timer * timer); - -extern bool wf_timer_impl_is_timeout( - struct wf_timer * timer); - -extern void wf_timer_impl_trigger( - struct wf_timer * timer); - - -#ifdef __cplusplus -} -#endif - - - -#endif diff --git a/lib/wf/timer/test/wf/timer/test_timepoint.cc b/lib/wf/timer/test/wf/timer/test_timepoint.cc deleted file mode 100644 index 723ff6a..0000000 --- a/lib/wf/timer/test/wf/timer/test_timepoint.cc +++ /dev/null @@ -1,38 +0,0 @@ -#include - -#include "wf/timer/impl/timepoint.h" - -#include -#include - -using namespace std::chrono_literals; - -TEST(wf_timer_timepoint, now) -{ - wf_timer_timepoint start = wf_timer_impl_timepoint_now(); - std::this_thread::sleep_for(42ms); - wf_timer_timepoint end = wf_timer_impl_timepoint_now(); - - ASSERT_LT(start, end); - ASSERT_LT(end, start + 500); -} - -TEST(wf_timer_timepoint, in_msec) -{ - wf_timer_timepoint now = wf_timer_impl_timepoint_now(); - wf_timer_timepoint later = wf_timer_impl_timepoint_in_msec(42); - - ASSERT_LT(now, later); - ASSERT_LT(later, now + 500); -} - -TEST(wf_timer_timepoint, elapsed) -{ - wf_timer_timepoint now; - - now = wf_timer_impl_timepoint_now(); - ASSERT_TRUE(wf_timer_impl_timepoint_is_elapsed(now - 1)); - - now = wf_timer_impl_timepoint_now(); - ASSERT_FALSE(wf_timer_impl_timepoint_is_elapsed(now + 500)); -} diff --git a/test/webfuse/mocks/fake_invokation_context.cc b/test/webfuse/mocks/fake_invokation_context.cc new file mode 100644 index 0000000..4aea3ee --- /dev/null +++ b/test/webfuse/mocks/fake_invokation_context.cc @@ -0,0 +1,17 @@ +#include "webfuse/mocks/fake_invokation_context.hpp" + +namespace webfuse_test +{ + +wfp_impl_invokation_context create_context(MockProvider& provider, wfp_request * request) +{ + wfp_impl_invokation_context context = + { + provider.get_provider(), + provider.get_userdata(), + request + }; + return context; +} + +} \ No newline at end of file diff --git a/test/webfuse/mocks/fake_invokation_context.hpp b/test/webfuse/mocks/fake_invokation_context.hpp new file mode 100644 index 0000000..07eb971 --- /dev/null +++ b/test/webfuse/mocks/fake_invokation_context.hpp @@ -0,0 +1,16 @@ +#ifndef FAKE_INVOCATION_CONTEXT_HPP +#define FAKE_INVOCATION_CONTEXT_HPP + +#include "webfuse/provider/impl/provider.h" +#include "webfuse/mocks/mock_provider.hpp" + +namespace webfuse_test +{ + +wfp_impl_invokation_context create_context( + MockProvider& provider, + wfp_request * request = nullptr); + +} + +#endif \ No newline at end of file diff --git a/test/webfuse/mocks/mock_fuse.cc b/test/webfuse/mocks/mock_fuse.cc new file mode 100644 index 0000000..486c8ed --- /dev/null +++ b/test/webfuse/mocks/mock_fuse.cc @@ -0,0 +1,31 @@ +#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); +WF_WRAP_FUNC3(webfuse_test_FuseMock, int, fuse_reply_buf, fuse_req_t, const char *, size_t); +WF_WRAP_FUNC3(webfuse_test_FuseMock, int, fuse_reply_attr, fuse_req_t, const struct stat *, double); +WF_WRAP_FUNC1(webfuse_test_FuseMock, const struct fuse_ctx *, fuse_req_ctx, fuse_req_t); +WF_WRAP_FUNC2(webfuse_test_FuseMock, int, fuse_reply_entry, fuse_req_t, const struct fuse_entry_param *); +} + +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..cc3fb71 --- /dev/null +++ b/test/webfuse/mocks/mock_fuse.hpp @@ -0,0 +1,27 @@ +#ifndef MOCK_FUSE_HPP +#define MOCK_FUSE_HPP + +#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)); + MOCK_METHOD3(fuse_reply_buf, int (fuse_req_t req, const char *buf, size_t size)); + MOCK_METHOD3(fuse_reply_attr, int (fuse_req_t req, const struct stat *attr, double attr_timeout)); + MOCK_METHOD1(fuse_req_ctx, const struct fuse_ctx *(fuse_req_t req)); + MOCK_METHOD2(fuse_reply_entry, int (fuse_req_t req, const struct fuse_entry_param *e)); +}; + +} + +#endif diff --git a/test/webfuse/mocks/mock_jsonrpc_proxy.cc b/test/webfuse/mocks/mock_jsonrpc_proxy.cc new file mode 100644 index 0000000..3fb243e --- /dev/null +++ b/test/webfuse/mocks/mock_jsonrpc_proxy.cc @@ -0,0 +1,34 @@ +#include "webfuse/mocks/mock_jsonrpc_proxy.hpp" +#include "webfuse/utils/wrap.hpp" + +extern "C" +{ +static webfuse_test::MockJsonRpcProxy * webfuse_test_MockJsonRpcProxy = nullptr; + +WF_WRAP_VFUNC5(webfuse_test_MockJsonRpcProxy, void, wf_jsonrpc_proxy_vinvoke, + struct wf_jsonrpc_proxy *, + wf_jsonrpc_proxy_finished_fn *, + void *, + char const *, + char const *); + +WF_WRAP_VFUNC3(webfuse_test_MockJsonRpcProxy, void, wf_jsonrpc_proxy_vnotify, + struct wf_jsonrpc_proxy *, + char const *, + char const *); +} + +namespace webfuse_test +{ + +MockJsonRpcProxy::MockJsonRpcProxy() +{ + webfuse_test_MockJsonRpcProxy = this; +} + +MockJsonRpcProxy::~MockJsonRpcProxy() +{ + webfuse_test_MockJsonRpcProxy = nullptr; +} + +} \ No newline at end of file diff --git a/test/webfuse/mocks/mock_jsonrpc_proxy.hpp b/test/webfuse/mocks/mock_jsonrpc_proxy.hpp new file mode 100644 index 0000000..74d61af --- /dev/null +++ b/test/webfuse/mocks/mock_jsonrpc_proxy.hpp @@ -0,0 +1,30 @@ +#ifndef MOCK_JSONRPC_PROXY_HPP +#define MOCK_JSONRPC_PROXY_HPP + +#include "webfuse/core/jsonrpc/proxy_intern.h" +#include + +namespace webfuse_test +{ + +class MockJsonRpcProxy +{ +public: + MockJsonRpcProxy(); + virtual ~MockJsonRpcProxy(); + MOCK_METHOD5(wf_jsonrpc_proxy_vinvoke, void ( + struct wf_jsonrpc_proxy * proxy, + wf_jsonrpc_proxy_finished_fn * finished, + void * user_data, + char const * method_name, + char const * param_info)); + MOCK_METHOD3(wf_jsonrpc_proxy_vnotify, void ( + struct wf_jsonrpc_proxy * proxy, + char const * method_name, + char const * param_info)); + +}; + +} + +#endif diff --git a/test/webfuse/mocks/mock_operation_context.cc b/test/webfuse/mocks/mock_operation_context.cc new file mode 100644 index 0000000..9537533 --- /dev/null +++ b/test/webfuse/mocks/mock_operation_context.cc @@ -0,0 +1,27 @@ +#include "webfuse/mocks/mock_operation_context.hpp" +#include "webfuse/utils/wrap.hpp" + +extern "C" +{ +static webfuse_test::MockOperationContext * webfuse_test_MockOperationContext = nullptr; + +WF_WRAP_FUNC1(webfuse_test_MockOperationContext, + struct wf_jsonrpc_proxy *, wf_impl_operation_context_get_proxy, + struct wf_impl_operation_context *); + +} + +namespace webfuse_test +{ + +MockOperationContext::MockOperationContext() +{ + webfuse_test_MockOperationContext = this; +} + +MockOperationContext::~MockOperationContext() +{ + webfuse_test_MockOperationContext = nullptr; +} + +} \ No newline at end of file diff --git a/test/webfuse/mocks/mock_operation_context.hpp b/test/webfuse/mocks/mock_operation_context.hpp new file mode 100644 index 0000000..e692e76 --- /dev/null +++ b/test/webfuse/mocks/mock_operation_context.hpp @@ -0,0 +1,22 @@ +#ifndef MOCK_OPERATIONS_CONTEXT_HPP +#define MOCK_OPERATIONS_CONTEXT_HPP + +#include "webfuse/adapter/impl/operation/context.h" +#include + +namespace webfuse_test +{ + +class MockOperationContext +{ +public: + MockOperationContext(); + virtual ~MockOperationContext(); + MOCK_METHOD1(wf_impl_operation_context_get_proxy, wf_jsonrpc_proxy * ( + struct wf_impl_operation_context * context)); + +}; + +} + +#endif diff --git a/test/webfuse/mocks/mock_provider.cc b/test/webfuse/mocks/mock_provider.cc new file mode 100644 index 0000000..a81b302 --- /dev/null +++ b/test/webfuse/mocks/mock_provider.cc @@ -0,0 +1,138 @@ +#include "webfuse/mocks/mock_provider.hpp" + +extern "C" +{ +using webfuse_test::MockProvider; + +static void webfuse_test_MockProvider_connected( + void * user_data) +{ + auto * provider = reinterpret_cast(user_data); + provider->connected(); +} + +static void webfuse_test_MockProvider_disconnected( + void * user_data) +{ + auto * provider = reinterpret_cast(user_data); + provider->disconnected(); +} + +static void webfuse_test_MockProvider_on_timer( + void * user_data) +{ + auto * provider = reinterpret_cast(user_data); + provider->on_timer(); +} + +static void webfuse_test_MockProvider_lookup( + wfp_request * request, + ino_t parent, + char const * name, + void * user_data) +{ + auto * provider = reinterpret_cast(user_data); + provider->lookup(request, parent, name); +} + +static void webfuse_test_MockProvider_getattr( + wfp_request * request, + ino_t inode, + void * user_data) +{ + auto * provider = reinterpret_cast(user_data); + provider->getattr(request, inode); +} + +static void webfuse_test_MockProvider_readdir( + wfp_request * request, + ino_t directory, + void * user_data) +{ + auto * provider = reinterpret_cast(user_data); + provider->readdir(request, directory); +} + +static void webfuse_test_MockProvider_open( + wfp_request * request, + ino_t inode, + int flags, + void * user_data) +{ + auto * provider = reinterpret_cast(user_data); + provider->open(request, inode, flags); + +} + +static void webfuse_test_MockProvider_close( + ino_t inode, + uint32_t handle, + int flags, + void * user_data) +{ + auto * provider = reinterpret_cast(user_data); + provider->close(inode, handle, flags); + +} + +static void webfuse_test_MockProvider_read( + wfp_request * request, + ino_t inode, + uint32_t handle, + size_t offset, + size_t length, + void * user_data) +{ + auto * provider = reinterpret_cast(user_data); + provider->read(request, inode, handle, offset, length); +} + +static void webfuse_test_MockProvider_get_credentials( + wfp_credentials * credentials, + void * user_data) +{ + auto * provider = reinterpret_cast(user_data); + provider->get_credentials(credentials); +} + + +static wfp_provider const webfuse_test_MockProvider = +{ + &webfuse_test_MockProvider_connected, + &webfuse_test_MockProvider_disconnected, + &webfuse_test_MockProvider_on_timer, + &webfuse_test_MockProvider_lookup, + &webfuse_test_MockProvider_getattr, + &webfuse_test_MockProvider_readdir, + &webfuse_test_MockProvider_open, + &webfuse_test_MockProvider_close, + &webfuse_test_MockProvider_read, + &webfuse_test_MockProvider_get_credentials, +}; + +} + +namespace webfuse_test +{ + +MockProvider::MockProvider() +{ + +} + +MockProvider::~MockProvider() +{ + +} + +wfp_provider const * MockProvider::get_provider() +{ + return &webfuse_test_MockProvider; +} + +void * MockProvider::get_userdata() +{ + return reinterpret_cast(this); +} + +} \ No newline at end of file diff --git a/test/webfuse/mocks/mock_provider.hpp b/test/webfuse/mocks/mock_provider.hpp new file mode 100644 index 0000000..1f03b08 --- /dev/null +++ b/test/webfuse/mocks/mock_provider.hpp @@ -0,0 +1,31 @@ +#ifndef WF_MOCK_PROVIDER_HPP +#define WF_MOCK_PROVIDER_HPP + +#include "webfuse/provider/impl/provider.h" +#include + +namespace webfuse_test +{ + +class MockProvider +{ +public: + MockProvider(); + ~MockProvider(); + wfp_provider const * get_provider(); + void * get_userdata(); + MOCK_METHOD0(connected, void ()); + MOCK_METHOD0(disconnected, void ()); + MOCK_METHOD0(on_timer, void()); + MOCK_METHOD3(lookup, void(wfp_request * request, ino_t parent, char const * name)); + MOCK_METHOD2(getattr, void(wfp_request * request, ino_t inode)); + MOCK_METHOD2(readdir, void(wfp_request * request, ino_t directory)); + MOCK_METHOD3(open, void(wfp_request * request, ino_t inode, int flags)); + MOCK_METHOD3(close, void(ino_t inode, uint32_t handle, int flags)); + MOCK_METHOD5(read, void(wfp_request * request, ino_t inode, uint32_t handle, size_t offset, size_t length)); + MOCK_METHOD1(get_credentials, void(wfp_credentials * credentials)); +}; + +} + +#endif diff --git a/test/webfuse/mocks/mock_request.cc b/test/webfuse/mocks/mock_request.cc index c2dd441..5b31c63 100644 --- a/test/webfuse/mocks/mock_request.cc +++ b/test/webfuse/mocks/mock_request.cc @@ -1,15 +1,14 @@ #include "webfuse/mocks/mock_request.hpp" #include -namespace +extern "C" { -extern "C" void -respond( +static void webfuse_test_MockRequest_respond( json_t * response, void * user_data) { - webfuse_test::Request * request = reinterpret_cast(user_data); + auto * request = reinterpret_cast(user_data); json_t * result = json_object_get(response, "result"); json_t * error = json_object_get(response, "error"); @@ -31,21 +30,17 @@ respond( } } - } namespace webfuse_test { -struct wfp_request * -request_create( - Request * req, - int id) +struct wfp_request * MockRequest::create_request(int id) { - struct wfp_request * request = reinterpret_cast(malloc(sizeof(struct wfp_request))); + auto * request = reinterpret_cast(malloc(sizeof(wfp_request))); + request->respond = &webfuse_test_MockRequest_respond; + request->user_data = reinterpret_cast(this); request->id = id; - request->user_data = reinterpret_cast(req); - request->respond = &respond; return request; } diff --git a/test/webfuse/mocks/mock_request.hpp b/test/webfuse/mocks/mock_request.hpp index 51884f4..0582d6a 100644 --- a/test/webfuse/mocks/mock_request.hpp +++ b/test/webfuse/mocks/mock_request.hpp @@ -10,27 +10,15 @@ 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 +class MockRequest { public: + struct wfp_request * create_request(int id); 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, "") +MATCHER_P3(StatMatcher, 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))) @@ -56,6 +44,43 @@ MATCHER_P3(GetAttrMatcher, 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_P3(ReadResultMatcher, data, format, count, "") +{ + json_t * format_holder = json_object_get(arg, "format"); + if ((!json_is_string(format_holder)) || (0 != strcmp(format, json_string_value(format_holder)))) + { + *result_listener << "invalid or missing format: " << json_string_value(format_holder); + return false; + } + + json_t * count_holder = json_object_get(arg, "count"); + if ((!json_is_integer(count_holder)) || (count != json_integer_value(count_holder))) + { + *result_listener << "invalid or missing count: " << json_integer_value(count_holder); + return false; + } + + json_t * data_holder = json_object_get(arg, "data"); + if ((!json_is_string(data_holder)) || (0 != strcmp(data, json_string_value(data_holder)))) + { + *result_listener << "invalid or missing data: " << json_string_value(data_holder); + return false; + } + + return true; +} MATCHER_P(ReaddirMatcher, contained_elements , "") { diff --git a/test/webfuse/tests/adapter/operation/test_close.cc b/test/webfuse/tests/adapter/operation/test_close.cc new file mode 100644 index 0000000..00e4763 --- /dev/null +++ b/test/webfuse/tests/adapter/operation/test_close.cc @@ -0,0 +1,56 @@ +#include "webfuse/adapter/impl/operation/close.h" +#include + +#include "webfuse/mocks/mock_fuse.hpp" +#include "webfuse/mocks/mock_operation_context.hpp" +#include "webfuse/mocks/mock_jsonrpc_proxy.hpp" + +#include + +using webfuse_test::MockJsonRpcProxy; +using webfuse_test::MockOperationContext; +using webfuse_test::FuseMock; +using testing::_; +using testing::Return; +using testing::StrEq; + +TEST(wf_impl_operation_close, notify_proxy) +{ + MockJsonRpcProxy proxy; + EXPECT_CALL(proxy, wf_jsonrpc_proxy_vnotify(_,StrEq("close"),StrEq("siii"))).Times(1); + + MockOperationContext context; + EXPECT_CALL(context, wf_impl_operation_context_get_proxy(_)).Times(1) + .WillOnce(Return(reinterpret_cast(&proxy))); + + wf_impl_operation_context op_context; + op_context.name = nullptr; + FuseMock fuse; + EXPECT_CALL(fuse, fuse_req_userdata(_)).Times(1).WillOnce(Return(&op_context)); + EXPECT_CALL(fuse, fuse_reply_err(_, 0)).Times(1).WillOnce(Return(0)); + + fuse_req_t request = nullptr; + fuse_ino_t inode = 1; + fuse_file_info file_info; + file_info.flags = 0; + wf_impl_operation_close(request, inode, &file_info); +} + +TEST(wf_impl_operation_close, fail_rpc_null) +{ + MockJsonRpcProxy proxy; + EXPECT_CALL(proxy, wf_jsonrpc_proxy_vnotify(_,_,_)).Times(0); + + MockOperationContext context; + EXPECT_CALL(context, wf_impl_operation_context_get_proxy(_)).Times(1) + .WillOnce(Return(nullptr)); + + FuseMock fuse; + EXPECT_CALL(fuse, fuse_req_userdata(_)).Times(1).WillOnce(Return(nullptr)); + EXPECT_CALL(fuse, fuse_reply_err(_, 0)).Times(1).WillOnce(Return(0)); + + fuse_req_t request = nullptr; + fuse_ino_t inode = 1; + fuse_file_info * file_info = nullptr; + wf_impl_operation_close(request, inode, file_info); +} diff --git a/test/webfuse/tests/adapter/operation/test_context.cc b/test/webfuse/tests/adapter/operation/test_context.cc new file mode 100644 index 0000000..77aa287 --- /dev/null +++ b/test/webfuse/tests/adapter/operation/test_context.cc @@ -0,0 +1,23 @@ +#include "webfuse/adapter/impl/operation/context.h" +#include "webfuse/adapter/impl/session.h" +#include + +TEST(wf_impl_operation_context, get_proxy) +{ + wf_jsonrpc_proxy * proxy = reinterpret_cast(42); + wf_impl_session session; + session.rpc = proxy; + wf_impl_operation_context context; + context.session = &session; + + ASSERT_EQ(proxy, wf_impl_operation_context_get_proxy(&context)); +} + +TEST(wf_impl_operation_context, get_proxy_fail_no_session) +{ + wf_impl_operation_context context; + context.session = nullptr; + + ASSERT_EQ(nullptr, wf_impl_operation_context_get_proxy(&context)); + +} \ No newline at end of file diff --git a/test/webfuse/tests/adapter/operation/test_getattr.cc b/test/webfuse/tests/adapter/operation/test_getattr.cc new file mode 100644 index 0000000..8249692 --- /dev/null +++ b/test/webfuse/tests/adapter/operation/test_getattr.cc @@ -0,0 +1,213 @@ +#include "webfuse/adapter/impl/operation/getattr.h" + +#include "webfuse/core/status.h" + +#include "webfuse/mocks/mock_fuse.hpp" +#include "webfuse/mocks/mock_operation_context.hpp" +#include "webfuse/mocks/mock_jsonrpc_proxy.hpp" + +#include + +using webfuse_test::MockJsonRpcProxy; +using webfuse_test::MockOperationContext; +using webfuse_test::FuseMock; +using testing::_; +using testing::Return; +using testing::Invoke; +using testing::StrEq; + +namespace +{ + +void free_context( + struct wf_jsonrpc_proxy * , + wf_jsonrpc_proxy_finished_fn * , + void * user_data, + char const * , + char const *) +{ + free(user_data); +} + +} + +TEST(wf_impl_operation_getattr, invoke_proxy) +{ + MockJsonRpcProxy proxy; + EXPECT_CALL(proxy, wf_jsonrpc_proxy_vinvoke(_,_,_,StrEq("getattr"),StrEq("si"))).Times(1) + .WillOnce(Invoke(free_context)); + + MockOperationContext context; + EXPECT_CALL(context, wf_impl_operation_context_get_proxy(_)).Times(1) + .WillOnce(Return(reinterpret_cast(&proxy))); + + wf_impl_operation_context op_context; + op_context.name = nullptr; + fuse_ctx fuse_context; + fuse_context.gid = 0; + fuse_context.uid = 0; + FuseMock fuse; + EXPECT_CALL(fuse, fuse_req_ctx(_)).Times(1).WillOnce(Return(&fuse_context)); + EXPECT_CALL(fuse, fuse_req_userdata(_)).Times(1).WillOnce(Return(&op_context)); + + + fuse_req_t request = nullptr; + fuse_ino_t inode = 1; + fuse_file_info * file_info = nullptr; + wf_impl_operation_getattr(request, inode, file_info); +} + +TEST(wf_impl_operation_getattr, fail_rpc_null) +{ + MockOperationContext context; + EXPECT_CALL(context, wf_impl_operation_context_get_proxy(_)).Times(1) + .WillOnce(Return(nullptr)); + + FuseMock fuse; + EXPECT_CALL(fuse, fuse_req_ctx(_)).Times(1).WillOnce(Return(nullptr)); + EXPECT_CALL(fuse, fuse_req_userdata(_)).Times(1).WillOnce(Return(nullptr)); + 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_getattr(request, inode, file_info); +} + +TEST(wf_impl_operation_getattr, finished_file) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_attr(_,_,_)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "mode", json_integer(0755)); + json_object_set_new(result, "type", json_string("file")); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_getattr_context))); + context->inode = 1; + context->gid = 0; + context->uid = 0; + wf_impl_operation_getattr_finished(context, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_getattr, finished_dir) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_attr(_,_,_)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "mode", json_integer(0755)); + json_object_set_new(result, "type", json_string("dir")); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_getattr_context))); + context->inode = 1; + context->gid = 0; + context->uid = 0; + wf_impl_operation_getattr_finished(context, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_getattr, finished_unknown_type) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_attr(_,_,_)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "mode", json_integer(0755)); + json_object_set_new(result, "type", json_string("unknown")); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_getattr_context))); + context->inode = 1; + context->gid = 0; + context->uid = 0; + wf_impl_operation_getattr_finished(context, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_getattr, finished_fail_missing_mode) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "type", json_string("file")); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_getattr_context))); + context->inode = 1; + context->gid = 0; + context->uid = 0; + wf_impl_operation_getattr_finished(context, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_getattr, finished_fail_invalid_mode_type) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "mode", json_string("0755")); + json_object_set_new(result, "type", json_string("file")); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_getattr_context))); + context->inode = 1; + context->gid = 0; + context->uid = 0; + wf_impl_operation_getattr_finished(context, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_getattr, finished_fail_type_mode) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "mode", json_integer(0755)); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_getattr_context))); + context->inode = 1; + context->gid = 0; + context->uid = 0; + wf_impl_operation_getattr_finished(context, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_getattr, finished_fail_invalid_type_type) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "mode", json_integer(0755)); + json_object_set_new(result, "type", json_integer(42)); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_getattr_context))); + context->inode = 1; + context->gid = 0; + context->uid = 0; + wf_impl_operation_getattr_finished(context, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_getattr, finished_error) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * error = json_object(); + json_object_set_new(error, "code", json_integer(WF_BAD)); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_getattr_context))); + context->inode = 1; + context->gid = 0; + context->uid = 0; + wf_impl_operation_getattr_finished(context, nullptr, error); + json_decref(error); +} diff --git a/test/webfuse/tests/adapter/operation/test_lookup.cc b/test/webfuse/tests/adapter/operation/test_lookup.cc new file mode 100644 index 0000000..002fc30 --- /dev/null +++ b/test/webfuse/tests/adapter/operation/test_lookup.cc @@ -0,0 +1,255 @@ +#include "webfuse/adapter/impl/operation/lookup.h" + +#include "webfuse/core/status.h" + +#include "webfuse/mocks/mock_fuse.hpp" +#include "webfuse/mocks/mock_operation_context.hpp" +#include "webfuse/mocks/mock_jsonrpc_proxy.hpp" + +#include + +using webfuse_test::MockJsonRpcProxy; +using webfuse_test::MockOperationContext; +using webfuse_test::FuseMock; +using testing::_; +using testing::Return; +using testing::Invoke; +using testing::StrEq; + +namespace +{ + +void free_context( + struct wf_jsonrpc_proxy * , + wf_jsonrpc_proxy_finished_fn * , + void * user_data, + char const * , + char const *) +{ + free(user_data); +} + +} + +TEST(wf_impl_operation_lookup, invoke_proxy) +{ + MockJsonRpcProxy proxy; + EXPECT_CALL(proxy, wf_jsonrpc_proxy_vinvoke(_,_,_,StrEq("lookup"),StrEq("sis"))).Times(1) + .WillOnce(Invoke(free_context)); + + MockOperationContext context; + EXPECT_CALL(context, wf_impl_operation_context_get_proxy(_)).Times(1) + .WillOnce(Return(reinterpret_cast(&proxy))); + + wf_impl_operation_context op_context; + op_context.name = nullptr; + fuse_ctx fuse_context; + fuse_context.gid = 0; + fuse_context.uid = 0; + FuseMock fuse; + EXPECT_CALL(fuse, fuse_req_ctx(_)).Times(1).WillOnce(Return(&fuse_context)); + EXPECT_CALL(fuse, fuse_req_userdata(_)).Times(1).WillOnce(Return(&op_context)); + + + fuse_req_t request = nullptr; + fuse_ino_t inode = 1; + wf_impl_operation_lookup(request, inode, "some.file"); +} + +TEST(wf_impl_operation_lookup, fail_rpc_null) +{ + MockOperationContext context; + EXPECT_CALL(context, wf_impl_operation_context_get_proxy(_)).Times(1) + .WillOnce(Return(nullptr)); + + FuseMock fuse; + EXPECT_CALL(fuse, fuse_req_ctx(_)).Times(1).WillOnce(Return(nullptr)); + EXPECT_CALL(fuse, fuse_req_userdata(_)).Times(1).WillOnce(Return(nullptr)); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + fuse_req_t request = nullptr; + fuse_ino_t inode = 1; + wf_impl_operation_lookup(request, inode, "some.file"); +} + +TEST(wf_impl_operation_lookup, finished_file) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_entry(_,_)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "inode", json_integer(42)); + json_object_set_new(result, "mode", json_integer(0755)); + json_object_set_new(result, "type", json_string("file")); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_lookup_context))); + context->timeout = 1.0; + context->gid = 0; + context->uid = 0; + wf_impl_operation_lookup_finished(context, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_lookup, finished_dir) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_entry(_,_)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "inode", json_integer(42)); + json_object_set_new(result, "mode", json_integer(0755)); + json_object_set_new(result, "type", json_string("dir")); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_lookup_context))); + context->timeout = 1.0; + context->gid = 0; + context->uid = 0; + wf_impl_operation_lookup_finished(context, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_lookup, finished_unknown_type) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_entry(_,_)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "inode", json_integer(42)); + json_object_set_new(result, "mode", json_integer(0755)); + json_object_set_new(result, "type", json_string("unknown")); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_lookup_context))); + context->timeout = 1.0; + context->gid = 0; + context->uid = 0; + wf_impl_operation_lookup_finished(context, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_lookup, finished_fail_missing_inode) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "mode", json_integer(0755)); + json_object_set_new(result, "type", json_string("file")); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_lookup_context))); + context->timeout = 1.0; + context->gid = 0; + context->uid = 0; + wf_impl_operation_lookup_finished(context, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_lookup, finished_fail_invalid_inode_type) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "inode", json_string("42")); + json_object_set_new(result, "mode", json_string("0755")); + json_object_set_new(result, "type", json_string("file")); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_lookup_context))); + context->timeout = 1.0; + context->gid = 0; + context->uid = 0; + wf_impl_operation_lookup_finished(context, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_lookup, finished_fail_missing_mode) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "inode", json_integer(42)); + json_object_set_new(result, "type", json_string("file")); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_lookup_context))); + context->timeout = 1.0; + context->gid = 0; + context->uid = 0; + wf_impl_operation_lookup_finished(context, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_lookup, finished_fail_invalid_mode_type) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "inode", json_integer(42)); + json_object_set_new(result, "mode", json_string("0755")); + json_object_set_new(result, "type", json_string("file")); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_lookup_context))); + context->timeout = 1.0; + context->gid = 0; + context->uid = 0; + wf_impl_operation_lookup_finished(context, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_lookup, finished_fail_type_mode) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "inode", json_integer(42)); + json_object_set_new(result, "mode", json_integer(0755)); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_lookup_context))); + context->timeout = 1.0; + context->gid = 0; + context->uid = 0; + wf_impl_operation_lookup_finished(context, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_lookup, finished_fail_invalid_type_type) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "inode", json_integer(42)); + json_object_set_new(result, "mode", json_integer(0755)); + json_object_set_new(result, "type", json_integer(42)); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_lookup_context))); + context->timeout = 1.0; + context->gid = 0; + context->uid = 0; + wf_impl_operation_lookup_finished(context, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_lookup, finished_error) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * error = json_object(); + json_object_set_new(error, "code", json_integer(WF_BAD)); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_lookup_context))); + context->timeout = 1.0; + context->gid = 0; + context->uid = 0; + wf_impl_operation_lookup_finished(context, nullptr, error); + json_decref(error); +} 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..e3d89bc --- /dev/null +++ b/test/webfuse/tests/adapter/operation/test_open.cc @@ -0,0 +1,100 @@ +#include "webfuse/adapter/impl/operation/open.h" + +#include "webfuse/core/status.h" + +#include "webfuse/mocks/mock_fuse.hpp" +#include "webfuse/mocks/mock_operation_context.hpp" +#include "webfuse/mocks/mock_jsonrpc_proxy.hpp" + +#include + +using webfuse_test::MockJsonRpcProxy; +using webfuse_test::MockOperationContext; +using webfuse_test::FuseMock; +using testing::_; +using testing::Return; +using testing::StrEq; + +TEST(wf_impl_operation_open, invoke_proxy) +{ + MockJsonRpcProxy proxy; + EXPECT_CALL(proxy, wf_jsonrpc_proxy_vinvoke(_,_,_,StrEq("open"),StrEq("sii"))).Times(1); + + MockOperationContext context; + EXPECT_CALL(context, wf_impl_operation_context_get_proxy(_)).Times(1) + .WillOnce(Return(reinterpret_cast(&proxy))); + + wf_impl_operation_context op_context; + op_context.name = nullptr; + FuseMock fuse; + EXPECT_CALL(fuse, fuse_req_userdata(_)).Times(1).WillOnce(Return(&op_context)); + + + fuse_req_t request = nullptr; + fuse_ino_t inode = 1; + fuse_file_info file_info; + file_info.flags = 0; + wf_impl_operation_open(request, inode, &file_info); +} + +TEST(wf_impl_operation_open, fail_rpc_null) +{ + MockOperationContext context; + EXPECT_CALL(context, wf_impl_operation_context_get_proxy(_)).Times(1) + .WillOnce(Return(nullptr)); + + FuseMock fuse; + EXPECT_CALL(fuse, fuse_req_userdata(_)).Times(1).WillOnce(Return(nullptr)); + 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); +} + +TEST(wf_impl_operation_open, finished) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "handle", json_integer(42)); + wf_impl_operation_open_finished(nullptr, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_open, finished_fail_error) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * error = json_object(); + json_object_set_new(error, "code", json_integer(WF_BAD)); + wf_impl_operation_open_finished(nullptr, nullptr, error); + json_decref(error); +} + +TEST(wf_impl_operation_open, finished_fail_no_handle) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + wf_impl_operation_open_finished(nullptr, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_open, finished_fail_invalid_handle_type) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_open(_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "handle", json_string("42")); + wf_impl_operation_open_finished(nullptr, result, nullptr); + json_decref(result); +} diff --git a/test/webfuse/tests/adapter/operation/test_read.cc b/test/webfuse/tests/adapter/operation/test_read.cc new file mode 100644 index 0000000..15b9f9c --- /dev/null +++ b/test/webfuse/tests/adapter/operation/test_read.cc @@ -0,0 +1,236 @@ +#include "webfuse/adapter/impl/operation/read.h" + +#include "webfuse/mocks/mock_fuse.hpp" +#include "webfuse/mocks/mock_operation_context.hpp" +#include "webfuse/mocks/mock_jsonrpc_proxy.hpp" + +#include + +using webfuse_test::MockJsonRpcProxy; +using webfuse_test::MockOperationContext; +using webfuse_test::FuseMock; +using testing::_; +using testing::Return; +using testing::StrEq; + +TEST(wf_impl_operation_read, invoke_proxy) +{ + MockJsonRpcProxy proxy; + EXPECT_CALL(proxy, wf_jsonrpc_proxy_vinvoke(_,_,_,StrEq("read"),StrEq("siiii"))).Times(1); + + MockOperationContext context; + EXPECT_CALL(context, wf_impl_operation_context_get_proxy(_)).Times(1) + .WillOnce(Return(reinterpret_cast(&proxy))); + + wf_impl_operation_context op_context; + op_context.name = nullptr; + FuseMock fuse; + EXPECT_CALL(fuse, fuse_req_userdata(_)).Times(1).WillOnce(Return(&op_context)); + + fuse_req_t request = nullptr; + fuse_ino_t inode = 1; + size_t size = 42; + off_t offset = 0; + fuse_file_info file_info; + file_info.fh = 1; + wf_impl_operation_read(request, inode, size, offset, &file_info); +} + +TEST(wf_impl_operation_read, invoke_proxy_limit_size) +{ + MockJsonRpcProxy proxy; + EXPECT_CALL(proxy, wf_jsonrpc_proxy_vinvoke(_,_,_,StrEq("read"),StrEq("siiii"))).Times(1); + + MockOperationContext context; + EXPECT_CALL(context, wf_impl_operation_context_get_proxy(_)).Times(1) + .WillOnce(Return(reinterpret_cast(&proxy))); + + wf_impl_operation_context op_context; + op_context.name = nullptr; + FuseMock fuse; + EXPECT_CALL(fuse, fuse_req_userdata(_)).Times(1).WillOnce(Return(&op_context)); + + fuse_req_t request = nullptr; + fuse_ino_t inode = 1; + size_t size = 100 * 1024 * 1024; + off_t offset = 0; + fuse_file_info file_info; + file_info.fh = 1; + wf_impl_operation_read(request, inode, size, offset, &file_info); +} + +TEST(wf_impl_operation_read, fail_rpc_null) +{ + MockOperationContext context; + EXPECT_CALL(context, wf_impl_operation_context_get_proxy(_)).Times(1) + .WillOnce(Return(nullptr)); + + FuseMock fuse; + EXPECT_CALL(fuse, fuse_req_userdata(_)).Times(1).WillOnce(Return(nullptr)); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + fuse_req_t request = nullptr; + fuse_ino_t inode = 1; + size_t size = 42; + off_t offset = 0; + fuse_file_info * file_info = nullptr; + wf_impl_operation_read(request, inode, size, offset, file_info); +} + +TEST(wf_impl_operation_read, fill_buffer_identity) +{ + wf_status status; + char * buffer = wf_impl_fill_buffer("brummni", 8, "identity", 8, &status); + ASSERT_EQ(WF_GOOD, status); + ASSERT_STREQ("brummni", buffer); + free(buffer); +} + +TEST(wf_impl_operation_read, fill_buffer_identity_fail_inconsistent_size) +{ + wf_status status; + char * buffer = wf_impl_fill_buffer("brummni", 8, "identity", 7, &status); + ASSERT_NE(WF_GOOD, status); + ASSERT_EQ(nullptr, buffer); +} + +TEST(wf_impl_operation_read, fill_buffer_base64) +{ + wf_status status; + char * buffer = wf_impl_fill_buffer("YnJ1bW1uaQ==", 12, "base64", 7, &status); + + ASSERT_EQ(WF_GOOD, status); + ASSERT_EQ(0, strncmp("brummni", buffer, 7)); + free(buffer); +} + +TEST(wf_impl_operation_read, fill_buffer_base64_fail_invalid_data) +{ + wf_status status; + char * buffer = wf_impl_fill_buffer("YnJ1bW1uaQ=A", 12, "base64", 8, &status); + ASSERT_NE(WF_GOOD, status); + ASSERT_EQ(nullptr, buffer); +} + +TEST(wf_impl_operation_read, fill_buffer_empty) +{ + wf_status status; + char * buffer = wf_impl_fill_buffer(nullptr, 0, "identity", 0, &status); + + ASSERT_EQ(WF_GOOD, status); + free(buffer); +} + +TEST(wf_impl_operation_read, fill_buffer_fail_invalid_format) +{ + wf_status status; + char * buffer = wf_impl_fill_buffer("some data", 9, "unknown", 9, &status); + ASSERT_NE(WF_GOOD, status); + ASSERT_EQ(nullptr, buffer); +} + +TEST(wf_impl_operation_read, finished) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_buf(_,_,7)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "data", json_string("brummni")); + json_object_set_new(result, "format", json_string("identity")); + json_object_set_new(result, "count", json_integer(7)); + wf_impl_operation_read_finished(nullptr, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_read, finished_fail_no_data) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, _)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "format", json_string("identity")); + json_object_set_new(result, "count", json_integer(7)); + wf_impl_operation_read_finished(nullptr, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_read, finished_fail_invalid_data_type) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, _)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "data", json_integer(42)); + json_object_set_new(result, "format", json_string("identity")); + json_object_set_new(result, "count", json_integer(7)); + wf_impl_operation_read_finished(nullptr, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_read, finished_fail_no_format) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, _)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "data", json_string("brummni")); + json_object_set_new(result, "count", json_integer(7)); + wf_impl_operation_read_finished(nullptr, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_read, finished_fail_invalid_format_type) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, _)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "data", json_string("brummni")); + json_object_set_new(result, "format", json_integer(42)); + json_object_set_new(result, "count", json_integer(7)); + wf_impl_operation_read_finished(nullptr, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_read, finished_fail_no_count) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, _)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "data", json_string("brummni")); + json_object_set_new(result, "format", json_string("identity")); + wf_impl_operation_read_finished(nullptr, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_read, finished_fail_invalid_count_type) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, _)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + json_object_set_new(result, "data", json_string("brummni")); + json_object_set_new(result, "format", json_string("identity")); + json_object_set_new(result, "count", json_string("7")); + wf_impl_operation_read_finished(nullptr, result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_read, finished_fail_error) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, _)).Times(1).WillOnce(Return(0)); + + json_t * error = json_object(); + json_object_set_new(error, "code", json_integer(WF_BAD)); + wf_impl_operation_read_finished(nullptr, nullptr, error); + json_decref(error); +} diff --git a/test/webfuse/tests/adapter/operation/test_readdir.cc b/test/webfuse/tests/adapter/operation/test_readdir.cc new file mode 100644 index 0000000..c020c3d --- /dev/null +++ b/test/webfuse/tests/adapter/operation/test_readdir.cc @@ -0,0 +1,265 @@ +#include "webfuse/adapter/impl/operation/readdir.h" + +#include "webfuse/core/status.h" + +#include "webfuse/mocks/mock_fuse.hpp" +#include "webfuse/mocks/mock_operation_context.hpp" +#include "webfuse/mocks/mock_jsonrpc_proxy.hpp" + +#include + +using webfuse_test::MockJsonRpcProxy; +using webfuse_test::MockOperationContext; +using webfuse_test::FuseMock; +using testing::_; +using testing::Return; +using testing::Invoke; +using testing::StrEq; + +namespace +{ + +void free_context( + struct wf_jsonrpc_proxy * , + wf_jsonrpc_proxy_finished_fn * , + void * user_data, + char const * , + char const *) +{ + free(user_data); +} + +} + +TEST(wf_impl_operation_readdir, invoke_proxy) +{ + MockJsonRpcProxy proxy; + EXPECT_CALL(proxy, wf_jsonrpc_proxy_vinvoke(_,_,_,StrEq("readdir"),StrEq("si"))) + .Times(1).WillOnce(Invoke(free_context)); + + MockOperationContext context; + EXPECT_CALL(context, wf_impl_operation_context_get_proxy(_)).Times(1) + .WillOnce(Return(reinterpret_cast(&proxy))); + + wf_impl_operation_context op_context; + op_context.name = nullptr; + FuseMock fuse; + EXPECT_CALL(fuse, fuse_req_userdata(_)).Times(1).WillOnce(Return(&op_context)); + + + fuse_req_t request = nullptr; + fuse_ino_t inode = 1; + size_t size = 10; + size_t offset = 0; + fuse_file_info file_info; + file_info.flags = 0; + wf_impl_operation_readdir(request, inode, size, offset, &file_info); +} + +TEST(wf_impl_operation_readdir, fail_rpc_null) +{ + MockOperationContext context; + EXPECT_CALL(context, wf_impl_operation_context_get_proxy(_)).Times(1) + .WillOnce(Return(nullptr)); + + FuseMock fuse; + EXPECT_CALL(fuse, fuse_req_userdata(_)).Times(1).WillOnce(Return(nullptr)); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + fuse_req_t request = nullptr; + fuse_ino_t inode = 1; + size_t size = 10; + size_t offset = 0; + fuse_file_info file_info; + file_info.flags = 0; + wf_impl_operation_readdir(request, inode, size, offset, &file_info); +} + +TEST(wf_impl_operation_readdir, finished) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(1).WillOnce(Return(0)); + + json_t * result = json_array(); + json_t * item = json_object(); + json_object_set_new(item, "name", json_string("a.file")); + json_object_set_new(item, "inode", json_integer(42)); + json_array_append_new(result, item); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_readdir_context))); + context->request = nullptr; + context->size = 1; + context->offset = 0; + wf_impl_operation_readdir_finished(reinterpret_cast(context), result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_readdir, finished_many_items) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(1).WillOnce(Return(0)); + + json_t * result = json_array(); + for(int i = 0; i < 100; i++) + { + json_t * item = json_object(); + json_object_set_new(item, "name", json_sprintf("file_%d.txt", i)); + json_object_set_new(item, "inode", json_integer(1 + i)); + json_array_append_new(result, item); + } + + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_readdir_context))); + context->request = nullptr; + context->size = 100; + context->offset = 0; + wf_impl_operation_readdir_finished(reinterpret_cast(context), result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_readdir, finished_read_after_end) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(1).WillOnce(Return(0)); + + json_t * result = json_array(); + json_t * item = json_object(); + json_object_set_new(item, "name", json_string("a.file")); + json_object_set_new(item, "inode", json_integer(42)); + json_array_append_new(result, item); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_readdir_context))); + context->request = nullptr; + context->size = 10; + context->offset = 2; + wf_impl_operation_readdir_finished(reinterpret_cast(context), result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_readdir, finished_fail_error) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * error = json_array(); + json_object_set_new(error, "code", json_integer(WF_BAD)); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_readdir_context))); + context->request = nullptr; + context->size = 1; + context->offset = 0; + wf_impl_operation_readdir_finished(reinterpret_cast(context), nullptr, error); + json_decref(error); +} + +TEST(wf_impl_operation_readdir, finished_fail_invalid_result_type) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * result = json_object(); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_readdir_context))); + context->request = nullptr; + context->size = 1; + context->offset = 0; + wf_impl_operation_readdir_finished(reinterpret_cast(context), result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_readdir, finished_fail_missing_name) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * result = json_array(); + json_t * item = json_object(); + json_object_set_new(item, "inode", json_integer(42)); + json_array_append_new(result, item); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_readdir_context))); + context->request = nullptr; + context->size = 1; + context->offset = 0; + wf_impl_operation_readdir_finished(reinterpret_cast(context), result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_readdir, finished_fail_invalid_name_type) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * result = json_array(); + json_t * item = json_object(); + json_object_set_new(item, "name", json_integer(42)); + json_object_set_new(item, "inode", json_integer(42)); + json_array_append_new(result, item); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_readdir_context))); + context->request = nullptr; + context->size = 1; + context->offset = 0; + wf_impl_operation_readdir_finished(reinterpret_cast(context), result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_readdir, finished_fail_missing_inode) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * result = json_array(); + json_t * item = json_object(); + json_object_set_new(item, "name", json_string("a.file")); + json_array_append_new(result, item); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_readdir_context))); + context->request = nullptr; + context->size = 1; + context->offset = 0; + wf_impl_operation_readdir_finished(reinterpret_cast(context), result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_readdir, finished_fail_invalid_inode_type) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * result = json_array(); + json_t * item = json_object(); + json_object_set_new(item, "name", json_string("a.file")); + json_object_set_new(item, "inode", json_string("42")); + json_array_append_new(result, item); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_readdir_context))); + context->request = nullptr; + context->size = 1; + context->offset = 0; + wf_impl_operation_readdir_finished(reinterpret_cast(context), result, nullptr); + json_decref(result); +} + +TEST(wf_impl_operation_readdir, finished_fail_invalid_item_type) +{ + FuseMock fuse; + EXPECT_CALL(fuse, fuse_reply_buf(_,_,_)).Times(0); + EXPECT_CALL(fuse, fuse_reply_err(_, ENOENT)).Times(1).WillOnce(Return(0)); + + json_t * result = json_array(); + json_array_append_new(result, json_string("item")); + + auto * context = reinterpret_cast(malloc(sizeof(wf_impl_operation_readdir_context))); + context->request = nullptr; + context->size = 1; + context->offset = 0; + wf_impl_operation_readdir_finished(reinterpret_cast(context), result, nullptr); + json_decref(result); +} diff --git a/test/webfuse/tests/adapter/test_credentials.cc b/test/webfuse/tests/adapter/test_credentials.cc index 72350d4..88e2552 100644 --- a/test/webfuse/tests/adapter/test_credentials.cc +++ b/test/webfuse/tests/adapter/test_credentials.cc @@ -1,5 +1,6 @@ #include +#include "webfuse/adapter/credentials.h" #include "webfuse/adapter/impl/credentials.h" #include @@ -8,7 +9,7 @@ TEST(Credentials, Type) struct wf_credentials creds; wf_impl_credentials_init(&creds, "test", nullptr); - ASSERT_STREQ("test", wf_impl_credentials_type(&creds)); + ASSERT_STREQ("test", wf_credentials_type(&creds)); wf_impl_credentials_cleanup(&creds); } @@ -20,9 +21,9 @@ TEST(Credentials, Get) json_object_set_new(data, "password", json_string("")); wf_impl_credentials_init(&creds, "username", data); - ASSERT_STREQ("username", wf_impl_credentials_type(&creds)); - ASSERT_STREQ("bob", wf_impl_credentials_get(&creds, "username")); - ASSERT_STREQ("", wf_impl_credentials_get(&creds, "password")); + ASSERT_STREQ("username", wf_credentials_type(&creds)); + ASSERT_STREQ("bob", wf_credentials_get(&creds, "username")); + ASSERT_STREQ("", wf_credentials_get(&creds, "password")); wf_impl_credentials_cleanup(&creds); json_decref(data); @@ -34,9 +35,9 @@ TEST(Credentials, FailedToGetNonexistingValue) json_t * data = json_object(); wf_impl_credentials_init(&creds, "username", data); - ASSERT_STREQ("username", wf_impl_credentials_type(&creds)); - ASSERT_STREQ(nullptr, wf_impl_credentials_get(&creds, "username")); - ASSERT_STREQ(nullptr, wf_impl_credentials_get(&creds, "password")); + ASSERT_STREQ("username", wf_credentials_type(&creds)); + ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "username")); + ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "password")); wf_impl_credentials_cleanup(&creds); json_decref(data); @@ -47,9 +48,9 @@ TEST(Credentials, FailedToGetWithoutData) struct wf_credentials creds; wf_impl_credentials_init(&creds, "username", nullptr); - ASSERT_STREQ("username", wf_impl_credentials_type(&creds)); - ASSERT_STREQ(nullptr, wf_impl_credentials_get(&creds, "username")); - ASSERT_STREQ(nullptr, wf_impl_credentials_get(&creds, "password")); + ASSERT_STREQ("username", wf_credentials_type(&creds)); + ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "username")); + ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "password")); wf_impl_credentials_cleanup(&creds); } @@ -60,11 +61,25 @@ TEST(Credentials, FailedToGetWrongDataType) json_t * data = json_string("invalid_creds"); wf_impl_credentials_init(&creds, "username", data); - ASSERT_STREQ("username", wf_impl_credentials_type(&creds)); - ASSERT_STREQ(nullptr, wf_impl_credentials_get(&creds, "username")); - ASSERT_STREQ(nullptr, wf_impl_credentials_get(&creds, "password")); + ASSERT_STREQ("username", wf_credentials_type(&creds)); + ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "username")); + ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "password")); wf_impl_credentials_cleanup(&creds); json_decref(data); } +TEST(Credentials, FailedToGetWrongElementDataType) +{ + struct wf_credentials creds; + json_t * data = json_object(); + json_object_set_new(data, "username", json_integer(42)); + + wf_impl_credentials_init(&creds, "username", data); + ASSERT_STREQ("username", wf_credentials_type(&creds)); + ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "username")); + ASSERT_STREQ(nullptr, wf_credentials_get(&creds, "password")); + + wf_impl_credentials_cleanup(&creds); + json_decref(data); +} diff --git a/test/webfuse/tests/core/jsonrpc/mock_timer.cc b/test/webfuse/tests/core/jsonrpc/mock_timer.cc new file mode 100644 index 0000000..3cf8e74 --- /dev/null +++ b/test/webfuse/tests/core/jsonrpc/mock_timer.cc @@ -0,0 +1,31 @@ +#include "webfuse/tests/core/jsonrpc/mock_timer.hpp" +#include "webfuse/utils/wrap.hpp" + +extern "C" +{ +static wf_jsonrpc_test::ITimer * wf_jsonrpc_MockTimer = nullptr; + +WF_WRAP_FUNC0(wf_jsonrpc_MockTimer, wf_timer_manager *, wf_timer_manager_create); +WF_WRAP_FUNC1(wf_jsonrpc_MockTimer, void, wf_timer_manager_dispose, wf_timer_manager *); +WF_WRAP_FUNC1(wf_jsonrpc_MockTimer, void, wf_timer_manager_check, wf_timer_manager *); + +WF_WRAP_FUNC3(wf_jsonrpc_MockTimer, wf_timer *, wf_timer_create, wf_timer_manager *, wf_timer_on_timer_fn *, void *); +WF_WRAP_FUNC1(wf_jsonrpc_MockTimer, void, wf_timer_dispose, wf_timer *); +WF_WRAP_FUNC2(wf_jsonrpc_MockTimer, void, wf_timer_start, wf_timer *, int); +WF_WRAP_FUNC1(wf_jsonrpc_MockTimer, void, wf_timer_cancel, wf_timer *); + +} + +namespace wf_jsonrpc_test +{ +MockTimer::MockTimer() +{ + wf_jsonrpc_MockTimer = this; +} + +MockTimer::~MockTimer() +{ + wf_jsonrpc_MockTimer = nullptr; +} + +} \ No newline at end of file diff --git a/test/webfuse/tests/core/jsonrpc/mock_timer.hpp b/test/webfuse/tests/core/jsonrpc/mock_timer.hpp new file mode 100644 index 0000000..5c95852 --- /dev/null +++ b/test/webfuse/tests/core/jsonrpc/mock_timer.hpp @@ -0,0 +1,48 @@ +#ifndef WF_JSONRPC_MOCK_TIMERMANAGER_HPP +#define WF_JSONRPC_MOCK_TIMERMANAGER_HPP + +#include "webfuse/core/timer/timer.h" +#include "webfuse/core/timer/manager.h" +#include + +namespace wf_jsonrpc_test +{ + +class ITimer +{ +public: + virtual ~ITimer() = default; + virtual wf_timer_manager * wf_timer_manager_create() = 0; + virtual void wf_timer_manager_dispose(wf_timer_manager * manager) = 0; + virtual void wf_timer_manager_check(wf_timer_manager * manager) = 0; + virtual wf_timer * wf_timer_create( + wf_timer_manager * manager, + wf_timer_on_timer_fn * on_timer, + void * user_data) = 0; + virtual void wf_timer_dispose(wf_timer * timer) = 0; + virtual void wf_timer_start(wf_timer * timer, int timeout_ms) = 0; + virtual void wf_timer_cancel(wf_timer * timer) = 0; +}; + +class MockTimer: public ITimer +{ +public: + MockTimer(); + ~MockTimer() override; + MOCK_METHOD0(wf_timer_manager_create, wf_timer_manager * ()); + MOCK_METHOD1(wf_timer_manager_dispose, void(wf_timer_manager * manager)); + MOCK_METHOD1(wf_timer_manager_check, void (wf_timer_manager * manager)); + MOCK_METHOD3(wf_timer_create, wf_timer *( + wf_timer_manager * manager, + wf_timer_on_timer_fn * on_timer, + void * user_data)); + MOCK_METHOD1(wf_timer_dispose, void (wf_timer * timer)); + MOCK_METHOD2(wf_timer_start, void (wf_timer * timer, int timeout_ms)); + MOCK_METHOD1(wf_timer_cancel, void (wf_timer * timer)); + +}; + + +} + +#endif diff --git a/test/webfuse/tests/core/jsonrpc/mock_timer_callback.cc b/test/webfuse/tests/core/jsonrpc/mock_timer_callback.cc new file mode 100644 index 0000000..7e62c31 --- /dev/null +++ b/test/webfuse/tests/core/jsonrpc/mock_timer_callback.cc @@ -0,0 +1,41 @@ +#include "webfuse/tests/core/jsonrpc/mock_timer_callback.hpp" + +extern "C" +{ +using wf_jsonrpc_test::MockTimerCallback; + +static void wf_jsonrpc_test_MockTimerCallback_on_timer( + wf_timer * timer, + void * user_data) +{ + auto * self = reinterpret_cast(user_data); + self->on_timer(timer, user_data); +} + +} + +namespace wf_jsonrpc_test +{ + +MockTimerCallback::MockTimerCallback() +{ + +} + +MockTimerCallback::~MockTimerCallback() +{ + +} + +wf_timer_on_timer_fn * MockTimerCallback::on_timer_fn() +{ + return &wf_jsonrpc_test_MockTimerCallback_on_timer; +} + +void * MockTimerCallback::user_data() +{ + return reinterpret_cast(this); +} + + +} \ No newline at end of file diff --git a/test/webfuse/tests/core/jsonrpc/mock_timer_callback.hpp b/test/webfuse/tests/core/jsonrpc/mock_timer_callback.hpp new file mode 100644 index 0000000..e55b7e1 --- /dev/null +++ b/test/webfuse/tests/core/jsonrpc/mock_timer_callback.hpp @@ -0,0 +1,23 @@ +#ifndef WF_JSONRPC_MOCK_TIMERCALLBACK_HPP +#define WF_JSONRPC_MOCK_TIMERCALLBACK_HPP + +#include "webfuse/core/timer/on_timer_fn.h" +#include + +namespace wf_jsonrpc_test +{ +class MockTimerCallback +{ +public: + MockTimerCallback(); + virtual ~MockTimerCallback(); + wf_timer_on_timer_fn * on_timer_fn(); + void * user_data(); + + MOCK_METHOD2(on_timer, void (wf_timer * timer, void * user_data)); + +}; + +} + +#endif diff --git a/lib/wf/jsonrpc/test/wf/jsonrpc/test_is_request.cc b/test/webfuse/tests/core/jsonrpc/test_is_request.cc similarity index 98% rename from lib/wf/jsonrpc/test/wf/jsonrpc/test_is_request.cc rename to test/webfuse/tests/core/jsonrpc/test_is_request.cc index 55e40c6..4599611 100644 --- a/lib/wf/jsonrpc/test/wf/jsonrpc/test_is_request.cc +++ b/test/webfuse/tests/core/jsonrpc/test_is_request.cc @@ -1,5 +1,5 @@ #include -#include "wf/jsonrpc/request.h" +#include "webfuse/core/jsonrpc/request.h" TEST(wf_jsonrpc_is_request, request_with_object_params) { diff --git a/lib/wf/jsonrpc/test/wf/jsonrpc/test_is_response.cc b/test/webfuse/tests/core/jsonrpc/test_is_response.cc similarity index 98% rename from lib/wf/jsonrpc/test/wf/jsonrpc/test_is_response.cc rename to test/webfuse/tests/core/jsonrpc/test_is_response.cc index 690bb6c..908ddf0 100644 --- a/lib/wf/jsonrpc/test/wf/jsonrpc/test_is_response.cc +++ b/test/webfuse/tests/core/jsonrpc/test_is_response.cc @@ -1,5 +1,5 @@ #include -#include "wf/jsonrpc/response.h" +#include "webfuse/core/jsonrpc/response.h" TEST(wf_jsonrpc_is_response, valid_result) { diff --git a/lib/wf/jsonrpc/test/wf/jsonrpc/test_proxy.cc b/test/webfuse/tests/core/jsonrpc/test_proxy.cc similarity index 86% rename from lib/wf/jsonrpc/test/wf/jsonrpc/test_proxy.cc rename to test/webfuse/tests/core/jsonrpc/test_proxy.cc index 66eafa9..bdc8a15 100644 --- a/lib/wf/jsonrpc/test/wf/jsonrpc/test_proxy.cc +++ b/test/webfuse/tests/core/jsonrpc/test_proxy.cc @@ -1,13 +1,19 @@ #include -#include "wf/jsonrpc/proxy.h" -#include "wf/jsonrpc/status.h" -#include "wf/timer/manager.h" +#include "webfuse/core/jsonrpc/proxy.h" +#include "webfuse/core/status.h" +#include "webfuse/core/timer/manager.h" + +#include "webfuse/tests/core/jsonrpc/mock_timer.hpp" #include #include using namespace std::chrono_literals; - +using wf_jsonrpc_test::MockTimer; +using testing::Return; +using testing::_; +using testing::DoAll; +using testing::SaveArg; #define WF_DEFAULT_TIMEOUT (10 * 1000) @@ -16,7 +22,7 @@ namespace int jsonrpc_get_status(json_t * error) { json_t * code = json_object_get(error, "code"); - return (json_is_integer(code)) ? json_integer_value(code) : WF_JSONRPC_BAD_FORMAT; + return (json_is_integer(code)) ? json_integer_value(code) : WF_BAD_FORMAT; } struct SendContext @@ -190,7 +196,7 @@ TEST(wf_jsonrpc_proxy, invoke_fails_if_another_request_is_pending) ASSERT_FALSE(finished_context.is_called); ASSERT_TRUE(finished_context2.is_called); - ASSERT_EQ(WF_JSONRPC_BAD_BUSY, jsonrpc_get_status(finished_context2.error)); + ASSERT_EQ(WF_BAD_BUSY, jsonrpc_get_status(finished_context2.error)); wf_jsonrpc_proxy_dispose(proxy); wf_timer_manager_dispose(timer_manager); @@ -211,7 +217,7 @@ TEST(wf_jsonrpc_proxy, invoke_fails_if_request_is_invalid) ASSERT_FALSE(send_context.is_called); ASSERT_TRUE(finished_context.is_called); - ASSERT_EQ(WF_JSONRPC_BAD, jsonrpc_get_status(finished_context.error)); + ASSERT_EQ(WF_BAD, jsonrpc_get_status(finished_context.error)); wf_jsonrpc_proxy_dispose(proxy); wf_timer_manager_dispose(timer_manager); @@ -301,7 +307,7 @@ TEST(wf_jsonrpc_proxy, timeout) wf_timer_manager_check(timer_manager); ASSERT_TRUE(finished_context.is_called); - ASSERT_EQ(WF_JSONRPC_BAD_TIMEOUT, jsonrpc_get_status(finished_context.error)); + ASSERT_EQ(WF_BAD_TIMEOUT, jsonrpc_get_status(finished_context.error)); wf_jsonrpc_proxy_dispose(proxy); wf_timer_manager_dispose(timer_manager); @@ -380,3 +386,44 @@ TEST(wf_jsonrpc_proxy, notify_dont_send_invalid_request) wf_jsonrpc_proxy_dispose(proxy); wf_timer_manager_dispose(timer_manager); } + +TEST(wf_jsonrpc_proxy, swallow_timeout_if_no_request_pending) +{ + MockTimer timer_api; + + wf_timer_on_timer_fn * on_timer = nullptr; + void * timer_context = nullptr; + EXPECT_CALL(timer_api, wf_timer_create(_, _, _)) + .Times(1) + .WillOnce(DoAll(SaveArg<1>(&on_timer), SaveArg<2>(&timer_context), Return(nullptr))); + + SendContext send_context; + void * send_data = reinterpret_cast(&send_context); + struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(nullptr, 1, &jsonrpc_send, send_data); + + on_timer(nullptr, timer_context); + ASSERT_FALSE(send_context.is_called); + + + wf_jsonrpc_proxy_dispose(proxy); +} + +TEST(wf_jsonrpc_proxy, on_result_swallow_if_no_request_pending) +{ + struct wf_timer_manager * timer_manager = wf_timer_manager_create(); + + SendContext send_context; + void * send_data = reinterpret_cast(&send_context); + struct wf_jsonrpc_proxy * proxy = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &jsonrpc_send, send_data); + + json_t * response = json_object(); + json_object_set_new(response, "result", json_string("okay")); + json_object_set_new(response, "id", json_integer(42)); + + wf_jsonrpc_proxy_onresult(proxy, response); + json_decref(response); + + wf_jsonrpc_proxy_dispose(proxy); + wf_timer_manager_dispose(timer_manager); +} + diff --git a/lib/wf/jsonrpc/test/wf/jsonrpc/test_request.cc b/test/webfuse/tests/core/jsonrpc/test_request.cc similarity index 66% rename from lib/wf/jsonrpc/test/wf/jsonrpc/test_request.cc rename to test/webfuse/tests/core/jsonrpc/test_request.cc index eb15e91..8d37a9f 100644 --- a/lib/wf/jsonrpc/test/wf/jsonrpc/test_request.cc +++ b/test/webfuse/tests/core/jsonrpc/test_request.cc @@ -1,6 +1,6 @@ #include -#include "wf/jsonrpc/request.h" -#include "wf/jsonrpc/status.h" +#include "webfuse/core/jsonrpc/request.h" +#include "webfuse/core/status.h" namespace { @@ -74,7 +74,7 @@ TEST(wf_jsonrpc_request, respond_error) struct wf_jsonrpc_request * request = wf_jsonrpc_request_create(42, &jsonrpc_send, user_data); - wf_jsonrpc_respond_error(request, WF_JSONRPC_BAD, "Bad"); + wf_jsonrpc_respond_error(request, WF_BAD, "Bad"); ASSERT_NE(nullptr, context.response); @@ -93,7 +93,7 @@ TEST(wf_jsonrpc_request, respond_error) json_t * err_code = json_object_get(err, "code"); ASSERT_TRUE(json_is_integer(err_code)); - ASSERT_EQ(WF_JSONRPC_BAD, json_integer_value(err_code)); + ASSERT_EQ(WF_BAD, json_integer_value(err_code)); json_t * err_message = json_object_get(err, "message"); ASSERT_TRUE(json_is_string(err_message)); @@ -101,3 +101,38 @@ TEST(wf_jsonrpc_request, respond_error) json_decref(response); } + +TEST(wf_jsonrpc_request, is_request_object_params) +{ + json_t * request = json_object(); + json_object_set_new(request, "method", json_string("some_method")); + json_object_set_new(request, "params", json_object()); + json_object_set_new(request, "id", json_integer(42)); + + ASSERT_TRUE(wf_jsonrpc_is_request(request)); + + json_decref(request); +} + +TEST(wf_jsonrpc_request, is_request_fail_missing_params) +{ + json_t * request = json_object(); + json_object_set_new(request, "method", json_string("some_method")); + json_object_set_new(request, "id", json_integer(42)); + + ASSERT_FALSE(wf_jsonrpc_is_request(request)); + + json_decref(request); +} + +TEST(wf_jsonrpc_request, is_request_fail_params_wrong_type) +{ + json_t * request = json_object(); + json_object_set_new(request, "method", json_string("some_method")); + json_object_set_new(request, "params", json_string("invalid_params")); + json_object_set_new(request, "id", json_integer(42)); + + ASSERT_FALSE(wf_jsonrpc_is_request(request)); + + json_decref(request); +} diff --git a/test/webfuse/tests/core/jsonrpc/test_response.cc b/test/webfuse/tests/core/jsonrpc/test_response.cc new file mode 100644 index 0000000..f6ef9e7 --- /dev/null +++ b/test/webfuse/tests/core/jsonrpc/test_response.cc @@ -0,0 +1,147 @@ +#include +#include "webfuse/core/jsonrpc/response_intern.h" +#include "webfuse/core/status.h" + +TEST(wf_json_response, init_result) +{ + json_t * message = json_object(); + json_object_set_new(message, "result", json_integer(47)); + json_object_set_new(message, "id", json_integer(11)); + + struct wf_jsonrpc_response response; + wf_jsonrpc_response_init(&response, message); + + ASSERT_EQ(nullptr, response.error); + ASSERT_TRUE(json_is_integer(response.result)); + ASSERT_EQ(47, json_integer_value(response.result)); + ASSERT_EQ(11, response.id); + + wf_jsonrpc_response_cleanup(&response); + json_decref(message); +} + +TEST(wf_json_response, init_error) +{ + json_t * message = json_object(); + json_t * err = json_object(); + json_object_set_new(err, "code", json_integer(42)); + json_object_set_new(err, "message", json_string("Don't Panic!")); + json_object_set_new(message, "error", err); + json_object_set_new(message, "id", json_integer(23)); + + struct wf_jsonrpc_response response; + wf_jsonrpc_response_init(&response, message); + + ASSERT_EQ(42, json_integer_value(json_object_get(response.error, "code"))); + ASSERT_STREQ("Don't Panic!", json_string_value(json_object_get(response.error, "message"))); + ASSERT_EQ(nullptr, response.result); + ASSERT_EQ(23, response.id); + + wf_jsonrpc_response_cleanup(&response); + json_decref(message); +} + +TEST(wf_json_response, init_fail_missing_result_and_error) +{ + json_t * message = json_object(); + json_object_set_new(message, "id", json_integer(12)); + + struct wf_jsonrpc_response response; + wf_jsonrpc_response_init(&response, message); + + ASSERT_EQ(WF_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code"))); + ASSERT_EQ(nullptr, response.result); + ASSERT_EQ(12, response.id); + + wf_jsonrpc_response_cleanup(&response); + json_decref(message); +} + +TEST(wf_json_response, init_fail_missing_id) +{ + json_t * message = json_object(); + json_object_set_new(message, "result", json_integer(47)); + + struct wf_jsonrpc_response response; + wf_jsonrpc_response_init(&response, message); + + ASSERT_EQ(WF_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code"))); + ASSERT_EQ(nullptr, response.result); + ASSERT_EQ(-1, response.id); + + wf_jsonrpc_response_cleanup(&response); + json_decref(message); +} + +TEST(wf_json_response, init_fail_wrong_id_type) +{ + json_t * message = json_object(); + json_object_set_new(message, "result", json_integer(47)); + json_object_set_new(message, "id", json_string("42")); + + struct wf_jsonrpc_response response; + wf_jsonrpc_response_init(&response, message); + + ASSERT_EQ(WF_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code"))); + ASSERT_EQ(nullptr, response.result); + ASSERT_EQ(-1, response.id); + + wf_jsonrpc_response_cleanup(&response); + json_decref(message); +} + +TEST(wf_json_response, init_fail_error_missing_code) +{ + json_t * message = json_object(); + json_t * err = json_object(); + json_object_set_new(err, "message", json_string("Don't Panic!")); + json_object_set_new(message, "error", err); + json_object_set_new(message, "id", json_integer(23)); + + struct wf_jsonrpc_response response; + wf_jsonrpc_response_init(&response, message); + + ASSERT_EQ(WF_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code"))); + ASSERT_EQ(nullptr, response.result); + ASSERT_EQ(23, response.id); + + wf_jsonrpc_response_cleanup(&response); + json_decref(message); +} + +TEST(wf_json_response, init_fail_error_wrong_code_type) +{ + json_t * message = json_object(); + json_t * err = json_object(); + json_object_set_new(err, "code", json_string("42")); + json_object_set_new(err, "message", json_string("Don't Panic!")); + json_object_set_new(message, "error", err); + json_object_set_new(message, "id", json_integer(23)); + + struct wf_jsonrpc_response response; + wf_jsonrpc_response_init(&response, message); + + ASSERT_EQ(WF_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code"))); + ASSERT_EQ(nullptr, response.result); + ASSERT_EQ(23, response.id); + + wf_jsonrpc_response_cleanup(&response); + json_decref(message); +} + +TEST(wf_json_response, init_fail_error_wrong_type) +{ + json_t * message = json_object(); + json_object_set_new(message, "error", json_string("invalid error type")); + json_object_set_new(message, "id", json_integer(23)); + + struct wf_jsonrpc_response response; + wf_jsonrpc_response_init(&response, message); + + ASSERT_EQ(WF_BAD_FORMAT, json_integer_value(json_object_get(response.error, "code"))); + ASSERT_EQ(nullptr, response.result); + ASSERT_EQ(23, response.id); + + wf_jsonrpc_response_cleanup(&response); + json_decref(message); +} diff --git a/lib/wf/jsonrpc/test/wf/jsonrpc/test_response_parser.cc b/test/webfuse/tests/core/jsonrpc/test_response_parser.cc similarity index 80% rename from lib/wf/jsonrpc/test/wf/jsonrpc/test_response_parser.cc rename to test/webfuse/tests/core/jsonrpc/test_response_parser.cc index e3301fa..f9941c1 100644 --- a/lib/wf/jsonrpc/test/wf/jsonrpc/test_response_parser.cc +++ b/test/webfuse/tests/core/jsonrpc/test_response_parser.cc @@ -1,7 +1,7 @@ #include #include -#include "wf/jsonrpc/impl/response.h" +#include "webfuse/core/jsonrpc/response_intern.h" static void response_parse_str( @@ -11,7 +11,7 @@ static void response_parse_str( json_t * message = json_loadb(buffer.c_str(), buffer.size(), 0, nullptr); if (nullptr != message) { - wf_jsonrpc_impl_response_init(response, message); + wf_jsonrpc_response_init(response, message); json_decref(message); } } @@ -25,21 +25,21 @@ TEST(response_parser, test) ASSERT_NE(nullptr, response.error); ASSERT_EQ(-1, response.id); ASSERT_EQ(nullptr, response.result); - wf_jsonrpc_impl_response_cleanup(&response); + wf_jsonrpc_response_cleanup(&response); // empty response_parse_str("{}", &response); ASSERT_NE(nullptr, response.error); ASSERT_EQ(-1, response.id); ASSERT_EQ(nullptr, response.result); - wf_jsonrpc_impl_response_cleanup(&response); + wf_jsonrpc_response_cleanup(&response); // no data response_parse_str("{\"id\":42}", &response); ASSERT_NE(nullptr, response.error); ASSERT_EQ(42, response.id); ASSERT_EQ(nullptr, response.result); - wf_jsonrpc_impl_response_cleanup(&response); + wf_jsonrpc_response_cleanup(&response); // custom error code response_parse_str("{\"error\":{\"code\": 42}, \"id\": 42}", &response); @@ -47,12 +47,12 @@ TEST(response_parser, test) ASSERT_EQ(42, json_integer_value(json_object_get(response.error, "code"))); ASSERT_EQ(42, response.id); ASSERT_EQ(nullptr, response.result); - wf_jsonrpc_impl_response_cleanup(&response); + wf_jsonrpc_response_cleanup(&response); // valid response response_parse_str("{\"result\": true, \"id\": 42}", &response); ASSERT_EQ(nullptr, response.error); ASSERT_EQ(42, response.id); ASSERT_NE(nullptr, response.result); - wf_jsonrpc_impl_response_cleanup(&response); + wf_jsonrpc_response_cleanup(&response); } diff --git a/test/webfuse/tests/core/jsonrpc/test_server.cc b/test/webfuse/tests/core/jsonrpc/test_server.cc new file mode 100644 index 0000000..688028f --- /dev/null +++ b/test/webfuse/tests/core/jsonrpc/test_server.cc @@ -0,0 +1,241 @@ +#include +#include "webfuse/core/jsonrpc/server.h" +#include "webfuse/core/jsonrpc/request.h" +#include "webfuse/core/status.h" + +namespace +{ + struct Context + { + json_t * response; + bool is_called; + }; + + bool jsonrpc_send( + json_t * request, + void * user_data) + { + Context * context = reinterpret_cast(user_data); + context->is_called = true; + context->response = request; + json_incref(request); + + return true; + } + + void sayHello( + struct wf_jsonrpc_request * request, + char const * method_name, + json_t * params, + void * user_data) + { + (void) method_name; + (void) params; + (void) user_data; + + json_t * result = json_string("Hello"); + wf_jsonrpc_respond(request, result); + } + +} + +TEST(wf_jsonrpc_server, process_request) +{ + struct wf_jsonrpc_server * server = wf_jsonrpc_server_create(); + wf_jsonrpc_server_add(server, "sayHello", &sayHello, nullptr); + + Context context{nullptr, false}; + void * user_data = reinterpret_cast(&context); + json_t * request = json_object(); + json_object_set_new(request, "method", json_string("sayHello")); + json_object_set_new(request, "params", json_array()); + json_object_set_new(request, "id", json_integer(23)); + wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data); + + ASSERT_TRUE(context.is_called); + ASSERT_NE(nullptr, context.response); + ASSERT_TRUE(json_is_object(context.response)); + + json_t * id = json_object_get(context.response, "id"); + ASSERT_TRUE(json_is_integer(id)); + ASSERT_EQ(23, json_integer_value(id)); + + json_t * result = json_object_get(context.response, "result"); + ASSERT_TRUE(json_is_string(result)); + ASSERT_STREQ("Hello", json_string_value(result)); + + json_decref(context.response); + json_decref(request); + wf_jsonrpc_server_dispose(server); +} + +TEST(wf_jsonrpc_server, process_request_with_oject_params) +{ + struct wf_jsonrpc_server * server = wf_jsonrpc_server_create(); + wf_jsonrpc_server_add(server, "sayHello", &sayHello, nullptr); + + Context context{nullptr, false}; + void * user_data = reinterpret_cast(&context); + json_t * request = json_object(); + json_object_set_new(request, "method", json_string("sayHello")); + json_object_set_new(request, "params", json_object()); + json_object_set_new(request, "id", json_integer(23)); + wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data); + + ASSERT_TRUE(context.is_called); + ASSERT_NE(nullptr, context.response); + ASSERT_TRUE(json_is_object(context.response)); + + json_t * id = json_object_get(context.response, "id"); + ASSERT_TRUE(json_is_integer(id)); + ASSERT_EQ(23, json_integer_value(id)); + + json_t * result = json_object_get(context.response, "result"); + ASSERT_TRUE(json_is_string(result)); + ASSERT_STREQ("Hello", json_string_value(result)); + + json_decref(context.response); + json_decref(request); + wf_jsonrpc_server_dispose(server); +} + +TEST(wf_jsonrpc_server, invoke_unknown_method) +{ + struct wf_jsonrpc_server * server = wf_jsonrpc_server_create(); + wf_jsonrpc_server_add(server, "sayHello", &sayHello, nullptr); + + Context context{nullptr, false}; + void * user_data = reinterpret_cast(&context); + json_t * request = json_object(); + json_object_set_new(request, "method", json_string("greet")); + json_object_set_new(request, "params", json_array()); + json_object_set_new(request, "id", json_integer(42)); + wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data); + + ASSERT_TRUE(context.is_called); + ASSERT_NE(nullptr, context.response); + ASSERT_TRUE(json_is_object(context.response)); + + json_t * id = json_object_get(context.response, "id"); + ASSERT_TRUE(json_is_integer(id)); + ASSERT_EQ(42, json_integer_value(id)); + + json_t * err = json_object_get(context.response, "error"); + ASSERT_TRUE(json_is_object(err)); + + json_t * err_code = json_object_get(err, "code"); + ASSERT_TRUE(json_is_integer(err_code)); + ASSERT_EQ(WF_BAD_NOTIMPLEMENTED, json_integer_value(err_code)); + + json_t * err_message = json_object_get(err, "message"); + ASSERT_TRUE(json_is_string(err_message)); + + json_decref(context.response); + json_decref(request); + wf_jsonrpc_server_dispose(server); +} + +TEST(wf_jsonrpc_server, skip_invalid_request_missing_id) +{ + struct wf_jsonrpc_server * server = wf_jsonrpc_server_create(); + + Context context{nullptr, false}; + void * user_data = reinterpret_cast(&context); + json_t * request = json_object(); + json_object_set_new(request, "method", json_string("sayHello")); + json_object_set_new(request, "params", json_array()); + wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data); + + ASSERT_FALSE(context.is_called); + + json_decref(request); + wf_jsonrpc_server_dispose(server); +} + +TEST(wf_jsonrpc_server, skip_invalid_request_wrong_id_type) +{ + struct wf_jsonrpc_server * server = wf_jsonrpc_server_create(); + + Context context{nullptr, false}; + void * user_data = reinterpret_cast(&context); + json_t * request = json_object(); + json_object_set_new(request, "method", json_string("sayHello")); + json_object_set_new(request, "params", json_array()); + json_object_set_new(request, "id", json_string("42")); + wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data); + + ASSERT_FALSE(context.is_called); + + json_decref(request); + wf_jsonrpc_server_dispose(server); +} + +TEST(wf_jsonrpc_server, skip_invalid_request_missing_params) +{ + struct wf_jsonrpc_server * server = wf_jsonrpc_server_create(); + + Context context{nullptr, false}; + void * user_data = reinterpret_cast(&context); + json_t * request = json_object(); + json_object_set_new(request, "method", json_string("sayHello")); + json_object_set_new(request, "id", json_integer(42)); + wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data); + + ASSERT_FALSE(context.is_called); + + json_decref(request); + wf_jsonrpc_server_dispose(server); +} + +TEST(wf_jsonrpc_server, skip_invalid_request_wrong_params_type) +{ + struct wf_jsonrpc_server * server = wf_jsonrpc_server_create(); + + Context context{nullptr, false}; + void * user_data = reinterpret_cast(&context); + json_t * request = json_object(); + json_object_set_new(request, "method", json_string("sayHello")); + json_object_set_new(request, "params", json_string("invalid")); + json_object_set_new(request, "id", json_integer(42)); + wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data); + + ASSERT_FALSE(context.is_called); + + json_decref(request); + wf_jsonrpc_server_dispose(server); +} + +TEST(wf_jsonrpc_server, skip_invalid_request_missing_method) +{ + struct wf_jsonrpc_server * server = wf_jsonrpc_server_create(); + + Context context{nullptr, false}; + void * user_data = reinterpret_cast(&context); + json_t * request = json_object(); + json_object_set_new(request, "params", json_array()); + json_object_set_new(request, "id", json_integer(42)); + wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data); + + ASSERT_FALSE(context.is_called); + + json_decref(request); + wf_jsonrpc_server_dispose(server); +} + +TEST(wf_jsonrpc_server, skip_invalid_request_wront_method_type) +{ + struct wf_jsonrpc_server * server = wf_jsonrpc_server_create(); + + Context context{nullptr, false}; + void * user_data = reinterpret_cast(&context); + json_t * request = json_object(); + json_object_set_new(request, "method", json_integer(42)); + json_object_set_new(request, "params", json_array()); + json_object_set_new(request, "id", json_integer(42)); + wf_jsonrpc_server_process(server, request, &jsonrpc_send, user_data); + + ASSERT_FALSE(context.is_called); + + json_decref(request); + wf_jsonrpc_server_dispose(server); +} \ No newline at end of file diff --git a/test/webfuse/tests/core/timer/test_timepoint.cc b/test/webfuse/tests/core/timer/test_timepoint.cc new file mode 100644 index 0000000..f55adab --- /dev/null +++ b/test/webfuse/tests/core/timer/test_timepoint.cc @@ -0,0 +1,38 @@ +#include + +#include "webfuse/core/timer/timepoint.h" + +#include +#include + +using namespace std::chrono_literals; + +TEST(wf_timer_timepoint, now) +{ + wf_timer_timepoint start = wf_timer_timepoint_now(); + std::this_thread::sleep_for(42ms); + wf_timer_timepoint end = wf_timer_timepoint_now(); + + ASSERT_LT(start, end); + ASSERT_LT(end, start + 500); +} + +TEST(wf_timer_timepoint, in_msec) +{ + wf_timer_timepoint now = wf_timer_timepoint_now(); + wf_timer_timepoint later = wf_timer_timepoint_in_msec(42); + + ASSERT_LT(now, later); + ASSERT_LT(later, now + 500); +} + +TEST(wf_timer_timepoint, elapsed) +{ + wf_timer_timepoint now; + + now = wf_timer_timepoint_now(); + ASSERT_TRUE(wf_timer_timepoint_is_elapsed(now - 1)); + + now = wf_timer_timepoint_now(); + ASSERT_FALSE(wf_timer_timepoint_is_elapsed(now + 500)); +} diff --git a/lib/wf/timer/test/wf/timer/test_timer.cc b/test/webfuse/tests/core/timer/test_timer.cc similarity index 88% rename from lib/wf/timer/test/wf/timer/test_timer.cc rename to test/webfuse/tests/core/timer/test_timer.cc index e97c011..d25b1f5 100644 --- a/lib/wf/timer/test/wf/timer/test_timer.cc +++ b/test/webfuse/tests/core/timer/test_timer.cc @@ -4,8 +4,8 @@ #include #include -#include "wf/timer/timer.h" -#include "wf/timer/manager.h" +#include "webfuse/core/timer/timer.h" +#include "webfuse/core/timer/manager.h" using std::size_t; using namespace std::chrono_literals; @@ -37,8 +37,7 @@ TEST(wf_timer, trigger) struct wf_timer_manager * manager = wf_timer_manager_create(); struct wf_timer * timer = wf_timer_create(manager, &on_timeout, reinterpret_cast(&triggered)); - wf_timer_start(timer, 250); - std::this_thread::sleep_for(500ms); + wf_timer_start(timer, -1); wf_timer_manager_check(manager); ASSERT_TRUE(triggered); @@ -135,3 +134,15 @@ TEST(wf_timer, multiple_timers) wf_timer_manager_dispose(manager); } + +TEST(wf_timer, dont_trigger_null_callback) +{ + struct wf_timer_manager * manager = wf_timer_manager_create(); + struct wf_timer * timer = wf_timer_create(manager, nullptr, nullptr); + + wf_timer_start(timer, -1); + wf_timer_manager_check(manager); + + wf_timer_dispose(timer); + wf_timer_manager_dispose(manager); +} diff --git a/test/webfuse/tests/integration/file.cc b/test/webfuse/tests/integration/file.cc new file mode 100644 index 0000000..5719382 --- /dev/null +++ b/test/webfuse/tests/integration/file.cc @@ -0,0 +1,85 @@ +#include "webfuse/tests/integration/file.hpp" +#include +#include + +namespace +{ + +bool invoke(std::string const & command) +{ + int exit_code = -1; + + FILE * file = ::popen(command.c_str(), "r"); + if (nullptr != file) + { + exit_code = ::pclose(file); + } + + return (0 == exit_code); +} + +} + +namespace webfuse_test +{ + +File::File(std::string const& path) +: path_(path) +{ + +} + +File::~File() +{ + +} + +bool File::isFile() +{ + std::stringstream command; + command << "./fs_check -c is_file -f " << path_; + + return invoke(command.str()); +} + +bool File::isDirectory() +{ + std::stringstream command; + command << "./fs_check -c is_dir -f " << path_; + + return invoke(command.str()); +} + +bool File::hasAccessRights(int accessRights) +{ + std::stringstream command; + command << "./fs_check -c has_mode -f " << path_ << " -a " << accessRights; + + return invoke(command.str()); +} + +bool File::hasSize(size_t size) +{ + std::stringstream command; + command << "./fs_check -c has_size -f " << path_ << " -a " << size; + + return invoke(command.str()); +} + +bool File::hasSubdirectory(std::string const & subdir) +{ + std::stringstream command; + command << "./fs_check -c has_subdir -f " << path_ << " -a " << subdir; + + return invoke(command.str()); +} + +bool File::hasContents(std::string const & contents) +{ + std::stringstream command; + command << "./fs_check -c has_contents -f " << path_ << " -a " << contents; + + return invoke(command.str()); +} + +} \ No newline at end of file diff --git a/test/webfuse/tests/integration/file.hpp b/test/webfuse/tests/integration/file.hpp new file mode 100644 index 0000000..e36e6ce --- /dev/null +++ b/test/webfuse/tests/integration/file.hpp @@ -0,0 +1,26 @@ +#ifndef WF_TEST_INTEGRATION_FILE_HPP +#define WF_TEST_INTEGRATION_FILE_HPP + +#include + +namespace webfuse_test +{ + +class File final +{ +public: + explicit File(std::string const& path); + ~File(); + bool isFile(); + bool isDirectory(); + bool hasAccessRights(int accessRights); + bool hasSize(size_t size); + bool hasSubdirectory(std::string const & subdir); + bool hasContents(std::string const & contents); +private: + std::string path_; +}; + +} + +#endif diff --git a/test/webfuse/tests/integration/fs_check.c b/test/webfuse/tests/integration/fs_check.c new file mode 100644 index 0000000..55d24c4 --- /dev/null +++ b/test/webfuse/tests/integration/fs_check.c @@ -0,0 +1,288 @@ +/* Why this tool is used: + * + * In order to test webfuse as a fuse filesystem, file system operations should be made. + * To check for memory leaks, valgind (memcheck) is used. + * + * Early tests discovered some ugly behavior of valgrind: + * - valgrind intercepts syscalls like open, read and write + * - valgrind does not expect that syscalls are handled within the process to be checked + * + * There is a more or less (un-) documented switch, which changes valgrind's bevahior, but + * this caused other problems. + * + * The second approach used GTests's death tests. Death tests were used quite a while, + * until we discovered a configuration bug when running CTest: + * - memory leaks did not lead to test error + * + * After fixing CTest configuration, memory leaks within the death tests were shown. + * Which is correct, since death tests pematurely exits the program an therefore no + * cleanup is done. + * + * Finally, it was decided to use good old popen together with this tool. + */ + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +struct command; + +typedef bool +command_invoke_fn( + struct command * command); + +struct command +{ + command_invoke_fn * invoke; + char * file; + char * arg; + bool success; + bool has_arg; +}; + +static bool print_usage( + struct command * command) +{ + printf( + "fs_check, (c) 2020 by Webfuse authors (https://github.com/falk-werner/webfuse)\n" + "Checks file information\n" + "\n" + "Usage:\n" + "\t./fs_check --command --file [--arg ]\n" + "\n" + "Options:\n" + "\t-c, --command - Check to perform (see below)\n" + "\t-f, --file - Path to file to check\n" + "\t-a, --arg - Argument, depending on command\n" + "\n" + "Commands:\n" + "\tis_file - checks, if is a regular file; no arg\n" + "\tis_dir - checks, if is a directory; no arg\n" + "\thas_mode - checks, if has the mode given in \n" + "\thas_size - checks, if has the size given in \n" + "\thas_subdir - checks, if contains the sub directory given in \n" + "\thas_contents - checks, if has the contents given in \n" + ); + + return command->success; +} + +static bool is_file( + struct command * command) +{ + struct stat buffer; + int rc = stat(command->file, &buffer); + + return ((0 == rc) && (S_ISREG(buffer.st_mode))); +} + +static bool is_dir( + struct command * command) +{ + struct stat buffer; + int rc = stat(command->file, &buffer); + + return ((0 == rc) && (S_ISDIR(buffer.st_mode))); +} + +static bool has_mode( + struct command * command) +{ + mode_t mode = (mode_t) atoi(command->arg); + struct stat buffer; + int rc = stat(command->file, &buffer); + + return ((0 == rc) && (mode == (buffer.st_mode & 0777))); +} + +static bool has_size( + struct command * command) +{ + int size = atoi(command->arg); + struct stat buffer; + int rc = stat(command->file, &buffer); + + return ((0 == rc) && (size == (buffer.st_size))); +} + +static bool has_subdir( + struct command * command) +{ + bool result = false; + char const * subdir = command->arg; + DIR * dir = opendir(command->file); + if (NULL != dir) + { + struct dirent * entry = readdir(dir); + while (NULL != entry) + { + if (0 == strcmp(subdir, entry->d_name)) + { + result = true; + break; + } + + entry = readdir(dir); + } + + closedir(dir); + } + + return result; +} + +static bool has_contents( + struct command * command) +{ + bool result = false; + char const * contents = command->arg; + size_t length = strlen(contents); + + char * buffer = malloc(length); + FILE * file = fopen(command->file, "rb"); + { + ssize_t count = fread(buffer, 1, length, file); + fclose(file); + + result = (count == (ssize_t) length) && (0 == strncmp(buffer, contents, length)); + } + + free(buffer); + return result; +} + +static bool get_command( + struct command * command, + char const * name) +{ + static struct { + char const * name; + command_invoke_fn * invoke; + bool has_arg; + } commands[] = + { + {"is_file" , &is_file , false}, + {"is_dir" , &is_dir , false}, + {"has_mode" , &has_mode , true}, + {"has_size" , &has_size , true}, + {"has_subdir" , &has_subdir , true}, + {"has_contents", &has_contents, true}, + {NULL, NULL, false} + }; + + for (int i = 0; NULL != commands[i].name; i++) + { + if (0 == strcmp(commands[i].name, name)) + { + command->invoke = commands[i].invoke; + command->has_arg = commands[i].has_arg; + return true; + } + } + + return false; +} + +static void command_init( + struct command * command, + int argc, + char * argv[]) +{ + static struct option const options[] = + { + {"file" , required_argument, NULL, 'f'}, + {"command", required_argument, NULL, 'c'}, + {"arg" , required_argument, NULL, 'a'}, + {"help" , no_argument , NULL, 'h'}, + {NULL, 0, NULL, 0} + }; + + command->invoke = &print_usage; + command->file = NULL; + command->arg = NULL; + command->success = true; + command->has_arg = false; + + optind = 0; + bool is_finished = false; + while (!is_finished) + { + int option_index = 0; + int const c = getopt_long(argc, argv, "f:c:a:h", options, &option_index); + + switch(c) + { + case -1: + is_finished = true; + break; + case 'c': + if (!get_command(command, optarg)) + { + fprintf(stderr, "error: unknown command\n"); + command->invoke = &print_usage; + command->success = false; + is_finished = true; + } + break; + case 'f': + free(command->file); + command->file = strdup(optarg); + break; + case 'a': + free(command->arg); + command->arg = strdup(optarg); + break; + case 'h': + command->invoke = &print_usage; + break; + default: + fprintf(stderr, "error: unknown argument\n"); + command->invoke = &print_usage; + command->success = false; + is_finished = true; + break; + } + } + + if (command->success) + { + if (NULL == command->file) + { + fprintf(stderr, "error: missing required arg: -f\n"); + command->invoke = &print_usage; + command->success = false; + } + else if ((command->has_arg) && (NULL == command->arg)) + { + fprintf(stderr, "error: missing required arg: -a\n"); + command->invoke = &print_usage; + command->success = false; + } + } +} + +static void command_cleanup( + struct command * command) +{ + free(command->file); + free(command->arg); +} + +int main(int argc, char* argv[]) +{ + struct command command; + command_init(&command, argc, argv); + + bool success = command.invoke(&command); + + command_cleanup(&command); + return success ? EXIT_SUCCESS : EXIT_FAILURE; +} \ No newline at end of file diff --git a/test/webfuse/tests/integration/provider.cc b/test/webfuse/tests/integration/provider.cc index b1fa44d..e635c9b 100644 --- a/test/webfuse/tests/integration/provider.cc +++ b/test/webfuse/tests/integration/provider.cc @@ -19,6 +19,9 @@ public: : is_shutdown_requested(false) { config = wfp_client_config_create(); + wfp_client_config_set_certpath(config, "client-cert.pem"); + wfp_client_config_set_keypath(config, "client-key.pem"); + wfp_client_config_set_ca_filepath(config, "server-cert.pem"); fs = wfp_static_filesystem_create(config); wfp_static_filesystem_add_text(fs, "hello.txt", 0444, "Hello, World"); diff --git a/test/webfuse/tests/integration/server.cc b/test/webfuse/tests/integration/server.cc index 931f932..8f15161 100644 --- a/test/webfuse/tests/integration/server.cc +++ b/test/webfuse/tests/integration/server.cc @@ -63,6 +63,8 @@ public: wf_server_config_set_mountpoint_factory(config, &webfuse_test_server_create_mountpoint, reinterpret_cast(base_dir)); + wf_server_config_set_keypath(config, "server-key.pem"); + wf_server_config_set_certpath(config, "server-cert.pem"); server = wf_server_create(config); diff --git a/test/webfuse/tests/integration/test_integration.cc b/test/webfuse/tests/integration/test_integration.cc index 4cc2844..e7e8215 100644 --- a/test/webfuse/tests/integration/test_integration.cc +++ b/test/webfuse/tests/integration/test_integration.cc @@ -1,6 +1,7 @@ #include #include "webfuse/tests/integration/server.hpp" #include "webfuse/tests/integration/provider.hpp" +#include "webfuse/tests/integration/file.hpp" #include #include @@ -15,11 +16,10 @@ #include #include "webfuse/core/lws_log.h" -#include "webfuse/utils/die_if.hpp" using webfuse_test::Server; using webfuse_test::Provider; -using webfuse_test::die_if; +using webfuse_test::File; namespace { @@ -38,7 +38,7 @@ namespace void SetUp() { server = new Server(); - provider = new Provider("ws://localhost:8080/"); + provider = new Provider("wss://localhost:8080/"); } void TearDown() @@ -70,87 +70,28 @@ TEST_F(IntegrationTest, ProvidesTextFile) { std::string file_name = std::string(GetBaseDir()) + "/cprovider/hello.txt"; - ASSERT_EXIT({ - struct stat buffer; - int rc = stat(file_name.c_str(), &buffer); - - die_if(0 != rc); - die_if(!S_ISREG(buffer.st_mode)); - die_if(0444 != (buffer.st_mode & 0777)); - die_if(12 != buffer.st_size); - - exit(0); - }, ::testing::ExitedWithCode(0), ".*"); + File file(file_name); + ASSERT_TRUE(file.isFile()); + ASSERT_TRUE(file.hasAccessRights(0444)); + ASSERT_TRUE(file.hasSize(12)); } TEST_F(IntegrationTest, ReadTextFile) { std::string file_name = std::string(GetBaseDir()) + "/cprovider/hello.txt"; - ASSERT_EXIT({ - FILE * file = fopen(file_name.c_str(), "rb"); - die_if(nullptr == file); - - char buffer[13]; - ssize_t count = fread(buffer, 1, 12, file); - int rc = fclose(file); - - die_if(12 != count); - die_if(0 != strncmp("Hello, World", buffer, 12)); - die_if(0 != rc); - - exit(0); - }, ::testing::ExitedWithCode(0), ".*"); - + File file(file_name); + ASSERT_TRUE(file.hasContents("Hello, World")); } TEST_F(IntegrationTest, ReadDir) { std::string dir_name = std::string(GetBaseDir()) + "/cprovider"; - ASSERT_EXIT({ - - DIR * dir = opendir(dir_name.c_str()); - die_if(nullptr == dir); - - bool found_self = false; - bool found_parent = false; - bool found_hello_txt = false; - bool found_other = false; - - dirent * entry = readdir(dir); - while (NULL != entry) - { - if (0 == strcmp(".", entry->d_name)) - { - found_self = true; - } - else if (0 == strcmp("..", entry->d_name)) - { - found_parent = true; - } - else if (0 == strcmp("hello.txt", entry->d_name)) - { - found_hello_txt = true; - } - else - { - found_other = true; - } - - - entry = readdir(dir); - } - - closedir(dir); - - die_if(!found_self); - die_if(!found_parent); - die_if(!found_hello_txt); - - die_if(found_other); - - exit(0); - }, ::testing::ExitedWithCode(0), ".*"); - + File dir(dir_name); + ASSERT_TRUE(dir.isDirectory()); + ASSERT_TRUE(dir.hasSubdirectory(".")); + ASSERT_TRUE(dir.hasSubdirectory("..")); + ASSERT_TRUE(dir.hasSubdirectory("hello.txt")); + ASSERT_FALSE(dir.hasSubdirectory("other")); } \ No newline at end of file diff --git a/test/webfuse/tests/integration/test_lowlevel.cc b/test/webfuse/tests/integration/test_lowlevel.cc new file mode 100644 index 0000000..050b23a --- /dev/null +++ b/test/webfuse/tests/integration/test_lowlevel.cc @@ -0,0 +1,112 @@ +#include "webfuse_adapter.h" +#include "webfuse_provider.h" +#include + +#include "webfuse/utils/tempdir.hpp" + +#include + +using ::webfuse_test::TempDir; + +extern "C" +{ + +wf_mountpoint * +wf_test_integration_lowlevel_create_mountpoint( + char const *, void * user_data) +{ + auto * tempDir = reinterpret_cast(user_data); + return wf_mountpoint_create(tempDir->path()); +} + +void +wf_test_integration_lowlevel_on_connected( + void * user_data) +{ + int * state = reinterpret_cast(user_data); + *state = 1; +} + +void +wf_test_integration_lowlevel_on_disconnected( + void * user_data) +{ + int * state = reinterpret_cast(user_data); + *state = -1; +} + +bool +wf_test_integration_lowlevel_authenticate( + struct wf_credentials * credentials, + void * ) +{ + char const * username = wf_credentials_get(credentials, "username"); + char const * password = wf_credentials_get(credentials, "password"); + + return ((0 == strcmp(username, "bob")) && (0 == strcmp(password, "secret"))); +} + +void +wf_test_integration_lowlevel_get_credentials( + struct wfp_credentials * credentials, + void * ) +{ + wfp_credentials_set_type(credentials, "username"); + wfp_credentials_add(credentials, "username", "bob"); + wfp_credentials_add(credentials, "password", "secret"); +} + +} + +TEST(integration, lowlevel) +{ + TempDir dir("wf_test"); + + wf_server_protocol * server_protocol = wf_server_protocol_create( + &wf_test_integration_lowlevel_create_mountpoint, + reinterpret_cast(&dir)); + ASSERT_NE(nullptr, server_protocol); + wf_server_protocol_add_authenticator(server_protocol, "username", + &wf_test_integration_lowlevel_authenticate, nullptr); + + int state = 0; + wfp_client_config * client_config = wfp_client_config_create(); + ASSERT_NE(nullptr, client_config); + wfp_client_config_set_userdata(client_config, reinterpret_cast(&state)); + wfp_client_config_set_onconnected(client_config, &wf_test_integration_lowlevel_on_connected); + wfp_client_config_set_ondisconnected(client_config, &wf_test_integration_lowlevel_on_disconnected); + wfp_client_config_enable_authentication(client_config, &wf_test_integration_lowlevel_get_credentials); + + wfp_client_protocol * client_protocol = wfp_client_protocol_create(client_config); + ASSERT_NE(nullptr, client_protocol); + + lws_protocols protocols[3]; + memset(protocols, 0, 3 * sizeof(lws_protocols)); + wf_server_protocol_init_lws(server_protocol, &protocols[0]); + wfp_client_protocol_init_lws(client_protocol, &protocols[1]); + + lws_context_creation_info info; + memset(&info, 0, sizeof(info)); + info.port = 8080; + info.protocols = protocols; + info.vhost_name = "localhost"; + info.ws_ping_pong_interval = 10; + info.options = LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE; + + struct lws_context * context = lws_create_context(&info); + ASSERT_NE(nullptr, context); + + wfp_client_protocol_connect(client_protocol, context, "ws://localhost:8080/"); + while (0 == state) + { + lws_service(context, 0); + } + + EXPECT_EQ(1, state); + + lws_context_destroy(context); + + wfp_client_protocol_dispose(client_protocol); + wfp_client_config_dispose(client_config); + wf_server_protocol_dispose(server_protocol); +} \ No newline at end of file diff --git a/test/webfuse/tests/provider/operation/test_close.cc b/test/webfuse/tests/provider/operation/test_close.cc new file mode 100644 index 0000000..3769579 --- /dev/null +++ b/test/webfuse/tests/provider/operation/test_close.cc @@ -0,0 +1,99 @@ +#include "webfuse/provider/impl/operation/close.h" +#include "webfuse/mocks/mock_provider.hpp" +#include "webfuse/mocks/fake_invokation_context.hpp" + +#include + +using ::webfuse_test::MockProvider; +using ::webfuse_test::create_context; +using ::testing::_; + +TEST(wfp_close, close) +{ + int inode = 42; + int handle = 0xdeadbeef; + int flags = 23; + + MockProvider provider; + EXPECT_CALL(provider, close(inode, handle, flags)).Times(1); + + wfp_impl_invokation_context context = create_context(provider); + + 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(handle)); + json_array_append_new(params, json_integer(flags)); + + wfp_impl_close(&context, params, 42); + json_decref(params); +} + +TEST(wfp_close, close_fail_invalid_param_count) +{ + MockProvider provider; + EXPECT_CALL(provider, close(_,_,_)).Times(0); + + wfp_impl_invokation_context context = create_context(provider); + + json_t * params = json_array(); + wfp_impl_close(&context, params, 42); + json_decref(params); +} + +TEST(wfp_close, close_fail_inode_invalid_type) +{ + MockProvider provider; + EXPECT_CALL(provider, close(_,_,_)).Times(0); + + wfp_impl_invokation_context context = create_context(provider); + + json_t * params = json_array(); + json_array_append_new(params, json_string("test.filesystem")); + json_array_append_new(params, json_string("42")); + json_array_append_new(params, json_integer(0)); + json_array_append_new(params, json_integer(0)); + + wfp_impl_close(&context, params, 42); + json_decref(params); +} + +TEST(wfp_close, close_fail_handle_invalid_type) +{ + MockProvider provider; + EXPECT_CALL(provider, close(_,_,_)).Times(0); + + wfp_impl_invokation_context context = create_context(provider); + + json_t * params = json_array(); + json_array_append_new(params, json_string("test.filesystem")); + json_array_append_new(params, json_integer(0)); + json_array_append_new(params, json_string("42")); + json_array_append_new(params, json_integer(0)); + + wfp_impl_close(&context, params, 42); + json_decref(params); +} + +TEST(wfp_close, close_fail_flags_invalid_type) +{ + MockProvider provider; + EXPECT_CALL(provider, close(_,_,_)).Times(0); + + wfp_impl_invokation_context context = create_context(provider); + + json_t * params = json_array(); + json_array_append_new(params, json_string("test.filesystem")); + json_array_append_new(params, json_integer(0)); + json_array_append_new(params, json_integer(0)); + json_array_append_new(params, json_string("42")); + + wfp_impl_close(&context, params, 42); + json_decref(params); +} + + +TEST(wfp_close, default_nop) +{ + wfp_impl_close_default(0, 0, 0, nullptr); +} \ No newline at end of file diff --git a/test/webfuse/tests/provider/operation/test_getattr.cc b/test/webfuse/tests/provider/operation/test_getattr.cc new file mode 100644 index 0000000..332ab89 --- /dev/null +++ b/test/webfuse/tests/provider/operation/test_getattr.cc @@ -0,0 +1,107 @@ +#include "webfuse/provider/impl/operation/getattr.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::StatMatcher; +using ::webfuse_test::create_context; +using ::testing::_; +using ::testing::Invoke; + +namespace +{ + +void free_request(wfp_request * request, ino_t) +{ + free(request); +} + +} + +TEST(wfp_impl_getattr, default_responds_error) +{ + MockRequest request; + auto * req = request.create_request(42); + EXPECT_CALL(request, respond_error(_,42)).Times(1); + + wfp_impl_getattr_default(req, 0, nullptr); +} + +TEST(wfp_impl_getattr, respond_file) +{ + MockRequest request; + auto * req = request.create_request(42); + EXPECT_CALL(request, respond(StatMatcher(23, 0754, "file"), 42)).Times(1); + + struct stat buffer; + memset(&buffer, 0, sizeof(buffer)); + buffer.st_ino = 23; + buffer.st_mode = S_IFREG | 0754; + wfp_impl_respond_getattr(req, &buffer); +} + +TEST(wfp_impl_getattr, respond_dir) +{ + MockRequest request; + auto * req = request.create_request(42); + EXPECT_CALL(request, respond(StatMatcher(23, 0754, "dir"), 42)).Times(1); + + struct stat buffer; + memset(&buffer, 0, sizeof(buffer)); + buffer.st_ino = 23; + buffer.st_mode = S_IFDIR | 0754; + wfp_impl_respond_getattr(req, &buffer); +} + +TEST(wfp_impl_getattr, invoke_provider) +{ + ino_t inode = 23; + MockProvider provider; + EXPECT_CALL(provider,getattr(_, inode)).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)); + + wfp_impl_getattr(&context, params, 42); + json_decref(params); +} + +TEST(wfp_impl_getattr, fail_invalid_param_count) +{ + MockProvider provider; + EXPECT_CALL(provider,getattr(_, _)).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")); + + wfp_impl_getattr(&context, params, 42); + json_decref(params); +} + +TEST(wfp_impl_getattr, fail_invalid_inode_type) +{ + MockProvider provider; + EXPECT_CALL(provider,getattr(_, _)).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("42")); + + wfp_impl_getattr(&context, params, 42); + json_decref(params); +} diff --git a/test/webfuse/tests/provider/operation/test_lookup.cc b/test/webfuse/tests/provider/operation/test_lookup.cc new file mode 100644 index 0000000..4cbccd4 --- /dev/null +++ b/test/webfuse/tests/provider/operation/test_lookup.cc @@ -0,0 +1,129 @@ +#include "webfuse/provider/impl/operation/lookup.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::StatMatcher; +using ::webfuse_test::create_context; +using ::testing::_; +using ::testing::Invoke; +using ::testing::StrEq; + +namespace +{ + +void free_request(wfp_request * request, ino_t, char const *) +{ + free(request); +} + +} + +TEST(wfp_impl_lookup, invoke_provider) +{ + ino_t inode = 42; + MockProvider provider; + EXPECT_CALL(provider,lookup(_, inode,StrEq("some.file"))).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_string("some.file")); + + wfp_impl_lookup(&context, params, 42); + json_decref(params); +} + +TEST(wfp_impl_lookup, fail_invalid_param_count) +{ + MockProvider provider; + EXPECT_CALL(provider,lookup(_, _,_)).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_lookup(&context, params, 42); + json_decref(params); +} + +TEST(wfp_impl_lookup, fail_invalid_inode_type) +{ + MockProvider provider; + EXPECT_CALL(provider,lookup(_, _,_)).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("23")); + json_array_append_new(params, json_string("some.file")); + + wfp_impl_lookup(&context, params, 42); + json_decref(params); +} + +TEST(wfp_impl_lookup, fail_invalid_name_type) +{ + MockProvider provider; + EXPECT_CALL(provider,lookup(_, _,_)).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_integer(1)); + + wfp_impl_lookup(&context, params, 42); + json_decref(params); +} + +TEST(wfp_impl_lookup, default_responds_error) +{ + MockRequest request; + auto * req = request.create_request(42); + EXPECT_CALL(request, respond_error(_,42)).Times(1); + + wfp_impl_lookup_default(req, 1, "some.file", nullptr); +} + +TEST(wfp_impl_lookup, respond_file) +{ + MockRequest request; + auto * req = request.create_request(42); + EXPECT_CALL(request, respond(StatMatcher(23, 0754, "file"), 42)).Times(1); + + struct stat buffer; + memset(&buffer, 0, sizeof(buffer)); + buffer.st_ino = 23; + buffer.st_mode = S_IFREG | 0754; + wfp_impl_respond_lookup(req, &buffer); +} + +TEST(wfp_impl_lookup, respond_dir) +{ + MockRequest request; + auto * req = request.create_request(42); + EXPECT_CALL(request, respond(StatMatcher(23, 0754, "dir"), 42)).Times(1); + + struct stat buffer; + memset(&buffer, 0, sizeof(buffer)); + buffer.st_ino = 23; + buffer.st_mode = S_IFDIR | 0754; + wfp_impl_respond_lookup(req, &buffer); +} 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..4c52b02 --- /dev/null +++ b/test/webfuse/tests/provider/operation/test_open.cc @@ -0,0 +1,114 @@ +#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); + json_decref(params); +} + +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); + json_decref(params); +} + +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); + json_decref(params); +} + +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); + json_decref(params); +} + +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); +} + diff --git a/test/webfuse/tests/provider/operation/test_read.cc b/test/webfuse/tests/provider/operation/test_read.cc new file mode 100644 index 0000000..36bf14f --- /dev/null +++ b/test/webfuse/tests/provider/operation/test_read.cc @@ -0,0 +1,173 @@ +#include "webfuse/provider/impl/operation/read.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::ReadResultMatcher; +using ::webfuse_test::create_context; +using ::testing::_; +using ::testing::Invoke; +using ::testing::StrEq; + +namespace +{ + +void free_request(wfp_request * request, ino_t, uint32_t, size_t ,size_t) +{ + free(request); +} + +} + +TEST(wfp_impl_read, invoke_provider) +{ + ino_t inode = 42; + uint32_t handle = 5; + size_t offset = 2; + size_t length = 1; + MockProvider provider; + EXPECT_CALL(provider, read(_, inode, handle, offset, length)).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(handle)); + json_array_append_new(params, json_integer(offset)); + json_array_append_new(params, json_integer(length)); + + wfp_impl_read(&context, params, 42); + json_decref(params); +} + +TEST(wfp_impl_read, fail_invalid_param_count) +{ + MockProvider provider; + EXPECT_CALL(provider, read(_, _, _, _, _)).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(1)); + json_array_append_new(params, json_integer(2)); + json_array_append_new(params, json_integer(3)); + json_array_append_new(params, json_integer(4)); + json_array_append_new(params, json_integer(5)); + + wfp_impl_read(&context, params, 42); + json_decref(params); +} + +TEST(wfp_impl_read, fail_invalid_inode_type) +{ + MockProvider provider; + EXPECT_CALL(provider, read(_, _, _, _, _)).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("42")); + json_array_append_new(params, json_integer(2)); + json_array_append_new(params, json_integer(3)); + json_array_append_new(params, json_integer(4)); + + wfp_impl_read(&context, params, 42); + json_decref(params); +} + +TEST(wfp_impl_read, fail_invalid_handle_type) +{ + MockProvider provider; + EXPECT_CALL(provider, read(_, _, _, _, _)).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(1)); + json_array_append_new(params, json_string("42")); + json_array_append_new(params, json_integer(3)); + json_array_append_new(params, json_integer(4)); + + wfp_impl_read(&context, params, 42); + json_decref(params); +} + +TEST(wfp_impl_read, fail_invalid_offset_type) +{ + MockProvider provider; + EXPECT_CALL(provider, read(_, _, _, _, _)).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(1)); + json_array_append_new(params, json_integer(2)); + json_array_append_new(params, json_string("42")); + json_array_append_new(params, json_integer(4)); + + wfp_impl_read(&context, params, 42); + json_decref(params); +} + +TEST(wfp_impl_read, fail_invalid_length_type) +{ + MockProvider provider; + EXPECT_CALL(provider, read(_, _, _, _, _)).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(1)); + json_array_append_new(params, json_integer(2)); + json_array_append_new(params, json_integer(3)); + json_array_append_new(params, json_string("42")); + + wfp_impl_read(&context, params, 42); + json_decref(params); +} + +TEST(wfp_impl_read, default_responds_error) +{ + MockRequest request; + auto * req = request.create_request(42); + EXPECT_CALL(request, respond_error(_,42)).Times(1); + + wfp_impl_read_default(req, 0, 0, 1, 2, nullptr); +} + +TEST(wfp_impl_read, respond) +{ + MockRequest request; + auto * req = request.create_request(42); + EXPECT_CALL(request, respond(ReadResultMatcher("d2Y=", "base64", 2), 42)).Times(1); + + char const data[] = "wf"; + wfp_impl_respond_read(req, data, 2); +} + +TEST(wfp_impl_read, respond_empty) +{ + MockRequest request; + auto * req = request.create_request(42); + EXPECT_CALL(request, respond(ReadResultMatcher("", "identity", 0), 42)).Times(1); + + wfp_impl_respond_read(req, nullptr, 0); +} diff --git a/test/webfuse/tests/provider/operation/test_readdir.cc b/test/webfuse/tests/provider/operation/test_readdir.cc new file mode 100644 index 0000000..33557f5 --- /dev/null +++ b/test/webfuse/tests/provider/operation/test_readdir.cc @@ -0,0 +1,100 @@ +#include "webfuse/provider/impl/operation/readdir.h" +#include "webfuse/mocks/mock_request.hpp" +#include "webfuse/mocks/mock_provider.hpp" +#include "webfuse/mocks/fake_invokation_context.hpp" +#include "webfuse/provider/dirbuffer.h" + +#include +#include + +using ::webfuse_test::MockProvider; +using ::webfuse_test::MockRequest; +using ::webfuse_test::ReaddirMatcher; +using ::webfuse_test::create_context; +using ::testing::_; +using ::testing::Invoke; + +namespace +{ + +void free_request(wfp_request * request, ino_t) +{ + free(request); +} + +} + +TEST(wfp_impl_readdir, invoke_provider) +{ + ino_t inode = 23; + MockProvider provider; + EXPECT_CALL(provider,readdir(_, inode)).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)); + + wfp_impl_readdir(&context, params, 42); + json_decref(params); +} + +TEST(wfp_impl_readdir, fail_invalid_param_count) +{ + MockProvider provider; + EXPECT_CALL(provider,readdir(_, _)).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(1)); + json_array_append_new(params, json_integer(1)); + + wfp_impl_readdir(&context, params, 42); + json_decref(params); +} + +TEST(wfp_impl_readdir, fail_invalid_inode_type) +{ + MockProvider provider; + EXPECT_CALL(provider,readdir(_, _)).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("1")); + + wfp_impl_readdir(&context, params, 42); + json_decref(params); +} + +TEST(wfp_impl_readdir, default_responds_error) +{ + MockRequest request; + auto * req = request.create_request(42); + EXPECT_CALL(request, respond_error(_,42)).Times(1); + + wfp_impl_readdir_default(req, 0, nullptr); +} + +TEST(wfp_impl_readdir, respond) +{ + char const item0[] = "some.file"; + char const * items[] = { item0, nullptr }; + + MockRequest request; + auto * req = request.create_request(42); + EXPECT_CALL(request, respond(ReaddirMatcher(items), 42)).Times(1); + + wfp_dirbuffer * buffer = wfp_dirbuffer_create(); + wfp_dirbuffer_add(buffer, item0, 42); + wfp_impl_respond_readdir(req, buffer); + wfp_dirbuffer_dispose(buffer); + +} diff --git a/test/webfuse/utils/die_if.cc b/test/webfuse/utils/die_if.cc deleted file mode 100644 index bd8dd70..0000000 --- a/test/webfuse/utils/die_if.cc +++ /dev/null @@ -1,15 +0,0 @@ -#include "webfuse/utils/die_if.hpp" -#include - -namespace webfuse_test -{ - -void die_if(bool expression) -{ - if (expression) - { - exit(EXIT_FAILURE); - } -} - -} \ No newline at end of file diff --git a/test/webfuse/utils/die_if.hpp b/test/webfuse/utils/die_if.hpp deleted file mode 100644 index 95d3ff2..0000000 --- a/test/webfuse/utils/die_if.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef WF_TEST_DIE_IF_HPP -#define WF_TEST_DIE_IF_HPP - -namespace webfuse_test -{ - -extern void die_if(bool expression); - -} - -#endif diff --git a/test/webfuse/utils/path.c b/test/webfuse/utils/path.c index 3cb222e..e63aafb 100644 --- a/test/webfuse/utils/path.c +++ b/test/webfuse/utils/path.c @@ -45,24 +45,21 @@ wf_path_create( char const * value) { struct wf_path * path = malloc(sizeof(struct wf_path)); - if (NULL != path) - { - path->elements = malloc(sizeof(char*) * WF_PATH_DEFAULT_CAPACITY); - path->capacity = WF_PATH_DEFAULT_CAPACITY; - path->count = 0; - - char const * remainder = value; - char const * pos = strchr(remainder, '/'); - while (NULL != pos) - { - wf_path_add(path, remainder, (pos - remainder)); - remainder = pos + 1; - pos = strchr(remainder, '/'); - } + path->elements = malloc(sizeof(char*) * WF_PATH_DEFAULT_CAPACITY); + path->capacity = WF_PATH_DEFAULT_CAPACITY; + path->count = 0; - wf_path_add(path, remainder, strlen(remainder)); + char const * remainder = value; + char const * pos = strchr(remainder, '/'); + while (NULL != pos) + { + wf_path_add(path, remainder, (pos - remainder)); + remainder = pos + 1; + pos = strchr(remainder, '/'); } + wf_path_add(path, remainder, strlen(remainder)); + return path; } diff --git a/test/webfuse/utils/static_filesystem.c b/test/webfuse/utils/static_filesystem.c index 62b507a..855564b 100644 --- a/test/webfuse/utils/static_filesystem.c +++ b/test/webfuse/utils/static_filesystem.c @@ -332,21 +332,18 @@ wfp_static_filesystem_create( (void) config; struct wfp_static_filesystem * filesystem = malloc(sizeof(struct wfp_static_filesystem)); - if (NULL != filesystem) - { - filesystem->entries = malloc(sizeof(struct wfp_static_filesystem_entry) * WFP_STATIC_FILESYSTEM_DEFAULT_CAPACITY); - filesystem->size = 0; - filesystem->capacity = WFP_STATIC_FILESYSTEM_DEFAULT_CAPACITY; - - wfp_static_filesystem_add_dir(filesystem, 0, ""); - - wfp_client_config_set_userdata(config, filesystem); - wfp_client_config_set_onlookup(config, &wfp_static_filesystem_lookup); - wfp_client_config_set_ongetattr(config, &wfp_static_filesystem_getattr); - wfp_client_config_set_onreaddir(config, &wfp_static_filesystem_readdir); - wfp_client_config_set_onopen(config, &wfp_static_filesystem_open); - wfp_client_config_set_onread(config, &wfp_static_filesystem_read); - } + filesystem->entries = malloc(sizeof(struct wfp_static_filesystem_entry) * WFP_STATIC_FILESYSTEM_DEFAULT_CAPACITY); + filesystem->size = 0; + filesystem->capacity = WFP_STATIC_FILESYSTEM_DEFAULT_CAPACITY; + + wfp_static_filesystem_add_dir(filesystem, 0, ""); + + wfp_client_config_set_userdata(config, filesystem); + wfp_client_config_set_onlookup(config, &wfp_static_filesystem_lookup); + wfp_client_config_set_ongetattr(config, &wfp_static_filesystem_getattr); + wfp_client_config_set_onreaddir(config, &wfp_static_filesystem_readdir); + wfp_client_config_set_onopen(config, &wfp_static_filesystem_open); + wfp_client_config_set_onread(config, &wfp_static_filesystem_read); return filesystem; } diff --git a/test/webfuse/utils/wrap.hpp b/test/webfuse/utils/wrap.hpp new file mode 100644 index 0000000..30694de --- /dev/null +++ b/test/webfuse/utils/wrap.hpp @@ -0,0 +1,132 @@ +#ifndef WF_WRAP_HPP +#define WF_WRAP_HPP + +#define WF_WRAP_FUNC0( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME ) \ + extern RETURN_TYPE __real_ ## FUNC_NAME (); \ + RETURN_TYPE __wrap_ ## FUNC_NAME () \ + { \ + if (nullptr == GLOBAL_VAR ) \ + { \ + return __real_ ## FUNC_NAME (); \ + } \ + else \ + { \ + return GLOBAL_VAR -> FUNC_NAME(); \ + } \ + } + +#define WF_WRAP_FUNC1( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE ) \ + extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE); \ + RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1) \ + { \ + if (nullptr == GLOBAL_VAR ) \ + { \ + return __real_ ## FUNC_NAME (arg1); \ + } \ + else \ + { \ + return GLOBAL_VAR -> FUNC_NAME(arg1); \ + } \ + } + +#define WF_WRAP_FUNC2( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE ) \ + extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE); \ + RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2) \ + { \ + if (nullptr == GLOBAL_VAR ) \ + { \ + return __real_ ## FUNC_NAME (arg1, arg2); \ + } \ + else \ + { \ + return GLOBAL_VAR -> FUNC_NAME(arg1, arg2); \ + } \ + } + +#define WF_WRAP_FUNC3( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE ) \ + extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE); \ + RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3) \ + { \ + if (nullptr == GLOBAL_VAR ) \ + { \ + return __real_ ## FUNC_NAME (arg1, arg2, arg3); \ + } \ + else \ + { \ + return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3); \ + } \ + } + +#define WF_WRAP_FUNC4( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE ) \ + extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE); \ + RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3, ARG4_TYPE arg4) \ + { \ + if (nullptr == GLOBAL_VAR ) \ + { \ + return __real_ ## FUNC_NAME (arg1, arg2, arg3, arg4); \ + } \ + else \ + { \ + return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3, arg4); \ + } \ + } + +#define WF_WRAP_FUNC5( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE, ARG5_TYPE ) \ + extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE, ARG5_TYPE); \ + RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3, ARG4_TYPE arg4, ARG5_TYPE arg5) \ + { \ + if (nullptr == GLOBAL_VAR ) \ + { \ + return __real_ ## FUNC_NAME (arg1, arg2, arg3, arg4, arg5); \ + } \ + else \ + { \ + return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3, arg4, arg5); \ + } \ + } + +#define WF_WRAP_FUNC6( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE, ARG5_TYPE, ARG6_TYPE ) \ + extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE, ARG5_TYPE, ARG6_TYPE); \ + RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3, ARG4_TYPE arg4, ARG5_TYPE arg5, ARG6_TYPE arg6) \ + { \ + if (nullptr == GLOBAL_VAR ) \ + { \ + return __real_ ## FUNC_NAME (arg1, arg2, arg3, arg4, arg5, arg6); \ + } \ + else \ + { \ + return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3, arg4, arg5, arg6); \ + } \ + } + + +#define WF_WRAP_VFUNC3( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE ) \ + extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, va_list); \ + RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3, va_list args) \ + { \ + if (nullptr == GLOBAL_VAR ) \ + { \ + return __real_ ## FUNC_NAME (arg1, arg2, arg3, args); \ + } \ + else \ + { \ + return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3); \ + } \ + } + +#define WF_WRAP_VFUNC5( GLOBAL_VAR, RETURN_TYPE, FUNC_NAME, ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE, ARG5_TYPE ) \ + extern RETURN_TYPE __real_ ## FUNC_NAME (ARG1_TYPE, ARG2_TYPE, ARG3_TYPE, ARG4_TYPE, ARG5_TYPE, va_list); \ + RETURN_TYPE __wrap_ ## FUNC_NAME (ARG1_TYPE arg1, ARG2_TYPE arg2, ARG3_TYPE arg3, ARG4_TYPE arg4, ARG5_TYPE arg5, va_list args) \ + { \ + if (nullptr == GLOBAL_VAR ) \ + { \ + return __real_ ## FUNC_NAME (arg1, arg2, arg3, arg4, arg5, args); \ + } \ + else \ + { \ + return GLOBAL_VAR -> FUNC_NAME(arg1, arg2, arg3, arg4, arg5); \ + } \ + } + + +#endif