blob: 30efb9afd4335f8c76ab7690926f7fd9ae1d2b49 [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>
32#include <osmocom/core/logging.h>
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +010033#include <osmocom/core/stats.h>
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +020034#include <osmocom/core/utils.h>
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +020035#include <osmocom/core/signal.h>
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +020036#include <osmocom/vty/misc.h>
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020037#include <osmocom/vty/vty.h>
38#include <osmocom/vty/command.h>
39#include <osmocom/vty/buffer.h>
Holger Hans Peter Freyther6ef71b02013-09-10 11:17:46 +020040#include <osmocom/vty/logging.h>
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +010041#include <osmocom/vty/stats.h>
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020042
43static enum event last_vty_connection_event = -1;
Neels Hofmeyra829b452018-04-05 03:02:35 +020044void *ctx = NULL;
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +020045
46static 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 Hofmeyra829b452018-04-05 03:02:35 +020059 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 +020060 printf ("Tested with %%s-strings, resulting cmd = '%s'\n", cmd);
61 talloc_free (cmd);
62}
63
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020064static 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 Freyther2c9168c2013-10-10 20:21:33 +020077/* Handle the events from telnet_interface.c */
78static int vty_event_cb(unsigned int subsys, unsigned int signal,
79 void *handler_data, void *_signal_data)
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020080{
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +020081 struct vty_signal_data *signal_data;
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +020082
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +020083 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 Erlbeck0c987bd2013-09-06 16:52:00 +020093}
94
Jacob Erlbeckca6602f2015-11-03 13:47:11 +010095struct vty_test {
96 int sock[2];
97};
98
99static 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
112static 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 Erlbeck0c987bd2013-09-06 16:52:00 +0200118static void test_node_tree_structure(void)
119{
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100120 struct vty_test test;
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200121 struct vty *vty;
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200122
123 printf("Going to test VTY node tree structure\n");
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100124 vty = create_test_vty(&test);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200125
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 Hofmeyrd64b6ae2017-09-07 04:52:05 +0200162 /* Check for not searching the parent node for matching commands. */
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200163 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 Hofmeyrd64b6ae2017-09-07 04:52:05 +0200167 OSMO_ASSERT(do_vty_command(vty, "line vty") == CMD_ERR_NO_MATCH);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200168 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 Erlbeckca6602f2015-11-03 13:47:11 +0100177 destroy_test_vty(&test, vty);
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200178}
179
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100180static 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
235static 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 Hofmeyr4a31ffa2017-09-07 03:08:06 +0200295void 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 Hofmeyrb022c862017-09-20 01:49:11 +0200304enum test_nodes {
305 LEVEL1_NODE = _LAST_OSMOVTY_NODE + 1,
306 LEVEL2_NODE,
307 LEVEL3_NODE,
308};
309
310struct cmd_node level1_node = {
311 LEVEL1_NODE,
312 "%s(config-level1)# ",
313 1
314};
315
316struct cmd_node level2_node = {
317 LEVEL2_NODE,
318 "%s(config-level1-level2)# ",
319 1
320};
321
322struct cmd_node level3_node = {
323 LEVEL3_NODE,
324 "%s(config-level1-level2-level3)# ",
325 1
326};
327
328DEFUN(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
339DEFUN(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
348DEFUN(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
359DEFUN(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
368DEFUN(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
379DEFUN(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
Neels Hofmeyr5314c512018-07-09 23:22:21 +0200388DEFUN(cfg_ambiguous_nr_1, cfg_ambiguous_nr_1_cmd,
389 "ambiguous_nr [<0-23>]",
390 "testing is_cmd_ambiguous()\n"
391 "optional number arg\n")
392{
393 printf("Called: 'ambiguous_nr [<0-23>]' (argc=%d)\n", argc);
394 return CMD_SUCCESS;
395}
396
397DEFUN(cfg_ambiguous_nr_2, cfg_ambiguous_nr_2_cmd,
398 "ambiguous_nr <0-23> keyword",
399 "testing is_cmd_ambiguous()\n"
400 "optional number arg\n")
401{
402 printf("Called: 'ambiguous_nr <0-23> keyword'\n");
403 return CMD_SUCCESS;
404}
405
406DEFUN(cfg_ambiguous_str_1, cfg_ambiguous_str_1_cmd,
407 "ambiguous_str [ARG]",
408 "testing is_cmd_ambiguous()\n"
409 "optional string arg\n")
410{
411 printf("Called: 'ambiguous_str [ARG]' (argc=%d)\n", argc);
412 return CMD_SUCCESS;
413}
414
415DEFUN(cfg_ambiguous_str_2, cfg_ambiguous_str_2_cmd,
416 "ambiguous_str ARG keyword",
417 "testing is_cmd_ambiguous()\n"
418 "optional string arg\n")
419{
420 printf("Called: 'ambiguous_str ARG keyword'\n");
421 return CMD_SUCCESS;
422}
423
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200424void test_vty_add_cmds()
425{
426 install_element(CONFIG_NODE, &cfg_level1_cmd);
427 install_node(&level1_node, NULL);
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200428 install_element(LEVEL1_NODE, &cfg_level1_child_cmd);
429 install_element(LEVEL1_NODE, &cfg_level2_cmd);
430
431 install_node(&level2_node, NULL);
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200432 install_element(LEVEL2_NODE, &cfg_level2_child_cmd);
433 install_element(LEVEL2_NODE, &cfg_level3_cmd);
434
435 install_node(&level3_node, NULL);
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200436 install_element(LEVEL3_NODE, &cfg_level3_child_cmd);
Neels Hofmeyr5314c512018-07-09 23:22:21 +0200437
438 install_element_ve(&cfg_ambiguous_nr_1_cmd);
439 install_element_ve(&cfg_ambiguous_nr_2_cmd);
440 install_element_ve(&cfg_ambiguous_str_1_cmd);
441 install_element_ve(&cfg_ambiguous_str_2_cmd);
442}
443
444void test_is_cmd_ambiguous()
445{
446 struct vty *vty;
447 struct vty_test test;
448
449 printf("Going to test is_cmd_ambiguous()\n");
450 vty = create_test_vty(&test);
451
452 OSMO_ASSERT(do_vty_command(vty, "ambiguous_nr") == CMD_SUCCESS);
453 OSMO_ASSERT(do_vty_command(vty, "ambiguous_nr 23") == CMD_SUCCESS);
454 OSMO_ASSERT(do_vty_command(vty, "ambiguous_nr 23 keyword") == CMD_SUCCESS);
455
456 OSMO_ASSERT(do_vty_command(vty, "ambiguous_str") == CMD_SUCCESS);
457 OSMO_ASSERT(do_vty_command(vty, "ambiguous_str arg") == CMD_SUCCESS);
458 OSMO_ASSERT(do_vty_command(vty, "ambiguous_str arg keyword") == CMD_SUCCESS);
459
460 destroy_test_vty(&test, vty);
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200461}
462
463static int go_parent_cb(struct vty *vty)
464{
465 /*
466 * - For the interactive VTY tests above, it is expected to bounce back to
467 * the CONFIG_NODE. Hence do so in go_parent_cb().
468 * - In the config file parsing tests, setting vty->node in go_parent_cb() has no
469 * effect, because we will subsequently pop a parent node from the parent stack
470 * and override to go to the node that was recorded as the actual parent.
471 */
472 vty->node = CONFIG_NODE;
473 vty->index = NULL;
474 return 0;
475}
476
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200477int main(int argc, char **argv)
478{
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100479 struct vty_app_info vty_info = {
480 .name = "VtyTest",
481 .version = 0,
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200482 .go_parent_cb = go_parent_cb,
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100483 .is_config_node = NULL,
484 };
485
486 const struct log_info_cat default_categories[] = {};
487
488 const struct log_info log_info = {
489 .cat = default_categories,
490 .num_cat = ARRAY_SIZE(default_categories),
491 };
Neels Hofmeyra829b452018-04-05 03:02:35 +0200492 void *stats_ctx;
493
494 ctx = talloc_named_const(NULL, 0, "stats test context");
495 stats_ctx = talloc_named_const(ctx, 1, "stats test context");
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100496
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200497 osmo_signal_register_handler(SS_L_VTY, vty_event_cb, NULL);
498
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100499 /* Fake logging. */
Neels Hofmeyra829b452018-04-05 03:02:35 +0200500 osmo_init_logging2(ctx, &log_info);
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100501
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100502 /* Init stats */
503 osmo_stats_init(stats_ctx);
504
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100505 vty_init(&vty_info);
506
507 /* Setup VTY commands */
Maxc65c5b42017-03-15 13:20:23 +0100508 logging_vty_add_cmds();
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100509 osmo_stats_vty_add_cmds();
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100510
Neels Hofmeyrb022c862017-09-20 01:49:11 +0200511 test_vty_add_cmds();
512
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200513 test_cmd_string_from_valstr();
Jacob Erlbeck0c987bd2013-09-06 16:52:00 +0200514 test_node_tree_structure();
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100515 test_stats_vty();
Neels Hofmeyr4a31ffa2017-09-07 03:08:06 +0200516 test_exit_by_indent("ok.cfg", 0);
517 test_exit_by_indent("ok_more_spaces.cfg", 0);
518 test_exit_by_indent("ok_tabs.cfg", 0);
519 test_exit_by_indent("ok_tabs_and_spaces.cfg", 0);
520 test_exit_by_indent("ok_ignore_comment.cfg", 0);
521 test_exit_by_indent("ok_ignore_blank.cfg", 0);
522 test_exit_by_indent("fail_not_de-indented.cfg", -EINVAL);
523 test_exit_by_indent("fail_too_much_indent.cfg", -EINVAL);
524 test_exit_by_indent("fail_tabs_and_spaces.cfg", -EINVAL);
525 test_exit_by_indent("ok_indented_root.cfg", 0);
Neels Hofmeyr43063632017-09-19 23:54:01 +0200526 test_exit_by_indent("ok_empty_parent.cfg", 0);
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100527
Neels Hofmeyr5314c512018-07-09 23:22:21 +0200528 test_is_cmd_ambiguous();
529
Jacob Erlbeckbe37fb72015-11-03 15:21:34 +0100530 /* Leak check */
531 OSMO_ASSERT(talloc_total_blocks(stats_ctx) == 1);
Jacob Erlbeckca6602f2015-11-03 13:47:11 +0100532
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200533 printf("All tests passed\n");
534
535 return 0;
536}