From 94a5e7934c7c3a6529165f7fa888a44bee4b3148 Mon Sep 17 00:00:00 2001 From: hyunho Date: Wed, 9 Sep 2020 13:16:04 +0900 Subject: [PATCH] Implement gtest for every public API Change-Id: I82c589b8ddaaa97ffb57c36c63e69c70e702a685 Signed-off-by: hyunho --- mock/gio_mock.cc | 20 ++ mock/gio_mock.h | 10 + src/message_port_remote.c | 3 +- test/unit_tests/test_message_port.cc | 411 +++++++++++++++++++++++++++++++++++ 4 files changed, 443 insertions(+), 1 deletion(-) diff --git a/mock/gio_mock.cc b/mock/gio_mock.cc index 4f727e5..0b88132 100644 --- a/mock/gio_mock.cc +++ b/mock/gio_mock.cc @@ -101,3 +101,23 @@ extern "C" GDBusMessage* g_dbus_connection_send_message_with_reply_finish( return MOCK_HOOK_P3(GioMock, g_dbus_connection_send_message_with_reply_finish, arg0, arg1, arg2); } + +extern "C" gboolean g_dbus_connection_send_message(GDBusConnection* arg0, + GDBusMessage* arg1, GDBusSendMessageFlags arg2, + volatile guint32* arg3, GError** arg4) { + return MOCK_HOOK_P5(GioMock, g_dbus_connection_send_message, + arg0, arg1, arg2, arg3, arg4); +} + +extern "C" void g_dbus_message_set_unix_fd_list(GDBusMessage* arg0, + GUnixFDList* arg1) { + return MOCK_HOOK_P2(GioMock, g_dbus_message_set_unix_fd_list, arg0, arg1); +} + +extern "C" guint g_bus_watch_name_on_connection(GDBusConnection* arg0, + const gchar* arg1, GBusNameWatcherFlags arg2, + GBusNameAppearedCallback arg3, GBusNameVanishedCallback arg4, gpointer arg5, + GDestroyNotify arg6) { + return MOCK_HOOK_P7(GioMock, g_bus_watch_name_on_connection, arg0, arg1, arg2, + arg3, arg4, arg5, arg6); +} diff --git a/mock/gio_mock.h b/mock/gio_mock.h index 0ff6e33..3867d5c 100644 --- a/mock/gio_mock.h +++ b/mock/gio_mock.h @@ -64,6 +64,16 @@ class GioMock : public virtual ModuleMock { MOCK_METHOD2(g_dbus_method_invocation_return_value, GDBusNodeInfo*(GDBusMethodInvocation*, GVariant*)); + + MOCK_METHOD5(g_dbus_connection_send_message, + gboolean(GDBusConnection*, GDBusMessage*, GDBusSendMessageFlags, + volatile guint32*, GError**)); + MOCK_METHOD2(g_dbus_message_set_unix_fd_list, + void(GDBusMessage*, GUnixFDList*)); + MOCK_METHOD7(g_bus_watch_name_on_connection, + guint(GDBusConnection*, const gchar*, GBusNameWatcherFlags, + GBusNameAppearedCallback, GBusNameVanishedCallback, gpointer, + GDestroyNotify)); }; #endif // UNIT_TESTS_MOCK_GIO_MOCK_H_ diff --git a/src/message_port_remote.c b/src/message_port_remote.c index 4bb0b13..17d61c6 100644 --- a/src/message_port_remote.c +++ b/src/message_port_remote.c @@ -308,7 +308,8 @@ static bool __validate_callback_info(callback_key_info_s *key_info) cb_list = g_list_find(cb_list, key_info->callback_info); if (cb_list == NULL) { - _LOGI("local_info : %d is already released", key_info->local_id); + _LOGI("local_info : %d is already released from list", + key_info->local_id); return false; } diff --git a/test/unit_tests/test_message_port.cc b/test/unit_tests/test_message_port.cc index b3281e0..46f76be 100644 --- a/test/unit_tests/test_message_port.cc +++ b/test/unit_tests/test_message_port.cc @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -60,18 +61,69 @@ extern "C" int __dlog_print( return 0; } +extern "C" void g_object_unref(gpointer _object) { +} + +extern "C" gboolean g_source_remove(guint tag) { + return true; +} + +extern "C" int aul_request_message_port_socket_pair(int* fd) { + fd[0] = 77; + fd[1] = 777; + return 0; +} + +extern "C" int pthread_mutex_lock(pthread_mutex_t* mutex) { + return 0; +} + +extern "C" int pthread_mutex_unlock(pthread_mutex_t* mutex) { + return 0; +} + extern "C" int aul_app_get_appid_bypid(int pid, char* appid, int len) { char test[5] = "test"; snprintf(appid, sizeof(test), "%s", test); return 0; } +extern "C" gint g_unix_fd_list_append(GUnixFDList* list, + gint fd, GError** error) { + return 0; +} + +extern "C" int pkgmgrinfo_appinfo_get_usr_appinfo(const char* appid, uid_t uid, + pkgmgrinfo_appinfo_h* handle) { + return 0; +} + +extern "C" int pkgmgrinfo_appinfo_is_preload( + pkgmgrinfo_appinfo_h handle, bool* preload) { + *preload = false; + return 0; +} + +extern "C" int pkgmgrinfo_appinfo_destroy_appinfo(pkgmgrinfo_appinfo_h handle) { + return 0; +} + +extern "C" int pkgmgrinfo_pkginfo_compare_usr_app_cert_info( + const char* lhs_app_id, const char* rhs_app_id, uid_t uid, + pkgmgrinfo_cert_compare_result_type_e* compare_result) { + *compare_result = PMINFO_CERT_COMPARE_MATCH; + return 0; +} + +GVariant* __call_sync_reply; extern "C" GVariant* g_dbus_connection_call_sync( GDBusConnection* connection, const gchar* bus_name, const gchar* object_path, const gchar* interface_name, const gchar* method_name, GVariant* parameters, const GVariantType* reply_type, GDBusCallFlags flags, gint timeout_msec, GCancellable* cancellable, GError** error) { + if (__call_sync_reply != nullptr) + return __call_sync_reply; return g_variant_new("(u)", 1); } @@ -101,12 +153,42 @@ static void __trusted_message_cb(int trusted_local_port_id, bool trusted_remote_port, bundle* message, void* user_data) { } +struct _GDBusMessage { + int test; +}; +typedef struct _GDBusMessage GDBusMessage; +GDBusMessage __message; + +typedef struct _GUnixFDList GUnixFDList; +struct _GUnixFDList { + int test; +}; +GUnixFDList __fd_list; +extern "C" GUnixFDList* g_dbus_message_get_unix_fd_list( + GDBusMessage* message) { + return &__fd_list; +} + typedef struct _GDBusConnection GDBusConnection; struct _GDBusConnection { int test; }; GDBusConnection __gdbus_conn; +int __port_id; +static guint __connection_register_object(GDBusConnection* connection, + const gchar* object_path, GDBusInterfaceInfo* interface_info, + const GDBusInterfaceVTable* vtable, gpointer user_data, + GDestroyNotify user_data_free_func, GError** error) { + tizen_base::Bundle msg; + vtable->method_call(&__gdbus_conn, "sender", "obj_path", "iface_name", + "send_message", + g_variant_new("(ssbbssbus)", "test", "PORT", 0, 0, "test", + "PORT", 0, msg.ToRaw().second, msg.ToRaw().first.get()), + nullptr, nullptr); + return __port_id; +} + TEST_F(MessagePortTest, message_port_register_local_port) { EXPECT_CALL(GetMock(), g_bus_get_sync(_, _, _)). WillOnce(Return(&__gdbus_conn)); @@ -126,6 +208,203 @@ TEST_F(MessagePortTest, message_port_register_local_port) { int port = message_port_register_local_port("PORT", __message_cb, nullptr); EXPECT_EQ(port, 1); + port = message_port_register_local_port("PORT", __message_cb, nullptr); + EXPECT_EQ(port, 1); +} + +extern "C" gint* g_unix_fd_list_steal_fds(GUnixFDList* list, gint* length) { + gint* fds = (gint*)calloc(2, sizeof(gint)); + fds[0] = 1; + fds[1] = 2; + return fds; +} + +GIOChannel __gio_channel; +extern "C" GIOChannel* g_io_channel_unix_new(gint fd) { + return &__gio_channel; +} + +GIOFunc __io_func; +gpointer __io_data; +GDestroyNotify __io_notify; +extern "C" guint g_io_add_watch_full(GIOChannel* channel, gint priority, + GIOCondition condition, GIOFunc func, gpointer user_data, + GDestroyNotify notify) { + __io_func = func; + __io_data = user_data; + __io_notify = notify; + return 1; +} + +extern "C" GIOStatus g_io_channel_shutdown(GIOChannel* channel, + gboolean flush, GError** err) { + return G_IO_STATUS_NORMAL; +} + +extern "C" void g_io_channel_unref(GIOChannel* channel) { +} + +extern "C" gint g_io_channel_unix_get_fd(GIOChannel* channel) { + return 1; +} + +extern "C" int read(int desc, void* buf, size_t count) { + if (count == 4 && buf != NULL) { + int val = 4; + memcpy(buf, &val, count); + } + return count; +} + +extern "C" ssize_t write(int fd, const void* buf, size_t count) { + return count; +} + +guint __g_bus_watch_name_on_connection(GDBusConnection* connection, + const gchar* name, GBusNameWatcherFlags flags, + GBusNameAppearedCallback name_appeared_handler, + GBusNameVanishedCallback name_vanished_handler, + gpointer user_data, + GDestroyNotify user_data_free_func) { + int* id = NULL; + name_appeared_handler(nullptr, "test", "test", (gpointer)id); + name_vanished_handler(nullptr, "test", (gpointer)id); + return 10; +} + +TEST_F(MessagePortTest, message_port_register_local_port2) { + GDBusNodeInfo* info = reinterpret_cast( + malloc(sizeof(GDBusNodeInfo))); + info->ref_count = 10; + info->path = NULL; + info->interfaces = reinterpret_cast( + malloc(sizeof(GDBusInterfaceInfo*))); + info->nodes = NULL; + info->annotations = NULL; + EXPECT_CALL(GetMock(), + g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info)); + EXPECT_CALL(GetMock(), + g_dbus_connection_register_object(_, _, _, _, _, _, _)). + WillOnce(Invoke(__connection_register_object)); + EXPECT_CALL(GetMock(), + g_bus_watch_name_on_connection(_, _, _, _, _, _, _)). + WillOnce(Invoke(__g_bus_watch_name_on_connection)); + EXPECT_CALL(GetMock(), + g_dbus_message_new_method_call(_, _, _, _)). + WillOnce(Return(&__message)); + EXPECT_CALL(GetMock(), + g_dbus_connection_send_message_with_reply_sync(_, _, _, _, _, _, _)). + WillOnce(Return(&__message)); + + __port_id = 10; + int port = message_port_register_local_port("PORT2", __message_cb, nullptr); + EXPECT_EQ(port, __port_id); + __io_func(nullptr, G_IO_IN, __io_data); +} + +TEST_F(MessagePortTest, message_port_register_local_port3) { + GDBusNodeInfo* info = reinterpret_cast( + malloc(sizeof(GDBusNodeInfo))); + info->ref_count = 10; + info->path = NULL; + info->interfaces = reinterpret_cast( + malloc(sizeof(GDBusInterfaceInfo*))); + info->nodes = NULL; + info->annotations = NULL; + EXPECT_CALL(GetMock(), + g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info)); + EXPECT_CALL(GetMock(), + g_dbus_connection_register_object(_, _, _, _, _, _, _)). + WillOnce(Invoke(__connection_register_object)); + + __port_id = 20; + int port = message_port_register_local_port("PORT3", __message_cb, nullptr); + EXPECT_EQ(port, __port_id); + __io_notify(__io_data); +} + +TEST_F(MessagePortTest, message_port_register_local_port_n) { + int port = message_port_register_local_port(nullptr, __message_cb, nullptr); + EXPECT_EQ(port, MESSAGE_PORT_ERROR_INVALID_PARAMETER); +} + +TEST_F(MessagePortTest, message_port_send_message) { + EXPECT_CALL(GetMock(), + g_dbus_message_new_method_call(_, _, _, _)). + WillOnce(Return(&__message)); + + tizen_base::Bundle message; + int ret = message_port_send_message("test", "PORT", message.GetHandle()); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE); +} + +TEST_F(MessagePortTest, message_port_send_message_n) { + int ret = message_port_send_message(nullptr, "PORT", nullptr); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER); +} + +TEST_F(MessagePortTest, message_port_send_message_with_local_port) { + EXPECT_CALL(GetMock(), + g_dbus_message_new_method_call(_, _, _, _)). + WillOnce(Return(&__message)); + + tizen_base::Bundle message; + int ret = message_port_send_message_with_local_port( + "test", "PORT", message.GetHandle(), 1); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE); +} + +TEST_F(MessagePortTest, message_port_send_message_with_local_port_n1) { + int ret = message_port_send_message_with_local_port( + nullptr, "PORT", nullptr, 1); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER); +} + +TEST_F(MessagePortTest, message_port_send_message_with_local_port_n2) { + tizen_base::Bundle message; + int ret = message_port_send_message_with_local_port( + "test", "PORT", message.GetHandle(), -1); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER); +} + +TEST_F(MessagePortTest, message_port_send_message_with_local_port2) { + if (__call_sync_reply != nullptr) + g_variant_unref(__call_sync_reply); + __call_sync_reply = g_variant_new("(b)", true); + + EXPECT_CALL(GetMock(), + g_dbus_message_new_method_call(_, _, _, _)). + WillOnce(Return(&__message)); + tizen_base::Bundle message; + int ret = message_port_send_message_with_local_port( + "remote_app", "PORT", message.GetHandle(), 1); + g_variant_unref(__call_sync_reply); + __call_sync_reply = nullptr; + + ret = message_port_send_message_with_local_port( + "remote_app", "PORT", message.GetHandle(), 1); + g_variant_unref(__call_sync_reply); + __call_sync_reply = nullptr; + + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE); + __io_func(nullptr, G_IO_IN, __io_data); +} + +TEST_F(MessagePortTest, message_port_send_message_with_local_port3) { + if (__call_sync_reply != nullptr) + g_variant_unref(__call_sync_reply); + __call_sync_reply = g_variant_new("(b)", true); + + EXPECT_CALL(GetMock(), + g_dbus_message_new_method_call(_, _, _, _)). + WillOnce(Return(&__message)); + tizen_base::Bundle message; + int ret = message_port_send_message_with_local_port( + "remote_app", "PORT", message.GetHandle(), 1); + g_variant_unref(__call_sync_reply); + __call_sync_reply = nullptr; + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE); + __io_notify(__io_data); } TEST_F(MessagePortTest, message_port_register_trusted_local_port) { @@ -148,12 +427,144 @@ TEST_F(MessagePortTest, message_port_register_trusted_local_port) { EXPECT_EQ(port, 2); } +TEST_F(MessagePortTest, message_port_register_trusted_local_port_n) { + int port = message_port_register_trusted_local_port( + nullptr, __message_cb, nullptr); + EXPECT_EQ(port, MESSAGE_PORT_ERROR_INVALID_PARAMETER); +} + +TEST_F(MessagePortTest, message_port_send_trusted_message_with_local_port) { + EXPECT_CALL(GetMock(), + g_dbus_message_new_method_call(_, _, _, _)). + WillOnce(Return(&__message)); + + tizen_base::Bundle message; + int ret = message_port_send_trusted_message_with_local_port( + "test", "PORT", message.GetHandle(), 1); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE); +} + +TEST_F(MessagePortTest, message_port_send_trusted_message_with_local_port_n1) { + int ret = message_port_send_trusted_message_with_local_port( + nullptr, "PORT", nullptr, 1); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER); +} + +TEST_F(MessagePortTest, message_port_send_trusted_message_with_local_port_n2) { + tizen_base::Bundle message; + int ret = message_port_send_trusted_message_with_local_port( + "test", "PORT", message.GetHandle(), -1); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER); +} + +TEST_F(MessagePortTest, message_port_send_trusted_message) { + EXPECT_CALL(GetMock(), + g_dbus_message_new_method_call(_, _, _, _)). + WillOnce(Return(&__message)); + + tizen_base::Bundle message; + int ret = message_port_send_trusted_message( + "test", "PORT", message.GetHandle()); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE); +} + +TEST_F(MessagePortTest, message_port_send_trusted_message_n) { + int ret = message_port_send_trusted_message(nullptr, "PORT", nullptr); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER); +} + TEST_F(MessagePortTest, message_port_unregister_local_port) { int ret = message_port_unregister_local_port(1); EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE); } +TEST_F(MessagePortTest, message_port_unregister_local_port_n) { + int ret = message_port_unregister_local_port(-1); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER); +} + TEST_F(MessagePortTest, message_port_unregister_trusted_local_port) { int ret = message_port_unregister_trusted_local_port(2); EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE); } + +TEST_F(MessagePortTest, message_port_unregister_trusted_local_port_n) { + int ret = message_port_unregister_trusted_local_port(-1); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER); +} + +TEST_F(MessagePortTest, message_port_check_remote_port) { + bool exist; + int ret = message_port_check_remote_port("test", "test", &exist); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE); +} + +TEST_F(MessagePortTest, message_port_check_remote_port_n) { + bool exist; + int ret = message_port_check_remote_port(nullptr, "test", &exist); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER); +} + +TEST_F(MessagePortTest, message_port_check_trusted_remote_port) { + if (__call_sync_reply != nullptr) + g_variant_unref(__call_sync_reply); + __call_sync_reply = g_variant_new("(b)", true); + + tizen_base::Bundle message; + bool exist; + int ret = message_port_check_trusted_remote_port("test2", "PORT", &exist); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE); +} + +TEST_F(MessagePortTest, message_port_check_trusted_remote_port_n) { + bool exist; + int ret = message_port_check_trusted_remote_port(nullptr, "PORT", &exist); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER); +} + +static void __registration_event_cb(const char* remote_app_id, + const char* remote_port, bool trusted_remote_port, void* user_data) { +} + +TEST_F(MessagePortTest, message_port_add_registered_cb) { + EXPECT_CALL(GetMock(), + g_bus_watch_name_on_connection(_, _, _, _, _, _, _)).WillOnce( + Invoke(__g_bus_watch_name_on_connection)); + int watcher_id; + int ret = message_port_add_registered_cb("test", "PORT", false, + __registration_event_cb, nullptr, &watcher_id); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE); +} + +TEST_F(MessagePortTest, message_port_add_registered_cb_n) { + int watcher_id; + int ret = message_port_add_registered_cb(nullptr, "PORT", false, + __registration_event_cb, nullptr, &watcher_id); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER); +} + +TEST_F(MessagePortTest, message_port_add_unregistered_cb) { + EXPECT_CALL(GetMock(), + g_bus_watch_name_on_connection(_, _, _, _, _, _, _)).WillOnce(Return(2)); + int watcher_id; + int ret = message_port_add_unregistered_cb("test", "PORT", false, + __registration_event_cb, nullptr, &watcher_id); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE); +} + +TEST_F(MessagePortTest, message_port_add_unregistered_cb_n) { + int watcher_id; + int ret = message_port_add_unregistered_cb(nullptr, "PORT", false, + __registration_event_cb, nullptr, &watcher_id); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER); +} + +TEST_F(MessagePortTest, message_port_remove_registration_event_cb) { + int ret = message_port_remove_registration_event_cb(10); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE); +} + +TEST_F(MessagePortTest, message_port_remove_registration_event_cb_n) { + int ret = message_port_remove_registration_event_cb(-1); + EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER); +} -- 2.7.4