Implement gtest for every public API
[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.h>
26
27 #include <iostream>
28 #include <memory>
29
30 #include "message_port.h"
31 #include "app_common_mock.h"
32 #include "gio_mock.h"
33 #include "aul_mock.h"
34 #include "test_fixture.h"
35
36 using ::testing::_;
37 using ::testing::DoAll;
38 using ::testing::Return;
39 using ::testing::SetArgPointee;
40 using ::testing::Invoke;
41
42 typedef enum {
43   LOG_ID_INVALID = -1,
44   LOG_ID_MAIN,
45   LOG_ID_RADIO,
46   LOG_ID_SYSTEM,
47   LOG_ID_APPS,
48   LOG_ID_KMSG,
49   LOG_ID_SYSLOG,
50   LOG_ID_MAX
51 } log_id_t;
52
53 extern "C" int __dlog_print(
54   log_id_t log_id, int prio, const char* tag, const char* fmt, ...) {
55   va_list ap;
56   va_start(ap, fmt);
57   vprintf(fmt, ap);
58   va_end(ap);
59   printf("\n");
60
61   return 0;
62 }
63
64 extern "C" void g_object_unref(gpointer _object) {
65 }
66
67 extern "C" gboolean g_source_remove(guint tag) {
68   return true;
69 }
70
71 extern "C" int aul_request_message_port_socket_pair(int* fd) {
72   fd[0] = 77;
73   fd[1] = 777;
74   return 0;
75 }
76
77 extern "C" int pthread_mutex_lock(pthread_mutex_t* mutex) {
78   return 0;
79 }
80
81 extern "C" int pthread_mutex_unlock(pthread_mutex_t* mutex) {
82   return 0;
83 }
84
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);
88   return 0;
89 }
90
91 extern "C" gint g_unix_fd_list_append(GUnixFDList* list,
92     gint fd, GError** error) {
93   return 0;
94 }
95
96 extern "C" int pkgmgrinfo_appinfo_get_usr_appinfo(const char* appid, uid_t uid,
97     pkgmgrinfo_appinfo_h* handle) {
98   return 0;
99 }
100
101 extern "C" int pkgmgrinfo_appinfo_is_preload(
102     pkgmgrinfo_appinfo_h handle, bool* preload) {
103   *preload = false;
104   return 0;
105 }
106
107 extern "C" int pkgmgrinfo_appinfo_destroy_appinfo(pkgmgrinfo_appinfo_h handle) {
108   return 0;
109 }
110
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;
115   return 0;
116 }
117
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);
128 }
129
130 class Mocks : public ::testing::NiceMock<AppCommonMock>,
131               public ::testing::NiceMock<GioMock>,
132               public ::testing::NiceMock<AulMock> {};
133
134 class MessagePortTest : public TestFixture {
135  public:
136   MessagePortTest() : TestFixture(std::make_unique<Mocks>()) {}
137   virtual ~MessagePortTest() {}
138
139   virtual void SetUp() {
140   }
141
142   virtual void TearDown() {
143   }
144 };
145
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) {
149 }
150
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) {
154 }
155
156 struct _GDBusMessage {
157   int test;
158 };
159 typedef struct _GDBusMessage GDBusMessage;
160 GDBusMessage __message;
161
162 typedef struct _GUnixFDList GUnixFDList;
163 struct _GUnixFDList {
164   int test;
165 };
166 GUnixFDList __fd_list;
167 extern "C" GUnixFDList* g_dbus_message_get_unix_fd_list(
168     GDBusMessage* message) {
169   return &__fd_list;
170 }
171
172 typedef struct _GDBusConnection GDBusConnection;
173 struct _GDBusConnection {
174   int test;
175 };
176
177 GDBusConnection __gdbus_conn;
178 int __port_id;
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",
185       "send_message",
186       g_variant_new("(ssbbssbus)", "test", "PORT", 0, 0, "test",
187         "PORT", 0, msg.ToRaw().second, msg.ToRaw().first.get()),
188       nullptr, nullptr);
189   return __port_id;
190 }
191
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;
198   info->path = NULL;
199   info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
200       malloc(sizeof(GDBusInterfaceInfo*)));
201   info->nodes = NULL;
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(_, _, _, _, _, _, _)).
207           WillOnce(Return(1));
208
209   int port = message_port_register_local_port("PORT", __message_cb, nullptr);
210   EXPECT_EQ(port, 1);
211   port = message_port_register_local_port("PORT", __message_cb, nullptr);
212   EXPECT_EQ(port, 1);
213 }
214
215 extern "C" gint* g_unix_fd_list_steal_fds(GUnixFDList* list, gint* length) {
216   gint* fds = (gint*)calloc(2, sizeof(gint));
217   fds[0] = 1;
218   fds[1] = 2;
219   return fds;
220 }
221
222 GIOChannel __gio_channel;
223 extern "C" GIOChannel* g_io_channel_unix_new(gint fd) {
224   return &__gio_channel;
225 }
226
227 GIOFunc __io_func;
228 gpointer __io_data;
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) {
233   __io_func = func;
234   __io_data = user_data;
235   __io_notify = notify;
236   return 1;
237 }
238
239 extern "C" GIOStatus g_io_channel_shutdown(GIOChannel* channel,
240     gboolean flush, GError** err) {
241   return G_IO_STATUS_NORMAL;
242 }
243
244 extern "C" void g_io_channel_unref(GIOChannel* channel) {
245 }
246
247 extern "C" gint g_io_channel_unix_get_fd(GIOChannel* channel) {
248   return 1;
249 }
250
251 extern "C" int read(int desc, void* buf, size_t count) {
252   if (count == 4 && buf != NULL) {
253     int val = 4;
254     memcpy(buf, &val, count);
255   }
256   return count;
257 }
258
259 extern "C" ssize_t write(int fd, const void* buf, size_t count) {
260   return count;
261 }
262
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,
267     gpointer user_data,
268     GDestroyNotify user_data_free_func) {
269   int* id = NULL;
270   name_appeared_handler(nullptr, "test", "test", (gpointer)id);
271   name_vanished_handler(nullptr, "test", (gpointer)id);
272   return 10;
273 }
274
275 TEST_F(MessagePortTest, message_port_register_local_port2) {
276   GDBusNodeInfo* info = reinterpret_cast<GDBusNodeInfo*>(
277       malloc(sizeof(GDBusNodeInfo)));
278   info->ref_count = 10;
279   info->path = NULL;
280   info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
281       malloc(sizeof(GDBusInterfaceInfo*)));
282   info->nodes = NULL;
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));
298
299   __port_id = 10;
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);
303 }
304
305 TEST_F(MessagePortTest, message_port_register_local_port3) {
306   GDBusNodeInfo* info = reinterpret_cast<GDBusNodeInfo*>(
307       malloc(sizeof(GDBusNodeInfo)));
308   info->ref_count = 10;
309   info->path = NULL;
310   info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
311       malloc(sizeof(GDBusInterfaceInfo*)));
312   info->nodes = NULL;
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));
319
320   __port_id = 20;
321   int port = message_port_register_local_port("PORT3", __message_cb, nullptr);
322   EXPECT_EQ(port, __port_id);
323   __io_notify(__io_data);
324 }
325
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);
329 }
330
331 TEST_F(MessagePortTest, message_port_send_message) {
332   EXPECT_CALL(GetMock<GioMock>(),
333       g_dbus_message_new_method_call(_, _, _, _)).
334           WillOnce(Return(&__message));
335
336   tizen_base::Bundle message;
337   int ret = message_port_send_message("test", "PORT", message.GetHandle());
338   EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
339 }
340
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);
344 }
345
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));
350
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);
355 }
356
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);
361 }
362
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);
368 }
369
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);
374
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;
383
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;
388
389   EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
390   __io_func(nullptr, G_IO_IN, __io_data);
391 }
392
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);
397
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);
408 }
409
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;
414   info->path = NULL;
415   info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
416       malloc(sizeof(GDBusInterfaceInfo*)));
417   info->nodes = NULL;
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(_, _, _, _, _, _, _)).
423           WillOnce(Return(2));
424
425   int port = message_port_register_trusted_local_port("PORT",
426       __trusted_message_cb, nullptr);
427   EXPECT_EQ(port, 2);
428 }
429
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);
434 }
435
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));
440
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);
445 }
446
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);
451 }
452
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);
458 }
459
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));
464
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);
469 }
470
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);
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   int ret = message_port_check_remote_port("test", "test", &exist);
499   EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
500 }
501
502 TEST_F(MessagePortTest, message_port_check_remote_port_n) {
503   bool exist;
504   int ret = message_port_check_remote_port(nullptr, "test", &exist);
505   EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
506 }
507
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);
512
513   tizen_base::Bundle message;
514   bool exist;
515   int ret = message_port_check_trusted_remote_port("test2", "PORT", &exist);
516   EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
517 }
518
519 TEST_F(MessagePortTest, message_port_check_trusted_remote_port_n) {
520   bool exist;
521   int ret = message_port_check_trusted_remote_port(nullptr, "PORT", &exist);
522   EXPECT_EQ(ret, MESSAGE_PORT_ERROR_INVALID_PARAMETER);
523 }
524
525 static void __registration_event_cb(const char* remote_app_id,
526   const char* remote_port, bool trusted_remote_port, void* user_data) {
527 }
528
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));
533   int watcher_id;
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);
537 }
538
539 TEST_F(MessagePortTest, message_port_add_registered_cb_n) {
540   int watcher_id;
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);
544 }
545
546 TEST_F(MessagePortTest, message_port_add_unregistered_cb) {
547   EXPECT_CALL(GetMock<GioMock>(),
548       g_bus_watch_name_on_connection(_, _, _, _, _, _, _)).WillOnce(Return(2));
549   int watcher_id;
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);
553 }
554
555 TEST_F(MessagePortTest, message_port_add_unregistered_cb_n) {
556   int watcher_id;
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);
560 }
561
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);
565 }
566
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);
570 }