Fix unittest error
[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 typedef struct _GUnixFDList GUnixFDList;
140 struct _GUnixFDList {
141   int test;
142 };
143 GUnixFDList __fd_list;
144 extern "C" GUnixFDList* g_dbus_message_get_unix_fd_list(
145     GDBusMessage* message) {
146   return &__fd_list;
147 }
148
149 typedef struct _GDBusConnection GDBusConnection;
150 struct _GDBusConnection {
151   int test;
152 };
153
154 GDBusConnection __gdbus_conn;
155 int __port_id;
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",
162       "send_message",
163       g_variant_new("(ssbbssbus)", "test", "PORT", 0, 0, "test",
164         "PORT", 0, msg.ToRaw().second, msg.ToRaw().first.get()),
165       nullptr, nullptr);
166   return __port_id;
167 }
168
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",
175       "send_message",
176       g_variant_new("(ssbbssbus)", "test", "PORT", 1, 0, "test",
177         "PORT", 0, 1, msg.ToRaw().first.get()),
178       nullptr, nullptr);
179   return __port_id;
180 }
181
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;
188   info->path = NULL;
189   info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
190       malloc(sizeof(GDBusInterfaceInfo*)));
191   info->nodes = NULL;
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(_, _, _, _, _, _, _)).
197           WillOnce(Return(1));
198
199   int port = message_port_register_local_port("PORT", __message_cb, nullptr);
200   EXPECT_EQ(port, 1);
201   port = message_port_register_local_port("PORT", __message_cb, nullptr);
202   EXPECT_EQ(port, 1);
203 }
204
205 extern "C" gint* g_unix_fd_list_steal_fds(GUnixFDList* list, gint* length) {
206   gint* fds = (gint*)calloc(2, sizeof(gint));
207   fds[0] = 1;
208   fds[1] = 2;
209   return fds;
210 }
211
212 GIOChannel __gio_channel;
213 extern "C" GIOChannel* g_io_channel_unix_new(gint fd) {
214   return &__gio_channel;
215 }
216
217 GIOFunc __io_func;
218 gpointer __io_data;
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) {
223   __io_func = func;
224   __io_data = user_data;
225   __io_notify = notify;
226   return 1;
227 }
228
229 extern "C" GIOStatus g_io_channel_shutdown(GIOChannel* channel,
230     gboolean flush, GError** err) {
231   return G_IO_STATUS_NORMAL;
232 }
233
234 extern "C" void g_io_channel_unref(GIOChannel* channel) {
235 }
236
237 extern "C" gint g_io_channel_unix_get_fd(GIOChannel* channel) {
238   return 1;
239 }
240
241 extern "C" ssize_t read(int desc, void* buf, size_t count) {
242   if (count == 4 && buf != NULL) {
243     int val = 4;
244     memcpy(buf, &val, count);
245   }
246   return count;
247 }
248
249 extern "C" ssize_t write(int fd, const void* buf, size_t count) {
250   return count;
251 }
252
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,
257     gpointer user_data,
258     GDestroyNotify user_data_free_func) {
259   int* id = NULL;
260   name_appeared_handler(nullptr, "test", "test", (gpointer)id);
261   name_vanished_handler(nullptr, "test", (gpointer)id);
262   return 10;
263 }
264
265 TEST_F(MessagePortTest, message_port_register_local_port2) {
266   GDBusNodeInfo* info = reinterpret_cast<GDBusNodeInfo*>(
267       malloc(sizeof(GDBusNodeInfo)));
268   info->ref_count = 10;
269   info->path = NULL;
270   info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
271       malloc(sizeof(GDBusInterfaceInfo*)));
272   info->nodes = NULL;
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));
288
289   __port_id = 10;
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);
293 }
294
295 TEST_F(MessagePortTest, message_port_register_local_port3) {
296   GDBusNodeInfo* info = reinterpret_cast<GDBusNodeInfo*>(
297       malloc(sizeof(GDBusNodeInfo)));
298   info->ref_count = 10;
299   info->path = NULL;
300   info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
301       malloc(sizeof(GDBusInterfaceInfo*)));
302   info->nodes = NULL;
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));
309
310   __port_id = 20;
311   int port = message_port_register_local_port("PORT3", __message_cb, nullptr);
312   EXPECT_EQ(port, __port_id);
313   __io_notify(__io_data);
314 }
315
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);
319 }
320
321 TEST_F(MessagePortTest, message_port_send_message) {
322   EXPECT_CALL(GetMock<GioMock>(),
323       g_dbus_message_new_method_call(_, _, _, _)).
324           WillOnce(Return(&__message));
325
326   tizen_base::Bundle message;
327   int ret = message_port_send_message("test", "PORT", message.GetHandle());
328   EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
329 }
330
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);
334 }
335
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));
340
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);
345 }
346
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);
351 }
352
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);
358 }
359
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);
364
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;
373
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;
378
379   EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
380   __io_func(nullptr, G_IO_IN, __io_data);
381 }
382
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);
387
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);
398 }
399
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;
404   info->path = NULL;
405   info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
406       malloc(sizeof(GDBusInterfaceInfo*)));
407   info->nodes = NULL;
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(_, _, _, _, _, _, _)).
413           WillOnce(Return(2));
414
415   int port = message_port_register_trusted_local_port("PORT",
416       __trusted_message_cb, nullptr);
417   EXPECT_EQ(port, 2);
418 }
419
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);
424 }
425
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));
430
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);
435 }
436
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);
441 }
442
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);
448 }
449
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));
454
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);
459 }
460
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);
464 }
465
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));
470
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);
475 }
476
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);
480 }
481
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);
485 }
486
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);
490 }
491
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);
495 }
496
497 TEST_F(MessagePortTest, message_port_check_remote_port) {
498   bool exist;
499
500   int ret = message_port_check_remote_port("test", "test", &exist);
501   EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
502 }
503
504 TEST_F(MessagePortTest, message_port_check_remote_port_n) {
505   bool exist;
506
507   int ret = message_port_check_remote_port(nullptr, "test", &exist);
508   EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
509 }
510
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),
516                       Return(0)));
517
518   if (__call_sync_reply != nullptr)
519     g_variant_unref(__call_sync_reply);
520   __call_sync_reply = g_variant_new("(b)", true);
521
522   tizen_base::Bundle message;
523   bool exist;
524   int ret = message_port_check_trusted_remote_port("test2", "PORT", &exist);
525   EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
526 }
527
528 TEST_F(MessagePortTest, message_port_check_trusted_remote_port_n) {
529   EXPECT_CALL(GetMock<PkgmgrInfoMock>(),
530       pkgmgrinfo_appinfo_get_usr_appinfo(_, _, _)).
531       Times(8).
532       WillOnce(Return(1)).
533       WillOnce(Return(0)).
534       WillOnce(Return(0)).
535       WillOnce(Return(1)).
536       WillOnce(Return(0)).
537       WillOnce(Return(0)).
538       WillOnce(Return(0)).
539       WillOnce(Return(0));
540   EXPECT_CALL(GetMock<PkgmgrInfoMock>(),
541       pkgmgrinfo_appinfo_is_preload(_, _)).
542       Times(6).
543       WillOnce(Return(1)).
544       WillOnce(Return(0)).
545       WillOnce(Return(0)).
546       WillOnce(Return(1)).
547       WillOnce(DoAll(SetArgPointee<1>(true),
548                       Return(0))).
549       WillOnce(DoAll(SetArgPointee<1>(true),
550                       Return(0)));
551   EXPECT_CALL(GetMock<PkgmgrInfoMock>(),
552       pkgmgrinfo_pkginfo_compare_usr_app_cert_info(_, _, _, _)).
553       WillRepeatedly(Return(-1));
554   bool exist;
555   int ret = message_port_check_trusted_remote_port(nullptr, "PORT", &exist);
556   EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
557
558   //For code coverage
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);
562
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);
566
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);
570
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);
574
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);
578 }
579
580 static void __registration_event_cb(const char* remote_app_id,
581   const char* remote_port, bool trusted_remote_port, void* user_data) {
582 }
583
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));
588   int watcher_id;
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);
592 }
593
594 TEST_F(MessagePortTest, message_port_add_registered_cb_n) {
595   int watcher_id;
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);
599 }
600
601 TEST_F(MessagePortTest, message_port_add_unregistered_cb) {
602   EXPECT_CALL(GetMock<GioMock>(),
603       g_bus_watch_name_on_connection(_, _, _, _, _, _, _)).WillOnce(Return(2));
604   int watcher_id;
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);
608 }
609
610 TEST_F(MessagePortTest, message_port_add_unregistered_cb_n) {
611   int watcher_id;
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);
615 }
616
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);
620 }
621
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);
625 }