2013-08-06 12:29:14 +00:00
|
|
|
/* (C) 2013 by Jacob Erlbeck <jerlbeck@sysmocom.de>
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* This program is iree software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-09-06 14:52:00 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
2013-09-10 09:17:46 +00:00
|
|
|
#include <osmocom/core/application.h>
|
2013-08-06 12:29:14 +00:00
|
|
|
#include <osmocom/core/talloc.h>
|
|
|
|
#include <osmocom/core/logging.h>
|
|
|
|
#include <osmocom/core/utils.h>
|
2013-10-10 18:21:33 +00:00
|
|
|
#include <osmocom/core/signal.h>
|
2013-08-06 12:29:14 +00:00
|
|
|
#include <osmocom/vty/misc.h>
|
2013-09-06 14:52:00 +00:00
|
|
|
#include <osmocom/vty/vty.h>
|
|
|
|
#include <osmocom/vty/command.h>
|
|
|
|
#include <osmocom/vty/buffer.h>
|
2013-09-10 09:17:46 +00:00
|
|
|
#include <osmocom/vty/logging.h>
|
2013-09-06 14:52:00 +00:00
|
|
|
|
|
|
|
static enum event last_vty_connection_event = -1;
|
2013-08-06 12:29:14 +00:00
|
|
|
|
|
|
|
static void test_cmd_string_from_valstr(void)
|
|
|
|
{
|
|
|
|
char *cmd;
|
|
|
|
const struct value_string printf_seq_vs[] = {
|
|
|
|
{ .value = 42, .str = "[foo%s%s%s%s%s]"},
|
|
|
|
{ .value = 43, .str = "[bar%s%s%s%s%s]"},
|
|
|
|
{ .value = 0, .str = NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
printf("Going to test vty_cmd_string_from_valstr()\n");
|
|
|
|
|
|
|
|
/* check against character strings that could break printf */
|
|
|
|
|
|
|
|
cmd = vty_cmd_string_from_valstr (NULL, printf_seq_vs, "[prefix%s%s%s%s%s]", "[sep%s%s%s%s%s]", "[end%s%s%s%s%s]", 1);
|
|
|
|
printf ("Tested with %%s-strings, resulting cmd = '%s'\n", cmd);
|
|
|
|
talloc_free (cmd);
|
|
|
|
}
|
|
|
|
|
2013-09-06 14:52:00 +00:00
|
|
|
static int do_vty_command(struct vty *vty, const char *cmd)
|
|
|
|
{
|
|
|
|
vector vline;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
printf("Going to execute '%s'\n", cmd);
|
|
|
|
vline = cmd_make_strvec(cmd);
|
|
|
|
ret = cmd_execute_command(vline, vty, NULL, 0);
|
|
|
|
cmd_free_strvec(vline);
|
|
|
|
printf("Returned: %d, Current node: %d '%s'\n", ret, vty->node, cmd_prompt(vty->node));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-10 18:21:33 +00:00
|
|
|
/* Handle the events from telnet_interface.c */
|
|
|
|
static int vty_event_cb(unsigned int subsys, unsigned int signal,
|
|
|
|
void *handler_data, void *_signal_data)
|
2013-09-06 14:52:00 +00:00
|
|
|
{
|
2013-10-10 18:21:33 +00:00
|
|
|
struct vty_signal_data *signal_data;
|
2013-09-06 14:52:00 +00:00
|
|
|
|
2013-10-10 18:21:33 +00:00
|
|
|
if (subsys != SS_L_VTY)
|
|
|
|
return 0;
|
|
|
|
if (signal != S_VTY_EVENT)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
signal_data = _signal_data;
|
|
|
|
last_vty_connection_event = signal_data->event;
|
|
|
|
|
|
|
|
fprintf(stderr, "Got VTY event: %d\n", signal_data->event);
|
|
|
|
return 0;
|
2013-09-06 14:52:00 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 12:47:11 +00:00
|
|
|
struct vty_test {
|
2013-09-06 14:52:00 +00:00
|
|
|
int sock[2];
|
2015-11-03 12:47:11 +00:00
|
|
|
};
|
2013-09-06 14:52:00 +00:00
|
|
|
|
2015-11-03 12:47:11 +00:00
|
|
|
static struct vty* create_test_vty(struct vty_test *data)
|
|
|
|
{
|
|
|
|
struct vty *vty;
|
|
|
|
/* Fake connection. */
|
|
|
|
socketpair(AF_UNIX, SOCK_STREAM, 0, data->sock);
|
2013-09-06 14:52:00 +00:00
|
|
|
|
2015-11-03 12:47:11 +00:00
|
|
|
vty = vty_create(data->sock[0], NULL);
|
|
|
|
OSMO_ASSERT(vty != NULL);
|
|
|
|
OSMO_ASSERT(vty->status != VTY_CLOSE);
|
2013-09-06 14:52:00 +00:00
|
|
|
|
2015-11-03 12:47:11 +00:00
|
|
|
return vty;
|
|
|
|
}
|
2013-09-06 14:52:00 +00:00
|
|
|
|
2015-11-03 12:47:11 +00:00
|
|
|
static void destroy_test_vty(struct vty_test *data, struct vty *vty)
|
|
|
|
{
|
|
|
|
vty_close(vty);
|
|
|
|
OSMO_ASSERT(last_vty_connection_event == VTY_CLOSED);
|
|
|
|
}
|
2013-09-06 14:52:00 +00:00
|
|
|
|
2015-11-03 12:47:11 +00:00
|
|
|
static void test_node_tree_structure(void)
|
|
|
|
{
|
|
|
|
struct vty_test test;
|
|
|
|
struct vty *vty;
|
2013-09-06 14:52:00 +00:00
|
|
|
|
2015-11-03 12:47:11 +00:00
|
|
|
printf("Going to test VTY node tree structure\n");
|
|
|
|
vty = create_test_vty(&test);
|
2013-09-06 14:52:00 +00:00
|
|
|
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "enable") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == ENABLE_NODE);
|
|
|
|
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == CONFIG_NODE);
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == ENABLE_NODE);
|
|
|
|
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == CONFIG_NODE);
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == ENABLE_NODE);
|
|
|
|
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == CONFIG_NODE);
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "log stderr") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == CFG_LOG_NODE);
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == CONFIG_NODE);
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "log stderr") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == CFG_LOG_NODE);
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == ENABLE_NODE);
|
|
|
|
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == CONFIG_NODE);
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "line vty") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == VTY_NODE);
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == CONFIG_NODE);
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "line vty") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == VTY_NODE);
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == ENABLE_NODE);
|
|
|
|
|
|
|
|
|
|
|
|
/* Check searching the parents nodes for matching commands. */
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "configure terminal") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == CONFIG_NODE);
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "log stderr") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == CFG_LOG_NODE);
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "line vty") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == VTY_NODE);
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "log stderr") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == CFG_LOG_NODE);
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "end") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == ENABLE_NODE);
|
|
|
|
|
|
|
|
/* Check for final 'exit' (connection close). */
|
|
|
|
OSMO_ASSERT(do_vty_command(vty, "exit") == CMD_SUCCESS);
|
|
|
|
OSMO_ASSERT(vty->node == ENABLE_NODE);
|
|
|
|
OSMO_ASSERT(vty->status == VTY_CLOSE);
|
|
|
|
|
2015-11-03 12:47:11 +00:00
|
|
|
destroy_test_vty(&test, vty);
|
2013-09-06 14:52:00 +00:00
|
|
|
}
|
|
|
|
|
2013-08-06 12:29:14 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2015-11-03 12:47:11 +00:00
|
|
|
struct vty_app_info vty_info = {
|
|
|
|
.name = "VtyTest",
|
|
|
|
.version = 0,
|
|
|
|
.go_parent_cb = NULL,
|
|
|
|
.is_config_node = NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct log_info_cat default_categories[] = {};
|
|
|
|
|
|
|
|
const struct log_info log_info = {
|
|
|
|
.cat = default_categories,
|
|
|
|
.num_cat = ARRAY_SIZE(default_categories),
|
|
|
|
};
|
2013-10-10 18:21:33 +00:00
|
|
|
osmo_signal_register_handler(SS_L_VTY, vty_event_cb, NULL);
|
|
|
|
|
2015-11-03 12:47:11 +00:00
|
|
|
/* Fake logging. */
|
|
|
|
osmo_init_logging(&log_info);
|
|
|
|
|
|
|
|
vty_init(&vty_info);
|
|
|
|
|
|
|
|
/* Setup VTY commands */
|
|
|
|
logging_vty_add_cmds(&log_info);
|
|
|
|
|
2013-08-06 12:29:14 +00:00
|
|
|
test_cmd_string_from_valstr();
|
2013-09-06 14:52:00 +00:00
|
|
|
test_node_tree_structure();
|
2015-11-03 12:47:11 +00:00
|
|
|
|
2013-08-06 12:29:14 +00:00
|
|
|
printf("All tests passed\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|