Added additional test cases for stc-manager firewall dbus methods
[platform/core/connectivity/stc-manager.git] / unittest / restriction.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 "restriction.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 RRule::RRule()
35 {
36         this->m_AppID[0] = '\0';
37         this->m_IfaceName[0] = '\0';
38         this->m_IfaceType = IFACE_ALL;
39         this->m_DataLimit = 0;
40         this->m_DataWarnLimit = 0;
41         this->m_RoamingType = ROAMING_DISABLED;
42         this->m_SubscriberID[0] = '\0';
43 }
44
45 RRule::~RRule()
46 {
47 }
48
49 Restriction::Restriction()
50 {
51         Create();
52 }
53
54 Restriction::~Restriction()
55 {
56         Destroy();
57 }
58
59 error_e Restriction::SetRule(const char *app_id, const char *iface_name,
60                 const char *subscriber_id, iface_type_e iface_type,
61                 int64_t data_limit, int64_t data_warn_limit,
62                 roaming_type_e roaming_type, const char *mac)
63 {
64         if (app_id == NULL || strlen(app_id) == 0)
65                 this->m_Rule.m_AppID[0] = '\0';
66         else
67                 g_strlcpy(this->m_Rule.m_AppID, app_id, APP_ID_LEN);
68
69         if (iface_name == NULL || strlen(iface_name) == 0)
70                 this->m_Rule.m_IfaceName[0] = '\0';
71         else
72                 g_strlcpy(this->m_Rule.m_IfaceName, iface_name, IFNAME_LEN);
73
74         if (subscriber_id == NULL || strlen(subscriber_id) == 0)
75                 this->m_Rule.m_SubscriberID[0] = '\0';
76         else
77                 g_strlcpy(this->m_Rule.m_SubscriberID, subscriber_id, SUBSCRIBER_ID_LEN);
78
79         if (mac == NULL || strlen(mac) == 0)
80                 this->m_Rule.m_Mac[0] = '\0';
81         else
82                 g_strlcpy(this->m_Rule.m_Mac, mac, MAC_LEN);
83
84         switch(iface_type) {
85         case IFACE_UNKNOWN:
86         case IFACE_DATACALL:
87         case IFACE_WIFI:
88         case IFACE_WIRED:
89         case IFACE_BLUETOOTH:
90         case IFACE_ALL:
91                 this->m_Rule.m_IfaceType = iface_type;
92                 break;
93         default:
94                 return ERROR_INVALID_PARAMETER;
95         }
96
97         switch(roaming_type) {
98         case ROAMING_UNKNOWN:
99         case ROAMING_ENABLED:
100         case ROAMING_DISABLED:
101                 this->m_Rule.m_RoamingType = roaming_type;
102                 break;
103         default:
104                 return ERROR_INVALID_PARAMETER;
105         }
106
107         this->m_Rule.m_DataLimit = data_limit;
108         this->m_Rule.m_DataWarnLimit = data_warn_limit;
109
110         return ERROR_NONE;
111 }
112
113 void Restriction::MakeRuleParams(GVariant **params)
114 {
115         GVariantBuilder *builder;
116
117         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
118
119         g_variant_builder_add(builder, "{sv}",
120                 RESTRICTION_RULE_APP_ID,
121                 g_variant_new_string(this->m_Rule.m_AppID));
122
123         g_variant_builder_add(builder, "{sv}",
124                 RESTRICTION_RULE_IFNAME,
125                 g_variant_new_string(this->m_Rule.m_IfaceName));
126
127         g_variant_builder_add(builder, "{sv}",
128                 RESTRICTION_RULE_IFTYPE,
129                 g_variant_new_uint16(this->m_Rule.m_IfaceType));
130
131         g_variant_builder_add(builder, "{sv}",
132                 RESTRICTION_RULE_ROAMING,
133                 g_variant_new_uint16(this->m_Rule.m_RoamingType));
134
135         g_variant_builder_add(builder, "{sv}",
136                 RESTRICTION_RULE_SUBSCRIBER_ID,
137                 g_variant_new_string(this->m_Rule.m_SubscriberID));
138
139         g_variant_builder_add(builder, "{sv}",
140                 RESTRICTION_RULE_DATA_LIMIT,
141                 g_variant_new_int64(this->m_Rule.m_DataLimit));
142
143         g_variant_builder_add(builder, "{sv}",
144                 RESTRICTION_RULE_DATA_WARN_LIMIT,
145                 g_variant_new_int64(this->m_Rule.m_DataWarnLimit));
146
147         g_variant_builder_add(builder, "{sv}",
148                 RESTRICTION_RULE_MAC,
149                 g_variant_new_string(this->m_Rule.m_Mac));
150
151
152         *params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
153         g_variant_builder_unref(builder);
154 }
155
156 void Restriction::MakeGetParams(GVariant **params)
157 {
158         *params = g_variant_new("(s)", this->m_Rule.m_AppID);
159 }
160
161 void Restriction::MakeStatusParams(GVariant **params)
162 {
163         *params = g_variant_new("(si)",
164                 this->m_Rule.m_AppID, this->m_Rule.m_IfaceType);
165 }
166
167 error_e Restriction::SetRstriction(void)
168 {
169         GVariant *message = NULL;
170         GVariant *params = NULL;
171         error_e error = ERROR_NONE;
172         int result = 0;
173
174         MakeRuleParams(&params);
175
176         message = InvokeMethod(STC_MGR_SERVICE,
177                 STC_MGR_RESTRICTION_PATH,
178                 STC_MGR_RESTRICTION_INTERFACE,
179                 STC_MGR_METHOD_RESTRICTION_SET,
180                 params,
181                 &error);
182
183         if (message == NULL) {
184                 GLOGD("Failed to invoke dbus method");
185                 return error;
186         }
187
188         g_variant_get(message, "(i)", &result);
189         GLOGD("Succeeded to set restriction [%d]", result);
190         g_variant_unref(message);
191
192         return ERROR_NONE;
193 }
194
195 error_e Restriction::ExcludeRstriction(void)
196 {
197         GVariant *message = NULL;
198         GVariant *params = NULL;
199         error_e error = ERROR_NONE;
200         int result = 0;
201
202         MakeRuleParams(&params);
203
204         message = InvokeMethod(STC_MGR_SERVICE,
205                 STC_MGR_RESTRICTION_PATH,
206                 STC_MGR_RESTRICTION_INTERFACE,
207                 STC_MGR_METHOD_RESTRICTION_EXCLUDE,
208                 params,
209                 &error);
210
211         if (message == NULL) {
212                 GLOGD("Failed to invoke dbus method");
213                 return error;
214         }
215
216         g_variant_get(message, "(i)", &result);
217         GLOGD("Succeeded to exclude restriction [%d]", result);
218         g_variant_unref(message);
219
220         return ERROR_NONE;
221 }
222
223 error_e Restriction::UnsetRstriction(void)
224 {
225         GVariant *message = NULL;
226         GVariant *params = NULL;
227         error_e error = ERROR_NONE;
228         int result = 0;
229
230         MakeRuleParams(&params);
231
232         message = InvokeMethod(STC_MGR_SERVICE,
233                 STC_MGR_RESTRICTION_PATH,
234                 STC_MGR_RESTRICTION_INTERFACE,
235                 STC_MGR_METHOD_RESTRICTION_UNSET,
236                 params,
237                 &error);
238
239         if (message == NULL) {
240                 GLOGD("Failed to invoke dbus method");
241                 return error;
242         }
243
244         g_variant_get(message, "(i)", &result);
245         GLOGD("Succeeded to unset restriction [%d]", result);
246         g_variant_unref(message);
247
248         return ERROR_NONE;
249 }
250
251 error_e Restriction::GetRstrictionStatus(restriction_status_e *status)
252 {
253         GVariant *message = NULL;
254         GVariant *params = NULL;
255         error_e error = ERROR_NONE;
256         int result = 0;
257
258         MakeStatusParams(&params);
259
260         message = InvokeMethod(STC_MGR_SERVICE,
261                 STC_MGR_RESTRICTION_PATH,
262                 STC_MGR_RESTRICTION_INTERFACE,
263                 STC_MGR_METHOD_RESTRICTION_GET_STATUS,
264                 params,
265                 &error);
266
267         if (message == NULL) {
268                 GLOGD("Failed to invoke dbus method");
269                 return error;
270         }
271
272         g_variant_get(message, "(ii)", &result, status);
273         GLOGD("Succeeded to get restriction status [%d:%d]", result, *status);
274         g_variant_unref(message);
275
276         return ERROR_NONE;
277 }
278
279 error_e Restriction::GetRstriction(void)
280 {
281         MainLoop M;
282         GVariant *params = NULL;
283         error_e error = ERROR_NONE;
284
285         MakeGetParams(&params);
286
287         error = InvokeMethodNonblock(STC_MGR_SERVICE,
288                 STC_MGR_RESTRICTION_PATH,
289                 STC_MGR_RESTRICTION_INTERFACE,
290                 STC_MGR_METHOD_RESTRICTION_GET_PER_APP_ID,
291                 params,
292                 DBUS_REPLY_TIMEOUT,
293                 AsyncReadyCallback,
294                 &M);
295
296         if (error != ERROR_NONE) {
297                 GLOGD("Failed to invoke dbus method nonblock");
298                 return error;
299         }
300
301         GLOGD("Succeeded to get restriction per app ID");
302
303         M.run(GMAINTIMEOUT);
304         return ERROR_NONE;
305 }
306
307 error_e Restriction::GetAllRstriction(void)
308 {
309         MainLoop M;
310         error_e error = ERROR_NONE;
311
312         error = InvokeMethodNonblock(STC_MGR_SERVICE,
313                 STC_MGR_RESTRICTION_PATH,
314                 STC_MGR_RESTRICTION_INTERFACE,
315                 STC_MGR_METHOD_RESTRICTION_GET_ALL,
316                 NULL,
317                 DBUS_REPLY_TIMEOUT,
318                 AsyncReadyCallback,
319                 &M);
320
321         if (error != ERROR_NONE) {
322                 GLOGD("Failed to invoke dbus method nonblock");
323                 return error;
324         }
325
326         GLOGD("Succeeded to get all restriction");
327
328         M.run(GMAINTIMEOUT);
329         return ERROR_NONE;
330 }