From 3496c605d2f09d5a7a2f9a1407c891227d461ca3 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sun, 19 Jul 2020 09:52:25 +0200 Subject: [PATCH] removed dependency to libjansson --- changelog.md | 4 + doc/build.md | 12 - meson.build | 13 +- subprojects/jansson.wrap | 10 - test/webfuse/mocks/getattr_matcher.hpp | 12 +- test/webfuse/mocks/lookup_matcher.hpp | 20 +- .../webfuse/mocks/mock_invokation_handler.hpp | 2 +- test/webfuse/mocks/open_matcher.hpp | 12 +- test/webfuse/mocks/readdir_matcher.hpp | 12 +- test/webfuse/test_client.cc | 60 ++--- test/webfuse/test_credentials.cc | 1 - test/webfuse/test_server.cc | 241 +++++++++--------- test/webfuse/test_server_protocol.cc | 59 +++-- test/webfuse/test_util/invokation_handler.hpp | 5 +- .../test_util/jansson_test_environment.cc | 20 -- test/webfuse/test_util/ws_client.cc | 73 ++---- test/webfuse/test_util/ws_server.cc | 48 ++-- test/webfuse/test_util/ws_server.hpp | 2 - 18 files changed, 258 insertions(+), 348 deletions(-) delete mode 100644 subprojects/jansson.wrap delete mode 100644 test/webfuse/test_util/jansson_test_environment.cc diff --git a/changelog.md b/changelog.md index 538ba63..b861c1d 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,10 @@ ## 0.5.0 _(unknown)_ +### Features + +* Removed dependency to libjansson + ### Fixes * Fix deadlock reading files larger that a few KBytes (Isseue #82) diff --git a/doc/build.md b/doc/build.md index 2b5d329..78e2ff3 100644 --- a/doc/build.md +++ b/doc/build.md @@ -39,7 +39,6 @@ After that, you will find the API documentation in the doc/api subdirectory. - [libfuse3](https://github.com/libfuse/libfuse/) - [libwebsockets](https://libwebsockets.org/) -- [Jansson](https://jansson.readthedocs.io) - [GoogleTest](https://github.com/google/googletest) *(optional)* ### Installation from source @@ -66,17 +65,6 @@ After that, you will find the API documentation in the doc/api subdirectory. make sudo make install -#### Jansson - - wget -O libjansson-2.12.tar.gz https://github.com/akheron/jansson/archive/v2.12.tar.gz - tar -xf libjansson-2.12.tar.gz - cd jansson-2.12 - mkdir .build - cd .build - cmake .. - make - sudo make install - #### GoogleTest Installation of GoogleTest is optional webfuse library, but required to compile tests. diff --git a/meson.build b/meson.build index d358db0..b417804 100644 --- a/meson.build +++ b/meson.build @@ -9,7 +9,6 @@ if not libwebsockets_dep.found() libwebsockets_dep = libwebsockets.dependency('websockets_shared') endif -jansson_dep = dependency('jansson', version: '>=2.11', fallback: ['jansson', 'jansson_dep']) libfuse_dep = dependency('fuse3', version: '>=3.8.0', fallback: ['fuse3', 'libfuse_dep']) pkg_config = import('pkgconfig') @@ -68,31 +67,31 @@ webfuse_static = static_library('webfuse', 'lib/webfuse/impl/client_tlsconfig.c', c_args: ['-fvisibility=hidden'], include_directories: private_inc_dir, - dependencies: [libfuse_dep, libwebsockets_dep, jansson_dep]) + dependencies: [libfuse_dep, libwebsockets_dep]) webfuse_static_dep = declare_dependency( include_directories: inc_dir, link_with: [webfuse_static], - dependencies: [libfuse_dep, libwebsockets_dep, jansson_dep]) + dependencies: [libfuse_dep, libwebsockets_dep]) webfuse = shared_library('webfuse', 'lib/webfuse/api.c', version: meson.project_version(), c_args: ['-fvisibility=hidden', '-DWF_API=WF_EXPORT'], include_directories: private_inc_dir, - dependencies: [webfuse_static_dep, libfuse_dep, libwebsockets_dep, jansson_dep], + dependencies: [webfuse_static_dep, libfuse_dep, libwebsockets_dep], install: true) webfuse_dep = declare_dependency( include_directories: inc_dir, link_with: [webfuse], - dependencies: [libfuse_dep, libwebsockets_dep, jansson_dep]) + dependencies: [libfuse_dep, libwebsockets_dep]) install_subdir('include/webfuse', install_dir: 'include') pkg_config.generate( libraries: [webfuse], - requires: ['fuse3', 'libwebsockets', 'jansson'], + requires: ['fuse3', 'libwebsockets'], subdirs: '.', version: meson.project_version(), name: 'libwebfuse', @@ -145,7 +144,6 @@ alltests = executable('alltests', 'test/webfuse/test_util/ws_client.cc', 'test/webfuse/test_util/adapter_client.cc', 'test/webfuse/test_util/file.cc', - 'test/webfuse/test_util/jansson_test_environment.cc', 'test/webfuse/test_util/lws_test_environment.cc', 'test/webfuse/test_util/json_doc.cc', 'test/webfuse/mocks/mock_authenticator.cc', @@ -203,7 +201,6 @@ alltests = executable('alltests', webfuse_static_dep, libwebsockets_dep, libfuse_dep, - jansson_dep, gtest_dep, gmock_main_dep, test_certs_dep diff --git a/subprojects/jansson.wrap b/subprojects/jansson.wrap deleted file mode 100644 index 6282afd..0000000 --- a/subprojects/jansson.wrap +++ /dev/null @@ -1,10 +0,0 @@ -[wrap-file] -directory = jansson-2.11 - -source_url = http://www.digip.org/jansson/releases/jansson-2.11.tar.bz2 -source_filename = jansson-2.11.tar.bz2 -source_hash = 783132e2fc970feefc2fa54199ef65ee020bd8e0e991a78ea44b8586353a0947 - -patch_url = https://wrapdb.mesonbuild.com/v1/projects/jansson/2.11/3/get_zip -patch_filename = jansson-2.11-3-wrap.zip -patch_hash = 0bcac510994890048d42658c674e33dd7d88715fc1e3bf49d10012f57b0e0020 \ No newline at end of file diff --git a/test/webfuse/mocks/getattr_matcher.hpp b/test/webfuse/mocks/getattr_matcher.hpp index c1f6cbb..2b53eff 100644 --- a/test/webfuse/mocks/getattr_matcher.hpp +++ b/test/webfuse/mocks/getattr_matcher.hpp @@ -1,31 +1,31 @@ #ifndef WF_GETATTR_MATCHER_HPP #define WF_GETATTR_MATCHER_HPP +#include "webfuse/impl/json/node.h" #include -#include namespace webfuse_test { MATCHER_P(GetAttr, inode, "") { - if (!json_is_array(arg)) + if (!wf_impl_json_is_array(arg)) { *result_listener << "json array expected"; return false; } - json_t * inode_ = json_array_get(arg, 1); - if (!json_is_integer(inode_)) + wf_json const * inode_ = wf_impl_json_array_get(arg, 1); + if (!wf_impl_json_is_int(inode_)) { *result_listener << "inode is expectoed to an integer"; return false; } - if (inode != json_integer_value(inode_)) + if (inode != wf_impl_json_int_get(inode_)) { *result_listener << "inode mismatch: expected" << inode - << " but was " << json_integer_value(inode_); + << " but was " << wf_impl_json_int_get(inode_); return false; } diff --git a/test/webfuse/mocks/lookup_matcher.hpp b/test/webfuse/mocks/lookup_matcher.hpp index e9c5a55..7c9b761 100644 --- a/test/webfuse/mocks/lookup_matcher.hpp +++ b/test/webfuse/mocks/lookup_matcher.hpp @@ -1,8 +1,8 @@ #ifndef WF_LOOKUP_MATCHER_HPP #define WF_LOOKUP_MATCHER_HPP +#include "webfuse/impl/json/node.h" #include -#include #include namespace webfuse_test @@ -10,35 +10,35 @@ namespace webfuse_test MATCHER_P2(Lookup, parent, name, "") { - if (!json_is_array(arg)) + if (!wf_impl_json_is_array(arg)) { *result_listener << "json array expected"; return false; } - json_t * parent_ = json_array_get(arg, 1); - if (!json_is_integer(parent_)) + wf_json const * parent_ = wf_impl_json_array_get(arg, 1); + if (!wf_impl_json_is_int(parent_)) { *result_listener << "parent is expected to be an integer"; return false; } - if (parent != json_integer_value(parent_)) + if (parent != wf_impl_json_int_get(parent_)) { *result_listener << "parent mismatch: expected " << parent - << " but was " << json_integer_value(parent_); + << " but was " << wf_impl_json_int_get(parent_); return false; } - json_t * name_ = json_array_get(arg, 2); - if (!json_is_string(name_)) + wf_json const * name_ = wf_impl_json_array_get(arg, 2); + if (!wf_impl_json_is_string(name_)) { *result_listener << "name is expected to be a string"; return false; } - if (0 != strcmp(name, json_string_value(name_))) + if (0 != strcmp(name, wf_impl_json_string_get(name_))) { *result_listener << "name mismatch: expected \"" << name - << "\" but was \"" << json_string_value(name_) << "\""; + << "\" but was \"" << wf_impl_json_string_get(name_) << "\""; return false; } diff --git a/test/webfuse/mocks/mock_invokation_handler.hpp b/test/webfuse/mocks/mock_invokation_handler.hpp index 9c79159..ef5721b 100644 --- a/test/webfuse/mocks/mock_invokation_handler.hpp +++ b/test/webfuse/mocks/mock_invokation_handler.hpp @@ -10,7 +10,7 @@ namespace webfuse_test class MockInvokationHander: public InvokationHandler { public: - MOCK_METHOD2(Invoke, std::string(char const * method, json_t * params)); + MOCK_METHOD2(Invoke, std::string(char const * method, wf_json const * params)); }; } diff --git a/test/webfuse/mocks/open_matcher.hpp b/test/webfuse/mocks/open_matcher.hpp index 1bc7bf4..b34c987 100644 --- a/test/webfuse/mocks/open_matcher.hpp +++ b/test/webfuse/mocks/open_matcher.hpp @@ -1,31 +1,31 @@ #ifndef WF_OPEN_MATCHER_HPP #define WF_OPEN_MATCHER_HPP +#include "webfuse/impl/json/node.h" #include -#include namespace webfuse_test { MATCHER_P(Open, inode, "") { - if (!json_is_array(arg)) + if (!wf_impl_json_is_array(arg)) { *result_listener << "json array expected"; return false; } - json_t * inode_ = json_array_get(arg, 1); - if (!json_is_integer(inode_)) + wf_json const * inode_ = wf_impl_json_array_get(arg, 1); + if (!wf_impl_json_is_int(inode_)) { *result_listener << "inode is expectoed to an integer"; return false; } - if (inode != json_integer_value(inode_)) + if (inode != wf_impl_json_int_get(inode_)) { *result_listener << "inode mismatch: expected" << inode - << " but was " << json_integer_value(inode_); + << " but was " << wf_impl_json_int_get(inode_); return false; } diff --git a/test/webfuse/mocks/readdir_matcher.hpp b/test/webfuse/mocks/readdir_matcher.hpp index 6c8134f..86355c3 100644 --- a/test/webfuse/mocks/readdir_matcher.hpp +++ b/test/webfuse/mocks/readdir_matcher.hpp @@ -2,30 +2,30 @@ #define WF_READDIR_MATCHER_HPP #include -#include +#include "webfuse/impl/json/node.h" namespace webfuse_test { MATCHER_P(ReadDir, inode, "") { - if (!json_is_array(arg)) + if (!wf_impl_json_is_array(arg)) { *result_listener << "json array expected"; return false; } - json_t * inode_ = json_array_get(arg, 1); - if (!json_is_integer(inode_)) + wf_json const * inode_ = wf_impl_json_array_get(arg, 1); + if (!wf_impl_json_is_int(inode_)) { *result_listener << "inode is expectoed to an integer"; return false; } - if (inode != json_integer_value(inode_)) + if (inode != wf_impl_json_int_get(inode_)) { *result_listener << "inode mismatch: expected" << inode - << " but was " << json_integer_value(inode_); + << " but was " << wf_impl_json_int_get(inode_); return false; } diff --git a/test/webfuse/test_client.cc b/test/webfuse/test_client.cc index 37b4a8b..0924368 100644 --- a/test/webfuse/test_client.cc +++ b/test/webfuse/test_client.cc @@ -1,6 +1,3 @@ -#include -#include - #include "webfuse/test_util/adapter_client.hpp" #include "webfuse/client_tlsconfig.h" #include "webfuse/credentials.h" @@ -13,8 +10,12 @@ #include "webfuse/mocks/open_matcher.hpp" #include "webfuse/mocks/getattr_matcher.hpp" +#include +#include + #include #include +#include using webfuse_test::AdapterClient; using webfuse_test::WsServer; @@ -148,8 +149,7 @@ TEST(AdapterClient, IgnoreInvalidJsonMessage) client.Connect(); ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT)); - json_t * invalid_request = json_object(); - server.SendMessage(invalid_request); + server.SendMessage("{}"); client.Disconnect(); ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT)); @@ -566,26 +566,21 @@ TEST(AdapterClient, ReadFile) EXPECT_CALL(handler, Invoke(StrEq("open"), Open(2))).Times(1) .WillOnce(Return("{\"handle\": 42}")); EXPECT_CALL(handler, Invoke(StrEq("read"), _)).Times(AnyNumber()) - .WillRepeatedly(Invoke([](char const *, json_t * params) { - int offset = json_integer_value(json_array_get(params, 3)); - int length = json_integer_value(json_array_get(params, 4)); + .WillRepeatedly(Invoke([](char const *, wf_json const * params) { + int offset = wf_impl_json_int_get(wf_impl_json_array_get(params, 3)); + int length = wf_impl_json_int_get(wf_impl_json_array_get(params, 4)); int remaining = (offset < 4096) ? 4096 - offset : 0; int count = (length < remaining) ? length : remaining; - std::string data = std::string(count, '*'); + std::ostringstream result; + result << "{" + << "\"data\": \"" << std::string(count, '*') << "\"," + << "\"format\": \"identity\"," + << "\"count\": " << count + << "}"; - json_t * result = json_object(); - json_object_set_new(result, "data", json_string(data.c_str())); - json_object_set_new(result, "format", json_string("identity")); - json_object_set_new(result, "count", json_integer(count)); - - char * result_text = json_dumps(result, 0); - std::string result_str = result_text; - free(result_text); - json_decref(result); - - return result_str; + return result.str(); })); EXPECT_CALL(handler, Invoke(StrEq("close"), _)).Times(AtMost(1)); @@ -641,26 +636,21 @@ TEST(AdapterClient, ReadLargeFile) EXPECT_CALL(handler, Invoke(StrEq("open"), Open(2))).Times(1) .WillOnce(Return("{\"handle\": 42}")); EXPECT_CALL(handler, Invoke(StrEq("read"), _)).Times(AnyNumber()) - .WillRepeatedly(Invoke([](char const *, json_t * params) { - int offset = json_integer_value(json_array_get(params, 3)); - int length = json_integer_value(json_array_get(params, 4)); + .WillRepeatedly(Invoke([](char const *, wf_json const * params) { + int offset = wf_impl_json_int_get(wf_impl_json_array_get(params, 3)); + int length = wf_impl_json_int_get(wf_impl_json_array_get(params, 4)); int remaining = (offset < 102400) ? 102400 - offset : 0; int count = (length < remaining) ? length : remaining; - std::string data = std::string(count, '*'); + std::ostringstream result; + result << "{" + << "\"data\": \"" << std::string(count, '*') << "\"," + << "\"format\": \"identity\"," + << "\"count\": " << count + << "}"; - json_t * result = json_object(); - json_object_set_new(result, "data", json_string(data.c_str())); - json_object_set_new(result, "format", json_string("identity")); - json_object_set_new(result, "count", json_integer(count)); - - char * result_text = json_dumps(result, 0); - std::string result_str = result_text; - free(result_text); - json_decref(result); - - return result_str; + return result.str(); })); EXPECT_CALL(handler, Invoke(StrEq("close"), _)).Times(AtMost(1)); diff --git a/test/webfuse/test_credentials.cc b/test/webfuse/test_credentials.cc index e3daf7b..4dfbc90 100644 --- a/test/webfuse/test_credentials.cc +++ b/test/webfuse/test_credentials.cc @@ -2,7 +2,6 @@ #include "webfuse/impl/credentials.h" #include "webfuse/test_util/json_doc.hpp" -#include #include using webfuse_test::JsonDoc; diff --git a/test/webfuse/test_server.cc b/test/webfuse/test_server.cc index 85aba28..b7482b3 100644 --- a/test/webfuse/test_server.cc +++ b/test/webfuse/test_server.cc @@ -1,19 +1,20 @@ -#include - -#include - #include "webfuse/server.h" #include "webfuse/server_config.h" #include "webfuse/test_util/server.hpp" #include "webfuse/test_util/ws_client.hpp" #include "webfuse/test_util/file.hpp" +#include "webfuse/test_util/json_doc.hpp" #include "webfuse/mocks/mock_invokation_handler.hpp" #include "webfuse/protocol_names.h" #include "webfuse/mocks/open_matcher.hpp" #include "webfuse/mocks/getattr_matcher.hpp" #include "webfuse/mocks/lookup_matcher.hpp" #include "webfuse/mocks/readdir_matcher.hpp" +#include "webfuse/impl/json/node.h" +#include + +using webfuse_test::JsonDoc; using webfuse_test::MockInvokationHander; using webfuse_test::WsClient; using webfuse_test::Server; @@ -85,13 +86,13 @@ TEST(server, add_filesystem) ASSERT_TRUE(connected); std::string response_text = client.Invoke("{\"method\": \"add_filesystem\", \"params\": [\"test\"], \"id\": 42}"); - json_t * response = json_loads(response_text.c_str(), 0, nullptr); - ASSERT_TRUE(json_is_object(response)); - json_t * result = json_object_get(response, "result"); - ASSERT_TRUE(json_is_object(result)); - json_t * id = json_object_get(response, "id"); - ASSERT_EQ(42, json_integer_value(id)); - json_decref(response); + JsonDoc doc(response_text); + wf_json const * response = doc.root(); + ASSERT_TRUE(wf_impl_json_is_object(response)); + wf_json const * result = wf_impl_json_object_get(response, "result"); + ASSERT_TRUE(wf_impl_json_is_object(result)); + wf_json const * id = wf_impl_json_object_get(response, "id"); + ASSERT_EQ(42, wf_impl_json_int_get(id)); std::string base_dir = server.GetBaseDir(); ASSERT_TRUE(File(base_dir).isDirectory()); @@ -112,13 +113,13 @@ TEST(server, add_filesystem_fail_missing_param) ASSERT_TRUE(connected); std::string response_text = client.Invoke("{\"method\": \"add_filesystem\", \"params\": [], \"id\": 42}"); - json_t * response = json_loads(response_text.c_str(), 0, nullptr); - ASSERT_TRUE(json_is_object(response)); - json_t * error = json_object_get(response, "error"); - ASSERT_TRUE(json_is_object(error)); - json_t * id = json_object_get(response, "id"); - ASSERT_EQ(42, json_integer_value(id)); - json_decref(response); + JsonDoc doc(response_text); + wf_json const * response = doc.root(); + ASSERT_TRUE(wf_impl_json_is_object(response)); + wf_json const * error = wf_impl_json_object_get(response, "error"); + ASSERT_TRUE(wf_impl_json_is_object(error)); + wf_json const * id = wf_impl_json_object_get(response, "id"); + ASSERT_EQ(42, wf_impl_json_int_get(id)); auto disconnected = client.Disconnect(); ASSERT_TRUE(disconnected); @@ -134,13 +135,13 @@ TEST(server, add_filesystem_fail_invalid_name_type) ASSERT_TRUE(connected); std::string response_text = client.Invoke("{\"method\": \"add_filesystem\", \"params\": [42], \"id\": 42}"); - json_t * response = json_loads(response_text.c_str(), 0, nullptr); - ASSERT_TRUE(json_is_object(response)); - json_t * error = json_object_get(response, "error"); - ASSERT_TRUE(json_is_object(error)); - json_t * id = json_object_get(response, "id"); - ASSERT_EQ(42, json_integer_value(id)); - json_decref(response); + JsonDoc doc(response_text); + wf_json const * response = doc.root(); + ASSERT_TRUE(wf_impl_json_is_object(response)); + wf_json const * error = wf_impl_json_object_get(response, "error"); + ASSERT_TRUE(wf_impl_json_is_object(error)); + wf_json const * id = wf_impl_json_object_get(response, "id"); + ASSERT_EQ(42, wf_impl_json_int_get(id)); auto disconnected = client.Disconnect(); ASSERT_TRUE(disconnected); @@ -156,13 +157,13 @@ TEST(server, add_filesystem_fail_invalid_name) ASSERT_TRUE(connected); std::string response_text = client.Invoke("{\"method\": \"add_filesystem\", \"params\": [\"invalid_1/name\"], \"id\": 42}"); - json_t * response = json_loads(response_text.c_str(), 0, nullptr); - ASSERT_TRUE(json_is_object(response)); - json_t * error = json_object_get(response, "error"); - ASSERT_TRUE(json_is_object(error)); - json_t * id = json_object_get(response, "id"); - ASSERT_EQ(42, json_integer_value(id)); - json_decref(response); + JsonDoc doc(response_text); + wf_json const * response = doc.root(); + ASSERT_TRUE(wf_impl_json_is_object(response)); + wf_json const * error = wf_impl_json_object_get(response, "error"); + ASSERT_TRUE(wf_impl_json_is_object(error)); + wf_json const * id = wf_impl_json_object_get(response, "id"); + ASSERT_EQ(42, wf_impl_json_int_get(id)); auto disconnected = client.Disconnect(); ASSERT_TRUE(disconnected); @@ -179,13 +180,13 @@ TEST(server, authenticate) ASSERT_TRUE(connected); std::string response_text = client.Invoke("{\"method\": \"authenticate\", \"params\": [\"username\", {\"username\": \"bob\", \"password\": \"secret\"}], \"id\": 42}"); - json_t * response = json_loads(response_text.c_str(), 0, nullptr); - ASSERT_TRUE(json_is_object(response)); - json_t * result = json_object_get(response, "result"); - ASSERT_TRUE(json_is_object(result)); - json_t * id = json_object_get(response, "id"); - ASSERT_EQ(42, json_integer_value(id)); - json_decref(response); + JsonDoc doc(response_text); + wf_json const * response = doc.root(); + ASSERT_TRUE(wf_impl_json_is_object(response)); + wf_json const * result = wf_impl_json_object_get(response, "result"); + ASSERT_TRUE(wf_impl_json_is_object(result)); + wf_json const * id = wf_impl_json_object_get(response, "id"); + ASSERT_EQ(42, wf_impl_json_int_get(id)); auto disconnected = client.Disconnect(); ASSERT_TRUE(disconnected); @@ -201,13 +202,13 @@ TEST(server, authenticate_fail_missing_params) ASSERT_TRUE(connected); std::string response_text = client.Invoke("{\"method\": \"authenticate\", \"params\": [], \"id\": 42}"); - json_t * response = json_loads(response_text.c_str(), 0, nullptr); - ASSERT_TRUE(json_is_object(response)); - json_t * error = json_object_get(response, "error"); - ASSERT_TRUE(json_is_object(error)); - json_t * id = json_object_get(response, "id"); - ASSERT_EQ(42, json_integer_value(id)); - json_decref(response); + JsonDoc doc(response_text); + wf_json const * response = doc.root(); + ASSERT_TRUE(wf_impl_json_is_object(response)); + wf_json const * error = wf_impl_json_object_get(response, "error"); + ASSERT_TRUE(wf_impl_json_is_object(error)); + wf_json const * id = wf_impl_json_object_get(response, "id"); + ASSERT_EQ(42, wf_impl_json_int_get(id)); auto disconnected = client.Disconnect(); ASSERT_TRUE(disconnected); @@ -223,13 +224,13 @@ TEST(server, authenticate_fail_invalid_type) ASSERT_TRUE(connected); std::string response_text = client.Invoke("{\"method\": \"authenticate\", \"params\": [42, {\"username\": \"bob\", \"password\": \"secret\"}], \"id\": 42}"); - json_t * response = json_loads(response_text.c_str(), 0, nullptr); - ASSERT_TRUE(json_is_object(response)); - json_t * error = json_object_get(response, "error"); - ASSERT_TRUE(json_is_object(error)); - json_t * id = json_object_get(response, "id"); - ASSERT_EQ(42, json_integer_value(id)); - json_decref(response); + JsonDoc doc(response_text); + wf_json const * response = doc.root(); + ASSERT_TRUE(wf_impl_json_is_object(response)); + wf_json const * error = wf_impl_json_object_get(response, "error"); + ASSERT_TRUE(wf_impl_json_is_object(error)); + wf_json const * id = wf_impl_json_object_get(response, "id"); + ASSERT_EQ(42, wf_impl_json_int_get(id)); auto disconnected = client.Disconnect(); ASSERT_TRUE(disconnected); @@ -245,13 +246,13 @@ TEST(server, authenticate_fail_invalid_credentials) ASSERT_TRUE(connected); std::string response_text = client.Invoke("{\"method\": \"authenticate\", \"params\": [\"username\", 42], \"id\": 42}"); - json_t * response = json_loads(response_text.c_str(), 0, nullptr); - ASSERT_TRUE(json_is_object(response)); - json_t * error = json_object_get(response, "error"); - ASSERT_TRUE(json_is_object(error)); - json_t * id = json_object_get(response, "id"); - ASSERT_EQ(42, json_integer_value(id)); - json_decref(response); + JsonDoc doc(response_text); + wf_json const * response = doc.root(); + ASSERT_TRUE(wf_impl_json_is_object(response)); + wf_json const * error = wf_impl_json_object_get(response, "error"); + ASSERT_TRUE(wf_impl_json_is_object(error)); + wf_json const * id = wf_impl_json_object_get(response, "id"); + ASSERT_EQ(42, wf_impl_json_int_get(id)); auto disconnected = client.Disconnect(); ASSERT_TRUE(disconnected); @@ -267,13 +268,13 @@ TEST(server, authenticate_fail_missing_credentials) ASSERT_TRUE(connected); std::string response_text = client.Invoke("{\"method\": \"authenticate\", \"params\": [\"username\"], \"id\": 42}"); - json_t * response = json_loads(response_text.c_str(), 0, nullptr); - ASSERT_TRUE(json_is_object(response)); - json_t * error = json_object_get(response, "error"); - ASSERT_TRUE(json_is_object(error)); - json_t * id = json_object_get(response, "id"); - ASSERT_EQ(42, json_integer_value(id)); - json_decref(response); + JsonDoc doc(response_text); + wf_json const * response = doc.root(); + ASSERT_TRUE(wf_impl_json_is_object(response)); + wf_json const * error = wf_impl_json_object_get(response, "error"); + ASSERT_TRUE(wf_impl_json_is_object(error)); + wf_json const * id = wf_impl_json_object_get(response, "id"); + ASSERT_EQ(42, wf_impl_json_int_get(id)); auto disconnected = client.Disconnect(); ASSERT_TRUE(disconnected); @@ -299,13 +300,13 @@ TEST(server, read) ASSERT_TRUE(connected); std::string response_text = client.Invoke("{\"method\": \"add_filesystem\", \"params\": [\"test\"], \"id\": 42}"); - json_t * response = json_loads(response_text.c_str(), 0, nullptr); - ASSERT_TRUE(json_is_object(response)); - json_t * result = json_object_get(response, "result"); - ASSERT_TRUE(json_is_object(result)); - json_t * id = json_object_get(response, "id"); - ASSERT_EQ(42, json_integer_value(id)); - json_decref(response); + JsonDoc doc(response_text); + wf_json const * response = doc.root(); + ASSERT_TRUE(wf_impl_json_is_object(response)); + wf_json const * result = wf_impl_json_object_get(response, "result"); + ASSERT_TRUE(wf_impl_json_is_object(result)); + wf_json const * id = wf_impl_json_object_get(response, "id"); + ASSERT_EQ(42, wf_impl_json_int_get(id)); std::string base_dir = server.GetBaseDir(); ASSERT_TRUE(File(base_dir).isDirectory()); @@ -330,26 +331,21 @@ TEST(server, read_large_file_contents) EXPECT_CALL(handler, Invoke(StrEq("open"), Open(2))).Times(1) .WillOnce(Return("{\"handle\": 42}")); EXPECT_CALL(handler, Invoke(StrEq("read"), _)).Times(AnyNumber()) - .WillRepeatedly(Invoke([](char const *, json_t * params) { - int offset = json_integer_value(json_array_get(params, 3)); - int length = json_integer_value(json_array_get(params, 4)); + .WillRepeatedly(Invoke([](char const *, wf_json const * params) { + int offset = wf_impl_json_int_get(wf_impl_json_array_get(params, 3)); + int length = wf_impl_json_int_get(wf_impl_json_array_get(params, 4)); int remaining = (offset < 4096) ? 4096 - offset : 0; int count = (length < remaining) ? length : remaining; - std::string data = std::string(count, '*'); + std::ostringstream result; + result << "{" + << "\"data\": \"" << std::string(count, '*') << "\"," + << "\"format\": \"identity\"," + << "\"count\": " << count + << "}"; - json_t * result = json_object(); - json_object_set_new(result, "data", json_string(data.c_str())); - json_object_set_new(result, "format", json_string("identity")); - json_object_set_new(result, "count", json_integer(count)); - - char * result_text = json_dumps(result, 0); - std::string result_str = result_text; - free(result_text); - json_decref(result); - - return result_str; + return result.str(); })); EXPECT_CALL(handler, Invoke(StrEq("close"), _)).Times(AtMost(1)); WsClient client(handler, WF_PROTOCOL_NAME_PROVIDER_CLIENT); @@ -358,13 +354,13 @@ TEST(server, read_large_file_contents) ASSERT_TRUE(connected); std::string response_text = client.Invoke("{\"method\": \"add_filesystem\", \"params\": [\"test\"], \"id\": 42}"); - json_t * response = json_loads(response_text.c_str(), 0, nullptr); - ASSERT_TRUE(json_is_object(response)); - json_t * result = json_object_get(response, "result"); - ASSERT_TRUE(json_is_object(result)); - json_t * id = json_object_get(response, "id"); - ASSERT_EQ(42, json_integer_value(id)); - json_decref(response); + JsonDoc doc(response_text); + wf_json const * response = doc.root(); + ASSERT_TRUE(wf_impl_json_is_object(response)); + wf_json const * result = wf_impl_json_object_get(response, "result"); + ASSERT_TRUE(wf_impl_json_is_object(result)); + wf_json const * id = wf_impl_json_object_get(response, "id"); + ASSERT_EQ(42, wf_impl_json_int_get(id)); std::string base_dir = server.GetBaseDir(); ASSERT_TRUE(File(base_dir).isDirectory()); @@ -390,26 +386,21 @@ TEST(server, read_large_file) EXPECT_CALL(handler, Invoke(StrEq("open"), Open(2))).Times(1) .WillOnce(Return("{\"handle\": 42}")); EXPECT_CALL(handler, Invoke(StrEq("read"), _)).Times(AnyNumber()) - .WillRepeatedly(Invoke([](char const *, json_t * params) { - int offset = json_integer_value(json_array_get(params, 3)); - int length = json_integer_value(json_array_get(params, 4)); + .WillRepeatedly(Invoke([](char const *, wf_json const * params) { + int offset = wf_impl_json_int_get(wf_impl_json_array_get(params, 3)); + int length = wf_impl_json_int_get(wf_impl_json_array_get(params, 4)); int remaining = (offset < 1024000) ? 1024000 - offset : 0; int count = (length < remaining) ? length : remaining; - std::string data = std::string(count, '*'); + std::ostringstream result; + result << "{" + << "\"data\": \"" << std::string(count, '*') << "\"," + << "\"format\": \"identity\"," + << "\"count\": " << count + << "}"; - json_t * result = json_object(); - json_object_set_new(result, "data", json_string(data.c_str())); - json_object_set_new(result, "format", json_string("identity")); - json_object_set_new(result, "count", json_integer(count)); - - char * result_text = json_dumps(result, 0); - std::string result_str = result_text; - free(result_text); - json_decref(result); - - return result_str; + return result.str(); })); EXPECT_CALL(handler, Invoke(StrEq("close"), _)).Times(AtMost(1)); WsClient client(handler, WF_PROTOCOL_NAME_PROVIDER_CLIENT); @@ -418,13 +409,13 @@ TEST(server, read_large_file) ASSERT_TRUE(connected); std::string response_text = client.Invoke("{\"method\": \"add_filesystem\", \"params\": [\"test\"], \"id\": 42}"); - json_t * response = json_loads(response_text.c_str(), 0, nullptr); - ASSERT_TRUE(json_is_object(response)); - json_t * result = json_object_get(response, "result"); - ASSERT_TRUE(json_is_object(result)); - json_t * id = json_object_get(response, "id"); - ASSERT_EQ(42, json_integer_value(id)); - json_decref(response); + JsonDoc doc(response_text); + wf_json const * response = doc.root(); + ASSERT_TRUE(wf_impl_json_is_object(response)); + wf_json const * result = wf_impl_json_object_get(response, "result"); + ASSERT_TRUE(wf_impl_json_is_object(result)); + wf_json const * id = wf_impl_json_object_get(response, "id"); + ASSERT_EQ(42, wf_impl_json_int_get(id)); std::string base_dir = server.GetBaseDir(); ASSERT_TRUE(File(base_dir).isDirectory()); @@ -450,13 +441,13 @@ TEST(server, readdir) ASSERT_TRUE(connected); std::string response_text = client.Invoke("{\"method\": \"add_filesystem\", \"params\": [\"test\"], \"id\": 42}"); - json_t * response = json_loads(response_text.c_str(), 0, nullptr); - ASSERT_TRUE(json_is_object(response)); - json_t * result = json_object_get(response, "result"); - ASSERT_TRUE(json_is_object(result)); - json_t * id = json_object_get(response, "id"); - ASSERT_EQ(42, json_integer_value(id)); - json_decref(response); + JsonDoc doc(response_text); + wf_json const * response = doc.root(); + ASSERT_TRUE(wf_impl_json_is_object(response)); + wf_json const * result = wf_impl_json_object_get(response, "result"); + ASSERT_TRUE(wf_impl_json_is_object(result)); + wf_json const * id = wf_impl_json_object_get(response, "id"); + ASSERT_EQ(42, wf_impl_json_int_get(id)); std::string base_dir = server.GetBaseDir(); ASSERT_TRUE(File(base_dir).isDirectory()); diff --git a/test/webfuse/test_server_protocol.cc b/test/webfuse/test_server_protocol.cc index bc608ba..5c1a1fd 100644 --- a/test/webfuse/test_server_protocol.cc +++ b/test/webfuse/test_server_protocol.cc @@ -1,6 +1,8 @@ #include "webfuse/test_util/server_protocol.hpp" #include "webfuse/test_util/ws_client.hpp" #include "webfuse/test_util/file.hpp" +#include "webfuse/test_util/json_doc.hpp" +#include "webfuse/impl/json/node.h" #include "webfuse/mocks/mock_invokation_handler.hpp" #include "webfuse/mocks/getattr_matcher.hpp" #include "webfuse/protocol_names.h" @@ -8,6 +10,7 @@ #include #include +using webfuse_test::JsonDoc; using webfuse_test::MockInvokationHander; using webfuse_test::WsClient; using webfuse_test::ServerProtocol; @@ -33,24 +36,24 @@ TEST(server_protocol, add_filesystem) { std::string response_text = client.Invoke("{\"method\": \"authenticate\", \"params\": [\"username\", {\"username\": \"bob\", \"password\": \"secret\"}], \"id\": 23}"); - json_t * response = json_loads(response_text.c_str(), 0, nullptr); - ASSERT_TRUE(json_is_object(response)); - json_t * result = json_object_get(response, "result"); - ASSERT_TRUE(json_is_object(result)); - json_t * id = json_object_get(response, "id"); - ASSERT_EQ(23, json_integer_value(id)); - json_decref(response); + JsonDoc doc(response_text); + wf_json const * response = doc.root(); + ASSERT_TRUE(wf_impl_json_is_object(response)); + wf_json const * result = wf_impl_json_object_get(response, "result"); + ASSERT_TRUE(wf_impl_json_is_object(result)); + wf_json const * id = wf_impl_json_object_get(response, "id"); + ASSERT_EQ(23, wf_impl_json_int_get(id)); } { std::string response_text = client.Invoke("{\"method\": \"add_filesystem\", \"params\": [\"test\"], \"id\": 42}"); - json_t * response = json_loads(response_text.c_str(), 0, nullptr); - ASSERT_TRUE(json_is_object(response)); - json_t * result = json_object_get(response, "result"); - ASSERT_TRUE(json_is_object(result)); - json_t * id = json_object_get(response, "id"); - ASSERT_EQ(42, json_integer_value(id)); - json_decref(response); + JsonDoc doc(response_text); + wf_json const * response = doc.root(); + ASSERT_TRUE(wf_impl_json_is_object(response)); + wf_json const * result = wf_impl_json_object_get(response, "result"); + ASSERT_TRUE(wf_impl_json_is_object(result)); + wf_json const * id = wf_impl_json_object_get(response, "id"); + ASSERT_EQ(42, wf_impl_json_int_get(id)); } std::string base_dir = server.GetBaseDir(); @@ -76,13 +79,13 @@ TEST(server_protocol, add_filesystem_fail_without_authentication) { std::string response_text = client.Invoke("{\"method\": \"add_filesystem\", \"params\": [\"test\"], \"id\": 42}"); - json_t * response = json_loads(response_text.c_str(), 0, nullptr); - ASSERT_TRUE(json_is_object(response)); - json_t * error = json_object_get(response, "error"); - ASSERT_TRUE(json_is_object(error)); - json_t * id = json_object_get(response, "id"); - ASSERT_EQ(42, json_integer_value(id)); - json_decref(response); + JsonDoc doc(response_text); + wf_json const * response = doc.root(); + ASSERT_TRUE(wf_impl_json_is_object(response)); + wf_json const * error = wf_impl_json_object_get(response, "error"); + ASSERT_TRUE(wf_impl_json_is_object(error)); + wf_json const * id = wf_impl_json_object_get(response, "id"); + ASSERT_EQ(42, wf_impl_json_int_get(id)); } auto disconnected = client.Disconnect(); @@ -103,13 +106,13 @@ TEST(server_protocol, authenticate_fail_wrong_credentials) { std::string response_text = client.Invoke("{\"method\": \"authenticate\", \"params\": [\"username\", {\"username\": \"alice\", \"password\": \"cheshire\"}], \"id\": 23}"); - json_t * response = json_loads(response_text.c_str(), 0, nullptr); - ASSERT_TRUE(json_is_object(response)); - json_t * error = json_object_get(response, "error"); - ASSERT_TRUE(json_is_object(error)); - json_t * id = json_object_get(response, "id"); - ASSERT_EQ(23, json_integer_value(id)); - json_decref(response); + JsonDoc doc(response_text); + wf_json const * response = doc.root(); + ASSERT_TRUE(wf_impl_json_is_object(response)); + wf_json const * error = wf_impl_json_object_get(response, "error"); + ASSERT_TRUE(wf_impl_json_is_object(error)); + wf_json const * id = wf_impl_json_object_get(response, "id"); + ASSERT_EQ(23, wf_impl_json_int_get(id)); } auto disconnected = client.Disconnect(); diff --git a/test/webfuse/test_util/invokation_handler.hpp b/test/webfuse/test_util/invokation_handler.hpp index ac8b7fe..84d5acb 100644 --- a/test/webfuse/test_util/invokation_handler.hpp +++ b/test/webfuse/test_util/invokation_handler.hpp @@ -1,9 +1,10 @@ #ifndef WF_TEST_UTIL_INVOKATION_HANDLER_HPP #define WF_TEST_UTIL_INVOKATION_HANDLER_HPP -#include #include +struct wf_json; + namespace webfuse_test { @@ -11,7 +12,7 @@ class InvokationHandler { public: virtual ~InvokationHandler() = default; - virtual std::string Invoke(char const * method, json_t * params) = 0; + virtual std::string Invoke(char const * method, wf_json const * params) = 0; }; } diff --git a/test/webfuse/test_util/jansson_test_environment.cc b/test/webfuse/test_util/jansson_test_environment.cc deleted file mode 100644 index d7e3c2d..0000000 --- a/test/webfuse/test_util/jansson_test_environment.cc +++ /dev/null @@ -1,20 +0,0 @@ -#include -#include - -namespace webfuse_test -{ - -class JanssonTestEnvironment: public ::testing::Environment -{ -public: - ~JanssonTestEnvironment() override { } - - void SetUp() override - { - json_object_seed(0); - } -}; - -::testing::Environment * const jansson_env = ::testing::AddGlobalTestEnvironment(new JanssonTestEnvironment()); - -} \ No newline at end of file diff --git a/test/webfuse/test_util/ws_client.cc b/test/webfuse/test_util/ws_client.cc index d074f7b..af9cda2 100644 --- a/test/webfuse/test_util/ws_client.cc +++ b/test/webfuse/test_util/ws_client.cc @@ -1,9 +1,10 @@ #include "webfuse/test_util/ws_client.hpp" #include "webfuse/test_util/invokation_handler.hpp" +#include "webfuse/test_util/json_doc.hpp" +#include "webfuse/impl/json/node.h" #include "webfuse/status.h" #include -#include #include #include @@ -11,6 +12,7 @@ #include #include #include +#include #define TIMEOUT (std::chrono::seconds(10)) @@ -225,52 +227,33 @@ public: { lock.unlock(); - json_t * request = json_loadb(data, length, 0, nullptr); - if (nullptr != request) + JsonDoc doc(std::string(data, length)); + wf_json const * request = doc.root(); + wf_json const * method = wf_impl_json_object_get(request, "method"); + wf_json const * params = wf_impl_json_object_get(request, "params"); + wf_json const * id = wf_impl_json_object_get(request, "id"); + + std::ostringstream response; + response << "{"; + try { - json_t * method = json_object_get(request, "method"); - json_t * params = json_object_get(request, "params"); - json_t * id = json_object_get(request, "id"); - - json_t * response = json_object(); - try - { - std::string result_text = handler_.Invoke(json_string_value(method), params); - json_t * result = json_loads(result_text.c_str(), 0, nullptr); - if (nullptr != result) - { - json_object_set_new(response, "result", result); - } - else - { - json_t * error = json_object(); - json_object_set_new(error, "code", json_integer(WF_BAD)); - json_object_set_new(response, "error",error); - } - - } - catch (...) - { - json_t * error = json_object(); - json_object_set_new(error, "code", json_integer(WF_BAD)); - json_object_set_new(response, "error",error); - } - - json_object_set(response, "id", id); - - char * response_text = json_dumps(response, 0); - lock.lock(); - send_queue.push(response_text); - commands.push(command::send); - lock.unlock(); - - lws_cancel_service(context); - - free(response_text); - - json_decref(response); - json_decref(request); + std::string result_text = handler_.Invoke(wf_impl_json_string_get(method), params); + if (result_text.empty()) { throw std::runtime_error("empty"); } + response << "\"result\": " << result_text; } + catch (...) + { + response << "\"error\": {\"code\": 1}"; + } + + response << ", \"id\": " << wf_impl_json_int_get(id) << "}"; + + lock.lock(); + send_queue.push(response.str()); + commands.push(command::send); + lock.unlock(); + + lws_cancel_service(context); } } diff --git a/test/webfuse/test_util/ws_server.cc b/test/webfuse/test_util/ws_server.cc index d9631be..2e50a35 100644 --- a/test/webfuse/test_util/ws_server.cc +++ b/test/webfuse/test_util/ws_server.cc @@ -1,6 +1,8 @@ #include "webfuse/test_util/ws_server.hpp" #include "webfuse/test_util/invokation_handler.hpp" #include "webfuse/impl/util/lws_log.h" +#include "webfuse/impl/json/node.h" +#include "webfuse/test_util/json_doc.hpp" #include #include @@ -9,6 +11,8 @@ #include #include +using webfuse_test::JsonDoc; + namespace { @@ -85,7 +89,6 @@ public: void OnWritable(struct lws * wsi) override; void SendMessage(char const * message); - void SendMessage(json_t * message); private: static void Run(Private * self); @@ -127,12 +130,6 @@ void WsServer::SendMessage(char const * message) d->SendMessage(message); } -void WsServer::SendMessage(json_t * message) -{ - d->SendMessage(message); -} - - WsServer::Private::Private( InvokationHandler & handler, std::string const & protocol, @@ -264,45 +261,34 @@ void WsServer::Private::SendMessage(char const * message) } } -void WsServer::Private::SendMessage(json_t * message) -{ - char* message_text = json_dumps(message, JSON_COMPACT); - SendMessage(message_text); - json_decref(message); - free(message_text); -} - void WsServer::Private::OnMessageReceived(struct lws * wsi, char const * data, size_t length) { (void) wsi; - json_t * request = json_loadb(data, length, JSON_DECODE_ANY, nullptr); - json_t * method = json_object_get(request, "method"); - json_t * params = json_object_get(request, "params"); - json_t * id = json_object_get(request, "id"); + JsonDoc doc(std::string(data, length)); + wf_json const * request = doc.root(); + wf_json const * method = wf_impl_json_object_get(request, "method"); + wf_json const * params = wf_impl_json_object_get(request, "params"); + wf_json const * id = wf_impl_json_object_get(request, "id"); - if (json_is_string(method) && json_is_array(params) && json_is_integer(id)) + if (wf_impl_json_is_string(method) && wf_impl_json_is_array(params) && wf_impl_json_is_int(id)) { - json_t * response = json_object(); + std::ostringstream response; + response << "{"; try { - std::string result_text = handler_.Invoke(json_string_value(method), params); - json_t * result = json_loads(result_text.c_str(), JSON_DECODE_ANY, nullptr); - json_object_set_new(response, "result", result); + std::string result_text = handler_.Invoke(wf_impl_json_string_get(method), params); + response << "\"result\": " << result_text; } catch (...) { - json_t * error = json_object(); - json_object_set_new(error, "code", json_integer(1)); - json_object_set_new(response, "error", error); + response << "\"error\": {\"code\": 1}"; } + response << ", \"id\": " << wf_impl_json_int_get(id) << "}"; - json_object_set(response, "id", id); - SendMessage(response); + SendMessage(response.str().c_str()); } - - json_decref(request); } std::string const & WsServer::Private::GetUrl() const diff --git a/test/webfuse/test_util/ws_server.hpp b/test/webfuse/test_util/ws_server.hpp index e8b0dfe..0f36336 100644 --- a/test/webfuse/test_util/ws_server.hpp +++ b/test/webfuse/test_util/ws_server.hpp @@ -1,7 +1,6 @@ #ifndef WF_TEST_UTIL_WS_SERVER2_HPP #define WF_TEST_UTIL_WS_SERVER2_HPP -#include #include namespace webfuse_test @@ -22,7 +21,6 @@ public: virtual ~WsServer(); std::string const & GetUrl() const; void SendMessage(char const * message); - void SendMessage(json_t * message); private: class Private; Private * d;