blob: 5f93b3dbb92cb1a1dbd6a804259af219596b2538 [file] [log] [blame]
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +02001/* (C) 2013 by Jacob Erlbeck <jerlbeck@sysmocom.de>
2 * All Rights Reserved
3 *
4 * This program is iree software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 */
19
20#include <stdio.h>
21#include <string.h>
22
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020023#include <sys/types.h>
24#include <sys/socket.h>
25#include <sys/un.h>
26
Holger Hans Peter Freyther6ef71b02013-09-10 11:17:46 +020027#include <osmocom/core/application.h>
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +020028#include <osmocom/core/talloc.h>
29#include <osmocom/core/logging.h>
30#include <osmocom/core/utils.h>
31#include <osmocom/vty/misc.h>
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020032#include <osmocom/vty/vty.h>
33#include <osmocom/vty/command.h>
34#include <osmocom/vty/buffer.h>
Holger Hans Peter Freyther6ef71b02013-09-10 11:17:46 +020035#include <osmocom/vty/logging.h>
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020036
37static enum event last_vty_connection_event = -1;
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +020038
39static void test_cmd_string_from_valstr(void)
40{
41 char *cmd;
42 const struct value_string printf_seq_vs[] = {
43 { .value = 42, .str = "[foo%s%s%s%s%s]"},
44 { .value = 43, .str = "[bar%s%s%s%s%s]"},
45 { .value = 0, .str = NULL}
46 };
47
48 printf("Going to test vty_cmd_string_from_valstr()\n");
49
50 /* check against character strings that could break printf */
51
52 cmd = vty_cmd_string_from_valstr (NULL, printf_seq_vs, "[prefix%s%s%s%s%s]", "[sep%s%s%s%s%s]", "[end%s%s%s%s%s]", 1);
53 printf ("Tested with %%s-strings, resulting cmd = '%s'\n", cmd);
54 talloc_free (cmd);
55}
56
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020057static int do_vty_command(struct vty *vty, const char *cmd)
58{
59 vector vline;
60 int ret;
61
62 printf("Going to execute '%s'\n", cmd);
63 vline = cmd_make_strvec(cmd);
64 ret = cmd_execute_command(vline, vty, NULL, 0);
65 cmd_free_strvec(vline);
66 printf("Returned: %d, Current node: %d '%s'\n", ret, vty->node, cmd_prompt(vty->node));
67 return ret;
68}
69
70/* Override the implementation from telnet_interface.c */
71void vty_event(enum event event, int sock, struct vty *vty)
72{
73 last_vty_connection_event = event;
74
75 fprintf(stderr, "Got VTY event: %d\n", event);
76}
77
78static void test_node_tree_structure(void)
79{
80 struct vty_app_info vty_info = {
81 .name = "VtyTest",
82 .version = 0,
83 .go_parent_cb = NULL,
84 .is_config_node = NULL,
85 };
86
87 const struct log_info_cat default_categories[] = {};
88
89 const struct log_info log_info = {
90 .cat = default_categories,
91 .num_cat = ARRAY_SIZE(default_categories),
92 };
93
94 struct vty *vty;
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020095 int sock[2];
96
97 printf("Going to test VTY node tree structure\n");
98
99 /* Fake logging. */
100 osmo_init_logging(&log_info);
101
102 vty_init(&vty_info);
103
104 logging_vty_add_cmds(&log_info);
105
106 /* Fake connection. */
107 socketpair(AF_UNIX, SOCK_STREAM, 0, sock);
108
109 vty = vty_create(sock[0], NULL);
110
111 OSMO_ASSERT(vty != NULL);
112
113 OSMO_ASSERT(do_vty_command(vty, "enable") == CMD_SUCCESS);
114 OSMO_ASSERT(vty->node == ENABLE_NODE);
115
116 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
117 OSMO_ASSERT(vty->node == CONFIG_NODE);
118 OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
119 OSMO_ASSERT(vty->node == ENABLE_NODE);
120
121 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
122 OSMO_ASSERT(vty->node == CONFIG_NODE);
123 OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS);
124 OSMO_ASSERT(vty->node == ENABLE_NODE);
125
126 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
127 OSMO_ASSERT(vty->node == CONFIG_NODE);
128 OSMO_ASSERT(do_vty_command(vty, "log stderr") == CMD_SUCCESS);
129 OSMO_ASSERT(vty->node == CFG_LOG_NODE);
130 OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
131 OSMO_ASSERT(vty->node == CONFIG_NODE);
132 OSMO_ASSERT(do_vty_command(vty, "log stderr") == CMD_SUCCESS);
133 OSMO_ASSERT(vty->node == CFG_LOG_NODE);
134 OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS);
135 OSMO_ASSERT(vty->node == ENABLE_NODE);
136
137 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
138 OSMO_ASSERT(vty->node == CONFIG_NODE);
139 OSMO_ASSERT(do_vty_command(vty, "line vty") == CMD_SUCCESS);
140 OSMO_ASSERT(vty->node == VTY_NODE);
141 OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
142 OSMO_ASSERT(vty->node == CONFIG_NODE);
143 OSMO_ASSERT(do_vty_command(vty, "line vty") == CMD_SUCCESS);
144 OSMO_ASSERT(vty->node == VTY_NODE);
145 OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS);
146 OSMO_ASSERT(vty->node == ENABLE_NODE);
147
148
149 /* Check searching the parents nodes for matching commands. */
150 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
151 OSMO_ASSERT(vty->node == CONFIG_NODE);
152 OSMO_ASSERT(do_vty_command(vty, "log stderr") == CMD_SUCCESS);
153 OSMO_ASSERT(vty->node == CFG_LOG_NODE);
154 OSMO_ASSERT(do_vty_command(vty, "line vty") == CMD_SUCCESS);
155 OSMO_ASSERT(vty->node == VTY_NODE);
156 OSMO_ASSERT(do_vty_command(vty, "log stderr") == CMD_SUCCESS);
157 OSMO_ASSERT(vty->node == CFG_LOG_NODE);
158 OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS);
159 OSMO_ASSERT(vty->node == ENABLE_NODE);
160
161 /* Check for final 'exit' (connection close). */
162 OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
163 OSMO_ASSERT(vty->node == ENABLE_NODE);
164 OSMO_ASSERT(vty->status == VTY_CLOSE);
165
166 vty_close(vty);
167 OSMO_ASSERT(last_vty_connection_event == VTY_CLOSED);
168}
169
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200170int main(int argc, char **argv)
171{
172 test_cmd_string_from_valstr();
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200173 test_node_tree_structure();
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200174 printf("All tests passed\n");
175
176 return 0;
177}