Merge "Added dump log for connect fail" into tizen
[platform/core/connectivity/net-config.git] / gtest / network_state.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 #include <stdint.h>
23
24 #include "network_state.h"
25
26 NetworkState::NetworkState()
27 {
28         Create();
29 }
30
31 NetworkState::~NetworkState()
32 {
33         Destroy();
34 }
35
36 error_e NetworkState::AddRoute(const char *ip, const char *mask, const char *interface,
37 const char *gateway, int family, bool *result)
38 {
39         GVariant *message = NULL;
40         error_e error = ERROR_NONE;
41
42         message = InvokeMethod(NETCONFIG_SERVICE,
43                 NETCONFIG_NETWORK_PATH,
44                 NETCONFIG_NETWORK_INTERFACE,
45                 ADD_ROUTE,
46                 g_variant_new("(ssssi)", ip, mask, interface,
47                         gateway, family),
48                 &error);
49
50         if (message == NULL) {
51                 GLOGD("Failed to invoke dbus method");
52                 return error;
53         }
54
55         g_variant_get(message, "(b)", result);
56         g_variant_unref(message);
57
58         return ERROR_NONE;
59 }
60
61 error_e NetworkState::RemoveRoute(const char *ip, const char *mask, const char *interface,
62 const char *gateway, int family, bool *result)
63 {
64         GVariant *message = NULL;
65         error_e error = ERROR_NONE;
66
67         message = InvokeMethod(NETCONFIG_SERVICE,
68                 NETCONFIG_NETWORK_PATH,
69                 NETCONFIG_NETWORK_INTERFACE,
70                 REMOVE_ROUTE,
71                 g_variant_new("(ssssi)", ip, mask, interface,
72                         gateway, family),
73                 &error);
74
75         if (message == NULL) {
76                 GLOGD("Failed to invoke dbus method");
77                 return error;
78         }
79
80         g_variant_get(message, "(b)", result);
81         g_variant_unref(message);
82
83         return ERROR_NONE;
84 }
85 error_e NetworkState::EthernetCableState(int *state)
86 {
87         GVariant *message = NULL;
88         error_e error = ERROR_NONE;
89
90         message = InvokeMethod(NETCONFIG_SERVICE,
91                 NETCONFIG_NETWORK_PATH,
92                 NETCONFIG_NETWORK_INTERFACE,
93                 ETHERNET_CABLE_STATE,
94                 NULL,
95                 &error);
96
97         if (message == NULL) {
98                 GLOGD("Failed to invoke dbus method");
99                 return error;
100         }
101
102         g_variant_get(message, "(i)", state);
103         g_variant_unref(message);
104
105         return ERROR_NONE;
106 }
107
108 error_e NetworkState::LaunchMdns(const char *name)
109 {
110         GVariant *message = NULL;
111         error_e error = ERROR_NONE;
112
113         message = InvokeMethod(NETCONFIG_SERVICE,
114                 NETCONFIG_NETWORK_PATH,
115                 NETCONFIG_NETWORK_INTERFACE,
116                 LAUNCH_MDNS,
117                 g_variant_new("(s)", name),
118                 &error);
119
120         if (message == NULL) {
121                 GLOGD("Failed to invoke dbus method");
122                 return error;
123         }
124
125         g_variant_unref(message);
126
127         return ERROR_NONE;
128 }
129 error_e NetworkState::DevicePolicySetWifi(int state)
130 {
131         GVariant *message = NULL;
132         error_e error = ERROR_NONE;
133
134         message = InvokeMethod(NETCONFIG_SERVICE,
135                 NETCONFIG_NETWORK_PATH,
136                 NETCONFIG_NETWORK_INTERFACE,
137                 DEVICE_POLICY_SET_WIFI,
138                 g_variant_new("(i)", state),
139                 &error);
140
141         if (message == NULL) {
142                 GLOGD("Failed to invoke dbus method");
143                 return error;
144         }
145
146         g_variant_unref(message);
147
148         return ERROR_NONE;
149 }
150 error_e NetworkState::DevicePolicyGetWifi(int *state)
151 {
152         GVariant *message = NULL;
153         error_e error = ERROR_NONE;
154
155         message = InvokeMethod(NETCONFIG_SERVICE,
156                 NETCONFIG_NETWORK_PATH,
157                 NETCONFIG_NETWORK_INTERFACE,
158                 DEVICE_POLICY_GET_WIFI,
159                 NULL,
160                 &error);
161
162         if (message == NULL) {
163                 GLOGD("Failed to invoke dbus method");
164                 return error;
165         }
166
167         g_variant_get(message, "(i)", state);
168         g_variant_unref(message);
169
170         return ERROR_NONE;
171 }
172 error_e NetworkState::DevicePolicySetWifiProfile(int state)
173 {
174         GVariant *message = NULL;
175         error_e error = ERROR_NONE;
176
177         message = InvokeMethod(NETCONFIG_SERVICE,
178                 NETCONFIG_NETWORK_PATH,
179                 NETCONFIG_NETWORK_INTERFACE,
180                 DEVICE_POLICY_SET_WIFI_PROFILE,
181                 g_variant_new("(i)", state),
182                 &error);
183
184         if (message == NULL) {
185                 GLOGD("Failed to invoke dbus method");
186                 return error;
187         }
188
189         g_variant_unref(message);
190
191         return ERROR_NONE;
192 }
193 error_e NetworkState::DevicePolicyGetWifiProfile(int *state)
194 {
195         GVariant *message = NULL;
196         error_e error = ERROR_NONE;
197
198         message = InvokeMethod(NETCONFIG_SERVICE,
199                 NETCONFIG_NETWORK_PATH,
200                 NETCONFIG_NETWORK_INTERFACE,
201                 DEVICE_POLICY_GET_WIFI_PROFILE,
202                 NULL,
203                 &error);
204
205         if (message == NULL) {
206                 GLOGD("Failed to invoke dbus method");
207                 return error;
208         }
209
210         g_variant_get(message, "(i)", state);
211         g_variant_unref(message);
212
213         return ERROR_NONE;
214 }
215 error_e NetworkState::GetMeteredInfo(bool *state)
216 {
217         GVariant *message = NULL;
218         error_e error = ERROR_NONE;
219
220         message = InvokeMethod(NETCONFIG_SERVICE,
221                 NETCONFIG_NETWORK_PATH,
222                 NETCONFIG_NETWORK_INTERFACE,
223                 GET_METERED_INFO,
224                 NULL,
225                 &error);
226
227         if (message == NULL) {
228                 GLOGD("Failed to invoke dbus method");
229                 return error;
230         }
231
232         g_variant_get(message, "(b)", state);
233         g_variant_unref(message);
234
235         return ERROR_NONE;
236 }
237 error_e NetworkState::PreferredIpv6Address(const char *profile, char *address)
238 {
239         GVariant *message = NULL;
240         error_e error = ERROR_NONE;
241
242         message = InvokeMethod(NETCONFIG_SERVICE,
243                 NETCONFIG_NETWORK_PATH,
244                 NETCONFIG_NETWORK_INTERFACE,
245                 PREFERRED_IPV6_ADDRESS,
246                 g_variant_new("(s)", profile),
247                 &error);
248
249         if (message == NULL) {
250                 GLOGD("Failed to invoke dbus method");
251                 return error;
252         }
253
254         g_variant_get(message, "(s)", address);
255         g_variant_unref(message);
256
257         return ERROR_NONE;
258 }
259 error_e NetworkState::GetTCPDumpState(bool *state)
260 {
261         GVariant *message = NULL;
262         error_e error = ERROR_NONE;
263
264         message = InvokeMethod(NETCONFIG_SERVICE,
265                 NETCONFIG_NETWORK_PATH,
266                 NETCONFIG_TCPDUMP_INTERFACE,
267                 GET_TCP_DUMPSTATE,
268                 NULL,
269                 &error);
270
271         if (message == NULL) {
272                 GLOGD("Failed to invoke dbus method");
273                 return error;
274         }
275
276         g_variant_get(message, "(b)", state);
277         g_variant_unref(message);
278
279         return ERROR_NONE;
280 }
281 error_e NetworkState::StartTCPDump()
282 {
283         GVariant *message = NULL;
284         error_e error = ERROR_NONE;
285
286         message = InvokeMethod(NETCONFIG_SERVICE,
287                 NETCONFIG_NETWORK_PATH,
288                 NETCONFIG_TCPDUMP_INTERFACE,
289                 START_TCP_DUMP,
290                 NULL,
291                 &error);
292
293         if (message == NULL) {
294                 GLOGD("Failed to invoke dbus method");
295                 return error;
296         }
297
298         g_variant_unref(message);
299
300         return ERROR_NONE;
301 }
302 error_e NetworkState::StopTCPDump()
303 {
304         GVariant *message = NULL;
305         error_e error = ERROR_NONE;
306
307         message = InvokeMethod(NETCONFIG_SERVICE,
308                 NETCONFIG_NETWORK_PATH,
309                 NETCONFIG_TCPDUMP_INTERFACE,
310                 STOP_TCP_DUMP,
311                 NULL,
312                 &error);
313
314         if (message == NULL) {
315                 GLOGD("Failed to invoke dbus method");
316                 return error;
317         }
318
319         g_variant_unref(message);
320
321         return ERROR_NONE;
322 }