blob: e54a205a94bd6e28f7ef2eb595034e508352432c [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
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +020027#include <osmocom/core/talloc.h>
28#include <osmocom/core/logging.h>
29#include <osmocom/core/utils.h>
30#include <osmocom/vty/misc.h>
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020031#include <osmocom/vty/vty.h>
32#include <osmocom/vty/command.h>
33#include <osmocom/vty/buffer.h>
34
35static enum event last_vty_connection_event = -1;
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +020036
37static void test_cmd_string_from_valstr(void)
38{
39 char *cmd;
40 const struct value_string printf_seq_vs[] = {
41 { .value = 42, .str = "[foo%s%s%s%s%s]"},
42 { .value = 43, .str = "[bar%s%s%s%s%s]"},
43 { .value = 0, .str = NULL}
44 };
45
46 printf("Going to test vty_cmd_string_from_valstr()\n");
47
48 /* check against character strings that could break printf */
49
50 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);
51 printf ("Tested with %%s-strings, resulting cmd = '%s'\n", cmd);
52 talloc_free (cmd);
53}
54
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020055static int do_vty_command(struct vty *vty, const char *cmd)
56{
57 vector vline;
58 int ret;
59
60 printf("Going to execute '%s'\n", cmd);
61 vline = cmd_make_strvec(cmd);
62 ret = cmd_execute_command(vline, vty, NULL, 0);
63 cmd_free_strvec(vline);
64 printf("Returned: %d, Current node: %d '%s'\n", ret, vty->node, cmd_prompt(vty->node));
65 return ret;
66}
67
68/* Override the implementation from telnet_interface.c */
69void vty_event(enum event event, int sock, struct vty *vty)
70{
71 last_vty_connection_event = event;
72
73 fprintf(stderr, "Got VTY event: %d\n", event);
74}
75
76static void test_node_tree_structure(void)
77{
78 struct vty_app_info vty_info = {
79 .name = "VtyTest",
80 .version = 0,
81 .go_parent_cb = NULL,
82 .is_config_node = NULL,
83 };
84
85 const struct log_info_cat default_categories[] = {};
86
87 const struct log_info log_info = {
88 .cat = default_categories,
89 .num_cat = ARRAY_SIZE(default_categories),
90 };
91
92 struct vty *vty;
93 vector vline;
94 int sock[2];
95
96 printf("Going to test VTY node tree structure\n");
97
98 /* Fake logging. */
99 osmo_init_logging(&log_info);
100
101 vty_init(&vty_info);
102
103 logging_vty_add_cmds(&log_info);
104
105 /* Fake connection. */
106 socketpair(AF_UNIX, SOCK_STREAM, 0, sock);
107
108 vty = vty_create(sock[0], NULL);
109
110 OSMO_ASSERT(vty != NULL);
111
112 OSMO_ASSERT(do_vty_command(vty, "enable") == CMD_SUCCESS);
113 OSMO_ASSERT(vty->node == ENABLE_NODE);
114
115 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
116 OSMO_ASSERT(vty->node == CONFIG_NODE);
117 OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
118 OSMO_ASSERT(vty->node == ENABLE_NODE);
119
120 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
121 OSMO_ASSERT(vty->node == CONFIG_NODE);
122 OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS);
123 OSMO_ASSERT(vty->node == ENABLE_NODE);
124
125 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
126 OSMO_ASSERT(vty->node == CONFIG_NODE);
127 OSMO_ASSERT(do_vty_command(vty, "log stderr") == CMD_SUCCESS);
128 OSMO_ASSERT(vty->node == CFG_LOG_NODE);
129 OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
130 OSMO_ASSERT(vty->node == CONFIG_NODE);
131 OSMO_ASSERT(do_vty_command(vty, "log stderr") == CMD_SUCCESS);
132 OSMO_ASSERT(vty->node == CFG_LOG_NODE);
133 OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS);
134 OSMO_ASSERT(vty->node == ENABLE_NODE);
135
136 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
137 OSMO_ASSERT(vty->node == CONFIG_NODE);
138 OSMO_ASSERT(do_vty_command(vty, "line vty") == CMD_SUCCESS);
139 OSMO_ASSERT(vty->node == VTY_NODE);
140 OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
141 OSMO_ASSERT(vty->node == CONFIG_NODE);
142 OSMO_ASSERT(do_vty_command(vty, "line vty") == CMD_SUCCESS);
143 OSMO_ASSERT(vty->node == VTY_NODE);
144 OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS);
145 OSMO_ASSERT(vty->node == ENABLE_NODE);
146
147
148 /* Check searching the parents nodes for matching commands. */
149 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
150 OSMO_ASSERT(vty->node == CONFIG_NODE);
151 OSMO_ASSERT(do_vty_command(vty, "log stderr") == CMD_SUCCESS);
152 OSMO_ASSERT(vty->node == CFG_LOG_NODE);
153 OSMO_ASSERT(do_vty_command(vty, "line vty") == CMD_SUCCESS);
154 OSMO_ASSERT(vty->node == VTY_NODE);
155 OSMO_ASSERT(do_vty_command(vty, "log stderr") == CMD_SUCCESS);
156 OSMO_ASSERT(vty->node == CFG_LOG_NODE);
157 OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS);
158 OSMO_ASSERT(vty->node == ENABLE_NODE);
159
160 /* Check for final 'exit' (connection close). */
161 OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
162 OSMO_ASSERT(vty->node == ENABLE_NODE);
163 OSMO_ASSERT(vty->status == VTY_CLOSE);
164
165 vty_close(vty);
166 OSMO_ASSERT(last_vty_connection_event == VTY_CLOSED);
167}
168
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200169int main(int argc, char **argv)
170{
171 test_cmd_string_from_valstr();
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200172 test_node_tree_structure();
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200173 printf("All tests passed\n");
174
175 return 0;
176}