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>
29 #include "message_port.h"
30 #include "app_common_mock.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" int aul_app_get_appid_bypid(int pid, char* appid, int len) {
64 char test[5] = "test";
65 snprintf(appid, sizeof(test), "%s", test);
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);
78 class Mocks : public ::testing::NiceMock<AppCommonMock>,
79 public ::testing::NiceMock<GioMock>,
80 public ::testing::NiceMock<AulMock> {};
82 class MessagePortTest : public TestFixture {
84 MessagePortTest() : TestFixture(std::make_unique<Mocks>()) {}
85 virtual ~MessagePortTest() {}
87 virtual void SetUp() {
90 virtual void TearDown() {
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) {
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) {
104 typedef struct _GDBusConnection GDBusConnection;
105 struct _GDBusConnection {
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;
117 info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
118 malloc(sizeof(GDBusInterfaceInfo*)));
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(_, _, _, _, _, _, _)).
127 int port = message_port_register_local_port("PORT", __message_cb, nullptr);
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;
136 info->interfaces = reinterpret_cast<GDBusInterfaceInfo**>(
137 malloc(sizeof(GDBusInterfaceInfo*)));
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(_, _, _, _, _, _, _)).
146 int port = message_port_register_trusted_local_port("PORT",
147 __trusted_message_cb, nullptr);
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);
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);