Add initial source codes for gtest
[platform/core/connectivity/stc-manager.git] / src / database / tables / table-restrictions.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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 /**
18  * This file implements restrictions entity handler methods.
19  *
20  * @file        table-restrictions.c
21  */
22
23 #include "stc-db.h"
24 #include "db-internal.h"
25 #include "table-restrictions.h"
26
27 /* DELETE statements */
28 #define DELETE_RESTRICTIONS "DELETE FROM restrictions " \
29         " WHERE binpath = ? AND iftype = ? AND subscriber_id = ?"
30
31 /* SELECT statements */
32 #define SELECT_RESTRICTIONS "SELECT binpath, data_limit, " \
33         " iftype, rst_state, roaming, ifname, subscriber_id, " \
34         " data_warn_limit, restriction_id FROM restrictions"
35
36 #define SELECT_RESTRICTIONS_PER_APP "SELECT binpath, data_limit, " \
37         " iftype, rst_state, roaming, ifname, subscriber_id, " \
38         " data_warn_limit, restriction_id " \
39         " FROM restrictions INDEXED BY restrictions_index " \
40         " WHERE binpath = ?"
41
42 #define SELECT_RESTRICTION_STATE "SELECT rst_state " \
43         " FROM restrictions INDEXED BY restrictions_index " \
44         " WHERE binpath = ? AND iftype = ?"
45
46 #define SELECT_RESTRICTION_STATE_SUBSCRIBER_ID "SELECT rst_state " \
47         " FROM restrictions INDEXED BY restrictions_index " \
48         " WHERE binpath = ? AND iftype = ? AND subscriber_id = ?"
49
50 #define SELECT_RESTRICTION_ID "SELECT restriction_id FROM restrictions " \
51         " WHERE binpath = ? AND iftype = ? AND subscriber_id = ? AND " \
52         " rst_state = ? AND roaming = ? AND ifname = ?"
53
54 /* UPDATE statement */
55 #define UPDATE_NET_RESTRICTIONS "UPDATE restrictions " \
56         " SET binpath = ?, data_limit = ?, iftype = ?, rst_state = ?, " \
57         " roaming = ?, ifname = ?, subscriber_id = ?, data_warn_limit = ? " \
58         " WHERE restriction_id = ?"
59
60 /* INSERT statement */
61 #define INSERT_NET_RESTRICTIONS "INSERT INTO restrictions " \
62         " (binpath, data_limit, iftype, rst_state, " \
63         " roaming, ifname, subscriber_id, data_warn_limit) " \
64         " VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
65
66 static void __finalize_delete(void);
67
68 #define PREPARE_DELETE(stm, query) do { \
69         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
70         if (rc != SQLITE_OK) { \
71                 stm = NULL; \
72                 __finalize_delete(); \
73                 STC_LOGE("Failed to prepare \"%s\"query" \
74                          , query); \
75                 return rc; \
76         } \
77 } while (0)
78
79 static void __finalize_select(void);
80
81 #define PREPARE_SELECT(stm, query) do { \
82         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
83         if (rc != SQLITE_OK) { \
84                 stm = NULL; \
85                 __finalize_select(); \
86                 STC_LOGE("Failed to prepare \"%s\"query" \
87                          , query); \
88                 return rc; \
89         } \
90 } while (0)
91
92 static void __finalize_update(void);
93
94 #define PREPARE_UPDATE(stm, query) do { \
95         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
96         if (rc != SQLITE_OK) { \
97                 stm = NULL; \
98                 __finalize_update(); \
99                 STC_LOGE("Failed to prepare \"%s\"query" \
100                          , query); \
101                 return rc; \
102         } \
103 } while (0)
104
105 static void __finalize_insert(void);
106
107 #define PREPARE_INSERT(stm, query) do { \
108         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
109         if (rc != SQLITE_OK) { \
110                 stm = NULL; \
111                 __finalize_insert(); \
112                 STC_LOGE("Failed to prepare \"%s\"query" \
113                          , query); \
114                 return rc; \
115         } \
116 } while (0)
117
118 #define FINALIZE(stm) do { \
119         if (stm) { \
120                 sqlite3_finalize(stm); \
121                 stm = NULL; \
122         } \
123 } while (0)
124
125 /* DELETE statements */
126 static sqlite3_stmt *delete_restrictions;
127
128 /* SELECT statements */
129 static sqlite3_stmt *select_restriction;
130 static sqlite3_stmt *select_restriction_per_app;
131 static sqlite3_stmt *select_restriction_state;
132 static sqlite3_stmt *select_restriction_state_subscriber_id;
133 static sqlite3_stmt *select_restriction_id;
134
135 /* REPLACE statements */
136 static sqlite3_stmt *update_net_restrictions;
137
138 /* INSERT statements */
139 static sqlite3_stmt *insert_net_restrictions;
140
141 static int __prepare_delete(sqlite3 *db)
142 {
143         __STC_LOG_FUNC_ENTER__;
144         int rc;
145         static int initialized;
146
147         if (initialized) {
148                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
149                 return SQLITE_OK; //LCOV_EXCL_LINE
150         }
151
152         PREPARE_DELETE(delete_restrictions, DELETE_RESTRICTIONS);
153
154         initialized = 1;
155         __STC_LOG_FUNC_EXIT__;
156         return rc;
157 }
158
159 static void __finalize_delete(void)
160 {
161         __STC_LOG_FUNC_ENTER__;
162
163         FINALIZE(delete_restrictions);
164
165         __STC_LOG_FUNC_EXIT__;
166 }
167
168 static int __prepare_select(sqlite3 *db)
169 {
170         __STC_LOG_FUNC_ENTER__;
171         int rc;
172         static int initialized;
173
174         if (initialized) {
175                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
176                 return SQLITE_OK; //LCOV_EXCL_LINE
177         }
178
179         PREPARE_SELECT(select_restriction, SELECT_RESTRICTIONS);
180         PREPARE_SELECT(select_restriction_per_app, SELECT_RESTRICTIONS_PER_APP);
181         PREPARE_SELECT(select_restriction_state, SELECT_RESTRICTION_STATE);
182         PREPARE_SELECT(select_restriction_state_subscriber_id, SELECT_RESTRICTION_STATE_SUBSCRIBER_ID);
183         PREPARE_SELECT(select_restriction_id, SELECT_RESTRICTION_ID);
184
185         initialized = 1;
186         __STC_LOG_FUNC_EXIT__;
187         return rc;
188 }
189
190 static void __finalize_select(void)
191 {
192         __STC_LOG_FUNC_ENTER__;
193
194         FINALIZE(select_restriction);
195         FINALIZE(select_restriction_per_app);
196         FINALIZE(select_restriction_state);
197         FINALIZE(select_restriction_state_subscriber_id);
198         FINALIZE(select_restriction_id);
199
200         __STC_LOG_FUNC_EXIT__;
201 }
202
203 static int __prepare_replace(sqlite3 *db)
204 {
205         __STC_LOG_FUNC_ENTER__;
206         int rc;
207         static int initialized;
208
209         if (initialized) {
210                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
211                 return SQLITE_OK; //LCOV_EXCL_LINE
212         }
213
214         PREPARE_UPDATE(update_net_restrictions, UPDATE_NET_RESTRICTIONS);
215
216         initialized = 1;
217         __STC_LOG_FUNC_EXIT__;
218         return rc;
219 }
220
221 static void __finalize_update(void)
222 {
223         __STC_LOG_FUNC_ENTER__;
224
225         FINALIZE(update_net_restrictions);
226
227         __STC_LOG_FUNC_EXIT__;
228 }
229
230 static int __prepare_insert(sqlite3 *db)
231 {
232         __STC_LOG_FUNC_ENTER__;
233         int rc;
234         static int initialized;
235
236         if (initialized) {
237                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
238                 return SQLITE_OK; //LCOV_EXCL_LINE
239         }
240
241         PREPARE_UPDATE(insert_net_restrictions, INSERT_NET_RESTRICTIONS);
242
243         initialized = 1;
244         __STC_LOG_FUNC_EXIT__;
245         return rc;
246 }
247
248 static void __finalize_insert(void)
249 {
250         __STC_LOG_FUNC_ENTER__;
251
252         FINALIZE(insert_net_restrictions);
253
254         __STC_LOG_FUNC_EXIT__;
255 }
256
257 stc_error_e table_restrictions_per_app(const gchar* app_id,
258                                        const table_restrictions_info_cb restriction_cb,
259                                        void *user_data)
260 {
261         __STC_LOG_FUNC_ENTER__;
262         table_restrictions_info data;
263         int rc;
264         stc_error_e error_code = STC_ERROR_NONE;
265         sqlite3_stmt *stmt = select_restriction_per_app;
266
267         if (!app_id) {
268                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
269                 return STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
270         }
271
272         DB_ACTION(sqlite3_bind_text(stmt, 1, app_id, -1,
273                                     SQLITE_TRANSIENT));
274         data.app_id = (char *)app_id;
275
276         do {
277                 rc = sqlite3_step(stmt);
278
279                 memset(&data, 0, sizeof(data));
280
281                 switch (rc) {
282                 case SQLITE_DONE:
283                         break;
284                 case SQLITE_ROW:
285                         data.app_id = (char *)sqlite3_column_text(stmt, 0);
286                         data.data_limit = sqlite3_column_int64(stmt, 1);
287                         data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 2);
288                         data.rst_state =
289                                 (stc_restriction_state_e)sqlite3_column_int(stmt, 3);
290                         data.roaming = sqlite3_column_int(stmt, 4);
291                         data.ifname = (char *)sqlite3_column_text(stmt, 5);
292                         data.subscriber_id = (char *)sqlite3_column_text(stmt, 6);
293                         data.data_warn_limit = sqlite3_column_int64(stmt, 7);
294                         data.restriction_id = sqlite3_column_int64(stmt, 8);
295
296                         if (restriction_cb(&data, user_data) == STC_CANCEL)
297                                 rc = SQLITE_DONE; //LCOV_EXCL_LINE
298                         break;
299                 case SQLITE_ERROR:
300                 default:
301                         STC_LOGE("Failed to enumerate restrictions: %s\n", //LCOV_EXCL_LINE
302                                  sqlite3_errmsg(stc_db_get_database()));
303
304                         __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
305                         error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
306                 }
307         } while (rc == SQLITE_ROW);
308
309 handle_error:
310         sqlite3_reset(stmt);
311         __STC_LOG_FUNC_EXIT__;
312         return error_code;
313 }
314
315 stc_error_e table_restrictions_foreach(const table_restrictions_info_cb restriction_cb,
316                                        void *user_data)
317 {
318         __STC_LOG_FUNC_ENTER__;
319         table_restrictions_info data;
320         int rc;
321         stc_error_e error_code = STC_ERROR_NONE;
322         sqlite3_stmt *stmt = select_restriction;
323
324         do {
325                 rc = sqlite3_step(stmt);
326
327                 memset(&data, 0, sizeof(data));
328
329                 switch (rc) {
330                 case SQLITE_DONE:
331                         break;
332                 case SQLITE_ROW:
333                         data.app_id = (char *)sqlite3_column_text(stmt, 0);
334                         data.data_limit = sqlite3_column_int64(stmt, 1);
335                         data.iftype = (stc_iface_type_e)sqlite3_column_int(stmt, 2);
336                         data.rst_state =
337                                 (stc_restriction_state_e)sqlite3_column_int(stmt, 3);
338                         data.roaming = sqlite3_column_int(stmt, 4);
339                         data.ifname = (char *)sqlite3_column_text(stmt, 5);
340                         data.subscriber_id = (char *)sqlite3_column_text(stmt, 6);
341                         data.data_warn_limit = sqlite3_column_int64(stmt, 7);
342                         data.restriction_id = sqlite3_column_int64(stmt, 8);
343
344                         if (restriction_cb(&data, user_data) == STC_CANCEL)
345                                 rc = SQLITE_DONE; //LCOV_EXCL_LINE
346                         break;
347                 case SQLITE_ERROR:
348                 default:
349                         STC_LOGE("Failed to enumerate restrictions: %s\n", //LCOV_EXCL_LINE
350                                  sqlite3_errmsg(stc_db_get_database()));
351
352                         __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
353                         error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
354                 }
355         } while (rc == SQLITE_ROW);
356
357         sqlite3_reset(stmt);
358         __STC_LOG_FUNC_EXIT__;
359         return error_code;
360 }
361
362 stc_error_e table_restrictions_get_restriction_state_subscriber_id(const char *app_id,
363                                                           stc_iface_type_e iftype,
364                                                           const char *subscriber_id,
365                                                           stc_restriction_state_e *state)
366 {
367         __STC_LOG_FUNC_ENTER__;
368         int error_code = STC_ERROR_NONE;
369         int ret;
370         bool state_subscriber_id = 0;
371
372         if (state == NULL) {
373                 STC_LOGE("Please provide valid argument!"); //LCOV_EXCL_LINE
374                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
375                 return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
376         }
377
378         *state = STC_RESTRICTION_UNKNOWN;
379         sqlite3_reset(select_restriction_state_subscriber_id);
380         sqlite3_reset(select_restriction_state);
381
382         if (subscriber_id == NULL) {
383                 state_subscriber_id = 0;
384                 DB_ACTION(sqlite3_bind_text(select_restriction_state, 1,
385                                             app_id ? app_id : "", -1,
386                                             SQLITE_STATIC));
387                 DB_ACTION(sqlite3_bind_int(select_restriction_state, 2,
388                                            iftype));
389                 ret = sqlite3_step(select_restriction_state);
390         } else {
391                 state_subscriber_id = 1;
392                 DB_ACTION(sqlite3_bind_text(select_restriction_state_subscriber_id, 1,
393                                             app_id ? app_id : "", -1,
394                                             SQLITE_STATIC));
395                 DB_ACTION(sqlite3_bind_int(select_restriction_state_subscriber_id, 2,
396                                            iftype));
397                 DB_ACTION(sqlite3_bind_text(select_restriction_state_subscriber_id, 3,
398                                             subscriber_id, -1, SQLITE_STATIC));
399                 ret = sqlite3_step(select_restriction_state_subscriber_id);
400         }
401
402         switch (ret) {
403         case SQLITE_DONE:
404                 break;
405         case SQLITE_ROW:
406                 if (state_subscriber_id)
407                         *state = (stc_restriction_state_e)sqlite3_column_int(select_restriction_state_subscriber_id, 0);
408                 else
409                         *state = (stc_restriction_state_e)sqlite3_column_int(select_restriction_state, 0);
410                 break;
411         case SQLITE_ERROR:
412         default:
413                 STC_LOGE("Can't perform sql query: %s \n%s", //LCOV_EXCL_LINE
414                          SELECT_RESTRICTION_STATE,
415                          sqlite3_errmsg(stc_db_get_database()));
416                 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
417         }
418
419 handle_error:
420         sqlite3_reset(select_restriction_state);
421         sqlite3_reset(select_restriction_state_subscriber_id);
422         return error_code;
423 }
424
425 stc_error_e table_restrictions_get_restriction_state(const char *app_id,
426                                                      stc_iface_type_e iftype,
427                                                      stc_restriction_state_e *state)
428 {
429         __STC_LOG_FUNC_ENTER__;
430         __STC_LOG_FUNC_EXIT__;
431         return table_restrictions_get_restriction_state_subscriber_id(app_id, iftype,
432                                                              NULL, state);
433 }
434
435 stc_error_e table_restrictions_delete(const char *app_id,
436                                       const stc_iface_type_e iftype,
437                                       const char *subscriber_id)
438 {
439         stc_error_e error_code = STC_ERROR_NONE;
440         sqlite3_stmt *stmt = delete_restrictions;
441
442         STC_LOGD("app_id [%s], iftype [%d], subscriber_id [%s]",
443                  app_id, iftype, subscriber_id);
444
445         DB_ACTION(sqlite3_bind_text(stmt, 1, app_id ? app_id : "",
446                                     -1, SQLITE_TRANSIENT));
447         DB_ACTION(sqlite3_bind_int(stmt, 2, iftype));
448         DB_ACTION(sqlite3_bind_text(stmt, 3, subscriber_id ? subscriber_id : "", -1,
449                                     SQLITE_TRANSIENT));
450
451         if (sqlite3_step(stmt) != SQLITE_DONE) {
452                 STC_LOGE("Failed to remove restrictions by network interface %s\n", //LCOV_EXCL_LINE
453                          sqlite3_errmsg(stc_db_get_database()));
454                 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
455                 goto handle_error; //LCOV_EXCL_LINE
456         }
457
458         STC_LOGD("Restriction deleted for app_id [%s]", app_id);
459
460 handle_error:
461
462         sqlite3_reset(stmt);
463         return error_code;
464 }
465
466 stc_error_e __get_restriction_id(table_restrictions_info *info)
467 {
468         __STC_LOG_FUNC_ENTER__;
469         int rc;
470         stc_error_e error_code = STC_ERROR_NONE;
471         sqlite3_stmt *stmt = select_restriction_id;
472
473         DB_ACTION(sqlite3_bind_text(stmt, 1, info->app_id ? info->app_id : "",
474                                     -1, SQLITE_TRANSIENT));
475         DB_ACTION(sqlite3_bind_int(stmt, 2, info->iftype));
476         DB_ACTION(sqlite3_bind_text(stmt, 3, info->subscriber_id ? info->subscriber_id : "",
477                                     -1, SQLITE_TRANSIENT));
478         DB_ACTION(sqlite3_bind_int(stmt, 4, info->rst_state));
479         DB_ACTION(sqlite3_bind_int(stmt, 5, info->roaming));
480         DB_ACTION(sqlite3_bind_text(stmt, 6, info->ifname ? info->ifname : "",
481                                     -1, SQLITE_TRANSIENT));
482
483         rc = sqlite3_step(stmt);
484
485         switch (rc) {
486         case SQLITE_DONE:
487                 break;
488         case SQLITE_ROW:
489                 info->restriction_id = sqlite3_column_int64(stmt, 0);
490                 STC_LOGD("restriction id [%llu]", info->restriction_id);
491                 break;
492         case SQLITE_ERROR:
493         default:
494                 STC_LOGE("Failed to get restriction id: %s\n", //LCOV_EXCL_LINE
495                          sqlite3_errmsg(stc_db_get_database()));
496         }
497
498 handle_error:
499         sqlite3_reset(stmt);
500         __STC_LOG_FUNC_EXIT__;
501         return error_code;
502 }
503
504 stc_error_e table_restrictions_update(table_restrictions_info *info)
505 {
506         stc_error_e error_code = STC_ERROR_NONE;
507         sqlite3_stmt *stmt = insert_net_restrictions;
508
509         if (!info) {
510                 error_code = STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
511                 goto handle_error; //LCOV_EXCL_LINE
512         }
513
514         __get_restriction_id(info);
515         if (info->restriction_id)
516                 stmt = update_net_restrictions;
517
518         DB_ACTION(sqlite3_bind_text(stmt, 1, info->app_id ? info->app_id : "",
519                                     -1, SQLITE_TRANSIENT));
520         DB_ACTION(sqlite3_bind_int64(stmt, 2, info->data_limit));
521         DB_ACTION(sqlite3_bind_int(stmt, 3, info->iftype));
522         DB_ACTION(sqlite3_bind_int(stmt, 4, info->rst_state));
523         DB_ACTION(sqlite3_bind_int(stmt, 5, info->roaming));
524         DB_ACTION(sqlite3_bind_text(stmt, 6, info->ifname ? info->ifname : "",
525                                     -1, SQLITE_TRANSIENT));
526         DB_ACTION(sqlite3_bind_text(stmt, 7, info->subscriber_id ? info->subscriber_id : "",
527                                     -1, SQLITE_TRANSIENT));
528         DB_ACTION(sqlite3_bind_int64(stmt, 8, info->data_warn_limit));
529
530         if (info->restriction_id)
531                 DB_ACTION(sqlite3_bind_int64(stmt, 9, info->restriction_id));
532
533         if (sqlite3_step(stmt) != SQLITE_DONE) {
534                 STC_LOGE("Failed to set network restriction: %s\n", //LCOV_EXCL_LINE
535                          sqlite3_errmsg(stc_db_get_database()));
536                 error_code = STC_ERROR_DB_FAILED; //LCOV_EXCL_LINE
537                 goto handle_error; //LCOV_EXCL_LINE
538         }
539
540         if (info->restriction_id) {
541                 STC_LOGD("Restriction updated app_id [%s]", info->app_id);
542         } else {
543                 STC_LOGD("Restriction inserted app_id [%s]", info->app_id);
544                 __get_restriction_id(info);
545         }
546
547 handle_error:
548         sqlite3_reset(stmt);
549         return error_code;
550 }
551
552 stc_error_e table_restrictions_prepare(sqlite3 *db)
553 {
554         __STC_LOG_FUNC_ENTER__;
555
556         stc_error_e error_code = STC_ERROR_NONE;
557
558         if (db == NULL) {
559                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
560                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
561         }
562
563         DB_ACTION(__prepare_delete(db));
564         DB_ACTION(__prepare_select(db));
565         DB_ACTION(__prepare_replace(db));
566         DB_ACTION(__prepare_insert(db));
567
568 handle_error:
569
570         __STC_LOG_FUNC_EXIT__;
571         return error_code;
572 }
573
574 void table_restrictions_finalize(void)
575 {
576         __STC_LOG_FUNC_ENTER__;
577         __finalize_delete();
578         __finalize_select();
579         __finalize_update();
580         __finalize_insert();
581         __STC_LOG_FUNC_EXIT__;
582 }