01af513b4c1105d87f4084f9a0e945ac46a6a8a1
[platform/core/connectivity/net-config.git] / gtest / netstat.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 "netstat.h"
25
26 NetStat::NetStat()
27 {
28         Create();
29 }
30
31 NetStat::~NetStat()
32 {
33         Destroy();
34 }
35
36 error_e NetStat::GetWifiTotalTxBytes(void)
37 {
38         uint64_t tx_bytes = 0;
39         GVariant *message = NULL;
40         error_e error = ERROR_NONE;
41
42         message = InvokeMethod(NETCONFIG_SERVICE,
43                 NETCONFIG_NETWORK_STATISTICS_PATH,
44                 NETCONFIG_NETWORK_STATISTICS_INTERFACE,
45                 GET_WIFI_TOTAL_TX_BYTES,
46                 NULL,
47                 &error);
48
49         if (message == NULL) {
50                 GLOGD("Failed to invoke dbus method");
51                 return error;
52         }
53
54         g_variant_get(message, "(t)", &tx_bytes);
55         g_variant_unref(message);
56
57         GLOGD("Succeeded to get wifi total tx bytes %d", tx_bytes);
58
59         return ERROR_NONE;
60 }
61
62 error_e NetStat::GetWifiTotalRxBytes(void)
63 {
64         uint64_t rx_bytes = 0;
65         GVariant *message = NULL;
66         error_e error = ERROR_NONE;
67
68         message = InvokeMethod(NETCONFIG_SERVICE,
69                 NETCONFIG_NETWORK_STATISTICS_PATH,
70                 NETCONFIG_NETWORK_STATISTICS_INTERFACE,
71                 GET_WIFI_TOTAL_RX_BYTES,
72                 NULL,
73                 &error);
74
75         if (message == NULL) {
76                 GLOGD("Failed to invoke dbus method");
77                 return error;
78         }
79
80         g_variant_get(message, "(t)", &rx_bytes);
81         g_variant_unref(message);
82
83         GLOGD("Succeeded to get wifi total rx bytes %d", rx_bytes);
84
85         return ERROR_NONE;
86 }
87
88 error_e NetStat::GetWifiLastTxBytes(void)
89 {
90         uint64_t tx_bytes = 0;
91         GVariant *message = NULL;
92         error_e error = ERROR_NONE;
93
94         message = InvokeMethod(NETCONFIG_SERVICE,
95                 NETCONFIG_NETWORK_STATISTICS_PATH,
96                 NETCONFIG_NETWORK_STATISTICS_INTERFACE,
97                 GET_WIFI_LAST_TX_BYTES,
98                 NULL,
99                 &error);
100
101         if (message == NULL) {
102                 GLOGD("Failed to invoke dbus method");
103                 return error;
104         }
105
106         g_variant_get(message, "(t)", &tx_bytes);
107         g_variant_unref(message);
108
109         GLOGD("Succeeded to get wifi last tx bytes %d", tx_bytes);
110
111         return ERROR_NONE;
112 }
113
114 error_e NetStat::GetWifiLastRxBytes(void)
115 {
116         uint64_t rx_bytes = 0;
117         GVariant *message = NULL;
118         error_e error = ERROR_NONE;
119
120         message = InvokeMethod(NETCONFIG_SERVICE,
121                 NETCONFIG_NETWORK_STATISTICS_PATH,
122                 NETCONFIG_NETWORK_STATISTICS_INTERFACE,
123                 GET_WIFI_LAST_RX_BYTES,
124                 NULL,
125                 &error);
126
127         if (message == NULL) {
128                 GLOGD("Failed to invoke dbus method");
129                 return error;
130         }
131
132         g_variant_get(message, "(t)", &rx_bytes);
133         g_variant_unref(message);
134
135         GLOGD("Succeeded to get wifi last rx bytes %d", rx_bytes);
136
137         return ERROR_NONE;
138 }
139
140 error_e NetStat::ResetCellularTotalTxBytes(void)
141 {
142         GVariant *message = NULL;
143         error_e error = ERROR_NONE;
144
145         message = InvokeMethod(NETCONFIG_SERVICE,
146                 NETCONFIG_NETWORK_STATISTICS_PATH,
147                 NETCONFIG_NETWORK_STATISTICS_INTERFACE,
148                 RESET_CELLULAR_TOTAL_TX_BYTES,
149                 NULL,
150                 &error);
151
152         if (message == NULL) {
153                 GLOGD("Failed to invoke dbus method");
154                 return error;
155         }
156
157         g_variant_unref(message);
158
159         return ERROR_NONE;
160 }
161
162 error_e NetStat::ResetCellularTotalRxBytes(void)
163 {
164         GVariant *message = NULL;
165         error_e error = ERROR_NONE;
166
167         message = InvokeMethod(NETCONFIG_SERVICE,
168                 NETCONFIG_NETWORK_STATISTICS_PATH,
169                 NETCONFIG_NETWORK_STATISTICS_INTERFACE,
170                 RESET_CELLULAR_TOTAL_RX_BYTES,
171                 NULL,
172                 &error);
173
174         if (message == NULL) {
175                 GLOGD("Failed to invoke dbus method");
176                 return error;
177         }
178
179         g_variant_unref(message);
180
181         return ERROR_NONE;
182 }
183
184 error_e NetStat::ResetCellularLastTxBytes(void)
185 {
186         GVariant *message = NULL;
187         error_e error = ERROR_NONE;
188
189         message = InvokeMethod(NETCONFIG_SERVICE,
190                 NETCONFIG_NETWORK_STATISTICS_PATH,
191                 NETCONFIG_NETWORK_STATISTICS_INTERFACE,
192                 RESET_CELLULAR_LAST_TX_BYTES,
193                 NULL,
194                 &error);
195
196         if (message == NULL) {
197                 GLOGD("Failed to invoke dbus method");
198                 return error;
199         }
200
201         g_variant_unref(message);
202
203         return ERROR_NONE;
204 }
205
206 error_e NetStat::ResetCellularLastRxBytes(void)
207 {
208         GVariant *message = NULL;
209         error_e error = ERROR_NONE;
210
211         message = InvokeMethod(NETCONFIG_SERVICE,
212                 NETCONFIG_NETWORK_STATISTICS_PATH,
213                 NETCONFIG_NETWORK_STATISTICS_INTERFACE,
214                 RESET_CELLULAR_LAST_RX_BYTES,
215                 NULL,
216                 &error);
217
218         if (message == NULL) {
219                 GLOGD("Failed to invoke dbus method");
220                 return error;
221         }
222
223         g_variant_unref(message);
224
225         return ERROR_NONE;
226 }
227
228 error_e NetStat::ResetWifiLastTxBytes(void)
229 {
230         GVariant *message = NULL;
231         error_e error = ERROR_NONE;
232
233         message = InvokeMethod(NETCONFIG_SERVICE,
234                 NETCONFIG_NETWORK_STATISTICS_PATH,
235                 NETCONFIG_NETWORK_STATISTICS_INTERFACE,
236                 RESET_WIFI_LAST_TX_BYTES,
237                 NULL,
238                 &error);
239
240         if (message == NULL) {
241                 GLOGD("Failed to invoke dbus method");
242                 return error;
243         }
244
245         g_variant_unref(message);
246
247         return ERROR_NONE;
248 }
249
250 error_e NetStat::ResetWifiLastRxBytes(void)
251 {
252         GVariant *message = NULL;
253         error_e error = ERROR_NONE;
254
255         message = InvokeMethod(NETCONFIG_SERVICE,
256                 NETCONFIG_NETWORK_STATISTICS_PATH,
257                 NETCONFIG_NETWORK_STATISTICS_INTERFACE,
258                 RESET_WIFI_LAST_RX_BYTES,
259                 NULL,
260                 &error);
261
262         if (message == NULL) {
263                 GLOGD("Failed to invoke dbus method");
264                 return error;
265         }
266
267         g_variant_unref(message);
268
269         return ERROR_NONE;
270 }
271
272 error_e NetStat::ResetWifiTotalTxBytes(void)
273 {
274         GVariant *message = NULL;
275         error_e error = ERROR_NONE;
276
277         message = InvokeMethod(NETCONFIG_SERVICE,
278                 NETCONFIG_NETWORK_STATISTICS_PATH,
279                 NETCONFIG_NETWORK_STATISTICS_INTERFACE,
280                 RESET_WIFI_TOTAL_TX_BYTES,
281                 NULL,
282                 &error);
283
284         if (message == NULL) {
285                 GLOGD("Failed to invoke dbus method");
286                 return error;
287         }
288
289         g_variant_unref(message);
290
291         return ERROR_NONE;
292 }
293
294 error_e NetStat::ResetWifiTotalRxBytes(void)
295 {
296         GVariant *message = NULL;
297         error_e error = ERROR_NONE;
298
299         message = InvokeMethod(NETCONFIG_SERVICE,
300                 NETCONFIG_NETWORK_STATISTICS_PATH,
301                 NETCONFIG_NETWORK_STATISTICS_INTERFACE,
302                 RESET_WIFI_TOTAL_RX_BYTES,
303                 NULL,
304                 &error);
305
306         if (message == NULL) {
307                 GLOGD("Failed to invoke dbus method");
308                 return error;
309         }
310
311         g_variant_unref(message);
312
313         return ERROR_NONE;
314 }