Merge "Return errors to caller" into tizen_5.5
[platform/core/connectivity/stc-manager.git] / unittest / 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(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_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                         GetCancellable(),
140                         &error);
141
142         if (reply == NULL) {
143                 if (error != NULL) {
144                         GLOGD("g_dbus_connection_call_sync() failed "
145                                 "error [%d: %s]", error->code, error->message);
146                         *dbus_error = ConvertErrorStringToEnum(error->message);
147                         g_error_free(error);
148                 } else {
149                         GLOGD("g_dbus_connection_call_sync() failed");
150                         *dbus_error = ERROR_OPERATION_FAILED;
151                 }
152
153                 return NULL;
154         }
155
156         return reply;
157 }
158
159 error_e GDbus::InvokeMethodNonblock(const char *dest, const char *path,
160         const char *iface_name, const char *method, GVariant *params, int timeout,
161         GAsyncReadyCallback notify_func, void *user_data)
162 {
163         GDBusConnection *connection = NULL;
164
165         connection = GetConnection();
166         if (connection == NULL) {
167                 GLOGD("GDBusconnection is NULL");
168                 return ERROR_NOT_INITIALIZED;
169         }
170
171         g_dbus_connection_call(connection,
172                         dest,
173                         path,
174                         iface_name,
175                         method,
176                         params,
177                         NULL,
178                         G_DBUS_CALL_FLAGS_NONE,
179                         timeout,
180                         GetCancellable(),
181                         (GAsyncReadyCallback) notify_func,
182                         (gpointer)user_data);
183
184         return ERROR_NONE;
185 }