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