2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <gtest/gtest.h>
22 #include <gmock/gmock.h>
23 #include <bundle_cpp.h>
24 #include <bundle_internal.h>
25 #include <pkgmgr-info.h>
30 #include "message_port.h"
31 #include "app_common_mock.h"
34 #include "test_fixture.h"
37 using ::testing::DoAll;
38 using ::testing::Return;
39 using ::testing::SetArgPointee;
40 using ::testing::Invoke;
53 extern "C" int __dlog_print(
54 log_id_t log_id, int prio, const char* tag, const char* fmt, ...) {
64 extern "C" void g_object_unref(gpointer _object) {
67 extern "C" gboolean g_source_remove(guint tag) {
71 extern "C" int aul_request_message_port_socket_pair(int* fd) {
77 extern "C" int pthread_mutex_lock(pthread_mutex_t* mutex) {
81 extern "C" int pthread_mutex_unlock(pthread_mutex_t* mutex) {
85 extern "C" int aul_app_get_appid_bypid(int pid, char* appid, int len) {
86 char test[5] = "test";
87 snprintf(appid, sizeof(test), "%s", test);
91 extern "C" gint g_unix_fd_list_append(GUnixFDList* list,
92 gint fd, GError** error) {
96 extern "C" int pkgmgrinfo_appinfo_get_usr_appinfo(const char* appid, uid_t uid,
97 pkgmgrinfo_appinfo_h* handle) {
101 extern "C" int pkgmgrinfo_appinfo_is_preload(
102 pkgmgrinfo_appinfo_h handle, bool* preload) {
107 extern "C" int pkgmgrinfo_appinfo_destroy_appinfo(pkgmgrinfo_appinfo_h handle) {
111 extern "C" int pkgmgrinfo_pkginfo_compare_usr_app_cert_info(
112 const char* lhs_app_id, const char* rhs_app_id, uid_t uid,
113 pkgmgrinfo_cert_compare_result_type_e* compare_result) {
114 *compare_result = PMINFO_CERT_COMPARE_MATCH;
118 GVariant* __call_sync_reply;
119 extern "C" GVariant* g_dbus_connection_call_sync(
120 GDBusConnection* connection, const gchar* bus_name,
121 const gchar* object_path, const gchar* interface_name,
122 const gchar* method_name, GVariant* parameters,
123 const GVariantType* reply_type, GDBusCallFlags flags,
124 gint timeout_msec, GCancellable* cancellable, GError** error) {
125 if (__call_sync_reply != nullptr)
126 return __call_sync_reply;
127 return g_variant_new("(u)", 1);
130 class Mocks : public ::testing::NiceMock<AppCommonMock>,
131 public ::testing::NiceMock<GioMock>,
132 public ::testing::NiceMock<AulMock> {};
134 class MessagePortTest : public TestFixture {
136 MessagePortTest() : TestFixture(std::make_unique<Mocks>()) {}
137 virtual ~MessagePortTest() {}
139 virtual void SetUp() {
142 virtual void TearDown() {
146 static void __message_cb(int local_port_id, const char* remote_app_id,
147 const char* remote_port, bool trusted_remote_port,
148 bundle* message, void* user_data) {
151 static void __trusted_message_cb(int trusted_local_port_id,
152 const char* remote_app_id, const char* remote_port,
153 bool trusted_remote_port, bundle* message, void* user_data) {
156 struct _GDBusMessage {
159 typedef struct _GDBusMessage GDBusMessage;
160 GDBusMessage __message;
162 typedef struct _GUnixFDList GUnixFDList;
163 struct _GUnixFDList {
166 GUnixFDList __fd_list;
167 extern "C" GUnixFDList* g_dbus_message_get_unix_fd_list(
168 GDBusMessage* message) {
172 typedef struct _GDBusConnection GDBusConnection;
173 struct _GDBusConnection {
177 GDBusConnection __gdbus_conn;
179 static guint __connection_register_object(GDBusConnection* connection,
180 const gchar* object_path, GDBusInterfaceInfo* interface_info,
181 const GDBusInterfaceVTable* vtable, gpointer user_data,
182 GDestroyNotify user_data_free_func, GError** error) {
183 tizen_base::Bundle msg;
184 vtable->method_call(&__gdbus_conn, "sender", "obj_path", "iface_name",
186 g_variant_new("(ssbbssbus)", "test", "PORT", 0, 0, "test",
187 "PORT", 0, msg.ToRaw().second, msg.ToRaw().first.get()),
192 TEST_F(MessagePortTest, message_port_register_local_port) {
193 EXPECT_CALL(GetMock<GioMock>(), g_bus_get_sync(_, _, _)).
194 WillOnce(Return(&__gdbus_conn));
195 GDBusNodeInfo* info = reinterpret_cast<GDBusNodeInfo*>(
196 malloc(sizeof(GDBusNodeInfo)));
197 info->ref_count = 10;
199 info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
200 malloc(sizeof(GDBusInterfaceInfo*)));
202 info->annotations = NULL;
203 EXPECT_CALL(GetMock<GioMock>(),
204 g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info));
205 EXPECT_CALL(GetMock<GioMock>(),
206 g_dbus_connection_register_object(_, _, _, _, _, _, _)).
209 int port = message_port_register_local_port("PORT", __message_cb, nullptr);
211 port = message_port_register_local_port("PORT", __message_cb, nullptr);
215 extern "C" gint* g_unix_fd_list_steal_fds(GUnixFDList* list, gint* length) {
216 gint* fds = (gint*)calloc(2, sizeof(gint));
222 GIOChannel __gio_channel;
223 extern "C" GIOChannel* g_io_channel_unix_new(gint fd) {
224 return &__gio_channel;
229 GDestroyNotify __io_notify;
230 extern "C" guint g_io_add_watch_full(GIOChannel* channel, gint priority,
231 GIOCondition condition, GIOFunc func, gpointer user_data,
232 GDestroyNotify notify) {
234 __io_data = user_data;
235 __io_notify = notify;
239 extern "C" GIOStatus g_io_channel_shutdown(GIOChannel* channel,
240 gboolean flush, GError** err) {
241 return G_IO_STATUS_NORMAL;
244 extern "C" void g_io_channel_unref(GIOChannel* channel) {
247 extern "C" gint g_io_channel_unix_get_fd(GIOChannel* channel) {
251 extern "C" int read(int desc, void* buf, size_t count) {
252 if (count == 4 && buf != NULL) {
254 memcpy(buf, &val, count);
259 extern "C" ssize_t write(int fd, const void* buf, size_t count) {
263 guint __g_bus_watch_name_on_connection(GDBusConnection* connection,
264 const gchar* name, GBusNameWatcherFlags flags,
265 GBusNameAppearedCallback name_appeared_handler,
266 GBusNameVanishedCallback name_vanished_handler,
268 GDestroyNotify user_data_free_func) {
270 name_appeared_handler(nullptr, "test", "test", (gpointer)id);
271 name_vanished_handler(nullptr, "test", (gpointer)id);
275 TEST_F(MessagePortTest, message_port_register_local_port2) {
276 GDBusNodeInfo* info = reinterpret_cast<GDBusNodeInfo*>(
277 malloc(sizeof(GDBusNodeInfo)));
278 info->ref_count = 10;
280 info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
281 malloc(sizeof(GDBusInterfaceInfo*)));
283 info->annotations = NULL;
284 EXPECT_CALL(GetMock<GioMock>(),
285 g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info));
286 EXPECT_CALL(GetMock<GioMock>(),
287 g_dbus_connection_register_object(_, _, _, _, _, _, _)).
288 WillOnce(Invoke(__connection_register_object));
289 EXPECT_CALL(GetMock<GioMock>(),
290 g_bus_watch_name_on_connection(_, _, _, _, _, _, _)).
291 WillOnce(Invoke(__g_bus_watch_name_on_connection));
292 EXPECT_CALL(GetMock<GioMock>(),
293 g_dbus_message_new_method_call(_, _, _, _)).
294 WillOnce(Return(&__message));
295 EXPECT_CALL(GetMock<GioMock>(),
296 g_dbus_connection_send_message_with_reply_sync(_, _, _, _, _, _, _)).
297 WillOnce(Return(&__message));
300 int port = message_port_register_local_port("PORT2", __message_cb, nullptr);
301 EXPECT_EQ(port, __port_id);
302 __io_func(nullptr, G_IO_IN, __io_data);
305 TEST_F(MessagePortTest, message_port_register_local_port3) {
306 GDBusNodeInfo* info = reinterpret_cast<GDBusNodeInfo*>(
307 malloc(sizeof(GDBusNodeInfo)));
308 info->ref_count = 10;
310 info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
311 malloc(sizeof(GDBusInterfaceInfo*)));
313 info->annotations = NULL;
314 EXPECT_CALL(GetMock<GioMock>(),
315 g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info));
316 EXPECT_CALL(GetMock<GioMock>(),
317 g_dbus_connection_register_object(_, _, _, _, _, _, _)).
318 WillOnce(Invoke(__connection_register_object));
321 int port = message_port_register_local_port("PORT3", __message_cb, nullptr);
322 EXPECT_EQ(port, __port_id);
323 __io_notify(__io_data);
326 TEST_F(MessagePortTest, message_port_register_local_port_n) {
327 int port = message_port_register_local_port(nullptr, __message_cb, nullptr);
328 EXPECT_EQ(port, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
331 TEST_F(MessagePortTest, message_port_send_message) {
332 EXPECT_CALL(GetMock<GioMock>(),
333 g_dbus_message_new_method_call(_, _, _, _)).
334 WillOnce(Return(&__message));
336 tizen_base::Bundle message;
337 int ret = message_port_send_message("test", "PORT", message.GetHandle());
338 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
341 TEST_F(MessagePortTest, message_port_send_message_n) {
342 int ret = message_port_send_message(nullptr, "PORT", nullptr);
343 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
346 TEST_F(MessagePortTest, message_port_send_message_with_local_port) {
347 EXPECT_CALL(GetMock<GioMock>(),
348 g_dbus_message_new_method_call(_, _, _, _)).
349 WillOnce(Return(&__message));
351 tizen_base::Bundle message;
352 int ret = message_port_send_message_with_local_port(
353 "test", "PORT", message.GetHandle(), 1);
354 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
357 TEST_F(MessagePortTest, message_port_send_message_with_local_port_n1) {
358 int ret = message_port_send_message_with_local_port(
359 nullptr, "PORT", nullptr, 1);
360 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
363 TEST_F(MessagePortTest, message_port_send_message_with_local_port_n2) {
364 tizen_base::Bundle message;
365 int ret = message_port_send_message_with_local_port(
366 "test", "PORT", message.GetHandle(), -1);
367 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
370 TEST_F(MessagePortTest, message_port_send_message_with_local_port2) {
371 if (__call_sync_reply != nullptr)
372 g_variant_unref(__call_sync_reply);
373 __call_sync_reply = g_variant_new("(b)", true);
375 EXPECT_CALL(GetMock<GioMock>(),
376 g_dbus_message_new_method_call(_, _, _, _)).
377 WillOnce(Return(&__message));
378 tizen_base::Bundle message;
379 int ret = message_port_send_message_with_local_port(
380 "remote_app", "PORT", message.GetHandle(), 1);
381 g_variant_unref(__call_sync_reply);
382 __call_sync_reply = nullptr;
384 ret = message_port_send_message_with_local_port(
385 "remote_app", "PORT", message.GetHandle(), 1);
386 g_variant_unref(__call_sync_reply);
387 __call_sync_reply = nullptr;
389 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
390 __io_func(nullptr, G_IO_IN, __io_data);
393 TEST_F(MessagePortTest, message_port_send_message_with_local_port3) {
394 if (__call_sync_reply != nullptr)
395 g_variant_unref(__call_sync_reply);
396 __call_sync_reply = g_variant_new("(b)", true);
398 EXPECT_CALL(GetMock<GioMock>(),
399 g_dbus_message_new_method_call(_, _, _, _)).
400 WillOnce(Return(&__message));
401 tizen_base::Bundle message;
402 int ret = message_port_send_message_with_local_port(
403 "remote_app", "PORT", message.GetHandle(), 1);
404 g_variant_unref(__call_sync_reply);
405 __call_sync_reply = nullptr;
406 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
407 __io_notify(__io_data);
410 TEST_F(MessagePortTest, message_port_register_trusted_local_port) {
411 GDBusNodeInfo* info = reinterpret_cast<GDBusNodeInfo*>(
412 malloc(sizeof(GDBusNodeInfo)));
413 info->ref_count = 10;
415 info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
416 malloc(sizeof(GDBusInterfaceInfo*)));
418 info->annotations = NULL;
419 EXPECT_CALL(GetMock<GioMock>(),
420 g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info));
421 EXPECT_CALL(GetMock<GioMock>(),
422 g_dbus_connection_register_object(_, _, _, _, _, _, _)).
425 int port = message_port_register_trusted_local_port("PORT",
426 __trusted_message_cb, nullptr);
430 TEST_F(MessagePortTest, message_port_register_trusted_local_port_n) {
431 int port = message_port_register_trusted_local_port(
432 nullptr, __message_cb, nullptr);
433 EXPECT_EQ(port, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
436 TEST_F(MessagePortTest, message_port_send_trusted_message_with_local_port) {
437 EXPECT_CALL(GetMock<GioMock>(),
438 g_dbus_message_new_method_call(_, _, _, _)).
439 WillOnce(Return(&__message));
441 tizen_base::Bundle message;
442 int ret = message_port_send_trusted_message_with_local_port(
443 "test", "PORT", message.GetHandle(), 1);
444 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
447 TEST_F(MessagePortTest, message_port_send_trusted_message_with_local_port_n1) {
448 int ret = message_port_send_trusted_message_with_local_port(
449 nullptr, "PORT", nullptr, 1);
450 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
453 TEST_F(MessagePortTest, message_port_send_trusted_message_with_local_port_n2) {
454 tizen_base::Bundle message;
455 int ret = message_port_send_trusted_message_with_local_port(
456 "test", "PORT", message.GetHandle(), -1);
457 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
460 TEST_F(MessagePortTest, message_port_send_trusted_message) {
461 EXPECT_CALL(GetMock<GioMock>(),
462 g_dbus_message_new_method_call(_, _, _, _)).
463 WillOnce(Return(&__message));
465 tizen_base::Bundle message;
466 int ret = message_port_send_trusted_message(
467 "test", "PORT", message.GetHandle());
468 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
471 TEST_F(MessagePortTest, message_port_send_trusted_message_n) {
472 int ret = message_port_send_trusted_message(nullptr, "PORT", nullptr);
473 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
476 TEST_F(MessagePortTest, message_port_unregister_local_port) {
477 int ret = message_port_unregister_local_port(1);
478 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
481 TEST_F(MessagePortTest, message_port_unregister_local_port_n) {
482 int ret = message_port_unregister_local_port(-1);
483 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
486 TEST_F(MessagePortTest, message_port_unregister_trusted_local_port) {
487 int ret = message_port_unregister_trusted_local_port(2);
488 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
491 TEST_F(MessagePortTest, message_port_unregister_trusted_local_port_n) {
492 int ret = message_port_unregister_trusted_local_port(-1);
493 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
496 TEST_F(MessagePortTest, message_port_check_remote_port) {
498 int ret = message_port_check_remote_port("test", "test", &exist);
499 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
502 TEST_F(MessagePortTest, message_port_check_remote_port_n) {
504 int ret = message_port_check_remote_port(nullptr, "test", &exist);
505 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
508 TEST_F(MessagePortTest, message_port_check_trusted_remote_port) {
509 if (__call_sync_reply != nullptr)
510 g_variant_unref(__call_sync_reply);
511 __call_sync_reply = g_variant_new("(b)", true);
513 tizen_base::Bundle message;
515 int ret = message_port_check_trusted_remote_port("test2", "PORT", &exist);
516 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
519 TEST_F(MessagePortTest, message_port_check_trusted_remote_port_n) {
521 int ret = message_port_check_trusted_remote_port(nullptr, "PORT", &exist);
522 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
525 static void __registration_event_cb(const char* remote_app_id,
526 const char* remote_port, bool trusted_remote_port, void* user_data) {
529 TEST_F(MessagePortTest, message_port_add_registered_cb) {
530 EXPECT_CALL(GetMock<GioMock>(),
531 g_bus_watch_name_on_connection(_, _, _, _, _, _, _)).WillOnce(
532 Invoke(__g_bus_watch_name_on_connection));
534 int ret = message_port_add_registered_cb("test", "PORT", false,
535 __registration_event_cb, nullptr, &watcher_id);
536 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
539 TEST_F(MessagePortTest, message_port_add_registered_cb_n) {
541 int ret = message_port_add_registered_cb(nullptr, "PORT", false,
542 __registration_event_cb, nullptr, &watcher_id);
543 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
546 TEST_F(MessagePortTest, message_port_add_unregistered_cb) {
547 EXPECT_CALL(GetMock<GioMock>(),
548 g_bus_watch_name_on_connection(_, _, _, _, _, _, _)).WillOnce(Return(2));
550 int ret = message_port_add_unregistered_cb("test", "PORT", false,
551 __registration_event_cb, nullptr, &watcher_id);
552 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
555 TEST_F(MessagePortTest, message_port_add_unregistered_cb_n) {
557 int ret = message_port_add_unregistered_cb(nullptr, "PORT", false,
558 __registration_event_cb, nullptr, &watcher_id);
559 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
562 TEST_F(MessagePortTest, message_port_remove_registration_event_cb) {
563 int ret = message_port_remove_registration_event_cb(10);
564 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
567 TEST_F(MessagePortTest, message_port_remove_registration_event_cb_n) {
568 int ret = message_port_remove_registration_event_cb(-1);
569 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);