Add gtest
[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
26 #include <iostream>
27 #include <memory>
28
29 #include "message_port.h"
30 #include "app_common_mock.h"
31 #include "gio_mock.h"
32 #include "aul_mock.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" int aul_app_get_appid_bypid(int pid, char* appid, int len) {
64   char test[5] = "test";
65   snprintf(appid, sizeof(test), "%s", test);
66   return 0;
67 }
68
69 extern "C" GVariant* g_dbus_connection_call_sync(
70     GDBusConnection* connection, const gchar* bus_name,
71     const gchar* object_path, const gchar* interface_name,
72     const gchar* method_name, GVariant* parameters,
73     const GVariantType* reply_type, GDBusCallFlags flags,
74     gint timeout_msec, GCancellable* cancellable, GError** error) {
75   return g_variant_new("(u)", 1);
76 }
77
78 class Mocks : public ::testing::NiceMock<AppCommonMock>,
79               public ::testing::NiceMock<GioMock>,
80               public ::testing::NiceMock<AulMock> {};
81
82 class MessagePortTest : public TestFixture {
83  public:
84   MessagePortTest() : TestFixture(std::make_unique<Mocks>()) {}
85   virtual ~MessagePortTest() {}
86
87   virtual void SetUp() {
88   }
89
90   virtual void TearDown() {
91   }
92 };
93
94 static void __message_cb(int local_port_id, const char* remote_app_id,
95     const char* remote_port, bool trusted_remote_port,
96     bundle* message, void* user_data) {
97 }
98
99 static void __trusted_message_cb(int trusted_local_port_id,
100     const char* remote_app_id, const char* remote_port,
101     bool trusted_remote_port, bundle* message, void* user_data) {
102 }
103
104 typedef struct _GDBusConnection GDBusConnection;
105 struct _GDBusConnection {
106   int test;
107 };
108
109 GDBusConnection __gdbus_conn;
110 TEST_F(MessagePortTest, message_port_register_local_port) {
111   EXPECT_CALL(GetMock<GioMock>(), g_bus_get_sync(_, _, _)).
112           WillOnce(Return(&__gdbus_conn));
113   GDBusNodeInfo* info = reinterpret_cast<GDBusNodeInfo*>(
114       malloc(sizeof(GDBusNodeInfo)));
115   info->ref_count = 10;
116   info->path = NULL;
117   info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
118       malloc(sizeof(GDBusInterfaceInfo*)));
119   info->nodes = NULL;
120   info->annotations = NULL;
121   EXPECT_CALL(GetMock<GioMock>(),
122       g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info));
123   EXPECT_CALL(GetMock<GioMock>(),
124       g_dbus_connection_register_object(_, _, _, _, _, _, _)).
125           WillOnce(Return(1));
126
127   int port = message_port_register_local_port("PORT", __message_cb, nullptr);
128   EXPECT_EQ(port, 1);
129 }
130
131 TEST_F(MessagePortTest, message_port_register_trusted_local_port) {
132   GDBusNodeInfo* info = reinterpret_cast<GDBusNodeInfo*>(
133       malloc(sizeof(GDBusNodeInfo)));
134   info->ref_count = 10;
135   info->path = NULL;
136   info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
137       malloc(sizeof(GDBusInterfaceInfo*)));
138   info->nodes = NULL;
139   info->annotations = NULL;
140   EXPECT_CALL(GetMock<GioMock>(),
141       g_dbus_node_info_new_for_xml(_, _)).WillOnce(Return(info));
142   EXPECT_CALL(GetMock<GioMock>(),
143       g_dbus_connection_register_object(_, _, _, _, _, _, _)).
144           WillOnce(Return(2));
145
146   int port = message_port_register_trusted_local_port("PORT",
147       __trusted_message_cb, nullptr);
148   EXPECT_EQ(port, 2);
149 }
150
151 TEST_F(MessagePortTest, message_port_unregister_local_port) {
152   int ret = message_port_unregister_local_port(1);
153   EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
154 }
155
156 TEST_F(MessagePortTest, message_port_unregister_trusted_local_port) {
157   int ret = message_port_unregister_trusted_local_port(2);
158   EXPECT_EQ(ret, MESSAGE_PORT_ERROR_NONE);
159 }