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_mock.h>
30 #include "message_port.h"
33 #include "test_fixture.h"
36 using ::testing::DoAll;
37 using ::testing::Return;
38 using ::testing::SetArgPointee;
39 using ::testing::Invoke;
52 extern "C" int __dlog_print(
53 log_id_t log_id, int prio, const char* tag, const char* fmt, ...) {
63 extern "C" void g_object_unref(gpointer _object) {
66 extern "C" gboolean g_source_remove(guint tag) {
70 extern "C" int aul_request_message_port_socket_pair(int* fd) {
76 extern "C" int pthread_mutex_lock(pthread_mutex_t* mutex) {
80 extern "C" int pthread_mutex_unlock(pthread_mutex_t* mutex) {
84 extern "C" int aul_app_get_appid_bypid(int pid, char* appid, int len) {
85 char test[5] = "test";
86 snprintf(appid, sizeof(test), "%s", test);
90 extern "C" gint g_unix_fd_list_append(GUnixFDList* list,
91 gint fd, GError** error) {
96 GVariant* __call_sync_reply;
97 extern "C" GVariant* g_dbus_connection_call_sync(
98 GDBusConnection* connection, const gchar* bus_name,
99 const gchar* object_path, const gchar* interface_name,
100 const gchar* method_name, GVariant* parameters,
101 const GVariantType* reply_type, GDBusCallFlags flags,
102 gint timeout_msec, GCancellable* cancellable, GError** error) {
103 if (__call_sync_reply != nullptr)
104 return __call_sync_reply;
105 return g_variant_new("(u)", 1);
108 class Mocks : public ::testing::NiceMock<GioMock>,
109 public ::testing::NiceMock<PkgmgrInfoMock> {};
111 class MessagePortTest : public TestFixture {
113 MessagePortTest() : TestFixture(std::make_unique<Mocks>()) {}
114 virtual ~MessagePortTest() {}
116 virtual void SetUp() {
119 virtual void TearDown() {
123 static void __message_cb(int local_port_id, const char* remote_app_id,
124 const char* remote_port, bool trusted_remote_port,
125 bundle* message, void* user_data) {
128 static void __trusted_message_cb(int trusted_local_port_id,
129 const char* remote_app_id, const char* remote_port,
130 bool trusted_remote_port, bundle* message, void* user_data) {
133 struct _GDBusMessage {
136 typedef struct _GDBusMessage GDBusMessage;
137 GDBusMessage __message;
139 GUnixFDList __fd_list;
140 extern "C" GUnixFDList* g_dbus_message_get_unix_fd_list(
141 GDBusMessage* message) {
145 typedef struct _GDBusConnection GDBusConnection;
146 struct _GDBusConnection {
150 GDBusConnection __gdbus_conn;
152 static guint __connection_register_object(GDBusConnection* connection,
153 const gchar* object_path, GDBusInterfaceInfo* interface_info,
154 const GDBusInterfaceVTable* vtable, gpointer user_data,
155 GDestroyNotify user_data_free_func, GError** error) {
156 tizen_base::Bundle msg;
157 vtable->method_call(&__gdbus_conn, "sender", "obj_path", "iface_name",
159 g_variant_new("(ssbbssbus)", "test", "PORT", 0, 0, "test",
160 "PORT", 0, msg.ToRaw().second, msg.ToRaw().first.get()),
165 static guint __connection_register_object_trusted(GDBusConnection* connection,
166 const gchar* object_path, GDBusInterfaceInfo* interface_info,
167 const GDBusInterfaceVTable* vtable, gpointer user_data,
168 GDestroyNotify user_data_free_func, GError** error) {
169 tizen_base::Bundle msg;
170 vtable->method_call(&__gdbus_conn, "sender", "obj_path", "iface_name",
172 g_variant_new("(ssbbssbus)", "test", "PORT", 1, 0, "test",
173 "PORT", 0, 1, msg.ToRaw().first.get()),
178 TEST_F(MessagePortTest, message_port_register_local_port) {
179 EXPECT_CALL(GetMock<GioMock>(), g_bus_get_sync(_, _, _)).
180 WillOnce(Return(&__gdbus_conn));
181 GDBusNodeInfo* info = reinterpret_cast<GDBusNodeInfo*>(
182 malloc(sizeof(GDBusNodeInfo)));
183 info->ref_count = 10;
185 info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
186 malloc(sizeof(GDBusInterfaceInfo*)));
188 info->annotations = NULL;
189 EXPECT_CALL(GetMock<GioMock>(),
190 g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info));
191 EXPECT_CALL(GetMock<GioMock>(),
192 g_dbus_connection_register_object(_, _, _, _, _, _, _)).
195 int port = message_port_register_local_port("PORT", __message_cb, nullptr);
197 port = message_port_register_local_port("PORT", __message_cb, nullptr);
201 extern "C" gint* g_unix_fd_list_steal_fds(GUnixFDList* list, gint* length) {
202 gint* fds = (gint*)calloc(2, sizeof(gint));
208 GIOChannel __gio_channel;
209 extern "C" GIOChannel* g_io_channel_unix_new(gint fd) {
210 return &__gio_channel;
215 GDestroyNotify __io_notify;
216 extern "C" guint g_io_add_watch_full(GIOChannel* channel, gint priority,
217 GIOCondition condition, GIOFunc func, gpointer user_data,
218 GDestroyNotify notify) {
220 __io_data = user_data;
221 __io_notify = notify;
225 extern "C" GIOStatus g_io_channel_shutdown(GIOChannel* channel,
226 gboolean flush, GError** err) {
227 return G_IO_STATUS_NORMAL;
230 extern "C" void g_io_channel_unref(GIOChannel* channel) {
233 extern "C" gint g_io_channel_unix_get_fd(GIOChannel* channel) {
237 extern "C" ssize_t read(int desc, void* buf, size_t count) {
238 if (count == 4 && buf != NULL) {
240 memcpy(buf, &val, count);
245 extern "C" ssize_t write(int fd, const void* buf, size_t count) {
249 guint __g_bus_watch_name_on_connection(GDBusConnection* connection,
250 const gchar* name, GBusNameWatcherFlags flags,
251 GBusNameAppearedCallback name_appeared_handler,
252 GBusNameVanishedCallback name_vanished_handler,
254 GDestroyNotify user_data_free_func) {
256 name_appeared_handler(nullptr, "test", "test", (gpointer)id);
257 name_vanished_handler(nullptr, "test", (gpointer)id);
260 GDBusMessage* g_dbus_message_new_method_call_fake(const gchar* arg0,
261 const gchar* arg1, const gchar* arg2, const gchar* arg3) {
262 GDBusMessage* message = (GDBusMessage*)g_object_new(G_TYPE_OBJECT, nullptr);
266 TEST_F(MessagePortTest, message_port_register_local_port2) {
267 GDBusNodeInfo* info = reinterpret_cast<GDBusNodeInfo*>(
268 malloc(sizeof(GDBusNodeInfo)));
269 info->ref_count = 10;
271 info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
272 malloc(sizeof(GDBusInterfaceInfo*)));
274 info->annotations = NULL;
275 EXPECT_CALL(GetMock<GioMock>(),
276 g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info));
277 EXPECT_CALL(GetMock<GioMock>(),
278 g_dbus_connection_register_object(_, _, _, _, _, _, _)).
279 WillOnce(Invoke(__connection_register_object));
280 EXPECT_CALL(GetMock<GioMock>(),
281 g_bus_watch_name_on_connection(_, _, _, _, _, _, _)).
282 WillOnce(Invoke(__g_bus_watch_name_on_connection));
283 EXPECT_CALL(GetMock<GioMock>(),
284 g_dbus_message_new_method_call(_, _, _, _)).
285 WillOnce(Invoke(g_dbus_message_new_method_call_fake));
286 EXPECT_CALL(GetMock<GioMock>(),
287 g_dbus_connection_send_message_with_reply_sync(_, _, _, _, _, _, _)).
288 WillOnce(Return(&__message));
291 int port = message_port_register_local_port("PORT2", __message_cb, nullptr);
292 EXPECT_EQ(port, __port_id);
293 __io_func(nullptr, G_IO_IN, __io_data);
296 TEST_F(MessagePortTest, message_port_register_local_port3) {
297 GDBusNodeInfo* info = reinterpret_cast<GDBusNodeInfo*>(
298 malloc(sizeof(GDBusNodeInfo)));
299 info->ref_count = 10;
301 info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
302 malloc(sizeof(GDBusInterfaceInfo*)));
304 info->annotations = NULL;
305 EXPECT_CALL(GetMock<GioMock>(),
306 g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info));
307 EXPECT_CALL(GetMock<GioMock>(),
308 g_dbus_connection_register_object(_, _, _, _, _, _, _)).
309 WillOnce(Invoke(__connection_register_object_trusted));
312 int port = message_port_register_local_port("PORT3", __message_cb, nullptr);
313 EXPECT_EQ(port, __port_id);
315 __io_notify(__io_data);
318 TEST_F(MessagePortTest, message_port_register_local_port_n) {
319 int port = message_port_register_local_port(nullptr, __message_cb, nullptr);
320 EXPECT_EQ(port, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
323 TEST_F(MessagePortTest, message_port_send_message) {
324 EXPECT_CALL(GetMock<GioMock>(),
325 g_dbus_message_new_method_call(_, _, _, _)).
326 WillOnce(Invoke(g_dbus_message_new_method_call_fake));
328 tizen_base::Bundle message;
329 int ret = message_port_send_message("test", "PORT", message.GetHandle());
330 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
333 TEST_F(MessagePortTest, message_port_send_message_n) {
334 int ret = message_port_send_message(nullptr, "PORT", nullptr);
335 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
338 TEST_F(MessagePortTest, message_port_send_message_with_local_port) {
339 EXPECT_CALL(GetMock<GioMock>(),
340 g_dbus_message_new_method_call(_, _, _, _)).
341 WillOnce(Invoke(g_dbus_message_new_method_call_fake));
343 tizen_base::Bundle message;
344 int ret = message_port_send_message_with_local_port(
345 "test", "PORT", message.GetHandle(), 1);
346 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
349 TEST_F(MessagePortTest, message_port_send_message_with_local_port_n1) {
350 int ret = message_port_send_message_with_local_port(
351 nullptr, "PORT", nullptr, 1);
352 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
355 TEST_F(MessagePortTest, message_port_send_message_with_local_port_n2) {
356 tizen_base::Bundle message;
357 int ret = message_port_send_message_with_local_port(
358 "test", "PORT", message.GetHandle(), -1);
359 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
362 TEST_F(MessagePortTest, message_port_send_message_with_local_port2) {
363 if (__call_sync_reply != nullptr)
364 g_variant_unref(__call_sync_reply);
365 __call_sync_reply = g_variant_new("(b)", true);
367 EXPECT_CALL(GetMock<GioMock>(),
368 g_dbus_message_new_method_call(_, _, _, _)).
369 WillOnce(Invoke(g_dbus_message_new_method_call_fake));
370 tizen_base::Bundle message;
371 int ret = message_port_send_message_with_local_port(
372 "remote_app", "PORT", message.GetHandle(), 1);
374 __call_sync_reply = g_variant_new("(b)", true);
375 ret = message_port_send_message_with_local_port(
376 "remote_app", "PORT", message.GetHandle(), 1);
378 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
379 __io_func(nullptr, G_IO_IN, __io_data);
382 TEST_F(MessagePortTest, message_port_send_message_with_local_port3) {
383 if (__call_sync_reply != nullptr)
384 g_variant_unref(__call_sync_reply);
385 __call_sync_reply = g_variant_new("(b)", true);
387 EXPECT_CALL(GetMock<GioMock>(),
388 g_dbus_message_new_method_call(_, _, _, _)).
389 WillOnce(Invoke(g_dbus_message_new_method_call_fake));
390 tizen_base::Bundle message;
391 int ret = message_port_send_message_with_local_port(
392 "remote_app", "PORT", message.GetHandle(), 1);
393 __call_sync_reply = nullptr;
394 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
396 __io_notify(__io_data);
399 TEST_F(MessagePortTest, message_port_register_trusted_local_port) {
400 GDBusNodeInfo* info = reinterpret_cast<GDBusNodeInfo*>(
401 malloc(sizeof(GDBusNodeInfo)));
402 info->ref_count = 10;
404 info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
405 malloc(sizeof(GDBusInterfaceInfo*)));
407 info->annotations = NULL;
408 EXPECT_CALL(GetMock<GioMock>(),
409 g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info));
410 EXPECT_CALL(GetMock<GioMock>(),
411 g_dbus_connection_register_object(_, _, _, _, _, _, _)).
414 int port = message_port_register_trusted_local_port("PORT",
415 __trusted_message_cb, nullptr);
419 TEST_F(MessagePortTest, message_port_register_trusted_local_port_n) {
420 int port = message_port_register_trusted_local_port(
421 nullptr, __message_cb, nullptr);
422 EXPECT_EQ(port, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
425 TEST_F(MessagePortTest, message_port_send_trusted_message_with_local_port) {
426 EXPECT_CALL(GetMock<GioMock>(),
427 g_dbus_message_new_method_call(_, _, _, _)).
428 WillOnce(Invoke(g_dbus_message_new_method_call_fake));
430 tizen_base::Bundle message;
431 int ret = message_port_send_trusted_message_with_local_port(
432 "test", "PORT", message.GetHandle(), 1);
433 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
436 TEST_F(MessagePortTest, message_port_send_trusted_message_with_local_port_n1) {
437 int ret = message_port_send_trusted_message_with_local_port(
438 nullptr, "PORT", nullptr, 1);
439 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
442 TEST_F(MessagePortTest, message_port_send_trusted_message_with_local_port_n2) {
443 tizen_base::Bundle message;
444 int ret = message_port_send_trusted_message_with_local_port(
445 "test", "PORT", message.GetHandle(), -1);
446 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
449 TEST_F(MessagePortTest, message_port_send_trusted_message) {
450 EXPECT_CALL(GetMock<GioMock>(),
451 g_dbus_message_new_method_call(_, _, _, _)).
452 WillOnce(Invoke(g_dbus_message_new_method_call_fake));
454 tizen_base::Bundle message;
455 int ret = message_port_send_trusted_message(
456 "test", "PORT", message.GetHandle());
457 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
460 TEST_F(MessagePortTest, message_port_send_trusted_message_n) {
461 int ret = message_port_send_trusted_message(nullptr, "PORT", nullptr);
462 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
465 TEST_F(MessagePortTest, message_port_send_trusted_message_n2) {
466 EXPECT_CALL(GetMock<GioMock>(),
467 g_dbus_message_new_method_call(_, _, _, _)).
468 WillOnce(Return(nullptr));
470 tizen_base::Bundle message;
471 int ret = message_port_send_trusted_message(
472 "test", "PORT", message.GetHandle());
473 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_OUT_OF_MEMORY);
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) {
499 int ret = message_port_check_remote_port("test", "test", &exist);
500 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
503 TEST_F(MessagePortTest, message_port_check_remote_port_n) {
506 int ret = message_port_check_remote_port(nullptr, "test", &exist);
507 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
510 TEST_F(MessagePortTest, message_port_check_trusted_remote_port) {
511 pkgmgrinfo_cert_compare_result_type_e res = PMINFO_CERT_COMPARE_MATCH;
512 EXPECT_CALL(GetMock<PkgmgrInfoMock>(),
513 pkgmgrinfo_pkginfo_compare_usr_app_cert_info(_, _, _, _)).
514 WillOnce(DoAll(SetArgPointee<3>(res),
517 if (__call_sync_reply != nullptr)
518 g_variant_unref(__call_sync_reply);
519 __call_sync_reply = g_variant_new("(b)", true);
521 tizen_base::Bundle message;
523 int ret = message_port_check_trusted_remote_port("test2", "PORT", &exist);
524 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
527 TEST_F(MessagePortTest, message_port_check_trusted_remote_port_n) {
528 EXPECT_CALL(GetMock<PkgmgrInfoMock>(),
529 pkgmgrinfo_appinfo_get_usr_appinfo(_, _, _)).
539 EXPECT_CALL(GetMock<PkgmgrInfoMock>(),
540 pkgmgrinfo_appinfo_is_preload(_, _)).
546 WillOnce(DoAll(SetArgPointee<1>(true),
548 WillOnce(DoAll(SetArgPointee<1>(true),
550 EXPECT_CALL(GetMock<PkgmgrInfoMock>(),
551 pkgmgrinfo_pkginfo_compare_usr_app_cert_info(_, _, _, _)).
552 WillRepeatedly(Return(-1));
554 int ret = message_port_check_trusted_remote_port(nullptr, "PORT", &exist);
555 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
558 __call_sync_reply = g_variant_new("(b)", true);
559 ret = message_port_check_trusted_remote_port("test3", "PORT", &exist);
560 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH);
562 __call_sync_reply = g_variant_new("(b)", true);
563 ret = message_port_check_trusted_remote_port("test4", "PORT", &exist);
564 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH);
566 __call_sync_reply = g_variant_new("(b)", true);
567 ret = message_port_check_trusted_remote_port("test5", "PORT", &exist);
568 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH);
570 __call_sync_reply = g_variant_new("(b)", true);
571 ret = message_port_check_trusted_remote_port("test6", "PORT", &exist);
572 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH);
574 __call_sync_reply = g_variant_new("(b)", true);
575 ret = message_port_check_trusted_remote_port("test7", "PORT", &exist);
576 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
579 static void __registration_event_cb(const char* remote_app_id,
580 const char* remote_port, bool trusted_remote_port, void* user_data) {
583 TEST_F(MessagePortTest, message_port_add_registered_cb) {
584 EXPECT_CALL(GetMock<GioMock>(),
585 g_bus_watch_name_on_connection(_, _, _, _, _, _, _)).WillOnce(
586 Invoke(__g_bus_watch_name_on_connection));
588 int ret = message_port_add_registered_cb("test", "PORT", false,
589 __registration_event_cb, nullptr, &watcher_id);
590 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
593 TEST_F(MessagePortTest, message_port_add_registered_cb_n) {
595 int ret = message_port_add_registered_cb(nullptr, "PORT", false,
596 __registration_event_cb, nullptr, &watcher_id);
597 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
600 TEST_F(MessagePortTest, message_port_add_unregistered_cb) {
601 EXPECT_CALL(GetMock<GioMock>(),
602 g_bus_watch_name_on_connection(_, _, _, _, _, _, _)).WillOnce(Return(2));
604 int ret = message_port_add_unregistered_cb("test", "PORT", false,
605 __registration_event_cb, nullptr, &watcher_id);
606 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
609 TEST_F(MessagePortTest, message_port_add_unregistered_cb_n) {
611 int ret = message_port_add_unregistered_cb(nullptr, "PORT", false,
612 __registration_event_cb, nullptr, &watcher_id);
613 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
616 TEST_F(MessagePortTest, message_port_remove_registration_event_cb) {
617 int ret = message_port_remove_registration_event_cb(10);
618 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
621 TEST_F(MessagePortTest, message_port_remove_registration_event_cb_n) {
622 int ret = message_port_remove_registration_event_cb(-1);
623 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);