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 typedef struct _GUnixFDList GUnixFDList;
140 struct _GUnixFDList {
143 GUnixFDList __fd_list;
144 extern "C" GUnixFDList* g_dbus_message_get_unix_fd_list(
145 GDBusMessage* message) {
149 typedef struct _GDBusConnection GDBusConnection;
150 struct _GDBusConnection {
154 GDBusConnection __gdbus_conn;
156 static guint __connection_register_object(GDBusConnection* connection,
157 const gchar* object_path, GDBusInterfaceInfo* interface_info,
158 const GDBusInterfaceVTable* vtable, gpointer user_data,
159 GDestroyNotify user_data_free_func, GError** error) {
160 tizen_base::Bundle msg;
161 vtable->method_call(&__gdbus_conn, "sender", "obj_path", "iface_name",
163 g_variant_new("(ssbbssbus)", "test", "PORT", 0, 0, "test",
164 "PORT", 0, msg.ToRaw().second, msg.ToRaw().first.get()),
169 static guint __connection_register_object_trusted(GDBusConnection* connection,
170 const gchar* object_path, GDBusInterfaceInfo* interface_info,
171 const GDBusInterfaceVTable* vtable, gpointer user_data,
172 GDestroyNotify user_data_free_func, GError** error) {
173 tizen_base::Bundle msg;
174 vtable->method_call(&__gdbus_conn, "sender", "obj_path", "iface_name",
176 g_variant_new("(ssbbssbus)", "test", "PORT", 1, 0, "test",
177 "PORT", 0, 1, msg.ToRaw().first.get()),
182 TEST_F(MessagePortTest, message_port_register_local_port) {
183 EXPECT_CALL(GetMock<GioMock>(), g_bus_get_sync(_, _, _)).
184 WillOnce(Return(&__gdbus_conn));
185 GDBusNodeInfo* info = reinterpret_cast<GDBusNodeInfo*>(
186 malloc(sizeof(GDBusNodeInfo)));
187 info->ref_count = 10;
189 info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
190 malloc(sizeof(GDBusInterfaceInfo*)));
192 info->annotations = NULL;
193 EXPECT_CALL(GetMock<GioMock>(),
194 g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info));
195 EXPECT_CALL(GetMock<GioMock>(),
196 g_dbus_connection_register_object(_, _, _, _, _, _, _)).
199 int port = message_port_register_local_port("PORT", __message_cb, nullptr);
201 port = message_port_register_local_port("PORT", __message_cb, nullptr);
205 extern "C" gint* g_unix_fd_list_steal_fds(GUnixFDList* list, gint* length) {
206 gint* fds = (gint*)calloc(2, sizeof(gint));
212 GIOChannel __gio_channel;
213 extern "C" GIOChannel* g_io_channel_unix_new(gint fd) {
214 return &__gio_channel;
219 GDestroyNotify __io_notify;
220 extern "C" guint g_io_add_watch_full(GIOChannel* channel, gint priority,
221 GIOCondition condition, GIOFunc func, gpointer user_data,
222 GDestroyNotify notify) {
224 __io_data = user_data;
225 __io_notify = notify;
229 extern "C" GIOStatus g_io_channel_shutdown(GIOChannel* channel,
230 gboolean flush, GError** err) {
231 return G_IO_STATUS_NORMAL;
234 extern "C" void g_io_channel_unref(GIOChannel* channel) {
237 extern "C" gint g_io_channel_unix_get_fd(GIOChannel* channel) {
241 extern "C" ssize_t read(int desc, void* buf, size_t count) {
242 if (count == 4 && buf != NULL) {
244 memcpy(buf, &val, count);
249 extern "C" ssize_t write(int fd, const void* buf, size_t count) {
253 guint __g_bus_watch_name_on_connection(GDBusConnection* connection,
254 const gchar* name, GBusNameWatcherFlags flags,
255 GBusNameAppearedCallback name_appeared_handler,
256 GBusNameVanishedCallback name_vanished_handler,
258 GDestroyNotify user_data_free_func) {
260 name_appeared_handler(nullptr, "test", "test", (gpointer)id);
261 name_vanished_handler(nullptr, "test", (gpointer)id);
265 TEST_F(MessagePortTest, message_port_register_local_port2) {
266 GDBusNodeInfo* info = reinterpret_cast<GDBusNodeInfo*>(
267 malloc(sizeof(GDBusNodeInfo)));
268 info->ref_count = 10;
270 info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
271 malloc(sizeof(GDBusInterfaceInfo*)));
273 info->annotations = NULL;
274 EXPECT_CALL(GetMock<GioMock>(),
275 g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info));
276 EXPECT_CALL(GetMock<GioMock>(),
277 g_dbus_connection_register_object(_, _, _, _, _, _, _)).
278 WillOnce(Invoke(__connection_register_object));
279 EXPECT_CALL(GetMock<GioMock>(),
280 g_bus_watch_name_on_connection(_, _, _, _, _, _, _)).
281 WillOnce(Invoke(__g_bus_watch_name_on_connection));
282 EXPECT_CALL(GetMock<GioMock>(),
283 g_dbus_message_new_method_call(_, _, _, _)).
284 WillOnce(Return(&__message));
285 EXPECT_CALL(GetMock<GioMock>(),
286 g_dbus_connection_send_message_with_reply_sync(_, _, _, _, _, _, _)).
287 WillOnce(Return(&__message));
290 int port = message_port_register_local_port("PORT2", __message_cb, nullptr);
291 EXPECT_EQ(port, __port_id);
292 __io_func(nullptr, G_IO_IN, __io_data);
295 TEST_F(MessagePortTest, message_port_register_local_port3) {
296 GDBusNodeInfo* info = reinterpret_cast<GDBusNodeInfo*>(
297 malloc(sizeof(GDBusNodeInfo)));
298 info->ref_count = 10;
300 info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
301 malloc(sizeof(GDBusInterfaceInfo*)));
303 info->annotations = NULL;
304 EXPECT_CALL(GetMock<GioMock>(),
305 g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info));
306 EXPECT_CALL(GetMock<GioMock>(),
307 g_dbus_connection_register_object(_, _, _, _, _, _, _)).
308 WillOnce(Invoke(__connection_register_object_trusted));
311 int port = message_port_register_local_port("PORT3", __message_cb, nullptr);
312 EXPECT_EQ(port, __port_id);
313 __io_notify(__io_data);
316 TEST_F(MessagePortTest, message_port_register_local_port_n) {
317 int port = message_port_register_local_port(nullptr, __message_cb, nullptr);
318 EXPECT_EQ(port, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
321 TEST_F(MessagePortTest, message_port_send_message) {
322 EXPECT_CALL(GetMock<GioMock>(),
323 g_dbus_message_new_method_call(_, _, _, _)).
324 WillOnce(Return(&__message));
326 tizen_base::Bundle message;
327 int ret = message_port_send_message("test", "PORT", message.GetHandle());
328 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
331 TEST_F(MessagePortTest, message_port_send_message_n) {
332 int ret = message_port_send_message(nullptr, "PORT", nullptr);
333 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
336 TEST_F(MessagePortTest, message_port_send_message_with_local_port) {
337 EXPECT_CALL(GetMock<GioMock>(),
338 g_dbus_message_new_method_call(_, _, _, _)).
339 WillOnce(Return(&__message));
341 tizen_base::Bundle message;
342 int ret = message_port_send_message_with_local_port(
343 "test", "PORT", message.GetHandle(), 1);
344 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
347 TEST_F(MessagePortTest, message_port_send_message_with_local_port_n1) {
348 int ret = message_port_send_message_with_local_port(
349 nullptr, "PORT", nullptr, 1);
350 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
353 TEST_F(MessagePortTest, message_port_send_message_with_local_port_n2) {
354 tizen_base::Bundle message;
355 int ret = message_port_send_message_with_local_port(
356 "test", "PORT", message.GetHandle(), -1);
357 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
360 TEST_F(MessagePortTest, message_port_send_message_with_local_port2) {
361 if (__call_sync_reply != nullptr)
362 g_variant_unref(__call_sync_reply);
363 __call_sync_reply = g_variant_new("(b)", true);
365 EXPECT_CALL(GetMock<GioMock>(),
366 g_dbus_message_new_method_call(_, _, _, _)).
367 WillOnce(Return(&__message));
368 tizen_base::Bundle message;
369 int ret = message_port_send_message_with_local_port(
370 "remote_app", "PORT", message.GetHandle(), 1);
371 g_variant_unref(__call_sync_reply);
372 __call_sync_reply = nullptr;
374 ret = message_port_send_message_with_local_port(
375 "remote_app", "PORT", message.GetHandle(), 1);
376 g_variant_unref(__call_sync_reply);
377 __call_sync_reply = nullptr;
379 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
380 __io_func(nullptr, G_IO_IN, __io_data);
383 TEST_F(MessagePortTest, message_port_send_message_with_local_port3) {
384 if (__call_sync_reply != nullptr)
385 g_variant_unref(__call_sync_reply);
386 __call_sync_reply = g_variant_new("(b)", true);
388 EXPECT_CALL(GetMock<GioMock>(),
389 g_dbus_message_new_method_call(_, _, _, _)).
390 WillOnce(Return(&__message));
391 tizen_base::Bundle message;
392 int ret = message_port_send_message_with_local_port(
393 "remote_app", "PORT", message.GetHandle(), 1);
394 g_variant_unref(__call_sync_reply);
395 __call_sync_reply = nullptr;
396 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
397 __io_notify(__io_data);
400 TEST_F(MessagePortTest, message_port_register_trusted_local_port) {
401 GDBusNodeInfo* info = reinterpret_cast<GDBusNodeInfo*>(
402 malloc(sizeof(GDBusNodeInfo)));
403 info->ref_count = 10;
405 info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
406 malloc(sizeof(GDBusInterfaceInfo*)));
408 info->annotations = NULL;
409 EXPECT_CALL(GetMock<GioMock>(),
410 g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info));
411 EXPECT_CALL(GetMock<GioMock>(),
412 g_dbus_connection_register_object(_, _, _, _, _, _, _)).
415 int port = message_port_register_trusted_local_port("PORT",
416 __trusted_message_cb, nullptr);
420 TEST_F(MessagePortTest, message_port_register_trusted_local_port_n) {
421 int port = message_port_register_trusted_local_port(
422 nullptr, __message_cb, nullptr);
423 EXPECT_EQ(port, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
426 TEST_F(MessagePortTest, message_port_send_trusted_message_with_local_port) {
427 EXPECT_CALL(GetMock<GioMock>(),
428 g_dbus_message_new_method_call(_, _, _, _)).
429 WillOnce(Return(&__message));
431 tizen_base::Bundle message;
432 int ret = message_port_send_trusted_message_with_local_port(
433 "test", "PORT", message.GetHandle(), 1);
434 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
437 TEST_F(MessagePortTest, message_port_send_trusted_message_with_local_port_n1) {
438 int ret = message_port_send_trusted_message_with_local_port(
439 nullptr, "PORT", nullptr, 1);
440 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
443 TEST_F(MessagePortTest, message_port_send_trusted_message_with_local_port_n2) {
444 tizen_base::Bundle message;
445 int ret = message_port_send_trusted_message_with_local_port(
446 "test", "PORT", message.GetHandle(), -1);
447 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
450 TEST_F(MessagePortTest, message_port_send_trusted_message) {
451 EXPECT_CALL(GetMock<GioMock>(),
452 g_dbus_message_new_method_call(_, _, _, _)).
453 WillOnce(Return(&__message));
455 tizen_base::Bundle message;
456 int ret = message_port_send_trusted_message(
457 "test", "PORT", message.GetHandle());
458 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
461 TEST_F(MessagePortTest, message_port_send_trusted_message_n) {
462 int ret = message_port_send_trusted_message(nullptr, "PORT", nullptr);
463 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
466 TEST_F(MessagePortTest, message_port_send_trusted_message_n2) {
467 EXPECT_CALL(GetMock<GioMock>(),
468 g_dbus_message_new_method_call(_, _, _, _)).
469 WillOnce(Return(nullptr));
471 tizen_base::Bundle message;
472 int ret = message_port_send_trusted_message(
473 "test", "PORT", message.GetHandle());
474 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_OUT_OF_MEMORY);
477 TEST_F(MessagePortTest, message_port_unregister_local_port) {
478 int ret = message_port_unregister_local_port(1);
479 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
482 TEST_F(MessagePortTest, message_port_unregister_local_port_n) {
483 int ret = message_port_unregister_local_port(-1);
484 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
487 TEST_F(MessagePortTest, message_port_unregister_trusted_local_port) {
488 int ret = message_port_unregister_trusted_local_port(2);
489 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
492 TEST_F(MessagePortTest, message_port_unregister_trusted_local_port_n) {
493 int ret = message_port_unregister_trusted_local_port(-1);
494 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
497 TEST_F(MessagePortTest, message_port_check_remote_port) {
500 int ret = message_port_check_remote_port("test", "test", &exist);
501 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
504 TEST_F(MessagePortTest, message_port_check_remote_port_n) {
507 int ret = message_port_check_remote_port(nullptr, "test", &exist);
508 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
511 TEST_F(MessagePortTest, message_port_check_trusted_remote_port) {
512 pkgmgrinfo_cert_compare_result_type_e res = PMINFO_CERT_COMPARE_MATCH;
513 EXPECT_CALL(GetMock<PkgmgrInfoMock>(),
514 pkgmgrinfo_pkginfo_compare_usr_app_cert_info(_, _, _, _)).
515 WillOnce(DoAll(SetArgPointee<3>(res),
518 if (__call_sync_reply != nullptr)
519 g_variant_unref(__call_sync_reply);
520 __call_sync_reply = g_variant_new("(b)", true);
522 tizen_base::Bundle message;
524 int ret = message_port_check_trusted_remote_port("test2", "PORT", &exist);
525 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
528 TEST_F(MessagePortTest, message_port_check_trusted_remote_port_n) {
529 EXPECT_CALL(GetMock<PkgmgrInfoMock>(),
530 pkgmgrinfo_appinfo_get_usr_appinfo(_, _, _)).
540 EXPECT_CALL(GetMock<PkgmgrInfoMock>(),
541 pkgmgrinfo_appinfo_is_preload(_, _)).
547 WillOnce(DoAll(SetArgPointee<1>(true),
549 WillOnce(DoAll(SetArgPointee<1>(true),
551 EXPECT_CALL(GetMock<PkgmgrInfoMock>(),
552 pkgmgrinfo_pkginfo_compare_usr_app_cert_info(_, _, _, _)).
553 WillRepeatedly(Return(-1));
555 int ret = message_port_check_trusted_remote_port(nullptr, "PORT", &exist);
556 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
559 __call_sync_reply = g_variant_new("(b)", true);
560 ret = message_port_check_trusted_remote_port("test3", "PORT", &exist);
561 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH);
563 __call_sync_reply = g_variant_new("(b)", true);
564 ret = message_port_check_trusted_remote_port("test4", "PORT", &exist);
565 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH);
567 __call_sync_reply = g_variant_new("(b)", true);
568 ret = message_port_check_trusted_remote_port("test5", "PORT", &exist);
569 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH);
571 __call_sync_reply = g_variant_new("(b)", true);
572 ret = message_port_check_trusted_remote_port("test6", "PORT", &exist);
573 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH);
575 __call_sync_reply = g_variant_new("(b)", true);
576 ret = message_port_check_trusted_remote_port("test7", "PORT", &exist);
577 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
580 static void __registration_event_cb(const char* remote_app_id,
581 const char* remote_port, bool trusted_remote_port, void* user_data) {
584 TEST_F(MessagePortTest, message_port_add_registered_cb) {
585 EXPECT_CALL(GetMock<GioMock>(),
586 g_bus_watch_name_on_connection(_, _, _, _, _, _, _)).WillOnce(
587 Invoke(__g_bus_watch_name_on_connection));
589 int ret = message_port_add_registered_cb("test", "PORT", false,
590 __registration_event_cb, nullptr, &watcher_id);
591 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
594 TEST_F(MessagePortTest, message_port_add_registered_cb_n) {
596 int ret = message_port_add_registered_cb(nullptr, "PORT", false,
597 __registration_event_cb, nullptr, &watcher_id);
598 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
601 TEST_F(MessagePortTest, message_port_add_unregistered_cb) {
602 EXPECT_CALL(GetMock<GioMock>(),
603 g_bus_watch_name_on_connection(_, _, _, _, _, _, _)).WillOnce(Return(2));
605 int ret = message_port_add_unregistered_cb("test", "PORT", false,
606 __registration_event_cb, nullptr, &watcher_id);
607 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
610 TEST_F(MessagePortTest, message_port_add_unregistered_cb_n) {
612 int ret = message_port_add_unregistered_cb(nullptr, "PORT", false,
613 __registration_event_cb, nullptr, &watcher_id);
614 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
617 TEST_F(MessagePortTest, message_port_remove_registration_event_cb) {
618 int ret = message_port_remove_registration_event_cb(10);
619 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
622 TEST_F(MessagePortTest, message_port_remove_registration_event_cb_n) {
623 int ret = message_port_remove_registration_event_cb(-1);
624 EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);