diff --git a/lib/webfuse_provider/impl/client_protocol.c b/lib/webfuse_provider/impl/client_protocol.c index c32e454..3359058 100644 --- a/lib/webfuse_provider/impl/client_protocol.c +++ b/lib/webfuse_provider/impl/client_protocol.c @@ -31,11 +31,8 @@ static void wfp_impl_client_protocol_respond( struct wfp_client_protocol * protocol = (struct wfp_client_protocol *) user_data; struct wfp_message * message = wfp_message_create(response); - if (NULL != message) - { - wfp_slist_append(&protocol->messages, &message->item); - lws_callback_on_writable(protocol->wsi); - } + wfp_slist_append(&protocol->messages, &message->item); + lws_callback_on_writable(protocol->wsi); } static void wfp_impl_client_protocol_process( @@ -223,18 +220,13 @@ static bool wfp_impl_client_protocol_send( json_t * request, void * user_data) { - bool result = false; struct wfp_client_protocol * protocol = user_data; struct wfp_message * message = wfp_message_create(request); - if (NULL != message) - { - wfp_slist_append(&protocol->messages, &message->item); - lws_callback_on_writable(protocol->wsi); - result = true; - } + wfp_slist_append(&protocol->messages, &message->item); + lws_callback_on_writable(protocol->wsi); - return result; + return true; } void wfp_impl_client_protocol_init( diff --git a/lib/webfuse_provider/impl/message.c b/lib/webfuse_provider/impl/message.c index 49ffc35..7943a5d 100644 --- a/lib/webfuse_provider/impl/message.c +++ b/lib/webfuse_provider/impl/message.c @@ -5,18 +5,14 @@ extern struct wfp_message * wfp_message_create(json_t const * value) { - struct wfp_message * message = NULL; size_t const length = json_dumpb(value, NULL, 0, JSON_COMPACT); - if (0 < length) - { - char * data = malloc(sizeof(struct wfp_message) + LWS_PRE + length); - message = (struct wfp_message *) data; - message->data = &data[sizeof(struct wfp_message) + LWS_PRE]; - message->length = length; + char * data = malloc(sizeof(struct wfp_message) + LWS_PRE + length); + struct wfp_message * message = (struct wfp_message *) data; + message->data = &data[sizeof(struct wfp_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/test/webfuse_provider/provider/test_client.cc b/test/webfuse_provider/provider/test_client.cc index 86388c8..4f9e41b 100644 --- a/test/webfuse_provider/provider/test_client.cc +++ b/test/webfuse_provider/provider/test_client.cc @@ -44,6 +44,37 @@ TEST(Client, Connect) wfp_client_config_dispose(config); } +TEST(Client, ConnectWithTls) +{ + MockProviderClient provider; + + std::promise connected; + EXPECT_CALL(provider, OnConnected()).Times(1) + .WillOnce(Invoke([&]() { connected.set_value(); })); + + std::promise disconnected; + EXPECT_CALL(provider, OnDisconnected()).Times(1) + .WillOnce(Invoke([&]() { disconnected.set_value(); })); + + wfp_client_config * 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"); + provider.AttachTo(config); + + { + WebfuseServer server(true); + Client client(config, server.GetUrl()); + + ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT)); + + client.Disconnect(); + ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT)); + } + + wfp_client_config_dispose(config); +} + TEST(Client, ConnectFailWithInvalidUrl) { MockProviderClient provider; @@ -138,8 +169,7 @@ TEST(Client, Lookup) wfp_client_config_dispose(config); } - -TEST(Client, ConnectWithTls) +TEST(Client, LookupFail) { MockProviderClient provider; @@ -151,21 +181,82 @@ TEST(Client, ConnectWithTls) EXPECT_CALL(provider, OnDisconnected()).Times(1) .WillOnce(Invoke([&]() { disconnected.set_value(); })); + EXPECT_CALL(provider, Lookup(1,StrEq("foo"),_)).Times(1) + .WillOnce(Invoke([](ino_t, char const *, struct stat * result) { + throw std::runtime_error("something went wrong"); + })); + wfp_client_config * 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"); provider.AttachTo(config); { - WebfuseServer server(true); + WebfuseServer server; Client client(config, server.GetUrl()); ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT)); + json_t * response = server.Lookup(1, "foo"); + ASSERT_TRUE(json_is_object(response)); + json_t * error = json_object_get(response, "error"); + + json_t * code = json_object_get(error, "code"); + ASSERT_NE(0, json_integer_value(code)); + + json_decref(response); + client.Disconnect(); ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT)); } wfp_client_config_dispose(config); -} \ No newline at end of file +} + +TEST(Client, Open) +{ + MockProviderClient provider; + + std::promise connected; + EXPECT_CALL(provider, OnConnected()).Times(1) + .WillOnce(Invoke([&]() { connected.set_value(); })); + + std::promise disconnected; + EXPECT_CALL(provider, OnDisconnected()).Times(1) + .WillOnce(Invoke([&]() { disconnected.set_value(); })); + + EXPECT_CALL(provider, Lookup(1,StrEq("foo"),_)).Times(1) + .WillOnce(Invoke([](ino_t, char const *, struct stat * result) { + result->st_ino = 42; + result->st_mode = S_IFREG | 0644; + })); + + wfp_client_config * config = wfp_client_config_create(); + provider.AttachTo(config); + + { + WebfuseServer server; + Client client(config, server.GetUrl()); + + ASSERT_EQ(std::future_status::ready, connected.get_future().wait_for(TIMEOUT)); + + json_t * response = server.Lookup(1, "foo"); + ASSERT_TRUE(json_is_object(response)); + json_t * result = json_object_get(response, "result"); + + json_t * inode = json_object_get(result, "inode"); + ASSERT_EQ(42, json_integer_value(inode)); + + json_t * mode = json_object_get(result, "mode"); + ASSERT_EQ(0644, json_integer_value(mode)); + + json_t * type = json_object_get(result, "type"); + ASSERT_STREQ("file", json_string_value(type)); + + json_decref(response); + + client.Disconnect(); + ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT)); + } + + wfp_client_config_dispose(config); +} + diff --git a/test/webfuse_provider/util/test_message.cc b/test/webfuse_provider/util/test_message.cc index 139c7bf..ae46b2f 100644 --- a/test/webfuse_provider/util/test_message.cc +++ b/test/webfuse_provider/util/test_message.cc @@ -14,9 +14,3 @@ TEST(wfp_message, create) wfp_message_dispose(message); json_decref(value); } - -TEST(wfp_message, fail_to_create) -{ - struct wfp_message * message = wfp_message_create(nullptr); - ASSERT_EQ(nullptr, message); -} \ No newline at end of file