Modify init value for logging mode
[platform/core/connectivity/stc-iptables.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, "NOT_PERMITTED"))
96                 return ERROR_NOT_PERMITTED;
97         else if (NULL != strstr(error, "OUT_OF_MEMORY"))
98                 return ERROR_OUT_OF_MEMORY;
99         else if (NULL != strstr(error, "PERMISSION_DENIED"))
100                 return ERROR_PERMISSION_DENIED;
101         else if (NULL != strstr(error, "INVALID_OPERATION"))
102                 return ERROR_INVALID_OPERATION;
103         else if (NULL != strstr(error, "INVALID_PARAMETER"))
104                 return ERROR_INVALID_PARAMETER;
105         else if (NULL != strstr(error, "OPERATION_FAILED"))
106                 return ERROR_OPERATION_FAILED;
107
108         return ERROR_OPERATION_FAILED;
109 }
110
111 GVariant *GDbus::InvokeMethod(const char *dest, const char *path,
112         const char *iface_name, const char *method, GVariant *params, error_e *dbus_error)
113 {
114         GError *error = NULL;
115         GVariant *reply = NULL;
116         GDBusConnection *connection = NULL;
117         *dbus_error = ERROR_NONE;
118
119         connection = GetConnection();
120         if (connection == NULL) {
121                 GLOGD("GDBusconnection is NULL");
122                 *dbus_error = ERROR_NOT_INITIALIZED;
123                 return reply;
124         }
125
126         reply = g_dbus_connection_call_sync(connection,
127                         dest,
128                         path,
129                         iface_name,
130                         method,
131                         params,
132                         NULL,
133                         G_DBUS_CALL_FLAGS_NONE,
134                         DBUS_REPLY_TIMEOUT,
135                         GetCancellable(),
136                         &error);
137
138         if (reply == NULL) {
139                 if (error != NULL) {
140                         GLOGD("g_dbus_connection_call_sync() failed "
141                                 "error [%d: %s]", error->code, error->message);
142                         *dbus_error = ConvertErrorStringToEnum(error->message);
143                         g_error_free(error);
144                 } else {
145                         GLOGD("g_dbus_connection_call_sync() failed");
146                         *dbus_error = ERROR_OPERATION_FAILED;
147                 }
148
149                 return NULL;
150         }
151
152         return reply;
153 }
154
155 error_e GDbus::InvokeMethodNonblock(const char *dest, const char *path,
156         const char *iface_name, const char *method, GVariant *params, int timeout,
157         GAsyncReadyCallback notify_func, void *user_data)
158 {
159         GDBusConnection *connection = NULL;
160
161         connection = GetConnection();
162         if (connection == NULL) {
163                 GLOGD("GDBusconnection is NULL");
164                 return ERROR_NOT_INITIALIZED;
165         }
166
167         g_dbus_connection_call(connection,
168                         dest,
169                         path,
170                         iface_name,
171                         method,
172                         params,
173                         NULL,
174                         G_DBUS_CALL_FLAGS_NONE,
175                         timeout,
176                         GetCancellable(),
177                         (GAsyncReadyCallback) notify_func,
178                         (gpointer)user_data);
179
180         return ERROR_NONE;
181 }