From a63ae2e30b1cd4212fe66b9a521cf5751da507c1 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Fri, 26 Jun 2020 18:11:10 +0200 Subject: [PATCH] added unit tests --- test/webfuse_provider/provider/test_client.cc | 92 +++++++++++++++++++ .../test_util/webfuse_server.cc | 20 ++++ .../test_util/webfuse_server.hpp | 2 + 3 files changed, 114 insertions(+) diff --git a/test/webfuse_provider/provider/test_client.cc b/test/webfuse_provider/provider/test_client.cc index 5010bfb..32843c9 100644 --- a/test/webfuse_provider/provider/test_client.cc +++ b/test/webfuse_provider/provider/test_client.cc @@ -252,3 +252,95 @@ TEST(Client, Open) wfp_client_config_dispose(config); } +TEST(Client, Read) +{ + 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, Read(42, 5, 0, 1, _, _)).Times(1) + .WillOnce(Invoke([](ino_t, uint32_t, size_t, size_t , char * buffer, size_t * bytes_read) { + buffer[0] = 42; + *bytes_read = 1; + })); + + 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.Read(42, 5, 0, 1); + ASSERT_TRUE(json_is_object(response)); + json_t * result = json_object_get(response, "result"); + + json_t * format = json_object_get(result, "format"); + ASSERT_STREQ("base64", json_string_value(format)); + + json_t * count = json_object_get(result, "count"); + ASSERT_EQ(1, json_integer_value(count)); + + json_t * data = json_object_get(result, "data"); + ASSERT_STREQ("Kg==", json_string_value(data)); + + json_decref(response); + + client.Disconnect(); + ASSERT_EQ(std::future_status::ready, disconnected.get_future().wait_for(TIMEOUT)); + } + + wfp_client_config_dispose(config); +} + +TEST(Client, ReadDir) +{ + 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, ReadDir(42, _)).Times(1) + .WillOnce(Invoke([](ino_t, wfp_dirbuffer * buffer) { + wfp_dirbuffer_add(buffer, ".", 42); + wfp_dirbuffer_add(buffer, "..", 1); + wfp_dirbuffer_add(buffer, "foo.txt", 43); + })); + + 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.ReadDir(42); + ASSERT_TRUE(json_is_object(response)); + json_t * result = json_object_get(response, "result"); + + ASSERT_TRUE(json_is_array(result)); + ASSERT_EQ(3, json_array_size(result)); + + 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/test_util/webfuse_server.cc b/test/webfuse_provider/test_util/webfuse_server.cc index f05b658..b57f247 100644 --- a/test/webfuse_provider/test_util/webfuse_server.cc +++ b/test/webfuse_provider/test_util/webfuse_server.cc @@ -338,5 +338,25 @@ json_t * WebfuseServer::Open(int inode, int flags) return d->Invoke("open", params); } +json_t * WebfuseServer::Read(int inode, int handle, int offset, int length) +{ + json_t * params = json_array(); + json_array_append_new(params, json_string(d->GetFilesystem().c_str())); + 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)); + + return d->Invoke("read", params); +} + +json_t * WebfuseServer::ReadDir(int inode) +{ + json_t * params = json_array(); + json_array_append_new(params, json_string(d->GetFilesystem().c_str())); + json_array_append_new(params, json_integer(inode)); + + return d->Invoke("readdir", params); +} } \ No newline at end of file diff --git a/test/webfuse_provider/test_util/webfuse_server.hpp b/test/webfuse_provider/test_util/webfuse_server.hpp index 24db92e..ec16dc4 100644 --- a/test/webfuse_provider/test_util/webfuse_server.hpp +++ b/test/webfuse_provider/test_util/webfuse_server.hpp @@ -19,6 +19,8 @@ public: json_t * Invoke(std::string const & method, std::string const & params); json_t * Lookup(int parent, std::string const & name); json_t * Open(int inode, int flags); + json_t * Read(int inode, int handle, int offset, int length); + json_t * ReadDir(int inode); private: class Private; Private * d;