27764d767d56da7be91071881d6a45966f9d0711
[platform/core/connectivity/net-config.git] / gtest / gdbus.cpp
1 /*
2  * Copyright (c) 2017 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 #include <stdio.h>
18 #include <stdlib.h>
19 #include <iostream>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22
23 #include "gdbus.h"
24
25 GDbus::GDbus()
26 {
27         this->m_pConnection = NULL;
28         this->m_pCancellable = NULL;
29 }
30
31 GDbus::~GDbus()
32 {
33         GDBusConnection *conn = this->m_pConnection;
34         GCancellable *cancel = this->m_pCancellable;
35
36         if (cancel) {
37                 g_cancellable_cancel(cancel);
38                 g_object_unref(cancel);
39                 cancel = NULL;
40         }
41
42         if (conn) {
43                 g_object_unref(conn);
44                 conn = NULL;
45         }
46 }
47
48 error_e GDbus::Create(void)
49 {
50         GError *err = NULL;
51
52 #if !GLIB_CHECK_VERSION(2, 36, 0)
53         g_type_init();
54 #endif
55
56         this->m_pConnection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
57         if (this->m_pConnection == NULL) {
58                 if (err != NULL) {
59                         GLOGD("Failed to connect to the D-BUS daemon [%s]", err->message);
60                         g_error_free(err);
61                 }
62
63                 return ERROR_OPERATION_FAILED;
64         }
65
66         this->m_pCancellable = g_cancellable_new();
67
68         return ERROR_NONE;
69 }
70
71 error_e GDbus::Destroy(void)
72 {
73         g_cancellable_cancel(this->m_pCancellable);
74         g_object_unref(this->m_pCancellable);
75         this->m_pCancellable = NULL;
76
77         g_object_unref(this->m_pConnection);
78         this->m_pConnection = NULL;
79
80         return ERROR_NONE;
81 }
82
83 GDBusConnection *GDbus::GetConnection(void)
84 {
85         return this->m_pConnection;
86 }
87
88 GCancellable *GDbus::GetCancellable(void)
89 {
90         return this->m_pCancellable;
91 }
92
93 error_e GDbus::ConvertErrorStringToEnum(const char *error)
94 {
95         if (NULL != strstr(error, "NoReply"))
96                 return ERROR_INVALID_OPERATION;
97         else if (NULL != strstr(error, "Failed"))
98                 return ERROR_OPERATION_FAILED;
99         else if (NULL != strstr(error, "UnknownMethod"))
100                 return ERROR_INVALID_OPERATION;
101         else if (NULL != strstr(error, "InvalidArguments"))
102                 return ERROR_INVALID_PARAMETER;
103         else if (NULL != strstr(error, "AccessDenied"))
104                 return ERROR_PERMISSION_DENIED;
105         else if (NULL != strstr(error, "PermissionDenied"))
106                 return ERROR_PERMISSION_DENIED;
107         else if (NULL != strstr(error, "NotSupported"))
108                 return ERROR_NOT_SUPPORTED;
109         else if (NULL != strstr(error, "InProgress"))
110                 return ERROR_IN_PROGRESS;
111
112         return ERROR_OPERATION_FAILED;
113 }
114
115 GVariant *GDbus::InvokeMethod_with_fd(GUnixFDList *fd_list, const char *dest, const char *path,
116         const char *iface_name, const char *method, GVariant *params, error_e *dbus_error)
117 {
118         GError *error = NULL;
119         GVariant *reply = NULL;
120         GDBusConnection *connection = NULL;
121         *dbus_error = ERROR_NONE;
122
123         connection = GetConnection();
124         if (connection == NULL) {
125                 GLOGD("GDBusconnection is NULL");
126                 *dbus_error = ERROR_NOT_INITIALIZED;
127                 return reply;
128         }
129
130         reply = g_dbus_connection_call_with_unix_fd_list_sync(connection,
131                         dest,
132                         path,
133                         iface_name,
134                         method,
135                         params,
136                         NULL,
137                         G_DBUS_CALL_FLAGS_NONE,
138                         DBUS_REPLY_TIMEOUT,
139                         fd_list,
140                         NULL,
141                         GetCancellable(),
142                         &error);
143
144         if (reply == NULL) {
145                 if (error != NULL) {
146                         GLOGD("g_dbus_connection_call_with_unix_fd_list_sync() failed "
147                                 "error [%d: %s]", error->code, error->message);
148                         *dbus_error = ConvertErrorStringToEnum(error->message);
149                         g_error_free(error);
150                 } else {
151                         GLOGD("g_dbus_connection_call_with_unix_fd_list_sync() failed");
152                         *dbus_error = ERROR_OPERATION_FAILED;
153                 }
154
155                 return NULL;
156         }
157
158         return reply;
159 }
160
161 GVariant *GDbus::InvokeMethod(const char *dest, const char *path,
162         const char *iface_name, const char *method, GVariant *params, error_e *dbus_error)
163 {
164         GError *error = NULL;
165         GVariant *reply = NULL;
166         GDBusConnection *connection = NULL;
167         *dbus_error = ERROR_NONE;
168
169         connection = GetConnection();
170         if (connection == NULL) {
171                 GLOGD("GDBusconnection is NULL");
172                 *dbus_error = ERROR_NOT_INITIALIZED;
173                 return reply;
174         }
175
176         reply = g_dbus_connection_call_sync(connection,
177                         dest,
178                         path,
179                         iface_name,
180                         method,
181                         params,
182                         NULL,
183                         G_DBUS_CALL_FLAGS_NONE,
184                         DBUS_REPLY_TIMEOUT,
185                         GetCancellable(),
186                         &error);
187
188         if (reply == NULL) {
189                 if (error != NULL) {
190                         GLOGD("g_dbus_connection_call_sync() failed "
191                                 "error [%d: %s]", error->code, error->message);
192                         *dbus_error = ConvertErrorStringToEnum(error->message);
193                         g_error_free(error);
194                 } else {
195                         GLOGD("g_dbus_connection_call_sync() failed");
196                         *dbus_error = ERROR_OPERATION_FAILED;
197                 }
198
199                 return NULL;
200         }
201
202         return reply;
203 }
204
205 error_e GDbus::InvokeMethodNonblock(const char *dest, const char *path,
206         const char *iface_name, const char *method, GVariant *params, int timeout,
207         GAsyncReadyCallback notify_func, void *user_data)
208 {
209         GDBusConnection *connection = NULL;
210
211         connection = GetConnection();
212         if (connection == NULL) {
213                 GLOGD("GDBusconnection is NULL");
214                 return ERROR_NOT_INITIALIZED;
215         }
216
217         g_dbus_connection_call(connection,
218                         dest,
219                         path,
220                         iface_name,
221                         method,
222                         params,
223                         NULL,
224                         G_DBUS_CALL_FLAGS_NONE,
225                         timeout,
226                         GetCancellable(),
227                         (GAsyncReadyCallback) notify_func,
228                         (gpointer)user_data);
229
230         return ERROR_NONE;
231 }