Initialize smart traffic control iptables package
[platform/core/connectivity/stc-iptables.git] / test / stc_ipt_chain.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 <glib-object.h>
20
21 #include "stc_ipt_test.h"
22 #include "stc_ipt_gdbus.h"
23 #include "stc_ipt_sys.h"
24 #include "stc_ipt_menu.h"
25
26 static char g_chain_name[MENU_DATA_SIZE] = "STC_CHAIN";
27
28 static int __stc_ipt_add_chain(void)
29 {
30         GVariant *message = NULL;
31         GVariant *params = NULL;
32         int err = STC_ERROR_NONE;
33         int result = 0;
34
35         params = g_variant_new("(s)", g_chain_name);
36
37         message = stc_ipt_dbus_invoke_method(
38                         STC_IPT_DBUS_SERVICE,
39                         STC_IPT_DBUS_CHAIN_PATH,
40                         STC_IPT_DBUS_CHAIN_INTERFACE,
41                         STC_IPT_DBUS_METHOD_IPT_ADD_CHAIN,
42                         params,
43                         &err);
44
45         if (message == NULL) {
46                 msg(LOG_RED "Failed to invoke dbus method" LOG_END);
47                 return err;
48         }
49
50         g_variant_get(message, "(i)", &result);
51         msg(LOG_GREEN "Successfully add chain [%d]" LOG_END, result);
52         g_variant_unref(message);
53
54         stc_ipt_exe_cmd(CHECK_IPTABLES);
55
56         return err;
57 }
58
59 static int __stc_ip6t_add_chain(void)
60 {
61         GVariant *message = NULL;
62         GVariant *params = NULL;
63         int err = STC_ERROR_NONE;
64         int result = 0;
65
66         params = g_variant_new("(s)", g_chain_name);
67
68         message = stc_ipt_dbus_invoke_method(
69                         STC_IPT_DBUS_SERVICE,
70                         STC_IPT_DBUS_CHAIN_PATH,
71                         STC_IPT_DBUS_CHAIN_INTERFACE,
72                         STC_IPT_DBUS_METHOD_IP6T_ADD_CHAIN,
73                         params,
74                         &err);
75
76         if (message == NULL) {
77                 msg(LOG_RED "Failed to invoke dbus method" LOG_END);
78                 return err;
79         }
80
81         g_variant_get(message, "(i)", &result);
82         msg(LOG_GREEN "Successfully add 6 chain [%d]" LOG_END, result);
83         g_variant_unref(message);
84
85         stc_ipt_exe_cmd(CHECK_IP6TABLES);
86
87         return err;
88 }
89
90 static int __stc_ipt_remove_chain(void)
91 {
92         GVariant *message = NULL;
93         GVariant *params = NULL;
94         int err = STC_ERROR_NONE;
95         int result = 0;
96
97         params = g_variant_new("(s)", g_chain_name);
98
99         message = stc_ipt_dbus_invoke_method(
100                         STC_IPT_DBUS_SERVICE,
101                         STC_IPT_DBUS_CHAIN_PATH,
102                         STC_IPT_DBUS_CHAIN_INTERFACE,
103                         STC_IPT_DBUS_METHOD_IPT_REMOVE_CHAIN,
104                         params,
105                         &err);
106
107         if (message == NULL) {
108                 msg(LOG_RED "Failed to invoke dbus method" LOG_END);
109                 return err;
110         }
111
112         stc_ipt_exe_cmd(CHECK_IPTABLES);
113
114         g_variant_get(message, "(i)", &result);
115         msg(LOG_GREEN "Successfully remove chain [%d]" LOG_END, result);
116         g_variant_unref(message);
117
118         return err;
119 }
120
121 static int __stc_ip6t_remove_chain(void)
122 {
123         GVariant *message = NULL;
124         GVariant *params = NULL;
125         int err = STC_ERROR_NONE;
126         int result = 0;
127
128         params = g_variant_new("(s)", g_chain_name);
129
130         message = stc_ipt_dbus_invoke_method(
131                         STC_IPT_DBUS_SERVICE,
132                         STC_IPT_DBUS_CHAIN_PATH,
133                         STC_IPT_DBUS_CHAIN_INTERFACE,
134                         STC_IPT_DBUS_METHOD_IP6T_REMOVE_CHAIN,
135                         params,
136                         &err);
137
138         if (message == NULL) {
139                 msg(LOG_RED "Failed to invoke dbus method" LOG_END);
140                 return err;
141         }
142
143         stc_ipt_exe_cmd(CHECK_IP6TABLES);
144
145         g_variant_get(message, "(i)", &result);
146         msg(LOG_GREEN "Successfully remove 6 chain [%d]" LOG_END, result);
147         g_variant_unref(message);
148
149         return err;
150 }
151
152 static int test_stc_ipt_add_chain(MManager *mm, struct menu_data *menu)
153 {
154         int err = STC_ERROR_NONE;
155
156         err = __stc_ipt_add_chain();
157         if (err != STC_ERROR_NONE)
158                 msg(LOG_RED "Failed to add chain" LOG_END);
159
160         err = __stc_ip6t_add_chain();
161         if (err != STC_ERROR_NONE)
162                 msg(LOG_RED "Failed to add 6 chain" LOG_END);
163
164         return err;
165 }
166
167 static int test_stc_ipt_remove_chain(MManager *mm, struct menu_data *menu)
168 {
169         int err = STC_ERROR_NONE;
170
171         err = __stc_ipt_remove_chain();
172         if (err != STC_ERROR_NONE)
173                 msg(LOG_RED "Failed to remove chain" LOG_END);
174
175         err = __stc_ip6t_remove_chain();
176         if (err != STC_ERROR_NONE)
177                 msg(LOG_RED "Failed to remove 6 chain" LOG_END);
178
179         return err;
180 }
181
182 struct menu_data menu_chain[] = {
183         { "1", LOG_LIGHTMAGENTA "[Set]" LOG_END " Chain Name", NULL, NULL, g_chain_name},
184         { "a", LOG_LIGHTBLUE "[Add]" LOG_END " Chain", NULL, test_stc_ipt_add_chain, NULL},
185         { "r", LOG_LIGHTMAGENTA "[Remove]" LOG_END " Chain", NULL, test_stc_ipt_remove_chain, NULL},
186         { NULL, NULL, },
187 };