From 99c5160cb5a1a169f86d334017efde4cef23ead8 Mon Sep 17 00:00:00 2001 From: NoName115 Date: Wed, 25 Oct 2017 14:20:52 +0200 Subject: config: glob support for include with test Signed-off-by: NoName115 Reviewed-by: Andreas Schneider --- src/config.c | 30 +++++++++++++++++++++++++++++- tests/unittests/torture_config.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/config.c b/src/config.c index 330fb7ee..cedf34e4 100644 --- a/src/config.c +++ b/src/config.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "libssh/priv.h" #include "libssh/session.h" @@ -316,6 +317,33 @@ static void local_parse_file(ssh_session session, const char *filename, int *par return; } +static void local_parse_glob(ssh_session session, + const char *fileglob, + int *parsing, + int seen[]) +{ + glob_t globbuf = {0}; + int rt; + u_int i; + + rt = glob(fileglob, GLOB_TILDE, NULL, &globbuf); + if (rt == GLOB_NOMATCH) { + globfree(&globbuf); + return; + } else if (rt != 0) { + SSH_LOG(SSH_LOG_RARE, "Glob error: %s", + fileglob); + globfree(&globbuf); + return; + } + + for (i = 0; i < globbuf.gl_pathc; i++) { + local_parse_file(session, globbuf.gl_pathv[i], parsing, seen); + } + + globfree(&globbuf); +} + static int ssh_config_parse_line(ssh_session session, const char *line, unsigned int count, int *parsing, int seen[]) { enum ssh_config_opcode_e opcode; @@ -361,7 +389,7 @@ static int ssh_config_parse_line(ssh_session session, const char *line, p = ssh_config_get_str_tok(&s, NULL); if (p && *parsing) { - local_parse_file(session, p, parsing, seen); + local_parse_glob(session, p, parsing, seen); } break; case SOC_HOST: { diff --git a/tests/unittests/torture_config.c b/tests/unittests/torture_config.c index a0b40239..8ca097c7 100644 --- a/tests/unittests/torture_config.c +++ b/tests/unittests/torture_config.c @@ -10,6 +10,9 @@ #define LIBSSH_TESTCONFIG2 "libssh_testconfig2.tmp" #define LIBSSH_TESTCONFIG3 "libssh_testconfig3.tmp" #define LIBSSH_TESTCONFIG4 "libssh_testconfig4.tmp" +#define LIBSSH_TESTCONFIG5 "libssh_testconfig5.tmp" +#define LIBSSH_TESTCONFIG6 "libssh_testconfig6.tmp" +#define LIBSSH_TESTCONFIGGLOB "libssh_testc*[36].tmp" #define USERNAME "testuser" #define PROXYCMD "ssh -q -W %h:%p gateway.example.com" @@ -25,6 +28,8 @@ static int setup_config_files(void **state) unlink(LIBSSH_TESTCONFIG2); unlink(LIBSSH_TESTCONFIG3); unlink(LIBSSH_TESTCONFIG4); + unlink(LIBSSH_TESTCONFIG5); + unlink(LIBSSH_TESTCONFIG6); torture_write_file(LIBSSH_TESTCONFIG1, "User "USERNAME"\nInclude "LIBSSH_TESTCONFIG2"\n\n"); @@ -40,6 +45,13 @@ static int setup_config_files(void **state) torture_write_file(LIBSSH_TESTCONFIG4, "Port 123\nPort 456\n"); + /* Testing glob include */ + torture_write_file(LIBSSH_TESTCONFIG5, + "User "USERNAME"\nInclude "LIBSSH_TESTCONFIGGLOB"\n\n"); + + torture_write_file(LIBSSH_TESTCONFIG6, + "ProxyCommand "PROXYCMD"\n\n"); + session = ssh_new(); *state = session; @@ -52,6 +64,8 @@ static int teardown(void **state) unlink(LIBSSH_TESTCONFIG2); unlink(LIBSSH_TESTCONFIG3); unlink(LIBSSH_TESTCONFIG4); + unlink(LIBSSH_TESTCONFIG5); + unlink(LIBSSH_TESTCONFIG6); ssh_free(*state); @@ -105,6 +119,29 @@ static void torture_config_double_ports(void **state) { assert_true(ret == 0); } +static void torture_config_glob(void **state) { + ssh_session session = *state; + int ret; + char *v; + + ret = ssh_config_parse_file(session, LIBSSH_TESTCONFIG5); + assert_true(ret == 0); + + /* Test the variable presence */ + + ret = ssh_options_get(session, SSH_OPTIONS_PROXYCOMMAND, &v); + assert_true(ret == 0); + + assert_string_equal(v, PROXYCMD); + ssh_string_free_char(v); + + ret = ssh_options_get(session, SSH_OPTIONS_IDENTITY, &v); + assert_true(ret == 0); + + assert_string_equal(v, ID_FILE); + ssh_string_free_char(v); +} + int torture_run_tests(void) { int rc; struct CMUnitTest tests[] = { @@ -114,6 +151,9 @@ int torture_run_tests(void) { cmocka_unit_test_setup_teardown(torture_config_double_ports, setup_config_files, teardown), + cmocka_unit_test_setup_teardown(torture_config_glob, + setup_config_files, + teardown), }; -- cgit v1.2.3