blob: 1139638d86aa42ba7dde75c70bbeb7e7bcd20d80 [file] [log] [blame]
Harald Weltee08da972017-11-13 01:00:26 +09001/* (C) 2013 by sysmocom - s.f.m.c. GmbH, Author: Jacob Erlbeck <jerlbeck@sysmocom.de>
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +02002 * All Rights Reserved
3 *
Harald Weltee08da972017-11-13 01:00:26 +09004 * SPDX-License-Identifier: GPL-2.0+
5 *
6 * This program is free software; you can redistribute it and/or modify
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +02007 * it under the terms of the GNU General Public License as published by
Harald Weltee08da972017-11-13 01:00:26 +09008 * the Free Software Foundation; either version 2 of the License, or
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +02009 * (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 Hofmeyr4a31ffa2017-09-07 03:08:06 +020024#include <errno.h>
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +020025
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020026#include <sys/types.h>
27#include <sys/socket.h>
28#include <sys/un.h>
29
Holger Hans Peter Freyther6ef71b02013-09-10 11:17:46 +020030#include <osmocom/core/application.h>
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +020031#include <osmocom/core/talloc.h>
Vadim Yanitskiy4abda9e2019-11-21 00:19:36 +070032#include <osmocom/core/logging_internal.h>
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +020033#include <osmocom/core/logging.h>
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +010034#include <osmocom/core/stats.h>
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +020035#include <osmocom/core/utils.h>
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +020036#include <osmocom/core/signal.h>
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +020037#include <osmocom/vty/misc.h>
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020038#include <osmocom/vty/vty.h>
39#include <osmocom/vty/command.h>
40#include <osmocom/vty/buffer.h>
Holger Hans Peter Freyther6ef71b02013-09-10 11:17:46 +020041#include <osmocom/vty/logging.h>
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +010042#include <osmocom/vty/stats.h>
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020043
44static enum event last_vty_connection_event = -1;
Neels Hofmeyra829b452018-04-05 03:02:35 +020045void *ctx = NULL;
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +020046
47static void test_cmd_string_from_valstr(void)
48{
49 char *cmd;
50 const struct value_string printf_seq_vs[] = {
51 { .value = 42, .str = "[foo%s%s%s%s%s]"},
52 { .value = 43, .str = "[bar%s%s%s%s%s]"},
53 { .value = 0, .str = NULL}
54 };
55
56 printf("Going to test vty_cmd_string_from_valstr()\n");
57
58 /* check against character strings that could break printf */
59
Neels Hofmeyra829b452018-04-05 03:02:35 +020060 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 Erlbeckae15a2c2013-08-06 14:29:14 +020061 printf ("Tested with %%s-strings, resulting cmd = '%s'\n", cmd);
62 talloc_free (cmd);
63}
64
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020065static int do_vty_command(struct vty *vty, const char *cmd)
66{
67 vector vline;
68 int ret;
69
70 printf("Going to execute '%s'\n", cmd);
71 vline = cmd_make_strvec(cmd);
72 ret = cmd_execute_command(vline, vty, NULL, 0);
73 cmd_free_strvec(vline);
74 printf("Returned: %d, Current node: %d '%s'\n", ret, vty->node, cmd_prompt(vty->node));
75 return ret;
76}
77
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +020078/* Handle the events from telnet_interface.c */
79static int vty_event_cb(unsigned int subsys, unsigned int signal,
80 void *handler_data, void *_signal_data)
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020081{
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +020082 struct vty_signal_data *signal_data;
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020083
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +020084 if (subsys != SS_L_VTY)
85 return 0;
86 if (signal != S_VTY_EVENT)
87 return 0;
88
89 signal_data = _signal_data;
90 last_vty_connection_event = signal_data->event;
91
92 fprintf(stderr, "Got VTY event: %d\n", signal_data->event);
93 return 0;
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020094}
95
Jacob Erlbeckca6602f2015-11-03 13:47:11 +010096struct vty_test {
97 int sock[2];
98};
99
100static struct vty* create_test_vty(struct vty_test *data)
101{
102 struct vty *vty;
103 /* Fake connection. */
104 socketpair(AF_UNIX, SOCK_STREAM, 0, data->sock);
105
106 vty = vty_create(data->sock[0], NULL);
107 OSMO_ASSERT(vty != NULL);
108 OSMO_ASSERT(vty->status != VTY_CLOSE);
109
110 return vty;
111}
112
113static void destroy_test_vty(struct vty_test *data, struct vty *vty)
114{
115 vty_close(vty);
116 OSMO_ASSERT(last_vty_connection_event == VTY_CLOSED);
117}
118
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200119static void test_node_tree_structure(void)
120{
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100121 struct vty_test test;
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200122 struct vty *vty;
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200123
124 printf("Going to test VTY node tree structure\n");
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100125 vty = create_test_vty(&test);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200126
127 OSMO_ASSERT(do_vty_command(vty, "enable") == CMD_SUCCESS);
128 OSMO_ASSERT(vty->node == ENABLE_NODE);
129
130 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
131 OSMO_ASSERT(vty->node == CONFIG_NODE);
132 OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
133 OSMO_ASSERT(vty->node == ENABLE_NODE);
134
135 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
136 OSMO_ASSERT(vty->node == CONFIG_NODE);
137 OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS);
138 OSMO_ASSERT(vty->node == ENABLE_NODE);
139
140 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
141 OSMO_ASSERT(vty->node == CONFIG_NODE);
142 OSMO_ASSERT(do_vty_command(vty, "log stderr") == CMD_SUCCESS);
143 OSMO_ASSERT(vty->node == CFG_LOG_NODE);
144 OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
145 OSMO_ASSERT(vty->node == CONFIG_NODE);
146 OSMO_ASSERT(do_vty_command(vty, "log stderr") == CMD_SUCCESS);
147 OSMO_ASSERT(vty->node == CFG_LOG_NODE);
148 OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS);
149 OSMO_ASSERT(vty->node == ENABLE_NODE);
150
151 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
152 OSMO_ASSERT(vty->node == CONFIG_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, "exit") == CMD_SUCCESS);
156 OSMO_ASSERT(vty->node == CONFIG_NODE);
157 OSMO_ASSERT(do_vty_command(vty, "line vty") == CMD_SUCCESS);
158 OSMO_ASSERT(vty->node == VTY_NODE);
159 OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS);
160 OSMO_ASSERT(vty->node == ENABLE_NODE);
161
162
Neels Hofmeyrd64b6ae2017-09-07 04:52:05 +0200163 /* Check for not searching the parent node for matching commands. */
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200164 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
165 OSMO_ASSERT(vty->node == CONFIG_NODE);
166 OSMO_ASSERT(do_vty_command(vty, "log stderr") == CMD_SUCCESS);
167 OSMO_ASSERT(vty->node == CFG_LOG_NODE);
Neels Hofmeyrd64b6ae2017-09-07 04:52:05 +0200168 OSMO_ASSERT(do_vty_command(vty, "line vty") == CMD_ERR_NO_MATCH);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200169 OSMO_ASSERT(vty->node == CFG_LOG_NODE);
170 OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS);
171 OSMO_ASSERT(vty->node == ENABLE_NODE);
172
173 /* Check for final 'exit' (connection close). */
174 OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
175 OSMO_ASSERT(vty->node == ENABLE_NODE);
176 OSMO_ASSERT(vty->status == VTY_CLOSE);
177
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100178 destroy_test_vty(&test, vty);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200179}
180
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100181static void check_srep_vty_config(struct vty* vty,
182 struct osmo_stats_reporter *srep)
183{
184 OSMO_ASSERT(srep->enabled == 0);
185
186 OSMO_ASSERT(do_vty_command(vty, "prefix myprefix") == CMD_SUCCESS);
187 OSMO_ASSERT(srep->name_prefix != NULL);
188 OSMO_ASSERT(strcmp(srep->name_prefix, "myprefix") == 0);
189 OSMO_ASSERT(do_vty_command(vty, "no prefix") == CMD_SUCCESS);
190 OSMO_ASSERT(srep->name_prefix == NULL || strlen(srep->name_prefix) == 0);
191
192 OSMO_ASSERT(srep->max_class == OSMO_STATS_CLASS_GLOBAL);
193 OSMO_ASSERT(do_vty_command(vty, "level peer") == CMD_SUCCESS);
194 OSMO_ASSERT(srep->max_class == OSMO_STATS_CLASS_PEER);
195 OSMO_ASSERT(do_vty_command(vty, "level subscriber") == CMD_SUCCESS);
196 OSMO_ASSERT(srep->max_class == OSMO_STATS_CLASS_SUBSCRIBER);
197 OSMO_ASSERT(do_vty_command(vty, "level global") == CMD_SUCCESS);
198 OSMO_ASSERT(srep->max_class == OSMO_STATS_CLASS_GLOBAL);
199 OSMO_ASSERT(do_vty_command(vty, "level foobar") == CMD_ERR_NO_MATCH);
200
201 if (srep->have_net_config) {
202 OSMO_ASSERT(do_vty_command(vty, "remote-ip 127.0.0.99") ==
203 CMD_SUCCESS);
204 OSMO_ASSERT(srep->dest_addr_str &&
205 strcmp(srep->dest_addr_str, "127.0.0.99") == 0);
206 OSMO_ASSERT(do_vty_command(vty, "remote-ip 678.0.0.99") ==
207 CMD_WARNING);
208 OSMO_ASSERT(srep->dest_addr_str &&
209 strcmp(srep->dest_addr_str, "127.0.0.99") == 0);
210
211 OSMO_ASSERT(do_vty_command(vty, "remote-port 12321") ==
212 CMD_SUCCESS);
213 OSMO_ASSERT(srep->dest_port == 12321);
214
215 OSMO_ASSERT(srep->bind_addr_str == NULL);
216 OSMO_ASSERT(do_vty_command(vty, "local-ip 127.0.0.98") ==
217 CMD_SUCCESS);
218 OSMO_ASSERT(srep->bind_addr_str &&
219 strcmp(srep->bind_addr_str, "127.0.0.98") == 0);
220 OSMO_ASSERT(do_vty_command(vty, "no local-ip") == CMD_SUCCESS);
221 OSMO_ASSERT(srep->bind_addr_str == NULL);
222
223 OSMO_ASSERT(srep->mtu == 0);
224 OSMO_ASSERT(do_vty_command(vty, "mtu 987") == CMD_SUCCESS);
225 OSMO_ASSERT(srep->mtu == 987);
226 OSMO_ASSERT(do_vty_command(vty, "no mtu") == CMD_SUCCESS);
227 OSMO_ASSERT(srep->mtu == 0);
228 };
229
230 OSMO_ASSERT(do_vty_command(vty, "enable") == CMD_SUCCESS);
231 OSMO_ASSERT(srep->enabled != 0);
232 OSMO_ASSERT(do_vty_command(vty, "disable") == CMD_SUCCESS);
233 OSMO_ASSERT(srep->enabled == 0);
234}
235
236static void test_stats_vty(void)
237{
238 struct osmo_stats_reporter *srep;
239 struct vty_test test;
240 struct vty *vty;
241
242 printf("Going to test VTY configuration of the stats subsystem\n");
243 vty = create_test_vty(&test);
244
245 /* Go to config node */
246 OSMO_ASSERT(do_vty_command(vty, "enable") == CMD_SUCCESS);
247 OSMO_ASSERT(vty->node == ENABLE_NODE);
248 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
249 OSMO_ASSERT(vty->node == CONFIG_NODE);
250
251 /* Try to create invalid reporter */
252 OSMO_ASSERT(do_vty_command(vty, "stats reporter foobar") ==
253 CMD_ERR_NO_MATCH);
254
255 /* Set reporting interval */
256 OSMO_ASSERT(do_vty_command(vty, "stats interval 42") == CMD_SUCCESS);
257 OSMO_ASSERT(osmo_stats_config->interval == 42);
258
259 /* Create log reporter */
260 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL);
261 OSMO_ASSERT(srep == NULL);
262 OSMO_ASSERT(do_vty_command(vty, "stats reporter log") == CMD_SUCCESS);
263 OSMO_ASSERT(vty->node == CFG_STATS_NODE);
264 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL);
265 OSMO_ASSERT(srep != NULL);
266 OSMO_ASSERT(srep->type == OSMO_STATS_REPORTER_LOG);
267 check_srep_vty_config(vty, srep);
268 OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
269 OSMO_ASSERT(vty->node == CONFIG_NODE);
270
271 /* Create statsd reporter */
272 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL);
273 OSMO_ASSERT(srep == NULL);
274 OSMO_ASSERT(do_vty_command(vty, "stats reporter statsd") == CMD_SUCCESS);
275 OSMO_ASSERT(vty->node == CFG_STATS_NODE);
276 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL);
277 OSMO_ASSERT(srep != NULL);
278 OSMO_ASSERT(srep->type == OSMO_STATS_REPORTER_STATSD);
279 check_srep_vty_config(vty, srep);
280 OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
281 OSMO_ASSERT(vty->node == CONFIG_NODE);
282
283 /* Destroy log reporter */
284 OSMO_ASSERT(osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL));
285 OSMO_ASSERT(do_vty_command(vty, "no stats reporter log") == CMD_SUCCESS);
286 OSMO_ASSERT(!osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL));
287
288 /* Destroy statsd reporter */
289 OSMO_ASSERT(osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL));
290 OSMO_ASSERT(do_vty_command(vty, "no stats reporter statsd") == CMD_SUCCESS);
291 OSMO_ASSERT(!osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL));
292
293 destroy_test_vty(&test, vty);
294}
295
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200296void test_exit_by_indent(const char *fname, int expect_rc)
297{
298 int rc;
299 printf("reading file %s, expecting rc=%d\n", fname, expect_rc);
300 rc = vty_read_config_file(fname, NULL);
301 printf("got rc=%d\n", rc);
302 OSMO_ASSERT(rc == expect_rc);
303}
304
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200305enum test_nodes {
306 LEVEL1_NODE = _LAST_OSMOVTY_NODE + 1,
307 LEVEL2_NODE,
308 LEVEL3_NODE,
309};
310
311struct cmd_node level1_node = {
312 LEVEL1_NODE,
313 "%s(config-level1)# ",
314 1
315};
316
317struct cmd_node level2_node = {
318 LEVEL2_NODE,
319 "%s(config-level1-level2)# ",
320 1
321};
322
323struct cmd_node level3_node = {
324 LEVEL3_NODE,
325 "%s(config-level1-level2-level3)# ",
326 1
327};
328
329DEFUN(cfg_level1, cfg_level1_cmd,
330 "level1 [MARKER]",
331 "Level 1 node for VTY testing purposes\n"
332 "optional string to mark the line for test debugging\n")
333{
334 vty->index = NULL;
335 vty->node = LEVEL1_NODE;
336 printf("called level1 node %s\n", argc? argv[0] : "");
337 return CMD_SUCCESS;
338}
339
340DEFUN(cfg_level1_child, cfg_level1_child_cmd,
341 "child1 [MARKER]",
342 "Level 1 child cmd for VTY testing purposes\n"
343 "optional string to mark the line for test debugging\n")
344{
345 printf("called level1 child cmd %s\n", argc? argv[0] : "");
346 return CMD_SUCCESS;
347}
348
349DEFUN(cfg_level2, cfg_level2_cmd,
350 "level2 [MARKER]",
351 "Level 2 node for VTY testing purposes\n"
352 "optional string to mark the line for test debugging\n")
353{
354 vty->index = NULL;
355 vty->node = LEVEL2_NODE;
356 printf("called level2 node %s\n", argc? argv[0] : "");
357 return CMD_SUCCESS;
358}
359
360DEFUN(cfg_level2_child, cfg_level2_child_cmd,
361 "child2 [MARKER]",
362 "Level 2 child cmd for VTY testing purposes\n"
363 "optional string to mark the line for test debugging\n")
364{
365 printf("called level2 child cmd %s\n", argc? argv[0] : "");
366 return CMD_SUCCESS;
367}
368
369DEFUN(cfg_level3, cfg_level3_cmd,
370 "level3 [MARKER]",
371 "Level 3 node for VTY testing purposes\n"
372 "optional string to mark the line for test debugging\n")
373{
374 vty->index = NULL;
375 vty->node = LEVEL3_NODE;
376 printf("called level3 node %s\n", argc? argv[0] : "");
377 return CMD_SUCCESS;
378}
379
380DEFUN(cfg_level3_child, cfg_level3_child_cmd,
381 "child3 [MARKER]",
382 "Level 3 child cmd for VTY testing purposes\n"
383 "optional string to mark the line for test debugging\n")
384{
385 printf("called level3 child cmd %s\n", argc? argv[0] : "");
386 return CMD_SUCCESS;
387}
388
Neels Hofmeyr5314c512018-07-09 23:22:21 +0200389DEFUN(cfg_ambiguous_nr_1, cfg_ambiguous_nr_1_cmd,
390 "ambiguous_nr [<0-23>]",
391 "testing is_cmd_ambiguous()\n"
392 "optional number arg\n")
393{
394 printf("Called: 'ambiguous_nr [<0-23>]' (argc=%d)\n", argc);
395 return CMD_SUCCESS;
396}
397
398DEFUN(cfg_ambiguous_nr_2, cfg_ambiguous_nr_2_cmd,
399 "ambiguous_nr <0-23> keyword",
400 "testing is_cmd_ambiguous()\n"
401 "optional number arg\n")
402{
403 printf("Called: 'ambiguous_nr <0-23> keyword'\n");
404 return CMD_SUCCESS;
405}
406
407DEFUN(cfg_ambiguous_str_1, cfg_ambiguous_str_1_cmd,
408 "ambiguous_str [ARG]",
409 "testing is_cmd_ambiguous()\n"
410 "optional string arg\n")
411{
412 printf("Called: 'ambiguous_str [ARG]' (argc=%d)\n", argc);
413 return CMD_SUCCESS;
414}
415
416DEFUN(cfg_ambiguous_str_2, cfg_ambiguous_str_2_cmd,
417 "ambiguous_str ARG keyword",
418 "testing is_cmd_ambiguous()\n"
419 "optional string arg\n")
420{
421 printf("Called: 'ambiguous_str ARG keyword'\n");
422 return CMD_SUCCESS;
423}
424
Pau Espin Pedrola0c81952019-10-22 18:38:01 +0200425DEFUN(cfg_ret_success, cfg_ret_success_cmd,
426 "return-success",
427 "testing return success\n")
428{
429 printf("Called: 'return-success'\n");
430 return CMD_SUCCESS;
431}
432
433DEFUN(cfg_ret_warning, cfg_ret_warning_cmd,
434 "return-warning",
435 "testing return warning\n")
436{
437 printf("Called: 'return-warning'\n");
438 return CMD_WARNING;
439}
440
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200441void test_vty_add_cmds()
442{
Pau Espin Pedrola0c81952019-10-22 18:38:01 +0200443 install_element(CONFIG_NODE, &cfg_ret_warning_cmd);
444 install_element(CONFIG_NODE, &cfg_ret_success_cmd);
445
Vadim Yanitskiy4abda9e2019-11-21 00:19:36 +0700446 logging_vty_add_deprecated_subsys(tall_log_ctx, "depr");
447
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200448 install_element(CONFIG_NODE, &cfg_level1_cmd);
449 install_node(&level1_node, NULL);
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200450 install_element(LEVEL1_NODE, &cfg_level1_child_cmd);
451 install_element(LEVEL1_NODE, &cfg_level2_cmd);
452
453 install_node(&level2_node, NULL);
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200454 install_element(LEVEL2_NODE, &cfg_level2_child_cmd);
455 install_element(LEVEL2_NODE, &cfg_level3_cmd);
456
457 install_node(&level3_node, NULL);
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200458 install_element(LEVEL3_NODE, &cfg_level3_child_cmd);
Neels Hofmeyr5314c512018-07-09 23:22:21 +0200459
460 install_element_ve(&cfg_ambiguous_nr_1_cmd);
461 install_element_ve(&cfg_ambiguous_nr_2_cmd);
462 install_element_ve(&cfg_ambiguous_str_1_cmd);
463 install_element_ve(&cfg_ambiguous_str_2_cmd);
464}
465
466void test_is_cmd_ambiguous()
467{
468 struct vty *vty;
469 struct vty_test test;
470
471 printf("Going to test is_cmd_ambiguous()\n");
472 vty = create_test_vty(&test);
473
474 OSMO_ASSERT(do_vty_command(vty, "ambiguous_nr") == CMD_SUCCESS);
475 OSMO_ASSERT(do_vty_command(vty, "ambiguous_nr 23") == CMD_SUCCESS);
476 OSMO_ASSERT(do_vty_command(vty, "ambiguous_nr 23 keyword") == CMD_SUCCESS);
477
478 OSMO_ASSERT(do_vty_command(vty, "ambiguous_str") == CMD_SUCCESS);
479 OSMO_ASSERT(do_vty_command(vty, "ambiguous_str arg") == CMD_SUCCESS);
480 OSMO_ASSERT(do_vty_command(vty, "ambiguous_str arg keyword") == CMD_SUCCESS);
481
482 destroy_test_vty(&test, vty);
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200483}
484
485static int go_parent_cb(struct vty *vty)
486{
487 /*
488 * - For the interactive VTY tests above, it is expected to bounce back to
489 * the CONFIG_NODE. Hence do so in go_parent_cb().
490 * - In the config file parsing tests, setting vty->node in go_parent_cb() has no
491 * effect, because we will subsequently pop a parent node from the parent stack
492 * and override to go to the node that was recorded as the actual parent.
493 */
494 vty->node = CONFIG_NODE;
495 vty->index = NULL;
496 return 0;
497}
498
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200499int main(int argc, char **argv)
500{
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100501 struct vty_app_info vty_info = {
502 .name = "VtyTest",
503 .version = 0,
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200504 .go_parent_cb = go_parent_cb,
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100505 .is_config_node = NULL,
506 };
507
508 const struct log_info_cat default_categories[] = {};
509
510 const struct log_info log_info = {
511 .cat = default_categories,
512 .num_cat = ARRAY_SIZE(default_categories),
513 };
Neels Hofmeyra829b452018-04-05 03:02:35 +0200514 void *stats_ctx;
515
516 ctx = talloc_named_const(NULL, 0, "stats test context");
517 stats_ctx = talloc_named_const(ctx, 1, "stats test context");
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100518
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200519 osmo_signal_register_handler(SS_L_VTY, vty_event_cb, NULL);
520
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100521 /* Fake logging. */
Neels Hofmeyra829b452018-04-05 03:02:35 +0200522 osmo_init_logging2(ctx, &log_info);
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100523
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100524 /* Init stats */
525 osmo_stats_init(stats_ctx);
526
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100527 vty_init(&vty_info);
528
529 /* Setup VTY commands */
Maxc65c5b42017-03-15 13:20:23 +0100530 logging_vty_add_cmds();
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100531 osmo_stats_vty_add_cmds();
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100532
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200533 test_vty_add_cmds();
534
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200535 test_cmd_string_from_valstr();
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200536 test_node_tree_structure();
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100537 test_stats_vty();
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200538 test_exit_by_indent("ok.cfg", 0);
539 test_exit_by_indent("ok_more_spaces.cfg", 0);
540 test_exit_by_indent("ok_tabs.cfg", 0);
541 test_exit_by_indent("ok_tabs_and_spaces.cfg", 0);
542 test_exit_by_indent("ok_ignore_comment.cfg", 0);
543 test_exit_by_indent("ok_ignore_blank.cfg", 0);
544 test_exit_by_indent("fail_not_de-indented.cfg", -EINVAL);
545 test_exit_by_indent("fail_too_much_indent.cfg", -EINVAL);
546 test_exit_by_indent("fail_tabs_and_spaces.cfg", -EINVAL);
547 test_exit_by_indent("ok_indented_root.cfg", 0);
Neels Hofmeyr43063632017-09-19 23:54:01 +0200548 test_exit_by_indent("ok_empty_parent.cfg", 0);
Pau Espin Pedrola0c81952019-10-22 18:38:01 +0200549 test_exit_by_indent("fail_cmd_ret_warning.cfg", -EINVAL);
Vadim Yanitskiy4abda9e2019-11-21 00:19:36 +0700550 test_exit_by_indent("ok_deprecated_logging.cfg", 0);
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100551
Neels Hofmeyr5314c512018-07-09 23:22:21 +0200552 test_is_cmd_ambiguous();
553
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100554 /* Leak check */
555 OSMO_ASSERT(talloc_total_blocks(stats_ctx) == 1);
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100556
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200557 printf("All tests passed\n");
558
559 return 0;
560}