blob: 3076a703db366def957195bdfa68f20ce1b82db2 [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
Pau Espin Pedrolc3b904a2020-07-28 15:37:02 +0200451DEFUN(cfg_range_base10, cfg_range_base10_cmd,
452 "range-base10 <0-999999>",
453 "testing decimal range\n"
454 "the decimal range\n")
455{
456 printf("Called: 'return-success'\n");
457 return CMD_SUCCESS;
458}
459
460DEFUN(cfg_range_base16, cfg_range_base16_cmd,
461 "range-base16 <0x0-0x8888>",
462 "testing hexadecimal range\n"
463 "the hexadecimal range\n")
464{
465 printf("Called: 'return-success'\n");
466 return CMD_SUCCESS;
467}
468
469DEFUN(cfg_range_baseboth, cfg_range_baseboth_cmd,
470 "range-baseboth (<0-999999>|<0x0-0x8888>)",
471 "testing both ranges\n"
472 "the decimal range\n"
473 "the hexadecimal range\n")
474{
475 printf("Called: 'return-success'\n");
476 return CMD_SUCCESS;
477}
478
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200479void test_vty_add_cmds()
480{
Pau Espin Pedrola0c81952019-10-22 18:38:01 +0200481 install_element(CONFIG_NODE, &cfg_ret_warning_cmd);
482 install_element(CONFIG_NODE, &cfg_ret_success_cmd);
483
Vadim Yanitskiy4abda9e2019-11-21 00:19:36 +0700484 logging_vty_add_deprecated_subsys(tall_log_ctx, "depr");
485
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200486 install_element(CONFIG_NODE, &cfg_level1_cmd);
487 install_node(&level1_node, NULL);
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200488 install_element(LEVEL1_NODE, &cfg_level1_child_cmd);
489 install_element(LEVEL1_NODE, &cfg_level2_cmd);
490
491 install_node(&level2_node, NULL);
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200492 install_element(LEVEL2_NODE, &cfg_level2_child_cmd);
493 install_element(LEVEL2_NODE, &cfg_level3_cmd);
494
495 install_node(&level3_node, NULL);
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200496 install_element(LEVEL3_NODE, &cfg_level3_child_cmd);
Neels Hofmeyr5314c512018-07-09 23:22:21 +0200497
498 install_element_ve(&cfg_ambiguous_nr_1_cmd);
499 install_element_ve(&cfg_ambiguous_nr_2_cmd);
500 install_element_ve(&cfg_ambiguous_str_1_cmd);
501 install_element_ve(&cfg_ambiguous_str_2_cmd);
Pau Espin Pedrold92be9a2020-07-30 14:55:49 +0200502
503 install_element_ve(&cfg_numeric_range_cmd);
Pau Espin Pedrolc3b904a2020-07-28 15:37:02 +0200504
505 install_element_ve(&cfg_range_base10_cmd);
506 install_element_ve(&cfg_range_base16_cmd);
507 install_element_ve(&cfg_range_baseboth_cmd);
Neels Hofmeyr5314c512018-07-09 23:22:21 +0200508}
509
510void test_is_cmd_ambiguous()
511{
512 struct vty *vty;
513 struct vty_test test;
514
515 printf("Going to test is_cmd_ambiguous()\n");
516 vty = create_test_vty(&test);
517
518 OSMO_ASSERT(do_vty_command(vty, "ambiguous_nr") == CMD_SUCCESS);
519 OSMO_ASSERT(do_vty_command(vty, "ambiguous_nr 23") == CMD_SUCCESS);
520 OSMO_ASSERT(do_vty_command(vty, "ambiguous_nr 23 keyword") == CMD_SUCCESS);
521
522 OSMO_ASSERT(do_vty_command(vty, "ambiguous_str") == CMD_SUCCESS);
523 OSMO_ASSERT(do_vty_command(vty, "ambiguous_str arg") == CMD_SUCCESS);
524 OSMO_ASSERT(do_vty_command(vty, "ambiguous_str arg keyword") == CMD_SUCCESS);
525
526 destroy_test_vty(&test, vty);
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200527}
528
Pau Espin Pedrold92be9a2020-07-30 14:55:49 +0200529void test_numeric_range()
530{
531 struct vty *vty;
532 struct vty_test test;
533
534 printf("Going to test test_numeric_range()\n");
535 vty = create_test_vty(&test);
536
Pau Espin Pedrol9fdc8712020-07-30 14:56:38 +0200537 OSMO_ASSERT(do_vty_command(vty, "numeric-range 0") == CMD_SUCCESS);
538 OSMO_ASSERT(do_vty_command(vty, "numeric-range 40000") == CMD_SUCCESS);
Pau Espin Pedrola1847012020-07-28 17:44:48 +0200539 OSMO_ASSERT(do_vty_command(vty, "numeric-range -400000") == CMD_ERR_NO_MATCH);
Pau Espin Pedrold92be9a2020-07-30 14:55:49 +0200540
541 destroy_test_vty(&test, vty);
542}
543
Pau Espin Pedrolc3b904a2020-07-28 15:37:02 +0200544void test_ranges()
545{
546 struct vty *vty;
547 struct vty_test test;
548
549 printf("Going to test test_ranges()\n");
550 vty = create_test_vty(&test);
551
552 printf("test range-base10\n");
553 OSMO_ASSERT(do_vty_command(vty, "range-base10 0") == CMD_SUCCESS);
554 OSMO_ASSERT(do_vty_command(vty, "range-base10 40000") == CMD_SUCCESS);
555 OSMO_ASSERT(do_vty_command(vty, "range-base10 -400000") == CMD_ERR_NO_MATCH);
556 OSMO_ASSERT(do_vty_command(vty, "range-base10 0x0") == CMD_ERR_NO_MATCH);
557 OSMO_ASSERT(do_vty_command(vty, "range-base10 0x343") == CMD_ERR_NO_MATCH);
558 OSMO_ASSERT(do_vty_command(vty, "range-base10 -0x343") == CMD_ERR_NO_MATCH);
559
560 printf("test range-base16\n");
561 OSMO_ASSERT(do_vty_command(vty, "range-base16 0") == CMD_ERR_NO_MATCH);
562 OSMO_ASSERT(do_vty_command(vty, "range-base16 40000") == CMD_ERR_NO_MATCH);
563 OSMO_ASSERT(do_vty_command(vty, "range-base16 -400000") == CMD_ERR_NO_MATCH);
564 OSMO_ASSERT(do_vty_command(vty, "range-base16 0x0") == CMD_SUCCESS);
565 OSMO_ASSERT(do_vty_command(vty, "range-base16 0x343") == CMD_SUCCESS);
566 OSMO_ASSERT(do_vty_command(vty, "range-base16 -0x343") == CMD_ERR_NO_MATCH);
567
568 printf("test range-baseboth\n");
569 OSMO_ASSERT(do_vty_command(vty, "range-baseboth 0") == CMD_SUCCESS);
570 OSMO_ASSERT(do_vty_command(vty, "range-baseboth 40000") == CMD_SUCCESS);
571 OSMO_ASSERT(do_vty_command(vty, "range-baseboth -400000") == CMD_ERR_NO_MATCH);
572 OSMO_ASSERT(do_vty_command(vty, "range-baseboth 0x0") == CMD_SUCCESS);
573 OSMO_ASSERT(do_vty_command(vty, "range-baseboth 0x343") == CMD_SUCCESS);
574 OSMO_ASSERT(do_vty_command(vty, "range-baseboth -0x343") == CMD_ERR_NO_MATCH);
575
576 destroy_test_vty(&test, vty);
577}
Vadim Yanitskiy024e1952020-10-02 18:23:38 +0700578/* Application specific attributes */
579enum vty_test_attr {
580 VTY_TEST_ATTR_FOO = 0,
581 VTY_TEST_ATTR_BAR,
582 VTY_TEST_ATTR_ZOO,
583 VTY_TEST_ATTR_FOO_DUP,
584 VTY_TEST_ATTR_ZOO_DUP,
Vadim Yanitskiyf94355d2020-10-03 17:43:37 +0700585 VTY_TEST_ATTR_UPPER,
Vadim Yanitskiyef4c5972020-10-07 13:44:31 +0700586 VTY_TEST_ATTR_RAFC_DOT,
587 VTY_TEST_ATTR_RAFC_EXCL,
588 VTY_TEST_ATTR_RAFC_AT,
Vadim Yanitskiy024e1952020-10-02 18:23:38 +0700589};
590
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200591int main(int argc, char **argv)
592{
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100593 struct vty_app_info vty_info = {
594 .name = "VtyTest",
595 .version = 0,
Vadim Yanitskiy024e1952020-10-02 18:23:38 +0700596 .usr_attr_letters = {
597 [VTY_TEST_ATTR_FOO] = 'f',
598 [VTY_TEST_ATTR_BAR] = 'b',
599 [VTY_TEST_ATTR_ZOO] = 'z',
600
601 /* Duplicate detection check */
602 [VTY_TEST_ATTR_FOO_DUP] = 'f',
603 [VTY_TEST_ATTR_ZOO_DUP] = 'z',
Vadim Yanitskiyf94355d2020-10-03 17:43:37 +0700604 /* Reserved for libraries */
605 [VTY_TEST_ATTR_UPPER] = 'X',
Vadim Yanitskiyef4c5972020-10-07 13:44:31 +0700606 /* Reserved for global attribues */
Vadim Yanitskiyce73dda2021-11-17 07:37:12 +0300607 [VTY_TEST_ATTR_RAFC_DOT] = '.',
608 [VTY_TEST_ATTR_RAFC_EXCL] = '!',
609 [VTY_TEST_ATTR_RAFC_AT] = '@',
Vadim Yanitskiy024e1952020-10-02 18:23:38 +0700610 },
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100611 };
612
613 const struct log_info_cat default_categories[] = {};
614
615 const struct log_info log_info = {
616 .cat = default_categories,
617 .num_cat = ARRAY_SIZE(default_categories),
618 };
Neels Hofmeyra829b452018-04-05 03:02:35 +0200619 void *stats_ctx;
620
621 ctx = talloc_named_const(NULL, 0, "stats test context");
622 stats_ctx = talloc_named_const(ctx, 1, "stats test context");
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100623
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200624 osmo_signal_register_handler(SS_L_VTY, vty_event_cb, NULL);
625
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100626 /* Fake logging. */
Neels Hofmeyra829b452018-04-05 03:02:35 +0200627 osmo_init_logging2(ctx, &log_info);
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100628
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100629 /* Init stats */
630 osmo_stats_init(stats_ctx);
631
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100632 vty_init(&vty_info);
633
634 /* Setup VTY commands */
Maxc65c5b42017-03-15 13:20:23 +0100635 logging_vty_add_cmds();
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100636 osmo_stats_vty_add_cmds();
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100637
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200638 test_vty_add_cmds();
639
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200640 test_cmd_string_from_valstr();
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200641 test_node_tree_structure();
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100642 test_stats_vty();
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200643 test_exit_by_indent("ok.cfg", 0);
644 test_exit_by_indent("ok_more_spaces.cfg", 0);
645 test_exit_by_indent("ok_tabs.cfg", 0);
646 test_exit_by_indent("ok_tabs_and_spaces.cfg", 0);
647 test_exit_by_indent("ok_ignore_comment.cfg", 0);
648 test_exit_by_indent("ok_ignore_blank.cfg", 0);
649 test_exit_by_indent("fail_not_de-indented.cfg", -EINVAL);
650 test_exit_by_indent("fail_too_much_indent.cfg", -EINVAL);
651 test_exit_by_indent("fail_tabs_and_spaces.cfg", -EINVAL);
652 test_exit_by_indent("ok_indented_root.cfg", 0);
Neels Hofmeyr43063632017-09-19 23:54:01 +0200653 test_exit_by_indent("ok_empty_parent.cfg", 0);
Pau Espin Pedrola0c81952019-10-22 18:38:01 +0200654 test_exit_by_indent("fail_cmd_ret_warning.cfg", -EINVAL);
Vadim Yanitskiy4abda9e2019-11-21 00:19:36 +0700655 test_exit_by_indent("ok_deprecated_logging.cfg", 0);
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100656
Neels Hofmeyr5314c512018-07-09 23:22:21 +0200657 test_is_cmd_ambiguous();
658
Pau Espin Pedrold92be9a2020-07-30 14:55:49 +0200659 test_numeric_range();
Pau Espin Pedrolc3b904a2020-07-28 15:37:02 +0200660 test_ranges();
Pau Espin Pedrold92be9a2020-07-30 14:55:49 +0200661
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100662 /* Leak check */
663 OSMO_ASSERT(talloc_total_blocks(stats_ctx) == 1);
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100664
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200665 printf("All tests passed\n");
666
667 return 0;
668}