Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 1 | /* (C) 2013 by sysmocom - s.f.m.c. GmbH, Author: Jacob Erlbeck <jerlbeck@sysmocom.de> |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 2 | * All Rights Reserved |
| 3 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 7 | * it under the terms of the GNU General Public License as published by |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * the Free Software Foundation; either version 2 of the License, or |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 24 | #include <errno.h> |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 25 | |
Jacob Erlbeck | 0c987bd | 2013-09-06 16:52:00 +0200 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | #include <sys/socket.h> |
| 28 | #include <sys/un.h> |
| 29 | |
Holger Hans Peter Freyther | 6ef71b0 | 2013-09-10 11:17:46 +0200 | [diff] [blame] | 30 | #include <osmocom/core/application.h> |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 31 | #include <osmocom/core/talloc.h> |
| 32 | #include <osmocom/core/logging.h> |
Jacob Erlbeck | be37fb7 | 2015-11-03 15:21:34 +0100 | [diff] [blame] | 33 | #include <osmocom/core/stats.h> |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 34 | #include <osmocom/core/utils.h> |
Holger Hans Peter Freyther | 2c9168c | 2013-10-10 20:21:33 +0200 | [diff] [blame] | 35 | #include <osmocom/core/signal.h> |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 36 | #include <osmocom/vty/misc.h> |
Jacob Erlbeck | 0c987bd | 2013-09-06 16:52:00 +0200 | [diff] [blame] | 37 | #include <osmocom/vty/vty.h> |
| 38 | #include <osmocom/vty/command.h> |
| 39 | #include <osmocom/vty/buffer.h> |
Holger Hans Peter Freyther | 6ef71b0 | 2013-09-10 11:17:46 +0200 | [diff] [blame] | 40 | #include <osmocom/vty/logging.h> |
Jacob Erlbeck | be37fb7 | 2015-11-03 15:21:34 +0100 | [diff] [blame] | 41 | #include <osmocom/vty/stats.h> |
Jacob Erlbeck | 0c987bd | 2013-09-06 16:52:00 +0200 | [diff] [blame] | 42 | |
| 43 | static enum event last_vty_connection_event = -1; |
Neels Hofmeyr | a829b45 | 2018-04-05 03:02:35 +0200 | [diff] [blame] | 44 | void *ctx = NULL; |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 45 | |
| 46 | static void test_cmd_string_from_valstr(void) |
| 47 | { |
| 48 | char *cmd; |
| 49 | const struct value_string printf_seq_vs[] = { |
| 50 | { .value = 42, .str = "[foo%s%s%s%s%s]"}, |
| 51 | { .value = 43, .str = "[bar%s%s%s%s%s]"}, |
| 52 | { .value = 0, .str = NULL} |
| 53 | }; |
| 54 | |
| 55 | printf("Going to test vty_cmd_string_from_valstr()\n"); |
| 56 | |
| 57 | /* check against character strings that could break printf */ |
| 58 | |
Neels Hofmeyr | a829b45 | 2018-04-05 03:02:35 +0200 | [diff] [blame] | 59 | cmd = vty_cmd_string_from_valstr (ctx, printf_seq_vs, "[prefix%s%s%s%s%s]", "[sep%s%s%s%s%s]", "[end%s%s%s%s%s]", 1); |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 60 | printf ("Tested with %%s-strings, resulting cmd = '%s'\n", cmd); |
| 61 | talloc_free (cmd); |
| 62 | } |
| 63 | |
Jacob Erlbeck | 0c987bd | 2013-09-06 16:52:00 +0200 | [diff] [blame] | 64 | static int do_vty_command(struct vty *vty, const char *cmd) |
| 65 | { |
| 66 | vector vline; |
| 67 | int ret; |
| 68 | |
| 69 | printf("Going to execute '%s'\n", cmd); |
| 70 | vline = cmd_make_strvec(cmd); |
| 71 | ret = cmd_execute_command(vline, vty, NULL, 0); |
| 72 | cmd_free_strvec(vline); |
| 73 | printf("Returned: %d, Current node: %d '%s'\n", ret, vty->node, cmd_prompt(vty->node)); |
| 74 | return ret; |
| 75 | } |
| 76 | |
Holger Hans Peter Freyther | 2c9168c | 2013-10-10 20:21:33 +0200 | [diff] [blame] | 77 | /* Handle the events from telnet_interface.c */ |
| 78 | static int vty_event_cb(unsigned int subsys, unsigned int signal, |
| 79 | void *handler_data, void *_signal_data) |
Jacob Erlbeck | 0c987bd | 2013-09-06 16:52:00 +0200 | [diff] [blame] | 80 | { |
Holger Hans Peter Freyther | 2c9168c | 2013-10-10 20:21:33 +0200 | [diff] [blame] | 81 | struct vty_signal_data *signal_data; |
Jacob Erlbeck | 0c987bd | 2013-09-06 16:52:00 +0200 | [diff] [blame] | 82 | |
Holger Hans Peter Freyther | 2c9168c | 2013-10-10 20:21:33 +0200 | [diff] [blame] | 83 | if (subsys != SS_L_VTY) |
| 84 | return 0; |
| 85 | if (signal != S_VTY_EVENT) |
| 86 | return 0; |
| 87 | |
| 88 | signal_data = _signal_data; |
| 89 | last_vty_connection_event = signal_data->event; |
| 90 | |
| 91 | fprintf(stderr, "Got VTY event: %d\n", signal_data->event); |
| 92 | return 0; |
Jacob Erlbeck | 0c987bd | 2013-09-06 16:52:00 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Jacob Erlbeck | ca6602f | 2015-11-03 13:47:11 +0100 | [diff] [blame] | 95 | struct vty_test { |
| 96 | int sock[2]; |
| 97 | }; |
| 98 | |
| 99 | static struct vty* create_test_vty(struct vty_test *data) |
| 100 | { |
| 101 | struct vty *vty; |
| 102 | /* Fake connection. */ |
| 103 | socketpair(AF_UNIX, SOCK_STREAM, 0, data->sock); |
| 104 | |
| 105 | vty = vty_create(data->sock[0], NULL); |
| 106 | OSMO_ASSERT(vty != NULL); |
| 107 | OSMO_ASSERT(vty->status != VTY_CLOSE); |
| 108 | |
| 109 | return vty; |
| 110 | } |
| 111 | |
| 112 | static void destroy_test_vty(struct vty_test *data, struct vty *vty) |
| 113 | { |
| 114 | vty_close(vty); |
| 115 | OSMO_ASSERT(last_vty_connection_event == VTY_CLOSED); |
| 116 | } |
| 117 | |
Jacob Erlbeck | 0c987bd | 2013-09-06 16:52:00 +0200 | [diff] [blame] | 118 | static void test_node_tree_structure(void) |
| 119 | { |
Jacob Erlbeck | ca6602f | 2015-11-03 13:47:11 +0100 | [diff] [blame] | 120 | struct vty_test test; |
Jacob Erlbeck | 0c987bd | 2013-09-06 16:52:00 +0200 | [diff] [blame] | 121 | struct vty *vty; |
Jacob Erlbeck | 0c987bd | 2013-09-06 16:52:00 +0200 | [diff] [blame] | 122 | |
| 123 | printf("Going to test VTY node tree structure\n"); |
Jacob Erlbeck | ca6602f | 2015-11-03 13:47:11 +0100 | [diff] [blame] | 124 | vty = create_test_vty(&test); |
Jacob Erlbeck | 0c987bd | 2013-09-06 16:52:00 +0200 | [diff] [blame] | 125 | |
| 126 | OSMO_ASSERT(do_vty_command(vty, "enable") == CMD_SUCCESS); |
| 127 | OSMO_ASSERT(vty->node == ENABLE_NODE); |
| 128 | |
| 129 | OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS); |
| 130 | OSMO_ASSERT(vty->node == CONFIG_NODE); |
| 131 | OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS); |
| 132 | OSMO_ASSERT(vty->node == ENABLE_NODE); |
| 133 | |
| 134 | OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS); |
| 135 | OSMO_ASSERT(vty->node == CONFIG_NODE); |
| 136 | OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS); |
| 137 | OSMO_ASSERT(vty->node == ENABLE_NODE); |
| 138 | |
| 139 | OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS); |
| 140 | OSMO_ASSERT(vty->node == CONFIG_NODE); |
| 141 | OSMO_ASSERT(do_vty_command(vty, "log stderr") == CMD_SUCCESS); |
| 142 | OSMO_ASSERT(vty->node == CFG_LOG_NODE); |
| 143 | OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS); |
| 144 | OSMO_ASSERT(vty->node == CONFIG_NODE); |
| 145 | OSMO_ASSERT(do_vty_command(vty, "log stderr") == CMD_SUCCESS); |
| 146 | OSMO_ASSERT(vty->node == CFG_LOG_NODE); |
| 147 | OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS); |
| 148 | OSMO_ASSERT(vty->node == ENABLE_NODE); |
| 149 | |
| 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, "line vty") == CMD_SUCCESS); |
| 153 | OSMO_ASSERT(vty->node == VTY_NODE); |
| 154 | OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS); |
| 155 | OSMO_ASSERT(vty->node == CONFIG_NODE); |
| 156 | OSMO_ASSERT(do_vty_command(vty, "line vty") == CMD_SUCCESS); |
| 157 | OSMO_ASSERT(vty->node == VTY_NODE); |
| 158 | OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS); |
| 159 | OSMO_ASSERT(vty->node == ENABLE_NODE); |
| 160 | |
| 161 | |
Neels Hofmeyr | d64b6ae | 2017-09-07 04:52:05 +0200 | [diff] [blame] | 162 | /* Check for not searching the parent node for matching commands. */ |
Jacob Erlbeck | 0c987bd | 2013-09-06 16:52:00 +0200 | [diff] [blame] | 163 | OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS); |
| 164 | OSMO_ASSERT(vty->node == CONFIG_NODE); |
| 165 | OSMO_ASSERT(do_vty_command(vty, "log stderr") == CMD_SUCCESS); |
| 166 | OSMO_ASSERT(vty->node == CFG_LOG_NODE); |
Neels Hofmeyr | d64b6ae | 2017-09-07 04:52:05 +0200 | [diff] [blame] | 167 | OSMO_ASSERT(do_vty_command(vty, "line vty") == CMD_ERR_NO_MATCH); |
Jacob Erlbeck | 0c987bd | 2013-09-06 16:52:00 +0200 | [diff] [blame] | 168 | OSMO_ASSERT(vty->node == CFG_LOG_NODE); |
| 169 | OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS); |
| 170 | OSMO_ASSERT(vty->node == ENABLE_NODE); |
| 171 | |
| 172 | /* Check for final 'exit' (connection close). */ |
| 173 | OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS); |
| 174 | OSMO_ASSERT(vty->node == ENABLE_NODE); |
| 175 | OSMO_ASSERT(vty->status == VTY_CLOSE); |
| 176 | |
Jacob Erlbeck | ca6602f | 2015-11-03 13:47:11 +0100 | [diff] [blame] | 177 | destroy_test_vty(&test, vty); |
Jacob Erlbeck | 0c987bd | 2013-09-06 16:52:00 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Jacob Erlbeck | be37fb7 | 2015-11-03 15:21:34 +0100 | [diff] [blame] | 180 | static void check_srep_vty_config(struct vty* vty, |
| 181 | struct osmo_stats_reporter *srep) |
| 182 | { |
| 183 | OSMO_ASSERT(srep->enabled == 0); |
| 184 | |
| 185 | OSMO_ASSERT(do_vty_command(vty, "prefix myprefix") == CMD_SUCCESS); |
| 186 | OSMO_ASSERT(srep->name_prefix != NULL); |
| 187 | OSMO_ASSERT(strcmp(srep->name_prefix, "myprefix") == 0); |
| 188 | OSMO_ASSERT(do_vty_command(vty, "no prefix") == CMD_SUCCESS); |
| 189 | OSMO_ASSERT(srep->name_prefix == NULL || strlen(srep->name_prefix) == 0); |
| 190 | |
| 191 | OSMO_ASSERT(srep->max_class == OSMO_STATS_CLASS_GLOBAL); |
| 192 | OSMO_ASSERT(do_vty_command(vty, "level peer") == CMD_SUCCESS); |
| 193 | OSMO_ASSERT(srep->max_class == OSMO_STATS_CLASS_PEER); |
| 194 | OSMO_ASSERT(do_vty_command(vty, "level subscriber") == CMD_SUCCESS); |
| 195 | OSMO_ASSERT(srep->max_class == OSMO_STATS_CLASS_SUBSCRIBER); |
| 196 | OSMO_ASSERT(do_vty_command(vty, "level global") == CMD_SUCCESS); |
| 197 | OSMO_ASSERT(srep->max_class == OSMO_STATS_CLASS_GLOBAL); |
| 198 | OSMO_ASSERT(do_vty_command(vty, "level foobar") == CMD_ERR_NO_MATCH); |
| 199 | |
| 200 | if (srep->have_net_config) { |
| 201 | OSMO_ASSERT(do_vty_command(vty, "remote-ip 127.0.0.99") == |
| 202 | CMD_SUCCESS); |
| 203 | OSMO_ASSERT(srep->dest_addr_str && |
| 204 | strcmp(srep->dest_addr_str, "127.0.0.99") == 0); |
| 205 | OSMO_ASSERT(do_vty_command(vty, "remote-ip 678.0.0.99") == |
| 206 | CMD_WARNING); |
| 207 | OSMO_ASSERT(srep->dest_addr_str && |
| 208 | strcmp(srep->dest_addr_str, "127.0.0.99") == 0); |
| 209 | |
| 210 | OSMO_ASSERT(do_vty_command(vty, "remote-port 12321") == |
| 211 | CMD_SUCCESS); |
| 212 | OSMO_ASSERT(srep->dest_port == 12321); |
| 213 | |
| 214 | OSMO_ASSERT(srep->bind_addr_str == NULL); |
| 215 | OSMO_ASSERT(do_vty_command(vty, "local-ip 127.0.0.98") == |
| 216 | CMD_SUCCESS); |
| 217 | OSMO_ASSERT(srep->bind_addr_str && |
| 218 | strcmp(srep->bind_addr_str, "127.0.0.98") == 0); |
| 219 | OSMO_ASSERT(do_vty_command(vty, "no local-ip") == CMD_SUCCESS); |
| 220 | OSMO_ASSERT(srep->bind_addr_str == NULL); |
| 221 | |
| 222 | OSMO_ASSERT(srep->mtu == 0); |
| 223 | OSMO_ASSERT(do_vty_command(vty, "mtu 987") == CMD_SUCCESS); |
| 224 | OSMO_ASSERT(srep->mtu == 987); |
| 225 | OSMO_ASSERT(do_vty_command(vty, "no mtu") == CMD_SUCCESS); |
| 226 | OSMO_ASSERT(srep->mtu == 0); |
| 227 | }; |
| 228 | |
| 229 | OSMO_ASSERT(do_vty_command(vty, "enable") == CMD_SUCCESS); |
| 230 | OSMO_ASSERT(srep->enabled != 0); |
| 231 | OSMO_ASSERT(do_vty_command(vty, "disable") == CMD_SUCCESS); |
| 232 | OSMO_ASSERT(srep->enabled == 0); |
| 233 | } |
| 234 | |
| 235 | static void test_stats_vty(void) |
| 236 | { |
| 237 | struct osmo_stats_reporter *srep; |
| 238 | struct vty_test test; |
| 239 | struct vty *vty; |
| 240 | |
| 241 | printf("Going to test VTY configuration of the stats subsystem\n"); |
| 242 | vty = create_test_vty(&test); |
| 243 | |
| 244 | /* Go to config node */ |
| 245 | OSMO_ASSERT(do_vty_command(vty, "enable") == CMD_SUCCESS); |
| 246 | OSMO_ASSERT(vty->node == ENABLE_NODE); |
| 247 | OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS); |
| 248 | OSMO_ASSERT(vty->node == CONFIG_NODE); |
| 249 | |
| 250 | /* Try to create invalid reporter */ |
| 251 | OSMO_ASSERT(do_vty_command(vty, "stats reporter foobar") == |
| 252 | CMD_ERR_NO_MATCH); |
| 253 | |
| 254 | /* Set reporting interval */ |
| 255 | OSMO_ASSERT(do_vty_command(vty, "stats interval 42") == CMD_SUCCESS); |
| 256 | OSMO_ASSERT(osmo_stats_config->interval == 42); |
| 257 | |
| 258 | /* Create log reporter */ |
| 259 | srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL); |
| 260 | OSMO_ASSERT(srep == NULL); |
| 261 | OSMO_ASSERT(do_vty_command(vty, "stats reporter log") == CMD_SUCCESS); |
| 262 | OSMO_ASSERT(vty->node == CFG_STATS_NODE); |
| 263 | srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL); |
| 264 | OSMO_ASSERT(srep != NULL); |
| 265 | OSMO_ASSERT(srep->type == OSMO_STATS_REPORTER_LOG); |
| 266 | check_srep_vty_config(vty, srep); |
| 267 | OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS); |
| 268 | OSMO_ASSERT(vty->node == CONFIG_NODE); |
| 269 | |
| 270 | /* Create statsd reporter */ |
| 271 | srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL); |
| 272 | OSMO_ASSERT(srep == NULL); |
| 273 | OSMO_ASSERT(do_vty_command(vty, "stats reporter statsd") == CMD_SUCCESS); |
| 274 | OSMO_ASSERT(vty->node == CFG_STATS_NODE); |
| 275 | srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL); |
| 276 | OSMO_ASSERT(srep != NULL); |
| 277 | OSMO_ASSERT(srep->type == OSMO_STATS_REPORTER_STATSD); |
| 278 | check_srep_vty_config(vty, srep); |
| 279 | OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS); |
| 280 | OSMO_ASSERT(vty->node == CONFIG_NODE); |
| 281 | |
| 282 | /* Destroy log reporter */ |
| 283 | OSMO_ASSERT(osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL)); |
| 284 | OSMO_ASSERT(do_vty_command(vty, "no stats reporter log") == CMD_SUCCESS); |
| 285 | OSMO_ASSERT(!osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL)); |
| 286 | |
| 287 | /* Destroy statsd reporter */ |
| 288 | OSMO_ASSERT(osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL)); |
| 289 | OSMO_ASSERT(do_vty_command(vty, "no stats reporter statsd") == CMD_SUCCESS); |
| 290 | OSMO_ASSERT(!osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL)); |
| 291 | |
| 292 | destroy_test_vty(&test, vty); |
| 293 | } |
| 294 | |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 295 | void test_exit_by_indent(const char *fname, int expect_rc) |
| 296 | { |
| 297 | int rc; |
| 298 | printf("reading file %s, expecting rc=%d\n", fname, expect_rc); |
| 299 | rc = vty_read_config_file(fname, NULL); |
| 300 | printf("got rc=%d\n", rc); |
| 301 | OSMO_ASSERT(rc == expect_rc); |
| 302 | } |
| 303 | |
Neels Hofmeyr | b022c86 | 2017-09-20 01:49:11 +0200 | [diff] [blame] | 304 | enum test_nodes { |
| 305 | LEVEL1_NODE = _LAST_OSMOVTY_NODE + 1, |
| 306 | LEVEL2_NODE, |
| 307 | LEVEL3_NODE, |
| 308 | }; |
| 309 | |
| 310 | struct cmd_node level1_node = { |
| 311 | LEVEL1_NODE, |
| 312 | "%s(config-level1)# ", |
| 313 | 1 |
| 314 | }; |
| 315 | |
| 316 | struct cmd_node level2_node = { |
| 317 | LEVEL2_NODE, |
| 318 | "%s(config-level1-level2)# ", |
| 319 | 1 |
| 320 | }; |
| 321 | |
| 322 | struct cmd_node level3_node = { |
| 323 | LEVEL3_NODE, |
| 324 | "%s(config-level1-level2-level3)# ", |
| 325 | 1 |
| 326 | }; |
| 327 | |
| 328 | DEFUN(cfg_level1, cfg_level1_cmd, |
| 329 | "level1 [MARKER]", |
| 330 | "Level 1 node for VTY testing purposes\n" |
| 331 | "optional string to mark the line for test debugging\n") |
| 332 | { |
| 333 | vty->index = NULL; |
| 334 | vty->node = LEVEL1_NODE; |
| 335 | printf("called level1 node %s\n", argc? argv[0] : ""); |
| 336 | return CMD_SUCCESS; |
| 337 | } |
| 338 | |
| 339 | DEFUN(cfg_level1_child, cfg_level1_child_cmd, |
| 340 | "child1 [MARKER]", |
| 341 | "Level 1 child cmd for VTY testing purposes\n" |
| 342 | "optional string to mark the line for test debugging\n") |
| 343 | { |
| 344 | printf("called level1 child cmd %s\n", argc? argv[0] : ""); |
| 345 | return CMD_SUCCESS; |
| 346 | } |
| 347 | |
| 348 | DEFUN(cfg_level2, cfg_level2_cmd, |
| 349 | "level2 [MARKER]", |
| 350 | "Level 2 node for VTY testing purposes\n" |
| 351 | "optional string to mark the line for test debugging\n") |
| 352 | { |
| 353 | vty->index = NULL; |
| 354 | vty->node = LEVEL2_NODE; |
| 355 | printf("called level2 node %s\n", argc? argv[0] : ""); |
| 356 | return CMD_SUCCESS; |
| 357 | } |
| 358 | |
| 359 | DEFUN(cfg_level2_child, cfg_level2_child_cmd, |
| 360 | "child2 [MARKER]", |
| 361 | "Level 2 child cmd for VTY testing purposes\n" |
| 362 | "optional string to mark the line for test debugging\n") |
| 363 | { |
| 364 | printf("called level2 child cmd %s\n", argc? argv[0] : ""); |
| 365 | return CMD_SUCCESS; |
| 366 | } |
| 367 | |
| 368 | DEFUN(cfg_level3, cfg_level3_cmd, |
| 369 | "level3 [MARKER]", |
| 370 | "Level 3 node for VTY testing purposes\n" |
| 371 | "optional string to mark the line for test debugging\n") |
| 372 | { |
| 373 | vty->index = NULL; |
| 374 | vty->node = LEVEL3_NODE; |
| 375 | printf("called level3 node %s\n", argc? argv[0] : ""); |
| 376 | return CMD_SUCCESS; |
| 377 | } |
| 378 | |
| 379 | DEFUN(cfg_level3_child, cfg_level3_child_cmd, |
| 380 | "child3 [MARKER]", |
| 381 | "Level 3 child cmd for VTY testing purposes\n" |
| 382 | "optional string to mark the line for test debugging\n") |
| 383 | { |
| 384 | printf("called level3 child cmd %s\n", argc? argv[0] : ""); |
| 385 | return CMD_SUCCESS; |
| 386 | } |
| 387 | |
| 388 | void test_vty_add_cmds() |
| 389 | { |
| 390 | install_element(CONFIG_NODE, &cfg_level1_cmd); |
| 391 | install_node(&level1_node, NULL); |
Neels Hofmeyr | b022c86 | 2017-09-20 01:49:11 +0200 | [diff] [blame] | 392 | install_element(LEVEL1_NODE, &cfg_level1_child_cmd); |
| 393 | install_element(LEVEL1_NODE, &cfg_level2_cmd); |
| 394 | |
| 395 | install_node(&level2_node, NULL); |
Neels Hofmeyr | b022c86 | 2017-09-20 01:49:11 +0200 | [diff] [blame] | 396 | install_element(LEVEL2_NODE, &cfg_level2_child_cmd); |
| 397 | install_element(LEVEL2_NODE, &cfg_level3_cmd); |
| 398 | |
| 399 | install_node(&level3_node, NULL); |
Neels Hofmeyr | b022c86 | 2017-09-20 01:49:11 +0200 | [diff] [blame] | 400 | install_element(LEVEL3_NODE, &cfg_level3_child_cmd); |
| 401 | } |
| 402 | |
| 403 | static int go_parent_cb(struct vty *vty) |
| 404 | { |
| 405 | /* |
| 406 | * - For the interactive VTY tests above, it is expected to bounce back to |
| 407 | * the CONFIG_NODE. Hence do so in go_parent_cb(). |
| 408 | * - In the config file parsing tests, setting vty->node in go_parent_cb() has no |
| 409 | * effect, because we will subsequently pop a parent node from the parent stack |
| 410 | * and override to go to the node that was recorded as the actual parent. |
| 411 | */ |
| 412 | vty->node = CONFIG_NODE; |
| 413 | vty->index = NULL; |
| 414 | return 0; |
| 415 | } |
| 416 | |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 417 | int main(int argc, char **argv) |
| 418 | { |
Jacob Erlbeck | ca6602f | 2015-11-03 13:47:11 +0100 | [diff] [blame] | 419 | struct vty_app_info vty_info = { |
| 420 | .name = "VtyTest", |
| 421 | .version = 0, |
Neels Hofmeyr | b022c86 | 2017-09-20 01:49:11 +0200 | [diff] [blame] | 422 | .go_parent_cb = go_parent_cb, |
Jacob Erlbeck | ca6602f | 2015-11-03 13:47:11 +0100 | [diff] [blame] | 423 | .is_config_node = NULL, |
| 424 | }; |
| 425 | |
| 426 | const struct log_info_cat default_categories[] = {}; |
| 427 | |
| 428 | const struct log_info log_info = { |
| 429 | .cat = default_categories, |
| 430 | .num_cat = ARRAY_SIZE(default_categories), |
| 431 | }; |
Neels Hofmeyr | a829b45 | 2018-04-05 03:02:35 +0200 | [diff] [blame] | 432 | void *stats_ctx; |
| 433 | |
| 434 | ctx = talloc_named_const(NULL, 0, "stats test context"); |
| 435 | stats_ctx = talloc_named_const(ctx, 1, "stats test context"); |
Jacob Erlbeck | be37fb7 | 2015-11-03 15:21:34 +0100 | [diff] [blame] | 436 | |
Holger Hans Peter Freyther | 2c9168c | 2013-10-10 20:21:33 +0200 | [diff] [blame] | 437 | osmo_signal_register_handler(SS_L_VTY, vty_event_cb, NULL); |
| 438 | |
Jacob Erlbeck | ca6602f | 2015-11-03 13:47:11 +0100 | [diff] [blame] | 439 | /* Fake logging. */ |
Neels Hofmeyr | a829b45 | 2018-04-05 03:02:35 +0200 | [diff] [blame] | 440 | osmo_init_logging2(ctx, &log_info); |
Jacob Erlbeck | ca6602f | 2015-11-03 13:47:11 +0100 | [diff] [blame] | 441 | |
Jacob Erlbeck | be37fb7 | 2015-11-03 15:21:34 +0100 | [diff] [blame] | 442 | /* Init stats */ |
| 443 | osmo_stats_init(stats_ctx); |
| 444 | |
Jacob Erlbeck | ca6602f | 2015-11-03 13:47:11 +0100 | [diff] [blame] | 445 | vty_init(&vty_info); |
| 446 | |
| 447 | /* Setup VTY commands */ |
Max | c65c5b4 | 2017-03-15 13:20:23 +0100 | [diff] [blame] | 448 | logging_vty_add_cmds(); |
Jacob Erlbeck | be37fb7 | 2015-11-03 15:21:34 +0100 | [diff] [blame] | 449 | osmo_stats_vty_add_cmds(); |
Jacob Erlbeck | ca6602f | 2015-11-03 13:47:11 +0100 | [diff] [blame] | 450 | |
Neels Hofmeyr | b022c86 | 2017-09-20 01:49:11 +0200 | [diff] [blame] | 451 | test_vty_add_cmds(); |
| 452 | |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 453 | test_cmd_string_from_valstr(); |
Jacob Erlbeck | 0c987bd | 2013-09-06 16:52:00 +0200 | [diff] [blame] | 454 | test_node_tree_structure(); |
Jacob Erlbeck | be37fb7 | 2015-11-03 15:21:34 +0100 | [diff] [blame] | 455 | test_stats_vty(); |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 456 | test_exit_by_indent("ok.cfg", 0); |
| 457 | test_exit_by_indent("ok_more_spaces.cfg", 0); |
| 458 | test_exit_by_indent("ok_tabs.cfg", 0); |
| 459 | test_exit_by_indent("ok_tabs_and_spaces.cfg", 0); |
| 460 | test_exit_by_indent("ok_ignore_comment.cfg", 0); |
| 461 | test_exit_by_indent("ok_ignore_blank.cfg", 0); |
| 462 | test_exit_by_indent("fail_not_de-indented.cfg", -EINVAL); |
| 463 | test_exit_by_indent("fail_too_much_indent.cfg", -EINVAL); |
| 464 | test_exit_by_indent("fail_tabs_and_spaces.cfg", -EINVAL); |
| 465 | test_exit_by_indent("ok_indented_root.cfg", 0); |
Neels Hofmeyr | 4306363 | 2017-09-19 23:54:01 +0200 | [diff] [blame] | 466 | test_exit_by_indent("ok_empty_parent.cfg", 0); |
Jacob Erlbeck | be37fb7 | 2015-11-03 15:21:34 +0100 | [diff] [blame] | 467 | |
| 468 | /* Leak check */ |
| 469 | OSMO_ASSERT(talloc_total_blocks(stats_ctx) == 1); |
Jacob Erlbeck | ca6602f | 2015-11-03 13:47:11 +0100 | [diff] [blame] | 470 | |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 471 | printf("All tests passed\n"); |
| 472 | |
| 473 | return 0; |
| 474 | } |