2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "stc_ipt_test.h"
22 #include "stc_ipt_gdbus.h"
23 #include "stc_ipt_rule.h"
24 #include "stc_ipt_menu.h"
26 extern struct menu_data menu_chain[];
27 extern struct menu_data menu_rule[];
29 const char *test_stc_ipt_convert_error_type_to_string(stc_error_e err)
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";
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},
57 static int __test_stc_ipt_initialize(MManager *mm, struct menu_data *menu)
59 int ret = STC_ERROR_NONE;
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));
71 static int __test_stc_ipt_deinitialize(void)
73 GDBusConnection *conn;
74 int ret = STC_ERROR_NONE;
76 test_stc_ipt_clear_rule_list();
78 conn = stc_ipt_dbus_get_connection();
80 msg("Already de-registered");
81 return STC_ERROR_NONE;
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));
92 static gboolean __test_stc_ipt_create_init_menu(struct menu_data init_menu[1])
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;
103 int main(int arg, char **argv)
105 GMainLoop *mainloop = NULL;
106 GIOChannel *channel = g_io_channel_unix_new(STDIN_FILENO);
108 struct menu_data init_menu[1+1] = { {NULL, NULL, } };
110 #if !GLIB_CHECK_VERSION(2, 35, 0)
113 mainloop = g_main_loop_new(NULL, FALSE);
116 msg(LOG_GREEN "* STC Iptables Test application " LOG_END);
117 msg("* Build On: %s %s", __DATE__, __TIME__);
119 if (__test_stc_ipt_create_init_menu(init_menu) == FALSE)
122 manager = menu_manager_new(init_menu, mainloop);
126 menu_manager_run(manager);
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);
133 __test_stc_ipt_deinitialize();
134 g_main_loop_unref(mainloop);
135 msg("******* Bye bye *******");