blob: c84e4c9c10fe367eec7cc6ef25f4b3325fdf944b [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 *
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +020016 */
17
18#include <stdio.h>
19#include <string.h>
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +020020#include <errno.h>
Pau Espin Pedrold92be9a2020-07-30 14:55:49 +020021#include <limits.h>
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +020022
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>
Vadim Yanitskiy4abda9e2019-11-21 00:19:36 +070029#include <osmocom/core/logging_internal.h>
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +020030#include <osmocom/core/logging.h>
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +010031#include <osmocom/core/stats.h>
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +020032#include <osmocom/core/utils.h>
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +020033#include <osmocom/core/signal.h>
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +020034#include <osmocom/vty/misc.h>
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020035#include <osmocom/vty/vty.h>
36#include <osmocom/vty/command.h>
37#include <osmocom/vty/buffer.h>
Holger Hans Peter Freyther6ef71b02013-09-10 11:17:46 +020038#include <osmocom/vty/logging.h>
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +010039#include <osmocom/vty/stats.h>
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020040
41static enum event last_vty_connection_event = -1;
Neels Hofmeyra829b452018-04-05 03:02:35 +020042void *ctx = NULL;
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +020043
44static void test_cmd_string_from_valstr(void)
45{
46 char *cmd;
47 const struct value_string printf_seq_vs[] = {
48 { .value = 42, .str = "[foo%s%s%s%s%s]"},
49 { .value = 43, .str = "[bar%s%s%s%s%s]"},
50 { .value = 0, .str = NULL}
51 };
52
53 printf("Going to test vty_cmd_string_from_valstr()\n");
54
55 /* check against character strings that could break printf */
56
Neels Hofmeyra829b452018-04-05 03:02:35 +020057 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 +020058 printf ("Tested with %%s-strings, resulting cmd = '%s'\n", cmd);
59 talloc_free (cmd);
60}
61
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020062static int do_vty_command(struct vty *vty, const char *cmd)
63{
64 vector vline;
65 int ret;
66
67 printf("Going to execute '%s'\n", cmd);
68 vline = cmd_make_strvec(cmd);
69 ret = cmd_execute_command(vline, vty, NULL, 0);
70 cmd_free_strvec(vline);
71 printf("Returned: %d, Current node: %d '%s'\n", ret, vty->node, cmd_prompt(vty->node));
72 return ret;
73}
74
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +020075/* Handle the events from telnet_interface.c */
76static int vty_event_cb(unsigned int subsys, unsigned int signal,
77 void *handler_data, void *_signal_data)
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020078{
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +020079 struct vty_signal_data *signal_data;
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020080
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +020081 if (subsys != SS_L_VTY)
82 return 0;
83 if (signal != S_VTY_EVENT)
84 return 0;
85
86 signal_data = _signal_data;
87 last_vty_connection_event = signal_data->event;
88
89 fprintf(stderr, "Got VTY event: %d\n", signal_data->event);
90 return 0;
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020091}
92
Jacob Erlbeckca6602f2015-11-03 13:47:11 +010093struct vty_test {
94 int sock[2];
95};
96
97static struct vty* create_test_vty(struct vty_test *data)
98{
99 struct vty *vty;
100 /* Fake connection. */
101 socketpair(AF_UNIX, SOCK_STREAM, 0, data->sock);
102
103 vty = vty_create(data->sock[0], NULL);
104 OSMO_ASSERT(vty != NULL);
105 OSMO_ASSERT(vty->status != VTY_CLOSE);
106
107 return vty;
108}
109
110static void destroy_test_vty(struct vty_test *data, struct vty *vty)
111{
112 vty_close(vty);
113 OSMO_ASSERT(last_vty_connection_event == VTY_CLOSED);
114}
115
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200116static void test_node_tree_structure(void)
117{
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100118 struct vty_test test;
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200119 struct vty *vty;
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200120
121 printf("Going to test VTY node tree structure\n");
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100122 vty = create_test_vty(&test);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200123
124 OSMO_ASSERT(do_vty_command(vty, "enable") == CMD_SUCCESS);
125 OSMO_ASSERT(vty->node == ENABLE_NODE);
126
127 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
128 OSMO_ASSERT(vty->node == CONFIG_NODE);
129 OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
130 OSMO_ASSERT(vty->node == ENABLE_NODE);
131
132 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
133 OSMO_ASSERT(vty->node == CONFIG_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, "log stderr") == CMD_SUCCESS);
140 OSMO_ASSERT(vty->node == CFG_LOG_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, "log stderr") == CMD_SUCCESS);
144 OSMO_ASSERT(vty->node == CFG_LOG_NODE);
145 OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS);
146 OSMO_ASSERT(vty->node == ENABLE_NODE);
147
148 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
149 OSMO_ASSERT(vty->node == CONFIG_NODE);
150 OSMO_ASSERT(do_vty_command(vty, "line vty") == CMD_SUCCESS);
151 OSMO_ASSERT(vty->node == VTY_NODE);
152 OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
153 OSMO_ASSERT(vty->node == CONFIG_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, "end") == CMD_SUCCESS);
157 OSMO_ASSERT(vty->node == ENABLE_NODE);
158
159
Neels Hofmeyrd64b6ae2017-09-07 04:52:05 +0200160 /* Check for not searching the parent node for matching commands. */
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200161 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
162 OSMO_ASSERT(vty->node == CONFIG_NODE);
163 OSMO_ASSERT(do_vty_command(vty, "log stderr") == CMD_SUCCESS);
164 OSMO_ASSERT(vty->node == CFG_LOG_NODE);
Neels Hofmeyrd64b6ae2017-09-07 04:52:05 +0200165 OSMO_ASSERT(do_vty_command(vty, "line vty") == CMD_ERR_NO_MATCH);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200166 OSMO_ASSERT(vty->node == CFG_LOG_NODE);
167 OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS);
168 OSMO_ASSERT(vty->node == ENABLE_NODE);
169
170 /* Check for final 'exit' (connection close). */
171 OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
172 OSMO_ASSERT(vty->node == ENABLE_NODE);
173 OSMO_ASSERT(vty->status == VTY_CLOSE);
174
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100175 destroy_test_vty(&test, vty);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200176}
177
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100178static void check_srep_vty_config(struct vty* vty,
179 struct osmo_stats_reporter *srep)
180{
181 OSMO_ASSERT(srep->enabled == 0);
182
183 OSMO_ASSERT(do_vty_command(vty, "prefix myprefix") == CMD_SUCCESS);
184 OSMO_ASSERT(srep->name_prefix != NULL);
185 OSMO_ASSERT(strcmp(srep->name_prefix, "myprefix") == 0);
186 OSMO_ASSERT(do_vty_command(vty, "no prefix") == CMD_SUCCESS);
187 OSMO_ASSERT(srep->name_prefix == NULL || strlen(srep->name_prefix) == 0);
188
189 OSMO_ASSERT(srep->max_class == OSMO_STATS_CLASS_GLOBAL);
190 OSMO_ASSERT(do_vty_command(vty, "level peer") == CMD_SUCCESS);
191 OSMO_ASSERT(srep->max_class == OSMO_STATS_CLASS_PEER);
192 OSMO_ASSERT(do_vty_command(vty, "level subscriber") == CMD_SUCCESS);
193 OSMO_ASSERT(srep->max_class == OSMO_STATS_CLASS_SUBSCRIBER);
194 OSMO_ASSERT(do_vty_command(vty, "level global") == CMD_SUCCESS);
195 OSMO_ASSERT(srep->max_class == OSMO_STATS_CLASS_GLOBAL);
196 OSMO_ASSERT(do_vty_command(vty, "level foobar") == CMD_ERR_NO_MATCH);
197
198 if (srep->have_net_config) {
199 OSMO_ASSERT(do_vty_command(vty, "remote-ip 127.0.0.99") ==
200 CMD_SUCCESS);
201 OSMO_ASSERT(srep->dest_addr_str &&
202 strcmp(srep->dest_addr_str, "127.0.0.99") == 0);
203 OSMO_ASSERT(do_vty_command(vty, "remote-ip 678.0.0.99") ==
204 CMD_WARNING);
205 OSMO_ASSERT(srep->dest_addr_str &&
206 strcmp(srep->dest_addr_str, "127.0.0.99") == 0);
207
208 OSMO_ASSERT(do_vty_command(vty, "remote-port 12321") ==
209 CMD_SUCCESS);
210 OSMO_ASSERT(srep->dest_port == 12321);
211
212 OSMO_ASSERT(srep->bind_addr_str == NULL);
213 OSMO_ASSERT(do_vty_command(vty, "local-ip 127.0.0.98") ==
214 CMD_SUCCESS);
215 OSMO_ASSERT(srep->bind_addr_str &&
216 strcmp(srep->bind_addr_str, "127.0.0.98") == 0);
217 OSMO_ASSERT(do_vty_command(vty, "no local-ip") == CMD_SUCCESS);
218 OSMO_ASSERT(srep->bind_addr_str == NULL);
219
220 OSMO_ASSERT(srep->mtu == 0);
221 OSMO_ASSERT(do_vty_command(vty, "mtu 987") == CMD_SUCCESS);
222 OSMO_ASSERT(srep->mtu == 987);
223 OSMO_ASSERT(do_vty_command(vty, "no mtu") == CMD_SUCCESS);
224 OSMO_ASSERT(srep->mtu == 0);
225 };
226
227 OSMO_ASSERT(do_vty_command(vty, "enable") == CMD_SUCCESS);
228 OSMO_ASSERT(srep->enabled != 0);
229 OSMO_ASSERT(do_vty_command(vty, "disable") == CMD_SUCCESS);
230 OSMO_ASSERT(srep->enabled == 0);
231}
232
233static void test_stats_vty(void)
234{
235 struct osmo_stats_reporter *srep;
236 struct vty_test test;
237 struct vty *vty;
238
239 printf("Going to test VTY configuration of the stats subsystem\n");
240 vty = create_test_vty(&test);
241
242 /* Go to config node */
243 OSMO_ASSERT(do_vty_command(vty, "enable") == CMD_SUCCESS);
244 OSMO_ASSERT(vty->node == ENABLE_NODE);
245 OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
246 OSMO_ASSERT(vty->node == CONFIG_NODE);
247
248 /* Try to create invalid reporter */
249 OSMO_ASSERT(do_vty_command(vty, "stats reporter foobar") ==
250 CMD_ERR_NO_MATCH);
251
252 /* Set reporting interval */
253 OSMO_ASSERT(do_vty_command(vty, "stats interval 42") == CMD_SUCCESS);
254 OSMO_ASSERT(osmo_stats_config->interval == 42);
255
256 /* Create log reporter */
257 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL);
258 OSMO_ASSERT(srep == NULL);
259 OSMO_ASSERT(do_vty_command(vty, "stats reporter log") == CMD_SUCCESS);
260 OSMO_ASSERT(vty->node == CFG_STATS_NODE);
261 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL);
262 OSMO_ASSERT(srep != NULL);
263 OSMO_ASSERT(srep->type == OSMO_STATS_REPORTER_LOG);
264 check_srep_vty_config(vty, srep);
265 OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
266 OSMO_ASSERT(vty->node == CONFIG_NODE);
267
268 /* Create statsd reporter */
269 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL);
270 OSMO_ASSERT(srep == NULL);
271 OSMO_ASSERT(do_vty_command(vty, "stats reporter statsd") == CMD_SUCCESS);
272 OSMO_ASSERT(vty->node == CFG_STATS_NODE);
273 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL);
274 OSMO_ASSERT(srep != NULL);
275 OSMO_ASSERT(srep->type == OSMO_STATS_REPORTER_STATSD);
276 check_srep_vty_config(vty, srep);
277 OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
278 OSMO_ASSERT(vty->node == CONFIG_NODE);
279
280 /* Destroy log reporter */
281 OSMO_ASSERT(osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL));
282 OSMO_ASSERT(do_vty_command(vty, "no stats reporter log") == CMD_SUCCESS);
283 OSMO_ASSERT(!osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL));
284
285 /* Destroy statsd reporter */
286 OSMO_ASSERT(osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL));
287 OSMO_ASSERT(do_vty_command(vty, "no stats reporter statsd") == CMD_SUCCESS);
288 OSMO_ASSERT(!osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL));
289
290 destroy_test_vty(&test, vty);
291}
292
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200293void test_exit_by_indent(const char *fname, int expect_rc)
294{
295 int rc;
296 printf("reading file %s, expecting rc=%d\n", fname, expect_rc);
297 rc = vty_read_config_file(fname, NULL);
298 printf("got rc=%d\n", rc);
299 OSMO_ASSERT(rc == expect_rc);
300}
301
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200302enum test_nodes {
303 LEVEL1_NODE = _LAST_OSMOVTY_NODE + 1,
304 LEVEL2_NODE,
305 LEVEL3_NODE,
306};
307
308struct cmd_node level1_node = {
309 LEVEL1_NODE,
310 "%s(config-level1)# ",
311 1
312};
313
314struct cmd_node level2_node = {
315 LEVEL2_NODE,
316 "%s(config-level1-level2)# ",
317 1
318};
319
320struct cmd_node level3_node = {
321 LEVEL3_NODE,
322 "%s(config-level1-level2-level3)# ",
323 1
324};
325
326DEFUN(cfg_level1, cfg_level1_cmd,
327 "level1 [MARKER]",
328 "Level 1 node for VTY testing purposes\n"
329 "optional string to mark the line for test debugging\n")
330{
331 vty->index = NULL;
332 vty->node = LEVEL1_NODE;
333 printf("called level1 node %s\n", argc? argv[0] : "");
334 return CMD_SUCCESS;
335}
336
337DEFUN(cfg_level1_child, cfg_level1_child_cmd,
338 "child1 [MARKER]",
339 "Level 1 child cmd for VTY testing purposes\n"
340 "optional string to mark the line for test debugging\n")
341{
342 printf("called level1 child cmd %s\n", argc? argv[0] : "");
343 return CMD_SUCCESS;
344}
345
346DEFUN(cfg_level2, cfg_level2_cmd,
347 "level2 [MARKER]",
348 "Level 2 node for VTY testing purposes\n"
349 "optional string to mark the line for test debugging\n")
350{
351 vty->index = NULL;
352 vty->node = LEVEL2_NODE;
353 printf("called level2 node %s\n", argc? argv[0] : "");
354 return CMD_SUCCESS;
355}
356
357DEFUN(cfg_level2_child, cfg_level2_child_cmd,
358 "child2 [MARKER]",
359 "Level 2 child cmd for VTY testing purposes\n"
360 "optional string to mark the line for test debugging\n")
361{
362 printf("called level2 child cmd %s\n", argc? argv[0] : "");
363 return CMD_SUCCESS;
364}
365
366DEFUN(cfg_level3, cfg_level3_cmd,
367 "level3 [MARKER]",
368 "Level 3 node for VTY testing purposes\n"
369 "optional string to mark the line for test debugging\n")
370{
371 vty->index = NULL;
372 vty->node = LEVEL3_NODE;
373 printf("called level3 node %s\n", argc? argv[0] : "");
374 return CMD_SUCCESS;
375}
376
377DEFUN(cfg_level3_child, cfg_level3_child_cmd,
378 "child3 [MARKER]",
379 "Level 3 child cmd for VTY testing purposes\n"
380 "optional string to mark the line for test debugging\n")
381{
382 printf("called level3 child cmd %s\n", argc? argv[0] : "");
383 return CMD_SUCCESS;
384}
385
Neels Hofmeyr5314c512018-07-09 23:22:21 +0200386DEFUN(cfg_ambiguous_nr_1, cfg_ambiguous_nr_1_cmd,
387 "ambiguous_nr [<0-23>]",
388 "testing is_cmd_ambiguous()\n"
389 "optional number arg\n")
390{
391 printf("Called: 'ambiguous_nr [<0-23>]' (argc=%d)\n", argc);
392 return CMD_SUCCESS;
393}
394
395DEFUN(cfg_ambiguous_nr_2, cfg_ambiguous_nr_2_cmd,
396 "ambiguous_nr <0-23> keyword",
397 "testing is_cmd_ambiguous()\n"
398 "optional number arg\n")
399{
400 printf("Called: 'ambiguous_nr <0-23> keyword'\n");
401 return CMD_SUCCESS;
402}
403
404DEFUN(cfg_ambiguous_str_1, cfg_ambiguous_str_1_cmd,
405 "ambiguous_str [ARG]",
406 "testing is_cmd_ambiguous()\n"
407 "optional string arg\n")
408{
409 printf("Called: 'ambiguous_str [ARG]' (argc=%d)\n", argc);
410 return CMD_SUCCESS;
411}
412
413DEFUN(cfg_ambiguous_str_2, cfg_ambiguous_str_2_cmd,
414 "ambiguous_str ARG keyword",
415 "testing is_cmd_ambiguous()\n"
416 "optional string arg\n")
417{
418 printf("Called: 'ambiguous_str ARG keyword'\n");
419 return CMD_SUCCESS;
420}
421
Pau Espin Pedrola0c81952019-10-22 18:38:01 +0200422DEFUN(cfg_ret_success, cfg_ret_success_cmd,
423 "return-success",
424 "testing return success\n")
425{
426 printf("Called: 'return-success'\n");
427 return CMD_SUCCESS;
428}
429
430DEFUN(cfg_ret_warning, cfg_ret_warning_cmd,
431 "return-warning",
432 "testing return warning\n")
433{
434 printf("Called: 'return-warning'\n");
435 return CMD_WARNING;
436}
437
Pau Espin Pedrold92be9a2020-07-30 14:55:49 +0200438DEFUN(cfg_numeric_range, cfg_numeric_range_cmd,
439#if ULONG_MAX == 18446744073709551615UL
440 "numeric-range <0-18446744073709551615>",
441#else
442 "numeric-range <0-4294967295>",
443#endif
444 "testing numeric range\n"
445 "the numeric range\n")
446{
447 printf("Called: 'return-success'\n");
448 return CMD_SUCCESS;
449}
450
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200451void test_vty_add_cmds()
452{
Pau Espin Pedrola0c81952019-10-22 18:38:01 +0200453 install_element(CONFIG_NODE, &cfg_ret_warning_cmd);
454 install_element(CONFIG_NODE, &cfg_ret_success_cmd);
455
Vadim Yanitskiy4abda9e2019-11-21 00:19:36 +0700456 logging_vty_add_deprecated_subsys(tall_log_ctx, "depr");
457
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200458 install_element(CONFIG_NODE, &cfg_level1_cmd);
459 install_node(&level1_node, NULL);
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200460 install_element(LEVEL1_NODE, &cfg_level1_child_cmd);
461 install_element(LEVEL1_NODE, &cfg_level2_cmd);
462
463 install_node(&level2_node, NULL);
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200464 install_element(LEVEL2_NODE, &cfg_level2_child_cmd);
465 install_element(LEVEL2_NODE, &cfg_level3_cmd);
466
467 install_node(&level3_node, NULL);
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200468 install_element(LEVEL3_NODE, &cfg_level3_child_cmd);
Neels Hofmeyr5314c512018-07-09 23:22:21 +0200469
470 install_element_ve(&cfg_ambiguous_nr_1_cmd);
471 install_element_ve(&cfg_ambiguous_nr_2_cmd);
472 install_element_ve(&cfg_ambiguous_str_1_cmd);
473 install_element_ve(&cfg_ambiguous_str_2_cmd);
Pau Espin Pedrold92be9a2020-07-30 14:55:49 +0200474
475 install_element_ve(&cfg_numeric_range_cmd);
Neels Hofmeyr5314c512018-07-09 23:22:21 +0200476}
477
478void test_is_cmd_ambiguous()
479{
480 struct vty *vty;
481 struct vty_test test;
482
483 printf("Going to test is_cmd_ambiguous()\n");
484 vty = create_test_vty(&test);
485
486 OSMO_ASSERT(do_vty_command(vty, "ambiguous_nr") == CMD_SUCCESS);
487 OSMO_ASSERT(do_vty_command(vty, "ambiguous_nr 23") == CMD_SUCCESS);
488 OSMO_ASSERT(do_vty_command(vty, "ambiguous_nr 23 keyword") == CMD_SUCCESS);
489
490 OSMO_ASSERT(do_vty_command(vty, "ambiguous_str") == CMD_SUCCESS);
491 OSMO_ASSERT(do_vty_command(vty, "ambiguous_str arg") == CMD_SUCCESS);
492 OSMO_ASSERT(do_vty_command(vty, "ambiguous_str arg keyword") == CMD_SUCCESS);
493
494 destroy_test_vty(&test, vty);
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200495}
496
Pau Espin Pedrold92be9a2020-07-30 14:55:49 +0200497void test_numeric_range()
498{
499 struct vty *vty;
500 struct vty_test test;
501
502 printf("Going to test test_numeric_range()\n");
503 vty = create_test_vty(&test);
504
Pau Espin Pedrol9fdc8712020-07-30 14:56:38 +0200505 OSMO_ASSERT(do_vty_command(vty, "numeric-range 0") == CMD_SUCCESS);
506 OSMO_ASSERT(do_vty_command(vty, "numeric-range 40000") == CMD_SUCCESS);
Pau Espin Pedrola1847012020-07-28 17:44:48 +0200507 OSMO_ASSERT(do_vty_command(vty, "numeric-range -400000") == CMD_ERR_NO_MATCH);
Pau Espin Pedrold92be9a2020-07-30 14:55:49 +0200508
509 destroy_test_vty(&test, vty);
510}
511
Vadim Yanitskiy024e1952020-10-02 18:23:38 +0700512/* Application specific attributes */
513enum vty_test_attr {
514 VTY_TEST_ATTR_FOO = 0,
515 VTY_TEST_ATTR_BAR,
516 VTY_TEST_ATTR_ZOO,
517 VTY_TEST_ATTR_FOO_DUP,
518 VTY_TEST_ATTR_ZOO_DUP,
Vadim Yanitskiyf94355d2020-10-03 17:43:37 +0700519 VTY_TEST_ATTR_UPPER,
Vadim Yanitskiyef4c5972020-10-07 13:44:31 +0700520 VTY_TEST_ATTR_RAFC_DOT,
521 VTY_TEST_ATTR_RAFC_EXCL,
522 VTY_TEST_ATTR_RAFC_AT,
Vadim Yanitskiy024e1952020-10-02 18:23:38 +0700523};
524
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200525int main(int argc, char **argv)
526{
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100527 struct vty_app_info vty_info = {
528 .name = "VtyTest",
529 .version = 0,
Vadim Yanitskiy024e1952020-10-02 18:23:38 +0700530 .usr_attr_letters = {
531 [VTY_TEST_ATTR_FOO] = 'f',
532 [VTY_TEST_ATTR_BAR] = 'b',
533 [VTY_TEST_ATTR_ZOO] = 'z',
534
535 /* Duplicate detection check */
536 [VTY_TEST_ATTR_FOO_DUP] = 'f',
537 [VTY_TEST_ATTR_ZOO_DUP] = 'z',
Vadim Yanitskiyf94355d2020-10-03 17:43:37 +0700538 /* Reserved for libraries */
539 [VTY_TEST_ATTR_UPPER] = 'X',
Vadim Yanitskiyef4c5972020-10-07 13:44:31 +0700540 /* Reserved for global attribues */
Vadim Yanitskiyce73dda2021-11-17 07:37:12 +0300541 [VTY_TEST_ATTR_RAFC_DOT] = '.',
542 [VTY_TEST_ATTR_RAFC_EXCL] = '!',
543 [VTY_TEST_ATTR_RAFC_AT] = '@',
Vadim Yanitskiy024e1952020-10-02 18:23:38 +0700544 },
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100545 };
546
547 const struct log_info_cat default_categories[] = {};
548
549 const struct log_info log_info = {
550 .cat = default_categories,
551 .num_cat = ARRAY_SIZE(default_categories),
552 };
Neels Hofmeyra829b452018-04-05 03:02:35 +0200553 void *stats_ctx;
554
555 ctx = talloc_named_const(NULL, 0, "stats test context");
556 stats_ctx = talloc_named_const(ctx, 1, "stats test context");
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100557
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200558 osmo_signal_register_handler(SS_L_VTY, vty_event_cb, NULL);
559
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100560 /* Fake logging. */
Neels Hofmeyra829b452018-04-05 03:02:35 +0200561 osmo_init_logging2(ctx, &log_info);
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100562
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100563 /* Init stats */
564 osmo_stats_init(stats_ctx);
565
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100566 vty_init(&vty_info);
567
568 /* Setup VTY commands */
Maxc65c5b42017-03-15 13:20:23 +0100569 logging_vty_add_cmds();
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100570 osmo_stats_vty_add_cmds();
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100571
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200572 test_vty_add_cmds();
573
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200574 test_cmd_string_from_valstr();
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200575 test_node_tree_structure();
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100576 test_stats_vty();
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200577 test_exit_by_indent("ok.cfg", 0);
578 test_exit_by_indent("ok_more_spaces.cfg", 0);
579 test_exit_by_indent("ok_tabs.cfg", 0);
580 test_exit_by_indent("ok_tabs_and_spaces.cfg", 0);
581 test_exit_by_indent("ok_ignore_comment.cfg", 0);
582 test_exit_by_indent("ok_ignore_blank.cfg", 0);
583 test_exit_by_indent("fail_not_de-indented.cfg", -EINVAL);
584 test_exit_by_indent("fail_too_much_indent.cfg", -EINVAL);
585 test_exit_by_indent("fail_tabs_and_spaces.cfg", -EINVAL);
586 test_exit_by_indent("ok_indented_root.cfg", 0);
Neels Hofmeyr43063632017-09-19 23:54:01 +0200587 test_exit_by_indent("ok_empty_parent.cfg", 0);
Pau Espin Pedrola0c81952019-10-22 18:38:01 +0200588 test_exit_by_indent("fail_cmd_ret_warning.cfg", -EINVAL);
Vadim Yanitskiy4abda9e2019-11-21 00:19:36 +0700589 test_exit_by_indent("ok_deprecated_logging.cfg", 0);
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100590
Neels Hofmeyr5314c512018-07-09 23:22:21 +0200591 test_is_cmd_ambiguous();
592
Pau Espin Pedrold92be9a2020-07-30 14:55:49 +0200593 test_numeric_range();
594
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100595 /* Leak check */
596 OSMO_ASSERT(talloc_total_blocks(stats_ctx) == 1);
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100597
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200598 printf("All tests passed\n");
599
600 return 0;
601}