Release version 1.4.25
[platform/core/appfw/message-port.git] / test / unit_tests / test_message_port.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <stdarg.h>
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>
26
27 #include <iostream>
28 #include <memory>
29
30 #include "message_port.h"
31 #include "gio_mock.h"
32 #include "aul.h"
33 #include "test_fixture.h"
34
35 using ::testing::_;
36 using ::testing::DoAll;
37 using ::testing::Return;
38 using ::testing::SetArgPointee;
39 using ::testing::Invoke;
40
41 typedef enum {
42   LOG_ID_INVALID = -1,
43   LOG_ID_MAIN,
44   LOG_ID_RADIO,
45   LOG_ID_SYSTEM,
46   LOG_ID_APPS,
47   LOG_ID_KMSG,
48   LOG_ID_SYSLOG,
49   LOG_ID_MAX
50 } log_id_t;
51
52 extern "C" int __dlog_print(
53   log_id_t log_id, int prio, const char* tag, const char* fmt, ...) {
54   va_list ap;
55   va_start(ap, fmt);
56   vprintf(fmt, ap);
57   va_end(ap);
58   printf("\n");
59
60   return 0;
61 }
62
63 extern "C" void g_object_unref(gpointer _object) {
64 }
65
66 extern "C" gboolean g_source_remove(guint tag) {
67   return true;
68 }
69
70 extern "C" int aul_request_message_port_socket_pair(int* fd) {
71   fd[0] = 77;
72   fd[1] = 777;
73   return 0;
74 }
75
76 extern "C" int pthread_mutex_lock(pthread_mutex_t* mutex) {
77   return 0;
78 }
79
80 extern "C" int pthread_mutex_unlock(pthread_mutex_t* mutex) {
81   return 0;
82 }
83
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);
87   return 0;
88 }
89
90 extern "C" gint g_unix_fd_list_append(GUnixFDList* list,
91     gint fd, GError** error) {
92   return 0;
93 }
94
95
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);
106 }
107
108 class Mocks : public ::testing::NiceMock<GioMock>,
109               public ::testing::NiceMock<PkgmgrInfoMock> {};
110
111 class MessagePortTest : public TestFixture {
112  public:
113   MessagePortTest() : TestFixture(std::make_unique<Mocks>()) {}
114   virtual ~MessagePortTest() {}
115
116   virtual void SetUp() {
117   }
118
119   virtual void TearDown() {
120   }
121 };
122
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) {
126 }
127
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) {
131 }
132
133 struct _GDBusMessage {
134   int test;
135 };
136 typedef struct _GDBusMessage GDBusMessage;
137 GDBusMessage __message;
138
139 GUnixFDList __fd_list;
140 extern "C" GUnixFDList* g_dbus_message_get_unix_fd_list(
141     GDBusMessage* message) {
142   return &__fd_list;
143 }
144
145 typedef struct _GDBusConnection GDBusConnection;
146 struct _GDBusConnection {
147   int test;
148 };
149
150 GDBusConnection __gdbus_conn;
151 int __port_id;
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",
158       "send_message",
159       g_variant_new("(ssbbssbus)", "test", "PORT", 0, 0, "test",
160         "PORT", 0, msg.ToRaw().second, msg.ToRaw().first.get()),
161       nullptr, nullptr);
162   return __port_id;
163 }
164
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",
171       "send_message",
172       g_variant_new("(ssbbssbus)", "test", "PORT", 1, 0, "test",
173         "PORT", 0, 1, msg.ToRaw().first.get()),
174       nullptr, nullptr);
175   return __port_id;
176 }
177
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;
184   info->path = NULL;
185   info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
186       malloc(sizeof(GDBusInterfaceInfo*)));
187   info->nodes = NULL;
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(_, _, _, _, _, _, _)).
193           WillOnce(Return(1));
194
195   int port = message_port_register_local_port("PORT", __message_cb, nullptr);
196   EXPECT_EQ(port, 1);
197   port = message_port_register_local_port("PORT", __message_cb, nullptr);
198   EXPECT_EQ(port, 1);
199 }
200
201 extern "C" gint* g_unix_fd_list_steal_fds(GUnixFDList* list, gint* length) {
202   gint* fds = (gint*)calloc(2, sizeof(gint));
203   fds[0] = 1;
204   fds[1] = 2;
205   return fds;
206 }
207
208 GIOChannel __gio_channel;
209 extern "C" GIOChannel* g_io_channel_unix_new(gint fd) {
210   return &__gio_channel;
211 }
212
213 GIOFunc __io_func;
214 gpointer __io_data;
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) {
219   __io_func = func;
220   __io_data = user_data;
221   __io_notify = notify;
222   return 1;
223 }
224
225 extern "C" GIOStatus g_io_channel_shutdown(GIOChannel* channel,
226     gboolean flush, GError** err) {
227   return G_IO_STATUS_NORMAL;
228 }
229
230 extern "C" void g_io_channel_unref(GIOChannel* channel) {
231 }
232
233 extern "C" gint g_io_channel_unix_get_fd(GIOChannel* channel) {
234   return 1;
235 }
236
237 extern "C" ssize_t read(int desc, void* buf, size_t count) {
238   if (count == 4 && buf != NULL) {
239     int val = 4;
240     memcpy(buf, &val, count);
241   }
242   return count;
243 }
244
245 extern "C" ssize_t write(int fd, const void* buf, size_t count) {
246   return count;
247 }
248
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,
253     gpointer user_data,
254     GDestroyNotify user_data_free_func) {
255   int* id = NULL;
256   name_appeared_handler(nullptr, "test", "test", (gpointer)id);
257   name_vanished_handler(nullptr, "test", (gpointer)id);
258   return 10;
259 }
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);
263   return message;
264 }
265
266 TEST_F(MessagePortTest, message_port_register_local_port2) {
267   GDBusNodeInfo* info = reinterpret_cast<GDBusNodeInfo*>(
268       malloc(sizeof(GDBusNodeInfo)));
269   info->ref_count = 10;
270   info->path = NULL;
271   info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
272       malloc(sizeof(GDBusInterfaceInfo*)));
273   info->nodes = NULL;
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));
289
290   __port_id = 10;
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);
294 }
295
296 TEST_F(MessagePortTest, message_port_register_local_port3) {
297   GDBusNodeInfo* info = reinterpret_cast<GDBusNodeInfo*>(
298       malloc(sizeof(GDBusNodeInfo)));
299   info->ref_count = 10;
300   info->path = NULL;
301   info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
302       malloc(sizeof(GDBusInterfaceInfo*)));
303   info->nodes = NULL;
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));
310
311   __port_id = 20;
312   int port = message_port_register_local_port("PORT3", __message_cb, nullptr);
313   EXPECT_EQ(port, __port_id);
314   if (__io_notify)
315     __io_notify(__io_data);
316 }
317
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);
321 }
322
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));
327
328   tizen_base::Bundle message;
329   int ret = message_port_send_message("test", "PORT", message.GetHandle());
330   EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
331 }
332
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);
336 }
337
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));
342
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);
347 }
348
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);
353 }
354
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);
360 }
361
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);
366
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);
373
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);
377
378   EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
379   __io_func(nullptr, G_IO_IN, __io_data);
380 }
381
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);
386
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);
395   if (__io_notify)
396     __io_notify(__io_data);
397 }
398
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;
403   info->path = NULL;
404   info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
405       malloc(sizeof(GDBusInterfaceInfo*)));
406   info->nodes = NULL;
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(_, _, _, _, _, _, _)).
412           WillOnce(Return(2));
413
414   int port = message_port_register_trusted_local_port("PORT",
415       __trusted_message_cb, nullptr);
416   EXPECT_EQ(port, 2);
417 }
418
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);
423 }
424
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));
429
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);
434 }
435
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);
440 }
441
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);
447 }
448
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));
453
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);
458 }
459
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);
463 }
464
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));
469
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);
474 }
475
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);
479 }
480
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);
484 }
485
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);
489 }
490
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);
494 }
495
496 TEST_F(MessagePortTest, message_port_check_remote_port) {
497   bool exist;
498
499   int ret = message_port_check_remote_port("test", "test", &exist);
500   EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
501 }
502
503 TEST_F(MessagePortTest, message_port_check_remote_port_n) {
504   bool exist;
505
506   int ret = message_port_check_remote_port(nullptr, "test", &exist);
507   EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
508 }
509
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),
515                       Return(0)));
516
517   if (__call_sync_reply != nullptr)
518     g_variant_unref(__call_sync_reply);
519   __call_sync_reply = g_variant_new("(b)", true);
520
521   tizen_base::Bundle message;
522   bool exist;
523   int ret = message_port_check_trusted_remote_port("test2", "PORT", &exist);
524   EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
525 }
526
527 TEST_F(MessagePortTest, message_port_check_trusted_remote_port_n) {
528   EXPECT_CALL(GetMock<PkgmgrInfoMock>(),
529       pkgmgrinfo_appinfo_get_usr_appinfo(_, _, _)).
530       Times(8).
531       WillOnce(Return(1)).
532       WillOnce(Return(0)).
533       WillOnce(Return(0)).
534       WillOnce(Return(1)).
535       WillOnce(Return(0)).
536       WillOnce(Return(0)).
537       WillOnce(Return(0)).
538       WillOnce(Return(0));
539   EXPECT_CALL(GetMock<PkgmgrInfoMock>(),
540       pkgmgrinfo_appinfo_is_preload(_, _)).
541       Times(6).
542       WillOnce(Return(1)).
543       WillOnce(Return(0)).
544       WillOnce(Return(0)).
545       WillOnce(Return(1)).
546       WillOnce(DoAll(SetArgPointee<1>(true),
547                       Return(0))).
548       WillOnce(DoAll(SetArgPointee<1>(true),
549                       Return(0)));
550   EXPECT_CALL(GetMock<PkgmgrInfoMock>(),
551       pkgmgrinfo_pkginfo_compare_usr_app_cert_info(_, _, _, _)).
552       WillRepeatedly(Return(-1));
553   bool exist;
554   int ret = message_port_check_trusted_remote_port(nullptr, "PORT", &exist);
555   EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
556
557   //For code coverage
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);
561
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);
565
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);
569
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);
573
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);
577 }
578
579 static void __registration_event_cb(const char* remote_app_id,
580   const char* remote_port, bool trusted_remote_port, void* user_data) {
581 }
582
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));
587   int watcher_id;
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);
591 }
592
593 TEST_F(MessagePortTest, message_port_add_registered_cb_n) {
594   int watcher_id;
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);
598 }
599
600 TEST_F(MessagePortTest, message_port_add_unregistered_cb) {
601   EXPECT_CALL(GetMock<GioMock>(),
602       g_bus_watch_name_on_connection(_, _, _, _, _, _, _)).WillOnce(Return(2));
603   int watcher_id;
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);
607 }
608
609 TEST_F(MessagePortTest, message_port_add_unregistered_cb_n) {
610   int watcher_id;
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);
614 }
615
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);
619 }
620
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);
624 }