49ebd6a29818d156704d22a9476a4c9773b963be
[platform/core/connectivity/stc-manager.git] / unittest / statistics.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 "statistics.h"
24
25 static GMainLoop *g_pMainLoop = NULL;
26 static guint g_nTimeoutId = 0;
27
28 static gboolean CallbackTimeut(gpointer data)
29 {
30         if (g_pMainLoop)
31                 g_main_loop_quit(g_pMainLoop);
32
33         return false;
34 }
35
36 static void RunMainLoop(void)
37 {
38         g_pMainLoop = g_main_loop_new(NULL, false);
39         g_nTimeoutId = g_timeout_add(GMAINTIMEOUT, CallbackTimeut, g_pMainLoop);
40         g_main_loop_run(g_pMainLoop);
41
42         g_source_remove(g_nTimeoutId);
43         g_pMainLoop = NULL;
44 }
45
46 static void QuitMainLoop(void)
47 {
48         if (g_pMainLoop)
49                 g_main_loop_quit(g_pMainLoop);
50 }
51
52 static void AsyncReadyCallback(GObject *source_object,
53         GAsyncResult *res, gpointer user_data)
54 {
55         GLOGD("Succeeded to response async callback");
56         QuitMainLoop();
57 }
58
59 SRule::SRule()
60 {
61         this->m_IfaceType = IFACE_UNKNOWN;
62         this->m_TimePeriod = TIME_PERIOD_UNKNOWN;
63         this->m_Interval.from = 0;
64         this->m_Interval.to = 0;
65 }
66
67 SRule::~SRule()
68 {
69 }
70
71 Statistics::Statistics()
72 {
73         Create();
74 }
75
76 Statistics::~Statistics()
77 {
78         Destroy();
79 }
80
81 error_e Statistics::SetRule(const char *app_id, const char *subscriber_id,
82         iface_type_e iface_type, time_t from, time_t to, time_period_e time_period)
83 {
84         if (app_id == NULL || strlen(app_id) == 0)
85                 this->m_Rule.m_AppID[0] = '\0';
86         else
87                 g_strlcpy(this->m_Rule.m_AppID, app_id, APP_ID_LEN);
88
89         if (subscriber_id == NULL || strlen(subscriber_id) == 0)
90                 this->m_Rule.m_SubscriberID[0] = '\0';
91         else
92                 g_strlcpy(this->m_Rule.m_SubscriberID, subscriber_id, SUBSCRIBER_ID_LEN);
93
94         switch(iface_type) {
95         case IFACE_UNKNOWN:
96         case IFACE_DATACALL:
97         case IFACE_WIFI:
98         case IFACE_WIRED:
99         case IFACE_BLUETOOTH:
100         case IFACE_ALL:
101                 this->m_Rule.m_IfaceType = iface_type;
102                 break;
103         default:
104                 return ERROR_INVALID_PARAMETER;
105         }
106
107         if (from < 0 || to < 0)
108                 return ERROR_INVALID_PARAMETER;
109
110         this->m_Rule.m_Interval.from = from;
111         this->m_Rule.m_Interval.to = to;
112
113         switch(time_period) {
114         case TIME_PERIOD_UNKNOWN:
115         case TIME_PERIOD_HOUR:
116         case TIME_PERIOD_DAY:
117         case TIME_PERIOD_WEEK:
118         case TIME_PERIOD_MONTH:
119                 this->m_Rule.m_TimePeriod = time_period;
120                 break;
121         default:
122                 return ERROR_INVALID_PARAMETER;
123         }
124
125         return ERROR_NONE;
126 }
127
128 time_t Statistics::MakeTime(int year, int mon, int day, int hour, int min)
129 {
130         struct tm curr = { 0, };
131         curr.tm_year = year - 1900;
132         curr.tm_mon = mon - 1;
133         curr.tm_mday = day;
134         curr.tm_hour = hour;
135         curr.tm_min = min;
136         return mktime(&curr);
137 }
138
139 void Statistics::MakeRuleParams(GVariant **params, int mode)
140 {
141         GVariantBuilder *builder;
142
143         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
144
145         g_variant_builder_add(builder, "{sv}",
146                 STATISTICS_RULE_INTERVAL_FROM,
147                 g_variant_new_uint64(this->m_Rule.m_Interval.from));
148
149         g_variant_builder_add(builder, "{sv}",
150                 STATISTICS_RULE_INTERVAL_TO,
151                 g_variant_new_uint64(this->m_Rule.m_Interval.to));
152
153         g_variant_builder_add(builder, "{sv}",
154                 STATISTICS_RULE_IFTYPE,
155                 g_variant_new_uint16(this->m_Rule.m_IfaceType));
156
157         g_variant_builder_add(builder, "{sv}",
158                 STATISTICS_RULE_TIME_PERIOD,
159                 g_variant_new_int32(this->m_Rule.m_TimePeriod));
160
161         switch (mode) {
162         case 0: /* reset */
163                 g_variant_builder_add(builder, "{sv}",
164                         STATISTICS_RULE_APP_ID,
165                         g_variant_new_string(this->m_Rule.m_AppID));
166
167                 g_variant_builder_add(builder, "{sv}",
168                         RESET_RULE_SUBSCRIBER_ID,
169                         g_variant_new_string(this->m_Rule.m_SubscriberID));
170
171                 *params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
172                 break;
173         case 1: /* get app */
174                 *params = g_variant_new("(s@a{sv})", this->m_Rule.m_AppID, g_variant_builder_end(builder));
175                 break;
176         case 2: /* get total */
177                 *params = g_variant_new("(s@a{sv})", "", g_variant_builder_end(builder));
178                 break;
179         case 3: /* get all */
180         default:
181                 *params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));            
182                 break;
183         }
184
185         g_variant_builder_unref(builder);
186 }
187
188 error_e Statistics::InitStatistics(void)
189 {
190         GVariant *message = NULL;
191         error_e error = ERROR_NONE;
192
193         message = InvokeMethod(STC_MGR_SERVICE,
194                 STC_MGR_STATISTICS_PATH,
195                 STC_MGR_STATISTICS_INTERFACE,
196                 STC_MGR_METHOD_STATISTICS_INIT,
197                 NULL,
198                 &error);
199
200         if (message == NULL) {
201                 GLOGD("Failed to invoke dbus method");
202                 return error;
203         }
204
205         GLOGD("Succeeded to init statistics");
206
207         return ERROR_NONE;
208 }
209
210 error_e Statistics::GetStatistics(void)
211 {
212         GVariant *params = NULL;
213         error_e error = ERROR_NONE;
214
215         MakeRuleParams(&params, 1);
216
217         error = InvokeMethodNonblock(STC_MGR_SERVICE,
218                 STC_MGR_STATISTICS_PATH,
219                 STC_MGR_STATISTICS_INTERFACE,
220                 STC_MGR_METHOD_STATISTICS_GET_PER_APP_ID,
221                 params,
222                 DBUS_REPLY_TIMEOUT,
223                 AsyncReadyCallback,
224                 NULL);
225
226         if (error != ERROR_NONE) {
227                 GLOGD("Failed to invoke dbus method nonblock");
228                 return error;
229         }
230
231         RunMainLoop();
232         GLOGD("Succeeded to get statistics per app ID");
233
234         return ERROR_NONE;
235 }
236
237 error_e Statistics::GetTotalStatistics(void)
238 {
239         GVariant *params = NULL;
240         error_e error = ERROR_NONE;
241
242         MakeRuleParams(&params, 2);
243
244         error = InvokeMethodNonblock(STC_MGR_SERVICE,
245                 STC_MGR_STATISTICS_PATH,
246                 STC_MGR_STATISTICS_INTERFACE,
247                 STC_MGR_METHOD_STATISTICS_GET_TOTAL,
248                 params,
249                 DBUS_REPLY_TIMEOUT,
250                 AsyncReadyCallback,
251                 NULL);
252
253         if (error != ERROR_NONE) {
254                 GLOGD("Failed to invoke dbus method nonblock");
255                 return error;
256         }
257
258         RunMainLoop();
259         GLOGD("Succeeded to get total statistics");
260
261         return ERROR_NONE;
262 }
263
264 error_e Statistics::GetAllStatistics(void)
265 {
266         GVariant *params = NULL;
267         error_e error = ERROR_NONE;
268
269         MakeRuleParams(&params, 3);
270
271         error = InvokeMethodNonblock(STC_MGR_SERVICE,
272                 STC_MGR_STATISTICS_PATH,
273                 STC_MGR_STATISTICS_INTERFACE,
274                 STC_MGR_METHOD_STATISTICS_GET_ALL,
275                 params,
276                 DBUS_REPLY_TIMEOUT,
277                 AsyncReadyCallback,
278                 NULL);
279
280         if (error != ERROR_NONE) {
281                 GLOGD("Failed to invoke dbus method nonblock");
282                 return error;
283         }
284
285         RunMainLoop();
286         GLOGD("Succeeded to get all statistics");
287
288         return ERROR_NONE;
289 }
290
291 error_e Statistics::ResetStatistics(void)
292 {
293         GVariant *message = NULL;
294         GVariant *params = NULL;
295         error_e error = ERROR_NONE;
296         int result = 0;
297
298         MakeRuleParams(&params, 0);
299
300         message = InvokeMethod(STC_MGR_SERVICE,
301                 STC_MGR_STATISTICS_PATH,
302                 STC_MGR_STATISTICS_INTERFACE,
303                 STC_MGR_METHOD_STATISTICS_RESET,
304                 params,
305                 &error);
306
307         if (message == NULL) {
308                 GLOGD("Failed to invoke dbus method");
309                 return error;
310         }
311
312         g_variant_get(message, "(i)", &result);
313         GLOGD("Succeeded to reset statistics", result);
314         g_variant_unref(message);
315
316         return ERROR_NONE;
317 }