Initialize smart traffic control iptables package
[platform/core/connectivity/stc-iptables.git] / test / stc_ipt_test.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 #include <stdio.h>
18 #include <glib.h>
19 #include <gio/gio.h>
20
21 #include "stc_ipt_test.h"
22 #include "stc_ipt_gdbus.h"
23 #include "stc_ipt_rule.h"
24 #include "stc_ipt_menu.h"
25
26 extern struct menu_data menu_chain[];
27 extern struct menu_data menu_rule[];
28
29 const char *test_stc_ipt_convert_error_type_to_string(stc_error_e err)
30 {
31         switch (err) {
32         case STC_ERROR_NONE:
33                 return "Successful";
34         case STC_ERROR_NOT_PERMITTED:
35                 return "Operation not permitted";
36         case STC_ERROR_OUT_OF_MEMORY:
37                 return "Out of memory";
38         case STC_ERROR_PERMISSION_DENIED:
39                 return "Permission denied";
40         case STC_ERROR_INVALID_OPERATION:
41                 return "Invalid operation";
42         case STC_ERROR_INVALID_PARAMETER:
43                 return "Invalid parameter";
44         case STC_ERROR_OPERATION_FAILED:
45                 return "Operation failed";
46         default:
47                 return "Unknown";
48         }
49 }
50
51 static struct menu_data menu_main[] = {
52         { "1", LOG_LIGHTBLUE "[Chain]" LOG_END, menu_chain, NULL, NULL},
53         { "2", LOG_LIGHTMAGENTA "[Rule]" LOG_END, menu_rule, NULL, NULL},
54         { NULL, NULL, },
55 };
56
57 static int __test_stc_ipt_initialize(MManager *mm, struct menu_data *menu)
58 {
59         int ret = STC_ERROR_NONE;
60
61         ret = stc_ipt_dbus_create();
62         if (ret != STC_ERROR_NONE) {
63                 msg("Failed to create DBus " LOG_RED "[%s]" LOG_END,
64                         test_stc_ipt_convert_error_type_to_string(ret));
65                 return ret;
66         }
67
68         return ret;
69 }
70
71 static int __test_stc_ipt_deinitialize(void)
72 {
73         GDBusConnection *conn;
74         int ret = STC_ERROR_NONE;
75
76         test_stc_ipt_clear_rule_list();
77
78         conn = stc_ipt_dbus_get_connection();
79         if (conn == NULL) {
80                 msg("Already de-registered");
81                 return STC_ERROR_NONE;
82         }
83
84         ret = stc_ipt_dbus_destroy();
85         if (ret != STC_ERROR_NONE)
86                 msg("Failed to destroy DBus " LOG_RED "[%s]" LOG_END,
87                         test_stc_ipt_convert_error_type_to_string(ret));
88
89         return ret;
90 }
91
92 static gboolean __test_stc_ipt_create_init_menu(struct menu_data init_menu[1])
93 {
94         init_menu[0].key = "1";
95         init_menu[0].title = LOG_YELLOW "STC Iptables" LOG_END;
96         init_menu[0].sub_menu = menu_main;
97         init_menu[0].callback = __test_stc_ipt_initialize;
98         init_menu[0].data = NULL;
99
100         return TRUE;
101 }
102
103 int main(int arg, char **argv)
104 {
105         GMainLoop *mainloop = NULL;
106         GIOChannel *channel = g_io_channel_unix_new(STDIN_FILENO);
107         MManager *manager;
108         struct menu_data init_menu[1+1] = { {NULL, NULL, } };
109
110 #if !GLIB_CHECK_VERSION(2, 35, 0)
111         g_type_init();
112 #endif
113         mainloop = g_main_loop_new(NULL, FALSE);
114
115         msg("");
116         msg(LOG_GREEN "* STC Iptables Test application " LOG_END);
117         msg("* Build On: %s  %s", __DATE__, __TIME__);
118
119         if (__test_stc_ipt_create_init_menu(init_menu) == FALSE)
120                 goto OUT;
121
122         manager = menu_manager_new(init_menu, mainloop);
123         if (!manager)
124                 goto OUT;
125
126         menu_manager_run(manager);
127
128         g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL),
129                         on_menu_manager_keyboard, manager);
130         g_main_loop_run(mainloop);
131
132 OUT:
133         __test_stc_ipt_deinitialize();
134         g_main_loop_unref(mainloop);
135         msg("******* Bye bye *******");
136
137         return 0;
138 }